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