1 /* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2011
2 Free Software Foundation, Inc.
4 Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
5 with help from Dan Sahlin (dan@sics.se) and
6 commentary by Jim Blandy (jimb@ai.mit.edu);
7 adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
8 and implemented in glibc by Roland McGrath (roland@ai.mit.edu).
9 Extension to memchr2 implemented by Eric Blake (ebb9@byu.net).
11 This program is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 3 of the License, or any
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* Return the first address of either C1 or C2 (treated as unsigned
33 char) that occurs within N bytes of the memory region S. If
34 neither byte appears, return NULL. */
36 memchr2 (void const *s, int c1_in, int c2_in, size_t n)
38 /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
39 long instead of a 64-bit uintmax_t tends to give better
40 performance. On 64-bit hardware, unsigned long is generally 64
41 bits already. Change this typedef to experiment with
43 typedef unsigned long int longword;
45 const unsigned char *char_ptr;
46 const longword *longword_ptr;
47 longword repeated_one;
53 c1 = (unsigned char) c1_in;
54 c2 = (unsigned char) c2_in;
57 return memchr (s, c1, n);
59 /* Handle the first few bytes by reading one byte at a time.
60 Do this until CHAR_PTR is aligned on a longword boundary. */
61 for (char_ptr = (const unsigned char *) s;
62 n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
64 if (*char_ptr == c1 || *char_ptr == c2)
65 return (void *) char_ptr;
67 longword_ptr = (const longword *) char_ptr;
69 /* All these elucidatory comments refer to 4-byte longwords,
70 but the theory applies equally well to any size longwords. */
72 /* Compute auxiliary longword values:
73 repeated_one is a value which has a 1 in every byte.
74 repeated_c1 has c1 in every byte.
75 repeated_c2 has c2 in every byte. */
76 repeated_one = 0x01010101;
77 repeated_c1 = c1 | (c1 << 8);
78 repeated_c2 = c2 | (c2 << 8);
79 repeated_c1 |= repeated_c1 << 16;
80 repeated_c2 |= repeated_c2 << 16;
81 if (0xffffffffU < (longword) -1)
83 repeated_one |= repeated_one << 31 << 1;
84 repeated_c1 |= repeated_c1 << 31 << 1;
85 repeated_c2 |= repeated_c2 << 31 << 1;
86 if (8 < sizeof (longword))
90 for (i = 64; i < sizeof (longword) * 8; i *= 2)
92 repeated_one |= repeated_one << i;
93 repeated_c1 |= repeated_c1 << i;
94 repeated_c2 |= repeated_c2 << i;
99 /* Instead of the traditional loop which tests each byte, we will test a
100 longword at a time. The tricky part is testing if *any of the four*
101 bytes in the longword in question are equal to c1 or c2. We first use
102 an xor with repeated_c1 and repeated_c2, respectively. This reduces
103 the task to testing whether *any of the four* bytes in longword1 or
106 Let's consider longword1. We compute tmp1 =
107 ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
108 That is, we perform the following operations:
109 1. Subtract repeated_one.
111 3. & a mask consisting of 0x80 in every byte.
112 Consider what happens in each byte:
113 - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
114 and step 3 transforms it into 0x80. A carry can also be propagated
115 to more significant bytes.
116 - If a byte of longword1 is nonzero, let its lowest 1 bit be at
117 position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
118 the byte ends in a single bit of value 0 and k bits of value 1.
119 After step 2, the result is just k bits of value 1: 2^k - 1. After
120 step 3, the result is 0. And no carry is produced.
121 So, if longword1 has only non-zero bytes, tmp1 is zero.
122 Whereas if longword1 has a zero byte, call j the position of the least
123 significant zero byte. Then the result has a zero at positions 0, ...,
124 j-1 and a 0x80 at position j. We cannot predict the result at the more
125 significant bytes (positions j+1..3), but it does not matter since we
126 already have a non-zero bit at position 8*j+7.
128 Similary, we compute tmp2 =
129 ((longword2 - repeated_one) & ~longword2) & (repeated_one << 7).
131 The test whether any byte in longword1 or longword2 is zero is equivalent
132 to testing whether tmp1 is nonzero or tmp2 is nonzero. We can combine
133 this into a single test, whether (tmp1 | tmp2) is nonzero. */
135 while (n >= sizeof (longword))
137 longword longword1 = *longword_ptr ^ repeated_c1;
138 longword longword2 = *longword_ptr ^ repeated_c2;
140 if (((((longword1 - repeated_one) & ~longword1)
141 | ((longword2 - repeated_one) & ~longword2))
142 & (repeated_one << 7)) != 0)
145 n -= sizeof (longword);
148 char_ptr = (const unsigned char *) longword_ptr;
150 /* At this point, we know that either n < sizeof (longword), or one of the
151 sizeof (longword) bytes starting at char_ptr is == c1 or == c2. On
152 little-endian machines, we could determine the first such byte without
153 any further memory accesses, just by looking at the (tmp1 | tmp2) result
154 from the last loop iteration. But this does not work on big-endian
155 machines. Choose code that works in both cases. */
157 for (; n > 0; --n, ++char_ptr)
159 if (*char_ptr == c1 || *char_ptr == c2)
160 return (void *) char_ptr;