Blame gnulib/lib/rawmemchr.c

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