190885
From 4c0828b81a6d4fd6ca49a4fcef45bb6b479d0d67 Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Fri, 29 Oct 2021 12:40:20 -0700
190885
Subject: [PATCH] x86-64: Improve EVEX strcmp with masked load
190885
190885
In strcmp-evex.S, to compare 2 32-byte strings, replace
190885
190885
        VMOVU   (%rdi, %rdx), %YMM0
190885
        VMOVU   (%rsi, %rdx), %YMM1
190885
        /* Each bit in K0 represents a mismatch in YMM0 and YMM1.  */
190885
        VPCMP   $4, %YMM0, %YMM1, %k0
190885
        VPCMP   $0, %YMMZERO, %YMM0, %k1
190885
        VPCMP   $0, %YMMZERO, %YMM1, %k2
190885
        /* Each bit in K1 represents a NULL in YMM0 or YMM1.  */
190885
        kord    %k1, %k2, %k1
190885
        /* Each bit in K1 represents a NULL or a mismatch.  */
190885
        kord    %k0, %k1, %k1
190885
        kmovd   %k1, %ecx
190885
        testl   %ecx, %ecx
190885
        jne     L(last_vector)
190885
190885
with
190885
190885
        VMOVU   (%rdi, %rdx), %YMM0
190885
        VPTESTM %YMM0, %YMM0, %k2
190885
        /* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
           in YMM0 and 32 bytes at (%rsi, %rdx).  */
190885
        VPCMP   $0, (%rsi, %rdx), %YMM0, %k1{%k2}
190885
        kmovd   %k1, %ecx
190885
        incl    %ecx
190885
        jne     L(last_vector)
190885
190885
It makes EVEX strcmp faster than AVX2 strcmp by up to 40% on Tiger Lake
190885
and Ice Lake.
190885
190885
Co-Authored-By: Noah Goldstein <goldstein.w.n@gmail.com>
190885
(cherry picked from commit c46e9afb2df5fc9e39ff4d13777e4b4c26e04e55)
190885
---
190885
 sysdeps/x86_64/multiarch/strcmp-evex.S | 461 +++++++++++++------------
190885
 1 file changed, 243 insertions(+), 218 deletions(-)
190885
190885
diff --git a/sysdeps/x86_64/multiarch/strcmp-evex.S b/sysdeps/x86_64/multiarch/strcmp-evex.S
190885
index d5aa6daa..82f12ac8 100644
190885
--- a/sysdeps/x86_64/multiarch/strcmp-evex.S
190885
+++ b/sysdeps/x86_64/multiarch/strcmp-evex.S
190885
@@ -41,6 +41,8 @@
190885
 # ifdef USE_AS_WCSCMP
190885
 /* Compare packed dwords.  */
190885
 #  define VPCMP		vpcmpd
190885
+#  define VPMINU	vpminud
190885
+#  define VPTESTM	vptestmd
190885
 #  define SHIFT_REG32	r8d
190885
 #  define SHIFT_REG64	r8
190885
 /* 1 dword char == 4 bytes.  */
190885
@@ -48,6 +50,8 @@
190885
 # else
190885
 /* Compare packed bytes.  */
190885
 #  define VPCMP		vpcmpb
190885
+#  define VPMINU	vpminub
190885
+#  define VPTESTM	vptestmb
190885
 #  define SHIFT_REG32	ecx
190885
 #  define SHIFT_REG64	rcx
190885
 /* 1 byte char == 1 byte.  */
190885
@@ -67,6 +71,9 @@
190885
 # define YMM5		ymm22
190885
 # define YMM6		ymm23
190885
 # define YMM7		ymm24
190885
+# define YMM8		ymm25
190885
+# define YMM9		ymm26
190885
+# define YMM10		ymm27
190885
 
190885
 /* Warning!
190885
            wcscmp/wcsncmp have to use SIGNED comparison for elements.
190885
@@ -76,7 +83,7 @@
190885
 /* The main idea of the string comparison (byte or dword) using 256-bit
190885
    EVEX instructions consists of comparing (VPCMP) two ymm vectors. The
190885
    latter can be on either packed bytes or dwords depending on
190885
-   USE_AS_WCSCMP. In order to check the null char, algorithm keeps the
190885
+   USE_AS_WCSCMP. In order to check the null CHAR, algorithm keeps the
190885
    matched bytes/dwords, requiring 5 EVEX instructions (3 VPCMP and 2
190885
    KORD). In general, the costs of comparing VEC_SIZE bytes (32-bytes)
190885
    are 3 VPCMP and 2 KORD instructions, together with VMOVU and ktestd
190885
@@ -123,27 +130,21 @@ ENTRY (STRCMP)
190885
 	jg	L(cross_page)
190885
 	/* Start comparing 4 vectors.  */
190885
 	VMOVU	(%rdi), %YMM0
190885
-	VMOVU	(%rsi), %YMM1
190885
 
190885
-	/* Each bit in K0 represents a mismatch in YMM0 and YMM1.  */
190885
-	VPCMP	$4, %YMM0, %YMM1, %k0
190885
+	/* Each bit set in K2 represents a non-null CHAR in YMM0.  */
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
 
190885
-	/* Check for NULL in YMM0.  */
190885
-	VPCMP	$0, %YMMZERO, %YMM0, %k1
190885
-	/* Check for NULL in YMM1.  */
190885
-	VPCMP	$0, %YMMZERO, %YMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in YMM0 or YMM1.  */
190885
-	kord	%k1, %k2, %k1
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at (%rsi).  */
190885
+	VPCMP	$0, (%rsi), %YMM0, %k1{%k2}
190885
 
190885
-	/* Each bit in K1 represents:
190885
-	   1. A mismatch in YMM0 and YMM1.  Or
190885
-	   2. A NULL in YMM0 or YMM1.
190885
-	 */
190885
-	kord	%k0, %k1, %k1
190885
-
190885
-	ktestd	%k1, %k1
190885
-	je	L(next_3_vectors)
190885
 	kmovd	%k1, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
+	je	L(next_3_vectors)
190885
 	tzcntl	%ecx, %edx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -172,9 +173,7 @@ L(return):
190885
 # endif
190885
 	ret
190885
 
190885
-	.p2align 4
190885
 L(return_vec_size):
190885
-	kmovd	%k1, %ecx
190885
 	tzcntl	%ecx, %edx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -210,9 +209,7 @@ L(return_vec_size):
190885
 # endif
190885
 	ret
190885
 
190885
-	.p2align 4
190885
 L(return_2_vec_size):
190885
-	kmovd	%k1, %ecx
190885
 	tzcntl	%ecx, %edx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -248,9 +245,7 @@ L(return_2_vec_size):
190885
 # endif
190885
 	ret
190885
 
190885
-	.p2align 4
190885
 L(return_3_vec_size):
190885
-	kmovd	%k1, %ecx
190885
 	tzcntl	%ecx, %edx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -289,43 +284,45 @@ L(return_3_vec_size):
190885
 	.p2align 4
190885
 L(next_3_vectors):
190885
 	VMOVU	VEC_SIZE(%rdi), %YMM0
190885
-	VMOVU	VEC_SIZE(%rsi), %YMM1
190885
-	/* Each bit in K0 represents a mismatch in YMM0 and YMM1.  */
190885
-	VPCMP	$4, %YMM0, %YMM1, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM0, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in YMM0 or YMM1.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
-	ktestd	%k1, %k1
190885
+	/* Each bit set in K2 represents a non-null CHAR in YMM0.  */
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at VEC_SIZE(%rsi).  */
190885
+	VPCMP	$0, VEC_SIZE(%rsi), %YMM0, %k1{%k2}
190885
+	kmovd	%k1, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	jne	L(return_vec_size)
190885
 
190885
-	VMOVU	(VEC_SIZE * 2)(%rdi), %YMM2
190885
-	VMOVU	(VEC_SIZE * 3)(%rdi), %YMM3
190885
-	VMOVU	(VEC_SIZE * 2)(%rsi), %YMM4
190885
-	VMOVU	(VEC_SIZE * 3)(%rsi), %YMM5
190885
-
190885
-	/* Each bit in K0 represents a mismatch in YMM2 and YMM4.  */
190885
-	VPCMP	$4, %YMM2, %YMM4, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM2, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM4, %k2
190885
-	/* Each bit in K1 represents a NULL in YMM2 or YMM4.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
-	ktestd	%k1, %k1
190885
+	VMOVU	(VEC_SIZE * 2)(%rdi), %YMM0
190885
+	/* Each bit set in K2 represents a non-null CHAR in YMM0.  */
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at (VEC_SIZE * 2)(%rsi).  */
190885
+	VPCMP	$0, (VEC_SIZE * 2)(%rsi), %YMM0, %k1{%k2}
190885
+	kmovd	%k1, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	jne	L(return_2_vec_size)
190885
 
190885
-	/* Each bit in K0 represents a mismatch in YMM3 and YMM5.  */
190885
-	VPCMP	$4, %YMM3, %YMM5, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM3, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM5, %k2
190885
-	/* Each bit in K1 represents a NULL in YMM3 or YMM5.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
-	ktestd	%k1, %k1
190885
+	VMOVU	(VEC_SIZE * 3)(%rdi), %YMM0
190885
+	/* Each bit set in K2 represents a non-null CHAR in YMM0.  */
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at (VEC_SIZE * 2)(%rsi).  */
190885
+	VPCMP	$0, (VEC_SIZE * 3)(%rsi), %YMM0, %k1{%k2}
190885
+	kmovd	%k1, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	jne	L(return_3_vec_size)
190885
 L(main_loop_header):
190885
 	leaq	(VEC_SIZE * 4)(%rdi), %rdx
190885
@@ -375,56 +372,51 @@ L(back_to_loop):
190885
 	VMOVA	VEC_SIZE(%rax), %YMM2
190885
 	VMOVA	(VEC_SIZE * 2)(%rax), %YMM4
190885
 	VMOVA	(VEC_SIZE * 3)(%rax), %YMM6
190885
-	VMOVU	(%rdx), %YMM1
190885
-	VMOVU	VEC_SIZE(%rdx), %YMM3
190885
-	VMOVU	(VEC_SIZE * 2)(%rdx), %YMM5
190885
-	VMOVU	(VEC_SIZE * 3)(%rdx), %YMM7
190885
-
190885
-	VPCMP	$4, %YMM0, %YMM1, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM0, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM1, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K4 represents a NULL or a mismatch in YMM0 and
190885
-	   YMM1.  */
190885
-	kord	%k0, %k1, %k4
190885
-
190885
-	VPCMP	$4, %YMM2, %YMM3, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM2, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM3, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K5 represents a NULL or a mismatch in YMM2 and
190885
-	   YMM3.  */
190885
-	kord	%k0, %k1, %k5
190885
-
190885
-	VPCMP	$4, %YMM4, %YMM5, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM4, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM5, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K6 represents a NULL or a mismatch in YMM4 and
190885
-	   YMM5.  */
190885
-	kord	%k0, %k1, %k6
190885
-
190885
-	VPCMP	$4, %YMM6, %YMM7, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM6, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM7, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K7 represents a NULL or a mismatch in YMM6 and
190885
-	   YMM7.  */
190885
-	kord	%k0, %k1, %k7
190885
-
190885
-	kord	%k4, %k5, %k0
190885
-	kord	%k6, %k7, %k1
190885
-
190885
-	/* Test each mask (32 bits) individually because for VEC_SIZE
190885
-	   == 32 is not possible to OR the four masks and keep all bits
190885
-	   in a 64-bit integer register, differing from SSE2 strcmp
190885
-	   where ORing is possible.  */
190885
-	kortestd %k0, %k1
190885
-	je	L(loop)
190885
-	ktestd	%k4, %k4
190885
+
190885
+	VPMINU	%YMM0, %YMM2, %YMM8
190885
+	VPMINU	%YMM4, %YMM6, %YMM9
190885
+
190885
+	/* A zero CHAR in YMM8 means that there is a null CHAR.  */
190885
+	VPMINU	%YMM8, %YMM9, %YMM8
190885
+
190885
+	/* Each bit set in K1 represents a non-null CHAR in YMM8.  */
190885
+	VPTESTM	%YMM8, %YMM8, %k1
190885
+
190885
+	/* (YMM ^ YMM): A non-zero CHAR represents a mismatch.  */
190885
+	vpxorq	(%rdx), %YMM0, %YMM1
190885
+	vpxorq	VEC_SIZE(%rdx), %YMM2, %YMM3
190885
+	vpxorq	(VEC_SIZE * 2)(%rdx), %YMM4, %YMM5
190885
+	vpxorq	(VEC_SIZE * 3)(%rdx), %YMM6, %YMM7
190885
+
190885
+	vporq	%YMM1, %YMM3, %YMM9
190885
+	vporq	%YMM5, %YMM7, %YMM10
190885
+
190885
+	/* A non-zero CHAR in YMM9 represents a mismatch.  */
190885
+	vporq	%YMM9, %YMM10, %YMM9
190885
+
190885
+	/* Each bit cleared in K0 represents a mismatch or a null CHAR.  */
190885
+	VPCMP	$0, %YMMZERO, %YMM9, %k0{%k1}
190885
+	kmovd   %k0, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
+	je	 L(loop)
190885
+
190885
+	/* Each bit set in K1 represents a non-null CHAR in YMM0.  */
190885
+	VPTESTM	%YMM0, %YMM0, %k1
190885
+	/* Each bit cleared in K0 represents a mismatch or a null CHAR
190885
+	   in YMM0 and (%rdx).  */
190885
+	VPCMP	$0, %YMMZERO, %YMM1, %k0{%k1}
190885
+	kmovd	%k0, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	je	L(test_vec)
190885
-	kmovd	%k4, %edi
190885
-	tzcntl	%edi, %ecx
190885
+	tzcntl	%ecx, %ecx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
 	sall	$2, %ecx
190885
@@ -466,9 +458,18 @@ L(test_vec):
190885
 	cmpq	$VEC_SIZE, %r11
190885
 	jbe	L(zero)
190885
 # endif
190885
-	ktestd	%k5, %k5
190885
+	/* Each bit set in K1 represents a non-null CHAR in YMM2.  */
190885
+	VPTESTM	%YMM2, %YMM2, %k1
190885
+	/* Each bit cleared in K0 represents a mismatch or a null CHAR
190885
+	   in YMM2 and VEC_SIZE(%rdx).  */
190885
+	VPCMP	$0, %YMMZERO, %YMM3, %k0{%k1}
190885
+	kmovd	%k0, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	je	L(test_2_vec)
190885
-	kmovd	%k5, %ecx
190885
 	tzcntl	%ecx, %edi
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -512,9 +513,18 @@ L(test_2_vec):
190885
 	cmpq	$(VEC_SIZE * 2), %r11
190885
 	jbe	L(zero)
190885
 # endif
190885
-	ktestd	%k6, %k6
190885
+	/* Each bit set in K1 represents a non-null CHAR in YMM4.  */
190885
+	VPTESTM	%YMM4, %YMM4, %k1
190885
+	/* Each bit cleared in K0 represents a mismatch or a null CHAR
190885
+	   in YMM4 and (VEC_SIZE * 2)(%rdx).  */
190885
+	VPCMP	$0, %YMMZERO, %YMM5, %k0{%k1}
190885
+	kmovd	%k0, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	je	L(test_3_vec)
190885
-	kmovd	%k6, %ecx
190885
 	tzcntl	%ecx, %edi
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
@@ -558,8 +568,18 @@ L(test_3_vec):
190885
 	cmpq	$(VEC_SIZE * 3), %r11
190885
 	jbe	L(zero)
190885
 # endif
190885
-	kmovd	%k7, %esi
190885
-	tzcntl	%esi, %ecx
190885
+	/* Each bit set in K1 represents a non-null CHAR in YMM6.  */
190885
+	VPTESTM	%YMM6, %YMM6, %k1
190885
+	/* Each bit cleared in K0 represents a mismatch or a null CHAR
190885
+	   in YMM6 and (VEC_SIZE * 3)(%rdx).  */
190885
+	VPCMP	$0, %YMMZERO, %YMM7, %k0{%k1}
190885
+	kmovd	%k0, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
+	tzcntl	%ecx, %ecx
190885
 # ifdef USE_AS_WCSCMP
190885
 	/* NB: Multiply wchar_t count by 4 to get the number of bytes.  */
190885
 	sall	$2, %ecx
190885
@@ -615,39 +635,51 @@ L(loop_cross_page):
190885
 
190885
 	VMOVU	(%rax, %r10), %YMM2
190885
 	VMOVU	VEC_SIZE(%rax, %r10), %YMM3
190885
-	VMOVU	(%rdx, %r10), %YMM4
190885
-	VMOVU	VEC_SIZE(%rdx, %r10), %YMM5
190885
-
190885
-	VPCMP	$4, %YMM4, %YMM2, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM2, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM4, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch in YMM2 and
190885
-	   YMM4.  */
190885
-	kord	%k0, %k1, %k1
190885
-
190885
-	VPCMP	$4, %YMM5, %YMM3, %k3
190885
-	VPCMP	$0, %YMMZERO, %YMM3, %k4
190885
-	VPCMP	$0, %YMMZERO, %YMM5, %k5
190885
-	kord	%k4, %k5, %k4
190885
-	/* Each bit in K3 represents a NULL or a mismatch in YMM3 and
190885
-	   YMM5.  */
190885
-	kord	%k3, %k4, %k3
190885
+
190885
+	/* Each bit set in K2 represents a non-null CHAR in YMM2.  */
190885
+	VPTESTM	%YMM2, %YMM2, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM2 and 32 bytes at (%rdx, %r10).  */
190885
+	VPCMP	$0, (%rdx, %r10), %YMM2, %k1{%k2}
190885
+	kmovd	%k1, %r9d
190885
+	/* Don't use subl since it is the lower 16/32 bits of RDI
190885
+	   below.  */
190885
+	notl	%r9d
190885
+# ifdef USE_AS_WCSCMP
190885
+	/* Only last 8 bits are valid.  */
190885
+	andl	$0xff, %r9d
190885
+# endif
190885
+
190885
+	/* Each bit set in K4 represents a non-null CHAR in YMM3.  */
190885
+	VPTESTM	%YMM3, %YMM3, %k4
190885
+	/* Each bit cleared in K3 represents a mismatch or a null CHAR
190885
+	   in YMM3 and 32 bytes at VEC_SIZE(%rdx, %r10).  */
190885
+	VPCMP	$0, VEC_SIZE(%rdx, %r10), %YMM3, %k3{%k4}
190885
+	kmovd	%k3, %edi
190885
+# ifdef USE_AS_WCSCMP
190885
+	/* Don't use subl since it is the upper 8 bits of EDI below.  */
190885
+	notl	%edi
190885
+	andl	$0xff, %edi
190885
+# else
190885
+	incl	%edi
190885
+# endif
190885
 
190885
 # ifdef USE_AS_WCSCMP
190885
-	/* NB: Each bit in K1/K3 represents 4-byte element.  */
190885
-	kshiftlw $8, %k3, %k2
190885
+	/* NB: Each bit in EDI/R9D represents 4-byte element.  */
190885
+	sall	$8, %edi
190885
 	/* NB: Divide shift count by 4 since each bit in K1 represent 4
190885
 	   bytes.  */
190885
 	movl	%ecx, %SHIFT_REG32
190885
 	sarl	$2, %SHIFT_REG32
190885
+
190885
+	/* Each bit in EDI represents a null CHAR or a mismatch.  */
190885
+	orl	%r9d, %edi
190885
 # else
190885
-	kshiftlq $32, %k3, %k2
190885
-# endif
190885
+	salq	$32, %rdi
190885
 
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	korq	%k1, %k2, %k1
190885
-	kmovq	%k1, %rdi
190885
+	/* Each bit in RDI represents a null CHAR or a mismatch.  */
190885
+	orq	%r9, %rdi
190885
+# endif
190885
 
190885
 	/* Since ECX < VEC_SIZE * 2, simply skip the first ECX bytes.  */
190885
 	shrxq	%SHIFT_REG64, %rdi, %rdi
190885
@@ -692,35 +724,45 @@ L(loop_cross_page_2_vec):
190885
 	/* The first VEC_SIZE * 2 bytes match or are ignored.  */
190885
 	VMOVU	(VEC_SIZE * 2)(%rax, %r10), %YMM0
190885
 	VMOVU	(VEC_SIZE * 3)(%rax, %r10), %YMM1
190885
-	VMOVU	(VEC_SIZE * 2)(%rdx, %r10), %YMM2
190885
-	VMOVU	(VEC_SIZE * 3)(%rdx, %r10), %YMM3
190885
-
190885
-	VPCMP	$4, %YMM0, %YMM2, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM0, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM2, %k2
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch in YMM0 and
190885
-	   YMM2.  */
190885
-	kord	%k0, %k1, %k1
190885
-
190885
-	VPCMP	$4, %YMM1, %YMM3, %k3
190885
-	VPCMP	$0, %YMMZERO, %YMM1, %k4
190885
-	VPCMP	$0, %YMMZERO, %YMM3, %k5
190885
-	kord	%k4, %k5, %k4
190885
-	/* Each bit in K3 represents a NULL or a mismatch in YMM1 and
190885
-	   YMM3.  */
190885
-	kord	%k3, %k4, %k3
190885
 
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at (VEC_SIZE * 2)(%rdx, %r10).  */
190885
+	VPCMP	$0, (VEC_SIZE * 2)(%rdx, %r10), %YMM0, %k1{%k2}
190885
+	kmovd	%k1, %r9d
190885
+	/* Don't use subl since it is the lower 16/32 bits of RDI
190885
+	   below.  */
190885
+	notl	%r9d
190885
 # ifdef USE_AS_WCSCMP
190885
-	/* NB: Each bit in K1/K3 represents 4-byte element.  */
190885
-	kshiftlw $8, %k3, %k2
190885
+	/* Only last 8 bits are valid.  */
190885
+	andl	$0xff, %r9d
190885
+# endif
190885
+
190885
+	VPTESTM	%YMM1, %YMM1, %k4
190885
+	/* Each bit cleared in K3 represents a mismatch or a null CHAR
190885
+	   in YMM1 and 32 bytes at (VEC_SIZE * 3)(%rdx, %r10).  */
190885
+	VPCMP	$0, (VEC_SIZE * 3)(%rdx, %r10), %YMM1, %k3{%k4}
190885
+	kmovd	%k3, %edi
190885
+# ifdef USE_AS_WCSCMP
190885
+	/* Don't use subl since it is the upper 8 bits of EDI below.  */
190885
+	notl	%edi
190885
+	andl	$0xff, %edi
190885
 # else
190885
-	kshiftlq $32, %k3, %k2
190885
+	incl	%edi
190885
 # endif
190885
 
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	korq	%k1, %k2, %k1
190885
-	kmovq	%k1, %rdi
190885
+# ifdef USE_AS_WCSCMP
190885
+	/* NB: Each bit in EDI/R9D represents 4-byte element.  */
190885
+	sall	$8, %edi
190885
+
190885
+	/* Each bit in EDI represents a null CHAR or a mismatch.  */
190885
+	orl	%r9d, %edi
190885
+# else
190885
+	salq	$32, %rdi
190885
+
190885
+	/* Each bit in RDI represents a null CHAR or a mismatch.  */
190885
+	orq	%r9, %rdi
190885
+# endif
190885
 
190885
 	xorl	%r8d, %r8d
190885
 	/* If ECX > VEC_SIZE * 2, skip ECX - (VEC_SIZE * 2) bytes.  */
190885
@@ -729,12 +771,15 @@ L(loop_cross_page_2_vec):
190885
 	/* R8 has number of bytes skipped.  */
190885
 	movl	%ecx, %r8d
190885
 # ifdef USE_AS_WCSCMP
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 RDI represent 4
190885
 	   bytes.  */
190885
 	sarl	$2, %ecx
190885
-# endif
190885
+	/* Skip ECX bytes.  */
190885
+	shrl	%cl, %edi
190885
+# else
190885
 	/* Skip ECX bytes.  */
190885
 	shrq	%cl, %rdi
190885
+# endif
190885
 1:
190885
 	/* Before jumping back to the loop, set ESI to the number of
190885
 	   VEC_SIZE * 4 blocks before page crossing.  */
190885
@@ -818,7 +863,7 @@ L(cross_page_loop):
190885
 	movzbl	(%rdi, %rdx), %eax
190885
 	movzbl	(%rsi, %rdx), %ecx
190885
 # endif
190885
-	/* Check null char.  */
190885
+	/* Check null CHAR.  */
190885
 	testl	%eax, %eax
190885
 	jne	L(cross_page_loop)
190885
 	/* Since %eax == 0, subtract is OK for both SIGNED and UNSIGNED
190885
@@ -901,18 +946,17 @@ L(cross_page):
190885
 	jg	L(cross_page_1_vector)
190885
 L(loop_1_vector):
190885
 	VMOVU	(%rdi, %rdx), %YMM0
190885
-	VMOVU	(%rsi, %rdx), %YMM1
190885
-
190885
-	/* Each bit in K0 represents a mismatch in YMM0 and YMM1.  */
190885
-	VPCMP	$4, %YMM0, %YMM1, %k0
190885
-	VPCMP	$0, %YMMZERO, %YMM0, %k1
190885
-	VPCMP	$0, %YMMZERO, %YMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in YMM0 or YMM1.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
+
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in YMM0 and 32 bytes at (%rsi, %rdx).  */
190885
+	VPCMP	$0, (%rsi, %rdx), %YMM0, %k1{%k2}
190885
 	kmovd	%k1, %ecx
190885
-	testl	%ecx, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xff, %ecx
190885
+# else
190885
+	incl	%ecx
190885
+# endif
190885
 	jne	L(last_vector)
190885
 
190885
 	addl	$VEC_SIZE, %edx
190885
@@ -931,18 +975,17 @@ L(cross_page_1_vector):
190885
 	cmpl	$(PAGE_SIZE - 16), %eax
190885
 	jg	L(cross_page_1_xmm)
190885
 	VMOVU	(%rdi, %rdx), %XMM0
190885
-	VMOVU	(%rsi, %rdx), %XMM1
190885
-
190885
-	/* Each bit in K0 represents a mismatch in XMM0 and XMM1.  */
190885
-	VPCMP	$4, %XMM0, %XMM1, %k0
190885
-	VPCMP	$0, %XMMZERO, %XMM0, %k1
190885
-	VPCMP	$0, %XMMZERO, %XMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in XMM0 or XMM1.  */
190885
-	korw	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	korw	%k0, %k1, %k1
190885
-	kmovw	%k1, %ecx
190885
-	testl	%ecx, %ecx
190885
+
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in XMM0 and 16 bytes at (%rsi, %rdx).  */
190885
+	VPCMP	$0, (%rsi, %rdx), %XMM0, %k1{%k2}
190885
+	kmovd	%k1, %ecx
190885
+# ifdef USE_AS_WCSCMP
190885
+	subl	$0xf, %ecx
190885
+# else
190885
+	subl	$0xffff, %ecx
190885
+# endif
190885
 	jne	L(last_vector)
190885
 
190885
 	addl	$16, %edx
190885
@@ -965,25 +1008,16 @@ L(cross_page_1_xmm):
190885
 	vmovq	(%rdi, %rdx), %XMM0
190885
 	vmovq	(%rsi, %rdx), %XMM1
190885
 
190885
-	/* Each bit in K0 represents a mismatch in XMM0 and XMM1.  */
190885
-	VPCMP	$4, %XMM0, %XMM1, %k0
190885
-	VPCMP	$0, %XMMZERO, %XMM0, %k1
190885
-	VPCMP	$0, %XMMZERO, %XMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in XMM0 or XMM1.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
-	kmovd	%k1, %ecx
190885
-
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in XMM0 and XMM1.  */
190885
+	VPCMP	$0, %XMM1, %XMM0, %k1{%k2}
190885
+	kmovb	%k1, %ecx
190885
 # ifdef USE_AS_WCSCMP
190885
-	/* Only last 2 bits are valid.  */
190885
-	andl	$0x3, %ecx
190885
+	subl	$0x3, %ecx
190885
 # else
190885
-	/* Only last 8 bits are valid.  */
190885
-	andl	$0xff, %ecx
190885
+	subl	$0xff, %ecx
190885
 # endif
190885
-
190885
-	testl	%ecx, %ecx
190885
 	jne	L(last_vector)
190885
 
190885
 	addl	$8, %edx
190885
@@ -1002,25 +1036,16 @@ L(cross_page_8bytes):
190885
 	vmovd	(%rdi, %rdx), %XMM0
190885
 	vmovd	(%rsi, %rdx), %XMM1
190885
 
190885
-	/* Each bit in K0 represents a mismatch in XMM0 and XMM1.  */
190885
-	VPCMP	$4, %XMM0, %XMM1, %k0
190885
-	VPCMP	$0, %XMMZERO, %XMM0, %k1
190885
-	VPCMP	$0, %XMMZERO, %XMM1, %k2
190885
-	/* Each bit in K1 represents a NULL in XMM0 or XMM1.  */
190885
-	kord	%k1, %k2, %k1
190885
-	/* Each bit in K1 represents a NULL or a mismatch.  */
190885
-	kord	%k0, %k1, %k1
190885
+	VPTESTM	%YMM0, %YMM0, %k2
190885
+	/* Each bit cleared in K1 represents a mismatch or a null CHAR
190885
+	   in XMM0 and XMM1.  */
190885
+	VPCMP	$0, %XMM1, %XMM0, %k1{%k2}
190885
 	kmovd	%k1, %ecx
190885
-
190885
 # ifdef USE_AS_WCSCMP
190885
-	/* Only the last bit is valid.  */
190885
-	andl	$0x1, %ecx
190885
+	subl	$0x1, %ecx
190885
 # else
190885
-	/* Only last 4 bits are valid.  */
190885
-	andl	$0xf, %ecx
190885
+	subl	$0xf, %ecx
190885
 # endif
190885
-
190885
-	testl	%ecx, %ecx
190885
 	jne	L(last_vector)
190885
 
190885
 	addl	$4, %edx
190885
-- 
190885
GitLab
190885