1 /* Copyright (C) 1991-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
4 with help from Dan Sahlin (dan@sics.se) and
5 commentary by Jim Blandy (jimb@ai.mit.edu);
6 adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
7 and implemented by Roland McGrath (roland@ai.mit.edu).
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 The GNU C Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, see
21 <https://www.gnu.org/licenses/>. */
39 # define __memchr memchr
43 # define MEMCHR __memchr
46 /* Search no more than N bytes of S for C. */
48 MEMCHR (void const *s
, int c_in
, size_t n
)
50 /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
51 long instead of a 64-bit uintmax_t tends to give better
52 performance. On 64-bit hardware, unsigned long is generally 64
53 bits already. Change this typedef to experiment with
55 typedef unsigned long int longword
;
57 const unsigned char *char_ptr
;
58 const longword
*longword_ptr
;
59 longword repeated_one
;
63 c
= (unsigned char) c_in
;
65 /* Handle the first few bytes by reading one byte at a time.
66 Do this until CHAR_PTR is aligned on a longword boundary. */
67 for (char_ptr
= (const unsigned char *) s
;
68 n
> 0 && (size_t) char_ptr
% sizeof (longword
) != 0;
71 return (void *) char_ptr
;
73 longword_ptr
= (const longword
*) char_ptr
;
75 /* All these elucidatory comments refer to 4-byte longwords,
76 but the theory applies equally well to any size longwords. */
78 /* Compute auxiliary longword values:
79 repeated_one is a value which has a 1 in every byte.
80 repeated_c has c in every byte. */
81 repeated_one
= 0x01010101;
82 repeated_c
= c
| (c
<< 8);
83 repeated_c
|= repeated_c
<< 16;
84 if (0xffffffffU
< (longword
) -1)
86 repeated_one
|= repeated_one
<< 31 << 1;
87 repeated_c
|= repeated_c
<< 31 << 1;
88 if (8 < sizeof (longword
))
92 for (i
= 64; i
< sizeof (longword
) * 8; i
*= 2)
94 repeated_one
|= repeated_one
<< i
;
95 repeated_c
|= repeated_c
<< i
;
100 /* Instead of the traditional loop which tests each byte, we will test a
101 longword at a time. The tricky part is testing if *any of the four*
102 bytes in the longword in question are equal to c. We first use an xor
103 with repeated_c. This reduces the task to testing whether *any of the
104 four* bytes in longword1 is zero.
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, tmp 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 So, the test whether any byte in longword1 is zero is equivalent to
129 testing whether tmp is nonzero. */
131 while (n
>= sizeof (longword
))
133 longword longword1
= *longword_ptr
^ repeated_c
;
135 if ((((longword1
- repeated_one
) & ~longword1
)
136 & (repeated_one
<< 7)) != 0)
139 n
-= sizeof (longword
);
142 char_ptr
= (const unsigned char *) longword_ptr
;
144 /* At this point, we know that either n < sizeof (longword), or one of the
145 sizeof (longword) bytes starting at char_ptr is == c. On little-endian
146 machines, we could determine the first such byte without any further
147 memory accesses, just by looking at the tmp result from the last loop
148 iteration. But this does not work on big-endian machines. Choose code
149 that works in both cases. */
151 for (; n
> 0; --n
, ++char_ptr
)
154 return (void *) char_ptr
;
160 weak_alias (__memchr
, memchr
)
162 libc_hidden_builtin_def (memchr
)