076f82
commit b13a2e68eb3b84f2a7b587132ec2ea813815febf
076f82
Author: Noah Goldstein <goldstein.w.n@gmail.com>
076f82
Date:   Thu Mar 24 18:56:13 2022 -0500
076f82
076f82
    x86: Add EVEX optimized str{n}casecmp
076f82
    
076f82
    geometric_mean(N=40) of all benchmarks EVEX / SSE42: .621
076f82
    
076f82
    All string/memory tests pass.
076f82
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
076f82
    
076f82
    (cherry picked from commit 84e7c46df4086873eae28a1fb87d2cf5388b1e16)
076f82
076f82
diff --git a/sysdeps/x86_64/multiarch/Makefile b/sysdeps/x86_64/multiarch/Makefile
076f82
index 711ecf2ee45d61b9..359712c1491a2431 100644
076f82
--- a/sysdeps/x86_64/multiarch/Makefile
076f82
+++ b/sysdeps/x86_64/multiarch/Makefile
076f82
@@ -53,6 +53,7 @@ sysdep_routines += \
076f82
   strcasecmp_l-avx \
076f82
   strcasecmp_l-avx2 \
076f82
   strcasecmp_l-avx2-rtm \
076f82
+  strcasecmp_l-evex \
076f82
   strcasecmp_l-sse2 \
076f82
   strcasecmp_l-sse4_2 \
076f82
   strcasecmp_l-ssse3 \
076f82
@@ -93,6 +94,7 @@ sysdep_routines += \
076f82
   strncase_l-avx \
076f82
   strncase_l-avx2 \
076f82
   strncase_l-avx2-rtm \
076f82
+  strncase_l-evex \
076f82
   strncase_l-sse2 \
076f82
   strncase_l-sse4_2 \
076f82
   strncase_l-ssse3 \
076f82
diff --git a/sysdeps/x86_64/multiarch/ifunc-impl-list.c b/sysdeps/x86_64/multiarch/ifunc-impl-list.c
076f82
index a687b387c91aa9ae..f6994e5406933d53 100644
076f82
--- a/sysdeps/x86_64/multiarch/ifunc-impl-list.c
076f82
+++ b/sysdeps/x86_64/multiarch/ifunc-impl-list.c
076f82
@@ -418,6 +418,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
076f82
 
076f82
   /* Support sysdeps/x86_64/multiarch/strcasecmp_l.c.  */
076f82
   IFUNC_IMPL (i, name, strcasecmp,
076f82
+	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX512VL)
076f82
+			       && CPU_FEATURE_USABLE (AVX512BW)),
076f82
+			      __strcasecmp_evex)
076f82
 	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX2),
076f82
 			      __strcasecmp_avx2)
076f82
@@ -438,6 +442,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
076f82
 
076f82
   /* Support sysdeps/x86_64/multiarch/strcasecmp_l.c.  */
076f82
   IFUNC_IMPL (i, name, strcasecmp_l,
076f82
+	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX512VL)
076f82
+			       && CPU_FEATURE_USABLE (AVX512BW)),
076f82
+			      __strcasecmp_l_evex)
076f82
 	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX2),
076f82
 			      __strcasecmp_l_avx2)
076f82
@@ -572,6 +580,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
076f82
 
076f82
   /* Support sysdeps/x86_64/multiarch/strncase_l.c.  */
076f82
   IFUNC_IMPL (i, name, strncasecmp,
076f82
+	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX512VL)
076f82
+			       && CPU_FEATURE_USABLE (AVX512BW)),
076f82
+			      __strncasecmp_evex)
076f82
 	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX2),
076f82
 			      __strncasecmp_avx2)
076f82
@@ -593,6 +605,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
076f82
 
076f82
   /* Support sysdeps/x86_64/multiarch/strncase_l.c.  */
076f82
   IFUNC_IMPL (i, name, strncasecmp_l,
076f82
+	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX512VL)
076f82
+			       && CPU_FEATURE_USABLE (AVX512BW)),
076f82
+			      __strncasecmp_l_evex)
076f82
 	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX2),
076f82
 			      __strncasecmp_l_avx2)
076f82
diff --git a/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h b/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
index 64d0cd6ef25f73c0..488e99e4997f379b 100644
076f82
--- a/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
+++ b/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
@@ -25,6 +25,7 @@ extern __typeof (REDIRECT_NAME) OPTIMIZE (sse42) attribute_hidden;
076f82
 extern __typeof (REDIRECT_NAME) OPTIMIZE (avx) attribute_hidden;
076f82
 extern __typeof (REDIRECT_NAME) OPTIMIZE (avx2) attribute_hidden;
076f82
 extern __typeof (REDIRECT_NAME) OPTIMIZE (avx2_rtm) attribute_hidden;
076f82
+extern __typeof (REDIRECT_NAME) OPTIMIZE (evex) attribute_hidden;
076f82
 
076f82
 static inline void *
076f82
 IFUNC_SELECTOR (void)
076f82
@@ -34,6 +35,10 @@ IFUNC_SELECTOR (void)
076f82
   if (CPU_FEATURE_USABLE_P (cpu_features, AVX2)
076f82
       && CPU_FEATURES_ARCH_P (cpu_features, AVX_Fast_Unaligned_Load))
076f82
     {
076f82
+      if (CPU_FEATURE_USABLE_P (cpu_features, AVX512VL)
076f82
+          && CPU_FEATURE_USABLE_P (cpu_features, AVX512BW))
076f82
+        return OPTIMIZE (evex);
076f82
+
076f82
       if (CPU_FEATURE_USABLE_P (cpu_features, RTM))
076f82
         return OPTIMIZE (avx2_rtm);
076f82
 
076f82
diff --git a/sysdeps/x86_64/multiarch/strcasecmp_l-evex.S b/sysdeps/x86_64/multiarch/strcasecmp_l-evex.S
076f82
new file mode 100644
076f82
index 0000000000000000..58642db748e3db71
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strcasecmp_l-evex.S
076f82
@@ -0,0 +1,23 @@
076f82
+/* strcasecmp_l optimized with EVEX.
076f82
+   Copyright (C) 2017-2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#ifndef STRCMP
076f82
+# define STRCMP	__strcasecmp_l_evex
076f82
+#endif
076f82
+#define USE_AS_STRCASECMP_L
076f82
+#include "strcmp-evex.S"
076f82
diff --git a/sysdeps/x86_64/multiarch/strcmp-evex.S b/sysdeps/x86_64/multiarch/strcmp-evex.S
076f82
index 0dfa62bd149c02b4..b81b57753c38db1f 100644
076f82
--- a/sysdeps/x86_64/multiarch/strcmp-evex.S
076f82
+++ b/sysdeps/x86_64/multiarch/strcmp-evex.S
076f82
@@ -19,6 +19,9 @@
076f82
 #if IS_IN (libc)
076f82
 
076f82
 # include <sysdep.h>
076f82
+# if defined USE_AS_STRCASECMP_L
076f82
+#  include "locale-defines.h"
076f82
+# endif
076f82
 
076f82
 # ifndef STRCMP
076f82
 #  define STRCMP	__strcmp_evex
076f82
@@ -34,19 +37,29 @@
076f82
 # define VMOVA	vmovdqa64
076f82
 
076f82
 # ifdef USE_AS_WCSCMP
076f82
-#  define TESTEQ	subl	$0xff,
076f82
+#  ifndef OVERFLOW_STRCMP
076f82
+#   define OVERFLOW_STRCMP	__wcscmp_evex
076f82
+#  endif
076f82
+
076f82
+#  define TESTEQ	subl $0xff,
076f82
 	/* Compare packed dwords.  */
076f82
 #  define VPCMP	vpcmpd
076f82
 #  define VPMINU	vpminud
076f82
 #  define VPTESTM	vptestmd
076f82
+#  define VPTESTNM	vptestnmd
076f82
 	/* 1 dword char == 4 bytes.  */
076f82
 #  define SIZE_OF_CHAR	4
076f82
 # else
076f82
+#  ifndef OVERFLOW_STRCMP
076f82
+#   define OVERFLOW_STRCMP	__strcmp_evex
076f82
+#  endif
076f82
+
076f82
 #  define TESTEQ	incl
076f82
 	/* Compare packed bytes.  */
076f82
 #  define VPCMP	vpcmpb
076f82
 #  define VPMINU	vpminub
076f82
 #  define VPTESTM	vptestmb
076f82
+#  define VPTESTNM	vptestnmb
076f82
 	/* 1 byte char == 1 byte.  */
076f82
 #  define SIZE_OF_CHAR	1
076f82
 # endif
076f82
@@ -73,11 +86,16 @@
076f82
 #  define VEC_OFFSET	(-VEC_SIZE)
076f82
 # endif
076f82
 
076f82
-# define XMMZERO	xmm16
076f82
 # define XMM0	xmm17
076f82
 # define XMM1	xmm18
076f82
 
076f82
-# define YMMZERO	ymm16
076f82
+# define XMM10	xmm27
076f82
+# define XMM11	xmm28
076f82
+# define XMM12	xmm29
076f82
+# define XMM13	xmm30
076f82
+# define XMM14	xmm31
076f82
+
076f82
+
076f82
 # define YMM0	ymm17
076f82
 # define YMM1	ymm18
076f82
 # define YMM2	ymm19
076f82
@@ -89,6 +107,87 @@
076f82
 # define YMM8	ymm25
076f82
 # define YMM9	ymm26
076f82
 # define YMM10	ymm27
076f82
+# define YMM11	ymm28
076f82
+# define YMM12	ymm29
076f82
+# define YMM13	ymm30
076f82
+# define YMM14	ymm31
076f82
+
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+#  define BYTE_LOOP_REG	OFFSET_REG
076f82
+# else
076f82
+#  define BYTE_LOOP_REG	ecx
076f82
+# endif
076f82
+
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+#  ifdef USE_AS_STRNCMP
076f82
+#   define STRCASECMP	__strncasecmp_evex
076f82
+#   define LOCALE_REG	rcx
076f82
+#   define LOCALE_REG_LP	RCX_LP
076f82
+#   define STRCASECMP_NONASCII	__strncasecmp_l_nonascii
076f82
+#  else
076f82
+#   define STRCASECMP	__strcasecmp_evex
076f82
+#   define LOCALE_REG	rdx
076f82
+#   define LOCALE_REG_LP	RDX_LP
076f82
+#   define STRCASECMP_NONASCII	__strcasecmp_l_nonascii
076f82
+#  endif
076f82
+# endif
076f82
+
076f82
+# define LCASE_MIN_YMM	%YMM12
076f82
+# define LCASE_MAX_YMM	%YMM13
076f82
+# define CASE_ADD_YMM	%YMM14
076f82
+
076f82
+# define LCASE_MIN_XMM	%XMM12
076f82
+# define LCASE_MAX_XMM	%XMM13
076f82
+# define CASE_ADD_XMM	%XMM14
076f82
+
076f82
+	/* NB: wcsncmp uses r11 but strcasecmp is never used in
076f82
+	   conjunction with wcscmp.  */
076f82
+# define TOLOWER_BASE	%r11
076f82
+
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+#  define _REG(x, y) x ## y
076f82
+#  define REG(x, y) _REG(x, y)
076f82
+#  define TOLOWER(reg1, reg2, ext)										\
076f82
+	vpsubb	REG(LCASE_MIN_, ext), reg1, REG(%ext, 10);					\
076f82
+	vpsubb	REG(LCASE_MIN_, ext), reg2, REG(%ext, 11);					\
076f82
+	vpcmpub	$1, REG(LCASE_MAX_, ext), REG(%ext, 10), %k5;				\
076f82
+	vpcmpub	$1, REG(LCASE_MAX_, ext), REG(%ext, 11), %k6;				\
076f82
+	vpaddb	reg1, REG(CASE_ADD_, ext), reg1{%k5};						\
076f82
+	vpaddb	reg2, REG(CASE_ADD_, ext), reg2{%k6}
076f82
+
076f82
+#  define TOLOWER_gpr(src, dst) movl (TOLOWER_BASE, src, 4), dst
076f82
+#  define TOLOWER_YMM(...)	TOLOWER(__VA_ARGS__, YMM)
076f82
+#  define TOLOWER_XMM(...)	TOLOWER(__VA_ARGS__, XMM)
076f82
+
076f82
+#  define CMP_R1_R2(s1_reg, s2_reg, reg_out, ext)						\
076f82
+	TOLOWER	(s1_reg, s2_reg, ext);										\
076f82
+	VPCMP	$0, s1_reg, s2_reg, reg_out
076f82
+
076f82
+#  define CMP_R1_S2(s1_reg, s2_mem, s2_reg, reg_out, ext)				\
076f82
+	VMOVU	s2_mem, s2_reg;												\
076f82
+	CMP_R1_R2(s1_reg, s2_reg, reg_out, ext)
076f82
+
076f82
+#  define CMP_R1_R2_YMM(...) CMP_R1_R2(__VA_ARGS__, YMM)
076f82
+#  define CMP_R1_R2_XMM(...) CMP_R1_R2(__VA_ARGS__, XMM)
076f82
+
076f82
+#  define CMP_R1_S2_YMM(...) CMP_R1_S2(__VA_ARGS__, YMM)
076f82
+#  define CMP_R1_S2_XMM(...) CMP_R1_S2(__VA_ARGS__, XMM)
076f82
+
076f82
+# else
076f82
+#  define TOLOWER_gpr(...)
076f82
+#  define TOLOWER_YMM(...)
076f82
+#  define TOLOWER_XMM(...)
076f82
+
076f82
+#  define CMP_R1_R2_YMM(s1_reg, s2_reg, reg_out)						\
076f82
+	VPCMP	$0, s2_reg, s1_reg, reg_out
076f82
+
076f82
+#  define CMP_R1_R2_XMM(...) CMP_R1_R2_YMM(__VA_ARGS__)
076f82
+
076f82
+#  define CMP_R1_S2_YMM(s1_reg, s2_mem, unused, reg_out)				\
076f82
+	VPCMP	$0, s2_mem, s1_reg, reg_out
076f82
+
076f82
+#  define CMP_R1_S2_XMM(...) CMP_R1_S2_YMM(__VA_ARGS__)
076f82
+# endif
076f82
 
076f82
 /* Warning!
076f82
            wcscmp/wcsncmp have to use SIGNED comparison for elements.
076f82
@@ -112,8 +211,45 @@
076f82
    returned.  */
076f82
 
076f82
 	.section .text.evex, "ax", @progbits
076f82
-ENTRY(STRCMP)
076f82
+	.align	16
076f82
+	.type	STRCMP, @function
076f82
+	.globl	STRCMP
076f82
+	.hidden	STRCMP
076f82
+
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+ENTRY (STRCASECMP)
076f82
+	movq	__libc_tsd_LOCALE@gottpoff(%rip), %rax
076f82
+	mov	%fs:(%rax), %LOCALE_REG_LP
076f82
+
076f82
+	/* Either 1 or 5 bytes (dependeing if CET is enabled).  */
076f82
+	.p2align 4
076f82
+END (STRCASECMP)
076f82
+	/* FALLTHROUGH to strcasecmp/strncasecmp_l.  */
076f82
+# endif
076f82
+
076f82
+	.p2align 4
076f82
+STRCMP:
076f82
+	cfi_startproc
076f82
+	_CET_ENDBR
076f82
+	CALL_MCOUNT
076f82
+
076f82
+# if defined USE_AS_STRCASECMP_L
076f82
+	/* We have to fall back on the C implementation for locales with
076f82
+	   encodings not matching ASCII for single bytes.  */
076f82
+#  if LOCALE_T___LOCALES != 0 || LC_CTYPE != 0
076f82
+	mov	LOCALE_T___LOCALES + LC_CTYPE * LP_SIZE(%LOCALE_REG), %RAX_LP
076f82
+#  else
076f82
+	mov	(%LOCALE_REG), %RAX_LP
076f82
+#  endif
076f82
+	testl	$1, LOCALE_DATA_VALUES + _NL_CTYPE_NONASCII_CASE * SIZEOF_VALUES(%rax)
076f82
+	jne	STRCASECMP_NONASCII
076f82
+	leaq	_nl_C_LC_CTYPE_tolower + 128 * 4(%rip), TOLOWER_BASE
076f82
+# endif
076f82
+
076f82
 # ifdef USE_AS_STRNCMP
076f82
+	/* Don't overwrite LOCALE_REG (rcx) until we have pass
076f82
+	   L(one_or_less). Otherwise we might use the wrong locale in
076f82
+	   the OVERFLOW_STRCMP (strcasecmp_l).  */
076f82
 #  ifdef __ILP32__
076f82
 	/* Clear the upper 32 bits.  */
076f82
 	movl	%edx, %edx
076f82
@@ -125,6 +261,32 @@ ENTRY(STRCMP)
076f82
 	   actually bound the buffer.  */
076f82
 	jle	L(one_or_less)
076f82
 # endif
076f82
+
076f82
+# if defined USE_AS_STRCASECMP_L
076f82
+	.section .rodata.cst32, "aM", @progbits, 32
076f82
+	.align	32
076f82
+L(lcase_min):
076f82
+	.quad	0x4141414141414141
076f82
+	.quad	0x4141414141414141
076f82
+	.quad	0x4141414141414141
076f82
+	.quad	0x4141414141414141
076f82
+L(lcase_max):
076f82
+	.quad	0x1a1a1a1a1a1a1a1a
076f82
+	.quad	0x1a1a1a1a1a1a1a1a
076f82
+	.quad	0x1a1a1a1a1a1a1a1a
076f82
+	.quad	0x1a1a1a1a1a1a1a1a
076f82
+L(case_add):
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.previous
076f82
+
076f82
+	vmovdqa64 L(lcase_min)(%rip), LCASE_MIN_YMM
076f82
+	vmovdqa64 L(lcase_max)(%rip), LCASE_MAX_YMM
076f82
+	vmovdqa64 L(case_add)(%rip), CASE_ADD_YMM
076f82
+# endif
076f82
+
076f82
 	movl	%edi, %eax
076f82
 	orl	%esi, %eax
076f82
 	/* Shift out the bits irrelivant to page boundary ([63:12]).  */
076f82
@@ -139,7 +301,7 @@ L(no_page_cross):
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
 	/* Each bit cleared in K1 represents a mismatch or a null CHAR
076f82
 	   in YMM0 and 32 bytes at (%rsi).  */
076f82
-	VPCMP	$0, (%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_STRNCMP
076f82
 	cmpq	$CHAR_PER_VEC, %rdx
076f82
@@ -169,6 +331,8 @@ L(return_vec_0):
076f82
 # else
076f82
 	movzbl	(%rdi, %rcx), %eax
076f82
 	movzbl	(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 # endif
076f82
 L(ret0):
076f82
@@ -188,11 +352,15 @@ L(ret_zero):
076f82
 
076f82
 	.p2align 4,, 5
076f82
 L(one_or_less):
076f82
+#  ifdef USE_AS_STRCASECMP_L
076f82
+	/* Set locale argument for strcasecmp.  */
076f82
+	movq	%LOCALE_REG, %rdx
076f82
+#  endif
076f82
 	jb	L(ret_zero)
076f82
-#  ifdef USE_AS_WCSCMP
076f82
 	/* 'nbe' covers the case where length is negative (large
076f82
 	   unsigned).  */
076f82
-	jnbe	__wcscmp_evex
076f82
+	jnbe	OVERFLOW_STRCMP
076f82
+#  ifdef USE_AS_WCSCMP
076f82
 	movl	(%rdi), %edx
076f82
 	xorl	%eax, %eax
076f82
 	cmpl	(%rsi), %edx
076f82
@@ -201,11 +369,10 @@ L(one_or_less):
076f82
 	negl	%eax
076f82
 	orl	$1, %eax
076f82
 #  else
076f82
-	/* 'nbe' covers the case where length is negative (large
076f82
-	   unsigned).  */
076f82
-	jnbe	__strcmp_evex
076f82
 	movzbl	(%rdi), %eax
076f82
 	movzbl	(%rsi), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 #  endif
076f82
 L(ret1):
076f82
@@ -233,6 +400,8 @@ L(return_vec_1):
076f82
 # else
076f82
 	movzbl	VEC_SIZE(%rdi, %rcx), %eax
076f82
 	movzbl	VEC_SIZE(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 # endif
076f82
 L(ret2):
076f82
@@ -270,6 +439,8 @@ L(return_vec_2):
076f82
 # else
076f82
 	movzbl	(VEC_SIZE * 2)(%rdi, %rcx), %eax
076f82
 	movzbl	(VEC_SIZE * 2)(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 # endif
076f82
 L(ret3):
076f82
@@ -290,6 +461,8 @@ L(return_vec_3):
076f82
 #  else
076f82
 	movzbl	(VEC_SIZE * 3)(%rdi, %rcx), %eax
076f82
 	movzbl	(VEC_SIZE * 3)(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 #  endif
076f82
 L(ret4):
076f82
@@ -303,7 +476,7 @@ L(more_3x_vec):
076f82
 	/* Safe to compare 4x vectors.  */
076f82
 	VMOVU	(VEC_SIZE)(%rdi), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (VEC_SIZE)(%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, VEC_SIZE(%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_1)
076f82
@@ -315,14 +488,14 @@ L(more_3x_vec):
076f82
 
076f82
 	VMOVU	(VEC_SIZE * 2)(%rdi), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (VEC_SIZE * 2)(%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (VEC_SIZE * 2)(%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_2)
076f82
 
076f82
 	VMOVU	(VEC_SIZE * 3)(%rdi), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (VEC_SIZE * 3)(%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (VEC_SIZE * 3)(%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_3)
076f82
@@ -381,7 +554,6 @@ L(prepare_loop_aligned):
076f82
 	subl	%esi, %eax
076f82
 	andl	$(PAGE_SIZE - 1), %eax
076f82
 
076f82
-	vpxorq	%YMMZERO, %YMMZERO, %YMMZERO
076f82
 
076f82
 	/* Loop 4x comparisons at a time.  */
076f82
 	.p2align 4
076f82
@@ -413,22 +585,35 @@ L(loop_skip_page_cross_check):
076f82
 	/* A zero CHAR in YMM9 means that there is a null CHAR.  */
076f82
 	VPMINU	%YMM8, %YMM9, %YMM9
076f82
 
076f82
-	/* Each bit set in K1 represents a non-null CHAR in YMM8.  */
076f82
+	/* Each bit set in K1 represents a non-null CHAR in YMM9.  */
076f82
 	VPTESTM	%YMM9, %YMM9, %k1
076f82
-
076f82
+# ifndef USE_AS_STRCASECMP_L
076f82
 	vpxorq	(VEC_SIZE * 0)(%rsi), %YMM0, %YMM1
076f82
 	vpxorq	(VEC_SIZE * 1)(%rsi), %YMM2, %YMM3
076f82
 	vpxorq	(VEC_SIZE * 2)(%rsi), %YMM4, %YMM5
076f82
 	/* Ternary logic to xor (VEC_SIZE * 3)(%rsi) with YMM6 while
076f82
 	   oring with YMM1. Result is stored in YMM6.  */
076f82
 	vpternlogd $0xde, (VEC_SIZE * 3)(%rsi), %YMM1, %YMM6
076f82
-
076f82
+# else
076f82
+	VMOVU	(VEC_SIZE * 0)(%rsi), %YMM1
076f82
+	TOLOWER_YMM (%YMM0, %YMM1)
076f82
+	VMOVU	(VEC_SIZE * 1)(%rsi), %YMM3
076f82
+	TOLOWER_YMM (%YMM2, %YMM3)
076f82
+	VMOVU	(VEC_SIZE * 2)(%rsi), %YMM5
076f82
+	TOLOWER_YMM (%YMM4, %YMM5)
076f82
+	VMOVU	(VEC_SIZE * 3)(%rsi), %YMM7
076f82
+	TOLOWER_YMM (%YMM6, %YMM7)
076f82
+	vpxorq	%YMM0, %YMM1, %YMM1
076f82
+	vpxorq	%YMM2, %YMM3, %YMM3
076f82
+	vpxorq	%YMM4, %YMM5, %YMM5
076f82
+	vpternlogd $0xde, %YMM7, %YMM1, %YMM6
076f82
+# endif
076f82
 	/* Or together YMM3, YMM5, and YMM6.  */
076f82
 	vpternlogd $0xfe, %YMM3, %YMM5, %YMM6
076f82
 
076f82
 
076f82
 	/* A non-zero CHAR in YMM6 represents a mismatch.  */
076f82
-	VPCMP	$0, %YMMZERO, %YMM6, %k0{%k1}
076f82
+	VPTESTNM %YMM6, %YMM6, %k0{%k1}
076f82
 	kmovd	%k0, %LOOP_REG
076f82
 
076f82
 	TESTEQ	%LOOP_REG
076f82
@@ -437,13 +622,13 @@ L(loop_skip_page_cross_check):
076f82
 
076f82
 	/* Find which VEC has the mismatch of end of string.  */
076f82
 	VPTESTM	%YMM0, %YMM0, %k1
076f82
-	VPCMP	$0, %YMMZERO, %YMM1, %k0{%k1}
076f82
+	VPTESTNM %YMM1, %YMM1, %k0{%k1}
076f82
 	kmovd	%k0, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_0_end)
076f82
 
076f82
 	VPTESTM	%YMM2, %YMM2, %k1
076f82
-	VPCMP	$0, %YMMZERO, %YMM3, %k0{%k1}
076f82
+	VPTESTNM %YMM3, %YMM3, %k0{%k1}
076f82
 	kmovd	%k0, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_1_end)
076f82
@@ -457,7 +642,7 @@ L(return_vec_2_3_end):
076f82
 # endif
076f82
 
076f82
 	VPTESTM	%YMM4, %YMM4, %k1
076f82
-	VPCMP	$0, %YMMZERO, %YMM5, %k0{%k1}
076f82
+	VPTESTNM %YMM5, %YMM5, %k0{%k1}
076f82
 	kmovd	%k0, %ecx
076f82
 	TESTEQ	%ecx
076f82
 # if CHAR_PER_VEC <= 16
076f82
@@ -493,6 +678,8 @@ L(return_vec_3_end):
076f82
 # else
076f82
 	movzbl	(VEC_SIZE * 2)(%rdi, %LOOP_REG64), %eax
076f82
 	movzbl	(VEC_SIZE * 2)(%rsi, %LOOP_REG64), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -545,6 +732,8 @@ L(return_vec_0_end):
076f82
 # else
076f82
 	movzbl	(%rdi, %rcx), %eax
076f82
 	movzbl	(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	/* Flip `eax` if `rdi` and `rsi` where swapped in page cross
076f82
 	   logic. Subtract `r8d` after xor for zero case.  */
076f82
@@ -569,6 +758,8 @@ L(return_vec_1_end):
076f82
 #  else
076f82
 	movzbl	VEC_SIZE(%rdi, %rcx), %eax
076f82
 	movzbl	VEC_SIZE(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -598,7 +789,7 @@ L(page_cross_during_loop):
076f82
 
076f82
 	VMOVA	(%rdi), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_0_end)
076f82
@@ -619,8 +810,7 @@ L(less_1x_vec_till_page_cross):
076f82
 	   been loaded earlier so must be valid.  */
076f82
 	VMOVU	-VEC_SIZE(%rdi, %rax), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, -VEC_SIZE(%rsi, %rax), %YMM0, %k1{%k2}
076f82
-
076f82
+	CMP_R1_S2_YMM (%YMM0, -VEC_SIZE(%rsi, %rax), %YMM1, %k1){%k2}
076f82
 	/* Mask of potentially valid bits. The lower bits can be out of
076f82
 	   range comparisons (but safe regarding page crosses).  */
076f82
 
076f82
@@ -642,6 +832,8 @@ L(less_1x_vec_till_page_cross):
076f82
 
076f82
 # ifdef USE_AS_STRNCMP
076f82
 #  ifdef USE_AS_WCSCMP
076f82
+	/* NB: strcasecmp not used with WCSCMP so this access to r11 is
076f82
+	   safe.  */
076f82
 	movl	%eax, %r11d
076f82
 	shrl	$2, %r11d
076f82
 	cmpq	%r11, %rdx
076f82
@@ -679,6 +871,8 @@ L(return_page_cross_cmp_mem):
076f82
 # else
076f82
 	movzbl	VEC_OFFSET(%rdi, %rcx), %eax
076f82
 	movzbl	VEC_OFFSET(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -709,7 +903,7 @@ L(more_2x_vec_till_page_cross):
076f82
 
076f82
 	VMOVA	VEC_SIZE(%rdi), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, VEC_SIZE(%rsi), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, VEC_SIZE(%rsi), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_1_end)
076f82
@@ -724,14 +918,14 @@ L(more_2x_vec_till_page_cross):
076f82
 	/* Safe to include comparisons from lower bytes.  */
076f82
 	VMOVU	-(VEC_SIZE * 2)(%rdi, %rax), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, -(VEC_SIZE * 2)(%rsi, %rax), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, -(VEC_SIZE * 2)(%rsi, %rax), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_page_cross_0)
076f82
 
076f82
 	VMOVU	-(VEC_SIZE * 1)(%rdi, %rax), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, -(VEC_SIZE * 1)(%rsi, %rax), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, -(VEC_SIZE * 1)(%rsi, %rax), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(return_vec_page_cross_1)
076f82
@@ -740,6 +934,8 @@ L(more_2x_vec_till_page_cross):
076f82
 	/* Must check length here as length might proclude reading next
076f82
 	   page.  */
076f82
 #  ifdef USE_AS_WCSCMP
076f82
+	/* NB: strcasecmp not used with WCSCMP so this access to r11 is
076f82
+	   safe.  */
076f82
 	movl	%eax, %r11d
076f82
 	shrl	$2, %r11d
076f82
 	cmpq	%r11, %rdx
076f82
@@ -754,12 +950,19 @@ L(more_2x_vec_till_page_cross):
076f82
 	VMOVA	(VEC_SIZE * 3)(%rdi), %YMM6
076f82
 	VPMINU	%YMM4, %YMM6, %YMM9
076f82
 	VPTESTM	%YMM9, %YMM9, %k1
076f82
-
076f82
+# ifndef USE_AS_STRCASECMP_L
076f82
 	vpxorq	(VEC_SIZE * 2)(%rsi), %YMM4, %YMM5
076f82
 	/* YMM6 = YMM5 | ((VEC_SIZE * 3)(%rsi) ^ YMM6).  */
076f82
 	vpternlogd $0xde, (VEC_SIZE * 3)(%rsi), %YMM5, %YMM6
076f82
-
076f82
-	VPCMP	$0, %YMMZERO, %YMM6, %k0{%k1}
076f82
+# else
076f82
+	VMOVU	(VEC_SIZE * 2)(%rsi), %YMM5
076f82
+	TOLOWER_YMM (%YMM4, %YMM5)
076f82
+	VMOVU	(VEC_SIZE * 3)(%rsi), %YMM7
076f82
+	TOLOWER_YMM (%YMM6, %YMM7)
076f82
+	vpxorq	%YMM4, %YMM5, %YMM5
076f82
+	vpternlogd $0xde, %YMM7, %YMM5, %YMM6
076f82
+# endif
076f82
+	VPTESTNM %YMM6, %YMM6, %k0{%k1}
076f82
 	kmovd	%k0, %LOOP_REG
076f82
 	TESTEQ	%LOOP_REG
076f82
 	jnz	L(return_vec_2_3_end)
076f82
@@ -815,6 +1018,8 @@ L(return_vec_page_cross_1):
076f82
 # else
076f82
 	movzbl	VEC_OFFSET(%rdi, %rcx), %eax
076f82
 	movzbl	VEC_OFFSET(%rsi, %rcx), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -871,7 +1076,7 @@ L(page_cross):
076f82
 L(page_cross_loop):
076f82
 	VMOVU	(%rdi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	TESTEQ	%ecx
076f82
 	jnz	L(check_ret_vec_page_cross)
076f82
@@ -895,7 +1100,7 @@ L(page_cross_loop):
076f82
 	 */
076f82
 	VMOVU	(%rdi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM0
076f82
 	VPTESTM	%YMM0, %YMM0, %k2
076f82
-	VPCMP	$0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM0, %k1{%k2}
076f82
+	CMP_R1_S2_YMM (%YMM0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %YMM1, %k1){%k2}
076f82
 
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_STRNCMP
076f82
@@ -930,6 +1135,8 @@ L(ret_vec_page_cross_cont):
076f82
 # else
076f82
 	movzbl	(%rdi, %rcx, SIZE_OF_CHAR), %eax
076f82
 	movzbl	(%rsi, %rcx, SIZE_OF_CHAR), %ecx
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %ecx)
076f82
 	subl	%ecx, %eax
076f82
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -989,7 +1196,7 @@ L(less_1x_vec_till_page):
076f82
 	/* Use 16 byte comparison.  */
076f82
 	vmovdqu	(%rdi), %xmm0
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, (%rsi), %xmm0, %k1{%k2}
076f82
+	CMP_R1_S2_XMM (%xmm0, (%rsi), %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_WCSCMP
076f82
 	subl	$0xf, %ecx
076f82
@@ -1009,7 +1216,7 @@ L(less_1x_vec_till_page):
076f82
 # endif
076f82
 	vmovdqu	(%rdi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm0
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm0, %k1{%k2}
076f82
+	CMP_R1_S2_XMM (%xmm0, (%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_WCSCMP
076f82
 	subl	$0xf, %ecx
076f82
@@ -1048,7 +1255,7 @@ L(less_16_till_page):
076f82
 	vmovq	(%rdi), %xmm0
076f82
 	vmovq	(%rsi), %xmm1
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, %xmm1, %xmm0, %k1{%k2}
076f82
+	CMP_R1_R2_XMM (%xmm0, %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_WCSCMP
076f82
 	subl	$0x3, %ecx
076f82
@@ -1068,7 +1275,7 @@ L(less_16_till_page):
076f82
 	vmovq	(%rdi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm0
076f82
 	vmovq	(%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm1
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, %xmm1, %xmm0, %k1{%k2}
076f82
+	CMP_R1_R2_XMM (%xmm0, %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 # ifdef USE_AS_WCSCMP
076f82
 	subl	$0x3, %ecx
076f82
@@ -1128,7 +1335,7 @@ L(ret_less_8_wcs):
076f82
 	vmovd	(%rdi), %xmm0
076f82
 	vmovd	(%rsi), %xmm1
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, %xmm1, %xmm0, %k1{%k2}
076f82
+	CMP_R1_R2_XMM (%xmm0, %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	subl	$0xf, %ecx
076f82
 	jnz	L(check_ret_vec_page_cross)
076f82
@@ -1143,7 +1350,7 @@ L(ret_less_8_wcs):
076f82
 	vmovd	(%rdi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm0
076f82
 	vmovd	(%rsi, %OFFSET_REG64, SIZE_OF_CHAR), %xmm1
076f82
 	VPTESTM	%xmm0, %xmm0, %k2
076f82
-	VPCMP	$0, %xmm1, %xmm0, %k1{%k2}
076f82
+	CMP_R1_R2_XMM (%xmm0, %xmm1, %k1){%k2}
076f82
 	kmovd	%k1, %ecx
076f82
 	subl	$0xf, %ecx
076f82
 	jnz	L(check_ret_vec_page_cross)
076f82
@@ -1176,7 +1383,9 @@ L(less_4_till_page):
076f82
 L(less_4_loop):
076f82
 	movzbl	(%rdi), %eax
076f82
 	movzbl	(%rsi, %rdi), %ecx
076f82
-	subl	%ecx, %eax
076f82
+	TOLOWER_gpr (%rax, %eax)
076f82
+	TOLOWER_gpr (%rcx, %BYTE_LOOP_REG)
076f82
+	subl	%BYTE_LOOP_REG, %eax
076f82
 	jnz	L(ret_less_4_loop)
076f82
 	testl	%ecx, %ecx
076f82
 	jz	L(ret_zero_4_loop)
076f82
@@ -1203,5 +1412,6 @@ L(ret_less_4_loop):
076f82
 	subl	%r8d, %eax
076f82
 	ret
076f82
 # endif
076f82
-END(STRCMP)
076f82
+	cfi_endproc
076f82
+	.size	STRCMP, .-STRCMP
076f82
 #endif
076f82
diff --git a/sysdeps/x86_64/multiarch/strncase_l-evex.S b/sysdeps/x86_64/multiarch/strncase_l-evex.S
076f82
new file mode 100644
076f82
index 0000000000000000..8a5af3695cb8cfff
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strncase_l-evex.S
076f82
@@ -0,0 +1,25 @@
076f82
+/* strncasecmp_l optimized with EVEX.
076f82
+   Copyright (C) 2017-2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#ifndef STRCMP
076f82
+# define STRCMP	__strncasecmp_l_evex
076f82
+#endif
076f82
+#define OVERFLOW_STRCMP	__strcasecmp_l_evex
076f82
+#define USE_AS_STRCASECMP_L
076f82
+#define USE_AS_STRNCMP
076f82
+#include "strcmp-evex.S"