1 /* strchr -- find the first instance of C in a nul-terminated string.
2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
25 @ r0 = start of string
26 @ r1 = character to match
27 @ returns NULL for no match, or a pointer to the match
28 ldrb r2, [r0] @ load the first byte asap
31 @ To cater to long strings, we want to search through a few
32 @ characters until we reach an aligned pointer. To cater to
33 @ small strings, we don't want to start doing word operations
34 @ immediately. The compromise is a maximum of 16 bytes less
35 @ whatever is required to end with an aligned pointer.
36 @ r3 = number of characters to search in alignment loop
38 rsb r3, r3, #15 @ 16 - 1 peeled loop iteration
41 cmpne r2, #0 @ Found EOS?
44 @ Loop until we find ...
46 subs r3, r3, #1 @ ... the aligment point
48 cmpne r2, r1 @ ... or the character
50 cmpne r2, #0 @ ... or EOS
53 @ Disambiguate the exit possibilites above
54 cmp r2, r1 @ Found the character
56 cmpne r2, #0 @ Found EOS
60 @ So now we're aligned. Now we actually need a stack frame.
61 push { r4, r5, r6, r7 }
62 cfi_adjust_cfa_offset (16)
63 cfi_rel_offset (r4, 0)
64 cfi_rel_offset (r5, 4)
65 cfi_rel_offset (r6, 8)
66 cfi_rel_offset (r7, 12)
69 orr r1, r1, r1, lsl #8 @ Replicate C to all bytes
78 orr r1, r1, r1, lsl #16
80 @ Loop searching for EOS or C, 8 bytes at a time.
82 @ Subtracting (unsigned saturating) from 1 means result of 1 for
83 @ any byte that was originally zero and 0 otherwise. Therefore
84 @ we consider the lsb of each byte the "found" bit.
85 uqsub8 r4, ip, r2 @ Find EOS
86 eor r6, r2, r1 @ Convert C bytes to 0
89 uqsub8 r6, ip, r6 @ Find C
90 pld [r0, #128] @ Prefetch 2 lines ahead
92 orr r4, r4, r6 @ Combine found for EOS and C
94 orrs r6, r4, r5 @ Combine the two words
96 ldrdeq r2, r3, [r0], #8
99 @ Found something. Disambiguate between first and second words.
100 @ Adjust r0 to point to the word containing the match.
101 @ Adjust r2 to the contents of the word containing the match.
102 @ Adjust r4 to the found bits for the word containing the match.
110 @ Find the bit-offset of the match within the word.
111 #if defined(__ARMEL__)
112 @ For LE, swap the found word so clz searches from the little end.
115 @ For BE, byte swap the word to make it easier to extract the byte.
118 @ We're counting 0x01 (not 0x80), so the bit offset is 7 too high.
121 lsr r2, r2, r3 @ Shift down found byte
122 uxtb r1, r1 @ Undo replication of C
123 uxtb r2, r2 @ Extract found byte
124 add r0, r0, r3, lsr #3 @ Adjust the pointer to the found byte
126 pop { r4, r5, r6, r7 }
127 cfi_adjust_cfa_offset (-16)
133 @ Disambiguate between EOS and C.
137 movne r0, #0 @ Found EOS, return NULL
142 weak_alias (strchr, index)
143 libc_hidden_builtin_def (strchr)