1 /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in the
4 Copyright (C) 1999, 2000, 2003 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, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include "asm-syntax.h"
28 /* Save the callee-saved registers we use. */
29 moveml R(d2)-R(d4),MEM_PREDEC(sp)
31 /* Get string pointer, character and length. */
32 movel MEM_DISP(sp,16),R(a0)
33 moveb MEM_DISP(sp,23),R(d0)
34 movel MEM_DISP(sp,24),R(d4)
36 /* Check if at least four bytes left to search. */
41 /* Distribute the character to all bytes of a longword. */
49 /* First search for the character one byte at a time until the
50 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
109 /* Still at least 4 bytes to search? */
114 /* Get the longword in question. */
115 movel MEM_POSTINC(a0),R(d1)
116 /* XOR with the byte we search for. */
119 /* Add the magic value. We get carry bits reported for each byte
124 /* Check the fourth carry bit before it is clobbered by the next
125 XOR. If it is not set we have a hit. */
128 /* We are only interested in carry bits that change due to the
129 previous add, so remove original bits. */
132 /* Now test for the other three overflow bits.
133 Set all non-carry bits. */
135 /* Add 1 to get zero if all carry bits were set. */
138 /* If we don't get zero then at least one byte of the word equals
142 /* Still at least 4 bytes to search? */
146 /* Get the longword in question. */
147 movel MEM_POSTINC(a0),R(d1)
148 /* XOR with the byte we search for. */
151 /* Add the magic value. We get carry bits reported for each byte
156 /* Check the fourth carry bit before it is clobbered by the next
157 XOR. If it is not set we have a hit. */
160 /* We are only interested in carry bits that change due to the
161 previous add, so remove original bits */
164 /* Now test for the other three overflow bits.
165 Set all non-carry bits. */
167 /* Add 1 to get zero if all carry bits were set. */
170 /* If we don't get zero then at least one byte of the word equals
174 /* Still at least 4 bytes to search? */
179 /* Search one byte at a time in the remaining less than 4 bytes. */
201 moveml MEM_POSTINC(sp),R(d2)-R(d4)
205 /* We have a hit. Check to see which byte it was. First
206 compensate for the autoincrement in the loop. */
221 /* Otherwise the fourth byte must equal C. */
224 moveml MEM_POSTINC(sp),R(d2)-R(d4)
228 weak_alias (__memchr, memchr)
229 #if !__BOUNDED_POINTERS__
230 weak_alias (__memchr, __ubp_memchr)
232 libc_hidden_builtin_def (memchr)