2N/A/* Searching in a string.
2N/A Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Specification. */
2N/A#include <string.h>
2N/A
2N/A/* Find the first occurrence of C in S. */
2N/Avoid *
2N/Arawmemchr (const void *s, int c_in)
2N/A{
2N/A /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
2N/A long instead of a 64-bit uintmax_t tends to give better
2N/A performance. On 64-bit hardware, unsigned long is generally 64
2N/A bits already. Change this typedef to experiment with
2N/A performance. */
2N/A typedef unsigned long int longword;
2N/A
2N/A const unsigned char *char_ptr;
2N/A const longword *longword_ptr;
2N/A longword repeated_one;
2N/A longword repeated_c;
2N/A unsigned char c;
2N/A
2N/A c = (unsigned char) c_in;
2N/A
2N/A /* Handle the first few bytes by reading one byte at a time.
2N/A Do this until CHAR_PTR is aligned on a longword boundary. */
2N/A for (char_ptr = (const unsigned char *) s;
2N/A (size_t) char_ptr % sizeof (longword) != 0;
2N/A ++char_ptr)
2N/A if (*char_ptr == c)
2N/A return (void *) char_ptr;
2N/A
2N/A longword_ptr = (const longword *) char_ptr;
2N/A
2N/A /* All these elucidatory comments refer to 4-byte longwords,
2N/A but the theory applies equally well to any size longwords. */
2N/A
2N/A /* Compute auxiliary longword values:
2N/A repeated_one is a value which has a 1 in every byte.
2N/A repeated_c has c in every byte. */
2N/A repeated_one = 0x01010101;
2N/A repeated_c = c | (c << 8);
2N/A repeated_c |= repeated_c << 16;
2N/A if (0xffffffffU < (longword) -1)
2N/A {
2N/A repeated_one |= repeated_one << 31 << 1;
2N/A repeated_c |= repeated_c << 31 << 1;
2N/A if (8 < sizeof (longword))
2N/A {
2N/A size_t i;
2N/A
2N/A for (i = 64; i < sizeof (longword) * 8; i *= 2)
2N/A {
2N/A repeated_one |= repeated_one << i;
2N/A repeated_c |= repeated_c << i;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Instead of the traditional loop which tests each byte, we will
2N/A test a longword at a time. The tricky part is testing if *any of
2N/A the four* bytes in the longword in question are equal to NUL or
2N/A c. We first use an xor with repeated_c. This reduces the task
2N/A to testing whether *any of the four* bytes in longword1 is zero.
2N/A
2N/A We compute tmp =
2N/A ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
2N/A That is, we perform the following operations:
2N/A 1. Subtract repeated_one.
2N/A 2. & ~longword1.
2N/A 3. & a mask consisting of 0x80 in every byte.
2N/A Consider what happens in each byte:
2N/A - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
2N/A and step 3 transforms it into 0x80. A carry can also be propagated
2N/A to more significant bytes.
2N/A - If a byte of longword1 is nonzero, let its lowest 1 bit be at
2N/A position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
2N/A the byte ends in a single bit of value 0 and k bits of value 1.
2N/A After step 2, the result is just k bits of value 1: 2^k - 1. After
2N/A step 3, the result is 0. And no carry is produced.
2N/A So, if longword1 has only non-zero bytes, tmp is zero.
2N/A Whereas if longword1 has a zero byte, call j the position of the least
2N/A significant zero byte. Then the result has a zero at positions 0, ...,
2N/A j-1 and a 0x80 at position j. We cannot predict the result at the more
2N/A significant bytes (positions j+1..3), but it does not matter since we
2N/A already have a non-zero bit at position 8*j+7.
2N/A
2N/A The test whether any byte in longword1 is zero is equivalent
2N/A to testing whether tmp is nonzero.
2N/A
2N/A This test can read beyond the end of a string, depending on where
2N/A C_IN is encountered. However, this is considered safe since the
2N/A initialization phase ensured that the read will be aligned,
2N/A therefore, the read will not cross page boundaries and will not
2N/A cause a fault. */
2N/A
2N/A while (1)
2N/A {
2N/A longword longword1 = *longword_ptr ^ repeated_c;
2N/A
2N/A if ((((longword1 - repeated_one) & ~longword1)
2N/A & (repeated_one << 7)) != 0)
2N/A break;
2N/A longword_ptr++;
2N/A }
2N/A
2N/A char_ptr = (const unsigned char *) longword_ptr;
2N/A
2N/A /* At this point, we know that one of the sizeof (longword) bytes
2N/A starting at char_ptr is == c. On little-endian machines, we
2N/A could determine the first such byte without any further memory
2N/A accesses, just by looking at the tmp result from the last loop
2N/A iteration. But this does not work on big-endian machines.
2N/A Choose code that works in both cases. */
2N/A
2N/A char_ptr = (unsigned char *) longword_ptr;
2N/A while (*char_ptr != c)
2N/A char_ptr++;
2N/A return (void *) char_ptr;
2N/A}