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