xref: /titanic_52/usr/src/lib/libc/sparcv9/gen/strlcpy.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	"strlcpy.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
307c478bd9Sstevel@tonic-gate * The strlcpy() function copies at most dstsize-1 characters
317c478bd9Sstevel@tonic-gate * (dstsize being the size of the string buffer dst) from src
327c478bd9Sstevel@tonic-gate * to dst, truncating src if necessary. The result is always
337c478bd9Sstevel@tonic-gate * null-terminated.  The function returns strlen(src). Buffer
347c478bd9Sstevel@tonic-gate * overflow can be checked as follows:
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate *   if (strlcpy(dst, src, dstsize) >= dstsize)
377c478bd9Sstevel@tonic-gate *           return -1;
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate	! strlcpy implementation is similar to that of strcpy, except
437c478bd9Sstevel@tonic-gate	! in this case, the maximum size of the detination must be
447c478bd9Sstevel@tonic-gate	! tracked since it bounds our maximum copy size.  However,
457c478bd9Sstevel@tonic-gate	! we must still continue to check for zero since the routine
467c478bd9Sstevel@tonic-gate	! is expected to null-terminate any string that is within
477c478bd9Sstevel@tonic-gate	! the dest size bound.
487c478bd9Sstevel@tonic-gate	!
497c478bd9Sstevel@tonic-gate	! this method starts by checking for and arranging source alignment.
507c478bd9Sstevel@tonic-gate	! Once this has occurred, we copy based upon destination alignment.
517c478bd9Sstevel@tonic-gate	! This is either by xword, word, halfword, or byte.  As this occurs, we
527c478bd9Sstevel@tonic-gate	! check for a zero-byte.  If one is found, we branch to a method
537c478bd9Sstevel@tonic-gate	! which checks for the exact location of a zero-byte within a
547c478bd9Sstevel@tonic-gate	! larger xword/word/half-word quantity.
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate	ENTRY(strlcpy)
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate	.align 32
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate	save	%sp, -SA(WINDOWSIZE), %sp
627c478bd9Sstevel@tonic-gate	subcc	%g0, %i2, %g4		! n = -n, n == 0 ?
637c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .getstrlen	! n == 0, must determine strlen
647c478bd9Sstevel@tonic-gate	add	%i1, %i2, %i3		! src = src + n
657c478bd9Sstevel@tonic-gate	andcc	%i1, 7, %i4		! src dword aligned ?
667c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .dwordaligned	! yup
677c478bd9Sstevel@tonic-gate	add	%i0, %i2, %i2		! dst = dst + n
687c478bd9Sstevel@tonic-gate	sub	%i4, 8, %i4		! bytes until src aligned
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate.alignsrc:
717c478bd9Sstevel@tonic-gate	ldub	[%i3 + %g4], %l1	! src[]
727c478bd9Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached (null byte) ?
737c478bd9Sstevel@tonic-gate	stub	%l1, [%i2 + %g4]	! dst[] = src[]
747c478bd9Sstevel@tonic-gate	bz,a	%ncc, .done		! yes, done
757c478bd9Sstevel@tonic-gate	add 	%i2, %g4, %i2		! need single dest pointer for strlen
767c478bd9Sstevel@tonic-gate	addcc	%g4, 1, %g4		! src++, dst++, n--
777c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .forcenullunalign	! n == 0, force null byte, compute len
787c478bd9Sstevel@tonic-gate	addcc	%i4, 1, %i4		! src aligned now?
797c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .alignsrc		! no, copy another byte
807c478bd9Sstevel@tonic-gate	nop				! pad
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate.dwordaligned:
837c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
847c478bd9Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst
857c478bd9Sstevel@tonic-gate	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
867c478bd9Sstevel@tonic-gate	and	%l0, 3, %g1		! dst<1:0> to examine offset
877c478bd9Sstevel@tonic-gate	sllx	%i4, 32, %l1		! spread magic1
887c478bd9Sstevel@tonic-gate	cmp	%g1, 1			! dst offset of 1 or 5
897c478bd9Sstevel@tonic-gate	or	%i4, %l1, %i4		!   to all 64 bits
907c478bd9Sstevel@tonic-gate	sub	%i2, 8, %i2		! adjust for dest pre-incr in cpy loops
917c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storebyte1241	! store 1, 2, 4, 1 bytes
927c478bd9Sstevel@tonic-gate	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
937c478bd9Sstevel@tonic-gate	cmp	%g1, 3			! dst offset of 3 or 7
947c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storebyte1421	! store 1, 4, 2, 1 bytes
957c478bd9Sstevel@tonic-gate	cmp	%g1, 2			! dst halfword aligned ?
967c478bd9Sstevel@tonic-gate	be,pn	%ncc, .storehalfword	! yup, store half-word wise
977c478bd9Sstevel@tonic-gate	andcc	%l0, 7, %g0		! dst word aligned ?
987c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .storeword2	! yup, store word wise
997c478bd9Sstevel@tonic-gate	nop				! ensure loop is 16-byte aligned
1007c478bd9Sstevel@tonic-gate	nop				! ensure loop is 16-byte aligned
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate.storedword:
1037c478bd9Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! src dword
1047c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! n += 8, src += 8, dst += 8
1057c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
1067c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
1077c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
1087c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x8080808080808080)
1097c478bd9Sstevel@tonic-gate	bz,a,pt	%ncc, .storedword	! no zero byte if magic expression == 0
1107c478bd9Sstevel@tonic-gate	stx	%l1, [%i2 + %g4]	! store word to dst (address pre-incremented)
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate	! n has not expired, but src is at the end. we need to push out the
1137c478bd9Sstevel@tonic-gate	! remaining src bytes. Since strlen(dts) == strlen(src), we can
1147c478bd9Sstevel@tonic-gate	! compute the return value as the difference of final dst pointer
1157c478bd9Sstevel@tonic-gate	! and the pointer to the start of dst
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate.zerobyte:
1187c478bd9Sstevel@tonic-gate	add	%i2, %g4, %i2		! pointer to dest string
1197c478bd9Sstevel@tonic-gate	srlx	%l1, 56, %g1		! first byte
1207c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1217c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1227c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1237c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1247c478bd9Sstevel@tonic-gate	srlx	%l1, 48, %g1		! second byte
1257c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1267c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1277c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1287c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1297c478bd9Sstevel@tonic-gate	srlx	%l1, 40, %g1		! third byte
1307c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1317c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1327c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1337c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1347c478bd9Sstevel@tonic-gate	srlx	%l1, 32, %g1		! fourth byte
1357c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1367c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1377c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1387c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1397c478bd9Sstevel@tonic-gate	srlx	%l1, 24, %g1		! fifth byte
1407c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1417c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1427c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1437c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1447c478bd9Sstevel@tonic-gate	srlx	%l1, 16, %g1		! sixth byte
1457c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1467c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1477c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1487c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1497c478bd9Sstevel@tonic-gate	srlx	%l1, 8, %g1		! seventh byte
1507c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of string ?
1517c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, copy done, return length
1527c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1537c478bd9Sstevel@tonic-gate	stb	%l1, [%i2 + 1]		! store eigth byte
1547c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate.done:
1577c478bd9Sstevel@tonic-gate	sub	%i2, %i0, %i0		! len = dst - orig dst
1587c478bd9Sstevel@tonic-gate	ret				! subroutine done
1597c478bd9Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate	! n expired, so this is the last word. It may contain null bytes.
1627c478bd9Sstevel@tonic-gate	! Store bytes until n == 0. If a null byte is encountered during
1637c478bd9Sstevel@tonic-gate	! processing of this last src word, we are done. Otherwise continue
1647c478bd9Sstevel@tonic-gate	! to scan src until we hit the end, and compute strlen from the
1657c478bd9Sstevel@tonic-gate	! difference between the pointer past the last byte of src and the
1667c478bd9Sstevel@tonic-gate	! original pointer to the start of src
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate.lastword:
1697c478bd9Sstevel@tonic-gate	add	%i2, %g4, %i2		! we want a single dst pointer here
1707c478bd9Sstevel@tonic-gate	sub	%g4, 8, %g4		! undo counter pre-increment
1717c478bd9Sstevel@tonic-gate	add	%i3, %g4, %i3		! we want a single src pointer here
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate	srlx	%l1, 56, %g1		! first byte
1747c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1757c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1767c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1777c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
1787c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute len
1797c478bd9Sstevel@tonic-gate	srlx	%l1, 48, %g1		! second byte
1807c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1817c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1827c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1837c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1847c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
1857c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute len
1867c478bd9Sstevel@tonic-gate	srlx	%l1, 40, %g1		! third byte
1877c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1887c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1897c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1907c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1917c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
1927c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
1937c478bd9Sstevel@tonic-gate	srlx	%l1, 32, %g1		! fourth byte
1947c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
1957c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
1967c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
1977c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
1987c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
1997c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2007c478bd9Sstevel@tonic-gate	srlx	%l1, 24, %g1		! fifth byte
2017c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2027c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2037c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2047c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2057c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2067c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2077c478bd9Sstevel@tonic-gate	srlx	%l1, 16, %g1		! sixth byte
2087c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2097c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2107c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2117c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2127c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2137c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2147c478bd9Sstevel@tonic-gate	srlx	%l1, 8, %g1		! seventh byte
2157c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2167c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! end of src reached ?
2177c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2187c478bd9Sstevel@tonic-gate	stb	%g1, [%i2]		! store it
2197c478bd9Sstevel@tonic-gate	inccc	%g4			! n--
2207c478bd9Sstevel@tonic-gate	bz	.forcenull		! if n == 0, force null byte, compute strlen
2217c478bd9Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached ?
2227c478bd9Sstevel@tonic-gate	add	%i2, 1, %i2		! dst++
2237c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup
2247c478bd9Sstevel@tonic-gate	stb	%l1, [%i2]		! store eigth byte
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate	! we need to force a null byte in the last position of dst
2277c478bd9Sstevel@tonic-gate	! %i2 points to the location
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate.forcenull:
2307c478bd9Sstevel@tonic-gate	stb	%g0, [%i2]		! force string terminating null byte
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate	! here: %i1 points to src start
2337c478bd9Sstevel@tonic-gate	!	%i3 points is current src ptr (8-byte aligned)
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate.searchword:
2367c478bd9Sstevel@tonic-gate	ldx	[%i3], %l1		! src dword
2377c478bd9Sstevel@tonic-gate.searchword2:
2387c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~dword & 0x8080808080808080
2397c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! dword - 0x0101010101010101
2407c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((dword - 0x0101010101010101) & ~dword & 0x80808080
2417c478bd9Sstevel@tonic-gate	bz,a,pt	%ncc, .searchword	! no null byte if expression is 0
2427c478bd9Sstevel@tonic-gate	add	%i3, 8, %i3		! src += 8
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate	mov	0xff, %i5		! create byte mask for null byte scanning
2457c478bd9Sstevel@tonic-gate	sllx	%i5, 56, %i5		! mask for 1st byte = 0xff0000000000000000
2467c478bd9Sstevel@tonic-gate.searchbyte:
2477c478bd9Sstevel@tonic-gate	andcc	%l1, %i5, %g0		! current byte zero?
2487c478bd9Sstevel@tonic-gate	srlx	%i5, 8, %i5		! byte mask for next byte
2497c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .searchbyte	! current byte != zero, continue search
2507c478bd9Sstevel@tonic-gate	add	%i3, 1, %i3		! src++
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate.endfound:
2537c478bd9Sstevel@tonic-gate	sub	%i3, %i1, %i0		! len = src - orig src
2547c478bd9Sstevel@tonic-gate	ret				! done
2557c478bd9Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
2567c478bd9Sstevel@tonic-gate	nop				! align loop on 16-byte
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate.storebyte1421:
2597c478bd9Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2607c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2617c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2627c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2637c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
2647c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2657c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! end of src found, may need to pad
2667c478bd9Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
2677c478bd9Sstevel@tonic-gate	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; word aligned now
2687c478bd9Sstevel@tonic-gate	stb	%g1, [%l0]		! store first byte
2697c478bd9Sstevel@tonic-gate	srlx	%l1, 24, %g1		! %g1<31:0> = bytes 2, 3, 4, 5
2707c478bd9Sstevel@tonic-gate	stw	%g1, [%l0 + 1]		! store bytes 2, 3, 4, 5
2717c478bd9Sstevel@tonic-gate	srlx	%l1, 8, %g1		! %g1<15:0> = bytes 6, 7
2727c478bd9Sstevel@tonic-gate	sth	%g1, [%l0 + 5]		! store bytes 6, 7
2737c478bd9Sstevel@tonic-gate	ba	.storebyte1421		! next dword
2747c478bd9Sstevel@tonic-gate	stb	%l1, [%l0 + 7]		! store eigth byte
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate.storebyte1241:
2777c478bd9Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2787c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2797c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2807c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2817c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
2827c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
2837c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
2847c478bd9Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
2857c478bd9Sstevel@tonic-gate	srlx	%l1, 56, %g1		! %g1<7:0> = first byte; half-word aligned now
2867c478bd9Sstevel@tonic-gate	stb	%g1, [%l0]		! store first byte
2877c478bd9Sstevel@tonic-gate	srlx	%l1, 40, %g1		! %g1<15:0> = bytes 2, 3
2887c478bd9Sstevel@tonic-gate	sth	%g1, [%l0 + 1]		! store bytes 2, 3
2897c478bd9Sstevel@tonic-gate	srlx	%l1, 8, %g1		! %g1<31:0> = bytes 4, 5, 6, 7
2907c478bd9Sstevel@tonic-gate	stw	%g1, [%l0 + 3]		! store bytes 4, 5, 6, 7
2917c478bd9Sstevel@tonic-gate	ba	.storebyte1241		! next dword
2927c478bd9Sstevel@tonic-gate	stb	%l1, [%l0 + 7]		! store eigth byte
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate.storehalfword:
2957c478bd9Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
2967c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
2977c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
2987c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
2997c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
3007c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3017c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3027c478bd9Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
3037c478bd9Sstevel@tonic-gate	srlx	%l1, 48, %g1		! %g1<15:0> = bytes 1, 2; word aligned now
3047c478bd9Sstevel@tonic-gate	sth	%g1, [%l0]		! store bytes 1, 2
3057c478bd9Sstevel@tonic-gate	srlx	%l1, 16, %g1		! %g1<31:0> = bytes 3, 4, 5, 6
3067c478bd9Sstevel@tonic-gate	stw	%g1, [%l0 + 2]		! store bytes 3, 4, 5, 6
3077c478bd9Sstevel@tonic-gate	ba	.storehalfword		! next dword
3087c478bd9Sstevel@tonic-gate	sth	%l1, [%l0 + 6]		! store bytes 7, 8
3097c478bd9Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3107c478bd9Sstevel@tonic-gate	nop				! align next loop to 16-byte boundary
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate.storeword2:
3137c478bd9Sstevel@tonic-gate	ldx	[%i3 + %g4], %l1	! x = src[]
3147c478bd9Sstevel@tonic-gate	addcc	%g4, 8, %g4		! src += 8, dst += 8
3157c478bd9Sstevel@tonic-gate	bcs,pn	%ncc, .lastword		! if counter wraps, last word
3167c478bd9Sstevel@tonic-gate	andn	%i5, %l1, %g1		! ~x & 0x8080808080808080
3177c478bd9Sstevel@tonic-gate	sub	%l1, %i4, %l0		! x - 0x0101010101010101
3187c478bd9Sstevel@tonic-gate	andcc	%l0, %g1, %g0		! ((x - 0x0101010101010101) & ~x & 0x8080808080808080)
3197c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .zerobyte		! x has zero byte, handle end cases
3207c478bd9Sstevel@tonic-gate	add	%i2, %g4, %l0		! dst (in pointer form)
3217c478bd9Sstevel@tonic-gate	srlx	%l1, 32, %g1		! %g1<31:0> = bytes 1, 2, 3, 4
3227c478bd9Sstevel@tonic-gate	stw	%g1, [%l0]		! store bytes 1, 2, 3, 4
3237c478bd9Sstevel@tonic-gate	ba	.storeword2		! next dword
3247c478bd9Sstevel@tonic-gate	stw	%l1, [%l0 + 4]		! store bytes 5, 6, 7, 8
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate	! n expired, i.e. end of destination buffer reached. Force null
3277c478bd9Sstevel@tonic-gate	! null termination of dst, then scan src until end foudn for
3287c478bd9Sstevel@tonic-gate	! determination of strlen(src)
3297c478bd9Sstevel@tonic-gate	!
3307c478bd9Sstevel@tonic-gate	! here: %i3 points to current src byte
3317c478bd9Sstevel@tonic-gate	!       %i2 points one byte past end of dst
3327c478bd9Sstevel@tonic-gate	! magic constants not loaded
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate.forcenullunalign:
3357c478bd9Sstevel@tonic-gate	add	%i2, %g4, %i2		! we need a single dst ptr
3367c478bd9Sstevel@tonic-gate	stb	%g0, [%i2 - 1]		! force string terminating null byte
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate.getstrlen:
3397c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %i4	! Alan Mycroft's magic1
3407c478bd9Sstevel@tonic-gate	or	%i4, %lo(0x01010101),%i4!  finish loading magic1
3417c478bd9Sstevel@tonic-gate	sllx	%i4, 32, %i2		! spread magic1
3427c478bd9Sstevel@tonic-gate	or	%i4, %i2, %i4		!   to all 64 bits
3437c478bd9Sstevel@tonic-gate	sllx	%i4, 7, %i5		!  Alan Mycroft's magic2
3447c478bd9Sstevel@tonic-gate	nop				! align loop to 16-byte boundary
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate.getstrlenloop:
3477c478bd9Sstevel@tonic-gate	andcc	%i3, 7, %g0		! src dword aligned?
3487c478bd9Sstevel@tonic-gate	bz,a,pn	%ncc, .searchword2	! yup, now search a dword at a time
3497c478bd9Sstevel@tonic-gate	ldx	[%i3], %l1		! src dword
3507c478bd9Sstevel@tonic-gate	ldub	[%i3], %l1		! load src byte
3517c478bd9Sstevel@tonic-gate	andcc	%l1, 0xff, %g0		! end of src reached?
3527c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .getstrlenloop	! yup, return length
3537c478bd9Sstevel@tonic-gate	add	%i3, 1, %i3		! src++
3547c478bd9Sstevel@tonic-gate	sub	%i3, %i1, %i0		! len = src - orig src
3557c478bd9Sstevel@tonic-gate	ret				! done
3567c478bd9Sstevel@tonic-gate	restore	%i0, %g0, %o0		! restore register window, return len
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate	nop				! pad tp 16-byte boundary
3597c478bd9Sstevel@tonic-gate	nop				! pad tp 16-byte boundary
3607c478bd9Sstevel@tonic-gate	SET_SIZE(strlcpy)
361