1 /* strchr (str, ch) -- Return pointer to last occurrence of CH in STR.
3 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
5 Some optimisations by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
6 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 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 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "asm-syntax.h"
34 pushl %edi /* Save callee-safe registers used here. */
38 movl 12(%esp), %esi /* get string pointer */
39 movl 16(%esp), %ecx /* get character we are looking for */
41 /* At the moment %ecx contains C. What we need for the
42 algorithm is C in all bytes of the dword. Avoid
43 operations on 16 bit words because these require an
44 prefix byte (and one more cycle). */
45 movb %cl, %ch /* now it is 0|0|c|c */
47 shll $16, %ecx /* now it is c|c|0|0 */
48 movw %dx, %cx /* and finally c|c|c|c */
50 /* Before we start with the main loop we process single bytes
51 until the source pointer is aligned. This has two reasons:
52 1. aligned 32-bit memory access is faster
54 2. we process in the main loop 32 bit in one step although
55 we don't know the end of the string. But accessing at
56 4-byte alignment guarantees that we never access illegal
57 memory if this would not also be done by the trivial
58 implementation (this is because all processor inherant
59 boundaries are multiples of 4. */
61 testb $3, %esi /* correctly aligned ? */
62 jz L19 /* yes => begin loop */
63 movb (%esi), %dl /* load byte in question (we need it twice) */
64 cmpb %dl, %cl /* compare byte */
65 jne L11 /* target found => return */
66 movl %esi, %eax /* remember pointer as possible result */
67 L11: orb %dl, %dl /* is NUL? */
68 jz L2 /* yes => return NULL */
69 incl %esi /* increment pointer */
71 testb $3, %esi /* correctly aligned ? */
72 jz L19 /* yes => begin loop */
73 movb (%esi), %dl /* load byte in question (we need it twice) */
74 cmpb %dl, %cl /* compare byte */
75 jne L12 /* target found => return */
76 movl %esi, %eax /* remember pointer as result */
77 L12: orb %dl, %dl /* is NUL? */
78 jz L2 /* yes => return NULL */
79 incl %esi /* increment pointer */
81 testb $3, %esi /* correctly aligned ? */
82 jz L19 /* yes => begin loop */
83 movb (%esi), %dl /* load byte in question (we need it twice) */
84 cmpb %dl, %cl /* compare byte */
85 jne L13 /* target found => return */
86 movl %esi, %eax /* remember pointer as result */
87 L13: orb %cl, %cl /* is NUL? */
88 jz L2 /* yes => return NULL */
89 incl %esi /* increment pointer */
91 /* No we have reached alignment. */
92 jmp L19 /* begin loop */
94 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
95 change any of the hole bits of LONGWORD.
97 1) Is this safe? Will it catch all the zero bytes?
98 Suppose there is a byte with all zeros. Any carry bits
99 propagating from its left will fall into the hole at its
100 least significant bit and stop. Since there will be no
101 carry from its most significant bit, the LSB of the
102 byte to the left will be unchanged, and the zero will be
105 2) Is this worthwhile? Will it ignore everything except
106 zero bytes? Suppose every byte of LONGWORD has a bit set
107 somewhere. There will be a carry into bit 8. If bit 8
108 is set, this will carry into bit 16. If bit 8 is clear,
109 one of bits 9-15 must be set, so there will be a carry
110 into bit 16. Similarly, there will be a carry into bit
111 24. If one of bits 24-31 is set, there will be a carry
112 into bit 32 (=carry flag), so all of the hole bits will
115 3) But wait! Aren't we looking for C, not zero?
116 Good point. So what we do is XOR LONGWORD with a longword,
117 each of whose bytes is C. This turns each byte that is C
120 /* Each round the main loop processes 16 bytes. */
122 /* Jump to here when the character is detected. We chose this
123 way around because the character one is looking for is not
124 as frequent as the rest and taking a conditional jump is more
125 expensive than ignoring it.
127 Some more words to the code below: it might not be obvious why
128 we decrement the source pointer here. In the loop the pointer
129 is not pre-incremented and so it still points before the word
130 we are looking at. But you should take a look at the instruction
131 which gets executed before we get into the loop: `addl $16, %esi'.
132 This makes the following subs into adds. */
134 /* These fill bytes make the main loop be correctly aligned.
135 We cannot use align because it is not the following instruction
136 which should be aligned. */
137 .byte 0, 0, 0, 0, 0, 0, 0, 0
139 L4: subl $4, %esi /* adjust pointer */
142 L43: testl $0xff000000, %edx /* is highest byte == C? */
143 jnz L33 /* no => try other bytes */
144 leal 15(%esi), %eax /* store address as result */
145 jmp L1 /* and start loop again */
147 L3: subl $4, %esi /* adjust pointer */
150 L33: testl $0xff0000, %edx /* is C in third byte? */
151 jnz L51 /* no => try other bytes */
152 leal 14(%esi), %eax /* store address as result */
153 jmp L1 /* and start loop again */
156 /* At this point we know that the byte is in one of the lower bytes.
157 We make a guess and correct it if necessary. This reduces the
158 number of necessary jumps. */
159 leal 12(%esi), %eax /* guess address of lowest byte as result */
160 testb %dh, %dh /* is guess correct? */
161 jnz L1 /* yes => start loop */
162 leal 13(%esi), %eax /* correct guess to second byte */
164 L1: addl $16, %esi /* increment pointer for full round */
166 L19: movl (%esi), %edx /* get word (= 4 bytes) in question */
167 movl $0xfefefeff, %edi /* magic value */
168 addl %edx, %edi /* add the magic value to the word. We get
169 carry bits reported for each byte which
172 /* According to the algorithm we had to reverse the effect of the
173 XOR first and then test the overflow bits. But because the
174 following XOR would destroy the carry flag and it would (in a
175 representation with more than 32 bits) not alter then last
176 overflow, we can now test this condition. If no carry is signaled
177 no overflow must have occured in the last byte => it was 0. */
179 jnc L20 /* found NUL => check last word */
181 /* We are only interested in carry bits that change due to the
182 previous add, so remove original bits */
183 xorl %edx, %edi /* (word+magic)^word */
185 /* Now test for the other three overflow bits. */
186 orl $0xfefefeff, %edi /* set all non-carry bits */
187 incl %edi /* add 1: if one carry bit was *not* set
188 the addition will not result in 0. */
190 /* If at least one byte of the word is C we don't get 0 in %edi. */
191 jnz L20 /* found NUL => check last word */
193 /* Now we made sure the dword does not contain the character we are
194 looking for. But because we deal with strings we have to check
195 for the end of string before testing the next dword. */
197 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
199 movl $0xfefefeff, %edi /* magic value */
200 addl %edx, %edi /* add the magic value to the word. We get
201 carry bits reported for each byte which
203 jnc L4 /* highest byte is C => examine dword */
204 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
205 orl $0xfefefeff, %edi /* set all non-carry bits */
206 incl %edi /* add 1: if one carry bit was *not* set
207 the addition will not result in 0. */
208 jnz L3 /* C is detected in the word => examine it */
210 movl 4(%esi), %edx /* get word (= 4 bytes) in question */
211 movl $0xfefefeff, %edi /* magic value */
212 addl %edx, %edi /* add the magic value to the word. We get
213 carry bits reported for each byte which
215 jnc L21 /* found NUL => check last word */
216 xorl %edx, %edi /* (word+magic)^word */
217 orl $0xfefefeff, %edi /* set all non-carry bits */
218 incl %edi /* add 1: if one carry bit was *not* set
219 the addition will not result in 0. */
220 jnz L21 /* found NUL => check last word */
221 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
223 movl $0xfefefeff, %edi /* magic value */
224 addl %edx, %edi /* add the magic value to the word. We get
225 carry bits reported for each byte which
227 jnc L41 /* highest byte is C => examine dword */
228 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
229 orl $0xfefefeff, %edi /* set all non-carry bits */
230 incl %edi /* add 1: if one carry bit was *not* set
231 the addition will not result in 0. */
232 jnz L31 /* C is detected in the word => examine it */
234 movl 8(%esi), %edx /* get word (= 4 bytes) in question */
235 movl $0xfefefeff, %edi /* magic value */
236 addl %edx, %edi /* add the magic value to the word. We get
237 carry bits reported for each byte which
239 jnc L22 /* found NUL => check last word */
240 xorl %edx, %edi /* (word+magic)^word */
241 orl $0xfefefeff, %edi /* set all non-carry bits */
242 incl %edi /* add 1: if one carry bit was *not* set
243 the addition will not result in 0. */
244 jnz L22 /* found NUL => check last word */
245 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
247 movl $0xfefefeff, %edi /* magic value */
248 addl %edx, %edi /* add the magic value to the word. We get
249 carry bits reported for each byte which
251 jnc L42 /* highest byte is C => examine dword */
252 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
253 orl $0xfefefeff, %edi /* set all non-carry bits */
254 incl %edi /* add 1: if one carry bit was *not* set
255 the addition will not result in 0. */
256 jnz L32 /* C is detected in the word => examine it */
258 movl 12(%esi), %edx /* get word (= 4 bytes) in question */
259 movl $0xfefefeff, %edi /* magic value */
260 addl %edx, %edi /* add the magic value to the word. We get
261 carry bits reported for each byte which
263 jnc L23 /* found NUL => check last word */
264 xorl %edx, %edi /* (word+magic)^word */
265 orl $0xfefefeff, %edi /* set all non-carry bits */
266 incl %edi /* add 1: if one carry bit was *not* set
267 the addition will not result in 0. */
268 jnz L23 /* found NUL => check last word */
269 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
271 movl $0xfefefeff, %edi /* magic value */
272 addl %edx, %edi /* add the magic value to the word. We get
273 carry bits reported for each byte which
275 jnc L43 /* highest byte is C => examine dword */
276 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
277 orl $0xfefefeff, %edi /* set all non-carry bits */
278 incl %edi /* add 1: if one carry bit was *not* set
279 the addition will not result in 0. */
280 jz L1 /* C is not detected => restart loop */
281 jmp L33 /* examine word */
283 L23: addl $4, %esi /* adjust pointer */
287 /* What remains to do is to test which byte the NUL char is and
288 whether the searched character appears in one of the bytes
289 before. A special case is that the searched byte maybe NUL.
290 In this case a pointer to the terminating NUL char has to be
293 L20: cmpb %cl, %dl /* is first byte == C? */
294 jne L24 /* no => skip */
295 movl %esi, %eax /* store address as result */
296 L24: testb %dl, %dl /* is first byte == NUL? */
297 jz L2 /* yes => return */
299 cmpb %cl, %dh /* is second byte == C? */
300 jne L25 /* no => skip */
301 leal 1(%esi), %eax /* store address as result */
302 L25: testb %dh, %dh /* is second byte == NUL? */
303 jz L2 /* yes => return */
305 shrl $16,%edx /* make upper bytes accessible */
306 cmpb %cl, %dl /* is third byte == C */
307 jne L26 /* no => skip */
308 leal 2(%esi), %eax /* store address as result */
309 L26: testb %dl, %dl /* is third byte == NUL */
310 jz L2 /* yes => return */
312 cmpb %cl, %dh /* is fourth byte == C */
313 jne L2 /* no => skip */
314 leal 3(%esi), %eax /* store address as result */
316 L2: popl %esi /* restore saved register content */
321 weak_alias (strrchr, rindex)