1 /* rawmemchr (str, ch) -- Return pointer to first occurrence of CH in STR.
3 Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 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 Optimised a little by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
7 This version is developed using the same algorithm as the fast C
8 version which carries the following introduction:
9 Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
10 with help from Dan Sahlin (dan@sics.se) and
11 commentary by Jim Blandy (jimb@ai.mit.edu);
12 adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
13 and implemented by Roland McGrath (roland@ai.mit.edu).
15 The GNU C Library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 2.1 of the License, or (at your option) any later version.
20 The GNU C Library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with the GNU C Library; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 #include "asm-syntax.h"
35 #define PARMS LINKAGE+4 /* space for 1 saved reg */
37 #define STR RTN+RTN_SIZE
38 #define CHR STR+PTR_SIZE
41 ENTRY (BP_SYM (__rawmemchr))
44 /* Save callee-safe register used in this function. */
47 /* Load parameters into registers. */
50 CHECK_BOUNDS_LOW (%eax, STR(%esp))
52 /* At the moment %edx contains C. What we need for the
53 algorithm is C in all bytes of the dword. Avoid
54 operations on 16 bit words because these require an
55 prefix byte (and one more cycle). */
56 movb %dl, %dh /* Now it is 0|0|c|c */
58 shll $16, %edx /* Now c|c|0|0 */
59 movw %cx, %dx /* And finally c|c|c|c */
61 /* Better performance can be achieved if the word (32
62 bit) memory access is aligned on a four-byte-boundary.
63 So process first bytes one by one until boundary is
64 reached. Don't use a loop for better performance. */
66 testb $3, %al /* correctly aligned ? */
67 je L(1) /* yes => begin loop */
68 cmpb %dl, (%eax) /* compare byte */
69 je L(9) /* target found => return */
70 incl %eax /* increment source pointer */
72 testb $3, %al /* correctly aligned ? */
73 je L(1) /* yes => begin loop */
74 cmpb %dl, (%eax) /* compare byte */
75 je L(9) /* target found => return */
76 incl %eax /* increment source pointer */
78 testb $3, %al /* correctly aligned ? */
79 je L(1) /* yes => begin loop */
80 cmpb %dl, (%eax) /* compare byte */
81 je L(9) /* target found => return */
82 incl %eax /* increment source pointer */
84 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
85 change any of the hole bits of LONGWORD.
87 1) Is this safe? Will it catch all the zero bytes?
88 Suppose there is a byte with all zeros. Any carry bits
89 propagating from its left will fall into the hole at its
90 least significant bit and stop. Since there will be no
91 carry from its most significant bit, the LSB of the
92 byte to the left will be unchanged, and the zero will be
95 2) Is this worthwhile? Will it ignore everything except
96 zero bytes? Suppose every byte of LONGWORD has a bit set
97 somewhere. There will be a carry into bit 8. If bit 8
98 is set, this will carry into bit 16. If bit 8 is clear,
99 one of bits 9-15 must be set, so there will be a carry
100 into bit 16. Similarly, there will be a carry into bit
101 24. If one of bits 24-31 is set, there will be a carry
102 into bit 32 (=carry flag), so all of the hole bits will
105 3) But wait! Aren't we looking for C, not zero?
106 Good point. So what we do is XOR LONGWORD with a longword,
107 each of whose bytes is C. This turns each byte that is C
111 /* Each round the main loop processes 16 bytes. */
114 L(1): movl (%eax), %ecx /* get word (= 4 bytes) in question */
115 movl $0xfefefeff, %edi /* magic value */
116 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
118 addl %ecx, %edi /* add the magic value to the word. We get
119 carry bits reported for each byte which
122 /* According to the algorithm we had to reverse the effect of the
123 XOR first and then test the overflow bits. But because the
124 following XOR would destroy the carry flag and it would (in a
125 representation with more than 32 bits) not alter then last
126 overflow, we can now test this condition. If no carry is signaled
127 no overflow must have occurred in the last byte => it was 0. */
130 /* We are only interested in carry bits that change due to the
131 previous add, so remove original bits */
132 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
134 /* Now test for the other three overflow bits. */
135 orl $0xfefefeff, %edi /* set all non-carry bits */
136 incl %edi /* add 1: if one carry bit was *not* set
137 the addition will not result in 0. */
139 /* If at least one byte of the word is C we don't get 0 in %edi. */
140 jnz L(8) /* found it => return pointer */
142 /* This process is unfolded four times for better performance.
143 we don't increment the source pointer each time. Instead we
144 use offsets and increment by 16 in each run of the loop. But
145 before probing for the matching byte we need some extra code
146 (following LL(13) below). Even the len can be compared with
147 constants instead of decrementing each time. */
149 movl 4(%eax), %ecx /* get word (= 4 bytes) in question */
150 movl $0xfefefeff, %edi /* magic value */
151 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
153 addl %ecx, %edi /* add the magic value to the word. We get
154 carry bits reported for each byte which
156 jnc L(7) /* highest byte is C => return pointer */
157 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
158 orl $0xfefefeff, %edi /* set all non-carry bits */
159 incl %edi /* add 1: if one carry bit was *not* set
160 the addition will not result in 0. */
161 jnz L(7) /* found it => return pointer */
163 movl 8(%eax), %ecx /* get word (= 4 bytes) in question */
164 movl $0xfefefeff, %edi /* magic value */
165 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
167 addl %ecx, %edi /* add the magic value to the word. We get
168 carry bits reported for each byte which
170 jnc L(6) /* highest byte is C => return pointer */
171 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
172 orl $0xfefefeff, %edi /* set all non-carry bits */
173 incl %edi /* add 1: if one carry bit was *not* set
174 the addition will not result in 0. */
175 jnz L(6) /* found it => return pointer */
177 movl 12(%eax), %ecx /* get word (= 4 bytes) in question */
178 movl $0xfefefeff, %edi /* magic value */
179 xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
181 addl %ecx, %edi /* add the magic value to the word. We get
182 carry bits reported for each byte which
184 jnc L(5) /* highest byte is C => return pointer */
185 xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
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 jnz L(5) /* found it => return pointer */
191 /* Adjust both counters for a full round, i.e. 16 bytes. */
194 /* add missing source pointer increments */
199 /* Test for the matching byte in the word. %ecx contains a NUL
200 char in the byte which originally was the byte we are looking
202 L(8): testb %cl, %cl /* test first byte in dword */
203 jz L(9) /* if zero => return pointer */
204 incl %eax /* increment source pointer */
206 testb %ch, %ch /* test second byte in dword */
207 jz L(9) /* if zero => return pointer */
208 incl %eax /* increment source pointer */
210 testl $0xff0000, %ecx /* test third byte in dword */
211 jz L(9) /* if zero => return pointer */
212 incl %eax /* increment source pointer */
214 /* No further test needed we we know it is one of the four bytes. */
217 CHECK_BOUNDS_HIGH (%eax, STR(%esp), jb)
218 RETURN_BOUNDED_POINTER (STR(%esp))
219 popl %edi /* pop saved register */
223 END (BP_SYM (__rawmemchr))
225 weak_alias (BP_SYM (__rawmemchr), BP_SYM (rawmemchr))