xref: /titanic_51/usr/src/lib/libc/sparcv9/gen/strncpy.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate/*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*9a70fc3bSMark J. Nelson	.file	"strncpy.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
307c478bd9Sstevel@tonic-gate * strncpy(s1, s2)
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Copy string s2 to s1, truncating or null-padding to always copy n bytes
337c478bd9Sstevel@tonic-gate * return s1.
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program for strncpy
367c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate *	char *
397c478bd9Sstevel@tonic-gate *	strncpy(char *s1, const char *s2, size_t n)
407c478bd9Sstevel@tonic-gate *	{
417c478bd9Sstevel@tonic-gate *		char *os1 = s1;
427c478bd9Sstevel@tonic-gate *
437c478bd9Sstevel@tonic-gate *		n++;
447c478bd9Sstevel@tonic-gate *		while ((--n != 0) &&  ((*s1++ = *s2++) != '\0'))
457c478bd9Sstevel@tonic-gate *			;
467c478bd9Sstevel@tonic-gate *		if (n != 0)
477c478bd9Sstevel@tonic-gate *			while (--n != 0)
487c478bd9Sstevel@tonic-gate *				*s1++ = '\0';
497c478bd9Sstevel@tonic-gate *		return (os1);
507c478bd9Sstevel@tonic-gate *	}
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate	! strncpy works similarly to strcpy, except that n bytes of s2
567c478bd9Sstevel@tonic-gate	! are copied to s1. If a null character is reached in s2 yet more
577c478bd9Sstevel@tonic-gate	! bytes remain to be copied, strncpy will copy null bytes into
587c478bd9Sstevel@tonic-gate	! the destination string.
597c478bd9Sstevel@tonic-gate	!
607c478bd9Sstevel@tonic-gate	! This implementation works by first aligning the src ptr and
617c478bd9Sstevel@tonic-gate	! performing small copies until it is aligned.  Then, the string
627c478bd9Sstevel@tonic-gate	! is copied based upon destination alignment.  (byte, half-word,
637c478bd9Sstevel@tonic-gate	! word, etc.)
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate	ENTRY(strncpy)
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate	.align 32
687c478bd9Sstevel@tonic-gate	nop				! pad to align loop on 16-byte boundary
697c478bd9Sstevel@tonic-gate	subcc	%g0, %o2, %g4		! n = -n, n == 0 ?
707c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! n == 0, done
717c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o3		! src = src + n
727c478bd9Sstevel@tonic-gate	andcc	%o1, 7, %o4		! dword aligned ?
737c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .dwordaligned	! yup
747c478bd9Sstevel@tonic-gate	add	%o0, %o2, %o2		! dst = dst + n
757c478bd9Sstevel@tonic-gate	sub	%o4, 8, %o4		! bytes until src aligned
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate.alignsrc:
787c478bd9Sstevel@tonic-gate	ldub	[%o3 + %g4], %o1	! src[]
797c478bd9Sstevel@tonic-gate	stb	%o1, [%o2 + %g4]	! dst[] = src[]
807c478bd9Sstevel@tonic-gate	addcc	%g4, 1, %g4		! src++, dst++, n--
817c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! n == 0, done
827c478bd9Sstevel@tonic-gate	tst	%o1			! end of src reached (null byte) ?
837c478bd9Sstevel@tonic-gate	bz,a	%ncc, .bytepad		! yes, at least one byte to pad here
847c478bd9Sstevel@tonic-gate	add 	%o2, %g4, %o3		! need single dest pointer for fill
857c478bd9Sstevel@tonic-gate	addcc	%o4, 1, %o4		! src aligned now?
867c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .alignsrc		! no, copy another byte
877c478bd9Sstevel@tonic-gate	nop				! pad
887c478bd9Sstevel@tonic-gate	nop				! pad
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate.dwordaligned:
917c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
927c478bd9Sstevel@tonic-gate	add	%o2, %g4, %g5		! dst
937c478bd9Sstevel@tonic-gate	or	%o4, %lo(0x01010101),%o4!  finish loading magic1
947c478bd9Sstevel@tonic-gate	and	%g5, 3, %g1		! dst<1:0> to examine offset
957c478bd9Sstevel@tonic-gate	sllx	%o4, 32, %o1		! spread magic1
967c478bd9Sstevel@tonic-gate	cmp	%g1, 1			! dst offset of 1 or 5
977c478bd9Sstevel@tonic-gate	or	%o4, %o1, %o4		!   to all 64 bits
987c478bd9Sstevel@tonic-gate	sub	%o2, 8, %o2		! adjust for dest pre-incr in cpy loops
997c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
1007c478bd9Sstevel@tonic-gate	sllx	%o4, 7, %o5		!  Alan Mycroft's magic2
1017c478bd9Sstevel@tonic-gate	cmp	%g1, 3			! dst offset of 3 or 7
1027c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
1037c478bd9Sstevel@tonic-gate	cmp	%g1, 2			! dst halfword aligned ?
1047c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storehalfword	! yup, store half-word wise
1057c478bd9Sstevel@tonic-gate	andcc	%g5, 7, %g0		! dst word aligned ?
1067c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .storeword2	! yup, store word wise
1077c478bd9Sstevel@tonic-gate	nop				! ensure loop is 16-byte aligned
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate.storedword:
1107c478bd9Sstevel@tonic-gate	ldx	[%o3 + %g4], %o1	! src dword
1117c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! n += 8, src += 8, dst += 8
1127c478bd9Sstevel@tonic-gate	bcs,pn	%ncc,.lastword		! if counter wraps, last word
1137c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~dword & 0x8080808080808080
1147c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g5		! dword - 0x0101010101010101
1157c478bd9Sstevel@tonic-gate	andcc	%g5, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
1167c478bd9Sstevel@tonic-gate	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
1177c478bd9Sstevel@tonic-gate	stx	%o1, [%o2 + %g4]	! store word to dst (address pre-incremented)
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate	! n has not expired, but src is at the end. we need to push out the
1207c478bd9Sstevel@tonic-gate	! remaining src bytes and then start padding with null bytes
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate.zerobyte:
1237c478bd9Sstevel@tonic-gate	add	%o2, %g4, %o3		! pointer to dest string
1247c478bd9Sstevel@tonic-gate	srlx	%o1, 56, %g1		! first byte
1257c478bd9Sstevel@tonic-gate	stb	%g1, [%o3]		! store it
1267c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1277c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1287c478bd9Sstevel@tonic-gate	srlx	%o1, 48, %g1		! second byte
1297c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 1]		! store it
1307c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1317c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1327c478bd9Sstevel@tonic-gate	srlx	%o1, 40, %g1		! third byte
1337c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 2]		! store it
1347c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1357c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1367c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %g1		! fourth byte
1377c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 3]		! store it
1387c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1397c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1407c478bd9Sstevel@tonic-gate	srlx	%o1, 24, %g1		! fifth byte
1417c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 4]		! store it
1427c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1437c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1447c478bd9Sstevel@tonic-gate	srlx	%o1, 16, %g1		! sixth byte
1457c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 5]		! store it
1467c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1477c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1487c478bd9Sstevel@tonic-gate	srlx	%o1, 8, %g1		! seventh byte
1497c478bd9Sstevel@tonic-gate	stb	%g1, [%o3 + 6]		! store it
1507c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1517c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
1527c478bd9Sstevel@tonic-gate	stb	%o1, [%o3 + 7]		! store eighth byte
1537c478bd9Sstevel@tonic-gate	addcc	%g4, 16, %g0		! number of pad bytes < 16 ?
1547c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .bytepad		! yes, do simple byte wise fill
1557c478bd9Sstevel@tonic-gate	add	%o3, 8, %o3		! dst += 8
1567c478bd9Sstevel@tonic-gate	andcc	%o3, 7, %o4		! dst offset relative to dword boundary
1577c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .fillaligned	! dst already dword aligned
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate	! here there is a least one more byte to zero out: otherwise we would
1607c478bd9Sstevel@tonic-gate	! have exited through label .lastword
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate	sub	%o4, 8, %o4		! bytes to align dst to dword boundary
1637c478bd9Sstevel@tonic-gate.makealigned:
1647c478bd9Sstevel@tonic-gate	stb	%g0, [%o3]		! dst[] = 0
1657c478bd9Sstevel@tonic-gate	addcc	%g4, 1, %g4		! n--
1667c478bd9Sstevel@tonic-gate	bz,pt	%ncc, .done		! n == 0, we are done
1677c478bd9Sstevel@tonic-gate	addcc	%o4, 1, %o4		! any more byte needed to align
1687c478bd9Sstevel@tonic-gate	bnz,pt	%ncc, .makealigned	! yup, pad another byte
1697c478bd9Sstevel@tonic-gate	add	%o3, 1, %o3		! dst++
1707c478bd9Sstevel@tonic-gate	nop				! pad to align copy loop below
1717c478bd9Sstevel@tonic-gate	nop				! pad to align copy loop below
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate	! here we know that there at least another 8 bytes to pad, since
1747c478bd9Sstevel@tonic-gate	! we don't get here unless there were >= 16 bytes to pad to begin
1757c478bd9Sstevel@tonic-gate	! with, and we have padded at most 7 bytes suring dst aligning
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate.fillaligned:
1787c478bd9Sstevel@tonic-gate	add	%g4, 7, %o2		! round up to next dword boundary
1797c478bd9Sstevel@tonic-gate	and	%o2, -8, %o4		! pointer to next dword boundary
1807c478bd9Sstevel@tonic-gate	and	%o2, 8, %o2		! dword count odd ? 8 : 0
1817c478bd9Sstevel@tonic-gate	stx	%g0, [%o3]		! store first dword
1827c478bd9Sstevel@tonic-gate	addcc	%o4, %o2, %o4		! dword count == 1 ?
1837c478bd9Sstevel@tonic-gate	add	%g4, %o2, %g4		! if dword count odd, n -= 8
1847c478bd9Sstevel@tonic-gate	bz,pt	%ncc, .bytepad		! if dword count == 1, pad leftover bytes
1857c478bd9Sstevel@tonic-gate	add	%o3, %o2, %o3		! bump dst if dword count odd
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate.filldword:
1887c478bd9Sstevel@tonic-gate	addcc	%o4, 16, %o4		! count -= 16
1897c478bd9Sstevel@tonic-gate	stx	%g0, [%o3]		! dst[n] = 0
1907c478bd9Sstevel@tonic-gate	stx	%g0, [%o3 + 8]		! dst[n+8] = 0
1917c478bd9Sstevel@tonic-gate	add	%o3, 16, %o3		! dst += 16
1927c478bd9Sstevel@tonic-gate	bcc,pt	%ncc, .filldword	! fill dwords until count == 0
1937c478bd9Sstevel@tonic-gate	addcc	%g4, 16, %g4		! n -= 16
1947c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! if n == 0, we are done
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate.bytepad:
1977c478bd9Sstevel@tonic-gate	and	%g4, 1, %o2		! byte count odd ? 1 : 0
1987c478bd9Sstevel@tonic-gate	stb	%g0, [%o3]		! store first byte
1997c478bd9Sstevel@tonic-gate	addcc	%g4, %o2, %g4		! byte count == 1 ?
2007c478bd9Sstevel@tonic-gate	bz,pt	%ncc, .done		! yup, we are done
2017c478bd9Sstevel@tonic-gate	add	%o3, %o2, %o3		! bump pointer if odd
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate.fillbyte:
2047c478bd9Sstevel@tonic-gate	addcc	%g4, 2, %g4		! n -= 2
2057c478bd9Sstevel@tonic-gate	stb	%g0, [%o3]		! dst[n] = 0
2067c478bd9Sstevel@tonic-gate	stb	%g0, [%o3 + 1]		! dst[n+1] = 0
2077c478bd9Sstevel@tonic-gate	bnz,pt	%ncc, .fillbyte		! fill until n == 0
2087c478bd9Sstevel@tonic-gate	add	%o3, 2, %o3		! dst += 2
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate.done:
2117c478bd9Sstevel@tonic-gate	retl				! done
2127c478bd9Sstevel@tonic-gate	nop				! pad to align loops below
2137c478bd9Sstevel@tonic-gate	nop				! pad to align loops below
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate	! this is the last word. It may contain null bytes. store bytes
2167c478bd9Sstevel@tonic-gate	! until n == 0. if null byte encountered, continue
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate.lastword:
2197c478bd9Sstevel@tonic-gate	sub	%g4, 8, %g4		! undo counter pre-increment
2207c478bd9Sstevel@tonic-gate	add	%o2, 8, %o2		! adjust dst for counter un-bumping
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate	srlx	%o1, 56, %g1		! first byte
2237c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2247c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2257c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2267c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2277c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2287c478bd9Sstevel@tonic-gate	srlx	%o1, 48, %g1		! second byte
2297c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2307c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2317c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2327c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2337c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2347c478bd9Sstevel@tonic-gate	srlx	%o1, 40, %g1		! third byte
2357c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2367c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2377c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2387c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2397c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2407c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %g1		! fourth byte
2417c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2427c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2437c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2447c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2457c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2467c478bd9Sstevel@tonic-gate	srlx	%o1, 24, %g1		! fifth byte
2477c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2487c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2497c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2507c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2517c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2527c478bd9Sstevel@tonic-gate	srlx	%o1, 16, %g1		! sixth byte
2537c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2547c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2557c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2567c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2577c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2587c478bd9Sstevel@tonic-gate	srlx	%o1, 8, %g1		! seventh byte
2597c478bd9Sstevel@tonic-gate	stb	%g1, [%o2 + %g4]	! store it
2607c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2617c478bd9Sstevel@tonic-gate	bz	.done			! if n == 0, we're done
2627c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2637c478bd9Sstevel@tonic-gate	movz	%ncc, %g0, %o1		! if so, start padding with null bytes
2647c478bd9Sstevel@tonic-gate	ba	.done			! here n must be zero, we are done
2657c478bd9Sstevel@tonic-gate	stb	%o1, [%o2 + %g4]	! store eigth byte
2667c478bd9Sstevel@tonic-gate	nop				! pad to align loops below
2677c478bd9Sstevel@tonic-gate	nop				! pad to align loops below
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate.storebyte1421:
2707c478bd9Sstevel@tonic-gate	ldx	[%o3 + %g4], %o1	! x = src[]
2717c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2727c478bd9Sstevel@tonic-gate	bcs,pn	%ncc,.lastword		! if counter wraps, last word
2737c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
2747c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g5		! x - 0x0101010101010101
2757c478bd9Sstevel@tonic-gate	andcc	%g5, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2767c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! end of src found, may need to pad
2777c478bd9Sstevel@tonic-gate	add	%o2, %g4, %g5		! dst (in pointer form)
2787c478bd9Sstevel@tonic-gate	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; word aligned now
2797c478bd9Sstevel@tonic-gate	stb	%g1, [%g5]		! store first byte
2807c478bd9Sstevel@tonic-gate	srlx	%o1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
2817c478bd9Sstevel@tonic-gate	stw	%g1, [%g5 + 1]		! store bytes 2, 3, 4, 5
2827c478bd9Sstevel@tonic-gate	srlx	%o1, 8, %g1		! %g1<15:0> = bytes 6, 7
2837c478bd9Sstevel@tonic-gate	sth	%g1, [%g5 + 5]		! store bytes 6, 7
2847c478bd9Sstevel@tonic-gate	ba	.storebyte1421		! next dword
2857c478bd9Sstevel@tonic-gate	stb	%o1, [%g5 + 7]		! store eigth byte
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate.storebyte1241:
2887c478bd9Sstevel@tonic-gate	ldx	[%o3 + %g4], %o1	! x = src[]
2897c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2907c478bd9Sstevel@tonic-gate	bcs,pn	%ncc,.lastword		! if counter wraps, last word
2917c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
2927c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g5		! x - 0x0101010101010101
2937c478bd9Sstevel@tonic-gate	andcc	%g5, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2947c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
2957c478bd9Sstevel@tonic-gate	add	%o2, %g4, %g5		! dst (in pointer form)
2967c478bd9Sstevel@tonic-gate	srlx	%o1, 56, %g1		! %g1<7:0> = first byte; half-word aligned now
2977c478bd9Sstevel@tonic-gate	stb	%g1, [%g5]		! store first byte
2987c478bd9Sstevel@tonic-gate	srlx	%o1, 40, %g1		! %g1<15:0> = bytes 2, 3
2997c478bd9Sstevel@tonic-gate	sth	%g1, [%g5 + 1]		! store bytes 2, 3
3007c478bd9Sstevel@tonic-gate	srlx	%o1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
3017c478bd9Sstevel@tonic-gate	stw	%g1, [%g5 + 3]		! store bytes 4, 5, 6, 7
3027c478bd9Sstevel@tonic-gate	ba	.storebyte1241		! next dword
3037c478bd9Sstevel@tonic-gate	stb	%o1, [%g5 + 7]		! store eigth byte
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate.storehalfword:
3067c478bd9Sstevel@tonic-gate	ldx	[%o3 + %g4], %o1	! x = src[]
3077c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
3087c478bd9Sstevel@tonic-gate	bcs,pn	%ncc,.lastword		! if counter wraps, last word
3097c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
3107c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g5		! x - 0x0101010101010101
3117c478bd9Sstevel@tonic-gate	andcc	%g5, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3127c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3137c478bd9Sstevel@tonic-gate	add	%o2, %g4, %g5		! dst (in pointer form)
3147c478bd9Sstevel@tonic-gate	srlx	%o1, 48, %g1		! %g1<15:0> = bytes 1, 2; word aligned now
3157c478bd9Sstevel@tonic-gate	sth	%g1, [%g5]		! store bytes 1, 2
3167c478bd9Sstevel@tonic-gate	srlx	%o1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6
3177c478bd9Sstevel@tonic-gate	stw	%g1, [%g5 + 2]		! store bytes 3, 4, 5, 6
3187c478bd9Sstevel@tonic-gate	ba	.storehalfword		! next dword
3197c478bd9Sstevel@tonic-gate	sth	%o1, [%g5 + 6]		! store bytes 7, 8
3207c478bd9Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3217c478bd9Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate.storeword2:
3247c478bd9Sstevel@tonic-gate	ldx	[%o3 + %g4], %o1	! x = src[]
3257c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
3267c478bd9Sstevel@tonic-gate	bcs,pn	%ncc,.lastword		! if counter wraps, last word
3277c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %g1		! ~x & 0x8080808080808080
3287c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g5		! x - 0x0101010101010101
3297c478bd9Sstevel@tonic-gate	andcc	%g5, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3307c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3317c478bd9Sstevel@tonic-gate	add	%o2, %g4, %g5		! dst (in pointer form)
3327c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %g1		! %g1<31:0> = bytes 1, 2, 3, 4
3337c478bd9Sstevel@tonic-gate	stw	%g1, [%g5]		! store bytes 1, 2, 3, 4
3347c478bd9Sstevel@tonic-gate	ba	.storeword2		! next dword
3357c478bd9Sstevel@tonic-gate	stw	%o1, [%g5 + 4]		! store bytes 5, 6, 7, 8
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate	! do not remove these pads, loop above may slow down otherwise
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate	nop				! pad
3407c478bd9Sstevel@tonic-gate	nop				! pad
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate	SET_SIZE(strncpy)
343