Blame string/memchr.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
Packit 6c4009
   with help from Dan Sahlin (dan@sics.se) and
Packit 6c4009
   commentary by Jim Blandy (jimb@ai.mit.edu);
Packit 6c4009
   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
Packit 6c4009
   and implemented by Roland McGrath (roland@ai.mit.edu).
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
# include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
Packit 6c4009
#undef __memchr
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# undef memchr
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef weak_alias
Packit 6c4009
# define __memchr memchr
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifndef MEMCHR
Packit 6c4009
# define MEMCHR __memchr
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Search no more than N bytes of S for C.  */
Packit 6c4009
void *
Packit 6c4009
MEMCHR (void const *s, int c_in, size_t n)
Packit 6c4009
{
Packit 6c4009
  /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
Packit 6c4009
     long instead of a 64-bit uintmax_t tends to give better
Packit 6c4009
     performance.  On 64-bit hardware, unsigned long is generally 64
Packit 6c4009
     bits already.  Change this typedef to experiment with
Packit 6c4009
     performance.  */
Packit 6c4009
  typedef unsigned long int longword;
Packit 6c4009
Packit 6c4009
  const unsigned char *char_ptr;
Packit 6c4009
  const longword *longword_ptr;
Packit 6c4009
  longword repeated_one;
Packit 6c4009
  longword repeated_c;
Packit 6c4009
  unsigned char c;
Packit 6c4009
Packit 6c4009
  c = (unsigned char) c_in;
Packit 6c4009
Packit 6c4009
  /* Handle the first few bytes by reading one byte at a time.
Packit 6c4009
     Do this until CHAR_PTR is aligned on a longword boundary.  */
Packit 6c4009
  for (char_ptr = (const unsigned char *) s;
Packit 6c4009
       n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
Packit 6c4009
       --n, ++char_ptr)
Packit 6c4009
    if (*char_ptr == c)
Packit 6c4009
      return (void *) char_ptr;
Packit 6c4009
Packit 6c4009
  longword_ptr = (const longword *) char_ptr;
Packit 6c4009
Packit 6c4009
  /* All these elucidatory comments refer to 4-byte longwords,
Packit 6c4009
     but the theory applies equally well to any size longwords.  */
Packit 6c4009
Packit 6c4009
  /* Compute auxiliary longword values:
Packit 6c4009
     repeated_one is a value which has a 1 in every byte.
Packit 6c4009
     repeated_c has c in every byte.  */
Packit 6c4009
  repeated_one = 0x01010101;
Packit 6c4009
  repeated_c = c | (c << 8);
Packit 6c4009
  repeated_c |= repeated_c << 16;
Packit 6c4009
  if (0xffffffffU < (longword) -1)
Packit 6c4009
    {
Packit 6c4009
      repeated_one |= repeated_one << 31 << 1;
Packit 6c4009
      repeated_c |= repeated_c << 31 << 1;
Packit 6c4009
      if (8 < sizeof (longword))
Packit 6c4009
	{
Packit 6c4009
	  size_t i;
Packit 6c4009
Packit 6c4009
	  for (i = 64; i < sizeof (longword) * 8; i *= 2)
Packit 6c4009
	    {
Packit 6c4009
	      repeated_one |= repeated_one << i;
Packit 6c4009
	      repeated_c |= repeated_c << i;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Instead of the traditional loop which tests each byte, we will test a
Packit 6c4009
     longword at a time.  The tricky part is testing if *any of the four*
Packit 6c4009
     bytes in the longword in question are equal to c.  We first use an xor
Packit 6c4009
     with repeated_c.  This reduces the task to testing whether *any of the
Packit 6c4009
     four* bytes in longword1 is zero.
Packit 6c4009
Packit 6c4009
     We compute tmp =
Packit 6c4009
       ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
Packit 6c4009
     That is, we perform the following operations:
Packit 6c4009
       1. Subtract repeated_one.
Packit 6c4009
       2. & ~longword1.
Packit 6c4009
       3. & a mask consisting of 0x80 in every byte.
Packit 6c4009
     Consider what happens in each byte:
Packit 6c4009
       - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
Packit 6c4009
	 and step 3 transforms it into 0x80.  A carry can also be propagated
Packit 6c4009
	 to more significant bytes.
Packit 6c4009
       - If a byte of longword1 is nonzero, let its lowest 1 bit be at
Packit 6c4009
	 position k (0 <= k <= 7); so the lowest k bits are 0.  After step 1,
Packit 6c4009
	 the byte ends in a single bit of value 0 and k bits of value 1.
Packit 6c4009
	 After step 2, the result is just k bits of value 1: 2^k - 1.  After
Packit 6c4009
	 step 3, the result is 0.  And no carry is produced.
Packit 6c4009
     So, if longword1 has only non-zero bytes, tmp is zero.
Packit 6c4009
     Whereas if longword1 has a zero byte, call j the position of the least
Packit 6c4009
     significant zero byte.  Then the result has a zero at positions 0, ...,
Packit 6c4009
     j-1 and a 0x80 at position j.  We cannot predict the result at the more
Packit 6c4009
     significant bytes (positions j+1..3), but it does not matter since we
Packit 6c4009
     already have a non-zero bit at position 8*j+7.
Packit 6c4009
Packit 6c4009
     So, the test whether any byte in longword1 is zero is equivalent to
Packit 6c4009
     testing whether tmp is nonzero.  */
Packit 6c4009
Packit 6c4009
  while (n >= sizeof (longword))
Packit 6c4009
    {
Packit 6c4009
      longword longword1 = *longword_ptr ^ repeated_c;
Packit 6c4009
Packit 6c4009
      if ((((longword1 - repeated_one) & ~longword1)
Packit 6c4009
	   & (repeated_one << 7)) != 0)
Packit 6c4009
	break;
Packit 6c4009
      longword_ptr++;
Packit 6c4009
      n -= sizeof (longword);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char_ptr = (const unsigned char *) longword_ptr;
Packit 6c4009
Packit 6c4009
  /* At this point, we know that either n < sizeof (longword), or one of the
Packit 6c4009
     sizeof (longword) bytes starting at char_ptr is == c.  On little-endian
Packit 6c4009
     machines, we could determine the first such byte without any further
Packit 6c4009
     memory accesses, just by looking at the tmp result from the last loop
Packit 6c4009
     iteration.  But this does not work on big-endian machines.  Choose code
Packit 6c4009
     that works in both cases.  */
Packit 6c4009
Packit 6c4009
  for (; n > 0; --n, ++char_ptr)
Packit 6c4009
    {
Packit 6c4009
      if (*char_ptr == c)
Packit 6c4009
	return (void *) char_ptr;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
#ifdef weak_alias
Packit 6c4009
weak_alias (__memchr, memchr)
Packit 6c4009
#endif
Packit 6c4009
libc_hidden_builtin_def (memchr)