08c3a6
commit dd6d3a0bbcc67cb2b50b0add0c599f9f99491d8b
08c3a6
Author: Noah Goldstein <goldstein.w.n@gmail.com>
08c3a6
Date:   Wed Mar 23 16:57:18 2022 -0500
08c3a6
08c3a6
    x86: Code cleanup in strchr-evex and comment justifying branch
08c3a6
    
08c3a6
    Small code cleanup for size: -81 bytes.
08c3a6
    
08c3a6
    Add comment justifying using a branch to do NULL/non-null return.
08c3a6
    
08c3a6
    All string/memory tests pass and no regressions in benchtests.
08c3a6
    
08c3a6
    geometric_mean(N=20) of all benchmarks New / Original: .985
08c3a6
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
08c3a6
    
08c3a6
    (cherry picked from commit ec285ea90415458225623ddc0492ae3f705af043)
08c3a6
08c3a6
diff --git a/sysdeps/x86_64/multiarch/strchr-evex.S b/sysdeps/x86_64/multiarch/strchr-evex.S
08c3a6
index 7f9d4ee48ddaa998..0b49e0ac54e7b0dd 100644
08c3a6
--- a/sysdeps/x86_64/multiarch/strchr-evex.S
08c3a6
+++ b/sysdeps/x86_64/multiarch/strchr-evex.S
08c3a6
@@ -30,6 +30,7 @@
08c3a6
 # ifdef USE_AS_WCSCHR
08c3a6
 #  define VPBROADCAST	vpbroadcastd
08c3a6
 #  define VPCMP		vpcmpd
08c3a6
+#  define VPTESTN	vptestnmd
08c3a6
 #  define VPMINU	vpminud
08c3a6
 #  define CHAR_REG	esi
08c3a6
 #  define SHIFT_REG	ecx
08c3a6
@@ -37,6 +38,7 @@
08c3a6
 # else
08c3a6
 #  define VPBROADCAST	vpbroadcastb
08c3a6
 #  define VPCMP		vpcmpb
08c3a6
+#  define VPTESTN	vptestnmb
08c3a6
 #  define VPMINU	vpminub
08c3a6
 #  define CHAR_REG	sil
08c3a6
 #  define SHIFT_REG	edx
08c3a6
@@ -61,13 +63,11 @@
08c3a6
 # define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
08c3a6
 
08c3a6
 	.section .text.evex,"ax",@progbits
08c3a6
-ENTRY (STRCHR)
08c3a6
+ENTRY_P2ALIGN (STRCHR, 5)
08c3a6
 	/* Broadcast CHAR to YMM0.	*/
08c3a6
 	VPBROADCAST	%esi, %YMM0
08c3a6
 	movl	%edi, %eax
08c3a6
 	andl	$(PAGE_SIZE - 1), %eax
08c3a6
-	vpxorq	%XMMZERO, %XMMZERO, %XMMZERO
08c3a6
-
08c3a6
 	/* Check if we cross page boundary with one vector load.
08c3a6
 	   Otherwise it is safe to use an unaligned load.  */
08c3a6
 	cmpl	$(PAGE_SIZE - VEC_SIZE), %eax
08c3a6
@@ -81,49 +81,35 @@ ENTRY (STRCHR)
08c3a6
 	vpxorq	%YMM1, %YMM0, %YMM2
08c3a6
 	VPMINU	%YMM2, %YMM1, %YMM2
08c3a6
 	/* Each bit in K0 represents a CHAR or a null byte in YMM1.  */
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM2, %k0
08c3a6
+	VPTESTN	%YMM2, %YMM2, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	testl	%eax, %eax
08c3a6
 	jz	L(aligned_more)
08c3a6
 	tzcntl	%eax, %eax
08c3a6
+# ifndef USE_AS_STRCHRNUL
08c3a6
+	/* Found CHAR or the null byte.  */
08c3a6
+	cmp	(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
+	/* NB: Use a branch instead of cmovcc here. The expectation is
08c3a6
+	   that with strchr the user will branch based on input being
08c3a6
+	   null. Since this branch will be 100% predictive of the user
08c3a6
+	   branch a branch miss here should save what otherwise would
08c3a6
+	   be branch miss in the user code. Otherwise using a branch 1)
08c3a6
+	   saves code size and 2) is faster in highly predictable
08c3a6
+	   environments.  */
08c3a6
+	jne	L(zero)
08c3a6
+# endif
08c3a6
 # ifdef USE_AS_WCSCHR
08c3a6
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.
08c3a6
 	 */
08c3a6
 	leaq	(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
 # else
08c3a6
 	addq	%rdi, %rax
08c3a6
-# endif
08c3a6
-# ifndef USE_AS_STRCHRNUL
08c3a6
-	/* Found CHAR or the null byte.	 */
08c3a6
-	cmp	(%rax), %CHAR_REG
08c3a6
-	jne	L(zero)
08c3a6
 # endif
08c3a6
 	ret
08c3a6
 
08c3a6
-	/* .p2align 5 helps keep performance more consistent if ENTRY()
08c3a6
-	   alignment % 32 was either 16 or 0. As well this makes the
08c3a6
-	   alignment % 32 of the loop_4x_vec fixed which makes tuning it
08c3a6
-	   easier.  */
08c3a6
-	.p2align 5
08c3a6
-L(first_vec_x3):
08c3a6
-	tzcntl	%eax, %eax
08c3a6
-# ifndef USE_AS_STRCHRNUL
08c3a6
-	/* Found CHAR or the null byte.	 */
08c3a6
-	cmp	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
-	jne	L(zero)
08c3a6
-# endif
08c3a6
-	/* NB: Multiply sizeof char type (1 or 4) to get the number of
08c3a6
-	   bytes.  */
08c3a6
-	leaq	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
-	ret
08c3a6
 
08c3a6
-# ifndef USE_AS_STRCHRNUL
08c3a6
-L(zero):
08c3a6
-	xorl	%eax, %eax
08c3a6
-	ret
08c3a6
-# endif
08c3a6
 
08c3a6
-	.p2align 4
08c3a6
+	.p2align 4,, 10
08c3a6
 L(first_vec_x4):
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Check to see if first match was CHAR (k0) or null (k1).  */
08c3a6
@@ -144,9 +130,18 @@ L(first_vec_x4):
08c3a6
 	leaq	(VEC_SIZE * 4)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
 	ret
08c3a6
 
08c3a6
+# ifndef USE_AS_STRCHRNUL
08c3a6
+L(zero):
08c3a6
+	xorl	%eax, %eax
08c3a6
+	ret
08c3a6
+# endif
08c3a6
+
08c3a6
+
08c3a6
 	.p2align 4
08c3a6
 L(first_vec_x1):
08c3a6
-	tzcntl	%eax, %eax
08c3a6
+	/* Use bsf here to save 1-byte keeping keeping the block in 1x
08c3a6
+	   fetch block. eax guranteed non-zero.  */
08c3a6
+	bsfl	%eax, %eax
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Found CHAR or the null byte.	 */
08c3a6
 	cmp	(VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
@@ -158,7 +153,7 @@ L(first_vec_x1):
08c3a6
 	leaq	(VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
 	ret
08c3a6
 
08c3a6
-	.p2align 4
08c3a6
+	.p2align 4,, 10
08c3a6
 L(first_vec_x2):
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Check to see if first match was CHAR (k0) or null (k1).  */
08c3a6
@@ -179,6 +174,21 @@ L(first_vec_x2):
08c3a6
 	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
 	ret
08c3a6
 
08c3a6
+	.p2align 4,, 10
08c3a6
+L(first_vec_x3):
08c3a6
+	/* Use bsf here to save 1-byte keeping keeping the block in 1x
08c3a6
+	   fetch block. eax guranteed non-zero.  */
08c3a6
+	bsfl	%eax, %eax
08c3a6
+# ifndef USE_AS_STRCHRNUL
08c3a6
+	/* Found CHAR or the null byte.	 */
08c3a6
+	cmp	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
+	jne	L(zero)
08c3a6
+# endif
08c3a6
+	/* NB: Multiply sizeof char type (1 or 4) to get the number of
08c3a6
+	   bytes.  */
08c3a6
+	leaq	(VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
+	ret
08c3a6
+
08c3a6
 	.p2align 4
08c3a6
 L(aligned_more):
08c3a6
 	/* Align data to VEC_SIZE.  */
08c3a6
@@ -195,7 +205,7 @@ L(cross_page_continue):
08c3a6
 	vpxorq	%YMM1, %YMM0, %YMM2
08c3a6
 	VPMINU	%YMM2, %YMM1, %YMM2
08c3a6
 	/* Each bit in K0 represents a CHAR or a null byte in YMM1.  */
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM2, %k0
08c3a6
+	VPTESTN	%YMM2, %YMM2, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	testl	%eax, %eax
08c3a6
 	jnz	L(first_vec_x1)
08c3a6
@@ -206,7 +216,7 @@ L(cross_page_continue):
08c3a6
 	/* Each bit in K0 represents a CHAR in YMM1.  */
08c3a6
 	VPCMP	$0, %YMM1, %YMM0, %k0
08c3a6
 	/* Each bit in K1 represents a CHAR in YMM1.  */
08c3a6
-	VPCMP	$0, %YMM1, %YMMZERO, %k1
08c3a6
+	VPTESTN	%YMM1, %YMM1, %k1
08c3a6
 	kortestd	%k0, %k1
08c3a6
 	jnz	L(first_vec_x2)
08c3a6
 
08c3a6
@@ -215,7 +225,7 @@ L(cross_page_continue):
08c3a6
 	vpxorq	%YMM1, %YMM0, %YMM2
08c3a6
 	VPMINU	%YMM2, %YMM1, %YMM2
08c3a6
 	/* Each bit in K0 represents a CHAR or a null byte in YMM1.  */
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM2, %k0
08c3a6
+	VPTESTN	%YMM2, %YMM2, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	testl	%eax, %eax
08c3a6
 	jnz	L(first_vec_x3)
08c3a6
@@ -224,7 +234,7 @@ L(cross_page_continue):
08c3a6
 	/* Each bit in K0 represents a CHAR in YMM1.  */
08c3a6
 	VPCMP	$0, %YMM1, %YMM0, %k0
08c3a6
 	/* Each bit in K1 represents a CHAR in YMM1.  */
08c3a6
-	VPCMP	$0, %YMM1, %YMMZERO, %k1
08c3a6
+	VPTESTN	%YMM1, %YMM1, %k1
08c3a6
 	kortestd	%k0, %k1
08c3a6
 	jnz	L(first_vec_x4)
08c3a6
 
08c3a6
@@ -265,33 +275,33 @@ L(loop_4x_vec):
08c3a6
 	VPMINU	%YMM3, %YMM4, %YMM4
08c3a6
 	VPMINU	%YMM2, %YMM4, %YMM4{%k4}{z}
08c3a6
 
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM4, %k1
08c3a6
+	VPTESTN	%YMM4, %YMM4, %k1
08c3a6
 	kmovd	%k1, %ecx
08c3a6
 	subq	$-(VEC_SIZE * 4), %rdi
08c3a6
 	testl	%ecx, %ecx
08c3a6
 	jz	L(loop_4x_vec)
08c3a6
 
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM1, %k0
08c3a6
+	VPTESTN	%YMM1, %YMM1, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	testl	%eax, %eax
08c3a6
 	jnz	L(last_vec_x1)
08c3a6
 
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM2, %k0
08c3a6
+	VPTESTN	%YMM2, %YMM2, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	testl	%eax, %eax
08c3a6
 	jnz	L(last_vec_x2)
08c3a6
 
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM3, %k0
08c3a6
+	VPTESTN	%YMM3, %YMM3, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
 	/* Combine YMM3 matches (eax) with YMM4 matches (ecx).  */
08c3a6
 # ifdef USE_AS_WCSCHR
08c3a6
 	sall	$8, %ecx
08c3a6
 	orl	%ecx, %eax
08c3a6
-	tzcntl	%eax, %eax
08c3a6
+	bsfl	%eax, %eax
08c3a6
 # else
08c3a6
 	salq	$32, %rcx
08c3a6
 	orq	%rcx, %rax
08c3a6
-	tzcntq	%rax, %rax
08c3a6
+	bsfq	%rax, %rax
08c3a6
 # endif
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Check if match was CHAR or null.  */
08c3a6
@@ -303,28 +313,28 @@ L(loop_4x_vec):
08c3a6
 	leaq	(VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
 	ret
08c3a6
 
08c3a6
-# ifndef USE_AS_STRCHRNUL
08c3a6
-L(zero_end):
08c3a6
-	xorl	%eax, %eax
08c3a6
-	ret
08c3a6
+	.p2align 4,, 8
08c3a6
+L(last_vec_x1):
08c3a6
+	bsfl	%eax, %eax
08c3a6
+# ifdef USE_AS_WCSCHR
08c3a6
+	/* NB: Multiply wchar_t count by 4 to get the number of bytes.
08c3a6
+	   */
08c3a6
+	leaq	(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
+# else
08c3a6
+	addq	%rdi, %rax
08c3a6
 # endif
08c3a6
 
08c3a6
-	.p2align 4
08c3a6
-L(last_vec_x1):
08c3a6
-	tzcntl	%eax, %eax
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Check if match was null.  */
08c3a6
-	cmp	(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
+	cmp	(%rax), %CHAR_REG
08c3a6
 	jne	L(zero_end)
08c3a6
 # endif
08c3a6
-	/* NB: Multiply sizeof char type (1 or 4) to get the number of
08c3a6
-	   bytes.  */
08c3a6
-	leaq	(%rdi, %rax, CHAR_SIZE), %rax
08c3a6
+
08c3a6
 	ret
08c3a6
 
08c3a6
-	.p2align 4
08c3a6
+	.p2align 4,, 8
08c3a6
 L(last_vec_x2):
08c3a6
-	tzcntl	%eax, %eax
08c3a6
+	bsfl	%eax, %eax
08c3a6
 # ifndef USE_AS_STRCHRNUL
08c3a6
 	/* Check if match was null.  */
08c3a6
 	cmp	(VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
@@ -336,7 +346,7 @@ L(last_vec_x2):
08c3a6
 	ret
08c3a6
 
08c3a6
 	/* Cold case for crossing page with first load.	 */
08c3a6
-	.p2align 4
08c3a6
+	.p2align 4,, 8
08c3a6
 L(cross_page_boundary):
08c3a6
 	movq	%rdi, %rdx
08c3a6
 	/* Align rdi.  */
08c3a6
@@ -346,9 +356,9 @@ L(cross_page_boundary):
08c3a6
 	vpxorq	%YMM1, %YMM0, %YMM2
08c3a6
 	VPMINU	%YMM2, %YMM1, %YMM2
08c3a6
 	/* Each bit in K0 represents a CHAR or a null byte in YMM1.  */
08c3a6
-	VPCMP	$0, %YMMZERO, %YMM2, %k0
08c3a6
+	VPTESTN	%YMM2, %YMM2, %k0
08c3a6
 	kmovd	%k0, %eax
08c3a6
-	/* Remove the leading bits.	 */
08c3a6
+	/* Remove the leading bits.  */
08c3a6
 # ifdef USE_AS_WCSCHR
08c3a6
 	movl	%edx, %SHIFT_REG
08c3a6
 	/* NB: Divide shift count by 4 since each bit in K1 represent 4
08c3a6
@@ -360,20 +370,24 @@ L(cross_page_boundary):
08c3a6
 	/* If eax is zero continue.  */
08c3a6
 	testl	%eax, %eax
08c3a6
 	jz	L(cross_page_continue)
08c3a6
-	tzcntl	%eax, %eax
08c3a6
-# ifndef USE_AS_STRCHRNUL
08c3a6
-	/* Check to see if match was CHAR or null.  */
08c3a6
-	cmp	(%rdx, %rax, CHAR_SIZE), %CHAR_REG
08c3a6
-	jne	L(zero_end)
08c3a6
-# endif
08c3a6
+	bsfl	%eax, %eax
08c3a6
+
08c3a6
 # ifdef USE_AS_WCSCHR
08c3a6
 	/* NB: Multiply wchar_t count by 4 to get the number of
08c3a6
 	   bytes.  */
08c3a6
 	leaq	(%rdx, %rax, CHAR_SIZE), %rax
08c3a6
 # else
08c3a6
 	addq	%rdx, %rax
08c3a6
+# endif
08c3a6
+# ifndef USE_AS_STRCHRNUL
08c3a6
+	/* Check to see if match was CHAR or null.  */
08c3a6
+	cmp	(%rax), %CHAR_REG
08c3a6
+	je	L(cross_page_ret)
08c3a6
+L(zero_end):
08c3a6
+	xorl	%eax, %eax
08c3a6
+L(cross_page_ret):
08c3a6
 # endif
08c3a6
 	ret
08c3a6
 
08c3a6
 END (STRCHR)
08c3a6
-# endif
08c3a6
+#endif