ce426f
    Backport of
ce426f
    commit d3b00f468bec441596877a685a19f43dee88657f
ce426f
    Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
ce426f
    Date:   Fri Jan 9 16:04:26 2015 -0500
ce426f
    
ce426f
        powerpc: Optimized strncmp for POWER8/PPC64
ce426f
    
ce426f
        This patch adds an optimized POWER8 strncmp.  The implementation focus
ce426f
        on speeding up unaligned cases follwing the ideas of power8 strcmp.
ce426f
    
ce426f
        The algorithm first check the initial 16 bytes, then align the first
ce426f
        function source and uses unaligned loads on second argument only.
ce426f
        Aditional checks for page boundaries are done for unaligned cases
ce426f
        (where sources alignment are different).
ce426f
    
ce426f
        ChangeLog:
ce426f
    	2015-01-13  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>
ce426f
    
ce426f
    	* sysdeps/powerpc/powerpc64/multiarch/strncmp-power8.S: New file.
ce426f
    	* sysdeps/powerpc/powerpc64/power8/strncmp.S: New file.
ce426f
    	* sysdeps/powerpc/powerpc64/multiarch/Makefile [sysdep_routines]: Add
ce426f
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
index 27c8b65..677d8ce 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
@@ -14,8 +14,9 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
ce426f
                   strncat-power7 \
ce426f
                   rawmemchr-ppc64 strlen-power7 strlen-ppc64 strnlen-power7 \
ce426f
                   strnlen-ppc64 strcasecmp-power7 strcasecmp_l-power7 \
ce426f
-                  strncase-power7 strncase_l-power7 strncmp-power7 \
ce426f
-                  strncmp-power4 strncmp-ppc64 strchr-power7 strchr-ppc64 \
ce426f
+                  strncase-power7 strncase_l-power7 \
ce426f
+                  strncmp-power8 strncmp-power7 strncmp-power4 strncmp-ppc64 \
ce426f
+                  strchr-power7 strchr-ppc64 \
ce426f
                   strchrnul-power7 strchrnul-ppc64 wcschr-power7 \
ce426f
                   wcschr-power6 wcschr-ppc64 wcsrchr-power7 wcsrchr-power6 \
ce426f
                   wcsrchr-ppc64 wcscpy-power7 wcscpy-power6 wcscpy-ppc64 \
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
index 2b38c71..a540abf 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
@@ -100,6 +100,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
ce426f
 
ce426f
   /* Support sysdeps/powerpc/powerpc64/multiarch/strncmp.c.  */
ce426f
   IFUNC_IMPL (i, name, strncmp,
ce426f
+              IFUNC_IMPL_ADD (array, i, strncmp, hwcap2 & PPC_FEATURE2_ARCH_2_07,
ce426f
+                              __strncmp_power8)
ce426f
              IFUNC_IMPL_ADD (array, i, strncmp, hwcap & PPC_FEATURE_HAS_VSX,
ce426f
                              __strncmp_power7)
ce426f
              IFUNC_IMPL_ADD (array, i, strncmp, hwcap & PPC_FEATURE_POWER4,
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncmp-power8.S b/sysdeps/powerpc/powerpc64/multiarch/strncmp-power8.S
ce426f
new file mode 100644
ce426f
index 0000000..8d7223d
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncmp-power8.S
ce426f
@@ -0,0 +1,40 @@
ce426f
+/* Copyright (C) 2015 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(__strncmp_power8)					\
ce426f
+  .align ALIGNARG(alignt);					\
ce426f
+  EALIGN_W_##words;						\
ce426f
+  BODY_LABEL(__strncmp_power8):					\
ce426f
+  cfi_startproc;						\
ce426f
+  LOCALENTRY(__strncmp_power8)
ce426f
+
ce426f
+#undef END
ce426f
+#define END(name)						\
ce426f
+  cfi_endproc;							\
ce426f
+  TRACEBACK(__strncmp_power8)					\
ce426f
+  END_2(__strncmp_power8)
ce426f
+
ce426f
+
ce426f
+#undef libc_hidden_builtin_def
ce426f
+#define libc_hidden_builtin_def(name)
ce426f
+
ce426f
+#include <sysdeps/powerpc/powerpc64/power8/strncmp.S>
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncmp.c b/sysdeps/powerpc/powerpc64/multiarch/strncmp.c
ce426f
index 9829d69..5e76783 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/strncmp.c
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncmp.c
ce426f
@@ -25,13 +25,16 @@
ce426f
 extern __typeof (strncmp) __strncmp_ppc attribute_hidden;
ce426f
 extern __typeof (strncmp) __strncmp_power4 attribute_hidden;
ce426f
 extern __typeof (strncmp) __strncmp_power7 attribute_hidden;
ce426f
+extern __typeof (strncmp) __strncmp_power8 attribute_hidden;
ce426f
 
ce426f
 /* Avoid DWARF definition DIE on ifunc symbol so that GDB can handle
ce426f
    ifunc symbol properly.  */
ce426f
 libc_ifunc (strncmp,
ce426f
-            (hwcap & PPC_FEATURE_HAS_VSX)
ce426f
-            ? __strncmp_power7 :
ce426f
-	      (hwcap & PPC_FEATURE_POWER4)
ce426f
+            (hwcap2 & PPC_FEATURE2_ARCH_2_07)
ce426f
+            ? __strncmp_power8 :
ce426f
+              (hwcap & PPC_FEATURE_HAS_VSX)
ce426f
+              ? __strncmp_power7 :
ce426f
+		(hwcap & PPC_FEATURE_POWER4)
ce426f
 		? __strncmp_power4
ce426f
             : __strncmp_ppc);
ce426f
 #endif
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power8/strncmp.S b/sysdeps/powerpc/powerpc64/power8/strncmp.S
ce426f
new file mode 100644
ce426f
index 0000000..56c814b
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/power8/strncmp.S
ce426f
@@ -0,0 +1,323 @@
ce426f
+/* Optimized strncmp implementation for PowerPC64/POWER8.
ce426f
+   Copyright (C) 2015 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
+/* Implements the function
ce426f
+
ce426f
+   int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t [r5] n)
ce426f
+
ce426f
+   The implementation uses unaligned doubleword access to avoid specialized
ce426f
+   code paths depending of data alignment.  Although recent powerpc64 uses
ce426f
+   64K as default, the page cross handling assumes minimum page size of
ce426f
+   4k.  */
ce426f
+
ce426f
+	.machine  power7
ce426f
+EALIGN (strncmp, 4, 0)
ce426f
+	/* Check if size is 0.  */
ce426f
+	mr.	r10,r5
ce426f
+	beq	cr0,L(ret0)
ce426f
+
ce426f
+	/* Check if [s1]+16 or [s2]+16 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 16.  */
ce426f
+	rldicl	r8,r3,0,52
ce426f
+	cmpldi	cr7,r8,4096-16
ce426f
+	bgt	cr7,L(pagecross)
ce426f
+	rldicl	r9,r4,0,52
ce426f
+	cmpldi	cr7,r9,4096-16
ce426f
+	bgt	cr7,L(pagecross)
ce426f
+
ce426f
+	/* For short string up to 16 bytes, load both s1 and s2 using
ce426f
+	   unaligned dwords and compare.  */
ce426f
+	ld	r7,0(r3)
ce426f
+	ld	r9,0(r4)
ce426f
+	li	r8,0
ce426f
+	cmpb	r8,r7,r8
ce426f
+	cmpb	r6,r7,r9
ce426f
+	orc.	r8,r8,r6
ce426f
+	bne	cr0,L(different1)
ce426f
+
ce426f
+	/* If the string compared are equal, but size is less or equal
ce426f
+	   to 8, return 0.  */
ce426f
+	cmpldi	cr7,r10,8
ce426f
+	li	r9,0
ce426f
+	ble	cr7,L(ret1)
ce426f
+	addi	r5,r10,-8
ce426f
+
ce426f
+	ld	r7,8(r3)
ce426f
+	ld	r9,8(r4)
ce426f
+	cmpb	r8,r7,r8
ce426f
+	cmpb	r6,r7,r9
ce426f
+	orc.	r8,r8,r6
ce426f
+	bne	cr0,L(different0)
ce426f
+
ce426f
+	cmpldi	cr7,r5,8
ce426f
+	mr	r9,r8
ce426f
+	ble	cr7,L(ret1)
ce426f
+
ce426f
+	/* Update pointers and size.  */
ce426f
+	addi	r10,r10,-16
ce426f
+	addi	r3,r3,16
ce426f
+	addi	r4,r4,16
ce426f
+
ce426f
+	/* Now it has checked for first 16 bytes, align source1 to doubleword
ce426f
+	   and adjust source2 address.  */
ce426f
+L(align_8b):
ce426f
+	rldicl	r5,r3,0,61
ce426f
+	rldicr	r3,r3,0,60
ce426f
+	subf	r4,r5,r4
ce426f
+	add	r10,r10,r5
ce426f
+
ce426f
+	/* At this point, source1 alignment is 0 and source2 alignment is
ce426f
+	   between 0 and 7.  Check is source2 alignment is 0, meaning both
ce426f
+	   sources have the same alignment.  */
ce426f
+	andi.	r8,r4,0x7
ce426f
+	beq	cr0,L(loop_eq_align_0)
ce426f
+
ce426f
+	li	r5,0
ce426f
+	b	L(loop_ne_align_1)
ce426f
+
ce426f
+	/* If source2 is unaligned to doubleword, the code needs to check
ce426f
+	   on each interation if the unaligned doubleword access will cross
ce426f
+	   a 4k page boundary.  */
ce426f
+	.align 4
ce426f
+L(loop_ne_align_0):
ce426f
+	ld	r7,0(r3)
ce426f
+	ld	r9,0(r4)
ce426f
+	cmpb	r8,r7,r5
ce426f
+	cmpb	r6,r7,r9
ce426f
+	orc.	r8,r8,r6
ce426f
+	bne	cr0,L(different1)
ce426f
+
ce426f
+	cmpldi	cr7,r10,8
ce426f
+	ble	cr7,L(ret0)
ce426f
+	addi	r10,r10,-8
ce426f
+	addi	r3,r3,8
ce426f
+	addi	r4,r4,8
ce426f
+L(loop_ne_align_1):
ce426f
+	rldicl	r9,r4,0,52
ce426f
+	cmpldi	r7,r9,4088
ce426f
+	ble	cr7,L(loop_ne_align_0)
ce426f
+	cmpdi	cr7,r10,0
ce426f
+	beq	cr7,L(ret0)
ce426f
+
ce426f
+	lbz	r9,0(r3)
ce426f
+	lbz	r8,0(r4)
ce426f
+	cmplw	cr7,r9,r8
ce426f
+	bne	cr7,L(byte_ne_4)
ce426f
+	cmpdi	cr7,r9,0
ce426f
+	beq	cr7,L(size_reached_0)
ce426f
+
ce426f
+	li	r9,r7
ce426f
+	addi	r8,r3,1
ce426f
+	mtctr	r9
ce426f
+	addi	r4,r4,1
ce426f
+	addi	r10,r10,-1
ce426f
+	addi	r3,r3,8
ce426f
+
ce426f
+	/* The unaligned read of source2 will cross a 4K page boundary,
ce426f
+	   and the different byte or NULL maybe be in the remaining page
ce426f
+	   bytes.  Since it can not use the unaligned load the algorithm
ce426f
+	   reads and compares 8 bytes to keep source1 doubleword aligned.  */
ce426f
+	.align 4
ce426f
+L(loop_ne_align_byte):
ce426f
+	cmpdi	cr7,r10,0
ce426f
+	addi	r10,r10,-1
ce426f
+	beq	cr7,L(ret0)
ce426f
+	lbz	r9,0(r8)
ce426f
+	lbz	r7,0(r4)
ce426f
+	addi	r8,r8,1
ce426f
+	addi	r4,r4,1
ce426f
+	cmplw	cr7,r9,r7
ce426f
+	cmpdi	cr5,r9,0
ce426f
+	bne	cr7,L(size_reached_2)
ce426f
+	beq	cr5,L(size_reached_0)
ce426f
+	bdnz	L(loop_ne_align_byte)
ce426f
+
ce426f
+	cmpdi	cr7,r10,0
ce426f
+	bne+	cr7,L(loop_ne_align_0)
ce426f
+
ce426f
+	.align 4
ce426f
+L(ret0):
ce426f
+	li	r9,0
ce426f
+L(ret1):
ce426f
+	mr	r3,r9
ce426f
+	blr
ce426f
+
ce426f
+	/* The code now check if r8 and r10 are different by issuing a
ce426f
+	   cmpb and shift the result based on its output:
ce426f
+
ce426f
+	#ifdef __LITTLE_ENDIAN__
ce426f
+	  leadzero = (__builtin_ffsl (z1) - 1);
ce426f
+	  leadzero = leadzero > (n-1)*8 ? (n-1)*8 : leadzero;
ce426f
+	  r1 = (r1 >> leadzero) & 0xFFUL;
ce426f
+	  r2 = (r2 >> leadzero) & 0xFFUL;
ce426f
+	#else
ce426f
+	  leadzero = __builtin_clzl (z1);
ce426f
+	  leadzero = leadzero > (n-1)*8 ? (n-1)*8 : leadzero;
ce426f
+	  r1 = (r1 >> (56 - leadzero)) & 0xFFUL;
ce426f
+	  r2 = (r2 >> (56 - leadzero)) & 0xFFUL;
ce426f
+	#endif
ce426f
+	  return r1 - r2;  */
ce426f
+
ce426f
+	.align 4
ce426f
+L(different0):
ce426f
+	mr	r10,r5
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+L(different1):
ce426f
+        neg	r11,r8
ce426f
+        sldi	r10,r10,3
ce426f
+        and	r8,r11,r8
ce426f
+        addi	r10,r10,-8
ce426f
+        cntlzd	r8,r8
ce426f
+        subfic	r8,r8,63
ce426f
+        extsw 	r8,r8
ce426f
+        cmpld	cr7,r8,r10
ce426f
+        ble	cr7,L(different2)
ce426f
+        mr	r8,r10
ce426f
+L(different2):
ce426f
+        extsw	r8,r8
ce426f
+#else
ce426f
+L(different1):
ce426f
+	addi	r10,r10,-1
ce426f
+	cntlzd	r8,r8
ce426f
+	sldi	r10,r10,3
ce426f
+	cmpld	cr7,r8,r10
ce426f
+	blt	cr7,L(different2)
ce426f
+	mr	r8,r10
ce426f
+L(different2):
ce426f
+	subfic	r8,r8,56
ce426f
+#endif
ce426f
+	srd	r7,r7,r8
ce426f
+	srd	r9,r9,r8
ce426f
+	rldicl	r3,r7,0,56
ce426f
+	rldicl	r9,r9,0,56
ce426f
+	subf	r9,r9,3
ce426f
+	extsw	r9,r9
ce426f
+	mr	r3,r9
ce426f
+	blr
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
+	.align 4
ce426f
+L(pagecross):
ce426f
+	lbz	r7,0(r3)
ce426f
+	lbz	r9,0(r4)
ce426f
+	subfic	r8,r8,4095
ce426f
+	cmplw	cr7,r9,r7
ce426f
+	bne	cr7,L(byte_ne_3)
ce426f
+	cmpdi	cr7,r9,0
ce426f
+	beq	cr7,L(byte_ne_0)
ce426f
+	addi	r10,r10,-1
ce426f
+	subf	r7,r8,r10
ce426f
+	subf	r9,r7,r10
ce426f
+	addi	r9,r9,1
ce426f
+	mtctr	r9
ce426f
+	b	L(pagecross_loop1)
ce426f
+
ce426f
+	.align 4
ce426f
+L(pagecross_loop0):
ce426f
+	beq	cr7,L(ret0)
ce426f
+	lbz	r9,0(r3)
ce426f
+	lbz	r8,0(r4)
ce426f
+	addi	r10,r10,-1
ce426f
+	cmplw	cr7,r9,r8
ce426f
+	cmpdi	cr5,r9,0
ce426f
+	bne	r7,L(byte_ne_2)
ce426f
+	beq	r5,L(byte_ne_0)
ce426f
+L(pagecross_loop1):
ce426f
+	cmpdi	cr7,r10,0
ce426f
+	addi	r3,r3,1
ce426f
+	addi	r4,r4,1
ce426f
+	bdnz	L(pagecross_loop0)
ce426f
+	cmpdi	cr7,r7,0
ce426f
+	li	r9,0
ce426f
+	bne+	cr7,L(align_8b)
ce426f
+	b	L(ret1)
ce426f
+
ce426f
+	/* If both source1 and source2 are doubleword aligned, there is no
ce426f
+	   need for page boundary cross checks.  */
ce426f
+	.align 4
ce426f
+L(loop_eq_align_0):
ce426f
+	ld	r7,0(r3)
ce426f
+	ld	r9,0(r4)
ce426f
+	cmpb	r8,r7,r8
ce426f
+	cmpb	r6,r7,r9
ce426f
+	orc.	r8,r8,r6
ce426f
+	bne	cr0,L(different1)
ce426f
+
ce426f
+	cmpldi	cr7,r10,8
ce426f
+	ble	cr7,L(ret0)
ce426f
+	addi	r9,r10,-9
ce426f
+
ce426f
+	li	r5,0
ce426f
+	srdi	r9,r9,3
ce426f
+	addi	r9,r9,1
ce426f
+	mtctr	r9
ce426f
+	b	L(loop_eq_align_2)
ce426f
+
ce426f
+	.align 4
ce426f
+L(loop_eq_align_1):
ce426f
+	bdz	L(ret0)
ce426f
+L(loop_eq_align_2):
ce426f
+	ldu	r7,8(r3)
ce426f
+	addi	r10,r10,-8
ce426f
+	ldu	r9,8(r4)
ce426f
+	cmpb	r8,r7,r5
ce426f
+	cmpb	r6,r7,r9
ce426f
+	orc.	r8,r8,r6
ce426f
+	beq	cr0,L(loop_eq_align_1)
ce426f
+	b	L(different1)
ce426f
+
ce426f
+	.align 4
ce426f
+L(byte_ne_0):
ce426f
+	li	r7,0
ce426f
+L(byte_ne_1):
ce426f
+	subf	r9,r9,r7
ce426f
+	extsw	r9,r9
ce426f
+	b	L(ret1)
ce426f
+
ce426f
+	.align 4
ce426f
+L(byte_ne_2):
ce426f
+	extsw	r7,r9
ce426f
+	mr	r9,r8
ce426f
+	b	L(byte_ne_1)
ce426f
+L(size_reached_0):
ce426f
+	li	r10,0
ce426f
+L(size_reached_1):
ce426f
+	subf	r9,r9,r10
ce426f
+	extsw	r9,r9
ce426f
+	b	L(ret1)
ce426f
+L(size_reached_2):
ce426f
+	extsw	r10,r9
ce426f
+	mr	r9,r7
ce426f
+	b	L(size_reached_1)
ce426f
+L(byte_ne_3):
ce426f
+	extsw	r7,r7
ce426f
+	b	L(byte_ne_1)
ce426f
+L(byte_ne_4):
ce426f
+	extsw	r10,r9
ce426f
+	mr	r9,r8
ce426f
+	b	L(size_reached_1)
ce426f
+END(strncmp)
ce426f
+libc_hidden_builtin_def(strncmp)