|
Packit |
6c4009 |
/* strlen -- find the length of a nul-terminated string.
|
|
Packit |
6c4009 |
Copyright (C) 2013-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library. If not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.syntax unified
|
|
Packit |
6c4009 |
.text
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY (strlen)
|
|
Packit |
6c4009 |
@ r0 = start of string
|
|
Packit |
6c4009 |
ldrb r2, [r0] @ load the first byte asap
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ To cater to long strings, we want to search through a few
|
|
Packit |
6c4009 |
@ characters until we reach an aligned pointer. To cater to
|
|
Packit |
6c4009 |
@ small strings, we don't want to start doing word operations
|
|
Packit |
6c4009 |
@ immediately. The compromise is a maximum of 16 bytes less
|
|
Packit |
6c4009 |
@ whatever is required to end with an aligned pointer.
|
|
Packit |
6c4009 |
@ r3 = number of characters to search in alignment loop
|
|
Packit |
6c4009 |
and r3, r0, #7
|
|
Packit |
6c4009 |
mov r1, r0 @ Save the input pointer
|
|
Packit |
6c4009 |
rsb r3, r3, #15 @ 16 - 1 peeled loop iteration
|
|
Packit |
6c4009 |
cmp r2, #0
|
|
Packit |
6c4009 |
beq 99f
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Loop until we find ...
|
|
Packit |
6c4009 |
1: ldrb r2, [r0, #1]!
|
|
Packit |
6c4009 |
subs r3, r3, #1 @ ... the aligment point
|
|
Packit |
6c4009 |
it ne
|
|
Packit |
6c4009 |
cmpne r2, #0 @ ... or EOS
|
|
Packit |
6c4009 |
bne 1b
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Disambiguate the exit possibilites above
|
|
Packit |
6c4009 |
cmp r2, #0 @ Found EOS
|
|
Packit |
6c4009 |
beq 99f
|
|
Packit |
6c4009 |
add r0, r0, #1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ So now we're aligned.
|
|
Packit |
6c4009 |
ldrd r2, r3, [r0], #8
|
|
Packit |
6c4009 |
#ifdef ARCH_HAS_T2
|
|
Packit |
6c4009 |
movw ip, #0x0101
|
|
Packit |
6c4009 |
pld [r0, #64]
|
|
Packit |
6c4009 |
movt ip, #0x0101
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
ldr ip, =0x01010101
|
|
Packit |
6c4009 |
pld [r0, #64]
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Loop searching for EOS, 8 bytes at a time.
|
|
Packit |
6c4009 |
@ Subtracting (unsigned saturating) from 1 for any byte means that
|
|
Packit |
6c4009 |
@ we get 1 for any byte that was originally zero and 0 otherwise.
|
|
Packit |
6c4009 |
@ Therefore we consider the lsb of each byte the "found" bit.
|
|
Packit |
6c4009 |
.balign 16
|
|
Packit |
6c4009 |
2: uqsub8 r2, ip, r2 @ Find EOS
|
|
Packit |
6c4009 |
uqsub8 r3, ip, r3
|
|
Packit |
6c4009 |
pld [r0, #128] @ Prefetch 2 lines ahead
|
|
Packit |
6c4009 |
orrs r3, r3, r2 @ Combine the two words
|
|
Packit |
6c4009 |
it eq
|
|
Packit |
6c4009 |
ldrdeq r2, r3, [r0], #8
|
|
Packit |
6c4009 |
beq 2b
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Found something. Disambiguate between first and second words.
|
|
Packit |
6c4009 |
@ Adjust r0 to point to the word containing the match.
|
|
Packit |
6c4009 |
@ Adjust r2 to the found bits for the word containing the match.
|
|
Packit |
6c4009 |
cmp r2, #0
|
|
Packit |
6c4009 |
sub r0, r0, #4
|
|
Packit |
6c4009 |
ite eq
|
|
Packit |
6c4009 |
moveq r2, r3
|
|
Packit |
6c4009 |
subne r0, r0, #4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Find the bit-offset of the match within the word. Note that the
|
|
Packit |
6c4009 |
@ bit result from clz will be 7 higher than "true", but we'll
|
|
Packit |
6c4009 |
@ immediately discard those bits converting to a byte offset.
|
|
Packit |
6c4009 |
#ifdef __ARMEL__
|
|
Packit |
6c4009 |
rev r2, r2 @ For LE, count from the little end
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
clz r2, r2
|
|
Packit |
6c4009 |
add r0, r0, r2, lsr #3 @ Adjust the pointer to the found byte
|
|
Packit |
6c4009 |
99:
|
|
Packit |
6c4009 |
sub r0, r0, r1 @ Subtract input to compute length
|
|
Packit |
6c4009 |
bx lr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
END (strlen)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
libc_hidden_builtin_def (strlen)
|