merge with 1.9.4f
[gnulib.git] / lib / memchr.c
1 /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
2    Based on strlen implemention by Torbjorn Granlund (tege@sics.se),
3    with help from Dan Sahlin (dan@sics.se) and
4    commentary by Jim Blandy (jimb@ai.mit.edu);
5    adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
6    and implemented by Roland McGrath (roland@ai.mit.edu).
7
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
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 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB.  If
20 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
21 Cambridge, MA 02139, USA.  */
22
23 #ifdef HAVE_CONFIG_H
24 #if defined (CONFIG_BROKETS)
25 /* We use <config.h> instead of "config.h" so that a compilation
26    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
27    (which it would do because it found this file in $srcdir).  */
28 #include <config.h>
29 #else
30 #include "config.h"
31 #endif
32 #endif
33
34 /* Search no more than N bytes of S for C.  */
35
36 char *
37 memchr (s, c, n)
38      unsigned char *s;
39      int c;
40      unsigned n;
41 {
42   const unsigned char *char_ptr;
43   const unsigned long int *longword_ptr;
44   unsigned long int longword, magic_bits, charmask;
45
46   c = (unsigned char) c;
47
48   /* Handle the first few characters by reading one character at a time.
49      Do this until CHAR_PTR is aligned on a longword boundary.  */
50   for (char_ptr = s; n > 0 && ((unsigned long int) char_ptr
51                                & (sizeof (longword) - 1)) != 0;
52        --n, ++char_ptr)
53     if (*char_ptr == c)
54       return (char *) char_ptr;
55
56   /* All these elucidatory comments refer to 4-byte longwords,
57      but the theory applies equally well to 8-byte longwords.  */
58
59   longword_ptr = (unsigned long int *) char_ptr;
60
61   /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
62      the "holes."  Note that there is a hole just to the left of
63      each byte, with an extra at the end:
64
65      bits:  01111110 11111110 11111110 11111111
66      bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
67
68      The 1-bits make sure that carries propagate to the next 0-bit.
69      The 0-bits provide holes for carries to fall into.  */
70 #ifdef LONG_64_BITS
71   /* 64-bit version of the magic.  */
72   magic_bits = ((unsigned long int) 0x7efefefe << 32) | 0xfefefeff;
73 #else
74   magic_bits = 0x7efefeff;
75 #endif /* LONG_64_BITS */
76
77   /* Set up a longword, each of whose bytes is C.  */
78   charmask = c | (c << 8);
79   charmask |= charmask << 16;
80 #ifdef LONG_64_BITS
81   charmask |= charmask << 32;
82 #endif /* LONG_64_BITS */
83   if (sizeof (longword) > 8)
84     abort ();
85
86
87   /* Instead of the traditional loop which tests each character,
88      we will test a longword at a time.  The tricky part is testing
89      if *any of the four* bytes in the longword in question are zero.  */
90   while (n >= sizeof (longword))
91     {
92       /* We tentatively exit the loop if adding MAGIC_BITS to
93          LONGWORD fails to change any of the hole bits of LONGWORD.
94
95          1) Is this safe?  Will it catch all the zero bytes?
96          Suppose there is a byte with all zeros.  Any carry bits
97          propagating from its left will fall into the hole at its
98          least significant bit and stop.  Since there will be no
99          carry from its most significant bit, the LSB of the
100          byte to the left will be unchanged, and the zero will be
101          detected.
102
103          2) Is this worthwhile?  Will it ignore everything except
104          zero bytes?  Suppose every byte of LONGWORD has a bit set
105          somewhere.  There will be a carry into bit 8.  If bit 8
106          is set, this will carry into bit 16.  If bit 8 is clear,
107          one of bits 9-15 must be set, so there will be a carry
108          into bit 16.  Similarly, there will be a carry into bit
109          24.  If one of bits 24-30 is set, there will be a carry
110          into bit 31, so all of the hole bits will be changed.
111
112          The one misfire occurs when bits 24-30 are clear and bit
113          31 is set; in this case, the hole at bit 31 is not
114          changed.  If we had access to the processor carry flag,
115          we could close this loophole by putting the fourth hole
116          at bit 32!
117
118          So it ignores everything except 128's, when they're aligned
119          properly.
120
121          3) But wait!  Aren't we looking for C, not zero?
122          Good point.  So what we do is XOR LONGWORD with a longword,
123          each of whose bytes is C.  This turns each byte that is C
124          into a zero.  */
125
126       longword = *longword_ptr++ ^ charmask;
127
128       /* Add MAGIC_BITS to LONGWORD.  */
129       if ((((longword + magic_bits)
130
131       /* Set those bits that were unchanged by the addition.  */
132             ^ ~longword)
133
134       /* Look at only the hole bits.  If any of the hole bits
135               are unchanged, most likely one of the bytes was a
136               zero.  */
137            & ~magic_bits) != 0)
138         {
139           /* Which of the bytes was C?  If none of them were, it was
140              a misfire; continue the search.  */
141
142           const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
143
144           if (cp[0] == c)
145             return (char *) cp;
146           if (cp[1] == c)
147             return (char *) &cp[1];
148           if (cp[2] == c)
149             return (char *) &cp[2];
150           if (cp[3] == c)
151             return (char *) &cp[3];
152 #ifdef LONG_64_BITS
153           if (cp[4] == c)
154             return (char *) &cp[4];
155           if (cp[5] == c)
156             return (char *) &cp[5];
157           if (cp[6] == c)
158             return (char *) &cp[6];
159           if (cp[7] == c)
160             return (char *) &cp[7];
161 #endif /* LONG_64_BITS */
162         }
163
164       n -= sizeof (longword);
165     }
166
167   char_ptr = (const unsigned char *) longword_ptr;
168
169   while (n-- > 0)
170     {
171       if (*char_ptr == c)
172         return (char *) char_ptr;
173       else
174         ++char_ptr;
175     }
176
177   return 0;
178 }