2.9
[glibc/nacl-glibc.git] / sysdeps / sparc / sparc32 / strchr.S
blob7c397171f577334aac2ff5c8bc9895c7660e7e24
1 /* 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>.
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.
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.
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.  */
23 #include <sysdep.h>
25         /* Normally, this uses ((xword - 0x01010101) & 0x80808080) test
26            to find out if any byte in xword could be zero. This is fast, but
27            also gives false alarm for any byte in range 0x81-0xff. It does
28            not matter for correctness, as if this test tells us there could
29            be some zero byte, we check it byte by byte, but if bytes with
30            high bits set are common in the strings, then this will give poor
31            performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
32            will use one tick slower, but more precise test
33            ((xword - 0x01010101) & (~xword) & 0x80808080),
34            which does not give any false alarms (but if some bits are set,
35            one cannot assume from it which bytes are zero and which are not).
36            It is yet to be measured, what is the correct default for glibc
37            in these days for an average user.
38          */
40         .text
41         .align          4
42 ENTRY(strchr)
43         andcc           %o1, 0xff, %o1
44         be              12f
45          sll            %o1, 8, %o2
46         andcc           %o0, 3, %g0
47         or              %o1, %o2, %o2
48         sethi           %hi(0x80808080), %o4
49         sll             %o2, 16, %o3
50         be              13f
51          or             %o3, %o2, %g2
53         ldub            [%o0], %g4
54         cmp             %g4, %o1
55         be              11f
56          add            %o0, 1, %o0
57         cmp             %g4, 0
58         be              9f
59          andcc          %o0, 3, %g0
60         be              4f
61          or             %o4, %lo(0x80808080), %o3
62         ldub            [%o0], %g4
63         cmp             %g4, %o1
64         be              11f
65          add            %o0, 1, %o0
66         cmp             %g4, 0
67         be              9f
68          andcc          %o0, 3, %g0
69         be              5f
70          sethi          %hi(0x01010101), %o5
71         ldub            [%o0], %g4
72         cmp             %g4, %o1
73         be              11f
74          add            %o0, 1, %o0
75         cmp             %g4, 0
76         be              9f
77          or             %o5, %lo(0x01010101), %o2
78         b               6f
79          ld             [%o0], %g4
80 11:     retl
81          sub            %o0, 1, %o0
83 13:     or              %o4, %lo(0x80808080), %o3
84 4:      sethi           %hi(0x01010101), %o5
85 5:      or              %o5, %lo(0x01010101), %o2
86 7:      ld              [%o0], %g4
87 6:      xor             %g4, %g2, %g5
88         sub             %g4, %o2, %o4
89 #ifdef EIGHTBIT_NOT_RARE
90         sub             %g5, %o2, %g6
91         andn            %o4, %g4, %o4
92         andn            %g6, %g5, %g5
93 #else
94         sub             %g5, %o2, %g5
95 #endif
96         or              %g5, %o4, %o4   
97         andcc           %o4, %o3, %g0
98         be              7b
99          add            %o0, 4, %o0
101         /* Check every byte. */
102 8:      srl             %g4, 24, %g5 
103 7:      andcc           %g5, 0xff, %g5
104         be              9f
105          cmp            %g5, %o1
106         be              4f
107          srl            %g4, 16, %g5
108         andcc           %g5, 0xff, %g5
109         be              9f
110          cmp            %g5, %o1
111         be              3f
112          srl            %g4, 8, %g5
113         andcc           %g5, 0xff, %g5
114         be              9f
115          cmp            %g5, %o1
116         be              2f
117          andcc          %g4, 0xff, %g5
118         be              9f
119          cmp            %g5, %o1
120         bne,a           6b
121          ld             [%o0], %g4
122         retl
123          sub            %o0, 1, %o0
124 2:      retl
125          sub            %o0, 2, %o0
126 3:      retl
127          sub            %o0, 3, %o0
128 4:      retl
129          sub            %o0, 4, %o0
130 9:      retl
131          clr            %o0
133 11:     ldub            [%o0], %o5
134         cmp             %o5, 0
135         be              1f
136          add            %o0, 1, %o0
137         andcc           %o0, 3, %g0
138         be              4f
139          or             %o4, %lo(0x80808080), %o3
140         ldub            [%o0], %o5
141         cmp             %o5, 0
142         be              1f
143          add            %o0, 1, %o0
144         andcc           %o0, 3, %g0
145         be              5f
146          sethi          %hi(0x01010101), %o4
147         ldub            [%o0], %o5
148         cmp             %o5, 0
149         be              1f
150          add            %o0, 1, %o0
151         b               6f
152          or             %o4, %lo(0x01010101), %o2
153 1:      retl
154          sub            %o0, 1, %o0
156 12:     andcc           %o0, 3, %g0
157         bne             11b
158          sethi          %hi(0x80808080), %o4
159         or              %o4, %lo(0x80808080), %o3
160 4:      sethi           %hi(0x01010101), %o4
161 5:      or              %o4, %lo(0x01010101), %o2
162 6:      ld              [%o0], %o5
163 7:      sub             %o5, %o2, %o4
164 #ifdef EIGHTBIT_NOT_RARE
165         andn            %o4, %o5, %o4
166 #endif
167         andcc           %o4, %o3, %g0
168         be              6b
169          add            %o0, 4, %o0
171         /* Check every byte. */
172         srl             %o5, 24, %g5
173         andcc           %g5, 0xff, %g0
174         be              8f
175          add            %o0, -4, %o4
176         srl             %o5, 16, %g5
177         andcc           %g5, 0xff, %g0
178         be              8f
179          add            %o4, 1, %o4
180         srl             %o5, 8, %g5
181         andcc           %g5, 0xff, %g0
182         be              8f
183          add            %o4, 1, %o4
184         andcc           %o5, 0xff, %g0
185         bne,a           7b
186          ld             [%o0], %o5
187         add             %o4, 1, %o4
188 8:      retl
189          mov            %o4, %o0
191 13:     ldub            [%o0], %g4
192         cmp             %g4, %o1
193         add             %o0, 1, %o0
194         be,a            1f
195          sub            %o0, 1, %o5
196         cmp             %g4, 0
197         be              9f
198 1:       andcc          %o0, 3, %g0
199         be              4f
200          or             %o4, %lo(0x80808080), %o3
201         ldub            [%o0], %g4
202         cmp             %g4, %o1
203         add             %o0, 1, %o0
204         be,a            1f
205          sub            %o0, 1, %o5
206         cmp             %g4, 0
207         be              9f
208 1:       andcc          %o0, 3, %g0
209         be              5f
210          sethi          %hi(0x01010101), %o4
211         ldub            [%o0], %g4
212         cmp             %g4, %o1
213         add             %o0, 1, %o0
214         be,a            1f
215          sub            %o0, 1, %o5
216         cmp             %g4, 0
217         be              9f
218 1:       or             %o4, %lo(0x01010101), %o2
219         b               7f
220          ld             [%o0], %g4
221 END(strchr)
223 ENTRY(strrchr)
224         andcc           %o1, 0xff, %o1
225         clr             %o5
226         be              12b
227          sll            %o1, 8, %o2
228         andcc           %o0, 3, %g0
229         or              %o1, %o2, %o2
230         sethi           %hi(0x80808080), %o4
231         sll             %o2, 16, %o3
232         bne             13b
233          or             %o3, %o2, %g2
234         or              %o4, %lo(0x80808080), %o3
235 4:      sethi           %hi(0x01010101), %o4
236 5:      or              %o4, %lo(0x01010101), %o2
237 6:      ld              [%o0], %g4
238 7:      xor             %g4, %g2, %g5
239         sub             %g4, %o2, %o4
240 #ifdef EIGHTBIT_NOT_RARE
241         sub             %g5, %o2, %g6
242         andn            %o4, %g4, %o4
243         andn            %g6, %g5, %g5
244 #else
245         sub             %g5, %o2, %g5
246 #endif
247         or              %g5, %o4, %o4   
248         andcc           %o4, %o3, %g0
249         be              6b
250          add            %o0, 4, %o0
252         /* Check every byte. */
253 3:      srl             %g4, 24, %g5 
254 8:      andcc           %g5, 0xff, %g5
255         be              9f
256          cmp            %g5, %o1
257         be,a            1f
258          sub            %o0, 4, %o5
259 1:      srl             %g4, 16, %g5
260         andcc           %g5, 0xff, %g5
261         be              9f
262          cmp            %g5, %o1
263         be,a            1f
264          sub            %o0, 3, %o5
265 1:      srl             %g4, 8, %g5
266         andcc           %g5, 0xff, %g5
267         be              9f
268          cmp            %g5, %o1
269         be,a            1f
270          sub            %o0, 2, %o5
271 1:      andcc           %g4, 0xff, %g5
272         be              9f
273          cmp            %g5, %o1
274         be,a            1f
275          sub            %o0, 1, %o5
276 1:      b               7b
277          ld             [%o0], %g4
278 9:      retl
279          mov            %o5, %o0
280 END(strrchr)
282 weak_alias (strchr, index)
283 weak_alias (strrchr, rindex)
284 libc_hidden_builtin_def (strchr)
285 libc_hidden_builtin_def (strrchr)