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