6fe9d0
commit a55e2da2702e235fa0ae66a116d304d1bffc060a
6fe9d0
Author: Lucas A. M. Magalhaes <lamm@linux.ibm.com>
6fe9d0
Date:   Thu May 6 17:01:52 2021 -0300
6fe9d0
6fe9d0
    powerpc: Optimized memcmp for power10
6fe9d0
    
6fe9d0
    This patch was based on the __memcmp_power8 and the recent
6fe9d0
    __strlen_power10.
6fe9d0
    
6fe9d0
    Improvements from __memcmp_power8:
6fe9d0
    
6fe9d0
    1. Don't need alignment code.
6fe9d0
    
6fe9d0
       On POWER10 lxvp and lxvl do not generate alignment interrupts, so
6fe9d0
    they are safe for use on caching-inhibited memory.  Notice that the
6fe9d0
    comparison on the main loop will wait for both VSR to be ready.
6fe9d0
    Therefore aligning one of the input address does not improve
6fe9d0
    performance.  In order to align both registers a vperm is necessary
6fe9d0
    which add too much overhead.
6fe9d0
    
6fe9d0
    2. Uses new POWER10 instructions
6fe9d0
    
6fe9d0
       This code uses lxvp to decrease contention on load by loading 32 bytes
6fe9d0
    per instruction.
6fe9d0
       The vextractbm is used to have a smaller tail code for calculating the
6fe9d0
    return value.
6fe9d0
    
6fe9d0
    3. Performance improvement
6fe9d0
    
6fe9d0
       This version has around 35% better performance on average. I saw no
6fe9d0
    performance regressions for any length or alignment.
6fe9d0
    
6fe9d0
    Thanks Matheus for helping me out with some details.
6fe9d0
    
6fe9d0
    Co-authored-by: Matheus Castanho <msc@linux.ibm.com>
6fe9d0
    Reviewed-by: Raphael M Zinsly <rzinsly@linux.ibm.com>
6fe9d0
6fe9d0
diff --git a/sysdeps/powerpc/powerpc64/le/power10/memcmp.S b/sysdeps/powerpc/powerpc64/le/power10/memcmp.S
6fe9d0
new file mode 100644
6fe9d0
index 0000000000000000..52f244e7e77cbdf9
6fe9d0
--- /dev/null
6fe9d0
+++ b/sysdeps/powerpc/powerpc64/le/power10/memcmp.S
6fe9d0
@@ -0,0 +1,179 @@
6fe9d0
+/* Optimized memcmp implementation for POWER10.
6fe9d0
+   Copyright (C) 2021 Free Software Foundation, Inc.
6fe9d0
+   This file is part of the GNU C Library.
6fe9d0
+
6fe9d0
+   The GNU C Library is free software; you can redistribute it and/or
6fe9d0
+   modify it under the terms of the GNU Lesser General Public
6fe9d0
+   License as published by the Free Software Foundation; either
6fe9d0
+   version 2.1 of the License, or (at your option) any later version.
6fe9d0
+
6fe9d0
+   The GNU C Library is distributed in the hope that it will be useful,
6fe9d0
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6fe9d0
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6fe9d0
+   Lesser General Public License for more details.
6fe9d0
+
6fe9d0
+   You should have received a copy of the GNU Lesser General Public
6fe9d0
+   License along with the GNU C Library; if not, see
6fe9d0
+   <https://www.gnu.org/licenses/>.  */
6fe9d0
+
6fe9d0
+#include <sysdep.h>
6fe9d0
+
6fe9d0
+/* TODO: Replace macros by the actual instructions when minimum binutils becomes
6fe9d0
+   >= 2.35.  This is used to keep compatibility with older versions.  */
6fe9d0
+#define VEXTRACTBM(rt,vrb)	 \
6fe9d0
+	.long(((4)<<(32-6))	 \
6fe9d0
+	      | ((rt)<<(32-11))  \
6fe9d0
+	      | ((8)<<(32-16))	 \
6fe9d0
+	      | ((vrb)<<(32-21)) \
6fe9d0
+	      | 1602)
6fe9d0
+
6fe9d0
+#define LXVP(xtp,dq,ra)			   \
6fe9d0
+	.long(((6)<<(32-6))		   \
6fe9d0
+	      | ((((xtp)-32)>>1)<<(32-10)) \
6fe9d0
+	      | ((1)<<(32-11))		   \
6fe9d0
+	      | ((ra)<<(32-16))		   \
6fe9d0
+	      | dq)
6fe9d0
+
6fe9d0
+/* Compare 32 bytes.  */
6fe9d0
+#define COMPARE_32(vr1,vr2,offset,tail_1,tail_2)\
6fe9d0
+	LXVP(32+vr1,offset,r3);		\
6fe9d0
+	LXVP(32+vr2,offset,r4);		\
6fe9d0
+	vcmpneb.  v5,vr1+1,vr2+1;	\
6fe9d0
+	bne	  cr6,L(tail_2);	\
6fe9d0
+	vcmpneb.  v4,vr1,vr2;		\
6fe9d0
+	bne	  cr6,L(tail_1);	\
6fe9d0
+
6fe9d0
+#define TAIL(v_res,s1,s2)	\
6fe9d0
+	vctzlsbb  r7,v_res;	\
6fe9d0
+	vextubrx  r8,r7,s1;	\
6fe9d0
+	vextubrx  r9,r7,s2;	\
6fe9d0
+	subf	  r3,r9,r8;	\
6fe9d0
+	blr;			\
6fe9d0
+
6fe9d0
+/* int [r3] memcmp (const char *s1 [r3], const char *s2 [r4],
6fe9d0
+					size_t size [r5])  */
6fe9d0
+
6fe9d0
+#ifndef MEMCMP
6fe9d0
+# define MEMCMP memcmp
6fe9d0
+#endif
6fe9d0
+	.machine  power9
6fe9d0
+ENTRY_TOCLESS (MEMCMP, 4)
6fe9d0
+	CALL_MCOUNT 3
6fe9d0
+
6fe9d0
+	cmpldi	cr6,r5,64
6fe9d0
+	bgt	cr6,L(loop_head)
6fe9d0
+
6fe9d0
+/* Compare 64 bytes. This section is used for lengths <= 64 and for the last
6fe9d0
+   bytes for larger lengths.  */
6fe9d0
+L(last_compare):
6fe9d0
+	li	r8,16
6fe9d0
+
6fe9d0
+	sldi	r9,r5,56
6fe9d0
+	sldi	r8,r8,56
6fe9d0
+	addi	r6,r3,16
6fe9d0
+	addi	r7,r4,16
6fe9d0
+
6fe9d0
+	/* Align up to 16 bytes.  */
6fe9d0
+	lxvl	32+v0,r3,r9
6fe9d0
+	lxvl	32+v2,r4,r9
6fe9d0
+
6fe9d0
+	/* The sub. and vcmpneb. results are concatenated by the crnand in order
6fe9d0
+	   to do a single branch. It's doing a NOT(CR0.GT AND CR6.EQ) then
6fe9d0
+	   loading to CR0.LT.  That means r9 is not bigger than 0 and v4 is not
6fe9d0
+	   all equal to 0.  */
6fe9d0
+	sub.	  r9,r9,r8
6fe9d0
+	vcmpneb.  v4,v0,v2
6fe9d0
+	crnand	  4*cr0+lt,4*cr0+gt,4*cr6+eq
6fe9d0
+	bt	  4*cr0+lt,L(tail1)
6fe9d0
+
6fe9d0
+	addi	  r3,r3,32
6fe9d0
+	addi	  r4,r4,32
6fe9d0
+
6fe9d0
+	lxvl	  32+v1,r6,r9
6fe9d0
+	lxvl	  32+v3,r7,r9
6fe9d0
+	sub.	  r9,r9,r8
6fe9d0
+	vcmpneb.  v5,v1,v3
6fe9d0
+	crnand	  4*cr0+lt,4*cr0+gt,4*cr6+eq
6fe9d0
+	bt	  4*cr0+lt,L(tail2)
6fe9d0
+
6fe9d0
+	addi	  r6,r3,16
6fe9d0
+	addi	  r7,r4,16
6fe9d0
+
6fe9d0
+	lxvl	  32+v6,r3,r9
6fe9d0
+	lxvl	  32+v8,r4,r9
6fe9d0
+	sub.	  r9,r9,r8
6fe9d0
+	vcmpneb.  v4,v6,v8
6fe9d0
+	crnand	  4*cr0+lt,4*cr0+gt,4*cr6+eq
6fe9d0
+	bt	  4*cr0+lt,L(tail3)
6fe9d0
+
6fe9d0
+	lxvl	  32+v7,r6,r9
6fe9d0
+	lxvl	  32+v9,r7,r9
6fe9d0
+	vcmpneb.  v5,v7,v9
6fe9d0
+	bne	  cr6,L(tail4)
6fe9d0
+
6fe9d0
+L(finish):
6fe9d0
+	/* The contents are equal.  */
6fe9d0
+	li	r3,0
6fe9d0
+	blr
6fe9d0
+
6fe9d0
+L(loop_head):
6fe9d0
+	/* Calculate how many loops to run.  */
6fe9d0
+	srdi.	r8,r5,7
6fe9d0
+	beq	L(loop_tail)
6fe9d0
+	mtctr	r8
6fe9d0
+
6fe9d0
+/* Main loop.  Compares 128 bytes each loop.  */
6fe9d0
+	.p2align 5
6fe9d0
+L(loop_128):
6fe9d0
+	COMPARE_32(v0,v2,0,tail1,tail2)
6fe9d0
+	COMPARE_32(v6,v8,32,tail3,tail4)
6fe9d0
+	COMPARE_32(v10,v12,64,tail5,tail6)
6fe9d0
+	COMPARE_32(v14,v16,96,tail7,tail8)
6fe9d0
+
6fe9d0
+	addi	r3,r3,128
6fe9d0
+	addi	r4,r4,128
6fe9d0
+	bdnz	L(loop_128)
6fe9d0
+
6fe9d0
+	/* Account loop comparisons.  */
6fe9d0
+	clrldi.  r5,r5,57
6fe9d0
+	beq	 L(finish)
6fe9d0
+
6fe9d0
+/* Compares 64 bytes if length is still bigger than 64 bytes.  */
6fe9d0
+	.p2align 5
6fe9d0
+L(loop_tail):
6fe9d0
+	cmpldi	r5,64
6fe9d0
+	ble	L(last_compare)
6fe9d0
+	COMPARE_32(v0,v2,0,tail1,tail2)
6fe9d0
+	COMPARE_32(v6,v8,32,tail3,tail4)
6fe9d0
+	addi	r3,r3,64
6fe9d0
+	addi	r4,r4,64
6fe9d0
+	subi	r5,r5,64
6fe9d0
+	b	L(last_compare)
6fe9d0
+
6fe9d0
+L(tail1):
6fe9d0
+	TAIL(v4,v0,v2)
6fe9d0
+
6fe9d0
+L(tail2):
6fe9d0
+	TAIL(v5,v1,v3)
6fe9d0
+
6fe9d0
+L(tail3):
6fe9d0
+	TAIL(v4,v6,v8)
6fe9d0
+
6fe9d0
+L(tail4):
6fe9d0
+	TAIL(v5,v7,v9)
6fe9d0
+
6fe9d0
+L(tail5):
6fe9d0
+	TAIL(v4,v10,v12)
6fe9d0
+
6fe9d0
+L(tail6):
6fe9d0
+	TAIL(v5,v11,v13)
6fe9d0
+
6fe9d0
+L(tail7):
6fe9d0
+	TAIL(v4,v14,v16)
6fe9d0
+
6fe9d0
+L(tail8):
6fe9d0
+	TAIL(v5,v15,v17)
6fe9d0
+
6fe9d0
+END (MEMCMP)
6fe9d0
+libc_hidden_builtin_def (memcmp)
6fe9d0
+weak_alias (memcmp, bcmp)
6fe9d0
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
6fe9d0
index ac2446aca62cc4ab..ee98417f4a383356 100644
6fe9d0
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
6fe9d0
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
6fe9d0
@@ -32,7 +32,7 @@ sysdep_routines += memcpy-power8-cached memcpy-power7 memcpy-a2 memcpy-power6 \
6fe9d0
 		   strncase-power8
6fe9d0
 
6fe9d0
 ifneq (,$(filter %le,$(config-machine)))
6fe9d0
-sysdep_routines += memcpy-power10 memmove-power10 memset-power10 \
6fe9d0
+sysdep_routines += memcmp-power10 memcpy-power10 memmove-power10 memset-power10 \
6fe9d0
 		   rawmemchr-power9 rawmemchr-power10 \
6fe9d0
 		   strcmp-power9 strncmp-power9 strcpy-power9 stpcpy-power9 \
6fe9d0
 		   strlen-power9 strncpy-power9 stpncpy-power9 strlen-power10
6fe9d0
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
6fe9d0
index 127af84b32a8196f..5213abdf87c79c88 100644
6fe9d0
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
6fe9d0
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
6fe9d0
@@ -184,6 +184,12 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
6fe9d0
 
6fe9d0
   /* Support sysdeps/powerpc/powerpc64/multiarch/memcmp.c.  */
6fe9d0
   IFUNC_IMPL (i, name, memcmp,
6fe9d0
+#ifdef __LITTLE_ENDIAN__
6fe9d0
+	      IFUNC_IMPL_ADD (array, i, memcmp,
6fe9d0
+            hwcap2 & PPC_FEATURE2_ARCH_3_1
6fe9d0
+            && hwcap & PPC_FEATURE_HAS_VSX,
6fe9d0
+			      __memcmp_power10)
6fe9d0
+#endif
6fe9d0
 	      IFUNC_IMPL_ADD (array, i, memcmp, hwcap2 & PPC_FEATURE2_ARCH_2_07,
6fe9d0
 			      __memcmp_power8)
6fe9d0
 	      IFUNC_IMPL_ADD (array, i, memcmp, hwcap & PPC_FEATURE_HAS_VSX,
6fe9d0
diff --git a/sysdeps/powerpc/powerpc64/multiarch/memcmp-power10.S b/sysdeps/powerpc/powerpc64/multiarch/memcmp-power10.S
6fe9d0
new file mode 100644
6fe9d0
index 0000000000000000..73a0debd4a811d8e
6fe9d0
--- /dev/null
6fe9d0
+++ b/sysdeps/powerpc/powerpc64/multiarch/memcmp-power10.S
6fe9d0
@@ -0,0 +1,26 @@
6fe9d0
+/* Optimized memcmp implementation for POWER10.
6fe9d0
+   Copyright (C) 2017-2021 Free Software Foundation, Inc.
6fe9d0
+   This file is part of the GNU C Library.
6fe9d0
+
6fe9d0
+   The GNU C Library is free software; you can redistribute it and/or
6fe9d0
+   modify it under the terms of the GNU Lesser General Public
6fe9d0
+   License as published by the Free Software Foundation; either
6fe9d0
+   version 2.1 of the License, or (at your option) any later version.
6fe9d0
+
6fe9d0
+   The GNU C Library is distributed in the hope that it will be useful,
6fe9d0
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6fe9d0
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6fe9d0
+   Lesser General Public License for more details.
6fe9d0
+
6fe9d0
+   You should have received a copy of the GNU Lesser General Public
6fe9d0
+   License along with the GNU C Library; if not, see
6fe9d0
+   <https://www.gnu.org/licenses/>.  */
6fe9d0
+
6fe9d0
+#define MEMCMP __memcmp_power10
6fe9d0
+
6fe9d0
+#undef libc_hidden_builtin_def
6fe9d0
+#define libc_hidden_builtin_def(name)
6fe9d0
+#undef weak_alias
6fe9d0
+#define weak_alias(name,alias)
6fe9d0
+
6fe9d0
+#include <sysdeps/powerpc/powerpc64/le/power10/memcmp.S>
6fe9d0
diff --git a/sysdeps/powerpc/powerpc64/multiarch/memcmp.c b/sysdeps/powerpc/powerpc64/multiarch/memcmp.c
6fe9d0
index 2c7a083a6560f920..0b8c0c1d8aa3f90a 100644
6fe9d0
--- a/sysdeps/powerpc/powerpc64/multiarch/memcmp.c
6fe9d0
+++ b/sysdeps/powerpc/powerpc64/multiarch/memcmp.c
6fe9d0
@@ -27,11 +27,17 @@ extern __typeof (memcmp) __memcmp_ppc attribute_hidden;
6fe9d0
 extern __typeof (memcmp) __memcmp_power4 attribute_hidden;
6fe9d0
 extern __typeof (memcmp) __memcmp_power7 attribute_hidden;
6fe9d0
 extern __typeof (memcmp) __memcmp_power8 attribute_hidden;
6fe9d0
+extern __typeof (memcmp) __memcmp_power10 attribute_hidden;
6fe9d0
 # undef memcmp
6fe9d0
 
6fe9d0
 /* Avoid DWARF definition DIE on ifunc symbol so that GDB can handle
6fe9d0
    ifunc symbol properly.  */
6fe9d0
 libc_ifunc_redirected (__redirect_memcmp, memcmp,
6fe9d0
+#ifdef __LITTLE_ENDIAN__
6fe9d0
+				(hwcap2 & PPC_FEATURE2_ARCH_3_1
6fe9d0
+				 && hwcap & PPC_FEATURE_HAS_VSX)
6fe9d0
+				 ? __memcmp_power10 :
6fe9d0
+#endif
6fe9d0
 		       (hwcap2 & PPC_FEATURE2_ARCH_2_07)
6fe9d0
 		       ? __memcmp_power8 :
6fe9d0
 		       (hwcap & PPC_FEATURE_HAS_VSX)