xref: /onnv-gate/usr/src/lib/libc/sparc/gen/strchr.s (revision 0:68f95e015346)
1*0Sstevel@tonic-gate/*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate/*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate.ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate	.file	"%M%"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate/*
32*0Sstevel@tonic-gate * The strchr() function returns a pointer to the first occurrence of c
33*0Sstevel@tonic-gate * (converted to a char) in string s, or a null pointer if c does not occur
34*0Sstevel@tonic-gate * in the string.
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate#include <sys/asm_linkage.h>
38*0Sstevel@tonic-gate#include "synonyms.h"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate	! Here, we start by checking to see if we're searching the dest
41*0Sstevel@tonic-gate	! string for a null byte.  We have fast code for this, so it's
42*0Sstevel@tonic-gate	! an important special case.  Otherwise, if the string is not
43*0Sstevel@tonic-gate	! word aligned, we check a for the search char a byte at a time
44*0Sstevel@tonic-gate	! until we've reached a word boundary.  Once this has happened
45*0Sstevel@tonic-gate	! some zero-byte finding values are initialized and the string
46*0Sstevel@tonic-gate	! is checked a word at a time
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate	ENTRY(strchr)
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate	.align 32
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate	andcc	%o1, 0xff, %o1		! search only for this one byte
53*0Sstevel@tonic-gate	bz	.searchnullbyte		! faster code for searching null
54*0Sstevel@tonic-gate	andcc	%o0, 3, %o4		! str word aligned ?
55*0Sstevel@tonic-gate	bz,a	.prepword2		! yup, prepare for word-wise search
56*0Sstevel@tonic-gate	sll	%o1, 8, %g1		! start spreading findchar across word
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate	ldub	[%o0], %o2		! str[0]
59*0Sstevel@tonic-gate	cmp	%o2, %o1		! str[0] == findchar ?
60*0Sstevel@tonic-gate	be	.done			! yup, done
61*0Sstevel@tonic-gate	tst	%o2			! str[0] == 0 ?
62*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
63*0Sstevel@tonic-gate	cmp	%o4, 3			! only one byte needed to align?
64*0Sstevel@tonic-gate	bz	.prepword		! yup, prepare for word-wise search
65*0Sstevel@tonic-gate	inc	%o0			! str++
66*0Sstevel@tonic-gate	ldub	[%o0], %o2		! str[1]
67*0Sstevel@tonic-gate	cmp	%o2, %o1		! str[1] == findchar ?
68*0Sstevel@tonic-gate	be	.done			! yup, done
69*0Sstevel@tonic-gate	tst	%o2			! str[1] == 0 ?
70*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
71*0Sstevel@tonic-gate	cmp	%o4, 2			! only two bytes needed to align?
72*0Sstevel@tonic-gate	bz	.prepword		! yup, prepare for word-wise search
73*0Sstevel@tonic-gate	inc	%o0			! str++
74*0Sstevel@tonic-gate	ldub	[%o0], %o2		! str[2]
75*0Sstevel@tonic-gate	cmp	%o2, %o1		! str[2] == findchar ?
76*0Sstevel@tonic-gate	be	.done			! yup, done
77*0Sstevel@tonic-gate	tst	%o2			! str[2] == 0 ?
78*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
79*0Sstevel@tonic-gate	inc	%o0			! str++
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate.prepword:
82*0Sstevel@tonic-gate	sll	%o1, 8, %g1		! spread findchar ------+
83*0Sstevel@tonic-gate.prepword2:							!
84*0Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1 !
85*0Sstevel@tonic-gate	or	%o1, %g1, %o1		!  across all <---------+
86*0Sstevel@tonic-gate	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2	!
87*0Sstevel@tonic-gate	sll	%o1, 16, %g1		!   four bytes <--------+
88*0Sstevel@tonic-gate	or	%o4, %lo(0x01010101), %o4			!
89*0Sstevel@tonic-gate	or	%o1, %g1, %o1		!    of a word <--------+
90*0Sstevel@tonic-gate	or	%o5, %lo(0x80808080), %o5
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate.searchchar:
93*0Sstevel@tonic-gate	lduw	[%o0], %o2		! src word
94*0Sstevel@tonic-gate	andn	%o5, %o2, %o3		! ~word & 0x80808080
95*0Sstevel@tonic-gate	sub	%o2, %o4, %g1		! word = (word - 0x01010101)
96*0Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! ((word - 0x01010101) & ~word & 0x80808080)
97*0Sstevel@tonic-gate	bnz	.haszerobyte		! zero byte if magic expression != 0
98*0Sstevel@tonic-gate	xor	%o2, %o1, %g1		! tword = word ^ findchar
99*0Sstevel@tonic-gate	andn	%o5, %g1, %o3		! ~tword & 0x80808080
100*0Sstevel@tonic-gate	sub	%g1, %o4, %o2		! (tword - 0x01010101)
101*0Sstevel@tonic-gate	andcc	%o3, %o2, %g0		! ((tword - 0x01010101) & ~tword & 0x80808080)
102*0Sstevel@tonic-gate	bz,a	.searchchar		! no findchar if magic expression == 0
103*0Sstevel@tonic-gate	add	%o0, 4, %o0		! str += 4
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate	! here we know "word" contains the searched character, but no null
106*0Sstevel@tonic-gate	! byte. if there was a null byte, we would have gone to .haszerobyte
107*0Sstevel@tonic-gate	! "tword" has null bytes where "word" had findchar. Examine "tword"
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate.foundchar:
110*0Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
111*0Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! first byte zero (= found search char) ?
112*0Sstevel@tonic-gate	bz	.done			! yup, done
113*0Sstevel@tonic-gate	set	0x00ff0000, %o5		! mask for 2nd byte
114*0Sstevel@tonic-gate	inc	%o0			! str++
115*0Sstevel@tonic-gate	andcc	%g1, %o5, %g0		! second byte zero (= found search char) ?
116*0Sstevel@tonic-gate	bz	.done			! yup, done
117*0Sstevel@tonic-gate	srl	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
118*0Sstevel@tonic-gate	inc	%o0			! str++
119*0Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! third byte zero (= found search char) ?
120*0Sstevel@tonic-gate	bnz,a	.done			! nope, increment in delay slot
121*0Sstevel@tonic-gate	inc	%o0			! str++
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate.done:
124*0Sstevel@tonic-gate	retl				! done with leaf function
125*0Sstevel@tonic-gate	nop				! padding
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate	! Here we know that "word" contains a null byte indicating the
128*0Sstevel@tonic-gate	! end of the string. However, "word" might also contain findchar
129*0Sstevel@tonic-gate	! "tword" (in %g1) has null bytes where "word" had findchar. So
130*0Sstevel@tonic-gate	! check both "tword" and "word"
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate.haszerobyte:
133*0Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
134*0Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! first byte == findchar ?
135*0Sstevel@tonic-gate	bz	.done			! yup, done
136*0Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! first byte == 0 ?
137*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
138*0Sstevel@tonic-gate	set	0x00ff0000, %o4		! mask for 2nd byte
139*0Sstevel@tonic-gate	inc	%o0			! str++
140*0Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! second byte == findchar ?
141*0Sstevel@tonic-gate	bz	.done			! yup, done
142*0Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! second byte == 0 ?
143*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
144*0Sstevel@tonic-gate	srl	%o4, 8, %o4		! mask for 3rd byte = 0x0000ff00
145*0Sstevel@tonic-gate	inc	%o0			! str++
146*0Sstevel@tonic-gate	andcc	%g1, %o4, %g0		! third byte == findchar ?
147*0Sstevel@tonic-gate	bz	.done			! yup, done
148*0Sstevel@tonic-gate	andcc	%o2, %o4, %g0		! third byte == 0 ?
149*0Sstevel@tonic-gate	bz	.notfound		! yup, return null pointer
150*0Sstevel@tonic-gate	andcc	%g1, 0xff, %g0		! fourth byte == findchar ?
151*0Sstevel@tonic-gate	bz	.done			! yup, done
152*0Sstevel@tonic-gate	inc	%o0			! str++
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate.notfound:
155*0Sstevel@tonic-gate	retl				! done with leaf function
156*0Sstevel@tonic-gate	xor	%o0, %o0, %o0		! return null pointer
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate	! since findchar == 0, we only have to do one test per item
159*0Sstevel@tonic-gate	! instead of two. This makes the search much faster.
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate.searchnullbyte:
162*0Sstevel@tonic-gate	bz	.straligned		! str is word aligned
163*0Sstevel@tonic-gate	nop				! padding
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate	cmp	%o4, 2			! str halfword aligned ?
166*0Sstevel@tonic-gate	be	.s2aligned		! yup
167*0Sstevel@tonic-gate	ldub	[%o0], %o1		! str[0]
168*0Sstevel@tonic-gate	tst	%o1			! byte zero?
169*0Sstevel@tonic-gate	bz	.done2			! yup, done
170*0Sstevel@tonic-gate	cmp	%o4, 3			! only one byte needed to align?
171*0Sstevel@tonic-gate	bz	.straligned		! yup
172*0Sstevel@tonic-gate	inc	%o0			! str++
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate	! check to see if we're half word aligned, which it better than
175*0Sstevel@tonic-gate	! not being aligned at all.  Search the first half of the word
176*0Sstevel@tonic-gate	! if we are, and then search by whole word.
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate.s2aligned:
179*0Sstevel@tonic-gate	lduh	[%o0], %o1		! str[]
180*0Sstevel@tonic-gate	srl	%o1, 8, %o4		! %o4<7:0> = first byte
181*0Sstevel@tonic-gate	tst	%o4			! first byte zero ?
182*0Sstevel@tonic-gate	bz	.done2			! yup, done
183*0Sstevel@tonic-gate	andcc	%o1, 0xff, %g0		! second byte zero ?
184*0Sstevel@tonic-gate	bz,a	.done2			! yup, done
185*0Sstevel@tonic-gate	inc	%o0			! str++
186*0Sstevel@tonic-gate	add	%o0, 2, %o0		! str+=2
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate.straligned:
189*0Sstevel@tonic-gate	sethi	%hi(0x01010101), %o4	! Alan Mycroft's magic1
190*0Sstevel@tonic-gate	sethi	%hi(0x80808080), %o5	! Alan Mycroft's magic2
191*0Sstevel@tonic-gate	or	%o4, %lo(0x01010101), %o4
192*0Sstevel@tonic-gate	or	%o5, %lo(0x80808080), %o5
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate.searchword:
195*0Sstevel@tonic-gate	lduw	[%o0], %o1		! src word
196*0Sstevel@tonic-gate	andn	%o5, %o1, %o3		! ~word & 0x80808080
197*0Sstevel@tonic-gate	sub	%o1, %o4, %g1		! word = (word - 0x01010101)
198*0Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! ((word - 0x01010101) & ~word & 0x80808080)
199*0Sstevel@tonic-gate	bz,a	.searchword		! no zero byte if magic expression == 0
200*0Sstevel@tonic-gate	add	%o0, 4, %o0		! str += 4
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate.zerobyte:
203*0Sstevel@tonic-gate	set	0xff000000, %o4		! mask for 1st byte
204*0Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! first byte zero?
205*0Sstevel@tonic-gate	bz	.done2			! yup, done
206*0Sstevel@tonic-gate	set	0x00ff0000, %o5		! mask for 2nd byte
207*0Sstevel@tonic-gate	inc	%o0			! str++
208*0Sstevel@tonic-gate	andcc	%o1, %o5, %g0		! second byte zero?
209*0Sstevel@tonic-gate	bz	.done2			! yup, done
210*0Sstevel@tonic-gate	srl	%o4, 16, %o4		! 0x0000ff00 = mask for 3rd byte
211*0Sstevel@tonic-gate	inc	%o0			! str++
212*0Sstevel@tonic-gate	andcc	%o1, %o4, %g0		! third byte zero?
213*0Sstevel@tonic-gate	bnz,a	.done2			! nope, increment in delay slot
214*0Sstevel@tonic-gate	inc	%o0			! str++
215*0Sstevel@tonic-gate.done2:
216*0Sstevel@tonic-gate    	retl				! return from leaf function
217*0Sstevel@tonic-gate	nop				! padding
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate	SET_SIZE(strchr)
220