|
Packit |
6c4009 |
/* memrchr optimized with AVX2.
|
|
Packit |
6c4009 |
Copyright (C) 2017-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 |
#if IS_IN (libc)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# ifndef VZEROUPPER
|
|
Packit |
6c4009 |
# define VZEROUPPER vzeroupper
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define VEC_SIZE 32
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.section .text.avx,"ax",@progbits
|
|
Packit |
6c4009 |
ENTRY (__memrchr_avx2)
|
|
Packit |
6c4009 |
/* Broadcast CHAR to YMM0. */
|
|
Packit |
6c4009 |
vmovd %esi, %xmm0
|
|
Packit |
6c4009 |
vpbroadcastb %xmm0, %ymm0
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
subq $VEC_SIZE, %rdx
|
|
Packit |
6c4009 |
jbe L(last_vec_or_less)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
addq %rdx, %rdi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check the last VEC_SIZE bytes. */
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 4), %rdi
|
|
Packit |
6c4009 |
movl %edi, %ecx
|
|
Packit |
6c4009 |
andl $(VEC_SIZE - 1), %ecx
|
|
Packit |
6c4009 |
jz L(aligned_more)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Align data for aligned loads in the loop. */
|
|
Packit |
6c4009 |
addq $VEC_SIZE, %rdi
|
|
Packit |
6c4009 |
addq $VEC_SIZE, %rdx
|
|
Packit |
6c4009 |
andq $-VEC_SIZE, %rdi
|
|
Packit |
6c4009 |
subq %rcx, %rdx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(aligned_more):
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 4), %rdx
|
|
Packit |
6c4009 |
jbe L(last_4x_vec_or_less)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check the last 4 * VEC_SIZE. Only one VEC_SIZE at a time
|
|
Packit |
6c4009 |
since data is only aligned to VEC_SIZE. */
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 3)(%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x3)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 2)(%rdi), %ymm0, %ymm2
|
|
Packit |
6c4009 |
vpmovmskb %ymm2, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x2)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb VEC_SIZE(%rdi), %ymm0, %ymm3
|
|
Packit |
6c4009 |
vpmovmskb %ymm3, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm4
|
|
Packit |
6c4009 |
vpmovmskb %ymm4, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Align data to 4 * VEC_SIZE for loop with fewer branches.
|
|
Packit |
6c4009 |
There are some overlaps with above if data isn't aligned
|
|
Packit |
6c4009 |
to 4 * VEC_SIZE. */
|
|
Packit |
6c4009 |
movl %edi, %ecx
|
|
Packit |
6c4009 |
andl $(VEC_SIZE * 4 - 1), %ecx
|
|
Packit |
6c4009 |
jz L(loop_4x_vec)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
addq $(VEC_SIZE * 4), %rdi
|
|
Packit |
6c4009 |
addq $(VEC_SIZE * 4), %rdx
|
|
Packit |
6c4009 |
andq $-(VEC_SIZE * 4), %rdi
|
|
Packit |
6c4009 |
subq %rcx, %rdx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(loop_4x_vec):
|
|
Packit |
6c4009 |
/* Compare 4 * VEC at a time forward. */
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 4), %rdi
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 4), %rdx
|
|
Packit |
6c4009 |
jbe L(last_4x_vec_or_less)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vmovdqa (%rdi), %ymm1
|
|
Packit |
6c4009 |
vmovdqa VEC_SIZE(%rdi), %ymm2
|
|
Packit |
6c4009 |
vmovdqa (VEC_SIZE * 2)(%rdi), %ymm3
|
|
Packit |
6c4009 |
vmovdqa (VEC_SIZE * 3)(%rdi), %ymm4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb %ymm1, %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpcmpeqb %ymm2, %ymm0, %ymm2
|
|
Packit |
6c4009 |
vpcmpeqb %ymm3, %ymm0, %ymm3
|
|
Packit |
6c4009 |
vpcmpeqb %ymm4, %ymm0, %ymm4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpor %ymm1, %ymm2, %ymm5
|
|
Packit |
6c4009 |
vpor %ymm3, %ymm4, %ymm6
|
|
Packit |
6c4009 |
vpor %ymm5, %ymm6, %ymm5
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm5, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(loop_4x_vec)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* There is a match. */
|
|
Packit |
6c4009 |
vpmovmskb %ymm4, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x3)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm3, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x2)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm2, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_4x_vec_or_less):
|
|
Packit |
6c4009 |
addl $(VEC_SIZE * 4), %edx
|
|
Packit |
6c4009 |
cmpl $(VEC_SIZE * 2), %edx
|
|
Packit |
6c4009 |
jbe L(last_2x_vec)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 3)(%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x3)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 2)(%rdi), %ymm0, %ymm2
|
|
Packit |
6c4009 |
vpmovmskb %ymm2, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x2)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb VEC_SIZE(%rdi), %ymm0, %ymm3
|
|
Packit |
6c4009 |
vpmovmskb %ymm3, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x1_check)
|
|
Packit |
6c4009 |
cmpl $(VEC_SIZE * 3), %edx
|
|
Packit |
6c4009 |
jbe L(zero)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm4
|
|
Packit |
6c4009 |
vpmovmskb %ymm4, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(zero)
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 4), %rdx
|
|
Packit |
6c4009 |
addq %rax, %rdx
|
|
Packit |
6c4009 |
jl L(zero)
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_2x_vec):
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 3)(%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x3_check)
|
|
Packit |
6c4009 |
cmpl $VEC_SIZE, %edx
|
|
Packit |
6c4009 |
jbe L(zero)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (VEC_SIZE * 2)(%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(zero)
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 2), %rdx
|
|
Packit |
6c4009 |
addq %rax, %rdx
|
|
Packit |
6c4009 |
jl L(zero)
|
|
Packit |
6c4009 |
addl $(VEC_SIZE * 2), %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x0):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x1):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addl $VEC_SIZE, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x2):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addl $(VEC_SIZE * 2), %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x3):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addl $(VEC_SIZE * 3), %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x1_check):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
subq $(VEC_SIZE * 3), %rdx
|
|
Packit |
6c4009 |
addq %rax, %rdx
|
|
Packit |
6c4009 |
jl L(zero)
|
|
Packit |
6c4009 |
addl $VEC_SIZE, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_x3_check):
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
subq $VEC_SIZE, %rdx
|
|
Packit |
6c4009 |
addq %rax, %rdx
|
|
Packit |
6c4009 |
jl L(zero)
|
|
Packit |
6c4009 |
addl $(VEC_SIZE * 3), %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(zero):
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
L(null):
|
|
Packit |
6c4009 |
xorl %eax, %eax
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_or_less_aligned):
|
|
Packit |
6c4009 |
movl %edx, %ecx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl $1, %edx
|
|
Packit |
6c4009 |
/* Support rdx << 32. */
|
|
Packit |
6c4009 |
salq %cl, %rdx
|
|
Packit |
6c4009 |
subq $1, %rdx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Remove the trailing bytes. */
|
|
Packit |
6c4009 |
andl %edx, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(zero)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_or_less):
|
|
Packit |
6c4009 |
addl $VEC_SIZE, %edx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check for zero length. */
|
|
Packit |
6c4009 |
testl %edx, %edx
|
|
Packit |
6c4009 |
jz L(null)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl %edi, %ecx
|
|
Packit |
6c4009 |
andl $(VEC_SIZE - 1), %ecx
|
|
Packit |
6c4009 |
jz L(last_vec_or_less_aligned)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl %ecx, %esi
|
|
Packit |
6c4009 |
movl %ecx, %r8d
|
|
Packit |
6c4009 |
addl %edx, %esi
|
|
Packit |
6c4009 |
andq $-VEC_SIZE, %rdi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
subl $VEC_SIZE, %esi
|
|
Packit |
6c4009 |
ja L(last_vec_2x_aligned)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check the last VEC. */
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Remove the leading and trailing bytes. */
|
|
Packit |
6c4009 |
sarl %cl, %eax
|
|
Packit |
6c4009 |
movl %edx, %ecx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl $1, %edx
|
|
Packit |
6c4009 |
sall %cl, %edx
|
|
Packit |
6c4009 |
subl $1, %edx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
andl %edx, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(zero)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
addq %r8, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.p2align 4
|
|
Packit |
6c4009 |
L(last_vec_2x_aligned):
|
|
Packit |
6c4009 |
movl %esi, %ecx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check the last VEC. */
|
|
Packit |
6c4009 |
vpcmpeqb VEC_SIZE(%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl $1, %edx
|
|
Packit |
6c4009 |
sall %cl, %edx
|
|
Packit |
6c4009 |
subl $1, %edx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Remove the trailing bytes. */
|
|
Packit |
6c4009 |
andl %edx, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jnz L(last_vec_x1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check the second last VEC. */
|
|
Packit |
6c4009 |
vpcmpeqb (%rdi), %ymm0, %ymm1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl %r8d, %ecx
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
vpmovmskb %ymm1, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Remove the leading bytes. Must use unsigned right shift for
|
|
Packit |
6c4009 |
bsrl below. */
|
|
Packit |
6c4009 |
shrl %cl, %eax
|
|
Packit |
6c4009 |
testl %eax, %eax
|
|
Packit |
6c4009 |
jz L(zero)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
bsrl %eax, %eax
|
|
Packit |
6c4009 |
addq %rdi, %rax
|
|
Packit |
6c4009 |
addq %r8, %rax
|
|
Packit |
6c4009 |
VZEROUPPER
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
END (__memrchr_avx2)
|
|
Packit |
6c4009 |
#endif
|