Blame gettext-tools/gnulib-lib/rawmemchr.c

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