|
Packit |
6c4009 |
/* rawmemchr -- find a byte within an unsized memory block.
|
|
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 (__rawmemchr)
|
|
Packit |
6c4009 |
@ r0 = start of string
|
|
Packit |
6c4009 |
@ r1 = character to match
|
|
Packit |
6c4009 |
@ returns a pointer to the match, which must be present.
|
|
Packit |
6c4009 |
ldrb r2, [r0] @ load 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 |
uxtb r1, r1
|
|
Packit |
6c4009 |
rsb r3, r3, #15 @ 16 - 1 peeled loop iteration
|
|
Packit |
6c4009 |
cmp r2, r1
|
|
Packit |
6c4009 |
it eq
|
|
Packit |
6c4009 |
bxeq lr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Loop until we find ...
|
|
Packit |
6c4009 |
1: ldrb r2, [r0, #1]!
|
|
Packit |
6c4009 |
subs r3, r3, #1 @ ... the alignment point
|
|
Packit |
6c4009 |
it ne
|
|
Packit |
6c4009 |
cmpne r2, r1 @ ... or C
|
|
Packit |
6c4009 |
bne 1b
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Disambiguate the exit possibilites above
|
|
Packit |
6c4009 |
cmp r2, r1 @ Found C
|
|
Packit |
6c4009 |
it eq
|
|
Packit |
6c4009 |
bxeq lr
|
|
Packit |
6c4009 |
add r0, r0, #1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ So now we're aligned.
|
|
Packit |
6c4009 |
ldrd r2, r3, [r0], #8
|
|
Packit |
6c4009 |
orr r1, r1, r1, lsl #8 @ Replicate C to all bytes
|
|
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 |
orr r1, r1, r1, lsl #16
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
@ Loop searching for C, 8 bytes at a time.
|
|
Packit |
6c4009 |
@ Subtracting (unsigned saturating) from 1 means result of 1 for
|
|
Packit |
6c4009 |
@ any byte that was originally zero and 0 otherwise. Therefore
|
|
Packit |
6c4009 |
@ we consider the lsb of each byte the "found" bit.
|
|
Packit |
6c4009 |
2: eor r2, r2, r1 @ Convert C bytes to 0
|
|
Packit |
6c4009 |
eor r3, r3, r1
|
|
Packit |
6c4009 |
uqsub8 r2, ip, r2 @ Find C
|
|
Packit |
6c4009 |
uqsub8 r3, ip, r3
|
|
Packit |
6c4009 |
pld [r0, #128]
|
|
Packit |
6c4009 |
orrs r3, r3, r2 @ Test both words for found
|
|
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 |
bx lr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
END (__rawmemchr)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
weak_alias (__rawmemchr, rawmemchr)
|
|
Packit |
6c4009 |
libc_hidden_def (__rawmemchr)
|