2018-08-15 17:31:33 +00:00
|
|
|
/* Strlen implementation that uses ASIMD instructions for load and NULL checks.
|
2020-01-01 00:14:33 +00:00
|
|
|
Copyright (C) 2018-2020 Free Software Foundation, Inc.
|
2018-08-15 17:31:33 +00:00
|
|
|
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library. If not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-08-15 17:31:33 +00:00
|
|
|
|
|
|
|
#include <sysdep.h>
|
|
|
|
|
|
|
|
/* Assumptions:
|
|
|
|
|
|
|
|
ARMv8-a, AArch64, ASIMD, unaligned accesses, min page size 4k. */
|
|
|
|
|
|
|
|
/* To test the page crossing code path more thoroughly, compile with
|
|
|
|
-DTEST_PAGE_CROSS - this will force all calls through the slower
|
|
|
|
entry path. This option is not intended for production use. */
|
|
|
|
|
|
|
|
/* Arguments and results. */
|
|
|
|
#define srcin x0
|
|
|
|
#define len x0
|
|
|
|
|
|
|
|
/* Locals and temporaries. */
|
|
|
|
#define src x1
|
|
|
|
#define data1 x2
|
|
|
|
#define data2 x3
|
|
|
|
#define has_nul1 x4
|
|
|
|
#define has_nul2 x5
|
|
|
|
#define tmp1 x4
|
|
|
|
#define tmp2 x5
|
|
|
|
#define tmp3 x6
|
|
|
|
#define tmp4 x7
|
|
|
|
#define zeroones x8
|
|
|
|
#define dataq q2
|
|
|
|
#define datav v2
|
|
|
|
#define datab2 b3
|
|
|
|
#define dataq2 q3
|
|
|
|
#define datav2 v3
|
|
|
|
|
2019-12-19 13:41:40 +00:00
|
|
|
#define REP8_01 0x0101010101010101
|
|
|
|
#define REP8_7f 0x7f7f7f7f7f7f7f7f
|
|
|
|
|
2018-08-15 17:31:33 +00:00
|
|
|
#ifdef TEST_PAGE_CROSS
|
|
|
|
# define MIN_PAGE_SIZE 16
|
|
|
|
#else
|
|
|
|
# define MIN_PAGE_SIZE 4096
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Since strings are short on average, we check the first 16 bytes
|
|
|
|
of the string for a NUL character. In order to do an unaligned load
|
|
|
|
safely we have to do a page cross check first. If there is a NUL
|
|
|
|
byte we calculate the length from the 2 8-byte words using
|
|
|
|
conditional select to reduce branch mispredictions (it is unlikely
|
|
|
|
strlen_asimd will be repeatedly called on strings with the same
|
|
|
|
length).
|
|
|
|
|
|
|
|
If the string is longer than 16 bytes, we align src so don't need
|
|
|
|
further page cross checks, and process 16 bytes per iteration.
|
|
|
|
|
|
|
|
If the page cross check fails, we read 16 bytes from an aligned
|
|
|
|
address, remove any characters before the string, and continue
|
|
|
|
in the main loop using aligned loads. Since strings crossing a
|
|
|
|
page in the first 16 bytes are rare (probability of
|
|
|
|
16/MIN_PAGE_SIZE ~= 0.4%), this case does not need to be optimized.
|
|
|
|
|
|
|
|
AArch64 systems have a minimum page size of 4k. We don't bother
|
|
|
|
checking for larger page sizes - the cost of setting up the correct
|
|
|
|
page size is just not worth the extra gain from a small reduction in
|
|
|
|
the cases taking the slow path. Note that we only care about
|
|
|
|
whether the first fetch, which may be misaligned, crosses a page
|
|
|
|
boundary. */
|
|
|
|
|
|
|
|
ENTRY_ALIGN (__strlen_asimd, 6)
|
|
|
|
DELOUSE (0)
|
|
|
|
DELOUSE (1)
|
|
|
|
and tmp1, srcin, MIN_PAGE_SIZE - 1
|
2019-12-19 13:41:40 +00:00
|
|
|
mov zeroones, REP8_01
|
2018-08-15 17:31:33 +00:00
|
|
|
cmp tmp1, MIN_PAGE_SIZE - 16
|
|
|
|
b.gt L(page_cross)
|
2019-12-19 13:41:40 +00:00
|
|
|
ldp data1, data2, [srcin]
|
2018-08-15 17:31:33 +00:00
|
|
|
#ifdef __AARCH64EB__
|
2019-12-19 13:41:40 +00:00
|
|
|
rev data1, data1
|
|
|
|
rev data2, data2
|
2018-08-15 17:31:33 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-19 13:41:40 +00:00
|
|
|
sub tmp1, data1, zeroones
|
|
|
|
orr tmp2, data1, REP8_7f
|
|
|
|
sub tmp3, data2, zeroones
|
|
|
|
orr tmp4, data2, REP8_7f
|
|
|
|
bics has_nul1, tmp1, tmp2
|
|
|
|
bic has_nul2, tmp3, tmp4
|
|
|
|
ccmp has_nul2, 0, 0, eq
|
|
|
|
beq L(main_loop_entry)
|
|
|
|
csel has_nul1, has_nul1, has_nul2, cc
|
2018-08-15 17:31:33 +00:00
|
|
|
mov len, 8
|
2019-12-19 13:41:40 +00:00
|
|
|
rev has_nul1, has_nul1
|
|
|
|
clz tmp1, has_nul1
|
|
|
|
csel len, xzr, len, cc
|
2018-08-15 17:31:33 +00:00
|
|
|
add len, len, tmp1, lsr 3
|
|
|
|
ret
|
|
|
|
|
|
|
|
L(main_loop_entry):
|
|
|
|
bic src, srcin, 15
|
2019-12-19 13:41:40 +00:00
|
|
|
sub src, src, 16
|
2018-08-15 17:31:33 +00:00
|
|
|
|
|
|
|
L(main_loop):
|
2019-12-19 13:41:40 +00:00
|
|
|
ldr dataq, [src, 32]!
|
2018-08-15 17:31:33 +00:00
|
|
|
L(page_cross_entry):
|
|
|
|
/* Get the minimum value and keep going if it is not zero. */
|
|
|
|
uminv datab2, datav.16b
|
|
|
|
mov tmp1, datav2.d[0]
|
2019-12-19 13:41:40 +00:00
|
|
|
cbz tmp1, L(tail)
|
|
|
|
ldr dataq, [src, 16]
|
|
|
|
uminv datab2, datav.16b
|
|
|
|
mov tmp1, datav2.d[0]
|
2018-08-15 17:31:33 +00:00
|
|
|
cbnz tmp1, L(main_loop)
|
2019-12-19 13:41:40 +00:00
|
|
|
add src, src, 16
|
2018-08-15 17:31:33 +00:00
|
|
|
|
|
|
|
L(tail):
|
|
|
|
#ifdef __AARCH64EB__
|
|
|
|
rev64 datav.16b, datav.16b
|
|
|
|
#endif
|
|
|
|
/* Set te NULL byte as 0xff and the rest as 0x00, move the data into a
|
|
|
|
pair of scalars and then compute the length from the earliest NULL
|
|
|
|
byte. */
|
|
|
|
cmeq datav.16b, datav.16b, #0
|
|
|
|
mov data1, datav.d[0]
|
|
|
|
mov data2, datav.d[1]
|
|
|
|
cmp data1, 0
|
|
|
|
csel data1, data1, data2, ne
|
|
|
|
sub len, src, srcin
|
|
|
|
rev data1, data1
|
|
|
|
add tmp2, len, 8
|
|
|
|
clz tmp1, data1
|
|
|
|
csel len, len, tmp2, ne
|
|
|
|
add len, len, tmp1, lsr 3
|
|
|
|
ret
|
|
|
|
|
|
|
|
/* Load 16 bytes from [srcin & ~15] and force the bytes that precede
|
|
|
|
srcin to 0xff, so we ignore any NUL bytes before the string.
|
|
|
|
Then continue in the aligned loop. */
|
|
|
|
L(page_cross):
|
|
|
|
mov tmp3, 63
|
|
|
|
bic src, srcin, 15
|
|
|
|
and tmp1, srcin, 7
|
|
|
|
ands tmp2, srcin, 8
|
|
|
|
ldr dataq, [src]
|
|
|
|
lsl tmp1, tmp1, 3
|
|
|
|
csel tmp2, tmp2, tmp1, eq
|
|
|
|
csel tmp1, tmp1, tmp3, eq
|
|
|
|
mov tmp4, -1
|
|
|
|
#ifdef __AARCH64EB__
|
|
|
|
/* Big-endian. Early bytes are at MSB. */
|
|
|
|
lsr tmp1, tmp4, tmp1
|
|
|
|
lsr tmp2, tmp4, tmp2
|
|
|
|
#else
|
|
|
|
/* Little-endian. Early bytes are at LSB. */
|
|
|
|
lsl tmp1, tmp4, tmp1
|
|
|
|
lsl tmp2, tmp4, tmp2
|
|
|
|
#endif
|
|
|
|
mov datav2.d[0], tmp1
|
|
|
|
mov datav2.d[1], tmp2
|
|
|
|
orn datav.16b, datav.16b, datav2.16b
|
|
|
|
b L(page_cross_entry)
|
|
|
|
END (__strlen_asimd)
|
|
|
|
weak_alias (__strlen_asimd, strlen_asimd)
|
|
|
|
libc_hidden_builtin_def (strlen_asimd)
|