1a7c91847Schristos /* Copyright (C) 1991, 1993, 1996, 1997, 1999, 2000, 2003, 2004 Free
2a7c91847Schristos Software Foundation, Inc.
3a7c91847Schristos
4a7c91847Schristos Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
5a7c91847Schristos with help from Dan Sahlin (dan@sics.se) and
6a7c91847Schristos commentary by Jim Blandy (jimb@ai.mit.edu);
7a7c91847Schristos adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
8a7c91847Schristos and implemented by Roland McGrath (roland@ai.mit.edu).
9a7c91847Schristos
10a7c91847Schristos NOTE: The canonical source of this file is maintained with the GNU C Library.
11a7c91847Schristos Bugs can be reported to bug-glibc@prep.ai.mit.edu.
12a7c91847Schristos
13a7c91847Schristos This program is free software; you can redistribute it and/or modify it
14a7c91847Schristos under the terms of the GNU General Public License as published by the
15a7c91847Schristos Free Software Foundation; either version 2, or (at your option) any
16a7c91847Schristos later version.
17a7c91847Schristos
18a7c91847Schristos This program is distributed in the hope that it will be useful,
19a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
20a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21a7c91847Schristos GNU General Public License for more details.
22a7c91847Schristos
23a7c91847Schristos You should have received a copy of the GNU General Public License
24a7c91847Schristos along with this program; if not, write to the Free Software Foundation,
25a7c91847Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26*5a6c14c8Schristos #include <sys/cdefs.h>
27*5a6c14c8Schristos __RCSID("$NetBSD: memchr.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
28*5a6c14c8Schristos
29a7c91847Schristos
30a7c91847Schristos #ifdef HAVE_CONFIG_H
31a7c91847Schristos # include <config.h>
32a7c91847Schristos #endif
33a7c91847Schristos
34a7c91847Schristos #include <string.h>
35a7c91847Schristos
36a7c91847Schristos #include <stddef.h>
37a7c91847Schristos
38a7c91847Schristos #if defined _LIBC
39a7c91847Schristos # include <memcopy.h>
40a7c91847Schristos #else
41a7c91847Schristos # define reg_char char
42a7c91847Schristos #endif
43a7c91847Schristos
44a7c91847Schristos #include <limits.h>
45a7c91847Schristos
46a7c91847Schristos #if HAVE_BP_SYM_H || defined _LIBC
47a7c91847Schristos # include <bp-sym.h>
48a7c91847Schristos #else
49a7c91847Schristos # define BP_SYM(sym) sym
50a7c91847Schristos #endif
51a7c91847Schristos
52a7c91847Schristos #undef memchr
53a7c91847Schristos #undef __memchr
54a7c91847Schristos
55a7c91847Schristos /* Search no more than N bytes of S for C. */
56a7c91847Schristos void *
__memchr(void const * s,int c_in,size_t n)57a7c91847Schristos __memchr (void const *s, int c_in, size_t n)
58a7c91847Schristos {
59a7c91847Schristos const unsigned char *char_ptr;
60a7c91847Schristos const unsigned long int *longword_ptr;
61a7c91847Schristos unsigned long int longword, magic_bits, charmask;
62a7c91847Schristos unsigned reg_char c;
63a7c91847Schristos int i;
64a7c91847Schristos
65a7c91847Schristos c = (unsigned char) c_in;
66a7c91847Schristos
67a7c91847Schristos /* Handle the first few characters by reading one character at a time.
68a7c91847Schristos Do this until CHAR_PTR is aligned on a longword boundary. */
69a7c91847Schristos for (char_ptr = (const unsigned char *) s;
70a7c91847Schristos n > 0 && (size_t) char_ptr % sizeof longword != 0;
71a7c91847Schristos --n, ++char_ptr)
72a7c91847Schristos if (*char_ptr == c)
73a7c91847Schristos return (void *) char_ptr;
74a7c91847Schristos
75a7c91847Schristos /* All these elucidatory comments refer to 4-byte longwords,
76a7c91847Schristos but the theory applies equally well to any size longwords. */
77a7c91847Schristos
78a7c91847Schristos longword_ptr = (const unsigned long int *) char_ptr;
79a7c91847Schristos
80a7c91847Schristos /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
81a7c91847Schristos the "holes." Note that there is a hole just to the left of
82a7c91847Schristos each byte, with an extra at the end:
83a7c91847Schristos
84a7c91847Schristos bits: 01111110 11111110 11111110 11111111
85a7c91847Schristos bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
86a7c91847Schristos
87a7c91847Schristos The 1-bits make sure that carries propagate to the next 0-bit.
88a7c91847Schristos The 0-bits provide holes for carries to fall into. */
89a7c91847Schristos
90a7c91847Schristos /* Set MAGIC_BITS to be this pattern of 1 and 0 bits.
91a7c91847Schristos Set CHARMASK to be a longword, each of whose bytes is C. */
92a7c91847Schristos
93a7c91847Schristos magic_bits = 0xfefefefe;
94a7c91847Schristos charmask = c | (c << 8);
95a7c91847Schristos charmask |= charmask << 16;
96a7c91847Schristos #if 0xffffffffU < ULONG_MAX
97a7c91847Schristos magic_bits |= magic_bits << 32;
98a7c91847Schristos charmask |= charmask << 32;
99a7c91847Schristos if (8 < sizeof longword)
100a7c91847Schristos for (i = 64; i < sizeof longword * 8; i *= 2)
101a7c91847Schristos {
102a7c91847Schristos magic_bits |= magic_bits << i;
103a7c91847Schristos charmask |= charmask << i;
104a7c91847Schristos }
105a7c91847Schristos #endif
106a7c91847Schristos magic_bits = (ULONG_MAX >> 1) & (magic_bits | 1);
107a7c91847Schristos
108a7c91847Schristos /* Instead of the traditional loop which tests each character,
109a7c91847Schristos we will test a longword at a time. The tricky part is testing
110a7c91847Schristos if *any of the four* bytes in the longword in question are zero. */
111a7c91847Schristos while (n >= sizeof longword)
112a7c91847Schristos {
113a7c91847Schristos /* We tentatively exit the loop if adding MAGIC_BITS to
114a7c91847Schristos LONGWORD fails to change any of the hole bits of LONGWORD.
115a7c91847Schristos
116a7c91847Schristos 1) Is this safe? Will it catch all the zero bytes?
117a7c91847Schristos Suppose there is a byte with all zeros. Any carry bits
118a7c91847Schristos propagating from its left will fall into the hole at its
119a7c91847Schristos least significant bit and stop. Since there will be no
120a7c91847Schristos carry from its most significant bit, the LSB of the
121a7c91847Schristos byte to the left will be unchanged, and the zero will be
122a7c91847Schristos detected.
123a7c91847Schristos
124a7c91847Schristos 2) Is this worthwhile? Will it ignore everything except
125a7c91847Schristos zero bytes? Suppose every byte of LONGWORD has a bit set
126a7c91847Schristos somewhere. There will be a carry into bit 8. If bit 8
127a7c91847Schristos is set, this will carry into bit 16. If bit 8 is clear,
128a7c91847Schristos one of bits 9-15 must be set, so there will be a carry
129a7c91847Schristos into bit 16. Similarly, there will be a carry into bit
130a7c91847Schristos 24. If one of bits 24-30 is set, there will be a carry
131a7c91847Schristos into bit 31, so all of the hole bits will be changed.
132a7c91847Schristos
133a7c91847Schristos The one misfire occurs when bits 24-30 are clear and bit
134a7c91847Schristos 31 is set; in this case, the hole at bit 31 is not
135a7c91847Schristos changed. If we had access to the processor carry flag,
136a7c91847Schristos we could close this loophole by putting the fourth hole
137a7c91847Schristos at bit 32!
138a7c91847Schristos
139a7c91847Schristos So it ignores everything except 128's, when they're aligned
140a7c91847Schristos properly.
141a7c91847Schristos
142a7c91847Schristos 3) But wait! Aren't we looking for C, not zero?
143a7c91847Schristos Good point. So what we do is XOR LONGWORD with a longword,
144a7c91847Schristos each of whose bytes is C. This turns each byte that is C
145a7c91847Schristos into a zero. */
146a7c91847Schristos
147a7c91847Schristos longword = *longword_ptr++ ^ charmask;
148a7c91847Schristos
149a7c91847Schristos /* Add MAGIC_BITS to LONGWORD. */
150a7c91847Schristos if ((((longword + magic_bits)
151a7c91847Schristos
152a7c91847Schristos /* Set those bits that were unchanged by the addition. */
153a7c91847Schristos ^ ~longword)
154a7c91847Schristos
155a7c91847Schristos /* Look at only the hole bits. If any of the hole bits
156a7c91847Schristos are unchanged, most likely one of the bytes was a
157a7c91847Schristos zero. */
158a7c91847Schristos & ~magic_bits) != 0)
159a7c91847Schristos {
160a7c91847Schristos /* Which of the bytes was C? If none of them were, it was
161a7c91847Schristos a misfire; continue the search. */
162a7c91847Schristos
163a7c91847Schristos const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
164a7c91847Schristos
165a7c91847Schristos if (cp[0] == c)
166a7c91847Schristos return (void *) cp;
167a7c91847Schristos if (cp[1] == c)
168a7c91847Schristos return (void *) &cp[1];
169a7c91847Schristos if (cp[2] == c)
170a7c91847Schristos return (void *) &cp[2];
171a7c91847Schristos if (cp[3] == c)
172a7c91847Schristos return (void *) &cp[3];
173a7c91847Schristos if (4 < sizeof longword && cp[4] == c)
174a7c91847Schristos return (void *) &cp[4];
175a7c91847Schristos if (5 < sizeof longword && cp[5] == c)
176a7c91847Schristos return (void *) &cp[5];
177a7c91847Schristos if (6 < sizeof longword && cp[6] == c)
178a7c91847Schristos return (void *) &cp[6];
179a7c91847Schristos if (7 < sizeof longword && cp[7] == c)
180a7c91847Schristos return (void *) &cp[7];
181a7c91847Schristos if (8 < sizeof longword)
182a7c91847Schristos for (i = 8; i < sizeof longword; i++)
183a7c91847Schristos if (cp[i] == c)
184a7c91847Schristos return (void *) &cp[i];
185a7c91847Schristos }
186a7c91847Schristos
187a7c91847Schristos n -= sizeof longword;
188a7c91847Schristos }
189a7c91847Schristos
190a7c91847Schristos char_ptr = (const unsigned char *) longword_ptr;
191a7c91847Schristos
192a7c91847Schristos while (n-- > 0)
193a7c91847Schristos {
194a7c91847Schristos if (*char_ptr == c)
195a7c91847Schristos return (void *) char_ptr;
196a7c91847Schristos else
197a7c91847Schristos ++char_ptr;
198a7c91847Schristos }
199a7c91847Schristos
200a7c91847Schristos return 0;
201a7c91847Schristos }
202a7c91847Schristos #ifdef weak_alias
203a7c91847Schristos weak_alias (__memchr, BP_SYM (memchr))
204a7c91847Schristos #endif
205