1 /* memrchr -- find the last occurrence of a byte in a memory block
3 Copyright (C) 1991, 1993, 1996, 1997, 1999, 2000, 2003, 2004 Free
4 Software Foundation, Inc.
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).
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
37 # define reg_char char
46 # define __memrchr memrchr
49 /* Search no more than N bytes of S for C. */
51 __memrchr (void const *s
, int c_in
, size_t n
)
53 const unsigned char *char_ptr
;
54 const unsigned long int *longword_ptr
;
55 unsigned long int longword
, magic_bits
, charmask
;
59 c
= (unsigned char) c_in
;
61 /* Handle the last few characters by reading one character at a time.
62 Do this until CHAR_PTR is aligned on a longword boundary. */
63 for (char_ptr
= (const unsigned char *) s
+ n
;
64 n
> 0 && (size_t) char_ptr
% sizeof longword
!= 0;
67 return (void *) char_ptr
;
69 /* All these elucidatory comments refer to 4-byte longwords,
70 but the theory applies equally well to any size longwords. */
72 longword_ptr
= (const unsigned long int *) char_ptr
;
74 /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
75 the "holes." Note that there is a hole just to the left of
76 each byte, with an extra at the end:
78 bits: 01111110 11111110 11111110 11111111
79 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
81 The 1-bits make sure that carries propagate to the next 0-bit.
82 The 0-bits provide holes for carries to fall into. */
84 /* Set MAGIC_BITS to be this pattern of 1 and 0 bits.
85 Set CHARMASK to be a longword, each of whose bytes is C. */
87 magic_bits
= 0xfefefefe;
88 charmask
= c
| (c
<< 8);
89 charmask
|= charmask
<< 16;
90 #if 0xffffffffU < ULONG_MAX
91 magic_bits
|= magic_bits
<< 32;
92 charmask
|= charmask
<< 32;
93 if (8 < sizeof longword
)
94 for (i
= 64; i
< sizeof longword
* 8; i
*= 2)
96 magic_bits
|= magic_bits
<< i
;
97 charmask
|= charmask
<< i
;
100 magic_bits
= (ULONG_MAX
>> 1) & (magic_bits
| 1);
102 /* Instead of the traditional loop which tests each character,
103 we will test a longword at a time. The tricky part is testing
104 if *any of the four* bytes in the longword in question are zero. */
105 while (n
>= sizeof longword
)
107 /* We tentatively exit the loop if adding MAGIC_BITS to
108 LONGWORD fails to change any of the hole bits of LONGWORD.
110 1) Is this safe? Will it catch all the zero bytes?
111 Suppose there is a byte with all zeros. Any carry bits
112 propagating from its left will fall into the hole at its
113 least significant bit and stop. Since there will be no
114 carry from its most significant bit, the LSB of the
115 byte to the left will be unchanged, and the zero will be
118 2) Is this worthwhile? Will it ignore everything except
119 zero bytes? Suppose every byte of LONGWORD has a bit set
120 somewhere. There will be a carry into bit 8. If bit 8
121 is set, this will carry into bit 16. If bit 8 is clear,
122 one of bits 9-15 must be set, so there will be a carry
123 into bit 16. Similarly, there will be a carry into bit
124 24. If one of bits 24-30 is set, there will be a carry
125 into bit 31, so all of the hole bits will be changed.
127 The one misfire occurs when bits 24-30 are clear and bit
128 31 is set; in this case, the hole at bit 31 is not
129 changed. If we had access to the processor carry flag,
130 we could close this loophole by putting the fourth hole
133 So it ignores everything except 128's, when they're aligned
136 3) But wait! Aren't we looking for C, not zero?
137 Good point. So what we do is XOR LONGWORD with a longword,
138 each of whose bytes is C. This turns each byte that is C
141 longword
= *--longword_ptr
^ charmask
;
143 /* Add MAGIC_BITS to LONGWORD. */
144 if ((((longword
+ magic_bits
)
146 /* Set those bits that were unchanged by the addition. */
149 /* Look at only the hole bits. If any of the hole bits
150 are unchanged, most likely one of the bytes was a
154 /* Which of the bytes was C? If none of them were, it was
155 a misfire; continue the search. */
157 const unsigned char *cp
= (const unsigned char *) longword_ptr
;
159 if (8 < sizeof longword
)
160 for (i
= sizeof longword
- 1; 8 <= i
; i
--)
162 return (void *) &cp
[i
];
163 if (7 < sizeof longword
&& cp
[7] == c
)
164 return (void *) &cp
[7];
165 if (6 < sizeof longword
&& cp
[6] == c
)
166 return (void *) &cp
[6];
167 if (5 < sizeof longword
&& cp
[5] == c
)
168 return (void *) &cp
[5];
169 if (4 < sizeof longword
&& cp
[4] == c
)
170 return (void *) &cp
[4];
172 return (void *) &cp
[3];
174 return (void *) &cp
[2];
176 return (void *) &cp
[1];
181 n
-= sizeof longword
;
184 char_ptr
= (const unsigned char *) longword_ptr
;
188 if (*--char_ptr
== c
)
189 return (void *) char_ptr
;
195 weak_alias (__memrchr
, memrchr
)