home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / alpha / strchr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  2.6 KB  |  86 lines

  1. /* Copyright (C) 1992 Free Software Foundation, Inc.
  2.  
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7.  
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. Library General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB.  If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.  */
  17.  
  18. #include <string.h>
  19.  
  20. /* Return the length of the null-terminated string STR.  Scan for
  21.    the null terminator quickly by testing eight bytes at a time.  */
  22.  
  23. char *
  24. strchr (const char *str, int c)
  25. {
  26.   const char *char_ptr;
  27.   const unsigned long int *longword_ptr;
  28.   unsigned long int charmask;
  29.  
  30.   c = (unsigned char) c;
  31.  
  32.   /* Handle the first few characters by reading one character at a time.
  33.      Do this until STR is aligned on a 8-byte border.  */
  34.   for (char_ptr = str; ((unsigned long int) char_ptr & 7) != 0; ++char_ptr)
  35.     if (*char_ptr == '\0')
  36.       return NULL;
  37.     else if (*char_ptr == c)
  38.       return char_ptr;
  39.  
  40.   longword_ptr = (unsigned long int *) char_ptr;
  41.  
  42.   /* Set up a longword, each of whose bytes is C.  */
  43.   charmask = c | (c << 8);
  44.   charmask |= charmask << 16;
  45.   charmask |= charmask << 32;
  46.  
  47.   for (;;)
  48.     {
  49.       const unsigned long int longword = *longword_ptr++;
  50.       int ge, le, zero;
  51.  
  52.       /* Set bits in ZERO if bytes in LONGWORD are zero.  */
  53.       asm ("cmpbge $31, %1, %0" : "=r" (zero) : "r" (longword));
  54.  
  55.       /* Set bits in GE if bytes in CHARMASK are >= bytes in LONGWORD.  */
  56.       asm ("cmpbge %1, %2, %0" : "=r" (ge) : "r" (charmask), "r" (longword));
  57.  
  58.       /* Set bits in LE if bytes in CHARMASK are <= bytes in LONGWORD.  */
  59.       asm ("cmpbge %2, %1, %0" : "=r" (le) : "r" (charmask), "r" (longword));
  60.  
  61.       /* Bytes that are both <= and >= are == to C.  */
  62.       if (zero || (ge & le))
  63.     {
  64.       /* Which of the bytes was the C?  */
  65.  
  66.       const char *cp = (const char *) (longword_ptr - 1);
  67.  
  68.       if (cp[0] == c)
  69.         return cp;
  70.       if (cp[0] == 0)
  71.         return NULL;
  72.       if (cp[1] == c)
  73.         return &cp[1];
  74.       if (cp[1] == 0)
  75.         return NULL;
  76.       if (cp[2] == c)
  77.         return &cp[2];
  78.       if (cp[2] == 0)
  79.         return NULL;
  80.       if (cp[3] == c)
  81.         return &cp[3];
  82.       return NULL;
  83.     }
  84.     }
  85. }
  86.