xref: /onnv-gate/usr/src/lib/libc/sparcv9/gen/strlcpy.s (revision 7298:b69e27387f74)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate/*
236812Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*7298SMark.J.Nelson@Sun.COM	.file	"strlcpy.s"
280Sstevel@tonic-gate
290Sstevel@tonic-gate/*
300Sstevel@tonic-gate * The strlcpy() function copies at most dstsize-1 characters
310Sstevel@tonic-gate * (dstsize being the size of the string buffer dst) from src
320Sstevel@tonic-gate * to dst, truncating src if necessary. The result is always
330Sstevel@tonic-gate * null-terminated.  The function returns strlen(src). Buffer
340Sstevel@tonic-gate * overflow can be checked as follows:
350Sstevel@tonic-gate *
360Sstevel@tonic-gate *   if (strlcpy(dst, src, dstsize) >= dstsize)
370Sstevel@tonic-gate *           return -1;
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate#include <sys/asm_linkage.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate	! strlcpy implementation is similar to that of strcpy, except
430Sstevel@tonic-gate	! in this case, the maximum size of the detination must be
440Sstevel@tonic-gate	! tracked since it bounds our maximum copy size.  However,
450Sstevel@tonic-gate	! we must still continue to check for zero since the routine
460Sstevel@tonic-gate	! is expected to null-terminate any string that is within
470Sstevel@tonic-gate	! the dest size bound.
480Sstevel@tonic-gate	!
490Sstevel@tonic-gate	! this method starts by checking for and arranging source alignment.
500Sstevel@tonic-gate	! Once this has occurred, we copy based upon destination alignment.
510Sstevel@tonic-gate	! This is either by xword, word, halfword, or byte.  As this occurs, we
520Sstevel@tonic-gate	! check for a zero-byte.  If one is found, we branch to a method
530Sstevel@tonic-gate	! which checks for the exact location of a zero-byte within a
540Sstevel@tonic-gate	! larger xword/word/half-word quantity.
550Sstevel@tonic-gate
560Sstevel@tonic-gate
570Sstevel@tonic-gate	ENTRY(strlcpy)
580Sstevel@tonic-gate
590Sstevel@tonic-gate	.align 32
600Sstevel@tonic-gate
610Sstevel@tonic-gate	save	%sp, -SA(WINDOWSIZE), %sp
620Sstevel@tonic-gate	subcc	%g0, %i2, %g4		! n = -n, n == 0 ?
630Sstevel@tonic-gate	bz,pn	%ncc, .getstrlen	! n == 0, must determine strlen
640Sstevel@tonic-gate	add	%i1, %i2, %i3		! src = src + n
650Sstevel@tonic-gate	andcc	%i1, 7, %i4		! src dword aligned ?
660Sstevel@tonic-gate	bz,pn	%ncc, .dwordaligned	! yup
670Sstevel@tonic-gate	add	%i0, %i2, %i2		! dst = dst + n
680Sstevel@tonic-gate	sub	%i4, 8, %i4		! bytes until src aligned
690Sstevel@tonic-gate
700Sstevel@tonic-gate.alignsrc:
710Sstevel@tonic-gate	ldub	[%i3 + %g4], %l1	! src[]
720Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached (null byte) ?
730Sstevel@tonic-gate	stub	%l1, [%i2 + %g4]	! dst[] = src[]
740Sstevel@tonic-gate	bz,a	%ncc, .done		! yes, done
750Sstevel@tonic-gate	add 	%i2, %g4, %i2		! need single dest pointer for strlen
760Sstevel@tonic-gate	addcc	%g4, 1, %g4		! src++, dst++, n--
770Sstevel@tonic-gate	bz,pn	%ncc, .forcenullunalign	! n == 0, force null byte, compute len
780Sstevel@tonic-gate	addcc	%i4, 1, %i4		! src aligned now?
790Sstevel@tonic-gate	bnz,a	%ncc, .alignsrc		! no, copy another byte
800Sstevel@tonic-gate	nop				! pad
810Sstevel@tonic-gate
820Sstevel@tonic-gate.dwordaligned:
830Sstevel@tonic-gate	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
840Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst
850Sstevel@tonic-gate	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
860Sstevel@tonic-gate	and	%l0, 3, %g1		! dst<1:0> to examine offset
870Sstevel@tonic-gate	sllx	%i4, 32, %l1		! spread magic1
880Sstevel@tonic-gate	cmp	%g1, 1			! dst offset of 1 or 5
890Sstevel@tonic-gate	or	%i4, %l1, %i4		!   to all 64 bits
900Sstevel@tonic-gate	sub	%i2, 8, %i2		! adjust for dest pre-incr in cpy loops
910Sstevel@tonic-gate	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
920Sstevel@tonic-gate	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
930Sstevel@tonic-gate	cmp	%g1, 3			! dst offset of 3 or 7
940Sstevel@tonic-gate	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
950Sstevel@tonic-gate	cmp	%g1, 2			! dst halfword aligned ?
960Sstevel@tonic-gate	be,pn	%ncc, .storehalfword	! yup, store half-word wise
970Sstevel@tonic-gate	andcc	%l0, 7, %g0		! dst word aligned ?
980Sstevel@tonic-gate	bnz,pn	%ncc, .storeword2	! yup, store word wise
990Sstevel@tonic-gate	nop				! ensure loop is 16-byte aligned
1000Sstevel@tonic-gate	nop				! ensure loop is 16-byte aligned
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate.storedword:
1030Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! src dword
1040Sstevel@tonic-gate	addcc	%g4, 8, %g4		! n += 8, src += 8, dst += 8
1050Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
1060Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
1070Sstevel@tonic-gate	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
1080Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
1090Sstevel@tonic-gate	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
1100Sstevel@tonic-gate	stx	%l1, [%i2 + %g4]	! store word to dst (address pre-incremented)
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate	! n has not expired, but src is at the end. we need to push out the
1130Sstevel@tonic-gate	! remaining src bytes. Since strlen(dts) == strlen(src), we can
1140Sstevel@tonic-gate	! compute the return value as the difference of final dst pointer
1150Sstevel@tonic-gate	! and the pointer to the start of dst
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate.zerobyte:
1180Sstevel@tonic-gate	add	%i2, %g4, %i2		! pointer to dest string
1190Sstevel@tonic-gate	srlx	%l1, 56, %g1		! first byte
1200Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1210Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1220Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1230Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1240Sstevel@tonic-gate	srlx	%l1, 48, %g1		! second byte
1250Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1260Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1270Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1280Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1290Sstevel@tonic-gate	srlx	%l1, 40, %g1		! third byte
1300Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1310Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1320Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1330Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1340Sstevel@tonic-gate	srlx	%l1, 32, %g1		! fourth byte
1350Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1360Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1370Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1380Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1390Sstevel@tonic-gate	srlx	%l1, 24, %g1		! fifth byte
1400Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1410Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1420Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1430Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1440Sstevel@tonic-gate	srlx	%l1, 16, %g1		! sixth byte
1450Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1460Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1470Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1480Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1490Sstevel@tonic-gate	srlx	%l1, 8, %g1		! seventh byte
1500Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1510Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1520Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1530Sstevel@tonic-gate	stb	%l1, [%i2 + 1]		! store eigth byte
1540Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate.done:
1570Sstevel@tonic-gate	sub	%i2, %i0, %i0		! len = dst - orig dst
1580Sstevel@tonic-gate	ret				! subroutine done
1590Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate	! n expired, so this is the last word. It may contain null bytes.
1620Sstevel@tonic-gate	! Store bytes until n == 0. If a null byte is encountered during
1630Sstevel@tonic-gate	! processing of this last src word, we are done. Otherwise continue
1640Sstevel@tonic-gate	! to scan src until we hit the end, and compute strlen from the
1650Sstevel@tonic-gate	! difference between the pointer past the last byte of src and the
1660Sstevel@tonic-gate	! original pointer to the start of src
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate.lastword:
1690Sstevel@tonic-gate	add	%i2, %g4, %i2		! we want a single dst pointer here
1700Sstevel@tonic-gate	sub	%g4, 8, %g4		! undo counter pre-increment
1710Sstevel@tonic-gate	add	%i3, %g4, %i3		! we want a single src pointer here
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate	srlx	%l1, 56, %g1		! first byte
1740Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1750Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1760Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1770Sstevel@tonic-gate	inccc	%g4			! n--
1780Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute len
1790Sstevel@tonic-gate	srlx	%l1, 48, %g1		! second byte
1800Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1810Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1820Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1830Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1840Sstevel@tonic-gate	inccc	%g4			! n--
1850Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute len
1860Sstevel@tonic-gate	srlx	%l1, 40, %g1		! third byte
1870Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1880Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1890Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1900Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1910Sstevel@tonic-gate	inccc	%g4			! n--
1920Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
1930Sstevel@tonic-gate	srlx	%l1, 32, %g1		! fourth byte
1940Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1950Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1960Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1970Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1980Sstevel@tonic-gate	inccc	%g4			! n--
1990Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2000Sstevel@tonic-gate	srlx	%l1, 24, %g1		! fifth byte
2010Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2020Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2030Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2040Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2050Sstevel@tonic-gate	inccc	%g4			! n--
2060Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2070Sstevel@tonic-gate	srlx	%l1, 16, %g1		! sixth byte
2080Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2090Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2100Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2110Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2120Sstevel@tonic-gate	inccc	%g4			! n--
2130Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2140Sstevel@tonic-gate	srlx	%l1, 8, %g1		! seventh byte
2150Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2160Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2170Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2180Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2190Sstevel@tonic-gate	inccc	%g4			! n--
2200Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2210Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached ?
2220Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2230Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2240Sstevel@tonic-gate	stb	%l1, [%i2]		! store eigth byte
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate	! we need to force a null byte in the last position of dst
2270Sstevel@tonic-gate	! %i2 points to the location
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate.forcenull:
2300Sstevel@tonic-gate	stb	%g0, [%i2]		! force string terminating null byte
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate	! here: %i1 points to src start
2330Sstevel@tonic-gate	!	%i3 points is current src ptr (8-byte aligned)
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate.searchword:
2360Sstevel@tonic-gate	ldx	[%i3], %l1		! src dword
2370Sstevel@tonic-gate.searchword2:
2380Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
2390Sstevel@tonic-gate	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
2400Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x80808080
2410Sstevel@tonic-gate	bz,a,pt	%ncc, .searchword	! no null byte if expression is 0
2420Sstevel@tonic-gate	add	%i3, 8, %i3		! src += 8
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate	mov	0xff, %i5		! create byte mask for null byte scanning
2450Sstevel@tonic-gate	sllx	%i5, 56, %i5		! mask for 1st byte = 0xff0000000000000000
2460Sstevel@tonic-gate.searchbyte:
2470Sstevel@tonic-gate	andcc	%l1, %i5, %g0		! current byte zero?
2480Sstevel@tonic-gate	srlx	%i5, 8, %i5		! byte mask for next byte
2490Sstevel@tonic-gate	bnz,a	%ncc, .searchbyte	! current byte != zero, continue search
2500Sstevel@tonic-gate	add	%i3, 1, %i3		! src++
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate.endfound:
2530Sstevel@tonic-gate	sub	%i3, %i1, %i0		! len = src - orig src
2540Sstevel@tonic-gate	ret				! done
2550Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
2560Sstevel@tonic-gate	nop				! align loop on 16-byte
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate.storebyte1421:
2590Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2600Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2610Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2620Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2630Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
2640Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2650Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! end of src found, may need to pad
2660Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
2670Sstevel@tonic-gate	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; word aligned now
2680Sstevel@tonic-gate	stb	%g1, [%l0]		! store first byte
2690Sstevel@tonic-gate	srlx	%l1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
2700Sstevel@tonic-gate	stw	%g1, [%l0 + 1]		! store bytes 2, 3, 4, 5
2710Sstevel@tonic-gate	srlx	%l1, 8, %g1		! %g1<15:0> = bytes 6, 7
2720Sstevel@tonic-gate	sth	%g1, [%l0 + 5]		! store bytes 6, 7
2730Sstevel@tonic-gate	ba	.storebyte1421		! next dword
2740Sstevel@tonic-gate	stb	%l1, [%l0 + 7]		! store eigth byte
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate.storebyte1241:
2770Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2780Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2790Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2800Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2810Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
2820Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2830Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
2840Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
2850Sstevel@tonic-gate	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; half-word aligned now
2860Sstevel@tonic-gate	stb	%g1, [%l0]		! store first byte
2870Sstevel@tonic-gate	srlx	%l1, 40, %g1		! %g1<15:0> = bytes 2, 3
2880Sstevel@tonic-gate	sth	%g1, [%l0 + 1]		! store bytes 2, 3
2890Sstevel@tonic-gate	srlx	%l1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
2900Sstevel@tonic-gate	stw	%g1, [%l0 + 3]		! store bytes 4, 5, 6, 7
2910Sstevel@tonic-gate	ba	.storebyte1241		! next dword
2920Sstevel@tonic-gate	stb	%l1, [%l0 + 7]		! store eigth byte
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate.storehalfword:
2950Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2960Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2970Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2980Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2990Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
3000Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3010Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3020Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
3030Sstevel@tonic-gate	srlx	%l1, 48, %g1		! %g1<15:0> = bytes 1, 2; word aligned now
3040Sstevel@tonic-gate	sth	%g1, [%l0]		! store bytes 1, 2
3050Sstevel@tonic-gate	srlx	%l1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6
3060Sstevel@tonic-gate	stw	%g1, [%l0 + 2]		! store bytes 3, 4, 5, 6
3070Sstevel@tonic-gate	ba	.storehalfword		! next dword
3080Sstevel@tonic-gate	sth	%l1, [%l0 + 6]		! store bytes 7, 8
3090Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3100Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate.storeword2:
3130Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
3140Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
3150Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
3160Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
3170Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
3180Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3190Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3200Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
3210Sstevel@tonic-gate	srlx	%l1, 32, %g1		! %g1<31:0> = bytes 1, 2, 3, 4
3220Sstevel@tonic-gate	stw	%g1, [%l0]		! store bytes 1, 2, 3, 4
3230Sstevel@tonic-gate	ba	.storeword2		! next dword
3240Sstevel@tonic-gate	stw	%l1, [%l0 + 4]		! store bytes 5, 6, 7, 8
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate	! n expired, i.e. end of destination buffer reached. Force null
3270Sstevel@tonic-gate	! null termination of dst, then scan src until end foudn for
3280Sstevel@tonic-gate	! determination of strlen(src)
3290Sstevel@tonic-gate	!
3300Sstevel@tonic-gate	! here: %i3 points to current src byte
3310Sstevel@tonic-gate	!       %i2 points one byte past end of dst
3320Sstevel@tonic-gate	! magic constants not loaded
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate.forcenullunalign:
3350Sstevel@tonic-gate	add	%i2, %g4, %i2		! we need a single dst ptr
3360Sstevel@tonic-gate	stb	%g0, [%i2 - 1]		! force string terminating null byte
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate.getstrlen:
3390Sstevel@tonic-gate	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
3400Sstevel@tonic-gate	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
3410Sstevel@tonic-gate	sllx	%i4, 32, %i2		! spread magic1
3420Sstevel@tonic-gate	or	%i4, %i2, %i4		!   to all 64 bits
3430Sstevel@tonic-gate	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
3440Sstevel@tonic-gate	nop				! align loop to 16-byte boundary
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate.getstrlenloop:
3470Sstevel@tonic-gate	andcc	%i3, 7, %g0		! src dword aligned?
3480Sstevel@tonic-gate	bz,a,pn	%ncc, .searchword2	! yup, now search a dword at a time
3490Sstevel@tonic-gate	ldx	[%i3], %l1		! src dword
3500Sstevel@tonic-gate	ldub	[%i3], %l1		! load src byte
3510Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached?
3520Sstevel@tonic-gate	bnz,a	%ncc, .getstrlenloop	! yup, return length
3530Sstevel@tonic-gate	add	%i3, 1, %i3		! src++
3540Sstevel@tonic-gate	sub	%i3, %i1, %i0		! len = src - orig src
3550Sstevel@tonic-gate	ret				! done
3560Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate	nop				! pad tp 16-byte boundary
3590Sstevel@tonic-gate	nop				! pad tp 16-byte boundary
3600Sstevel@tonic-gate	SET_SIZE(strlcpy)
361