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