blob: 16710d4e8f380a50619cfaa423604c7698f5f825 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
2 For SPARC v7.
3 Copyright (C) 1996, 1999, 2003 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Jakub Jelinek <jj@ultra.linux.cz> and
6 David S. Miller <davem@caip.rutgers.edu>.
7
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 02111-1307 USA. */
22
23 /* Normally, this uses ((xword - 0x01010101) & 0x80808080) test
24 to find out if any byte in xword could be zero. This is fast, but
25 also gives false alarm for any byte in range 0x81-0xff. It does
26 not matter for correctness, as if this test tells us there could
27 be some zero byte, we check it byte by byte, but if bytes with
28 high bits set are common in the strings, then this will give poor
29 performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
30 will use one tick slower, but more precise test
31 ((xword - 0x01010101) & (~xword) & 0x80808080),
32 which does not give any false alarms (but if some bits are set,
33 one cannot assume from it which bytes are zero and which are not).
34 It is yet to be measured, what is the correct default for glibc
35 in these days for an average user.
36 */
37
38#include <features.h>
39
40 .text
41 .align 4
4210: ldub [%o0], %g4
43 cmp %g4, %o1
44 be 1f
45 add %o0, 1, %o0
46 cmp %g4, 0
47 be 9f
48 andcc %o0, 3, %g0
49 be 4f
50 or %o4, %lo(0x80808080), %o3
51 ldub [%o0], %g4
52 cmp %g4, %o1
53 be 1f
54 add %o0, 1, %o0
55 cmp %g4, 0
56 be 9f
57 andcc %o0, 3, %g0
58 be 5f
59 sethi %hi(0x01010101), %o5
60 ldub [%o0], %g4
61 cmp %g4, %o1
62 be 1f
63 add %o0, 1, %o0
64 cmp %g4, 0
65 be 9f
66 or %o5, %lo(0x01010101), %o2
67 b 6f
68 ld [%o0], %g4
691: retl
70 sub %o0, 1, %o0
71
72ENTRY(strchr)
73 andcc %o1, 0xff, %o1
74 be 12f
75 sll %o1, 8, %o2
76 andcc %o0, 3, %g0
77 or %o1, %o2, %o2
78 sethi %hi(0x80808080), %o4
79 sll %o2, 16, %o3
80 bne 10b
81 or %o3, %o2, %g2
82 or %o4, %lo(0x80808080), %o3
834: sethi %hi(0x01010101), %o5
845: or %o5, %lo(0x01010101), %o2
857: ld [%o0], %g4
866: xor %g4, %g2, %g5
87 sub %g4, %o2, %o4
88#ifdef EIGHTBIT_NOT_RARE
89 sub %g5, %o2, %g6
90 andn %o4, %g4, %o4
91 andn %g6, %g5, %g5
92#else
93 sub %g5, %o2, %g5
94#endif
95 or %g5, %o4, %o4
96 andcc %o4, %o3, %g0
97 be 7b
98 add %o0, 4, %o0
99
100 /* Check every byte. */
1018: srl %g4, 24, %g5
1027: andcc %g5, 0xff, %g5
103 be 9f
104 cmp %g5, %o1
105 be 4f
106 srl %g4, 16, %g5
107 andcc %g5, 0xff, %g5
108 be 9f
109 cmp %g5, %o1
110 be 3f
111 srl %g4, 8, %g5
112 andcc %g5, 0xff, %g5
113 be 9f
114 cmp %g5, %o1
115 be 2f
116 andcc %g4, 0xff, %g5
117 be 9f
118 cmp %g5, %o1
119 bne,a 6b
120 ld [%o0], %g4
121 retl
122 sub %o0, 1, %o0
1232: retl
124 sub %o0, 2, %o0
1253: retl
126 sub %o0, 3, %o0
1274: retl
128 sub %o0, 4, %o0
1299: retl
130 clr %o0
131
13211: ldub [%o0], %o5
133 cmp %o5, 0
134 be 1f
135 add %o0, 1, %o0
136 andcc %o0, 3, %g0
137 be 4f
138 or %o4, %lo(0x80808080), %o3
139 ldub [%o0], %o5
140 cmp %o5, 0
141 be 1f
142 add %o0, 1, %o0
143 andcc %o0, 3, %g0
144 be 5f
145 sethi %hi(0x01010101), %o4
146 ldub [%o0], %o5
147 cmp %o5, 0
148 be 1f
149 add %o0, 1, %o0
150 b 6f
151 or %o4, %lo(0x01010101), %o2
1521: retl
153 sub %o0, 1, %o0
154
15512: andcc %o0, 3, %g0
156 bne 11b
157 sethi %hi(0x80808080), %o4
158 or %o4, %lo(0x80808080), %o3
1594: sethi %hi(0x01010101), %o4
1605: or %o4, %lo(0x01010101), %o2
1616: ld [%o0], %o5
1627: sub %o5, %o2, %o4
163#ifdef EIGHTBIT_NOT_RARE
164 andn %o4, %o5, %o4
165#endif
166 andcc %o4, %o3, %g0
167 be 6b
168 add %o0, 4, %o0
169
170 /* Check every byte. */
171 srl %o5, 24, %g5
172 andcc %g5, 0xff, %g0
173 be 8f
174 add %o0, -4, %o4
175 srl %o5, 16, %g5
176 andcc %g5, 0xff, %g0
177 be 8f
178 add %o4, 1, %o4
179 srl %o5, 8, %g5
180 andcc %g5, 0xff, %g0
181 be 8f
182 add %o4, 1, %o4
183 andcc %o5, 0xff, %g0
184 bne,a 7b
185 ld [%o0], %o5
186 add %o4, 1, %o4
1878: retl
188 mov %o4, %o0
189
19013: ldub [%o0], %g4
191 cmp %g4, %o1
192 add %o0, 1, %o0
193 be,a 1f
194 sub %o0, 1, %o5
195 cmp %g4, 0
196 be 9f
1971: andcc %o0, 3, %g0
198 be 4f
199 or %o4, %lo(0x80808080), %o3
200 ldub [%o0], %g4
201 cmp %g4, %o1
202 add %o0, 1, %o0
203 be,a 1f
204 sub %o0, 1, %o5
205 cmp %g4, 0
206 be 9f
2071: andcc %o0, 3, %g0
208 be 5f
209 sethi %hi(0x01010101), %o4
210 ldub [%o0], %g4
211 cmp %g4, %o1
212 add %o0, 1, %o0
213 be,a 1f
214 sub %o0, 1, %o5
215 cmp %g4, 0
216 be 9f
2171: or %o4, %lo(0x01010101), %o2
218 b 7f
219 ld [%o0], %g4
220END(strchr)
221libc_hidden_def(strchr)
222#ifdef __UCLIBC_SUSV3_LEGACY__
223strong_alias(strchr,index)
224#endif
225
226ENTRY(strrchr)
227 andcc %o1, 0xff, %o1
228 clr %o5
229 be 12b
230 sll %o1, 8, %o2
231 andcc %o0, 3, %g0
232 or %o1, %o2, %o2
233 sethi %hi(0x80808080), %o4
234 sll %o2, 16, %o3
235 bne 13b
236 or %o3, %o2, %g2
237 or %o4, %lo(0x80808080), %o3
2384: sethi %hi(0x01010101), %o4
2395: or %o4, %lo(0x01010101), %o2
2406: ld [%o0], %g4
2417: xor %g4, %g2, %g5
242 sub %g4, %o2, %o4
243#ifdef EIGHTBIT_NOT_RARE
244 sub %g5, %o2, %g6
245 andn %o4, %g4, %o4
246 andn %g6, %g5, %g5
247#else
248 sub %g5, %o2, %g5
249#endif
250 or %g5, %o4, %o4
251 andcc %o4, %o3, %g0
252 be 6b
253 add %o0, 4, %o0
254
255 /* Check every byte. */
2563: srl %g4, 24, %g5
2578: andcc %g5, 0xff, %g5
258 be 9f
259 cmp %g5, %o1
260 be,a 1f
261 sub %o0, 4, %o5
2621: srl %g4, 16, %g5
263 andcc %g5, 0xff, %g5
264 be 9f
265 cmp %g5, %o1
266 be,a 1f
267 sub %o0, 3, %o5
2681: srl %g4, 8, %g5
269 andcc %g5, 0xff, %g5
270 be 9f
271 cmp %g5, %o1
272 be,a 1f
273 sub %o0, 2, %o5
2741: andcc %g4, 0xff, %g5
275 be 9f
276 cmp %g5, %o1
277 be,a 1f
278 sub %o0, 1, %o5
2791: b 7b
280 ld [%o0], %g4
2819: retl
282 mov %o5, %o0
283END(strrchr)
284libc_hidden_def(strrchr)
285#ifdef __UCLIBC_SUSV3_LEGACY__
286strong_alias(strrchr,rindex)
287#endif