1 /* strchrnul (str, ch) -- Return pointer to first occurrence of CH in STR
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. */
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 cfi_rel_offset (R(d2), 4)
33 cfi_rel_offset (R(d3), 0)
35 /* Get string pointer and character. */
36 movel MEM_DISP(sp,12),R(a0)
37 moveb MEM_DISP(sp,19),R(d0)
39 /* Distribute the character to all bytes of a longword. */
47 /* First search for the character one byte at a time until the
48 pointer is aligned to a longword boundary. */
90 /* Load the magic bits. Unlike the generic implementation we can
91 use the carry bit as the fourth hole. */
92 movel #0xfefefeff,R(d3)
94 /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
95 change any of the hole bits of LONGWORD.
97 1) Is this safe? Will it catch all the zero bytes?
98 Suppose there is a byte with all zeros. Any carry bits
99 propagating from its left will fall into the hole at its
100 least significant bit and stop. Since there will be no
101 carry from its most significant bit, the LSB of the
102 byte to the left will be unchanged, and the zero will be
105 2) Is this worthwhile? Will it ignore everything except
106 zero bytes? Suppose every byte of LONGWORD has a bit set
107 somewhere. There will be a carry into bit 8. If bit 8
108 is set, this will carry into bit 16. If bit 8 is clear,
109 one of bits 9-15 must be set, so there will be a carry
110 into bit 16. Similarly, there will be a carry into bit
111 24. If one of bits 24-31 is set, there will be a carry
112 into bit 32 (=carry flag), so all of the hole bits will
115 3) But wait! Aren't we looking for C, not zero?
116 Good point. So what we do is XOR LONGWORD with a longword,
117 each of whose bytes is C. This turns each byte that is C
121 /* Get the longword in question. */
122 movel MEM_POSTINC(a0),R(d1)
123 /* XOR with the byte we search for. */
126 /* Add the magic value. We get carry bits reported for each byte
131 /* Check the fourth carry bit before it is clobbered by the next
132 XOR. If it is not set we have a hit. */
135 /* We are only interested in carry bits that change due to the
136 previous add, so remove original bits. */
139 /* Now test for the other three overflow bits.
140 Set all non-carry bits. */
142 /* Add 1 to get zero if all carry bits were set. */
145 /* If we don't get zero then at least one byte of the word equals
149 /* Next look for a NUL byte.
150 Restore original longword without reload. */
152 /* Add the magic value. We get carry bits reported for each byte
157 /* Check the fourth carry bit before it is clobbered by the next
158 XOR. If it is not set we have a hit. */
161 /* We are only interested in carry bits that change due to the
162 previous add, so remove original bits. */
165 /* Now test for the other three overflow bits.
166 Set all non-carry bits. */
168 /* Add 1 to get zero if all carry bits were set. */
171 /* If we don't get zero then at least one byte of the word was
172 NUL. Otherwise continue with the next longword. */
175 /* Get the longword in question. */
176 movel MEM_POSTINC(a0),R(d1)
177 /* XOR with the byte we search for. */
180 /* Add the magic value. We get carry bits reported for each byte
185 /* Check the fourth carry bit before it is clobbered by the next
186 XOR. If it is not set we have a hit. */
189 /* We are only interested in carry bits that change due to the
190 previous add, so remove original bits */
193 /* Now test for the other three overflow bits.
194 Set all non-carry bits. */
196 /* Add 1 to get zero if all carry bits were set. */
199 /* If we don't get zero then at least one byte of the word equals
203 /* Next look for a NUL byte.
204 Restore original longword without reload. */
206 /* Add the magic value. We get carry bits reported for each byte
211 /* Check the fourth carry bit before it is clobbered by the next
212 XOR. If it is not set we have a hit. */
215 /* We are only interested in carry bits that change due to the
216 previous add, so remove original bits */
219 /* Now test for the other three overflow bits.
220 Set all non-carry bits. */
222 /* Add 1 to get zero if all carry bits were set. */
225 /* If we don't get zero then at least one byte of the word was
226 NUL. Otherwise continue with the next longword. */
230 /* We have a hit. Check to see which byte it was. First
231 compensate for the autoincrement in the loop. */
255 /* Otherwise the fourth byte must equal C or be NUL. */
258 movel MEM_POSTINC(sp),R(d3)
259 cfi_adjust_cfa_offset (-4)
261 movel MEM_POSTINC(sp),R(d2)
262 cfi_adjust_cfa_offset (-4)
267 weak_alias (__strchrnul, strchrnul)