1 /* strlen -- find the length of 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 strlen,%function
27 @ r0 = start of string
28 ldrb r2, [r0] @ load the first byte asap
30 @ To cater to long strings, we want to search through a few
31 @ characters until we reach an aligned pointer. To cater to
32 @ small strings, we don't want to start doing word operations
33 @ immediately. The compromise is a maximum of 16 bytes less
34 @ whatever is required to end with an aligned pointer.
35 @ r3 = number of characters to search in alignment loop
37 mov r1, r0 @ Save the input pointer
38 rsb r3, r3, #15 @ 16 - 1 peeled loop iteration
42 @ Loop until we find ...
45 subs r3, r3, #1 @ ... the aligment point
47 cmpne r2, #0 @ ... or EOS
50 @ Disambiguate the exit possibilites above
51 cmp r2, #0 @ Found EOS
55 @ So now we're aligned.
66 @ Loop searching for EOS, 8 bytes at a time.
67 @ Subtracting (unsigned saturating) from 1 for any byte means that
68 @ we get 1 for any byte that was originally zero and 0 otherwise.
69 @ Therefore we consider the lsb of each byte the "found" bit.
71 2: uqsub8 r2, ip, r2 @ Find EOS
73 pld [r0, #128] @ Prefetch 2 lines ahead
74 orrs r3, r3, r2 @ Combine the two words
76 ldrdeq r2, r3, [r0], #8
79 @ Found something. Disambiguate between first and second words.
80 @ Adjust r0 to point to the word containing the match.
81 @ Adjust r2 to the found bits for the word containing the match.
88 @ Find the bit-offset of the match within the word. Note that the
89 @ bit result from clz will be 7 higher than "true", but we'll
90 @ immediately discard those bits converting to a byte offset.
92 rev r2, r2 @ For LE, count from the little end
95 add r0, r0, r2, lsr #3 @ Adjust the pointer to the found byte
97 sub r0, r0, r1 @ Subtract input to compute length