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