xref: /onnv-gate/usr/src/lib/libc/sparc/gen/strlen.s (revision 6812:febeba71273d)
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
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * 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 */
21*6812Sraf
220Sstevel@tonic-gate/*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*6812Sraf#pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate	.file	"%M%"
300Sstevel@tonic-gate
310Sstevel@tonic-gate/*
320Sstevel@tonic-gate * strlen(s)
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * Given string s, return length (not including the terminating null).
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * Fast assembler language version of the following C-program strlen
370Sstevel@tonic-gate * which represents the `standard' for the C-library.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate *	size_t
400Sstevel@tonic-gate *	strlen(s)
410Sstevel@tonic-gate *	register const char *s;
420Sstevel@tonic-gate *	{
430Sstevel@tonic-gate *		register const char *s0 = s + 1;
440Sstevel@tonic-gate *
450Sstevel@tonic-gate *		while (*s++ != '\0')
460Sstevel@tonic-gate *			;
470Sstevel@tonic-gate *		return (s - s0);
480Sstevel@tonic-gate *	}
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate#include <sys/asm_linkage.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate	! The object of strlen is to, as quickly as possible, find the
540Sstevel@tonic-gate	! null byte.  To this end, we attempt to get our string aligned
550Sstevel@tonic-gate	! and then blast across it using Alan Mycroft's algorithm for
560Sstevel@tonic-gate	! finding null bytes. If we are not aligned, the string is
570Sstevel@tonic-gate	! checked a byte at a time until it is.  Once this occurs,
580Sstevel@tonic-gate	! we can proceed word-wise across it.  Once a word with a
590Sstevel@tonic-gate	! zero byte has been found, we then check the word a byte
600Sstevel@tonic-gate	! at a time until we've located the zero byte, and return
610Sstevel@tonic-gate	! the proper length.
620Sstevel@tonic-gate
630Sstevel@tonic-gate	.align 32
640Sstevel@tonic-gate	ENTRY(strlen)
650Sstevel@tonic-gate	andcc		%o0, 3, %o4	! is src word aligned
660Sstevel@tonic-gate	bz,pt		%icc, .nowalgnd
670Sstevel@tonic-gate	mov		%o0, %o2
680Sstevel@tonic-gate
690Sstevel@tonic-gate	cmp		%o4, 2		! is src half-word aligned
700Sstevel@tonic-gate	be,a,pn		%icc, .s2algn
710Sstevel@tonic-gate	lduh		[%o2], %o1
720Sstevel@tonic-gate
730Sstevel@tonic-gate	ldub		[%o2], %o1
740Sstevel@tonic-gate	tst		%o1		! byte zero?
750Sstevel@tonic-gate	bz,pn		%icc, .done
760Sstevel@tonic-gate	cmp		%o4, 3		! src is byte aligned
770Sstevel@tonic-gate
780Sstevel@tonic-gate	be,pn		%icc, .nowalgnd
790Sstevel@tonic-gate	inc		1, %o2
800Sstevel@tonic-gate
810Sstevel@tonic-gate	lduh		[%o2], %o1
820Sstevel@tonic-gate
830Sstevel@tonic-gate.s2algn:
840Sstevel@tonic-gate	srl		%o1, 8, %o4
850Sstevel@tonic-gate	tst		%o4
860Sstevel@tonic-gate	bz,pn		%icc, .done
870Sstevel@tonic-gate	andcc		%o1, 0xff, %g0
880Sstevel@tonic-gate
890Sstevel@tonic-gate	bz,pn		%icc, .done
900Sstevel@tonic-gate	inc		1, %o2
910Sstevel@tonic-gate
920Sstevel@tonic-gate	inc		1, %o2
930Sstevel@tonic-gate
940Sstevel@tonic-gate.nowalgnd:
950Sstevel@tonic-gate	ld		[%o2], %o1
960Sstevel@tonic-gate	sethi		%hi(0x01010101), %o4
970Sstevel@tonic-gate	sethi		%hi(0x80808080), %o5
980Sstevel@tonic-gate	or		%o4, %lo(0x01010101), %o4
990Sstevel@tonic-gate	or		%o5, %lo(0x80808080), %o5
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate	andn		%o5, %o1, %o3
1020Sstevel@tonic-gate	sub		%o1, %o4, %g1
1030Sstevel@tonic-gate	andcc		%o3, %g1, %g0
1040Sstevel@tonic-gate	bnz,a,pn	%icc, .nullfound
1050Sstevel@tonic-gate	sethi		%hi(0xff000000), %o4
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate	ld		[%o2+4], %o1
1080Sstevel@tonic-gate	inc		4, %o2
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate.loop:						! this should be aligned to 32
1110Sstevel@tonic-gate	inc		4, %o2
1120Sstevel@tonic-gate	andn		%o5, %o1, %o3		! %o5 = ~word & 0x80808080
1130Sstevel@tonic-gate	sub		%o1, %o4, %g1		! %g1 = word - 0x01010101
1140Sstevel@tonic-gate	andcc		%o3, %g1, %g0
1150Sstevel@tonic-gate	bz,a,pt		%icc, .loop
1160Sstevel@tonic-gate	ld		[%o2], %o1
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate	dec		4, %o2
1190Sstevel@tonic-gate	sethi		%hi(0xff000000), %o4
1200Sstevel@tonic-gate.nullfound:
1210Sstevel@tonic-gate	andcc		%o1, %o4, %g0
1220Sstevel@tonic-gate	bz,pn		%icc, .done		! first byte zero
1230Sstevel@tonic-gate	srl		%o4, 8, %o4
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate	andcc		%o1, %o4, %g0
1260Sstevel@tonic-gate	bz,pn		%icc, .done		! second byte zero
1270Sstevel@tonic-gate	inc		1, %o2
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate	srl		%o4, 8, %o4
1300Sstevel@tonic-gate	andcc		%o1, %o4, %g0
1310Sstevel@tonic-gate	bz,pn		%icc, .done		! thrid byte zero
1320Sstevel@tonic-gate	inc		1, %o2
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate	inc		1, %o2			! fourth byte zero
1350Sstevel@tonic-gate.done:
1360Sstevel@tonic-gate	retl
1370Sstevel@tonic-gate	sub		%o2, %o0, %o0
1380Sstevel@tonic-gate	SET_SIZE(strlen)
1390Sstevel@tonic-gate
140