076f82
commit 3051cf3e745015a9106cf71be7f7adbb2f83fcac
076f82
Author: Noah Goldstein <goldstein.w.n@gmail.com>
076f82
Date:   Thu Mar 24 18:56:12 2022 -0500
076f82
076f82
    x86: Add AVX2 optimized str{n}casecmp
076f82
    
076f82
    geometric_mean(N=40) of all benchmarks AVX2 / SSE42: .702
076f82
    
076f82
    All string/memory tests pass.
076f82
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
076f82
    
076f82
    (cherry picked from commit bbf81222343fed5cd704001a2ae0d86c71544151)
076f82
076f82
diff --git a/sysdeps/x86_64/multiarch/Makefile b/sysdeps/x86_64/multiarch/Makefile
076f82
index 8c9e7812c6af10b8..711ecf2ee45d61b9 100644
076f82
--- a/sysdeps/x86_64/multiarch/Makefile
076f82
+++ b/sysdeps/x86_64/multiarch/Makefile
076f82
@@ -51,6 +51,8 @@ sysdep_routines += \
076f82
   stpncpy-sse2-unaligned \
076f82
   stpncpy-ssse3 \
076f82
   strcasecmp_l-avx \
076f82
+  strcasecmp_l-avx2 \
076f82
+  strcasecmp_l-avx2-rtm \
076f82
   strcasecmp_l-sse2 \
076f82
   strcasecmp_l-sse4_2 \
076f82
   strcasecmp_l-ssse3 \
076f82
@@ -89,6 +91,8 @@ sysdep_routines += \
076f82
   strlen-evex \
076f82
   strlen-sse2 \
076f82
   strncase_l-avx \
076f82
+  strncase_l-avx2 \
076f82
+  strncase_l-avx2-rtm \
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 4992d7bd3206a7c0..a687b387c91aa9ae 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,13 @@ __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 (AVX2),
076f82
+			      __strcasecmp_avx2)
076f82
+	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX2)
076f82
+			       && CPU_FEATURE_USABLE (RTM)),
076f82
+			      __strcasecmp_avx2_rtm)
076f82
 	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX),
076f82
 			      __strcasecmp_avx)
076f82
@@ -431,6 +438,13 @@ __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 (AVX2),
076f82
+			      __strcasecmp_l_avx2)
076f82
+	      IFUNC_IMPL_ADD (array, i, strcasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX2)
076f82
+			       && CPU_FEATURE_USABLE (RTM)),
076f82
+			      __strcasecmp_l_avx2_rtm)
076f82
 	      IFUNC_IMPL_ADD (array, i, strcasecmp_l,
076f82
 			      CPU_FEATURE_USABLE (AVX),
076f82
 			      __strcasecmp_l_avx)
076f82
@@ -558,6 +572,13 @@ __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 (AVX2),
076f82
+			      __strncasecmp_avx2)
076f82
+	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX2)
076f82
+			       && CPU_FEATURE_USABLE (RTM)),
076f82
+			      __strncasecmp_avx2_rtm)
076f82
 	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
 			      CPU_FEATURE_USABLE (AVX),
076f82
 			      __strncasecmp_avx)
076f82
@@ -572,6 +593,13 @@ __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 (AVX2),
076f82
+			      __strncasecmp_l_avx2)
076f82
+	      IFUNC_IMPL_ADD (array, i, strncasecmp,
076f82
+			      (CPU_FEATURE_USABLE (AVX2)
076f82
+			       && CPU_FEATURE_USABLE (RTM)),
076f82
+			      __strncasecmp_l_avx2_rtm)
076f82
 	      IFUNC_IMPL_ADD (array, i, strncasecmp_l,
076f82
 			      CPU_FEATURE_USABLE (AVX),
076f82
 			      __strncasecmp_l_avx)
076f82
diff --git a/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h b/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
index 931770e079fcc69f..64d0cd6ef25f73c0 100644
076f82
--- a/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
+++ b/sysdeps/x86_64/multiarch/ifunc-strcasecmp.h
076f82
@@ -23,12 +23,24 @@ extern __typeof (REDIRECT_NAME) OPTIMIZE (sse2) attribute_hidden;
076f82
 extern __typeof (REDIRECT_NAME) OPTIMIZE (ssse3) attribute_hidden;
076f82
 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
 
076f82
 static inline void *
076f82
 IFUNC_SELECTOR (void)
076f82
 {
076f82
   const struct cpu_features* cpu_features = __get_cpu_features ();
076f82
 
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, RTM))
076f82
+        return OPTIMIZE (avx2_rtm);
076f82
+
076f82
+      if (!CPU_FEATURES_ARCH_P (cpu_features, Prefer_No_VZEROUPPER))
076f82
+        return OPTIMIZE (avx2);
076f82
+    }
076f82
+
076f82
   if (CPU_FEATURE_USABLE_P (cpu_features, AVX))
076f82
     return OPTIMIZE (avx);
076f82
 
076f82
diff --git a/sysdeps/x86_64/multiarch/strcasecmp_l-avx2-rtm.S b/sysdeps/x86_64/multiarch/strcasecmp_l-avx2-rtm.S
076f82
new file mode 100644
076f82
index 0000000000000000..09957fc3c543b40c
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strcasecmp_l-avx2-rtm.S
076f82
@@ -0,0 +1,15 @@
076f82
+#ifndef STRCMP
076f82
+# define STRCMP	__strcasecmp_l_avx2_rtm
076f82
+#endif
076f82
+
076f82
+#define _GLABEL(x)	x ## _rtm
076f82
+#define GLABEL(x)	_GLABEL(x)
076f82
+
076f82
+#define ZERO_UPPER_VEC_REGISTERS_RETURN	\
076f82
+	ZERO_UPPER_VEC_REGISTERS_RETURN_XTEST
076f82
+
076f82
+#define VZEROUPPER_RETURN	jmp L(return_vzeroupper)
076f82
+
076f82
+#define SECTION(p)	p##.avx.rtm
076f82
+
076f82
+#include "strcasecmp_l-avx2.S"
076f82
diff --git a/sysdeps/x86_64/multiarch/strcasecmp_l-avx2.S b/sysdeps/x86_64/multiarch/strcasecmp_l-avx2.S
076f82
new file mode 100644
076f82
index 0000000000000000..e2762f2a222b2a65
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strcasecmp_l-avx2.S
076f82
@@ -0,0 +1,23 @@
076f82
+/* strcasecmp_l optimized with AVX2.
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_avx2
076f82
+#endif
076f82
+#define USE_AS_STRCASECMP_L
076f82
+#include "strcmp-avx2.S"
076f82
diff --git a/sysdeps/x86_64/multiarch/strcmp-avx2.S b/sysdeps/x86_64/multiarch/strcmp-avx2.S
076f82
index 09a73942086f9c9f..aa91f6e48a0e1ce5 100644
076f82
--- a/sysdeps/x86_64/multiarch/strcmp-avx2.S
076f82
+++ b/sysdeps/x86_64/multiarch/strcmp-avx2.S
076f82
@@ -20,6 +20,10 @@
076f82
 
076f82
 # include <sysdep.h>
076f82
 
076f82
+# if defined USE_AS_STRCASECMP_L
076f82
+#  include "locale-defines.h"
076f82
+# endif
076f82
+
076f82
 # ifndef STRCMP
076f82
 #  define STRCMP	__strcmp_avx2
076f82
 # endif
076f82
@@ -74,13 +78,88 @@
076f82
 #  define VEC_OFFSET	(-VEC_SIZE)
076f82
 # endif
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_avx2
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_avx2
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 xmmZERO	xmm15
076f82
 # define ymmZERO	ymm15
076f82
 
076f82
+# define LCASE_MIN_ymm	%ymm10
076f82
+# define LCASE_MAX_ymm	%ymm11
076f82
+# define CASE_ADD_ymm	%ymm12
076f82
+
076f82
+# define LCASE_MIN_xmm	%xmm10
076f82
+# define LCASE_MAX_xmm	%xmm11
076f82
+# define CASE_ADD_xmm	%xmm12
076f82
+
076f82
+	/* r11 is never use elsewhere so this is safe to maintain.  */
076f82
+# define TOLOWER_BASE	%r11
076f82
+
076f82
 # ifndef SECTION
076f82
 #  define SECTION(p)	p##.avx
076f82
 # endif
076f82
 
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+#  define REG(x, y) x ## y
076f82
+#  define TOLOWER(reg1_in, reg1_out, reg2_in, reg2_out, ext)			\
076f82
+	vpaddb	REG(LCASE_MIN_, ext), reg1_in, REG(%ext, 8);				\
076f82
+	vpaddb	REG(LCASE_MIN_, ext), reg2_in, REG(%ext, 9);				\
076f82
+	vpcmpgtb REG(LCASE_MAX_, ext), REG(%ext, 8), REG(%ext, 8);			\
076f82
+	vpcmpgtb REG(LCASE_MAX_, ext), REG(%ext, 9), REG(%ext, 9);			\
076f82
+	vpandn	REG(CASE_ADD_, ext), REG(%ext, 8), REG(%ext, 8);			\
076f82
+	vpandn	REG(CASE_ADD_, ext), REG(%ext, 9), REG(%ext, 9);			\
076f82
+	vpaddb	REG(%ext, 8), reg1_in, reg1_out;							\
076f82
+	vpaddb	REG(%ext, 9), reg2_in, reg2_out
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, scratch_reg, reg_out, ext)			\
076f82
+	TOLOWER	(s1_reg, scratch_reg, s2_reg, s2_reg, ext);					\
076f82
+	VPCMPEQ	scratch_reg, s2_reg, reg_out
076f82
+
076f82
+#  define CMP_R1_S2(s1_reg, s2_mem, scratch_reg, reg_out, ext)			\
076f82
+	VMOVU	s2_mem, reg_out;											\
076f82
+	CMP_R1_R2(s1_reg, reg_out, scratch_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, scratch_reg, reg_out)			\
076f82
+	VPCMPEQ	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(...) CMP_R1_R2_ymm(__VA_ARGS__)
076f82
+#  define CMP_R1_S2_xmm(...) CMP_R1_R2_xmm(__VA_ARGS__)
076f82
+# endif
076f82
+
076f82
 /* Warning!
076f82
            wcscmp/wcsncmp have to use SIGNED comparison for elements.
076f82
            strcmp/strncmp have to use UNSIGNED comparison for elements.
076f82
@@ -102,8 +181,49 @@
076f82
    returned.  */
076f82
 
076f82
 	.section SECTION(.text), "ax", @progbits
076f82
-ENTRY(STRCMP)
076f82
+	.align	16
076f82
+	.type	STRCMP, @function
076f82
+	.globl	STRCMP
076f82
+	.hidden	STRCMP
076f82
+
076f82
+# ifndef GLABEL
076f82
+#  define GLABEL(...)	__VA_ARGS__
076f82
+# endif
076f82
+
076f82
+# ifdef USE_AS_STRCASECMP_L
076f82
+ENTRY (GLABEL(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 (GLABEL(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
@@ -128,6 +248,30 @@ ENTRY(STRCMP)
076f82
 #  endif
076f82
 # endif
076f82
 	vpxor	%xmmZERO, %xmmZERO, %xmmZERO
076f82
+# if defined USE_AS_STRCASECMP_L
076f82
+	.section .rodata.cst32, "aM", @progbits, 32
076f82
+	.align	32
076f82
+L(lcase_min):
076f82
+	.quad	0x3f3f3f3f3f3f3f3f
076f82
+	.quad	0x3f3f3f3f3f3f3f3f
076f82
+	.quad	0x3f3f3f3f3f3f3f3f
076f82
+	.quad	0x3f3f3f3f3f3f3f3f
076f82
+L(lcase_max):
076f82
+	.quad	0x9999999999999999
076f82
+	.quad	0x9999999999999999
076f82
+	.quad	0x9999999999999999
076f82
+	.quad	0x9999999999999999
076f82
+L(case_add):
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.quad	0x2020202020202020
076f82
+	.previous
076f82
+
076f82
+	vmovdqa	L(lcase_min)(%rip), LCASE_MIN_ymm
076f82
+	vmovdqa	L(lcase_max)(%rip), LCASE_MAX_ymm
076f82
+	vmovdqa	L(case_add)(%rip), CASE_ADD_ymm
076f82
+# endif
076f82
 	movl	%edi, %eax
076f82
 	orl	%esi, %eax
076f82
 	sall	$20, %eax
076f82
@@ -138,8 +282,10 @@ ENTRY(STRCMP)
076f82
 L(no_page_cross):
076f82
 	/* Safe to compare 4x vectors.  */
076f82
 	VMOVU	(%rdi), %ymm0
076f82
-	/* 1s where s1 and s2 equal.  */
076f82
-	VPCMPEQ	(%rsi), %ymm0, %ymm1
076f82
+	/* 1s where s1 and s2 equal. Just VPCMPEQ if its not strcasecmp.
076f82
+	   Otherwise converts ymm0 and load from rsi to lower. ymm2 is
076f82
+	   scratch and ymm1 is the return.  */
076f82
+	CMP_R1_S2_ymm (%ymm0, (%rsi), %ymm2, %ymm1)
076f82
 	/* 1s at null CHAR.  */
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	/* 1s where s1 and s2 equal AND not null CHAR.  */
076f82
@@ -172,6 +318,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
@@ -192,6 +340,10 @@ 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
@@ -211,6 +363,8 @@ L(one_or_less):
076f82
 	jnbe	__strcmp_avx2
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
@@ -238,6 +392,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
@@ -269,6 +425,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
@@ -289,6 +447,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
@@ -299,7 +459,7 @@ L(ret4):
076f82
 L(more_3x_vec):
076f82
 	/* Safe to compare 4x vectors.  */
076f82
 	VMOVU	VEC_SIZE(%rdi), %ymm0
076f82
-	VPCMPEQ	VEC_SIZE(%rsi), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, VEC_SIZE(%rsi), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -312,7 +472,7 @@ L(more_3x_vec):
076f82
 # endif
076f82
 
076f82
 	VMOVU	(VEC_SIZE * 2)(%rdi), %ymm0
076f82
-	VPCMPEQ	(VEC_SIZE * 2)(%rsi), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, (VEC_SIZE * 2)(%rsi), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -320,7 +480,7 @@ L(more_3x_vec):
076f82
 	jnz	L(return_vec_2)
076f82
 
076f82
 	VMOVU	(VEC_SIZE * 3)(%rdi), %ymm0
076f82
-	VPCMPEQ	(VEC_SIZE * 3)(%rsi), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, (VEC_SIZE * 3)(%rsi), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -395,12 +555,10 @@ L(loop_skip_page_cross_check):
076f82
 	VMOVA	(VEC_SIZE * 3)(%rdi), %ymm6
076f82
 
076f82
 	/* ymm1 all 1s where s1 and s2 equal. All 0s otherwise.  */
076f82
-	VPCMPEQ	(VEC_SIZE * 0)(%rsi), %ymm0, %ymm1
076f82
-
076f82
-	VPCMPEQ	(VEC_SIZE * 1)(%rsi), %ymm2, %ymm3
076f82
-	VPCMPEQ	(VEC_SIZE * 2)(%rsi), %ymm4, %ymm5
076f82
-	VPCMPEQ	(VEC_SIZE * 3)(%rsi), %ymm6, %ymm7
076f82
-
076f82
+	CMP_R1_S2_ymm (%ymm0, (VEC_SIZE * 0)(%rsi), %ymm3, %ymm1)
076f82
+	CMP_R1_S2_ymm (%ymm2, (VEC_SIZE * 1)(%rsi), %ymm5, %ymm3)
076f82
+	CMP_R1_S2_ymm (%ymm4, (VEC_SIZE * 2)(%rsi), %ymm7, %ymm5)
076f82
+	CMP_R1_S2_ymm (%ymm6, (VEC_SIZE * 3)(%rsi), %ymm13, %ymm7)
076f82
 
076f82
 	/* If any mismatches or null CHAR then 0 CHAR, otherwise non-
076f82
 	   zero.  */
076f82
@@ -469,6 +627,8 @@ L(return_vec_2_3_end):
076f82
 # else
076f82
 	movzbl	(VEC_SIZE * 2 - VEC_OFFSET)(%rdi, %LOOP_REG64), %eax
076f82
 	movzbl	(VEC_SIZE * 2 - VEC_OFFSET)(%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
@@ -512,6 +672,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
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -534,6 +696,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
@@ -560,6 +724,8 @@ L(return_vec_2_end):
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
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -587,7 +753,7 @@ L(page_cross_during_loop):
076f82
 	jle	L(less_1x_vec_till_page_cross)
076f82
 
076f82
 	VMOVA	(%rdi), %ymm0
076f82
-	VPCMPEQ	(%rsi), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, (%rsi), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -609,7 +775,7 @@ L(less_1x_vec_till_page_cross):
076f82
 	   here, it means the previous page (rdi - VEC_SIZE) has already
076f82
 	   been loaded earlier so must be valid.  */
076f82
 	VMOVU	-VEC_SIZE(%rdi, %rax), %ymm0
076f82
-	VPCMPEQ	-VEC_SIZE(%rsi, %rax), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, -VEC_SIZE(%rsi, %rax), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -651,6 +817,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
@@ -677,7 +845,7 @@ L(more_2x_vec_till_page_cross):
076f82
 	   iteration here.  */
076f82
 
076f82
 	VMOVU	VEC_SIZE(%rdi), %ymm0
076f82
-	VPCMPEQ	VEC_SIZE(%rsi), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, VEC_SIZE(%rsi), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -693,7 +861,7 @@ L(more_2x_vec_till_page_cross):
076f82
 
076f82
 	/* Safe to include comparisons from lower bytes.  */
076f82
 	VMOVU	-(VEC_SIZE * 2)(%rdi, %rax), %ymm0
076f82
-	VPCMPEQ	-(VEC_SIZE * 2)(%rsi, %rax), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, -(VEC_SIZE * 2)(%rsi, %rax), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -701,7 +869,7 @@ L(more_2x_vec_till_page_cross):
076f82
 	jnz	L(return_vec_page_cross_0)
076f82
 
076f82
 	VMOVU	-(VEC_SIZE * 1)(%rdi, %rax), %ymm0
076f82
-	VPCMPEQ	-(VEC_SIZE * 1)(%rsi, %rax), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, -(VEC_SIZE * 1)(%rsi, %rax), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -719,8 +887,8 @@ L(more_2x_vec_till_page_cross):
076f82
 	VMOVA	(VEC_SIZE * 2)(%rdi), %ymm4
076f82
 	VMOVA	(VEC_SIZE * 3)(%rdi), %ymm6
076f82
 
076f82
-	VPCMPEQ	(VEC_SIZE * 2)(%rsi), %ymm4, %ymm5
076f82
-	VPCMPEQ	(VEC_SIZE * 3)(%rsi), %ymm6, %ymm7
076f82
+	CMP_R1_S2_ymm (%ymm4, (VEC_SIZE * 2)(%rsi), %ymm7, %ymm5)
076f82
+	CMP_R1_S2_ymm (%ymm6, (VEC_SIZE * 3)(%rsi), %ymm13, %ymm7)
076f82
 	vpand	%ymm4, %ymm5, %ymm5
076f82
 	vpand	%ymm6, %ymm7, %ymm7
076f82
 	VPMINU	%ymm5, %ymm7, %ymm7
076f82
@@ -771,6 +939,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
@@ -826,7 +996,7 @@ L(page_cross):
076f82
 L(page_cross_loop):
076f82
 
076f82
 	VMOVU	(%rdi, %OFFSET_REG64), %ymm0
076f82
-	VPCMPEQ	(%rsi, %OFFSET_REG64), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, (%rsi, %OFFSET_REG64), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -844,11 +1014,11 @@ L(page_cross_loop):
076f82
 	subl	%eax, %OFFSET_REG
076f82
 	/* OFFSET_REG has distance to page cross - VEC_SIZE. Guranteed
076f82
 	   to not cross page so is safe to load. Since we have already
076f82
-	   loaded at least 1 VEC from rsi it is also guranteed to be safe.
076f82
-	 */
076f82
+	   loaded at least 1 VEC from rsi it is also guranteed to be
076f82
+	   safe.  */
076f82
 
076f82
 	VMOVU	(%rdi, %OFFSET_REG64), %ymm0
076f82
-	VPCMPEQ	(%rsi, %OFFSET_REG64), %ymm0, %ymm1
076f82
+	CMP_R1_S2_ymm (%ymm0, (%rsi, %OFFSET_REG64), %ymm2, %ymm1)
076f82
 	VPCMPEQ	%ymm0, %ymmZERO, %ymm2
076f82
 	vpandn	%ymm1, %ymm2, %ymm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -881,6 +1051,8 @@ L(ret_vec_page_cross_cont):
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
 	xorl	%r8d, %eax
076f82
 	subl	%r8d, %eax
076f82
@@ -934,7 +1106,7 @@ L(less_1x_vec_till_page):
076f82
 	ja	L(less_16_till_page)
076f82
 
076f82
 	VMOVU	(%rdi), %xmm0
076f82
-	VPCMPEQ	(%rsi), %xmm0, %xmm1
076f82
+	CMP_R1_S2_xmm (%xmm0, (%rsi), %xmm2, %xmm1)
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -952,7 +1124,7 @@ L(less_1x_vec_till_page):
076f82
 # endif
076f82
 
076f82
 	VMOVU	(%rdi, %OFFSET_REG64), %xmm0
076f82
-	VPCMPEQ	(%rsi, %OFFSET_REG64), %xmm0, %xmm1
076f82
+	CMP_R1_S2_xmm (%xmm0, (%rsi, %OFFSET_REG64), %xmm2, %xmm1)
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
@@ -990,7 +1162,7 @@ L(less_16_till_page):
076f82
 	vmovq	(%rdi), %xmm0
076f82
 	vmovq	(%rsi), %xmm1
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
-	VPCMPEQ	%xmm1, %xmm0, %xmm1
076f82
+	CMP_R1_R2_xmm (%xmm0, %xmm1, %xmm3, %xmm1)
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
 	incb	%cl
076f82
@@ -1010,7 +1182,7 @@ L(less_16_till_page):
076f82
 	vmovq	(%rdi, %OFFSET_REG64), %xmm0
076f82
 	vmovq	(%rsi, %OFFSET_REG64), %xmm1
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
-	VPCMPEQ	%xmm1, %xmm0, %xmm1
076f82
+	CMP_R1_R2_xmm (%xmm0, %xmm1, %xmm3, %xmm1)
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
 	incb	%cl
076f82
@@ -1066,7 +1238,7 @@ L(ret_less_8_wcs):
076f82
 	vmovd	(%rdi), %xmm0
076f82
 	vmovd	(%rsi), %xmm1
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
-	VPCMPEQ	%xmm1, %xmm0, %xmm1
076f82
+	CMP_R1_R2_xmm (%xmm0, %xmm1, %xmm3, %xmm1)
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
 	subl	$0xf, %ecx
076f82
@@ -1085,7 +1257,7 @@ L(ret_less_8_wcs):
076f82
 	vmovd	(%rdi, %OFFSET_REG64), %xmm0
076f82
 	vmovd	(%rsi, %OFFSET_REG64), %xmm1
076f82
 	VPCMPEQ	%xmm0, %xmmZERO, %xmm2
076f82
-	VPCMPEQ	%xmm1, %xmm0, %xmm1
076f82
+	CMP_R1_R2_xmm (%xmm0, %xmm1, %xmm3, %xmm1)
076f82
 	vpandn	%xmm1, %xmm2, %xmm1
076f82
 	vpmovmskb %ymm1, %ecx
076f82
 	subl	$0xf, %ecx
076f82
@@ -1119,7 +1291,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
@@ -1146,5 +1320,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-avx2-rtm.S b/sysdeps/x86_64/multiarch/strncase_l-avx2-rtm.S
076f82
new file mode 100644
076f82
index 0000000000000000..58c05dcfb8643791
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strncase_l-avx2-rtm.S
076f82
@@ -0,0 +1,16 @@
076f82
+#ifndef STRCMP
076f82
+# define STRCMP	__strncasecmp_l_avx2_rtm
076f82
+#endif
076f82
+
076f82
+#define _GLABEL(x)	x ## _rtm
076f82
+#define GLABEL(x)	_GLABEL(x)
076f82
+
076f82
+#define ZERO_UPPER_VEC_REGISTERS_RETURN	\
076f82
+	ZERO_UPPER_VEC_REGISTERS_RETURN_XTEST
076f82
+
076f82
+#define VZEROUPPER_RETURN	jmp L(return_vzeroupper)
076f82
+
076f82
+#define SECTION(p)	p##.avx.rtm
076f82
+#define OVERFLOW_STRCMP	__strcasecmp_l_avx2_rtm
076f82
+
076f82
+#include "strncase_l-avx2.S"
076f82
diff --git a/sysdeps/x86_64/multiarch/strncase_l-avx2.S b/sysdeps/x86_64/multiarch/strncase_l-avx2.S
076f82
new file mode 100644
076f82
index 0000000000000000..48c0aa21f84ad32c
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86_64/multiarch/strncase_l-avx2.S
076f82
@@ -0,0 +1,27 @@
076f82
+/* strncasecmp_l optimized with AVX2.
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_avx2
076f82
+#endif
076f82
+#define USE_AS_STRCASECMP_L
076f82
+#define USE_AS_STRNCMP
076f82
+#ifndef OVERFLOW_STRCMP
076f82
+# define OVERFLOW_STRCMP	__strcasecmp_l_avx2
076f82
+#endif
076f82
+#include "strcmp-avx2.S"