1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Searching in a string.
4 Copyright (C) 2003, 2007-2019 Free Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include <string.h>
23
24 /* Find the first occurrence of C in S or the final NUL byte. */
25 char *
strchrnul(const char * s,int c_in)26 strchrnul (const char *s, int c_in)
27 {
28 /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
29 long instead of a 64-bit uintmax_t tends to give better
30 performance. On 64-bit hardware, unsigned long is generally 64
31 bits already. Change this typedef to experiment with
32 performance. */
33 typedef unsigned long int longword;
34
35 const unsigned char *char_ptr;
36 const longword *longword_ptr;
37 longword repeated_one;
38 longword repeated_c;
39 unsigned char c;
40
41 c = (unsigned char) c_in;
42 if (!c)
43 return rawmemchr (s, 0);
44
45 /* Handle the first few bytes by reading one byte at a time.
46 Do this until CHAR_PTR is aligned on a longword boundary. */
47 for (char_ptr = (const unsigned char *) s;
48 (size_t) char_ptr % sizeof (longword) != 0;
49 ++char_ptr)
50 if (!*char_ptr || *char_ptr == c)
51 return (char *) char_ptr;
52
53 longword_ptr = (const longword *) char_ptr;
54
55 /* All these elucidatory comments refer to 4-byte longwords,
56 but the theory applies equally well to any size longwords. */
57
58 /* Compute auxiliary longword values:
59 repeated_one is a value which has a 1 in every byte.
60 repeated_c has c in every byte. */
61 repeated_one = 0x01010101;
62 repeated_c = c | (c << 8);
63 repeated_c |= repeated_c << 16;
64 if (0xffffffffU < (longword) -1)
65 {
66 repeated_one |= repeated_one << 31 << 1;
67 repeated_c |= repeated_c << 31 << 1;
68 if (8 < sizeof (longword))
69 {
70 size_t i;
71
72 for (i = 64; i < sizeof (longword) * 8; i *= 2)
73 {
74 repeated_one |= repeated_one << i;
75 repeated_c |= repeated_c << i;
76 }
77 }
78 }
79
80 /* Instead of the traditional loop which tests each byte, we will
81 test a longword at a time. The tricky part is testing if *any of
82 the four* bytes in the longword in question are equal to NUL or
83 c. We first use an xor with repeated_c. This reduces the task
84 to testing whether *any of the four* bytes in longword1 or
85 longword2 is zero.
86
87 Let's consider longword1. We compute tmp =
88 ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
89 That is, we perform the following operations:
90 1. Subtract repeated_one.
91 2. & ~longword1.
92 3. & a mask consisting of 0x80 in every byte.
93 Consider what happens in each byte:
94 - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
95 and step 3 transforms it into 0x80. A carry can also be propagated
96 to more significant bytes.
97 - If a byte of longword1 is nonzero, let its lowest 1 bit be at
98 position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
99 the byte ends in a single bit of value 0 and k bits of value 1.
100 After step 2, the result is just k bits of value 1: 2^k - 1. After
101 step 3, the result is 0. And no carry is produced.
102 So, if longword1 has only non-zero bytes, tmp is zero.
103 Whereas if longword1 has a zero byte, call j the position of the least
104 significant zero byte. Then the result has a zero at positions 0, ...,
105 j-1 and a 0x80 at position j. We cannot predict the result at the more
106 significant bytes (positions j+1..3), but it does not matter since we
107 already have a non-zero bit at position 8*j+7.
108
109 The test whether any byte in longword1 or longword2 is zero is equivalent
110 to testing whether tmp1 is nonzero or tmp2 is nonzero. We can combine
111 this into a single test, whether (tmp1 | tmp2) is nonzero.
112
113 This test can read more than one byte beyond the end of a string,
114 depending on where the terminating NUL is encountered. However,
115 this is considered safe since the initialization phase ensured
116 that the read will be aligned, therefore, the read will not cross
117 page boundaries and will not cause a fault. */
118
119 while (1)
120 {
121 longword longword1 = *longword_ptr ^ repeated_c;
122 longword longword2 = *longword_ptr;
123
124 if (((((longword1 - repeated_one) & ~longword1)
125 | ((longword2 - repeated_one) & ~longword2))
126 & (repeated_one << 7)) != 0)
127 break;
128 longword_ptr++;
129 }
130
131 char_ptr = (const unsigned char *) longword_ptr;
132
133 /* At this point, we know that one of the sizeof (longword) bytes
134 starting at char_ptr is == 0 or == c. On little-endian machines,
135 we could determine the first such byte without any further memory
136 accesses, just by looking at the tmp result from the last loop
137 iteration. But this does not work on big-endian machines.
138 Choose code that works in both cases. */
139
140 char_ptr = (unsigned char *) longword_ptr;
141 while (*char_ptr && (*char_ptr != c))
142 char_ptr++;
143 return (char *) char_ptr;
144 }
145