.
[glibc.git] / sysdeps / alpha / strlen.c
blob36f106c9c3138a762bd33e377664d801325f27ce
1 /* Copyright (C) 1992 Free Software Foundation, Inc.
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.
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.
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. */
18 #include <string.h>
20 /* Return the length of the null-terminated string STR. Scan for
21 the null terminator quickly by testing eight bytes at a time. */
23 size_t
24 strlen (const char *str)
26 const char *char_ptr;
27 const unsigned long int *longword_ptr;
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;
35 longword_ptr = (unsigned long int *) char_ptr;
37 for (;;)
39 const unsigned long int longword = *longword_ptr++;
40 int mask;
42 /* Set bits in MASK if bytes in LONGWORD are zero. */
43 asm ("cmpbge $31, %1, %0" : "=r" (mask) : "r" (longword));
44 if (mask)
46 /* Which of the bytes was the zero? */
47 const char *cp = (const char *) (longword_ptr - 1);
48 int i;
50 for (i = 0; i < 8; i++)
51 if (cp[i] == 0)
52 return cp - str + i;