1 /* Copyright (C) 1992, 1993, 1994, 1995 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. */
20 /* Return the length of the null-terminated string STR. Scan for
21 the null terminator quickly by testing eight bytes at a time. */
24 strchr (const char *str
, int c
)
27 const unsigned long int *longword_ptr
;
28 unsigned long int charmask
;
30 c
= (unsigned char) c
;
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
)
36 return (char *) char_ptr
;
37 else if (*char_ptr
== '\0')
40 longword_ptr
= (unsigned long int *) char_ptr
;
42 /* Set up a longword, each of whose bytes is C. */
43 charmask
= c
| (c
<< 8);
44 charmask
|= charmask
<< 16;
45 charmask
|= charmask
<< 32;
49 const unsigned long int longword
= *longword_ptr
++;
52 /* Set bits in ZERO if bytes in LONGWORD are zero. */
53 asm ("cmpbge $31, %1, %0" : "=r" (zero
) : "r" (longword
));
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
));
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
));
61 /* Bytes that are both <= and >= are == to C. */
62 if (zero
|| (ge
& le
))
64 /* Which of the bytes was the C? */
66 char *cp
= (char *) (longword_ptr
- 1);
69 for (i
= 0; i
< 8; i
++)
83 weak_alias (strchr
, index
)