]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/i386/strrchr.S
Tue May 14 14:07:10 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / i386 / strrchr.S
CommitLineData
8f5ca04b
RM
1/* strchr (str, ch) -- Return pointer to last occurrence of CH in STR.
2For Intel 80x86, x>=3.
3Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
5Some optimisations by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
6This file is part of the GNU C Library.
7
8The GNU C Library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Library General Public License as
10published by the Free Software Foundation; either version 2 of the
11License, or (at your option) any later version.
12
13The GNU C Library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Library General Public License for more details.
17
18You should have received a copy of the GNU Library General Public
19License along with the GNU C Library; see the file COPYING.LIB. If
20not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
23#include <sysdep.h>
24#include "asm-syntax.h"
25
26/*
27 INPUT PARAMETERS:
28 str (sp + 4)
29 ch (sp + 8)
30*/
31
32 .text
33ENTRY (strrchr)
34 pushl %edi /* Save callee-safe registers used here. */
35 pushl %esi
36
37 xorl %eax, %eax
38 movl 12(%esp), %esi /* get string pointer */
39 movl 16(%esp), %ecx /* get character we are looking for */
40
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 */
46 movl %ecx, %edx
47 shll $16, %ecx /* now it is c|c|0|0 */
48 movw %dx, %cx /* and finally c|c|c|c */
49
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
53 and (more important)
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. */
60
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 */
67L11: orb %dl, %dl /* is NUL? */
68 jz L2 /* yes => return NULL */
69 incl %esi /* increment pointer */
70
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 */
77L12: orb %dl, %dl /* is NUL? */
78 jz L2 /* yes => return NULL */
79 incl %esi /* increment pointer */
80
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 */
87L13: orb %cl, %cl /* is NUL? */
88 jz L2 /* yes => return NULL */
89 incl %esi /* increment pointer */
90
91 /* No we have reached alignment. */
92 jmp L19 /* begin loop */
93
94 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
95 change any of the hole bits of LONGWORD.
96
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
103 detected.
104
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
113 be changed.
114
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
118 into a zero. */
119
120 /* Each round the main loop processes 16 bytes. */
121
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.
126
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. */
133
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
138
139L4: subl $4, %esi /* adjust pointer */
140L41: subl $4, %esi
141L42: subl $4, %esi
142L43: 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 */
146
147L3: subl $4, %esi /* adjust pointer */
148L31: subl $4, %esi
149L32: subl $4, %esi
150L33: 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 */
154
155L51:
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 */
163
164L1: addl $16, %esi /* increment pointer for full round */
165
166L19: 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
170 is *not* 0 */
171
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. */
178
179 jnc L20 /* found NUL => check last word */
180
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 */
184
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. */
189
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 */
192
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. */
196
197 xorl %ecx, %edx /* XOR with word c|c|c|c => bytes of str == c
198 are now 0 */
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
202 is *not* 0 */
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 */
209
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
214 is *not* 0 */
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
222 are now 0 */
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
226 is *not* 0 */
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 */
233
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
238 is *not* 0 */
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
246 are now 0 */
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
250 is *not* 0 */
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 */
257
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
262 is *not* 0 */
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
270 are now 0 */
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
274 is *not* 0 */
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 */
282
283L23: addl $4, %esi /* adjust pointer */
284L22: addl $4, %esi
285L21: addl $4, %esi
286
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
291 returned. */
292
293L20: cmpb %cl, %dl /* is first byte == C? */
294 jne L24 /* no => skip */
295 movl %esi, %eax /* store address as result */
296L24: testb %dl, %dl /* is first byte == NUL? */
297 jz L2 /* yes => return */
298
299 cmpb %cl, %dh /* is second byte == C? */
300 jne L25 /* no => skip */
301 leal 1(%esi), %eax /* store address as result */
302L25: testb %dh, %dh /* is second byte == NUL? */
303 jz L2 /* yes => return */
304
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 */
309L26: testb %dl, %dl /* is third byte == NUL */
310 jz L2 /* yes => return */
311
312 cmpb %cl, %dh /* is fourth byte == C */
313 jne L2 /* no => skip */
314 leal 3(%esi), %eax /* store address as result */
315
316L2: popl %esi /* restore saved register content */
317 popl %edi
318
319 ret
320
321weak_alias (strrchr, rindex)