xref: /illumos-gate/usr/src/lib/libc/sparcv9/gen/strchr.S (revision 55fea89d)
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
279a70fc3bSMark J. Nelson	.file	"strchr.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
30*55fea89dSDan Cross * The strchr() function returns a pointer to the first occurrence of c
317c478bd9Sstevel@tonic-gate * (converted to a char) in string s, or a null pointer if c does not occur
327c478bd9Sstevel@tonic-gate * in the string.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate	! Here, we start by checking to see if we're searching the dest
387c478bd9Sstevel@tonic-gate	! string for a null byte.  We have fast code for this, so it's
397c478bd9Sstevel@tonic-gate	! an important special case.  Otherwise, if the string is not
407c478bd9Sstevel@tonic-gate	! word aligned, we check a for the search char a byte at a time
417c478bd9Sstevel@tonic-gate	! until we've reached a word boundary.  Once this has happened
427c478bd9Sstevel@tonic-gate	! some zero-byte finding values are initialized and the string
437c478bd9Sstevel@tonic-gate	! is checked a word at a time
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate	ENTRY(strchr)
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate	.align 32
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %o1		! search only for this one byte
507c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .searchnullbyte	! faster code for searching null
517c478bd9Sstevel@tonic-gate	andcc	%o0, 3, %o4		! str word aligned ?
527c478bd9Sstevel@tonic-gate	bz,a,pn	%ncc, .prepword2	! yup, prepare for word-wise search
537c478bd9Sstevel@tonic-gate	sll	%o1, 8, %g1		! start spreading findchar across word
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate	ldub	[%o0], %o2		! str[0]
567c478bd9Sstevel@tonic-gate	cmp	%o2, %o1		! str[0] == findchar ?
577c478bd9Sstevel@tonic-gate	be,pn	%ncc, .done		! yup, done
587c478bd9Sstevel@tonic-gate	tst	%o2			! str[0] == 0 ?
597c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
607c478bd9Sstevel@tonic-gate	cmp	%o4, 3			! only one byte needed to align?
617c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .prepword		! yup, prepare for word-wise search
627c478bd9Sstevel@tonic-gate	inc	%o0			! str++
637c478bd9Sstevel@tonic-gate	ldub	[%o0], %o2		! str[1]
647c478bd9Sstevel@tonic-gate	cmp	%o2, %o1		! str[1] == findchar ?
657c478bd9Sstevel@tonic-gate	be,pn	%ncc, .done		! yup, done
667c478bd9Sstevel@tonic-gate	tst	%o2			! str[1] == 0 ?
677c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
687c478bd9Sstevel@tonic-gate	cmp	%o4, 2			! only two bytes needed to align?
697c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .prepword		! yup, prepare for word-wise search
707c478bd9Sstevel@tonic-gate	inc	%o0			! str++
717c478bd9Sstevel@tonic-gate	ldub	[%o0], %o2		! str[2]
727c478bd9Sstevel@tonic-gate	cmp	%o2, %o1		! str[2] == findchar ?
737c478bd9Sstevel@tonic-gate	be,pn	%ncc, .done		! yup, done
747c478bd9Sstevel@tonic-gate	tst	%o2			! str[2] == 0 ?
757c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
767c478bd9Sstevel@tonic-gate	inc	%o0			! str++
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate.prepword:
797c478bd9Sstevel@tonic-gate	sll	%o1, 8, %g1		! spread findchar ------+
807c478bd9Sstevel@tonic-gate.prepword2:							!
817c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1 !
827c478bd9Sstevel@tonic-gate	or	%o1, %g1, %o1		!  across all <---------+
837c478bd9Sstevel@tonic-gate	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2	!
847c478bd9Sstevel@tonic-gate	sll	%o1, 16, %g1		!   four bytes <--------+
857c478bd9Sstevel@tonic-gate	or	%o4, %lo(0x01010101), %o4			!
867c478bd9Sstevel@tonic-gate	or	%o1, %g1, %o1		!    of a word <--------+
877c478bd9Sstevel@tonic-gate	or	%o5, %lo(0x80808080), %o5
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate.searchchar:
907c478bd9Sstevel@tonic-gate	lduw	[%o0], %o2		! src word
917c478bd9Sstevel@tonic-gate	andn	%o5, %o2, %o3		! ~word & 0x80808080
927c478bd9Sstevel@tonic-gate	sub	%o2, %o4, %g1		! word = (word - 0x01010101)
937c478bd9Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! ((word - 0x01010101) & ~word & 0x80808080)
947c478bd9Sstevel@tonic-gate	bnz,pn	%ncc, .haszerobyte	! zero byte if magic expression != 0
957c478bd9Sstevel@tonic-gate	xor	%o2, %o1, %g1		! tword = word ^ findchar
967c478bd9Sstevel@tonic-gate	andn	%o5, %g1, %o3		! ~tword & 0x80808080
977c478bd9Sstevel@tonic-gate	sub	%g1, %o4, %o2		! (tword - 0x01010101)
987c478bd9Sstevel@tonic-gate	andcc	%o3, %o2, %g0		! ((tword - 0x01010101) & ~tword & 0x80808080)
997c478bd9Sstevel@tonic-gate	bz,a,pt	%ncc, .searchchar	! no findchar if magic expression == 0
1007c478bd9Sstevel@tonic-gate	add	%o0, 4, %o0		! str += 4
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate	! here we know "word" contains the searched character, but no null
1037c478bd9Sstevel@tonic-gate	! byte. if there was a null byte, we would have gone to .haszerobyte
1047c478bd9Sstevel@tonic-gate	! "tword" has null bytes where "word" had findchar. Examine "tword"
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate.foundchar:
1077c478bd9Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
1087c478bd9Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! first byte zero (= found search char) ?
1097c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1107c478bd9Sstevel@tonic-gate	set	0x00ff0000, %o5		! mask for 2nd byte
1117c478bd9Sstevel@tonic-gate	inc	%o0			! str++
112*55fea89dSDan Cross	andcc	%g1, %o5, %g0		! second byte zero (= found search char) ?
1137c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1147c478bd9Sstevel@tonic-gate	srl	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
1157c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1167c478bd9Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! third byte zero (= found search char) ?
1177c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .done		! nope, increment in delay slot
1187c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate.done:
1217c478bd9Sstevel@tonic-gate	retl				! done with leaf function
1227c478bd9Sstevel@tonic-gate	nop				! padding
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate	! Here we know that "word" contains a null byte indicating the
1257c478bd9Sstevel@tonic-gate	! end of the string. However, "word" might also contain findchar
1267c478bd9Sstevel@tonic-gate	! "tword" (in %g1) has null bytes where "word" had findchar. So
1277c478bd9Sstevel@tonic-gate	! check both "tword" and "word"
128*55fea89dSDan Cross
1297c478bd9Sstevel@tonic-gate.haszerobyte:
1307c478bd9Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
1317c478bd9Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! first byte == findchar ?
1327c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1337c478bd9Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! first byte == 0 ?
1347c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
1357c478bd9Sstevel@tonic-gate	set	0x00ff0000, %o4		! mask for 2nd byte
1367c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1377c478bd9Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! second byte == findchar ?
1387c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1397c478bd9Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! second byte == 0 ?
1407c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
1417c478bd9Sstevel@tonic-gate	srl	%o4, 8, %o4		! mask for 3rd byte = 0x0000ff00
1427c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1437c478bd9Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! third byte == findchar ?
1447c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1457c478bd9Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! third byte == 0 ?
1467c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .notfound		! yup, return null pointer
1477c478bd9Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! fourth byte == findchar ?
1487c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1497c478bd9Sstevel@tonic-gate	inc	%o0			! str++
150*55fea89dSDan Cross
1517c478bd9Sstevel@tonic-gate.notfound:
1527c478bd9Sstevel@tonic-gate	retl				! done with leaf function
1537c478bd9Sstevel@tonic-gate	xor	%o0, %o0, %o0		! return null pointer
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate	! since findchar == 0, we only have to do one test per item
1567c478bd9Sstevel@tonic-gate	! instead of two. This makes the search much faster.
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate.searchnullbyte:
1597c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .straligned	! str is word aligned
1607c478bd9Sstevel@tonic-gate	nop				! padding
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate	cmp	%o4, 2			! str halfword aligned ?
1637c478bd9Sstevel@tonic-gate	be,pn	%ncc, .s2aligned	! yup
1647c478bd9Sstevel@tonic-gate	ldub	[%o0], %o1		! str[0]
1657c478bd9Sstevel@tonic-gate	tst	%o1			! byte zero?
1667c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done		! yup, done
1677c478bd9Sstevel@tonic-gate	cmp	%o4, 3			! only one byte needed to align?
1687c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .straligned	! yup
1697c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate	! check to see if we're half word aligned, which it better than
1727c478bd9Sstevel@tonic-gate	! not being aligned at all.  Search the first half of the word
173*55fea89dSDan Cross	! if we are, and then search by whole word.
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate.s2aligned:
176*55fea89dSDan Cross	lduh	[%o0], %o1		! str[]
1777c478bd9Sstevel@tonic-gate	srl	%o1, 8, %o4		! %o4<7:0> = first byte
1787c478bd9Sstevel@tonic-gate	tst	%o4			! first byte zero ?
1797c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done2		! yup, done
1807c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %g0		! second byte zero ?
1817c478bd9Sstevel@tonic-gate	bz,a,pn	%ncc, .done2		! yup, done
1827c478bd9Sstevel@tonic-gate	inc	%o0			! str++
1837c478bd9Sstevel@tonic-gate	add	%o0, 2, %o0		! str+=2
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate.straligned:
1867c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
1877c478bd9Sstevel@tonic-gate	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2
1887c478bd9Sstevel@tonic-gate	or	%o4, %lo(0x01010101), %o4
1897c478bd9Sstevel@tonic-gate	or	%o5, %lo(0x80808080), %o5
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate.searchword:
1927c478bd9Sstevel@tonic-gate	lduw	[%o0], %o1		! src word
1937c478bd9Sstevel@tonic-gate	andn	%o5, %o1, %o3		! ~word & 0x80808080
1947c478bd9Sstevel@tonic-gate	sub	%o1, %o4, %g1		! word = (word - 0x01010101)
1957c478bd9Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! ((word - 0x01010101) & ~word & 0x80808080)
1967c478bd9Sstevel@tonic-gate	bz,a,pt	%ncc, .searchword	! no zero byte if magic expression == 0
1977c478bd9Sstevel@tonic-gate	add	%o0, 4, %o0		! str += 4
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate.zerobyte:
2007c478bd9Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
2017c478bd9Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! first byte zero?
2027c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done2		! yup, done
2037c478bd9Sstevel@tonic-gate	set	0x00ff0000, %o5		! mask for 2nd byte
2047c478bd9Sstevel@tonic-gate	inc	%o0			! str++
2057c478bd9Sstevel@tonic-gate	andcc	%o1, %o5, %g0		! second byte zero?
2067c478bd9Sstevel@tonic-gate	bz,pn	%ncc, .done2		! yup, done
2077c478bd9Sstevel@tonic-gate	srl	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
2087c478bd9Sstevel@tonic-gate	inc	%o0			! str++
2097c478bd9Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! third byte zero?
2107c478bd9Sstevel@tonic-gate	bnz,a	%ncc, .done2		! nope, increment in delay slot
2117c478bd9Sstevel@tonic-gate	inc	%o0			! str++
2127c478bd9Sstevel@tonic-gate.done2:
2137c478bd9Sstevel@tonic-gate    	retl				! return from leaf function
2147c478bd9Sstevel@tonic-gate	nop				! padding
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate	SET_SIZE(strchr)
217