2017-06-09 12:44:30 +00:00
|
|
|
/* memrchr optimized with AVX2.
|
2022-01-01 18:54:23 +00:00
|
|
|
Copyright (C) 2017-2022 Free Software Foundation, Inc.
|
2017-06-09 12:44:30 +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:44:30 +00:00
|
|
|
|
2022-07-13 23:32:59 +00:00
|
|
|
#include <isa-level.h>
|
|
|
|
|
|
|
|
#if ISA_SHOULD_BUILD (3)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
# include <sysdep.h>
|
|
|
|
|
2021-03-05 15:26:42 +00:00
|
|
|
# ifndef MEMRCHR
|
2022-06-07 04:11:32 +00:00
|
|
|
# define MEMRCHR __memrchr_avx2
|
2021-03-05 15:26:42 +00:00
|
|
|
# endif
|
|
|
|
|
2017-06-09 12:44:30 +00:00
|
|
|
# ifndef VZEROUPPER
|
2022-06-07 04:11:32 +00:00
|
|
|
# define VZEROUPPER vzeroupper
|
2017-06-09 12:44:30 +00:00
|
|
|
# endif
|
|
|
|
|
2021-03-05 15:26:42 +00:00
|
|
|
# ifndef SECTION
|
|
|
|
# define SECTION(p) p##.avx
|
|
|
|
# endif
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
# define VEC_SIZE 32
|
|
|
|
# define PAGE_SIZE 4096
|
|
|
|
.section SECTION(.text), "ax", @progbits
|
2022-06-24 16:42:12 +00:00
|
|
|
ENTRY_P2ALIGN(MEMRCHR, 6)
|
2022-06-07 04:11:32 +00:00
|
|
|
# ifdef __ILP32__
|
|
|
|
/* Clear upper bits. */
|
|
|
|
and %RDX_LP, %RDX_LP
|
|
|
|
# else
|
|
|
|
test %RDX_LP, %RDX_LP
|
|
|
|
# endif
|
|
|
|
jz L(zero_0)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
vmovd %esi, %xmm0
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Get end pointer. Minus one for two reasons. 1) It is necessary for a
|
|
|
|
correct page cross check and 2) it correctly sets up end ptr to be
|
|
|
|
subtract by lzcnt aligned. */
|
|
|
|
leaq -1(%rdx, %rdi), %rax
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
vpbroadcastb %xmm0, %ymm0
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Check if we can load 1x VEC without cross a page. */
|
|
|
|
testl $(PAGE_SIZE - VEC_SIZE), %eax
|
|
|
|
jz L(page_cross)
|
|
|
|
|
|
|
|
vpcmpeqb -(VEC_SIZE - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
cmpq $VEC_SIZE, %rdx
|
|
|
|
ja L(more_1x_vec)
|
|
|
|
|
|
|
|
L(ret_vec_x0_test):
|
|
|
|
/* If ecx is zero (no matches) lzcnt will set it 32 (VEC_SIZE) which
|
|
|
|
will gurantee edx (len) is less than it. */
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
|
|
|
|
/* Hoist vzeroupper (not great for RTM) to save code size. This allows
|
|
|
|
all logic for edx (len) <= VEC_SIZE to fit in first cache line. */
|
|
|
|
COND_VZEROUPPER
|
|
|
|
cmpl %ecx, %edx
|
|
|
|
jle L(zero_0)
|
|
|
|
subq %rcx, %rax
|
|
|
|
ret
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Fits in aligning bytes of first cache line. */
|
|
|
|
L(zero_0):
|
|
|
|
xorl %eax, %eax
|
|
|
|
ret
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 9
|
|
|
|
L(ret_vec_x0):
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
subq %rcx, %rax
|
2021-03-05 15:26:42 +00:00
|
|
|
L(return_vzeroupper):
|
|
|
|
ZERO_UPPER_VEC_REGISTERS_RETURN
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 10
|
|
|
|
L(more_1x_vec):
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x0)
|
|
|
|
|
|
|
|
/* Align rax (string pointer). */
|
|
|
|
andq $-VEC_SIZE, %rax
|
|
|
|
|
|
|
|
/* Recompute remaining length after aligning. */
|
|
|
|
movq %rax, %rdx
|
|
|
|
/* Need this comparison next no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE)(%rax), %ymm0, %ymm1
|
|
|
|
subq %rdi, %rdx
|
|
|
|
decq %rax
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
/* Fall through for short (hotter than length). */
|
|
|
|
cmpq $(VEC_SIZE * 2), %rdx
|
|
|
|
ja L(more_2x_vec)
|
2017-06-09 12:44:30 +00:00
|
|
|
L(last_2x_vec):
|
|
|
|
cmpl $VEC_SIZE, %edx
|
2022-06-07 04:11:32 +00:00
|
|
|
jbe L(ret_vec_x0_test)
|
|
|
|
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x0)
|
|
|
|
|
|
|
|
vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
/* 64-bit lzcnt. This will naturally add 32 to position. */
|
|
|
|
lzcntq %rcx, %rcx
|
|
|
|
COND_VZEROUPPER
|
|
|
|
cmpl %ecx, %edx
|
|
|
|
jle L(zero_0)
|
|
|
|
subq %rcx, %rax
|
|
|
|
ret
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Inexpensive place to put this regarding code size / target alignments
|
|
|
|
/ ICache NLP. Necessary for 2-byte encoding of jump to page cross
|
|
|
|
case which in turn is necessary for hot path (len <= VEC_SIZE) to fit
|
|
|
|
in first cache line. */
|
|
|
|
L(page_cross):
|
|
|
|
movq %rax, %rsi
|
|
|
|
andq $-VEC_SIZE, %rsi
|
|
|
|
vpcmpeqb (%rsi), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
/* Shift out negative alignment (because we are starting from endptr and
|
|
|
|
working backwards). */
|
|
|
|
movl %eax, %r8d
|
|
|
|
/* notl because eax already has endptr - 1. (-x = ~(x - 1)). */
|
|
|
|
notl %r8d
|
|
|
|
shlxl %r8d, %ecx, %ecx
|
|
|
|
cmpq %rdi, %rsi
|
|
|
|
ja L(more_1x_vec)
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
COND_VZEROUPPER
|
|
|
|
cmpl %ecx, %edx
|
|
|
|
jle L(zero_0)
|
|
|
|
subq %rcx, %rax
|
|
|
|
ret
|
|
|
|
.p2align 4,, 11
|
|
|
|
L(ret_vec_x1):
|
|
|
|
/* This will naturally add 32 to position. */
|
|
|
|
lzcntq %rcx, %rcx
|
|
|
|
subq %rcx, %rax
|
2021-03-05 15:26:42 +00:00
|
|
|
VZEROUPPER_RETURN
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 10
|
|
|
|
L(more_2x_vec):
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x0)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x1)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Needed no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
subq $(VEC_SIZE * 4), %rdx
|
|
|
|
ja L(more_4x_vec)
|
|
|
|
|
|
|
|
cmpl $(VEC_SIZE * -1), %edx
|
|
|
|
jle L(ret_vec_x2_test)
|
|
|
|
|
|
|
|
L(last_vec):
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x2)
|
|
|
|
|
|
|
|
/* Needed no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
subq $(VEC_SIZE * 3), %rax
|
|
|
|
COND_VZEROUPPER
|
|
|
|
subq %rcx, %rax
|
|
|
|
cmpq %rax, %rdi
|
|
|
|
ja L(zero_2)
|
|
|
|
ret
|
2021-03-05 15:26:42 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* First in aligning bytes. */
|
|
|
|
L(zero_2):
|
2017-06-09 12:44:30 +00:00
|
|
|
xorl %eax, %eax
|
|
|
|
ret
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 4
|
|
|
|
L(ret_vec_x2_test):
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
subq $(VEC_SIZE * 2), %rax
|
|
|
|
COND_VZEROUPPER
|
|
|
|
subq %rcx, %rax
|
|
|
|
cmpq %rax, %rdi
|
|
|
|
ja L(zero_2)
|
|
|
|
ret
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 11
|
|
|
|
L(ret_vec_x2):
|
|
|
|
/* ecx must be non-zero. */
|
|
|
|
bsrl %ecx, %ecx
|
|
|
|
leaq (VEC_SIZE * -3 + 1)(%rcx, %rax), %rax
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 14
|
|
|
|
L(ret_vec_x3):
|
|
|
|
/* ecx must be non-zero. */
|
|
|
|
bsrl %ecx, %ecx
|
|
|
|
leaq (VEC_SIZE * -4 + 1)(%rcx, %rax), %rax
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.p2align 4
|
2022-06-07 04:11:32 +00:00
|
|
|
L(more_4x_vec):
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x2)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x3)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Check if near end before re-aligning (otherwise might do an
|
|
|
|
unnecissary loop iteration). */
|
|
|
|
addq $-(VEC_SIZE * 4), %rax
|
|
|
|
cmpq $(VEC_SIZE * 4), %rdx
|
|
|
|
jbe L(last_4x_vec)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Align rax to (VEC_SIZE - 1). */
|
|
|
|
orq $(VEC_SIZE * 4 - 1), %rax
|
|
|
|
movq %rdi, %rdx
|
|
|
|
/* Get endptr for loop in rdx. NB: Can't just do while rax > rdi because
|
|
|
|
lengths that overflow can be valid and break the comparison. */
|
|
|
|
orq $(VEC_SIZE * 4 - 1), %rdx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4
|
|
|
|
L(loop_4x_vec):
|
|
|
|
/* Need this comparison next no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE * 1 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm2
|
|
|
|
vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm3
|
|
|
|
vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm4
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
vpor %ymm1, %ymm2, %ymm2
|
|
|
|
vpor %ymm3, %ymm4, %ymm4
|
|
|
|
vpor %ymm2, %ymm4, %ymm4
|
|
|
|
vpmovmskb %ymm4, %esi
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
testl %esi, %esi
|
|
|
|
jnz L(loop_end)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
addq $(VEC_SIZE * -4), %rax
|
|
|
|
cmpq %rdx, %rax
|
|
|
|
jne L(loop_4x_vec)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
subl %edi, %edx
|
|
|
|
incl %edx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
L(last_4x_vec):
|
|
|
|
/* Used no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE * 1 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
cmpl $(VEC_SIZE * 2), %edx
|
|
|
|
jbe L(last_2x_vec)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x0_end)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x1_end)
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
/* Used no matter what. */
|
|
|
|
vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm1
|
|
|
|
vpmovmskb %ymm1, %ecx
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
cmpl $(VEC_SIZE * 3), %edx
|
|
|
|
ja L(last_vec)
|
|
|
|
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
subq $(VEC_SIZE * 2), %rax
|
|
|
|
COND_VZEROUPPER
|
|
|
|
subq %rcx, %rax
|
|
|
|
cmpq %rax, %rdi
|
|
|
|
jbe L(ret0)
|
|
|
|
xorl %eax, %eax
|
|
|
|
L(ret0):
|
|
|
|
ret
|
2017-06-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4
|
|
|
|
L(loop_end):
|
|
|
|
vpmovmskb %ymm1, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x0_end)
|
|
|
|
|
|
|
|
vpmovmskb %ymm2, %ecx
|
|
|
|
testl %ecx, %ecx
|
|
|
|
jnz L(ret_vec_x1_end)
|
|
|
|
|
|
|
|
vpmovmskb %ymm3, %ecx
|
|
|
|
/* Combine last 2 VEC matches. If ecx (VEC3) is zero (no CHAR in VEC3)
|
|
|
|
then it won't affect the result in esi (VEC4). If ecx is non-zero
|
|
|
|
then CHAR in VEC3 and bsrq will use that position. */
|
|
|
|
salq $32, %rcx
|
|
|
|
orq %rsi, %rcx
|
|
|
|
bsrq %rcx, %rcx
|
|
|
|
leaq (VEC_SIZE * -4 + 1)(%rcx, %rax), %rax
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 4
|
|
|
|
L(ret_vec_x1_end):
|
|
|
|
/* 64-bit version will automatically add 32 (VEC_SIZE). */
|
|
|
|
lzcntq %rcx, %rcx
|
|
|
|
subq %rcx, %rax
|
|
|
|
VZEROUPPER_RETURN
|
2017-06-09 12:44:30 +00:00
|
|
|
|
2022-06-07 04:11:32 +00:00
|
|
|
.p2align 4,, 4
|
|
|
|
L(ret_vec_x0_end):
|
|
|
|
lzcntl %ecx, %ecx
|
|
|
|
subq %rcx, %rax
|
2021-03-05 15:26:42 +00:00
|
|
|
VZEROUPPER_RETURN
|
2022-06-07 04:11:32 +00:00
|
|
|
|
|
|
|
/* 2 bytes until next cache line. */
|
|
|
|
END(MEMRCHR)
|
2017-06-09 12:44:30 +00:00
|
|
|
#endif
|