1 /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in the
4 Copyright (C) 1999-2023 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library. If not, see
19 <https://www.gnu.org/licenses/>. */
22 #include "asm-syntax.h"
26 /* Save the callee-saved registers we use. */
28 movel R(d2),MEM_PREDEC(sp)
29 cfi_adjust_cfa_offset (4)
30 movel R(d3),MEM_PREDEC(sp)
31 cfi_adjust_cfa_offset (4)
32 movel R(d4),MEM_PREDEC(sp)
33 cfi_adjust_cfa_offset (4)
34 cfi_rel_offset (R(d2), 8)
35 cfi_rel_offset (R(d3), 4)
36 cfi_rel_offset (R(d4), 0)
38 moveml R(d2)-R(d4),MEM_PREDEC(sp)
39 cfi_adjust_cfa_offset (3*4)
40 cfi_rel_offset (R(d2), 0)
41 cfi_rel_offset (R(d3), 4)
42 cfi_rel_offset (R(d4), 8)
45 /* Get string pointer, character and length. */
46 movel MEM_DISP(sp,16),R(a0)
47 moveb MEM_DISP(sp,23),R(d0)
48 movel MEM_DISP(sp,24),R(d4)
50 /* Check if at least four bytes left to search. */
61 /* Distribute the character to all bytes of a longword. */
69 /* First search for the character one byte at a time until the
70 pointer is aligned to a longword boundary. */
109 /* Load the magic bits. Unlike the generic implementation we can
110 use the carry bit as the fourth hole. */
111 movel #0xfefefeff,R(d3)
113 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
114 change any of the hole bits of LONGWORD.
116 1) Is this safe? Will it catch all the zero bytes?
117 Suppose there is a byte with all zeros. Any carry bits
118 propagating from its left will fall into the hole at its
119 least significant bit and stop. Since there will be no
120 carry from its most significant bit, the LSB of the
121 byte to the left will be unchanged, and the zero will be
124 2) Is this worthwhile? Will it ignore everything except
125 zero bytes? Suppose every byte of LONGWORD has a bit set
126 somewhere. There will be a carry into bit 8. If bit 8
127 is set, this will carry into bit 16. If bit 8 is clear,
128 one of bits 9-15 must be set, so there will be a carry
129 into bit 16. Similarly, there will be a carry into bit
130 24. If one of bits 24-31 is set, there will be a carry
131 into bit 32 (=carry flag), so all of the hole bits will
134 3) But wait! Aren't we looking for C, not zero?
135 Good point. So what we do is XOR LONGWORD with a longword,
136 each of whose bytes is C. This turns each byte that is C
139 /* Still at least 4 bytes to search? */
144 /* Get the longword in question. */
145 movel MEM_POSTINC(a0),R(d1)
146 /* XOR with the byte we search for. */
149 /* Add the magic value. We get carry bits reported for each byte
154 /* Check the fourth carry bit before it is clobbered by the next
155 XOR. If it is not set we have a hit. */
158 /* We are only interested in carry bits that change due to the
159 previous add, so remove original bits. */
162 /* Now test for the other three overflow bits.
163 Set all non-carry bits. */
165 /* Add 1 to get zero if all carry bits were set. */
168 /* If we don't get zero then at least one byte of the word equals
172 /* Still at least 4 bytes to search? */
176 /* Get the longword in question. */
177 movel MEM_POSTINC(a0),R(d1)
178 /* XOR with the byte we search for. */
181 /* Add the magic value. We get carry bits reported for each byte
186 /* Check the fourth carry bit before it is clobbered by the next
187 XOR. If it is not set we have a hit. */
190 /* We are only interested in carry bits that change due to the
191 previous add, so remove original bits */
194 /* Now test for the other three overflow bits.
195 Set all non-carry bits. */
197 /* Add 1 to get zero if all carry bits were set. */
200 /* If we don't get zero then at least one byte of the word equals
204 /* Still at least 4 bytes to search? */
209 /* Search one byte at a time in the remaining less than 4 bytes. */
244 movel MEM_POSTINC(sp),R(d4)
246 cfi_adjust_cfa_offset (-4)
248 movel MEM_POSTINC(sp),R(d3)
249 cfi_adjust_cfa_offset (-4)
251 movel MEM_POSTINC(sp),R(d2)
252 cfi_adjust_cfa_offset (-4)
255 moveml MEM_POSTINC(sp),R(d2)-R(d4)
257 cfi_adjust_cfa_offset (-3*4)
266 /* We have a hit. Check to see which byte it was. First
267 compensate for the autoincrement in the loop. */
282 /* Otherwise the fourth byte must equal C. */
286 movel MEM_POSTINC(sp),R(d4)
287 cfi_adjust_cfa_offset (-4)
289 movel MEM_POSTINC(sp),R(d3)
290 cfi_adjust_cfa_offset (-4)
292 movel MEM_POSTINC(sp),R(d2)
293 cfi_adjust_cfa_offset (-4)
296 moveml MEM_POSTINC(sp),R(d2)-R(d4)
297 cfi_adjust_cfa_offset (-3*4)
305 weak_alias (__memchr, memchr)
306 libc_hidden_builtin_def (memchr)