ce426f
From 1e5a0d609f20a613e1e989802bbe479f61bed1ca Mon Sep 17 00:00:00 2001
ce426f
From: Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
ce426f
Date: Thu, 1 Dec 2016 11:35:43 +0530
ce426f
Subject: [PATCH] powerpc: strcmp optimization for power9
ce426f
ce426f
Vectorized loops are used for strings > 32B when compared
ce426f
to power8 optimization.
ce426f
ce426f
Tested on power9 ppc64le simulator.
ce426f
ce426f
(cherry picked from commit 80ab6401a9bb566de940cc6a5fb7a6af650f17b9)
ce426f
ce426f
Conflicts:
ce426f
	sysdeps/powerpc/powerpc64/multiarch/strcmp.c
ce426f
---
ce426f
 ChangeLog                                          |  11 +
ce426f
 sysdeps/powerpc/powerpc64/multiarch/Makefile       |   2 +-
ce426f
 .../powerpc/powerpc64/multiarch/ifunc-impl-list.c  |   3 +
ce426f
 .../powerpc/powerpc64/multiarch/strcmp-power9.S    |  40 +++
ce426f
 sysdeps/powerpc/powerpc64/multiarch/strcmp.c       |  13 +-
ce426f
 sysdeps/powerpc/powerpc64/power9/strcmp.S          | 278 +++++++++++++++++++++
ce426f
 6 files changed, 341 insertions(+), 6 deletions(-)
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strcmp-power9.S
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/power9/strcmp.S
ce426f
ce426f
diff --git a/ChangeLog b/ChangeLog
ce426f
index 6d6aab3..57152b8 100644
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
index e3ac285..2c83c22 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
@@ -16,7 +16,7 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
ce426f
                   strncase-ppc64 strncase-power8 \
ce426f
                   strcasestr-power8 strcasestr-ppc64 \
ce426f
                   strcat-power8 strcat-power7 strcat-ppc64 \
ce426f
-                  strcmp-power8 strcmp-power7 strcmp-ppc64 \
ce426f
+                  strcmp-power9 strcmp-power8 strcmp-power7 strcmp-ppc64 \
ce426f
                   strcpy-power8 strcpy-power7 strcpy-ppc64 \
ce426f
                   strcspn-power8 strcspn-ppc64 \
ce426f
                   stpncpy-power8 stpncpy-power7 stpncpy-ppc64 \
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
index aabd7bc..404a226 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
@@ -311,6 +311,9 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
ce426f
   /* Support sysdeps/powerpc/powerpc64/multiarch/strcmp.c.  */
ce426f
   IFUNC_IMPL (i, name, strcmp,
ce426f
 	      IFUNC_IMPL_ADD (array, i, strcmp,
ce426f
+			      hwcap2 & PPC_FEATURE2_ARCH_3_00,
ce426f
+			      __strcmp_power9)
ce426f
+	      IFUNC_IMPL_ADD (array, i, strcmp,
ce426f
 			      hwcap2 & PPC_FEATURE2_ARCH_2_07,
ce426f
 			      __strcmp_power8)
ce426f
 	      IFUNC_IMPL_ADD (array, i, strcmp,
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp-power9.S b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power9.S
ce426f
new file mode 100644
ce426f
index 0000000..0a09e5b
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power9.S
ce426f
@@ -0,0 +1,40 @@
ce426f
+/* Optimized strcmp implementation for POWER9/PPC64.
ce426f
+   Copyright (C) 2016 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <sysdep.h>
ce426f
+
ce426f
+#undef EALIGN
ce426f
+#define EALIGN(name, alignt, words)				\
ce426f
+  .section ".text";						\
ce426f
+  ENTRY_2(__strcmp_power9)					\
ce426f
+  .align ALIGNARG(alignt);					\
ce426f
+  EALIGN_W_##words;						\
ce426f
+  BODY_LABEL(__strcmp_power9):					\
ce426f
+  cfi_startproc;						\
ce426f
+  LOCALENTRY(__strcmp_power9)
ce426f
+
ce426f
+#undef END
ce426f
+#define END(name)						\
ce426f
+  cfi_endproc;							\
ce426f
+  TRACEBACK(__strcmp_power9)					\
ce426f
+  END_2(__strcmp_power9)
ce426f
+
ce426f
+#undef libc_hidden_builtin_def
ce426f
+#define libc_hidden_builtin_def(name)
ce426f
+
ce426f
+#include <sysdeps/powerpc/powerpc64/power9/strcmp.S>
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp.c b/sysdeps/powerpc/powerpc64/multiarch/strcmp.c
ce426f
index b45ba1f..7345f5a 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/strcmp.c
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp.c
ce426f
@@ -24,11 +24,14 @@
ce426f
 extern __typeof (strcmp) __strcmp_ppc attribute_hidden;
ce426f
 extern __typeof (strcmp) __strcmp_power7 attribute_hidden;
ce426f
 extern __typeof (strcmp) __strcmp_power8 attribute_hidden;
ce426f
+extern __typeof (strcmp) __strcmp_power9 attribute_hidden;
ce426f
 
ce426f
 libc_ifunc (strcmp,
ce426f
-            (hwcap2 & PPC_FEATURE2_ARCH_2_07)
ce426f
-              ? __strcmp_power8 :
ce426f
-              (hwcap & PPC_FEATURE_HAS_VSX)
ce426f
-              ? __strcmp_power7
ce426f
-            : __strcmp_ppc);
ce426f
+	    (hwcap2 & PPC_FEATURE2_ARCH_3_00)
ce426f
+	      ? __strcmp_power9 :
ce426f
+	    (hwcap2 & PPC_FEATURE2_ARCH_2_07)
ce426f
+	      ? __strcmp_power8 :
ce426f
+	    (hwcap & PPC_FEATURE_HAS_VSX)
ce426f
+	      ? __strcmp_power7
ce426f
+	    : __strcmp_ppc);
ce426f
 #endif
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power9/strcmp.S b/sysdeps/powerpc/powerpc64/power9/strcmp.S
ce426f
new file mode 100644
ce426f
index 0000000..754d508
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/power9/strcmp.S
ce426f
@@ -0,0 +1,278 @@
ce426f
+/* Optimized strcmp implementation for PowerPC64/POWER9.
ce426f
+   Copyright (C) 2016 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+#include <sysdep.h>
ce426f
+
ce426f
+/* Implements the function
ce426f
+
ce426f
+   int [r3] strcmp (const char *s1 [r3], const char *s2 [r4])
ce426f
+
ce426f
+   The implementation uses unaligned doubleword access for first 32 bytes
ce426f
+   as in POWER8 patch and uses vectorised loops after that.  */
ce426f
+
ce426f
+/* TODO: Change this to actual instructions when minimum binutils is upgraded
ce426f
+   to 2.27. Macros are defined below for these newer instructions in order
ce426f
+   to maintain compatibility.  */
ce426f
+# define VCTZLSBB(r,v) .long (0x10010602 | ((r)<<(32-11)) | ((v)<<(32-21)))
ce426f
+
ce426f
+# define VEXTUBRX(t,a,b) .long (0x1000070d \
ce426f
+				| ((t)<<(32-11))  \
ce426f
+				| ((a)<<(32-16))  \
ce426f
+				| ((b)<<(32-21)) )
ce426f
+
ce426f
+# define VCMPNEZB(t,a,b) .long (0x10000507 \
ce426f
+				| ((t)<<(32-11))  \
ce426f
+				| ((a)<<(32-16))  \
ce426f
+				| ((b)<<(32-21)) )
ce426f
+
ce426f
+/* Get 16 bytes for unaligned case.
ce426f
+   reg1: Vector to hold next 16 bytes.
ce426f
+   reg2: Address to read from.
ce426f
+   reg3: Permute control vector.  */
ce426f
+# define GET16BYTES(reg1, reg2, reg3) \
ce426f
+	lvx	reg1, 0, reg2; \
ce426f
+	vperm	v8, v2, reg1, reg3; \
ce426f
+	vcmpequb.	v8, v0, v8; \
ce426f
+	beq	cr6, 1f; \
ce426f
+	vspltisb	v9, 0; \
ce426f
+	b	2f; \
ce426f
+	.align 4; \
ce426f
+1: \
ce426f
+	addi    r6, reg2, 16; \
ce426f
+	lvx     v9, 0, r6; \
ce426f
+2: \
ce426f
+	vperm   reg1, v9, reg1, reg3;
ce426f
+
ce426f
+/* TODO: change this to .machine power9 when the minimum required binutils
ce426f
+   allows it.  */
ce426f
+
ce426f
+	.machine  power7
ce426f
+EALIGN (strcmp, 4, 0)
ce426f
+	li	r0, 0
ce426f
+
ce426f
+	/* Check if [s1]+32 or [s2]+32 will cross a 4K page boundary using
ce426f
+	   the code:
ce426f
+
ce426f
+	    (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
ce426f
+
ce426f
+	   with PAGE_SIZE being 4096 and ITER_SIZE begin 32.  */
ce426f
+
ce426f
+	rldicl	r7, r3, 0, 52
ce426f
+	rldicl	r9, r4, 0, 52
ce426f
+	cmpldi	cr7, r7, 4096-32
ce426f
+	bgt	cr7, L(pagecross_check)
ce426f
+	cmpldi	cr5, r9, 4096-32
ce426f
+	bgt	cr5, L(pagecross_check)
ce426f
+
ce426f
+	/* For short strings up to 32 bytes,  load both s1 and s2 using
ce426f
+	   unaligned dwords and compare.  */
ce426f
+	ld	r8, 0(r3)
ce426f
+	ld	r10, 0(r4)
ce426f
+	cmpb	r12, r8, r0
ce426f
+	cmpb	r11, r8, r10
ce426f
+	orc.	r9, r12, r11
ce426f
+	bne	cr0, L(different_nocmpb)
ce426f
+
ce426f
+	ld	r8, 8(r3)
ce426f
+	ld	r10, 8(r4)
ce426f
+	cmpb	r12, r8, r0
ce426f
+	cmpb	r11, r8, r10
ce426f
+	orc.	r9, r12, r11
ce426f
+	bne	cr0, L(different_nocmpb)
ce426f
+
ce426f
+	ld	r8, 16(r3)
ce426f
+	ld	r10, 16(r4)
ce426f
+	cmpb	r12, r8, r0
ce426f
+	cmpb	r11, r8, r10
ce426f
+	orc.	r9, r12, r11
ce426f
+	bne	cr0, L(different_nocmpb)
ce426f
+
ce426f
+	ld	r8, 24(r3)
ce426f
+	ld	r10, 24(r4)
ce426f
+	cmpb	r12, r8, r0
ce426f
+	cmpb	r11, r8, r10
ce426f
+	orc.	r9, r12, r11
ce426f
+	bne	cr0, L(different_nocmpb)
ce426f
+
ce426f
+	addi	r7, r3, 32
ce426f
+	addi	r4, r4, 32
ce426f
+
ce426f
+L(align):
ce426f
+	/* Now it has checked for first 32 bytes.  */
ce426f
+	vspltisb	v0, 0
ce426f
+	vspltisb	v2, -1
ce426f
+	lvsr	v6, 0, r4   /* Compute mask.  */
ce426f
+	or	r5, r4, r7
ce426f
+	andi.	r5, r5, 0xF
ce426f
+	beq	cr0, L(aligned)
ce426f
+	andi.	r5, r7, 0xF
ce426f
+	beq	cr0, L(s1_align)
ce426f
+	lvsr	v10, 0, r7   /* Compute mask.  */
ce426f
+
ce426f
+	/* Both s1 and s2 are unaligned.  */
ce426f
+	GET16BYTES(v4, r7, v10)
ce426f
+	GET16BYTES(v5, r4, v6)
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	beq	cr6, L(match)
ce426f
+	b	L(different)
ce426f
+
ce426f
+	/* Align s1 to qw and adjust s2 address.  */
ce426f
+	.align  4
ce426f
+L(match):
ce426f
+	clrldi	r6, r7, 60
ce426f
+	subfic	r5, r6, 16
ce426f
+	add	r7, r7, r5
ce426f
+	add	r4, r4, r5
ce426f
+	andi.	r5, r4, 0xF
ce426f
+	beq	cr0, L(aligned)
ce426f
+	lvsr	v6, 0, r4
ce426f
+	/* There are 2 loops depending on the input alignment.
ce426f
+	   Each loop gets 16 bytes from s1 and s2 and compares.
ce426f
+	   Loop until a mismatch or null occurs.  */
ce426f
+L(s1_align):
ce426f
+	lvx	v4, r7, r0
ce426f
+	GET16BYTES(v5, r4, v6)
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, r7, r0
ce426f
+	GET16BYTES(v5, r4, v6)
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, r7, r0
ce426f
+	GET16BYTES(v5, r4, v6)
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, r7, r0
ce426f
+	GET16BYTES(v5, r4, v6)
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	beq	cr6, L(s1_align)
ce426f
+	b	L(different)
ce426f
+
ce426f
+	.align  4
ce426f
+L(aligned):
ce426f
+	lvx	v4, 0, r7
ce426f
+	lvx	v5, 0, r4
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, 0, r7
ce426f
+	lvx	v5, 0, r4
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, 0, r7
ce426f
+	lvx	v5, 0, r4
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	bne	cr6, L(different)
ce426f
+
ce426f
+	lvx	v4, 0, r7
ce426f
+	lvx	v5, 0, r4
ce426f
+	VCMPNEZB(v7, v5, v4)
ce426f
+	addi	r7, r7, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	beq	cr6, L(aligned)
ce426f
+
ce426f
+	/* Calculate and return the difference.  */
ce426f
+L(different):
ce426f
+	VCTZLSBB(r6, v7)
ce426f
+	VEXTUBRX(r5, r6, v4)
ce426f
+	VEXTUBRX(r4, r6, v5)
ce426f
+	subf	r3, r4, r5
ce426f
+	extsw	r3, r3
ce426f
+	blr
ce426f
+
ce426f
+	.align  4
ce426f
+L(different_nocmpb):
ce426f
+	neg	r3, r9
ce426f
+	and	r9, r9, r3
ce426f
+	cntlzd	r9, r9
ce426f
+	subfic	r9, r9, 63
ce426f
+	srd	r3, r8, r9
ce426f
+	srd	r10, r10, r9
ce426f
+	rldicl	r10, r10, 0, 56
ce426f
+	rldicl	r3, r3, 0, 56
ce426f
+	subf	r3, r10, r3
ce426f
+	extsw	r3, r3
ce426f
+	blr
ce426f
+
ce426f
+	.align	4
ce426f
+L(pagecross_check):
ce426f
+	subfic	r9, r9, 4096
ce426f
+	subfic	r7, r7, 4096
ce426f
+	cmpld	cr7, r7, r9
ce426f
+	bge	cr7, L(pagecross)
ce426f
+	mr	r7, r9
ce426f
+
ce426f
+	/* If unaligned 16 bytes reads across a 4K page boundary, it uses
ce426f
+	   a simple byte a byte comparison until the page alignment for s1
ce426f
+	   is reached.  */
ce426f
+L(pagecross):
ce426f
+	add	r7, r3, r7
ce426f
+	subf	r9, r3, r7
ce426f
+	mtctr	r9
ce426f
+
ce426f
+	.align	4
ce426f
+L(pagecross_loop):
ce426f
+	/* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
ce426f
+	   and if *s1 is '\0'.  */
ce426f
+	lbz	r9, 0(r3)
ce426f
+	lbz	r10, 0(r4)
ce426f
+	addi	r3, r3, 1
ce426f
+	addi	r4, r4, 1
ce426f
+	cmplw	cr7, r9, r10
ce426f
+	cmpdi	cr5, r9, r0
ce426f
+	bne	cr7, L(pagecross_ne)
ce426f
+	beq	cr5, L(pagecross_nullfound)
ce426f
+	bdnz	L(pagecross_loop)
ce426f
+	b	L(align)
ce426f
+
ce426f
+	.align	4
ce426f
+L(pagecross_ne):
ce426f
+	extsw	r3, r9
ce426f
+	mr	r9, r10
ce426f
+L(pagecross_retdiff):
ce426f
+	subf	r9, r9, r3
ce426f
+	extsw	r3, r9
ce426f
+	blr
ce426f
+
ce426f
+	.align	4
ce426f
+L(pagecross_nullfound):
ce426f
+	li	r3, 0
ce426f
+	b	L(pagecross_retdiff)
ce426f
+END (strcmp)
ce426f
+libc_hidden_builtin_def (strcmp)
ce426f
+#else
ce426f
+#include <sysdeps/powerpc/powerpc64/power8/strcmp.S>
ce426f
+#endif
ce426f
-- 
ce426f
2.1.0
ce426f