]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/strchr.S
Update.
[thirdparty/glibc.git] / sysdeps / i386 / strchr.S
1 /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
2 For Intel 80x86, x>=3.
3 Copyright (C) 1994-1997,1999,2000,2002,2003 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
6 Some optimisations by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
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 #include <sysdep.h>
24 #include "asm-syntax.h"
25 #include "bp-sym.h"
26 #include "bp-asm.h"
27
28 #define PARMS LINKAGE+4 /* space for 1 saved reg */
29 #define RTN PARMS
30 #define STR RTN+RTN_SIZE
31 #define CHR STR+PTR_SIZE
32
33 .text
34 ENTRY (BP_SYM (strchr))
35 ENTER
36
37 pushl %edi /* Save callee-safe registers used here. */
38 movl STR(%esp), %eax
39 movl CHR(%esp), %edx
40 CHECK_BOUNDS_LOW (%eax, STR(%esp))
41
42 /* At the moment %edx contains C. What we need for the
43 algorithm is C in all bytes of the dword. Avoid
44 operations on 16 bit words because these require an
45 prefix byte (and one more cycle). */
46 movb %dl, %dh /* now it is 0|0|c|c */
47 movl %edx, %ecx
48 shll $16, %edx /* now it is c|c|0|0 */
49 movw %cx, %dx /* and finally c|c|c|c */
50
51 /* Before we start with the main loop we process single bytes
52 until the source pointer is aligned. This has two reasons:
53 1. aligned 32-bit memory access is faster
54 and (more important)
55 2. we process in the main loop 32 bit in one step although
56 we don't know the end of the string. But accessing at
57 4-byte alignment guarantees that we never access illegal
58 memory if this would not also be done by the trivial
59 implementation (this is because all processor inherent
60 boundaries are multiples of 4. */
61
62 testb $3, %al /* correctly aligned ? */
63 jz L(11) /* yes => begin loop */
64 movb (%eax), %cl /* load byte in question (we need it twice) */
65 cmpb %cl, %dl /* compare byte */
66 je L(6) /* target found => return */
67 testb %cl, %cl /* is NUL? */
68 jz L(2) /* yes => return NULL */
69 incl %eax /* increment pointer */
70
71 testb $3, %al /* correctly aligned ? */
72 jz L(11) /* yes => begin loop */
73 movb (%eax), %cl /* load byte in question (we need it twice) */
74 cmpb %cl, %dl /* compare byte */
75 je L(6) /* target found => return */
76 testb %cl, %cl /* is NUL? */
77 jz L(2) /* yes => return NULL */
78 incl %eax /* increment pointer */
79
80 testb $3, %al /* correctly aligned ? */
81 jz L(11) /* yes => begin loop */
82 movb (%eax), %cl /* load byte in question (we need it twice) */
83 cmpb %cl, %dl /* compare byte */
84 je L(6) /* target found => return */
85 testb %cl, %cl /* is NUL? */
86 jz L(2) /* yes => return NULL */
87 incl %eax /* increment pointer */
88
89 /* No we have reached alignment. */
90 jmp L(11) /* begin loop */
91
92 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
93 change any of the hole bits of LONGWORD.
94
95 1) Is this safe? Will it catch all the zero bytes?
96 Suppose there is a byte with all zeros. Any carry bits
97 propagating from its left will fall into the hole at its
98 least significant bit and stop. Since there will be no
99 carry from its most significant bit, the LSB of the
100 byte to the left will be unchanged, and the zero will be
101 detected.
102
103 2) Is this worthwhile? Will it ignore everything except
104 zero bytes? Suppose every byte of LONGWORD has a bit set
105 somewhere. There will be a carry into bit 8. If bit 8
106 is set, this will carry into bit 16. If bit 8 is clear,
107 one of bits 9-15 must be set, so there will be a carry
108 into bit 16. Similarly, there will be a carry into bit
109 24. If one of bits 24-31 is set, there will be a carry
110 into bit 32 (=carry flag), so all of the hole bits will
111 be changed.
112
113 3) But wait! Aren't we looking for C, not zero?
114 Good point. So what we do is XOR LONGWORD with a longword,
115 each of whose bytes is C. This turns each byte that is C
116 into a zero. */
117
118 /* Each round the main loop processes 16 bytes. */
119
120 ALIGN(4)
121
122 L(1): addl $16, %eax /* adjust pointer for whole round */
123
124 L(11): movl (%eax), %ecx /* get word (= 4 bytes) in question */
125 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
126 are now 0 */
127 movl $0xfefefeff, %edi /* magic value */
128 addl %ecx, %edi /* add the magic value to the word. We get
129 carry bits reported for each byte which
130 is *not* C */
131
132 /* According to the algorithm we had to reverse the effect of the
133 XOR first and then test the overflow bits. But because the
134 following XOR would destroy the carry flag and it would (in a
135 representation with more than 32 bits) not alter then last
136 overflow, we can now test this condition. If no carry is signaled
137 no overflow must have occurred in the last byte => it was 0. */
138 jnc L(7)
139
140 /* We are only interested in carry bits that change due to the
141 previous add, so remove original bits */
142 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
143
144 /* Now test for the other three overflow bits. */
145 orl $0xfefefeff, %edi /* set all non-carry bits */
146 incl %edi /* add 1: if one carry bit was *not* set
147 the addition will not result in 0. */
148
149 /* If at least one byte of the word is C we don't get 0 in %edi. */
150 jnz L(7) /* found it => return pointer */
151
152 /* Now we made sure the dword does not contain the character we are
153 looking for. But because we deal with strings we have to check
154 for the end of string before testing the next dword. */
155
156 xorl %edx, %ecx /* restore original dword without reload */
157 movl $0xfefefeff, %edi /* magic value */
158 addl %ecx, %edi /* add the magic value to the word. We get
159 carry bits reported for each byte which
160 is *not* 0 */
161 jnc L(2) /* highest byte is NUL => return NULL */
162 xorl %ecx, %edi /* (word+magic)^word */
163 orl $0xfefefeff, %edi /* set all non-carry bits */
164 incl %edi /* add 1: if one carry bit was *not* set
165 the addition will not result in 0. */
166 jnz L(2) /* found NUL => return NULL */
167
168 movl 4(%eax), %ecx /* get word (= 4 bytes) in question */
169 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
170 are now 0 */
171 movl $0xfefefeff, %edi /* magic value */
172 addl %ecx, %edi /* add the magic value to the word. We get
173 carry bits reported for each byte which
174 is *not* C */
175 jnc L(71) /* highest byte is C => return pointer */
176 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
177 orl $0xfefefeff, %edi /* set all non-carry bits */
178 incl %edi /* add 1: if one carry bit was *not* set
179 the addition will not result in 0. */
180 jnz L(71) /* found it => return pointer */
181 xorl %edx, %ecx /* restore original dword without reload */
182 movl $0xfefefeff, %edi /* magic value */
183 addl %ecx, %edi /* add the magic value to the word. We get
184 carry bits reported for each byte which
185 is *not* 0 */
186 jnc L(2) /* highest byte is NUL => return NULL */
187 xorl %ecx, %edi /* (word+magic)^word */
188 orl $0xfefefeff, %edi /* set all non-carry bits */
189 incl %edi /* add 1: if one carry bit was *not* set
190 the addition will not result in 0. */
191 jnz L(2) /* found NUL => return NULL */
192
193 movl 8(%eax), %ecx /* get word (= 4 bytes) in question */
194 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
195 are now 0 */
196 movl $0xfefefeff, %edi /* magic value */
197 addl %ecx, %edi /* add the magic value to the word. We get
198 carry bits reported for each byte which
199 is *not* C */
200 jnc L(72) /* highest byte is C => return pointer */
201 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
202 orl $0xfefefeff, %edi /* set all non-carry bits */
203 incl %edi /* add 1: if one carry bit was *not* set
204 the addition will not result in 0. */
205 jnz L(72) /* found it => return pointer */
206 xorl %edx, %ecx /* restore original dword without reload */
207 movl $0xfefefeff, %edi /* magic value */
208 addl %ecx, %edi /* add the magic value to the word. We get
209 carry bits reported for each byte which
210 is *not* 0 */
211 jnc L(2) /* highest byte is NUL => return NULL */
212 xorl %ecx, %edi /* (word+magic)^word */
213 orl $0xfefefeff, %edi /* set all non-carry bits */
214 incl %edi /* add 1: if one carry bit was *not* set
215 the addition will not result in 0. */
216 jnz L(2) /* found NUL => return NULL */
217
218 movl 12(%eax), %ecx /* get word (= 4 bytes) in question */
219 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
220 are now 0 */
221 movl $0xfefefeff, %edi /* magic value */
222 addl %ecx, %edi /* add the magic value to the word. We get
223 carry bits reported for each byte which
224 is *not* C */
225 jnc L(73) /* highest byte is C => return pointer */
226 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
227 orl $0xfefefeff, %edi /* set all non-carry bits */
228 incl %edi /* add 1: if one carry bit was *not* set
229 the addition will not result in 0. */
230 jnz L(73) /* found it => return pointer */
231 xorl %edx, %ecx /* restore original dword without reload */
232 movl $0xfefefeff, %edi /* magic value */
233 addl %ecx, %edi /* add the magic value to the word. We get
234 carry bits reported for each byte which
235 is *not* 0 */
236 jnc L(2) /* highest byte is NUL => return NULL */
237 xorl %ecx, %edi /* (word+magic)^word */
238 orl $0xfefefeff, %edi /* set all non-carry bits */
239 incl %edi /* add 1: if one carry bit was *not* set
240 the addition will not result in 0. */
241 jz L(1) /* no NUL found => restart loop */
242
243 L(2): /* Return NULL. */
244 xorl %eax, %eax
245 RETURN_NULL_BOUNDED_POINTER
246 popl %edi /* restore saved register content */
247
248 LEAVE
249 RET_PTR
250
251 L(73): addl $4, %eax /* adjust pointer */
252 L(72): addl $4, %eax
253 L(71): addl $4, %eax
254
255 /* We now scan for the byte in which the character was matched.
256 But we have to take care of the case that a NUL char is
257 found before this in the dword. Note that we XORed %ecx
258 with the byte we're looking for, therefore the tests below look
259 reversed. */
260
261 L(7): testb %cl, %cl /* is first byte C? */
262 jz L(6) /* yes => return pointer */
263 cmpb %dl, %cl /* is first byte NUL? */
264 je L(2) /* yes => return NULL */
265 incl %eax /* it's not in the first byte */
266
267 testb %ch, %ch /* is second byte C? */
268 jz L(6) /* yes => return pointer */
269 cmpb %dl, %ch /* is second byte NUL? */
270 je L(2) /* yes => return NULL? */
271 incl %eax /* it's not in the second byte */
272
273 shrl $16, %ecx /* make upper byte accessible */
274 testb %cl, %cl /* is third byte C? */
275 jz L(6) /* yes => return pointer */
276 cmpb %dl, %cl /* is third byte NUL? */
277 je L(2) /* yes => return NULL */
278
279 /* It must be in the fourth byte and it cannot be NUL. */
280 incl %eax
281
282 L(6):
283 CHECK_BOUNDS_HIGH (%eax, STR(%esp), jb)
284 RETURN_BOUNDED_POINTER (STR(%esp))
285 popl %edi /* restore saved register content */
286
287 LEAVE
288 RET_PTR
289 END (BP_SYM (strchr))
290
291 weak_alias (BP_SYM (strchr), BP_SYM (index))
292 libc_hidden_builtin_def (strchr)