190885
From 8bb82fd62bdb2d8741998a5a1be38388890452da Mon Sep 17 00:00:00 2001
190885
From: Noah Goldstein <goldstein.w.n@gmail.com>
190885
Date: Mon, 3 May 2021 03:03:19 -0400
190885
Subject: [PATCH] x86: Optimize memchr-evex.S
190885
190885
No bug. This commit optimizes memchr-evex.S. The optimizations include
190885
replacing some branches with cmovcc, avoiding some branches entirely
190885
in the less_4x_vec case, making the page cross logic less strict,
190885
saving some ALU in the alignment process, and most importantly
190885
increasing ILP in the 4x loop. test-memchr, test-rawmemchr, and
190885
test-wmemchr are all passing.
190885
190885
Signed-off-by: Noah Goldstein <goldstein.w.n@gmail.com>
190885
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
190885
(cherry picked from commit 2a76821c3081d2c0231ecd2618f52662cb48fccd)
190885
---
190885
 sysdeps/x86_64/multiarch/memchr-evex.S | 547 +++++++++++++++----------
190885
 1 file changed, 322 insertions(+), 225 deletions(-)
190885
190885
diff --git a/sysdeps/x86_64/multiarch/memchr-evex.S b/sysdeps/x86_64/multiarch/memchr-evex.S
190885
index 6dd5d67b..81d5cd64 100644
190885
--- a/sysdeps/x86_64/multiarch/memchr-evex.S
190885
+++ b/sysdeps/x86_64/multiarch/memchr-evex.S
190885
@@ -26,14 +26,28 @@
190885
 
190885
 # ifdef USE_AS_WMEMCHR
190885
 #  define VPBROADCAST	vpbroadcastd
190885
-#  define VPCMP		vpcmpd
190885
-#  define SHIFT_REG	r8d
190885
+#  define VPMINU	vpminud
190885
+#  define VPCMP	vpcmpd
190885
+#  define VPCMPEQ	vpcmpeqd
190885
+#  define CHAR_SIZE	4
190885
 # else
190885
 #  define VPBROADCAST	vpbroadcastb
190885
-#  define VPCMP		vpcmpb
190885
-#  define SHIFT_REG	ecx
190885
+#  define VPMINU	vpminub
190885
+#  define VPCMP	vpcmpb
190885
+#  define VPCMPEQ	vpcmpeqb
190885
+#  define CHAR_SIZE	1
190885
 # endif
190885
 
190885
+# ifdef USE_AS_RAWMEMCHR
190885
+#  define RAW_PTR_REG	rcx
190885
+#  define ALGN_PTR_REG	rdi
190885
+# else
190885
+#  define RAW_PTR_REG	rdi
190885
+#  define ALGN_PTR_REG	rcx
190885
+# endif
190885
+
190885
+# define XMMZERO	xmm23
190885
+# define YMMZERO	ymm23
190885
 # define XMMMATCH	xmm16
190885
 # define YMMMATCH	ymm16
190885
 # define YMM1		ymm17
190885
@@ -44,6 +58,8 @@
190885
 # define YMM6		ymm22
190885
 
190885
 # define VEC_SIZE 32
190885
+# define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
190885
+# define PAGE_SIZE 4096
190885
 
190885
 	.section .text.evex,"ax",@progbits
190885
 ENTRY (MEMCHR)
190885
@@ -51,11 +67,7 @@ ENTRY (MEMCHR)
190885
 	/* Check for zero length.  */
190885
 	test	%RDX_LP, %RDX_LP
190885
 	jz	L(zero)
190885
-# endif
190885
-	movl	%edi, %ecx
190885
-# ifdef USE_AS_WMEMCHR
190885
-	shl	$2, %RDX_LP
190885
-# else
190885
+
190885
 #  ifdef __ILP32__
190885
 	/* Clear the upper 32 bits.  */
190885
 	movl	%edx, %edx
190885
@@ -64,318 +76,403 @@ ENTRY (MEMCHR)
190885
 	/* Broadcast CHAR to YMMMATCH.  */
190885
 	VPBROADCAST %esi, %YMMMATCH
190885
 	/* Check if we may cross page boundary with one vector load.  */
190885
-	andl	$(2 * VEC_SIZE - 1), %ecx
190885
-	cmpl	$VEC_SIZE, %ecx
190885
-	ja	L(cros_page_boundary)
190885
+	movl	%edi, %eax
190885
+	andl	$(PAGE_SIZE - 1), %eax
190885
+	cmpl	$(PAGE_SIZE - VEC_SIZE), %eax
190885
+	ja	L(cross_page_boundary)
190885
 
190885
 	/* Check the first VEC_SIZE bytes.  */
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
-
190885
+	VPCMP	$0, (%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
 # ifndef USE_AS_RAWMEMCHR
190885
-	jnz	L(first_vec_x0_check)
190885
-	/* Adjust length and check the end of data.  */
190885
-	subq	$VEC_SIZE, %rdx
190885
-	jbe	L(zero)
190885
+	/* If length < CHAR_PER_VEC handle special.  */
190885
+	cmpq	$CHAR_PER_VEC, %rdx
190885
+	jbe	L(first_vec_x0)
190885
+# endif
190885
+	testl	%eax, %eax
190885
+	jz	L(aligned_more)
190885
+	tzcntl	%eax, %eax
190885
+# ifdef USE_AS_WMEMCHR
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(%rdi, %rax, CHAR_SIZE), %rax
190885
 # else
190885
-	jnz	L(first_vec_x0)
190885
+	addq	%rdi, %rax
190885
 # endif
190885
-
190885
-	/* Align data for aligned loads in the loop.  */
190885
-	addq	$VEC_SIZE, %rdi
190885
-	andl	$(VEC_SIZE - 1), %ecx
190885
-	andq	$-VEC_SIZE, %rdi
190885
+	ret
190885
 
190885
 # ifndef USE_AS_RAWMEMCHR
190885
-	/* Adjust length.  */
190885
-	addq	%rcx, %rdx
190885
-
190885
-	subq	$(VEC_SIZE * 4), %rdx
190885
-	jbe	L(last_4x_vec_or_less)
190885
-# endif
190885
-	jmp	L(more_4x_vec)
190885
+L(zero):
190885
+	xorl	%eax, %eax
190885
+	ret
190885
 
190885
+	.p2align 5
190885
+L(first_vec_x0):
190885
+	/* Check if first match was before length.  */
190885
+	tzcntl	%eax, %eax
190885
+	xorl	%ecx, %ecx
190885
+	cmpl	%eax, %edx
190885
+	leaq	(%rdi, %rax, CHAR_SIZE), %rax
190885
+	cmovle	%rcx, %rax
190885
+	ret
190885
+# else
190885
+	/* NB: first_vec_x0 is 17 bytes which will leave
190885
+	   cross_page_boundary (which is relatively cold) close enough
190885
+	   to ideal alignment. So only realign L(cross_page_boundary) if
190885
+	   rawmemchr.  */
190885
 	.p2align 4
190885
-L(cros_page_boundary):
190885
-	andl	$(VEC_SIZE - 1), %ecx
190885
+# endif
190885
+L(cross_page_boundary):
190885
+	/* Save pointer before aligning as its original value is
190885
+	   necessary for computer return address if byte is found or
190885
+	   adjusting length if it is not and this is memchr.  */
190885
+	movq	%rdi, %rcx
190885
+	/* Align data to VEC_SIZE. ALGN_PTR_REG is rcx for memchr and rdi
190885
+	   for rawmemchr.  */
190885
+	andq	$-VEC_SIZE, %ALGN_PTR_REG
190885
+	VPCMP	$0, (%ALGN_PTR_REG), %YMMMATCH, %k0
190885
+	kmovd	%k0, %r8d
190885
 # ifdef USE_AS_WMEMCHR
190885
-	/* NB: Divide shift count by 4 since each bit in K1 represent 4
190885
+	/* NB: Divide shift count by 4 since each bit in K0 represent 4
190885
 	   bytes.  */
190885
-	movl	%ecx, %SHIFT_REG
190885
-	sarl	$2, %SHIFT_REG
190885
+	sarl	$2, %eax
190885
+# endif
190885
+# ifndef USE_AS_RAWMEMCHR
190885
+	movl	$(PAGE_SIZE / CHAR_SIZE), %esi
190885
+	subl	%eax, %esi
190885
 # endif
190885
-	andq	$-VEC_SIZE, %rdi
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	/* Remove the leading bytes.  */
190885
-	sarxl	%SHIFT_REG, %eax, %eax
190885
-	testl	%eax, %eax
190885
-	jz	L(aligned_more)
190885
-	tzcntl	%eax, %eax
190885
 # ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	sall	$2, %eax
190885
+	andl	$(CHAR_PER_VEC - 1), %eax
190885
 # endif
190885
+	/* Remove the leading bytes.  */
190885
+	sarxl	%eax, %r8d, %eax
190885
 # ifndef USE_AS_RAWMEMCHR
190885
 	/* Check the end of data.  */
190885
-	cmpq	%rax, %rdx
190885
-	jbe	L(zero)
190885
+	cmpq	%rsi, %rdx
190885
+	jbe	L(first_vec_x0)
190885
+# endif
190885
+	testl	%eax, %eax
190885
+	jz	L(cross_page_continue)
190885
+	tzcntl	%eax, %eax
190885
+# ifdef USE_AS_WMEMCHR
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(%RAW_PTR_REG, %rax, CHAR_SIZE), %rax
190885
+# else
190885
+	addq	%RAW_PTR_REG, %rax
190885
 # endif
190885
-	addq	%rdi, %rax
190885
-	addq	%rcx, %rax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(aligned_more):
190885
-# ifndef USE_AS_RAWMEMCHR
190885
-        /* Calculate "rdx + rcx - VEC_SIZE" with "rdx - (VEC_SIZE - rcx)"
190885
-	   instead of "(rdx + rcx) - VEC_SIZE" to void possible addition
190885
-	   overflow.  */
190885
-	negq	%rcx
190885
-	addq	$VEC_SIZE, %rcx
190885
+L(first_vec_x1):
190885
+	tzcntl	%eax, %eax
190885
+	leaq	VEC_SIZE(%rdi, %rax, CHAR_SIZE), %rax
190885
+	ret
190885
 
190885
-	/* Check the end of data.  */
190885
-	subq	%rcx, %rdx
190885
-	jbe	L(zero)
190885
-# endif
190885
+	.p2align 4
190885
+L(first_vec_x2):
190885
+	tzcntl	%eax, %eax
190885
+	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
190885
+	ret
190885
 
190885
-	addq	$VEC_SIZE, %rdi
190885
+	.p2align 4
190885
+L(first_vec_x3):
190885
+	tzcntl	%eax, %eax
190885
+	leaq	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
190885
+	ret
190885
 
190885
-# ifndef USE_AS_RAWMEMCHR
190885
-	subq	$(VEC_SIZE * 4), %rdx
190885
-	jbe	L(last_4x_vec_or_less)
190885
-# endif
190885
+	.p2align 4
190885
+L(first_vec_x4):
190885
+	tzcntl	%eax, %eax
190885
+	leaq	(VEC_SIZE * 4)(%rdi, %rax, CHAR_SIZE), %rax
190885
+	ret
190885
 
190885
-L(more_4x_vec):
190885
+	.p2align 5
190885
+L(aligned_more):
190885
 	/* Check the first 4 * VEC_SIZE.  Only one VEC_SIZE at a time
190885
 	   since data is only aligned to VEC_SIZE.  */
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
-	jnz	L(first_vec_x0)
190885
 
190885
-	VPCMP	$0, VEC_SIZE(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
+# ifndef USE_AS_RAWMEMCHR
190885
+	/* Align data to VEC_SIZE.  */
190885
+L(cross_page_continue):
190885
+	xorl	%ecx, %ecx
190885
+	subl	%edi, %ecx
190885
+	andq	$-VEC_SIZE, %rdi
190885
+	/* esi is for adjusting length to see if near the end.  */
190885
+	leal	(VEC_SIZE * 5)(%rdi, %rcx), %esi
190885
+#  ifdef USE_AS_WMEMCHR
190885
+	/* NB: Divide bytes by 4 to get the wchar_t count.  */
190885
+	sarl	$2, %esi
190885
+#  endif
190885
+# else
190885
+	andq	$-VEC_SIZE, %rdi
190885
+L(cross_page_continue):
190885
+# endif
190885
+	/* Load first VEC regardless.  */
190885
+	VPCMP	$0, (VEC_SIZE)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+# ifndef USE_AS_RAWMEMCHR
190885
+	/* Adjust length. If near end handle specially.  */
190885
+	subq	%rsi, %rdx
190885
+	jbe	L(last_4x_vec_or_less)
190885
+# endif
190885
 	testl	%eax, %eax
190885
 	jnz	L(first_vec_x1)
190885
 
190885
-	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
+	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
 	testl	%eax, %eax
190885
 	jnz	L(first_vec_x2)
190885
 
190885
-	VPCMP	$0, (VEC_SIZE * 3)(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
+	VPCMP	$0, (VEC_SIZE * 3)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
 	testl	%eax, %eax
190885
 	jnz	L(first_vec_x3)
190885
 
190885
-	addq	$(VEC_SIZE * 4), %rdi
190885
+	VPCMP	$0, (VEC_SIZE * 4)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	testl	%eax, %eax
190885
+	jnz	L(first_vec_x4)
190885
+
190885
 
190885
 # ifndef USE_AS_RAWMEMCHR
190885
-	subq	$(VEC_SIZE * 4), %rdx
190885
-	jbe	L(last_4x_vec_or_less)
190885
-# endif
190885
+	/* Check if at last CHAR_PER_VEC * 4 length.  */
190885
+	subq	$(CHAR_PER_VEC * 4), %rdx
190885
+	jbe	L(last_4x_vec_or_less_cmpeq)
190885
+	addq	$VEC_SIZE, %rdi
190885
 
190885
-	/* Align data to 4 * VEC_SIZE.  */
190885
-	movq	%rdi, %rcx
190885
-	andl	$(4 * VEC_SIZE - 1), %ecx
190885
+	/* Align data to VEC_SIZE * 4 for the loop and readjust length.
190885
+	 */
190885
+#  ifdef USE_AS_WMEMCHR
190885
+	movl	%edi, %ecx
190885
 	andq	$-(4 * VEC_SIZE), %rdi
190885
-
190885
-# ifndef USE_AS_RAWMEMCHR
190885
-	/* Adjust length.  */
190885
+	andl	$(VEC_SIZE * 4 - 1), %ecx
190885
+	/* NB: Divide bytes by 4 to get the wchar_t count.  */
190885
+	sarl	$2, %ecx
190885
 	addq	%rcx, %rdx
190885
+#  else
190885
+	addq	%rdi, %rdx
190885
+	andq	$-(4 * VEC_SIZE), %rdi
190885
+	subq	%rdi, %rdx
190885
+#  endif
190885
+# else
190885
+	addq	$VEC_SIZE, %rdi
190885
+	andq	$-(4 * VEC_SIZE), %rdi
190885
 # endif
190885
 
190885
+	vpxorq	%XMMZERO, %XMMZERO, %XMMZERO
190885
+
190885
+	/* Compare 4 * VEC at a time forward.  */
190885
 	.p2align 4
190885
 L(loop_4x_vec):
190885
-	/* Compare 4 * VEC at a time forward.  */
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
-	VPCMP	$0, VEC_SIZE(%rdi), %YMMMATCH, %k2
190885
-	kord	%k1, %k2, %k5
190885
-	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k3
190885
-	VPCMP	$0, (VEC_SIZE * 3)(%rdi), %YMMMATCH, %k4
190885
-
190885
-	kord	%k3, %k4, %k6
190885
-	kortestd %k5, %k6
190885
-	jnz	L(4x_vec_end)
190885
-
190885
-	addq	$(VEC_SIZE * 4), %rdi
190885
-
190885
+	/* It would be possible to save some instructions using 4x VPCMP
190885
+	   but bottleneck on port 5 makes it not woth it.  */
190885
+	VPCMP	$4, (VEC_SIZE * 4)(%rdi), %YMMMATCH, %k1
190885
+	/* xor will set bytes match esi to zero.  */
190885
+	vpxorq	(VEC_SIZE * 5)(%rdi), %YMMMATCH, %YMM2
190885
+	vpxorq	(VEC_SIZE * 6)(%rdi), %YMMMATCH, %YMM3
190885
+	VPCMP	$0, (VEC_SIZE * 7)(%rdi), %YMMMATCH, %k3
190885
+	/* Reduce VEC2 / VEC3 with min and VEC1 with zero mask.  */
190885
+	VPMINU	%YMM2, %YMM3, %YMM3 {%k1} {z}
190885
+	VPCMP	$0, %YMM3, %YMMZERO, %k2
190885
 # ifdef USE_AS_RAWMEMCHR
190885
-	jmp	L(loop_4x_vec)
190885
+	subq	$-(VEC_SIZE * 4), %rdi
190885
+	kortestd %k2, %k3
190885
+	jz	L(loop_4x_vec)
190885
 # else
190885
-	subq	$(VEC_SIZE * 4), %rdx
190885
+	kortestd %k2, %k3
190885
+	jnz	L(loop_4x_vec_end)
190885
+
190885
+	subq	$-(VEC_SIZE * 4), %rdi
190885
+
190885
+	subq	$(CHAR_PER_VEC * 4), %rdx
190885
 	ja	L(loop_4x_vec)
190885
 
190885
+	/* Fall through into less than 4 remaining vectors of length case.
190885
+	 */
190885
+	VPCMP	$0, (VEC_SIZE * 4)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	addq	$(VEC_SIZE * 3), %rdi
190885
+	.p2align 4
190885
 L(last_4x_vec_or_less):
190885
-	/* Less than 4 * VEC and aligned to VEC_SIZE.  */
190885
-	addl	$(VEC_SIZE * 2), %edx
190885
-	jle	L(last_2x_vec)
190885
-
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
+	/* Check if first VEC contained match.  */
190885
 	testl	%eax, %eax
190885
-	jnz	L(first_vec_x0)
190885
+	jnz	L(first_vec_x1_check)
190885
 
190885
-	VPCMP	$0, VEC_SIZE(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
-	jnz	L(first_vec_x1)
190885
+	/* If remaining length > CHAR_PER_VEC * 2.  */
190885
+	addl	$(CHAR_PER_VEC * 2), %edx
190885
+	jg	L(last_4x_vec)
190885
 
190885
-	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
+L(last_2x_vec):
190885
+	/* If remaining length < CHAR_PER_VEC.  */
190885
+	addl	$CHAR_PER_VEC, %edx
190885
+	jle	L(zero_end)
190885
 
190885
-	jnz	L(first_vec_x2_check)
190885
-	subl	$VEC_SIZE, %edx
190885
-	jle	L(zero)
190885
+	/* Check VEC2 and compare any match with remaining length.  */
190885
+	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	tzcntl	%eax, %eax
190885
+	cmpl	%eax, %edx
190885
+	jbe	L(set_zero_end)
190885
+	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
190885
+L(zero_end):
190885
+	ret
190885
 
190885
-	VPCMP	$0, (VEC_SIZE * 3)(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
 
190885
-	jnz	L(first_vec_x3_check)
190885
+	.p2align 4
190885
+L(first_vec_x1_check):
190885
+	tzcntl	%eax, %eax
190885
+	/* Adjust length.  */
190885
+	subl	$-(CHAR_PER_VEC * 4), %edx
190885
+	/* Check if match within remaining length.  */
190885
+	cmpl	%eax, %edx
190885
+	jbe	L(set_zero_end)
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	VEC_SIZE(%rdi, %rax, CHAR_SIZE), %rax
190885
+	ret
190885
+L(set_zero_end):
190885
 	xorl	%eax, %eax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(last_2x_vec):
190885
-	addl	$(VEC_SIZE * 2), %edx
190885
-	VPCMP	$0, (%rdi), %YMMMATCH, %k1
190885
+L(loop_4x_vec_end):
190885
+# endif
190885
+	/* rawmemchr will fall through into this if match was found in
190885
+	   loop.  */
190885
+
190885
+	/* k1 has not of matches with VEC1.  */
190885
 	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
+# ifdef USE_AS_WMEMCHR
190885
+	subl	$((1 << CHAR_PER_VEC) - 1), %eax
190885
+# else
190885
+	incl	%eax
190885
+# endif
190885
+	jnz	L(last_vec_x1_return)
190885
 
190885
-	jnz	L(first_vec_x0_check)
190885
-	subl	$VEC_SIZE, %edx
190885
-	jle	L(zero)
190885
+	VPCMP	$0, %YMM2, %YMMZERO, %k0
190885
+	kmovd	%k0, %eax
190885
+	testl	%eax, %eax
190885
+	jnz	L(last_vec_x2_return)
190885
 
190885
-	VPCMP	$0, VEC_SIZE(%rdi), %YMMMATCH, %k1
190885
-	kmovd	%k1, %eax
190885
+	kmovd	%k2, %eax
190885
 	testl	%eax, %eax
190885
-	jnz	L(first_vec_x1_check)
190885
-	xorl	%eax, %eax
190885
-	ret
190885
+	jnz	L(last_vec_x3_return)
190885
 
190885
-	.p2align 4
190885
-L(first_vec_x0_check):
190885
+	kmovd	%k3, %eax
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	sall	$2, %eax
190885
+# ifdef USE_AS_RAWMEMCHR
190885
+	leaq	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
190885
+# else
190885
+	leaq	(VEC_SIZE * 7)(%rdi, %rax, CHAR_SIZE), %rax
190885
 # endif
190885
-	/* Check the end of data.  */
190885
-	cmpq	%rax, %rdx
190885
-	jbe	L(zero)
190885
-	addq	%rdi, %rax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(first_vec_x1_check):
190885
+L(last_vec_x1_return):
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	sall	$2, %eax
190885
-# endif
190885
-	/* Check the end of data.  */
190885
-	cmpq	%rax, %rdx
190885
-	jbe	L(zero)
190885
-	addq	$VEC_SIZE, %rax
190885
+# ifdef USE_AS_RAWMEMCHR
190885
+#  ifdef USE_AS_WMEMCHR
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(%rdi, %rax, CHAR_SIZE), %rax
190885
+#  else
190885
 	addq	%rdi, %rax
190885
-	ret
190885
-
190885
-	.p2align 4
190885
-L(first_vec_x2_check):
190885
-	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	sall	$2, %eax
190885
+#  endif
190885
+# else
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(VEC_SIZE * 4)(%rdi, %rax, CHAR_SIZE), %rax
190885
 # endif
190885
-	/* Check the end of data.  */
190885
-	cmpq	%rax, %rdx
190885
-	jbe	L(zero)
190885
-	addq	$(VEC_SIZE * 2), %rax
190885
-	addq	%rdi, %rax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(first_vec_x3_check):
190885
+L(last_vec_x2_return):
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	sall	$2, %eax
190885
+# ifdef USE_AS_RAWMEMCHR
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	VEC_SIZE(%rdi, %rax, CHAR_SIZE), %rax
190885
+# else
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(VEC_SIZE * 5)(%rdi, %rax, CHAR_SIZE), %rax
190885
 # endif
190885
-	/* Check the end of data.  */
190885
-	cmpq	%rax, %rdx
190885
-	jbe	L(zero)
190885
-	addq	$(VEC_SIZE * 3), %rax
190885
-	addq	%rdi, %rax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(zero):
190885
-	xorl	%eax, %eax
190885
-	ret
190885
-# endif
190885
-
190885
-	.p2align 4
190885
-L(first_vec_x0):
190885
+L(last_vec_x3_return):
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	leaq	(%rdi, %rax, 4), %rax
190885
+# ifdef USE_AS_RAWMEMCHR
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
190885
 # else
190885
-	addq	%rdi, %rax
190885
+	/* NB: Multiply bytes by CHAR_SIZE to get the wchar_t count.  */
190885
+	leaq	(VEC_SIZE * 6)(%rdi, %rax, CHAR_SIZE), %rax
190885
 # endif
190885
 	ret
190885
 
190885
+
190885
+# ifndef USE_AS_RAWMEMCHR
190885
+L(last_4x_vec_or_less_cmpeq):
190885
+	VPCMP	$0, (VEC_SIZE * 5)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	subq	$-(VEC_SIZE * 4), %rdi
190885
+	/* Check first VEC regardless.  */
190885
+	testl	%eax, %eax
190885
+	jnz	L(first_vec_x1_check)
190885
+
190885
+	/* If remaining length <= CHAR_PER_VEC * 2.  */
190885
+	addl	$(CHAR_PER_VEC * 2), %edx
190885
+	jle	L(last_2x_vec)
190885
+
190885
 	.p2align 4
190885
-L(first_vec_x1):
190885
+L(last_4x_vec):
190885
+	VPCMP	$0, (VEC_SIZE * 2)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	testl	%eax, %eax
190885
+	jnz	L(last_vec_x2)
190885
+
190885
+
190885
+	VPCMP	$0, (VEC_SIZE * 3)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	/* Create mask for possible matches within remaining length.  */
190885
+#  ifdef USE_AS_WMEMCHR
190885
+	movl	$((1 << (CHAR_PER_VEC * 2)) - 1), %ecx
190885
+	bzhil	%edx, %ecx, %ecx
190885
+#  else
190885
+	movq	$-1, %rcx
190885
+	bzhiq	%rdx, %rcx, %rcx
190885
+#  endif
190885
+	/* Test matches in data against length match.  */
190885
+	andl	%ecx, %eax
190885
+	jnz	L(last_vec_x3)
190885
+
190885
+	/* if remaining length <= CHAR_PER_VEC * 3 (Note this is after
190885
+	   remaining length was found to be > CHAR_PER_VEC * 2.  */
190885
+	subl	$CHAR_PER_VEC, %edx
190885
+	jbe	L(zero_end2)
190885
+
190885
+
190885
+	VPCMP	$0, (VEC_SIZE * 4)(%rdi), %YMMMATCH, %k0
190885
+	kmovd	%k0, %eax
190885
+	/* Shift remaining length mask for last VEC.  */
190885
+#  ifdef USE_AS_WMEMCHR
190885
+	shrl	$CHAR_PER_VEC, %ecx
190885
+#  else
190885
+	shrq	$CHAR_PER_VEC, %rcx
190885
+#  endif
190885
+	andl	%ecx, %eax
190885
+	jz	L(zero_end2)
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	leaq	VEC_SIZE(%rdi, %rax, 4), %rax
190885
-# else
190885
-	addq	$VEC_SIZE, %rax
190885
-	addq	%rdi, %rax
190885
-# endif
190885
+	leaq	(VEC_SIZE * 4)(%rdi, %rax, CHAR_SIZE), %rax
190885
+L(zero_end2):
190885
 	ret
190885
 
190885
-	.p2align 4
190885
-L(first_vec_x2):
190885
+L(last_vec_x2):
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	leaq	(VEC_SIZE * 2)(%rdi, %rax, 4), %rax
190885
-# else
190885
-	addq	$(VEC_SIZE * 2), %rax
190885
-	addq	%rdi, %rax
190885
-# endif
190885
+	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
190885
 	ret
190885
 
190885
 	.p2align 4
190885
-L(4x_vec_end):
190885
-	kmovd	%k1, %eax
190885
-	testl	%eax, %eax
190885
-	jnz	L(first_vec_x0)
190885
-	kmovd	%k2, %eax
190885
-	testl	%eax, %eax
190885
-	jnz	L(first_vec_x1)
190885
-	kmovd	%k3, %eax
190885
-	testl	%eax, %eax
190885
-	jnz	L(first_vec_x2)
190885
-	kmovd	%k4, %eax
190885
-	testl	%eax, %eax
190885
-L(first_vec_x3):
190885
+L(last_vec_x3):
190885
 	tzcntl	%eax, %eax
190885
-# ifdef USE_AS_WMEMCHR
190885
-	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
-	leaq	(VEC_SIZE * 3)(%rdi, %rax, 4), %rax
190885
-# else
190885
-	addq	$(VEC_SIZE * 3), %rax
190885
-	addq	%rdi, %rax
190885
-# endif
190885
+	leaq	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
190885
 	ret
190885
+# endif
190885
 
190885
 END (MEMCHR)
190885
 #endif
190885
-- 
190885
GitLab
190885