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