1 /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
3 Copyright (C) 1996-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
22 /* Normally, this uses ((xword - 0x01010101) & 0x80808080) test
23 to find out if any byte in xword could be zero. This is fast, but
24 also gives false alarm for any byte in range 0x81-0xff. It does
25 not matter for correctness, as if this test tells us there could
26 be some zero byte, we check it byte by byte, but if bytes with
27 high bits set are common in the strings, then this will give poor
28 performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
29 will use one tick slower, but more precise test
30 ((xword - 0x01010101) & (~xword) & 0x80808080),
31 which does not give any false alarms (but if some bits are set,
32 one cannot assume from it which bytes are zero and which are not).
33 It is yet to be measured, what is the correct default for glibc
34 in these days for an average user.
45 sethi %hi(0x80808080), %o4
58 or %o4, %lo(0x80808080), %o3
67 sethi %hi(0x01010101), %o5
74 or %o5, %lo(0x01010101), %o2
80 13: or %o4, %lo(0x80808080), %o3
81 4: sethi %hi(0x01010101), %o5
82 5: or %o5, %lo(0x01010101), %o2
86 #ifdef EIGHTBIT_NOT_RARE
98 /* Check every byte. */
100 7: andcc %g5, 0xff, %g5
136 or %o4, %lo(0x80808080), %o3
143 sethi %hi(0x01010101), %o4
149 or %o4, %lo(0x01010101), %o2
153 12: andcc %o0, 3, %g0
155 sethi %hi(0x80808080), %o4
156 or %o4, %lo(0x80808080), %o3
157 4: sethi %hi(0x01010101), %o4
158 5: or %o4, %lo(0x01010101), %o2
161 #ifdef EIGHTBIT_NOT_RARE
168 /* Check every byte. */
197 or %o4, %lo(0x80808080), %o3
207 sethi %hi(0x01010101), %o4
215 1: or %o4, %lo(0x01010101), %o2
227 sethi %hi(0x80808080), %o4
231 or %o4, %lo(0x80808080), %o3
232 4: sethi %hi(0x01010101), %o4
233 5: or %o4, %lo(0x01010101), %o2
237 #ifdef EIGHTBIT_NOT_RARE
249 /* Check every byte. */
251 8: andcc %g5, 0xff, %g5
268 1: andcc %g4, 0xff, %g5
279 weak_alias (strchr, index)
280 weak_alias (strrchr, rindex)
281 libc_hidden_builtin_def (strchr)
282 libc_hidden_builtin_def (strrchr)