1 /* rawmemchr (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. */
79 /* Load the magic bits. Unlike the generic implementation we can
80 use the carry bit as the fourth hole. */
81 movel #0xfefefeff,R(d3)
83 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
84 change any of the hole bits of LONGWORD.
86 1) Is this safe? Will it catch all the zero bytes?
87 Suppose there is a byte with all zeros. Any carry bits
88 propagating from its left will fall into the hole at its
89 least significant bit and stop. Since there will be no
90 carry from its most significant bit, the LSB of the
91 byte to the left will be unchanged, and the zero will be
94 2) Is this worthwhile? Will it ignore everything except
95 zero bytes? Suppose every byte of LONGWORD has a bit set
96 somewhere. There will be a carry into bit 8. If bit 8
97 is set, this will carry into bit 16. If bit 8 is clear,
98 one of bits 9-15 must be set, so there will be a carry
99 into bit 16. Similarly, there will be a carry into bit
100 24. If one of bits 24-31 is set, there will be a carry
101 into bit 32 (=carry flag), so all of the hole bits will
104 3) But wait! Aren't we looking for C, not zero?
105 Good point. So what we do is XOR LONGWORD with a longword,
106 each of whose bytes is C. This turns each byte that is C
110 /* Get the longword in question. */
111 movel MEM_POSTINC(a0),R(d1)
112 /* XOR with the byte we search for. */
115 /* Add the magic value. We get carry bits reported for each byte
120 /* Check the fourth carry bit before it is clobbered by the next
121 XOR. If it is not set we have a hit. */
124 /* We are only interested in carry bits that change due to the
125 previous add, so remove original bits. */
128 /* Now test for the other three overflow bits.
129 Set all non-carry bits. */
131 /* Add 1 to get zero if all carry bits were set. */
134 /* If we don't get zero then at least one byte of the word equals
138 /* Get the longword in question. */
139 movel MEM_POSTINC(a0),R(d1)
140 /* XOR with the byte we search for. */
143 /* Add the magic value. We get carry bits reported for each byte
148 /* Check the fourth carry bit before it is clobbered by the next
149 XOR. If it is not set we have a hit. */
152 /* We are only interested in carry bits that change due to the
153 previous add, so remove original bits */
156 /* Now test for the other three overflow bits.
157 Set all non-carry bits. */
159 /* Add 1 to get zero if all carry bits were set. */
162 /* If we don't get zero then at least one byte of the word equals
167 /* We have a hit. Check to see which byte it was. First
168 compensate for the autoincrement in the loop. */
183 /* Otherwise the fourth byte must equal C. */
186 movel MEM_POSTINC(sp),R(d3)
187 cfi_adjust_cfa_offset (-4)
189 movel MEM_POSTINC(sp),R(d2)
190 cfi_adjust_cfa_offset (-4)
195 libc_hidden_def (__rawmemchr)
196 weak_alias (__rawmemchr, rawmemchr)