1 /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
3 Copyright (C) 1999-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <https://www.gnu.org/licenses/>. */
21 #include "asm-syntax.h"
25 /* Save the callee-saved registers we use. */
26 movel R(d2),MEM_PREDEC(sp)
27 cfi_adjust_cfa_offset (4)
28 movel R(d3),MEM_PREDEC(sp)
29 cfi_adjust_cfa_offset (4)
30 cfi_rel_offset (R(d2),4)
31 cfi_rel_offset (R(d3),0)
33 /* Get string pointer and character. */
34 movel MEM_DISP(sp,12),R(a0)
35 moveb MEM_DISP(sp,19),R(d0)
37 /* Distribute the character to all bytes of a longword. */
45 /* First search for the character one byte at a time until the
46 pointer is aligned to a longword boundary. */
88 /* Load the magic bits. Unlike the generic implementation we can
89 use the carry bit as the fourth hole. */
90 movel #0xfefefeff,R(d3)
92 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
93 change any of the hole bits of LONGWORD.
95 1) Is this safe? Will it catch all the zero bytes?
96 Suppose there is a byte with all zeros. Any carry bits
97 propagating from its left will fall into the hole at its
98 least significant bit and stop. Since there will be no
99 carry from its most significant bit, the LSB of the
100 byte to the left will be unchanged, and the zero will be
103 2) Is this worthwhile? Will it ignore everything except
104 zero bytes? Suppose every byte of LONGWORD has a bit set
105 somewhere. There will be a carry into bit 8. If bit 8
106 is set, this will carry into bit 16. If bit 8 is clear,
107 one of bits 9-15 must be set, so there will be a carry
108 into bit 16. Similarly, there will be a carry into bit
109 24. If one of bits 24-31 is set, there will be a carry
110 into bit 32 (=carry flag), so all of the hole bits will
113 3) But wait! Aren't we looking for C, not zero?
114 Good point. So what we do is XOR LONGWORD with a longword,
115 each of whose bytes is C. This turns each byte that is C
119 /* Get the longword in question. */
120 movel MEM_POSTINC(a0),R(d1)
121 /* XOR with the byte we search for. */
124 /* Add the magic value. We get carry bits reported for each byte
129 /* Check the fourth carry bit before it is clobbered by the next
130 XOR. If it is not set we have a hit. */
133 /* We are only interested in carry bits that change due to the
134 previous add, so remove original bits. */
137 /* Now test for the other three overflow bits.
138 Set all non-carry bits. */
140 /* Add 1 to get zero if all carry bits were set. */
143 /* If we don't get zero then at least one byte of the word equals
147 /* Next look for a NUL byte.
148 Restore original longword without reload. */
150 /* Add the magic value. We get carry bits reported for each byte
155 /* Check the fourth carry bit before it is clobbered by the next
156 XOR. If it is not set we have a hit, and return NULL. */
159 /* We are only interested in carry bits that change due to the
160 previous add, so remove original bits. */
163 /* Now test for the other three overflow bits.
164 Set all non-carry bits. */
166 /* Add 1 to get zero if all carry bits were set. */
169 /* If we don't get zero then at least one byte of the word was NUL
170 and we return NULL. Otherwise continue with the next longword. */
173 /* Get the longword in question. */
174 movel MEM_POSTINC(a0),R(d1)
175 /* XOR with the byte we search for. */
178 /* Add the magic value. We get carry bits reported for each byte
183 /* Check the fourth carry bit before it is clobbered by the next
184 XOR. If it is not set we have a hit. */
187 /* We are only interested in carry bits that change due to the
188 previous add, so remove original bits */
191 /* Now test for the other three overflow bits.
192 Set all non-carry bits. */
194 /* Add 1 to get zero if all carry bits were set. */
197 /* If we don't get zero then at least one byte of the word equals
201 /* Next look for a NUL byte.
202 Restore original longword without reload. */
204 /* Add the magic value. We get carry bits reported for each byte
209 /* Check the fourth carry bit before it is clobbered by the next
210 XOR. If it is not set we have a hit, and return NULL. */
213 /* We are only interested in carry bits that change due to the
214 previous add, so remove original bits */
217 /* Now test for the other three overflow bits.
218 Set all non-carry bits. */
220 /* Add 1 to get zero if all carry bits were set. */
223 /* If we don't get zero then at least one byte of the word was NUL
224 and we return NULL. Otherwise continue with the next longword. */
231 movel MEM_POSTINC(sp),R(d3)
233 cfi_adjust_cfa_offset (-4)
235 movel MEM_POSTINC(sp),R(d2)
236 cfi_adjust_cfa_offset (-4)
242 /* We have a hit. Check to see which byte it was. First
243 compensate for the autoincrement in the loop. */
267 /* Otherwise the fourth byte must equal C. */
270 movel MEM_POSTINC(sp),R(d3)
271 cfi_adjust_cfa_offset (-4)
273 movel MEM_POSTINC(sp),R(d2)
274 cfi_adjust_cfa_offset (-4)
279 weak_alias (strchr, index)
280 libc_hidden_builtin_def (strchr)