Blame SOURCES/glibc-rh1240351-11.patch

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