]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 2011-2018 Free Software Foundation, Inc. |
63d143a2 CM |
2 | This file is part of the GNU C Library. |
3 | Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
ab84e3ff PE |
16 | License along with the GNU C Library. If not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
63d143a2 CM |
18 | |
19 | #include <string.h> | |
20 | #include <stdint.h> | |
21 | #include "string-endian.h" | |
22 | ||
23 | char * | |
24 | __strchrnul (const char *s, int c) | |
25 | { | |
26 | int z, g; | |
27 | ||
28 | /* Get an aligned pointer. */ | |
29 | const uintptr_t s_int = (uintptr_t) s; | |
30 | const uint64_t *p = (const uint64_t *) (s_int & -8); | |
31 | ||
32 | /* Create eight copies of the byte for which we are looking. */ | |
64d76ca0 | 33 | const uint64_t goal = copy_byte(c); |
63d143a2 CM |
34 | |
35 | /* Read the first aligned word, but force bytes before the string to | |
36 | match neither zero nor goal (we make sure the high bit of each byte | |
37 | is 1, and the low 7 bits are all the opposite of the goal byte). */ | |
38 | const uint64_t before_mask = MASK (s_int); | |
f18b8dc7 | 39 | uint64_t v = (*p | before_mask) ^ (goal & v1shrui (before_mask, 1)); |
63d143a2 CM |
40 | |
41 | uint64_t zero_matches, goal_matches; | |
42 | while (1) | |
43 | { | |
44 | /* Look for a terminating '\0'. */ | |
f18b8dc7 | 45 | zero_matches = v1cmpeqi (v, 0); |
63d143a2 CM |
46 | |
47 | /* Look for the goal byte. */ | |
f18b8dc7 | 48 | goal_matches = v1cmpeq (v, goal); |
63d143a2 CM |
49 | |
50 | if (__builtin_expect ((zero_matches | goal_matches) != 0, 0)) | |
51 | break; | |
52 | ||
53 | v = *++p; | |
54 | } | |
55 | ||
56 | z = CFZ (zero_matches); | |
57 | g = CFZ (goal_matches); | |
58 | ||
59 | /* Return a pointer to the NUL or goal, whichever is first. */ | |
60 | if (z < g) | |
61 | g = z; | |
62 | return ((char *) p) + (g >> 3); | |
63 | } | |
64 | weak_alias (__strchrnul, strchrnul) |