1
/* strchr -- find the first instance of C in a nul-terminated string.
2
Copyright (C) 2013 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
<http://www.gnu.org/licenses/>. */
24
.type strchr,%function
28
@ r0 = start of string
29
@ r1 = character to match
30
@ returns NULL for no match, or a pointer to the match
31
ldrb r2, [r0] @ load the first byte asap
34
@ To cater to long strings, we want to search through a few
35
@ characters until we reach an aligned pointer. To cater to
36
@ small strings, we don't want to start doing word operations
37
@ immediately. The compromise is a maximum of 16 bytes less
38
@ whatever is required to end with an aligned pointer.
39
@ r3 = number of characters to search in alignment loop
41
rsb r3, r3, #15 @ 16 - 1 peeled loop iteration
44
cmpne r2, #0 @ Found EOS?
47
@ Loop until we find ...
49
subs r3, r3, #1 @ ... the aligment point
51
cmpne r2, r1 @ ... or the character
53
cmpne r2, #0 @ ... or EOS
56
@ Disambiguate the exit possibilites above
57
cmp r2, r1 @ Found the character
59
cmpne r2, #0 @ Found EOS
63
@ So now we're aligned. Now we actually need a stack frame.
64
push { r4, r5, r6, r7 }
67
orr r1, r1, r1, lsl #8 @ Replicate C to all bytes
76
orr r1, r1, r1, lsl #16
78
@ Loop searching for EOS or C, 8 bytes at a time.
80
@ Subtracting (unsigned saturating) from 1 means result of 1 for
81
@ any byte that was originally zero and 0 otherwise. Therefore
82
@ we consider the lsb of each byte the "found" bit.
83
uqsub8 r4, ip, r2 @ Find EOS
84
eor r6, r2, r1 @ Convert C bytes to 0
87
uqsub8 r6, ip, r6 @ Find C
88
pld [r0, #128] @ Prefetch 2 lines ahead
90
orr r4, r4, r6 @ Combine found for EOS and C
92
orrs r6, r4, r5 @ Combine the two words
94
ldrdeq r2, r3, [r0], #8
97
@ Found something. Disambiguate between first and second words.
98
@ Adjust r0 to point to the word containing the match.
99
@ Adjust r2 to the contents of the word containing the match.
100
@ Adjust r4 to the found bits for the word containing the match.
108
@ Find the bit-offset of the match within the word.
109
#if defined(__ARMEL__)
110
@ For LE, swap the found word so clz searches from the little end.
113
@ For BE, byte swap the word to make it easier to extract the byte.
116
@ We're counting 0x01 (not 0x80), so the bit offset is 7 too high.
119
lsr r2, r2, r3 @ Shift down found byte
120
uxtb r1, r1 @ Undo replication of C
121
uxtb r2, r2 @ Extract found byte
122
add r0, r0, r3, lsr #3 @ Adjust the pointer to the found byte
124
pop { r4, r5, r6, r7 }
126
@ Disambiguate between EOS and C.
130
movne r0, #0 @ Found EOS, return NULL
132
.size strchr,.-strchr