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