annotate lib/strchrnul.c @ 423:079d43b6967b 3.0-beta2 3.0-beta2

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