1 /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in the
4 Copyright (C) 1999-2021 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6 Contributed by Andreas Schwab <schwab@gnu.org>.
8 The GNU C Library is free software; you can redistribute it and/or
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.
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
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library. If not, see
20 <https://www.gnu.org/licenses/>. */
23 #include "asm-syntax.h"
27 /* Save the callee-saved registers we use. */
29 movel R(d2),MEM_PREDEC(sp)
30 cfi_adjust_cfa_offset (4)
31 movel R(d3),MEM_PREDEC(sp)
32 cfi_adjust_cfa_offset (4)
33 movel R(d4),MEM_PREDEC(sp)
34 cfi_adjust_cfa_offset (4)
35 cfi_rel_offset (R(d2), 8)
36 cfi_rel_offset (R(d3), 4)
37 cfi_rel_offset (R(d4), 0)
39 moveml R(d2)-R(d4),MEM_PREDEC(sp)
40 cfi_adjust_cfa_offset (3*4)
41 cfi_rel_offset (R(d2), 0)
42 cfi_rel_offset (R(d3), 4)
43 cfi_rel_offset (R(d4), 8)
46 /* Get string pointer, character and length. */
47 movel MEM_DISP(sp,16),R(a0)
48 moveb MEM_DISP(sp,23),R(d0)
49 movel MEM_DISP(sp,24),R(d4)
51 /* Check if at least four bytes left to search. */
62 /* Distribute the character to all bytes of a longword. */
70 /* First search for the character one byte at a time until the
71 pointer is aligned to a longword boundary. */
110 /* Load the magic bits. Unlike the generic implementation we can
111 use the carry bit as the fourth hole. */
112 movel #0xfefefeff,R(d3)
114 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
115 change any of the hole bits of LONGWORD.
117 1) Is this safe? Will it catch all the zero bytes?
118 Suppose there is a byte with all zeros. Any carry bits
119 propagating from its left will fall into the hole at its
120 least significant bit and stop. Since there will be no
121 carry from its most significant bit, the LSB of the
122 byte to the left will be unchanged, and the zero will be
125 2) Is this worthwhile? Will it ignore everything except
126 zero bytes? Suppose every byte of LONGWORD has a bit set
127 somewhere. There will be a carry into bit 8. If bit 8
128 is set, this will carry into bit 16. If bit 8 is clear,
129 one of bits 9-15 must be set, so there will be a carry
130 into bit 16. Similarly, there will be a carry into bit
131 24. If one of bits 24-31 is set, there will be a carry
132 into bit 32 (=carry flag), so all of the hole bits will
135 3) But wait! Aren't we looking for C, not zero?
136 Good point. So what we do is XOR LONGWORD with a longword,
137 each of whose bytes is C. This turns each byte that is C
140 /* Still at least 4 bytes to search? */
145 /* Get the longword in question. */
146 movel MEM_POSTINC(a0),R(d1)
147 /* XOR with the byte we search for. */
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. */
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 equals
173 /* Still at least 4 bytes to search? */
177 /* Get the longword in question. */
178 movel MEM_POSTINC(a0),R(d1)
179 /* XOR with the byte we search for. */
182 /* Add the magic value. We get carry bits reported for each byte
187 /* Check the fourth carry bit before it is clobbered by the next
188 XOR. If it is not set we have a hit. */
191 /* We are only interested in carry bits that change due to the
192 previous add, so remove original bits */
195 /* Now test for the other three overflow bits.
196 Set all non-carry bits. */
198 /* Add 1 to get zero if all carry bits were set. */
201 /* If we don't get zero then at least one byte of the word equals
205 /* Still at least 4 bytes to search? */
210 /* Search one byte at a time in the remaining less than 4 bytes. */
245 movel MEM_POSTINC(sp),R(d4)
247 cfi_adjust_cfa_offset (-4)
249 movel MEM_POSTINC(sp),R(d3)
250 cfi_adjust_cfa_offset (-4)
252 movel MEM_POSTINC(sp),R(d2)
253 cfi_adjust_cfa_offset (-4)
256 moveml MEM_POSTINC(sp),R(d2)-R(d4)
258 cfi_adjust_cfa_offset (-3*4)
267 /* We have a hit. Check to see which byte it was. First
268 compensate for the autoincrement in the loop. */
283 /* Otherwise the fourth byte must equal C. */
287 movel MEM_POSTINC(sp),R(d4)
288 cfi_adjust_cfa_offset (-4)
290 movel MEM_POSTINC(sp),R(d3)
291 cfi_adjust_cfa_offset (-4)
293 movel MEM_POSTINC(sp),R(d2)
294 cfi_adjust_cfa_offset (-4)
297 moveml MEM_POSTINC(sp),R(d2)-R(d4)
298 cfi_adjust_cfa_offset (-3*4)
306 weak_alias (__memchr, memchr)
307 libc_hidden_builtin_def (memchr)