home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / alpha / strlen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  1.7 KB  |  57 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. size_t
  24. strlen (const char *str)
  25. {
  26.   const char *char_ptr;
  27.   const unsigned long int *longword_ptr;
  28.  
  29.   /* Handle the first few characters by reading one character at a time.
  30.      Do this until STR is aligned on a 8-byte border.  */
  31.   for (char_ptr = str; ((unsigned long int) char_ptr & 7) != 0; ++char_ptr)
  32.     if (*char_ptr == '\0')
  33.       return char_ptr - str;
  34.  
  35.   longword_ptr = (unsigned long int *) char_ptr;
  36.  
  37.   for (;;)
  38.     {
  39.       int mask;
  40.       asm ("cmpbge %1, %2, %0" : "=r" (mask) : "r" (0), "r" (*longword_ptr++));
  41.       if (mask)
  42.     {
  43.       /* Which of the bytes was the zero?  */
  44.  
  45.       const char *cp = (const char *) (longword_ptr - 1);
  46.  
  47.       if (cp[0] == 0)
  48.         return cp - str;
  49.       if (cp[1] == 0)
  50.         return cp - str + 1;
  51.       if (cp[2] == 0)
  52.         return cp - str + 2;
  53.       return cp - str + 3;
  54.     }
  55.     }
  56. }
  57.