1 /* strrchr (str, ch) -- Return pointer to last occurrence of CH in STR.
3 Copyright (C) 1994-2016 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>
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, see
20 <http://www.gnu.org/licenses/>. */
23 #include "asm-syntax.h"
25 #define PARMS 4+8 /* space for 2 saved regs */
33 pushl %edi /* Save callee-safe registers used here. */
34 cfi_adjust_cfa_offset (4)
35 cfi_rel_offset (edi, 0)
37 cfi_adjust_cfa_offset (4)
41 cfi_rel_offset (esi, 0)
44 /* At the moment %ecx contains C. What we need for the
45 algorithm is C in all bytes of the dword. Avoid
46 operations on 16 bit words because these require an
47 prefix byte (and one more cycle). */
48 movb %cl, %ch /* now it is 0|0|c|c */
50 shll $16, %ecx /* now it is c|c|0|0 */
51 movw %dx, %cx /* and finally c|c|c|c */
53 /* Before we start with the main loop we process single bytes
54 until the source pointer is aligned. This has two reasons:
55 1. aligned 32-bit memory access is faster
57 2. we process in the main loop 32 bit in one step although
58 we don't know the end of the string. But accessing at
59 4-byte alignment guarantees that we never access illegal
60 memory if this would not also be done by the trivial
61 implementation (this is because all processor inherent
62 boundaries are multiples of 4. */
64 testl $3, %esi /* correctly aligned ? */
65 jz L(19) /* yes => begin loop */
66 movb (%esi), %dl /* load byte in question (we need it twice) */
67 cmpb %dl, %cl /* compare byte */
68 jne L(11) /* target found => return */
69 movl %esi, %eax /* remember pointer as possible result */
70 L(11): orb %dl, %dl /* is NUL? */
71 jz L(2) /* yes => return NULL */
72 incl %esi /* increment pointer */
74 testl $3, %esi /* correctly aligned ? */
75 jz L(19) /* yes => begin loop */
76 movb (%esi), %dl /* load byte in question (we need it twice) */
77 cmpb %dl, %cl /* compare byte */
78 jne L(12) /* target found => return */
79 movl %esi, %eax /* remember pointer as result */
80 L(12): orb %dl, %dl /* is NUL? */
81 jz L(2) /* yes => return NULL */
82 incl %esi /* increment pointer */
84 testl $3, %esi /* correctly aligned ? */
85 jz L(19) /* yes => begin loop */
86 movb (%esi), %dl /* load byte in question (we need it twice) */
87 cmpb %dl, %cl /* compare byte */
88 jne L(13) /* target found => return */
89 movl %esi, %eax /* remember pointer as result */
90 L(13): orb %dl, %dl /* is NUL? */
91 jz L(2) /* yes => return NULL */
92 incl %esi /* increment pointer */
94 /* No we have reached alignment. */
95 jmp L(19) /* begin loop */
97 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
98 change any of the hole bits of LONGWORD.
100 1) Is this safe? Will it catch all the zero bytes?
101 Suppose there is a byte with all zeros. Any carry bits
102 propagating from its left will fall into the hole at its
103 least significant bit and stop. Since there will be no
104 carry from its most significant bit, the LSB of the
105 byte to the left will be unchanged, and the zero will be
108 2) Is this worthwhile? Will it ignore everything except
109 zero bytes? Suppose every byte of LONGWORD has a bit set
110 somewhere. There will be a carry into bit 8. If bit 8
111 is set, this will carry into bit 16. If bit 8 is clear,
112 one of bits 9-15 must be set, so there will be a carry
113 into bit 16. Similarly, there will be a carry into bit
114 24. If one of bits 24-31 is set, there will be a carry
115 into bit 32 (=carry flag), so all of the hole bits will
118 3) But wait! Aren't we looking for C, not zero?
119 Good point. So what we do is XOR LONGWORD with a longword,
120 each of whose bytes is C. This turns each byte that is C
123 /* Each round the main loop processes 16 bytes. */
125 /* Jump to here when the character is detected. We chose this
126 way around because the character one is looking for is not
127 as frequent as the rest and taking a conditional jump is more
128 expensive than ignoring it.
130 Some more words to the code below: it might not be obvious why
131 we decrement the source pointer here. In the loop the pointer
132 is not pre-incremented and so it still points before the word
133 we are looking at. But you should take a look at the instruction
134 which gets executed before we get into the loop: `addl $16, %esi'.
135 This makes the following subs into adds. */
137 /* These fill bytes make the main loop be correctly aligned.
138 We cannot use align because it is not the following instruction
139 which should be aligned. */
142 /* Profiling adds some code and so changes the alignment. */
146 L(4): subl $4, %esi /* adjust pointer */
149 L(43): testl $0xff000000, %edx /* is highest byte == C? */
150 jnz L(33) /* no => try other bytes */
151 leal 15(%esi), %eax /* store address as result */
152 jmp L(1) /* and start loop again */
154 L(3): subl $4, %esi /* adjust pointer */
157 L(33): testl $0xff0000, %edx /* is C in third byte? */
158 jnz L(51) /* no => try other bytes */
159 leal 14(%esi), %eax /* store address as result */
160 jmp L(1) /* and start loop again */
163 /* At this point we know that the byte is in one of the lower bytes.
164 We make a guess and correct it if necessary. This reduces the
165 number of necessary jumps. */
166 leal 12(%esi), %eax /* guess address of lowest byte as result */
167 testb %dh, %dh /* is guess correct? */
168 jnz L(1) /* yes => start loop */
169 leal 13(%esi), %eax /* correct guess to second byte */
171 L(1): addl $16, %esi /* increment pointer for full round */
173 L(19): movl (%esi), %edx /* get word (= 4 bytes) in question */
174 movl $0xfefefeff, %edi /* magic value */
175 addl %edx, %edi /* add the magic value to the word. We get
176 carry bits reported for each byte which
179 /* According to the algorithm we had to reverse the effect of the
180 XOR first and then test the overflow bits. But because the
181 following XOR would destroy the carry flag and it would (in a
182 representation with more than 32 bits) not alter then last
183 overflow, we can now test this condition. If no carry is signaled
184 no overflow must have occurred in the last byte => it was 0. */
186 jnc L(20) /* found NUL => check last word */
188 /* We are only interested in carry bits that change due to the
189 previous add, so remove original bits */
190 xorl %edx, %edi /* (word+magic)^word */
192 /* Now test for the other three overflow bits. */
193 orl $0xfefefeff, %edi /* set all non-carry bits */
194 incl %edi /* add 1: if one carry bit was *not* set
195 the addition will not result in 0. */
197 /* If at least one byte of the word is C we don't get 0 in %edi. */
198 jnz L(20) /* found NUL => check last word */
200 /* Now we made sure the dword does not contain the character we are
201 looking for. But because we deal with strings we have to check
202 for the end of string before testing the next dword. */
204 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
206 movl $0xfefefeff, %edi /* magic value */
207 addl %edx, %edi /* add the magic value to the word. We get
208 carry bits reported for each byte which
210 jnc L(4) /* highest byte is C => examine dword */
211 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
212 orl $0xfefefeff, %edi /* set all non-carry bits */
213 incl %edi /* add 1: if one carry bit was *not* set
214 the addition will not result in 0. */
215 jnz L(3) /* C is detected in the word => examine it */
217 movl 4(%esi), %edx /* get word (= 4 bytes) in question */
218 movl $0xfefefeff, %edi /* magic value */
219 addl %edx, %edi /* add the magic value to the word. We get
220 carry bits reported for each byte which
222 jnc L(21) /* found NUL => check last word */
223 xorl %edx, %edi /* (word+magic)^word */
224 orl $0xfefefeff, %edi /* set all non-carry bits */
225 incl %edi /* add 1: if one carry bit was *not* set
226 the addition will not result in 0. */
227 jnz L(21) /* found NUL => check last word */
228 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
230 movl $0xfefefeff, %edi /* magic value */
231 addl %edx, %edi /* add the magic value to the word. We get
232 carry bits reported for each byte which
234 jnc L(41) /* highest byte is C => examine dword */
235 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
236 orl $0xfefefeff, %edi /* set all non-carry bits */
237 incl %edi /* add 1: if one carry bit was *not* set
238 the addition will not result in 0. */
239 jnz L(31) /* C is detected in the word => examine it */
241 movl 8(%esi), %edx /* get word (= 4 bytes) in question */
242 movl $0xfefefeff, %edi /* magic value */
243 addl %edx, %edi /* add the magic value to the word. We get
244 carry bits reported for each byte which
246 jnc L(22) /* found NUL => check last word */
247 xorl %edx, %edi /* (word+magic)^word */
248 orl $0xfefefeff, %edi /* set all non-carry bits */
249 incl %edi /* add 1: if one carry bit was *not* set
250 the addition will not result in 0. */
251 jnz L(22) /* found NUL => check last word */
252 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
254 movl $0xfefefeff, %edi /* magic value */
255 addl %edx, %edi /* add the magic value to the word. We get
256 carry bits reported for each byte which
258 jnc L(42) /* highest byte is C => examine dword */
259 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
260 orl $0xfefefeff, %edi /* set all non-carry bits */
261 incl %edi /* add 1: if one carry bit was *not* set
262 the addition will not result in 0. */
263 jnz L(32) /* C is detected in the word => examine it */
265 movl 12(%esi), %edx /* get word (= 4 bytes) in question */
266 movl $0xfefefeff, %edi /* magic value */
267 addl %edx, %edi /* add the magic value to the word. We get
268 carry bits reported for each byte which
270 jnc L(23) /* found NUL => check last word */
271 xorl %edx, %edi /* (word+magic)^word */
272 orl $0xfefefeff, %edi /* set all non-carry bits */
273 incl %edi /* add 1: if one carry bit was *not* set
274 the addition will not result in 0. */
275 jnz L(23) /* found NUL => check last word */
276 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
278 movl $0xfefefeff, %edi /* magic value */
279 addl %edx, %edi /* add the magic value to the word. We get
280 carry bits reported for each byte which
282 jnc L(43) /* highest byte is C => examine dword */
283 xorl %edx, %edi /* ((word^charmask)+magic)^(word^charmask) */
284 orl $0xfefefeff, %edi /* set all non-carry bits */
285 incl %edi /* add 1: if one carry bit was *not* set
286 the addition will not result in 0. */
287 jz L(1) /* C is not detected => restart loop */
288 jmp L(33) /* examine word */
290 L(23): addl $4, %esi /* adjust pointer */
294 /* What remains to do is to test which byte the NUL char is and
295 whether the searched character appears in one of the bytes
296 before. A special case is that the searched byte maybe NUL.
297 In this case a pointer to the terminating NUL char has to be
300 L(20): cmpb %cl, %dl /* is first byte == C? */
301 jne L(24) /* no => skip */
302 movl %esi, %eax /* store address as result */
303 L(24): testb %dl, %dl /* is first byte == NUL? */
304 jz L(2) /* yes => return */
306 cmpb %cl, %dh /* is second byte == C? */
307 jne L(25) /* no => skip */
308 leal 1(%esi), %eax /* store address as result */
309 L(25): testb %dh, %dh /* is second byte == NUL? */
310 jz L(2) /* yes => return */
312 shrl $16,%edx /* make upper bytes accessible */
313 cmpb %cl, %dl /* is third byte == C */
314 jne L(26) /* no => skip */
315 leal 2(%esi), %eax /* store address as result */
316 L(26): testb %dl, %dl /* is third byte == NUL */
317 jz L(2) /* yes => return */
319 cmpb %cl, %dh /* is fourth byte == C */
320 jne L(2) /* no => skip */
321 leal 3(%esi), %eax /* store address as result */
323 L(2): popl %esi /* restore saved register content */
324 cfi_adjust_cfa_offset (-4)
327 cfi_adjust_cfa_offset (-4)
333 weak_alias (strrchr, rindex)
334 libc_hidden_builtin_def (strrchr)