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