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