1 /* Copyright (C) 2011-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
21 #include "string-endian.h"
24 __strchrnul (const char *s
, int c
)
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);
32 /* Create eight copies of the byte for which we are looking. */
33 const uint64_t goal
= copy_byte(c
);
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
);
39 uint64_t v
= (*p
| before_mask
) ^ (goal
& v1shrui (before_mask
, 1));
41 uint64_t zero_matches
, goal_matches
;
44 /* Look for a terminating '\0'. */
45 zero_matches
= v1cmpeqi (v
, 0);
47 /* Look for the goal byte. */
48 goal_matches
= v1cmpeq (v
, goal
);
50 if (__builtin_expect ((zero_matches
| goal_matches
) != 0, 0))
56 z
= CFZ (zero_matches
);
57 g
= CFZ (goal_matches
);
59 /* Return a pointer to the NUL or goal, whichever is first. */
62 return ((char *) p
) + (g
>> 3);
64 weak_alias (__strchrnul
, strchrnul
)