bca718
    Backport of
bca718
    commit 8bedcb5f03c62bf6001396dafdd82fbd4da7c2db
bca718
    Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
bca718
    Date:   Wed Jan 7 07:18:30 2015 -0500
bca718
    
bca718
        powerpc: Optimized strcmp for POWER8/PPC64
bca718
    
bca718
        This patch adds an optimized POWER8 strcmp using unaligned accesses.
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
    
bca718
        ChangeLog:
bca718
    	2015-01-13  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>
bca718
    
bca718
     	* sysdeps/powerpc/powerpc64/multiarch/Makefile [sysdep_routines]:
bca718
    	Add strcmp-power8 object.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
    	(__libc_ifunc_impl_list): Add __strcmp_power8 implementation.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/strcmp-power8.S: New file.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/strcmp.c (strcmp): Add
bca718
    	__strcmp_power8 implementation.
bca718
    	* sysdeps/powerpc/powerpc64/power8/strcmp.S: New file.
bca718
    	* NEWS: Update.
bca718
    
bca718
    and its dependency:
bca718
    commit e23d3d2690bf63207b1a47e83a94693daebbbfe5
bca718
    Author: Vidya Ranganathan <vidya@linux.vnet.ibm.com>
bca718
    Date:   Fri Jun 6 07:56:07 2014 -0500
bca718
    
bca718
        PowerPC: Optimized strcmp for PPC64/POWER7
bca718
    
bca718
        Optimization is achieved on 8 byte aligned strings with double word
bca718
        comparison using cmpb instruction. On unaligned strings loop unrolling
bca718
        is applied for Power7 gain.
bca718
    
bca718
        ChangeLog:
bca718
        2014-06-11  Vidya Ranganathan  <vidya@linux.vnet.ibm.com>
bca718
    
bca718
           * sysdeps/powerpc/powerpc64/power7/strcmp.S: New file: Optimization.
bca718
           * sysdeps/powerpc/powerpc64/multiarch/strcmp.c: New file:
bca718
           multiarch strcmp for PPC64.
bca718
           * sysdeps/powerpc/powerpc64/multiarch/strcmp-ppc64.S: New file.
bca718
           * sysdeps/powerpc/powerpc64/multiarch/strcmp-power7.S: New file.
bca718
           * sysdeps/powerpc/powerpc64/multiarch/Makefile: Add strcmp
bca718
           multiarch optimizations.
bca718
           * sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c:
bca718
           (__libc_ifunc_impl_list): Likewise.
bca718
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
index ef39917..27c8b65 100644
bca718
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
@@ -7,9 +7,10 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
bca718
                   memrchr-power7 memrchr-ppc64 rawmemchr-power7 \
bca718
                   stpcpy-power8 stpcpy-power7 stpcpy-ppc64 \
bca718
                   strcat-power8 strcat-power7 strcat-ppc64 \
bca718
+                  strcmp-power8 strcmp-power7 strcmp-ppc64 \
bca718
                   strcpy-power8 strcpy-power7 strcpy-ppc64 \
bca718
                   stpncpy-power8 stpncpy-power7 stpncpy-ppc64 \
bca718
-                  strncpy-power8 strncpy-power7 strncpy-ppc64 
bca718
+                  strncpy-power8 strncpy-power7 strncpy-ppc64 \
bca718
                   strncat-power7 \
bca718
                   rawmemchr-ppc64 strlen-power7 strlen-ppc64 strnlen-power7 \
bca718
                   strnlen-ppc64 strcasecmp-power7 strcasecmp_l-power7 \
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
index 23bf5dc..2b38c71 100644
bca718
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
@@ -277,5 +277,15 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bca718
 	      IFUNC_IMPL_ADD (array, i, stpncpy, 1,
bca718
 			     __stpncpy_ppc))
bca718
 
bca718
+  /* Support sysdeps/powerpc/powerpc64/multiarch/strcmp.c.  */
bca718
+  IFUNC_IMPL (i, name, strcmp,
bca718
+	      IFUNC_IMPL_ADD (array, i, strcmp,
bca718
+			      hwcap2 & PPC_FEATURE2_ARCH_2_07,
bca718
+			      __strcmp_power8)
bca718
+	      IFUNC_IMPL_ADD (array, i, strcmp,
bca718
+			      hwcap & PPC_FEATURE_HAS_VSX,
bca718
+			      __strcmp_power7)
bca718
+	      IFUNC_IMPL_ADD (array, i, strcmp, 1,
bca718
+			     __strcmp_ppc))
bca718
   return i;
bca718
 }
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp-power7.S b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power7.S
bca718
new file mode 100644
bca718
index 0000000..790ce8d
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power7.S
bca718
@@ -0,0 +1,40 @@
bca718
+/* Optimized strcmp implementation for POWER7.
bca718
+   Copyright (C) 2014 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(__strcmp_power7)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__strcmp_power7):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__strcmp_power7)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__strcmp_power7)					\
bca718
+  END_2(__strcmp_power7)
bca718
+
bca718
+#undef libc_hidden_builtin_def
bca718
+#define libc_hidden_builtin_def(name)
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power7/strcmp.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp-power8.S b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power8.S
bca718
new file mode 100644
bca718
index 0000000..dc4bfac
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp-power8.S
bca718
@@ -0,0 +1,40 @@
bca718
+/* Optimized strcmp implementation for POWER8/PPC64.
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(__strcmp_power8)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__strcmp_power8):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__strcmp_power8)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__strcmp_power8)					\
bca718
+  END_2(__strcmp_power8)
bca718
+
bca718
+#undef libc_hidden_builtin_def
bca718
+#define libc_hidden_builtin_def(name)
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power8/strcmp.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp-ppc64.S b/sysdeps/powerpc/powerpc64/multiarch/strcmp-ppc64.S
bca718
new file mode 100644
bca718
index 0000000..93d1277
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp-ppc64.S
bca718
@@ -0,0 +1,43 @@
bca718
+/* Default strcmp implementation for PowerPC64.
bca718
+   Copyright (C) 2014 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
+#if defined SHARED && !defined NOT_IN_libc
bca718
+# undef EALIGN
bca718
+# define EALIGN(name, alignt, words)				\
bca718
+  .section ".text";						\
bca718
+  ENTRY_2(__strcmp_ppc)						\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__strcmp_ppc):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__strcmp_ppc)
bca718
+
bca718
+# undef END
bca718
+# define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__strcmp_ppc)					\
bca718
+  END_2(__strcmp_ppc)
bca718
+
bca718
+# undef libc_hidden_builtin_def
bca718
+# define libc_hidden_builtin_def(name)				\
bca718
+    .globl __GI_strcmp; __GI_strcmp = __strcmp_ppc
bca718
+#endif
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/strcmp.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcmp.c b/sysdeps/powerpc/powerpc64/multiarch/strcmp.c
bca718
new file mode 100644
bca718
index 0000000..c711969
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcmp.c
bca718
@@ -0,0 +1,34 @@
bca718
+/* Multiple versions of strcmp. PowerPC64 version.
bca718
+   Copyright (C) 2014 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
+#if defined SHARED && !defined NOT_IN_libc
bca718
+# include <string.h>
bca718
+# include <shlib-compat.h>
bca718
+# include "init-arch.h"
bca718
+
bca718
+extern __typeof (strcmp) __strcmp_ppc attribute_hidden;
bca718
+extern __typeof (strcmp) __strcmp_power7 attribute_hidden;
bca718
+extern __typeof (strcmp) __strcmp_power8 attribute_hidden;
bca718
+
bca718
+libc_ifunc (strcmp,
bca718
+            (hwcap2 & PPC_FEATURE2_ARCH_2_07)
bca718
+              ? __strcmp_power8 :
bca718
+              (hwcap & PPC_FEATURE_HAS_VSX)
bca718
+              ? __strcmp_power7
bca718
+            : __strcmp_ppc);
bca718
+#endif
bca718
diff --git a/sysdeps/powerpc/powerpc64/power7/strcmp.S b/sysdeps/powerpc/powerpc64/power7/strcmp.S
bca718
new file mode 100644
bca718
index 0000000..f16a9d8
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power7/strcmp.S
bca718
@@ -0,0 +1,195 @@
bca718
+/* Optimized strcmp implementation for Power7 using 'cmpb' instruction
bca718
+   Copyright (C) 2014 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
+/* The optimization is achieved here through cmpb instruction.
bca718
+   8byte aligned strings are processed with double word comparision
bca718
+   and unaligned strings are handled effectively with loop unrolling
bca718
+   technique  */
bca718
+
bca718
+#include <sysdep.h>
bca718
+
bca718
+/* int [r3] strcmp (const char *s1 [r3], const char *s2 [r4])  */
bca718
+
bca718
+EALIGN (strcmp, 4, 0)
bca718
+	CALL_MCOUNT 2
bca718
+
bca718
+	or r9, r3, r4
bca718
+	rldicl. r10, r9, 0, 61	/* are s1 and s2 8 byte aligned..?  */
bca718
+	bne cr0, L(process_unaligned_bytes)
bca718
+
bca718
+/* process input parameters on double word aligned boundary  */
bca718
+	ld r9, 0(r4)		/* load s2 at offset=0  */
bca718
+	li r10, 0		/* load mask=0  */
bca718
+	cmpb r10, r9, r10	/* compare bytes at s2 with mask  */
bca718
+	cmpdi cr7, r10, 0	/* is NULL found ..? is end of string HIT  */
bca718
+	bne cr7, L(process_unaligned_bytes)	/* process byte by byte  */
bca718
+
bca718
+	ld r10, 0(r3)		/* load s1 at offset=0  */
bca718
+	li r8, 0		/* load mask=0  */
bca718
+	cmpb r8, r10, r8	/* compare bytes at s1 with mask  */
bca718
+	cmpdi cr7, r8, 0	/* is NULL found ..? is end of string HIT  */
bca718
+	bne cr7, L(process_unaligned_bytes)	/* process byte by byte  */
bca718
+
bca718
+/*s1 and s2 does not contain NULL now , so compare all 8 bytes in a GO  */
bca718
+	cmpb r9, r10, r9	/* compare s1 and s2  */
bca718
+	cmpdi cr7, r9, -1	/* compare result with 0xFFFFFFFFFFFFFFFF  */
bca718
+	bne cr7, L(process_unaligned_bytes)	/* s1,s2 mismatch found  */
bca718
+
bca718
+	addi r5, r3, 8		/* save next offset of s2  */
bca718
+	addi r11, r4, 8		/* save next offset of s1  */
bca718
+	ld r8, 8(r4)		/* load s2 at offset=8  */
bca718
+	li r9, 0		/* load mask=0  */
bca718
+	cmpb r9, r8, r9		/* compare bytes at s2 with mask  */
bca718
+	cmpdi cr7, r9, 0	/* NULL found ..?  */
bca718
+	bne cr7, L(processBytes)/* update input and process bytes one by one  */
bca718
+
bca718
+	mr r9, r4		/* save s2  */
bca718
+	li r10, 0		/* load mask=0  */
bca718
+
bca718
+	ld r7, 8(r3)		/* load s1 at offset=8  */
bca718
+	cmpb r6, r7, r10	/* compare bytes at s1 with mask  */
bca718
+	cmpdi cr7, r6, 0	/* is NULL found  */
bca718
+	bne cr7, L(processBytes)/* mismatch, so process one by one  */
bca718
+
bca718
+L(unrollDword):
bca718
+	cmpb r8, r7, r8		/* compare s1 and s2  */
bca718
+	cmpdi cr7, r8, -1	/* compare result with 0xFFFFFFFFFFFFFFFF  */
bca718
+	bne cr7, L(processBytes)/* mismatch with s1 and s2  */
bca718
+
bca718
+	addi r5, r3, 16		/* save offset=16 of s1  */
bca718
+	addi r4, r9, 16		/* save offset=16 of s2  */
bca718
+	ld r8, 16(r9)		/* load s2 at offset=16  */
bca718
+	cmpb r7, r8, r10	/* compare bytes at s2 with mask  */
bca718
+	cmpdi cr7, r7, 0	/* NULL found  ..?  */
bca718
+	bne cr7, L(update2processBytes)
bca718
+
bca718
+	ld r7, 16(r3)		/* load s1 at offset=16  */
bca718
+	cmpb r6, r7, r10	/* check s1 for end of string  */
bca718
+	cmpdi cr7, r6, 0	/* end of s1 ?,then handle byte by byte  */
bca718
+	bne 7,L(update2processBytes)
bca718
+
bca718
+	cmpb r8, r7, r8		/* compare s1 and s2 double words  */
bca718
+	cmpdi cr7, r8, -1	/* compare results with 0xFFFFFFFFFFFFFFFF  */
bca718
+	bne cr7,L(update2processBytes)
bca718
+
bca718
+	addi r5, r3, 24		/* update s1 to offset=24  */
bca718
+	addi r4, r9, 24		/* update s2 to offset=24  */
bca718
+
bca718
+	ld r8, 24(r9)		/* load s2  */
bca718
+	cmpb r7, r8, r10	/* compare s2 for NULL  */
bca718
+	cmpdi cr7, r7, 0	/* verify if s2 is ending now  */
bca718
+	bne cr7,L(update2processBytes)
bca718
+
bca718
+	ld r7, 24(r3)		/* load s1 at offset=24  */
bca718
+	cmpb r6, r7, r10	/* verify for NULL  */
bca718
+	cmpdi cr7, r6, 0	/* is NULL found  */
bca718
+	bne cr7, L(update2processBytes)
bca718
+
bca718
+	cmpb r8, r7, r8		/* compare s1 and s2  */
bca718
+	cmpdi cr7, r8, -1	/* are s1 and s2 same ..?  */
bca718
+	bne cr7, L(update2processBytes)
bca718
+
bca718
+	addi r7, r9, 32		/* update s2 to next double word  */
bca718
+	addi r3, r3, 32		/* update s1 to next double word  */
bca718
+
bca718
+	ld r8, 32(r9)		/* load s2  */
bca718
+	mr r4, r7		/* save s2  */
bca718
+	cmpb r6, r8, r10	/* compare s2 with NULL  */
bca718
+	cmpdi cr7, r6, 0	/* end of s2 ..? */
bca718
+	bne cr7, L(process_unaligned_bytes)
bca718
+
bca718
+	ld r6, 0(r3)		/* load and compare s1 for NULL  */
bca718
+	cmpb r5, r6, r10
bca718
+	cmpdi cr7, r5, 0
bca718
+	bne cr7, L(process_unaligned_bytes)
bca718
+
bca718
+	cmpb r8, r6, r8		/* compare s1 and s2  */
bca718
+	cmpdi cr7, r8, -1
bca718
+	bne cr7, L(process_unaligned_bytes)
bca718
+
bca718
+	addi r5, r3, 8		/* increment s1 and d2 here  */
bca718
+	addi r11, r9, 40
bca718
+
bca718
+	ld r8, 40(r9)		/* process s2 now  */
bca718
+	cmpb r9, r8, r10
bca718
+	cmpdi cr7, r9, 0
bca718
+	bne cr7, L(processBytes)
bca718
+
bca718
+	mr r9, r7
bca718
+	ld r7, 8(r3)		/* process s1 now  */
bca718
+	cmpb r6, r7, r10
bca718
+	cmpdi cr7, r6, 0
bca718
+	beq cr7, L(unrollDword)	/* unroll to compare s1 and s2  */
bca718
+
bca718
+L(processBytes):
bca718
+	mr r4, r11		/* update input params  */
bca718
+	mr r3, r5
bca718
+
bca718
+	.p2align 4
bca718
+L(process_unaligned_bytes):
bca718
+	lbz r9, 0(r3)		/* load byte from s1  */
bca718
+	lbz r10, 0(r4)		/* load byte from s2  */
bca718
+	cmpdi cr7, r9, 0	/* compare *s1 with NULL  */
bca718
+	beq cr7, L(diffOfNULL)	/* if *s1 is NULL , return *s1 - *s2  */
bca718
+	cmplw cr7, r9, r10	/* compare *s1 and *s2  */
bca718
+	bne cr7, L(ComputeDiff)	/* branch to compute difference and return  */
bca718
+
bca718
+	lbz r9, 1(r3)		/* load next byte from s1  */
bca718
+	lbz r10, 1(r4)		/* load next byte from s2  */
bca718
+	cmpdi cr7, r9, 0	/* compare *s1 with NULL  */
bca718
+	beq cr7, L(diffOfNULL)	/* if *s1 is NULL , return *s1 - *s2  */
bca718
+	cmplw cr7, r9, r10	/* compare *s1 and *s2  */
bca718
+	bne cr7, L(ComputeDiff)	/* branch to compute difference and return  */
bca718
+
bca718
+	lbz r9, 2(r3)		/* unroll 3rd byte here  */
bca718
+	lbz r10, 2(r4)
bca718
+	cmpdi cr7, r9, 0
bca718
+	beq cr7, L(diffOfNULL)
bca718
+	cmplw cr7, r9, r10
bca718
+	bne 7, L(ComputeDiff)
bca718
+
bca718
+	lbz r9, 3(r3)		/* unroll 4th byte now  */
bca718
+	lbz r10, 3(r4)
bca718
+	addi r3, r3, 4		/* increment s1 by unroll factor  */
bca718
+	cmpdi cr7, r9, 0
bca718
+	cmplw cr6, 9, r10
bca718
+	beq cr7, L(diffOfNULL)
bca718
+	addi r4, r4, 4		/* increment s2 by unroll factor  */
bca718
+	beq cr6, L(process_unaligned_bytes)	/* unroll byte processing  */
bca718
+
bca718
+	.p2align 4
bca718
+L(ComputeDiff):
bca718
+	extsw r9, r9
bca718
+	subf r10, r10, r9	/* compute s1 - s2  */
bca718
+	extsw r3, r10
bca718
+	blr			/* return  */
bca718
+
bca718
+	.p2align 4
bca718
+L(diffOfNULL):
bca718
+	li r9, 0
bca718
+	subf r10, r10, r9	/* compute s1 - s2  */
bca718
+	extsw r3, r10		/* sign extend result  */
bca718
+	blr			/* return  */
bca718
+
bca718
+	.p2align 4
bca718
+L(update2processBytes):
bca718
+	mr r3, r5		/* update and proceed  */
bca718
+	b L(process_unaligned_bytes)
bca718
+
bca718
+END (strcmp)
bca718
+libc_hidden_builtin_def (strcmp)
bca718
diff --git a/sysdeps/powerpc/powerpc64/power8/strcmp.S b/sysdeps/powerpc/powerpc64/power8/strcmp.S
bca718
new file mode 100644
bca718
index 0000000..223d891
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power8/strcmp.S
bca718
@@ -0,0 +1,257 @@
bca718
+/* Optimized strcmp 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
+   size_t [r3] strcmp (const char *s1 [r3], const char *s2 [r4])
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
+EALIGN (strcmp, 4, 0)
bca718
+	li	r0,0
bca718
+
bca718
+	/* Check if [s1]+32 or [s2]+32 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 32.  */
bca718
+
bca718
+	rldicl	r7,r3,0,52
bca718
+	rldicl	r9,r4,0,52
bca718
+	cmpldi	cr7,r7,4096-32
bca718
+	bgt	cr7,L(pagecross_check)
bca718
+	cmpldi	cr5,r9,4096-32
bca718
+	bgt	cr5,L(pagecross_check)
bca718
+
bca718
+	/* For short string up to 32 bytes, load both s1 and s2 using
bca718
+	   unaligned dwords and compare.  */
bca718
+	ld	r8,0(r3)
bca718
+	ld	r10,0(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	ld	r8,8(r3)
bca718
+	ld	r10,8(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	ld	r8,16(r3)
bca718
+	ld	r10,16(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	ld	r8,24(r3)
bca718
+	ld	r10,24(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	addi	r7,r3,32
bca718
+	addi	r4,r4,32
bca718
+
bca718
+L(align_8b):
bca718
+	/* Now it has checked for first 32 bytes, align source1 to doubleword
bca718
+	   and adjust source2 address.  */
bca718
+	rldicl	r9,r7,0,61	/* source1 alignment to doubleword  */
bca718
+	subf	r4,r9,r4	/* Adjust source2 address based on source1
bca718
+				   alignment.  */
bca718
+	rldicr	r7,r7,0,60	/* Align source1 to doubleword.  */
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.	r9,r4,0x7
bca718
+	bne	cr0,L(loop_diff_align)
bca718
+
bca718
+	/* If both source1 and source2 are doubleword aligned, there is no
bca718
+	   need for page boundary cross checks.  */
bca718
+
bca718
+	ld	r8,0(r7)
bca718
+	ld	r10,0(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	.align 4
bca718
+L(loop_equal_align):
bca718
+	ld	r8,8(r7)
bca718
+	ld	r10,8(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	ld	r8,16(r7)
bca718
+	ld	r10,16(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	ldu	r8,24(r7)
bca718
+	ldu	r10,24(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+
bca718
+	b	L(loop_equal_align)
bca718
+
bca718
+	/* A zero byte was found in r8 (s1 dword), r9 contains the cmpb
bca718
+	   result and r10 the dword from s2.  To code isolate the byte
bca718
+	   up to end (including the '\0'), masking with 0xFF the remaining
bca718
+	   ones:
bca718
+
bca718
+           #if __LITTLE_ENDIAN__
bca718
+	     (__builtin_ffsl (x) - 1) = counting trailing zero bits
bca718
+	     r9 = (__builtin_ffsl (r9) - 1) + 8;
bca718
+	     r9 = -1UL << r9
bca718
+	   #else
bca718
+	     r9  = __builtin_clzl (r9) + 8;
bca718
+	     r9  = -1UL >> r9
bca718
+	   #endif
bca718
+	     r8  = r8  | r9
bca718
+	     r10 = r10 | r9  */
bca718
+
bca718
+#ifdef __LITTLE_ENDIAN__
bca718
+	nor 	r9,r9,r9
bca718
+L(different_nocmpb):
bca718
+	neg	r3,r9
bca718
+	and	r9,r9,r3
bca718
+	cntlzd	r9,r9
bca718
+	subfic	r9,r9,63
bca718
+#else
bca718
+	not	r9,r9
bca718
+L(different_nocmpb):
bca718
+	cntlzd	r9,r9
bca718
+	subfic	r9,r9,56
bca718
+#endif
bca718
+	srd	r3,r8,r9
bca718
+	srd	r10,r10,r9
bca718
+	rldicl	r10,r10,0,56
bca718
+	rldicl	r3,r3,0,56
bca718
+	subf	r3,r10,r3
bca718
+	extsw	r3,r3
bca718
+	blr
bca718
+
bca718
+	.align	4
bca718
+L(pagecross_check):
bca718
+	subfic	r9,r9,4096
bca718
+	subfic	r7,r7,4096
bca718
+	cmpld	cr7,r7,r9
bca718
+	bge	cr7,L(pagecross)
bca718
+	mr	r7,r9
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
+L(pagecross):
bca718
+	add	r7,r3,r7
bca718
+	subf	r9,r3,r7
bca718
+	mtctr	r9
bca718
+
bca718
+	.align	4
bca718
+L(pagecross_loop):
bca718
+	/* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
bca718
+	   and if *s1 is '\0'.  */
bca718
+	lbz	r9,0(r3)
bca718
+	lbz	r10,0(r4)
bca718
+	addi	r3,r3,1
bca718
+	addi	r4,r4,1
bca718
+	cmplw	cr7,r9,r10
bca718
+	cmpdi	cr5,r9,r0
bca718
+	bne	cr7,L(pagecross_ne)
bca718
+	beq	cr5,L(pagecross_nullfound)
bca718
+	bdnz	L(pagecross_loop)
bca718
+	b	L(align_8b)
bca718
+
bca718
+	.align	4
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
+L(check_source2_byte):
bca718
+	li	r9,8
bca718
+	mtctr	r9
bca718
+
bca718
+	.align	4
bca718
+L(check_source2_byte_loop):
bca718
+	lbz	r9,0(r7)
bca718
+	lbz	r10,0(r4)
bca718
+	addi	r7,r7,1
bca718
+	addi	r4,r4,1
bca718
+	cmplw	cr7,r9,10
bca718
+	cmpdi	r5,r9,0
bca718
+	bne	cr7,L(pagecross_ne)
bca718
+	beq	cr5,L(pagecross_nullfound)
bca718
+	bdnz	L(check_source2_byte_loop)
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	5
bca718
+L(loop_unaligned):
bca718
+	ld	r8,0(r7)
bca718
+	ld	r10,0(r4)
bca718
+	cmpb	r12,r8,r0
bca718
+	cmpb	r11,r8,r10
bca718
+	orc.	r9,r12,r11
bca718
+	bne	cr0,L(different_nocmpb)
bca718
+	addi	r7,r7,8
bca718
+	addi	r4,r4,8
bca718
+
bca718
+L(loop_diff_align):
bca718
+	/* Check if [src2]+8 cross a 4k page boundary:
bca718
+
bca718
+	     srcin2 % PAGE_SIZE > (PAGE_SIZE - 8)
bca718
+
bca718
+	     with PAGE_SIZE being 4096.  */
bca718
+	rldicl	r9,r4,0,52
bca718
+	cmpldi	cr7,r9,4088
bca718
+	ble	cr7,L(loop_unaligned)
bca718
+	b	L(check_source2_byte)
bca718
+
bca718
+	.align	4
bca718
+L(pagecross_ne):
bca718
+	extsw	r3,r9
bca718
+	mr	r9,r10
bca718
+L(pagecross_retdiff):
bca718
+	subf	r9,r9,r3
bca718
+	extsw	r3,r9
bca718
+	blr
bca718
+
bca718
+	.align	4
bca718
+L(pagecross_nullfound):
bca718
+	li	r3,0
bca718
+	b	L(pagecross_retdiff)
bca718
+END (strcmp)
bca718
+libc_hidden_builtin_def (strcmp)