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