Debian Codegraph @

lib/memchr.c

raw
1 /* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2026
2 Free Software Foundation, Inc.
3
4 Based on strlen implementation by Torbjorn Granlund ([email protected]),
5 with help from Dan Sahlin ([email protected]) and
6 commentary by Jim Blandy ([email protected]);
7 adaptation to memchr suggested by Dick Karpinski ([email protected]),
8 and implemented by Roland McGrath ([email protected]).
9
10 NOTE: The canonical source of this file is maintained with the GNU C Library.
11 Bugs can be reported to [email protected].
12
13 This file is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation; either version 2.1 of the
16 License, or (at your option) any later version.
17
18 This file is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public License
24 along with this program. If not, see <https://www.gnu.org/licenses/>. */
25
26 #ifndef _LIBC
27 # include <config.h>
28 #endif
29
30 #include <string.h>
31
32 #include <stddef.h>
33
34 #if defined _LIBC
35 # include <memcopy.h>
36 #else
37 # define reg_char char
38 #endif
39
40 #include <limits.h>
41
42 #if HAVE_BP_SYM_H || defined _LIBC
43 # include <bp-sym.h>
44 #else
45 # define BP_SYM(sym) sym
46 #endif
47
48 #undef __memchr
49 #ifdef _LIBC
50 # undef memchr
51 #endif
52
53 #ifndef weak_alias
54 # define __memchr memchr
55 #endif
56
57 /* Search no more than N bytes of S for C. */
58 void *
59 __memchr (void const *s, int c_in, size_t n)
60 {
61 /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
62 long instead of a 64-bit uintmax_t tends to give better
63 performance. On 64-bit hardware, unsigned long is generally 64
64 bits already. Change this typedef to experiment with
65 performance. */
66 typedef unsigned long int longword;
67
68 unsigned reg_char c = (unsigned char) c_in;
69
70 const longword *longword_ptr;
71
72 /* Handle the first few bytes by reading one byte at a time.
73 Do this until CHAR_PTR is aligned on a longword boundary. */
74 {
75 const unsigned char *char_ptr;
76 for (char_ptr = (const unsigned char *) s;
77 n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
78 --n, ++char_ptr)
79 if (*char_ptr == c)
80 return (void *) char_ptr;
81
82 longword_ptr = (const longword *) char_ptr;
83 }
84
85 /* All these elucidatory comments refer to 4-byte longwords,
86 but the theory applies equally well to any size longwords. */
87 {
88 /* Compute auxiliary longword values:
89 repeated_one is a value which has a 1 in every byte.
90 repeated_c has c in every byte. */
91 longword repeated_one = 0x01010101;
92 longword repeated_c = c | (c << 8);
93 repeated_c |= repeated_c << 16;
94 if (0xffffffffU < (longword) -1)
95 {
96 repeated_one |= repeated_one << 31 << 1;
97 repeated_c |= repeated_c << 31 << 1;
98 if (8 < sizeof (longword))
99 for (size_t i = 64; i < sizeof (longword) * 8; i *= 2)
100 {
101 repeated_one |= repeated_one << i;
102 repeated_c |= repeated_c << i;
103 }
104 }
105
106 /* Instead of the traditional loop which tests each byte, we will test a
107 longword at a time. The tricky part is testing if *any of the four*
108 bytes in the longword in question are equal to c. We first use an xor
109 with repeated_c. This reduces the task to testing whether *any of the
110 four* bytes in longword1 is zero.
111
112 We compute tmp =
113 ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
114 That is, we perform the following operations:
115 1. Subtract repeated_one.
116 2. & ~longword1.
117 3. & a mask consisting of 0x80 in every byte.
118 Consider what happens in each byte:
119 - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
120 and step 3 transforms it into 0x80. A carry can also be propagated
121 to more significant bytes.
122 - If a byte of longword1 is nonzero, let its lowest 1 bit be at
123 position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
124 the byte ends in a single bit of value 0 and k bits of value 1.
125 After step 2, the result is just k bits of value 1: 2^k - 1. After
126 step 3, the result is 0. And no carry is produced.
127 So, if longword1 has only non-zero bytes, tmp is zero.
128 Whereas if longword1 has a zero byte, call j the position of the least
129 significant zero byte. Then the result has a zero at positions 0, ...,
130 j-1 and a 0x80 at position j. We cannot predict the result at the more
131 significant bytes (positions j+1..3), but it does not matter since we
132 already have a non-zero bit at position 8*j+7.
133
134 So, the test whether any byte in longword1 is zero is equivalent to
135 testing whether tmp is nonzero. */
136
137 while (n >= sizeof (longword))
138 {
139 longword longword1 = *longword_ptr ^ repeated_c;
140
141 if ((((longword1 - repeated_one) & ~longword1)
142 & (repeated_one << 7)) != 0)
143 break;
144 longword_ptr++;
145 n -= sizeof (longword);
146 }
147 }
148
149 {
150 const unsigned char *char_ptr = (const unsigned char *) longword_ptr;
151
152 /* At this point, we know that either n < sizeof (longword), or one of the
153 sizeof (longword) bytes starting at char_ptr is == c. On little-endian
154 machines, we could determine the first such byte without any further
155 memory accesses, just by looking at the tmp result from the last loop
156 iteration. But this does not work on big-endian machines. Choose code
157 that works in both cases. */
158
159 for (; n > 0; --n, ++char_ptr)
160 {
161 if (*char_ptr == c)
162 return (void *) char_ptr;
163 }
164 }
165
166 return NULL;
167 }
168 #ifdef weak_alias
169 weak_alias (__memchr, BP_SYM (memchr))
170 #endif