(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / sparc / sparc32 / strchr.S
blobb5be3adf604816d673642ea0eee1f71eef0c2048
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 10:     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
69 1:      retl
70          sub            %o0, 1, %o0
72 ENTRY(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
83 4:      sethi           %hi(0x01010101), %o5
84 5:      or              %o5, %lo(0x01010101), %o2
85 7:      ld              [%o0], %g4
86 6:      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
100         /* Check every byte. */
101 8:      srl             %g4, 24, %g5 
102 7:      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
123 2:      retl
124          sub            %o0, 2, %o0
125 3:      retl
126          sub            %o0, 3, %o0
127 4:      retl
128          sub            %o0, 4, %o0
129 9:      retl
130          clr            %o0
132 11:     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
152 1:      retl
153          sub            %o0, 1, %o0
155 12:     andcc           %o0, 3, %g0
156         bne             11b
157          sethi          %hi(0x80808080), %o4
158         or              %o4, %lo(0x80808080), %o3
159 4:      sethi           %hi(0x01010101), %o4
160 5:      or              %o4, %lo(0x01010101), %o2
161 6:      ld              [%o0], %o5
162 7:      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
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
187 8:      retl
188          mov            %o4, %o0
190 13:     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
197 1:       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
207 1:       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
217 1:       or             %o4, %lo(0x01010101), %o2
218         b               7f
219          ld             [%o0], %g4
220 END(strchr)
222 ENTRY(strrchr)
223         andcc           %o1, 0xff, %o1
224         clr             %o5
225         be              12b
226          sll            %o1, 8, %o2
227         andcc           %o0, 3, %g0
228         or              %o1, %o2, %o2
229         sethi           %hi(0x80808080), %o4
230         sll             %o2, 16, %o3
231         bne             13b
232          or             %o3, %o2, %g2
233         or              %o4, %lo(0x80808080), %o3
234 4:      sethi           %hi(0x01010101), %o4
235 5:      or              %o4, %lo(0x01010101), %o2
236 6:      ld              [%o0], %g4
237 7:      xor             %g4, %g2, %g5
238         sub             %g4, %o2, %o4
239 #ifdef EIGHTBIT_NOT_RARE
240         sub             %g5, %o2, %g6
241         andn            %o4, %g4, %o4
242         andn            %g6, %g5, %g5
243 #else
244         sub             %g5, %o2, %g5
245 #endif
246         or              %g5, %o4, %o4   
247         andcc           %o4, %o3, %g0
248         be              6b
249          add            %o0, 4, %o0
251         /* Check every byte. */
252 3:      srl             %g4, 24, %g5 
253 8:      andcc           %g5, 0xff, %g5
254         be              9f
255          cmp            %g5, %o1
256         be,a            1f
257          sub            %o0, 4, %o5
258 1:      srl             %g4, 16, %g5
259         andcc           %g5, 0xff, %g5
260         be              9f
261          cmp            %g5, %o1
262         be,a            1f
263          sub            %o0, 3, %o5
264 1:      srl             %g4, 8, %g5
265         andcc           %g5, 0xff, %g5
266         be              9f
267          cmp            %g5, %o1
268         be,a            1f
269          sub            %o0, 2, %o5
270 1:      andcc           %g4, 0xff, %g5
271         be              9f
272          cmp            %g5, %o1
273         be,a            1f
274          sub            %o0, 1, %o5
275 1:      b               7b
276          ld             [%o0], %g4
277 9:      retl
278          mov            %o5, %o0
279 END(strrchr)
281 weak_alias(strchr, index)
282 weak_alias(strrchr, rindex)
283 libc_hidden_builtin_def (strchr)
284 libc_hidden_builtin_def (strrchr)