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