OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gdb-7.2/] [gdb-7.2-or32-1.0rc3/] [gdb/] [gnulib/] [memchr.c] - Diff between revs 330 and 513

Only display areas with differences | Details | Blame | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.