2017-06-09 12:45:43 +00:00
|
|
|
/* strrchr/wcsrchr optimized with AVX2.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2017-2021 Free Software Foundation, Inc.
|
2017-06-09 12:45:43 +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/>. */
|
2017-06-09 12:45:43 +00:00
|
|
|
|
|
|
|
#if IS_IN (libc)
|
|
|
|
|
|
|
|
# include <sysdep.h>
|
|
|
|
|
|
|
|
# ifndef STRRCHR
|
|
|
|
# define STRRCHR __strrchr_avx2
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
# define VPBROADCAST vpbroadcastd
|
|
|
|
# define VPCMPEQ vpcmpeqd
|
2022-04-22 01:52:29 +00:00
|
|
|
# define VPMIN vpminud
|
|
|
|
# define CHAR_SIZE 4
|
2017-06-09 12:45:43 +00:00
|
|
|
# else
|
|
|
|
# define VPBROADCAST vpbroadcastb
|
|
|
|
# define VPCMPEQ vpcmpeqb
|
2022-04-22 01:52:29 +00:00
|
|
|
# define VPMIN vpminub
|
|
|
|
# define CHAR_SIZE 1
|
2017-06-09 12:45:43 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef VZEROUPPER
|
|
|
|
# define VZEROUPPER vzeroupper
|
|
|
|
# endif
|
|
|
|
|
2021-03-05 15:26:42 +00:00
|
|
|
# ifndef SECTION
|
|
|
|
# define SECTION(p) p##.avx
|
|
|
|
# endif
|
|
|
|
|
2017-06-09 12:45:43 +00:00
|
|
|
# define VEC_SIZE 32
|
2022-04-22 01:52:29 +00:00
|
|
|
# define PAGE_SIZE 4096
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.section SECTION(.text), "ax", @progbits
|
|
|
|
ENTRY(STRRCHR)
|
|
|
|
movd %esi, %xmm7
|
|
|
|
movl %edi, %eax
|
2017-06-09 12:45:43 +00:00
|
|
|
/* Broadcast CHAR to YMM4. */
|
2022-04-22 01:52:29 +00:00
|
|
|
VPBROADCAST %xmm7, %ymm7
|
2020-06-11 19:41:18 +00:00
|
|
|
vpxor %xmm0, %xmm0, %xmm0
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Shift here instead of `andl` to save code size (saves a fetch
|
|
|
|
block). */
|
|
|
|
sall $20, %eax
|
|
|
|
cmpl $((PAGE_SIZE - VEC_SIZE) << 20), %eax
|
|
|
|
ja L(cross_page)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
L(page_cross_continue):
|
2017-06-09 12:45:43 +00:00
|
|
|
vmovdqu (%rdi), %ymm1
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Check end of string match. */
|
|
|
|
VPCMPEQ %ymm1, %ymm0, %ymm6
|
|
|
|
vpmovmskb %ymm6, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jz L(aligned_more)
|
|
|
|
|
|
|
|
/* Only check match with search CHAR if needed. */
|
|
|
|
VPCMPEQ %ymm1, %ymm7, %ymm1
|
|
|
|
vpmovmskb %ymm1, %eax
|
|
|
|
/* Check if match before first zero. */
|
|
|
|
blsmskl %ecx, %ecx
|
|
|
|
andl %ecx, %eax
|
|
|
|
jz L(ret0)
|
|
|
|
bsrl %eax, %eax
|
|
|
|
addq %rdi, %rax
|
|
|
|
/* We are off by 3 for wcsrchr if search CHAR is non-zero. If
|
|
|
|
search CHAR is zero we are correct. Either way `andq
|
|
|
|
-CHAR_SIZE, %rax` gets the correct result. */
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
L(ret0):
|
|
|
|
L(return_vzeroupper):
|
|
|
|
ZERO_UPPER_VEC_REGISTERS_RETURN
|
|
|
|
|
|
|
|
/* Returns for first vec x1/x2 have hard coded backward search
|
|
|
|
path for earlier matches. */
|
|
|
|
.p2align 4,, 10
|
|
|
|
L(first_vec_x1):
|
|
|
|
VPCMPEQ %ymm2, %ymm7, %ymm6
|
|
|
|
vpmovmskb %ymm6, %eax
|
|
|
|
blsmskl %ecx, %ecx
|
|
|
|
andl %ecx, %eax
|
|
|
|
jnz L(first_vec_x1_return)
|
|
|
|
|
|
|
|
.p2align 4,, 4
|
|
|
|
L(first_vec_x0_test):
|
|
|
|
VPCMPEQ %ymm1, %ymm7, %ymm6
|
|
|
|
vpmovmskb %ymm6, %eax
|
|
|
|
testl %eax, %eax
|
|
|
|
jz L(ret1)
|
|
|
|
bsrl %eax, %eax
|
|
|
|
addq %r8, %rax
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
L(ret1):
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.p2align 4,, 10
|
|
|
|
L(first_vec_x0_x1_test):
|
|
|
|
VPCMPEQ %ymm2, %ymm7, %ymm6
|
|
|
|
vpmovmskb %ymm6, %eax
|
|
|
|
/* Check ymm2 for search CHAR match. If no match then check ymm1
|
|
|
|
before returning. */
|
2017-06-09 12:45:43 +00:00
|
|
|
testl %eax, %eax
|
2022-04-22 01:52:29 +00:00
|
|
|
jz L(first_vec_x0_test)
|
|
|
|
.p2align 4,, 4
|
|
|
|
L(first_vec_x1_return):
|
|
|
|
bsrl %eax, %eax
|
|
|
|
leaq 1(%rdi, %rax), %rax
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.p2align 4,, 10
|
|
|
|
L(first_vec_x2):
|
|
|
|
VPCMPEQ %ymm3, %ymm7, %ymm6
|
|
|
|
vpmovmskb %ymm6, %eax
|
|
|
|
blsmskl %ecx, %ecx
|
|
|
|
/* If no in-range search CHAR match in ymm3 then need to check
|
|
|
|
ymm1/ymm2 for an earlier match (we delay checking search
|
|
|
|
CHAR matches until needed). */
|
|
|
|
andl %ecx, %eax
|
|
|
|
jz L(first_vec_x0_x1_test)
|
|
|
|
bsrl %eax, %eax
|
|
|
|
leaq (VEC_SIZE + 1)(%rdi, %rax), %rax
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
VZEROUPPER_RETURN
|
|
|
|
|
2017-06-09 12:45:43 +00:00
|
|
|
|
|
|
|
.p2align 4
|
2022-04-22 01:52:29 +00:00
|
|
|
L(aligned_more):
|
|
|
|
/* Save original pointer if match was in VEC 0. */
|
|
|
|
movq %rdi, %r8
|
|
|
|
|
|
|
|
/* Align src. */
|
|
|
|
orq $(VEC_SIZE - 1), %rdi
|
|
|
|
vmovdqu 1(%rdi), %ymm2
|
|
|
|
VPCMPEQ %ymm2, %ymm0, %ymm6
|
|
|
|
vpmovmskb %ymm6, %ecx
|
2017-06-09 12:45:43 +00:00
|
|
|
testl %ecx, %ecx
|
2022-04-22 01:52:29 +00:00
|
|
|
jnz L(first_vec_x1)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
vmovdqu (VEC_SIZE + 1)(%rdi), %ymm3
|
|
|
|
VPCMPEQ %ymm3, %ymm0, %ymm6
|
|
|
|
vpmovmskb %ymm6, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(first_vec_x2)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Save pointer again before realigning. */
|
|
|
|
movq %rdi, %rsi
|
|
|
|
addq $(VEC_SIZE + 1), %rdi
|
|
|
|
andq $-(VEC_SIZE * 2), %rdi
|
2017-06-09 12:45:43 +00:00
|
|
|
.p2align 4
|
2022-04-22 01:52:29 +00:00
|
|
|
L(first_aligned_loop):
|
|
|
|
/* Do 2x VEC at a time. Any more and the cost of finding the
|
|
|
|
match outweights loop benefit. */
|
|
|
|
vmovdqa (VEC_SIZE * 0)(%rdi), %ymm4
|
|
|
|
vmovdqa (VEC_SIZE * 1)(%rdi), %ymm5
|
|
|
|
|
|
|
|
VPCMPEQ %ymm4, %ymm7, %ymm6
|
|
|
|
VPMIN %ymm4, %ymm5, %ymm8
|
|
|
|
VPCMPEQ %ymm5, %ymm7, %ymm10
|
|
|
|
vpor %ymm6, %ymm10, %ymm5
|
|
|
|
VPCMPEQ %ymm8, %ymm0, %ymm8
|
|
|
|
vpor %ymm5, %ymm8, %ymm9
|
|
|
|
|
|
|
|
vpmovmskb %ymm9, %eax
|
|
|
|
addq $(VEC_SIZE * 2), %rdi
|
|
|
|
/* No zero or search CHAR. */
|
2017-06-09 12:45:43 +00:00
|
|
|
testl %eax, %eax
|
2022-04-22 01:52:29 +00:00
|
|
|
jz L(first_aligned_loop)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* If no zero CHAR then go to second loop (this allows us to
|
|
|
|
throw away all prior work). */
|
|
|
|
vpmovmskb %ymm8, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jz L(second_aligned_loop_prep)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Search char could be zero so we need to get the true match.
|
|
|
|
*/
|
|
|
|
vpmovmskb %ymm5, %eax
|
|
|
|
testl %eax, %eax
|
|
|
|
jnz L(first_aligned_loop_return)
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.p2align 4,, 4
|
|
|
|
L(first_vec_x1_or_x2):
|
|
|
|
VPCMPEQ %ymm3, %ymm7, %ymm3
|
|
|
|
VPCMPEQ %ymm2, %ymm7, %ymm2
|
2017-06-09 12:45:43 +00:00
|
|
|
vpmovmskb %ymm3, %eax
|
2022-04-22 01:52:29 +00:00
|
|
|
vpmovmskb %ymm2, %edx
|
|
|
|
/* Use add for macro-fusion. */
|
|
|
|
addq %rax, %rdx
|
|
|
|
jz L(first_vec_x0_test)
|
|
|
|
/* NB: We could move this shift to before the branch and save a
|
|
|
|
bit of code size / performance on the fall through. The
|
|
|
|
branch leads to the null case which generally seems hotter
|
|
|
|
than char in first 3x VEC. */
|
|
|
|
salq $32, %rax
|
|
|
|
addq %rdx, %rax
|
|
|
|
bsrq %rax, %rax
|
|
|
|
leaq 1(%rsi, %rax), %rax
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.p2align 4,, 8
|
|
|
|
L(first_aligned_loop_return):
|
|
|
|
VPCMPEQ %ymm4, %ymm0, %ymm4
|
|
|
|
vpmovmskb %ymm4, %edx
|
|
|
|
salq $32, %rcx
|
|
|
|
orq %rdx, %rcx
|
|
|
|
|
|
|
|
vpmovmskb %ymm10, %eax
|
|
|
|
vpmovmskb %ymm6, %edx
|
|
|
|
salq $32, %rax
|
|
|
|
orq %rdx, %rax
|
|
|
|
blsmskq %rcx, %rcx
|
|
|
|
andq %rcx, %rax
|
|
|
|
jz L(first_vec_x1_or_x2)
|
|
|
|
|
|
|
|
bsrq %rax, %rax
|
|
|
|
leaq -(VEC_SIZE * 2)(%rdi, %rax), %rax
|
2017-06-09 12:45:43 +00:00
|
|
|
# ifdef USE_AS_WCSRCHR
|
2022-04-22 01:52:29 +00:00
|
|
|
andq $-CHAR_SIZE, %rax
|
2017-06-09 12:45:43 +00:00
|
|
|
# endif
|
2022-04-22 01:52:29 +00:00
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Search char cannot be zero. */
|
2017-06-09 12:45:43 +00:00
|
|
|
.p2align 4
|
2022-04-22 01:52:29 +00:00
|
|
|
L(second_aligned_loop_set_furthest_match):
|
|
|
|
/* Save VEC and pointer from most recent match. */
|
|
|
|
L(second_aligned_loop_prep):
|
2017-06-09 12:45:43 +00:00
|
|
|
movq %rdi, %rsi
|
2022-04-22 01:52:29 +00:00
|
|
|
vmovdqu %ymm6, %ymm2
|
|
|
|
vmovdqu %ymm10, %ymm3
|
2017-06-09 12:45:43 +00:00
|
|
|
|
|
|
|
.p2align 4
|
2022-04-22 01:52:29 +00:00
|
|
|
L(second_aligned_loop):
|
|
|
|
/* Search 2x at at time. */
|
|
|
|
vmovdqa (VEC_SIZE * 0)(%rdi), %ymm4
|
|
|
|
vmovdqa (VEC_SIZE * 1)(%rdi), %ymm5
|
|
|
|
|
|
|
|
VPCMPEQ %ymm4, %ymm7, %ymm6
|
|
|
|
VPMIN %ymm4, %ymm5, %ymm1
|
|
|
|
VPCMPEQ %ymm5, %ymm7, %ymm10
|
|
|
|
vpor %ymm6, %ymm10, %ymm5
|
|
|
|
VPCMPEQ %ymm1, %ymm0, %ymm1
|
|
|
|
vpor %ymm5, %ymm1, %ymm9
|
|
|
|
|
|
|
|
vpmovmskb %ymm9, %eax
|
|
|
|
addq $(VEC_SIZE * 2), %rdi
|
2017-06-09 12:45:43 +00:00
|
|
|
testl %eax, %eax
|
2022-04-22 01:52:29 +00:00
|
|
|
jz L(second_aligned_loop)
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jz L(second_aligned_loop_set_furthest_match)
|
|
|
|
vpmovmskb %ymm5, %eax
|
2017-06-09 12:45:43 +00:00
|
|
|
testl %eax, %eax
|
2022-04-22 01:52:29 +00:00
|
|
|
jnz L(return_new_match)
|
|
|
|
|
|
|
|
/* This is the hot patch. We know CHAR is inbounds and that
|
|
|
|
ymm3/ymm2 have latest match. */
|
|
|
|
.p2align 4,, 4
|
|
|
|
L(return_old_match):
|
|
|
|
vpmovmskb %ymm3, %eax
|
|
|
|
vpmovmskb %ymm2, %edx
|
|
|
|
salq $32, %rax
|
|
|
|
orq %rdx, %rax
|
|
|
|
bsrq %rax, %rax
|
|
|
|
/* Search char cannot be zero so safe to just use lea for
|
|
|
|
wcsrchr. */
|
|
|
|
leaq (VEC_SIZE * -2 -(CHAR_SIZE - 1))(%rsi, %rax), %rax
|
2021-03-05 15:26:42 +00:00
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
/* Last iteration also potentially has a match. */
|
|
|
|
.p2align 4,, 8
|
|
|
|
L(return_new_match):
|
|
|
|
VPCMPEQ %ymm4, %ymm0, %ymm4
|
|
|
|
vpmovmskb %ymm4, %edx
|
|
|
|
salq $32, %rcx
|
|
|
|
orq %rdx, %rcx
|
|
|
|
|
|
|
|
vpmovmskb %ymm10, %eax
|
|
|
|
vpmovmskb %ymm6, %edx
|
|
|
|
salq $32, %rax
|
|
|
|
orq %rdx, %rax
|
|
|
|
blsmskq %rcx, %rcx
|
|
|
|
andq %rcx, %rax
|
|
|
|
jz L(return_old_match)
|
|
|
|
bsrq %rax, %rax
|
|
|
|
/* Search char cannot be zero so safe to just use lea for
|
|
|
|
wcsrchr. */
|
|
|
|
leaq (VEC_SIZE * -2 -(CHAR_SIZE - 1))(%rdi, %rax), %rax
|
2021-03-05 15:26:42 +00:00
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:45:43 +00:00
|
|
|
|
2022-04-22 01:52:29 +00:00
|
|
|
.p2align 4,, 4
|
|
|
|
L(cross_page):
|
|
|
|
movq %rdi, %rsi
|
|
|
|
andq $-VEC_SIZE, %rsi
|
|
|
|
vmovdqu (%rsi), %ymm1
|
|
|
|
VPCMPEQ %ymm1, %ymm0, %ymm6
|
|
|
|
vpmovmskb %ymm6, %ecx
|
|
|
|
/* Shift out zero CHAR matches that are before the begining of
|
|
|
|
src (rdi). */
|
|
|
|
shrxl %edi, %ecx, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jz L(page_cross_continue)
|
|
|
|
VPCMPEQ %ymm1, %ymm7, %ymm1
|
|
|
|
vpmovmskb %ymm1, %eax
|
|
|
|
|
|
|
|
/* Shift out search CHAR matches that are before the begining of
|
|
|
|
src (rdi). */
|
|
|
|
shrxl %edi, %eax, %eax
|
|
|
|
blsmskl %ecx, %ecx
|
|
|
|
/* Check if any search CHAR match in range. */
|
|
|
|
andl %ecx, %eax
|
|
|
|
jz L(ret2)
|
|
|
|
bsrl %eax, %eax
|
|
|
|
addq %rdi, %rax
|
|
|
|
# ifdef USE_AS_WCSRCHR
|
|
|
|
andq $-CHAR_SIZE, %rax
|
|
|
|
# endif
|
|
|
|
L(ret2):
|
|
|
|
VZEROUPPER_RETURN
|
|
|
|
END(STRRCHR)
|
2017-06-09 12:45:43 +00:00
|
|
|
#endif
|