bca718
    Backport of:
bca718
    
bca718
    commit f06a4faf8a2b4d046eb40e94b47948cc47d79902
bca718
    Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
bca718
    Date:   Wed Dec 31 11:47:41 2014 -0500
bca718
    
bca718
        powerpc: Optimized st{r,p}ncpy for POWER8/PPC64
bca718
    
bca718
        This patch adds an optimized POWER8 st{r,p}ncpy using unaligned accesses.
bca718
        It shows 10%-80% improvement over the optimized POWER7 one that uses
bca718
        only aligned accesses, specially on unaligned inputs.
bca718
    
bca718
        The algorithm first read and check 16 bytes (if inputs do not cross a 4K
bca718
        page size).  The it realign source to 16-bytes and issue a 16 bytes read
bca718
        and compare loop to speedup null byte checks for large strings.  Also,
bca718
        different from POWER7 optimization, the null pad is done inline in the
bca718
        implementation using possible unaligned accesses, instead of realying on
bca718
        a memset call.  Special case is added for page cross reads.
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 strncpy-power8 and stpncpy-power8 objects.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
    	(__libc_ifunc_impl_list): Add __strncpy_power8 and stpncpy_power8
bca718
    	implementations.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/stpncpy-power8.S: New file.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/stpncpy.c (__stpncpy): Add
bca718
    	__stpncpy_power8 implementation.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/strncpy-power8.S: New file.
bca718
    	* sysdeps/powerpc/powerpc64/multiarch/strncpy.c (strncpy): Add
bca718
    	__strncpy_power8 implementation.
bca718
    	* sysdeps/powerpc/powerpc64/power8/stpncpy.S: New file.
bca718
    	* sysdeps/powerpc/powerpc64/power8/strncpy.S: New file.
bca718
    	* NEWS: Update.
bca718
    
bca718
    and its dependency:
bca718
    
bca718
    commit f360f94a05570045be615649e9a411cefba2e210
bca718
    Author: Vidya Ranganathan <vidya@linux.vnet.ibm.com>
bca718
    Date:   Mon May 5 19:10:45 2014 -0500
bca718
    
bca718
        PowerPC: strncpy/stpncpy optimization for PPC64/POWER7
bca718
    
bca718
        The optimization is achieved by following techniques:
bca718
          > data alignment [gain from aligned memory access on read/write]
bca718
          > POWER7 gains performance with loop unrolling/unwinding
bca718
            [gain by reduction of branch penalty].
bca718
          > zero padding done by calling optimized memset
bca718
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
index 74ae710..ef39917 100644
bca718
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
bca718
@@ -8,6 +8,8 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
bca718
                   stpcpy-power8 stpcpy-power7 stpcpy-ppc64 \
bca718
                   strcat-power8 strcat-power7 strcat-ppc64 \
bca718
                   strcpy-power8 strcpy-power7 strcpy-ppc64 \
bca718
+                  stpncpy-power8 stpncpy-power7 stpncpy-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 4e5bb17..23bf5dc 100644
bca718
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
bca718
@@ -255,5 +255,27 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bca718
              IFUNC_IMPL_ADD (array, i, wcscpy, 1,
bca718
                              __wcscpy_ppc))
bca718
 
bca718
+  /* Support sysdeps/powerpc/powerpc64/multiarch/strncpy.c.  */
bca718
+  IFUNC_IMPL (i, name, strncpy,
bca718
+	      IFUNC_IMPL_ADD (array, i, strncpy,
bca718
+			      hwcap2 & PPC_FEATURE2_ARCH_2_07,
bca718
+			      __strncpy_power8)
bca718
+	      IFUNC_IMPL_ADD (array, i, strncpy,
bca718
+			      hwcap & PPC_FEATURE_HAS_VSX,
bca718
+			      __strncpy_power7)
bca718
+	      IFUNC_IMPL_ADD (array, i, strncpy, 1,
bca718
+			     __strncpy_ppc))
bca718
+
bca718
+  /* Support sysdeps/powerpc/powerpc64/multiarch/stpncpy.c.  */
bca718
+  IFUNC_IMPL (i, name, stpncpy,
bca718
+	      IFUNC_IMPL_ADD (array, i, stpncpy,
bca718
+			      hwcap2 & PPC_FEATURE2_ARCH_2_07,
bca718
+			      __stpncpy_power8)
bca718
+	      IFUNC_IMPL_ADD (array, i, stpncpy,
bca718
+			      hwcap & PPC_FEATURE_HAS_VSX,
bca718
+			      __stpncpy_power7)
bca718
+	      IFUNC_IMPL_ADD (array, i, stpncpy, 1,
bca718
+			     __stpncpy_ppc))
bca718
+
bca718
   return i;
bca718
 }
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power7.S b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power7.S
bca718
new file mode 100644
bca718
index 0000000..e29674f
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power7.S
bca718
@@ -0,0 +1,44 @@
bca718
+/* Optimized stpncpy 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
+#define USE_AS_STPNCPY
bca718
+
bca718
+#undef EALIGN
bca718
+#define EALIGN(name, alignt, words)				\
bca718
+  .section ".text";						\
bca718
+  ENTRY_2(__stpncpy_power7)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__stpncpy_power7):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__stpncpy_power7)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__stpncpy_power7)					\
bca718
+  END_2(__stpncpy_power7)
bca718
+
bca718
+#undef libc_hidden_builtin_def
bca718
+#define libc_hidden_builtin_def(name)
bca718
+
bca718
+#define MEMSET __memset_power7
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power7/stpncpy.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power8.S b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power8.S
bca718
new file mode 100644
bca718
index 0000000..d5d835d
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power8.S
bca718
@@ -0,0 +1,39 @@
bca718
+/* Optimized stpncpy implementation for 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
+#define USE_AS_STPNCPY
bca718
+
bca718
+#undef EALIGN
bca718
+#define EALIGN(name, alignt, words)				\
bca718
+  .section ".text";						\
bca718
+  ENTRY_2(__stpncpy_power8)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__stpncpy_power8):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__stpncpy_power8)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__stpncpy_power8)					\
bca718
+  END_2(__stpncpy_power8)
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power8/stpncpy.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-ppc64.c
bca718
new file mode 100644
bca718
index 0000000..74f47a7
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-ppc64.c
bca718
@@ -0,0 +1,26 @@
bca718
+/* Default stpncpy 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
+#define STPNCPY __stpncpy_ppc
bca718
+#ifdef SHARED
bca718
+#undef libc_hidden_def
bca718
+#define libc_hidden_def(name) \
bca718
+  __hidden_ver1 (__stpncpy_ppc, __GI___stpncpy, __stpncpy_ppc);
bca718
+#endif
bca718
+
bca718
+#include <string/stpncpy.c>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c b/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c
bca718
new file mode 100644
bca718
index 0000000..3ee50e5
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c
bca718
@@ -0,0 +1,36 @@
bca718
+/* Multiple versions of stpncpy. 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
+#ifndef NOT_IN_libc
bca718
+# include <string.h>
bca718
+# include <shlib-compat.h>
bca718
+# include "init-arch.h"
bca718
+
bca718
+extern __typeof (__stpncpy) __stpncpy_ppc attribute_hidden;
bca718
+extern __typeof (__stpncpy) __stpncpy_power7 attribute_hidden;
bca718
+extern __typeof (__stpncpy) __stpncpy_power8 attribute_hidden;
bca718
+
bca718
+libc_ifunc (__stpncpy,
bca718
+            (hwcap2 & PPC_FEATURE2_ARCH_2_07)
bca718
+            ? __stpncpy_power8 :
bca718
+              (hwcap & PPC_FEATURE_HAS_VSX)
bca718
+              ? __stpncpy_power7
bca718
+            : __stpncpy_ppc);
bca718
+
bca718
+weak_alias (__stpncpy, stpncpy)
bca718
+#endif
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncpy-power7.S b/sysdeps/powerpc/powerpc64/multiarch/strncpy-power7.S
bca718
new file mode 100644
bca718
index 0000000..be349f9
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncpy-power7.S
bca718
@@ -0,0 +1,42 @@
bca718
+/* Optimized strncpy 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(__strncpy_power7)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__strncpy_power7):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__strncpy_power7)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__strncpy_power7)					\
bca718
+  END_2(__strncpy_power7)
bca718
+
bca718
+#undef libc_hidden_builtin_def
bca718
+#define libc_hidden_builtin_def(name)
bca718
+
bca718
+#define MEMSET __memset_power7
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power7/strncpy.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncpy-power8.S b/sysdeps/powerpc/powerpc64/multiarch/strncpy-power8.S
bca718
new file mode 100644
bca718
index 0000000..ed906a4
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncpy-power8.S
bca718
@@ -0,0 +1,40 @@
bca718
+/* Optimized strncpy implementation for 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
+#undef EALIGN
bca718
+#define EALIGN(name, alignt, words)				\
bca718
+  .section ".text";						\
bca718
+  ENTRY_2(__strncpy_power8)					\
bca718
+  .align ALIGNARG(alignt);					\
bca718
+  EALIGN_W_##words;						\
bca718
+  BODY_LABEL(__strncpy_power8):					\
bca718
+  cfi_startproc;						\
bca718
+  LOCALENTRY(__strncpy_power8)
bca718
+
bca718
+#undef END
bca718
+#define END(name)						\
bca718
+  cfi_endproc;							\
bca718
+  TRACEBACK(__strncpy_power8)					\
bca718
+  END_2(__strncpy_power8)
bca718
+
bca718
+#undef libc_hidden_builtin_def
bca718
+#define libc_hidden_builtin_def(name)
bca718
+
bca718
+#include <sysdeps/powerpc/powerpc64/power8/strncpy.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncpy-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/strncpy-ppc64.c
bca718
new file mode 100644
bca718
index 0000000..e3111d2
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncpy-ppc64.c
bca718
@@ -0,0 +1,33 @@
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 <string.h>
bca718
+
bca718
+#define STRNCPY __strncpy_ppc
bca718
+#undef weak_alias
bca718
+#define weak_alias(name, aliasname) \
bca718
+  extern __typeof (__strncpy_ppc) aliasname \
bca718
+    __attribute__ ((weak, alias ("__strncpy_ppc")));
bca718
+#if !defined(NOT_IN_libc) && defined(SHARED)
bca718
+# undef libc_hidden_builtin_def
bca718
+# define libc_hidden_builtin_def(name) \
bca718
+  __hidden_ver1(__strncpy_ppc, __GI_strncpy, __strncpy_ppc);
bca718
+#endif
bca718
+
bca718
+extern __typeof (strncpy) __strncpy_ppc attribute_hidden;
bca718
+
bca718
+#include <string/strncpy.c>
bca718
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strncpy.c b/sysdeps/powerpc/powerpc64/multiarch/strncpy.c
bca718
new file mode 100644
bca718
index 0000000..19927bc
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/multiarch/strncpy.c
bca718
@@ -0,0 +1,38 @@
bca718
+/* Multiple versions of strncpy.
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
+/* Define multiple versions only for definition in libc. */
bca718
+#ifndef NOT_IN_libc
bca718
+# include <string.h>
bca718
+# include <shlib-compat.h>
bca718
+# include "init-arch.h"
bca718
+
bca718
+extern __typeof (strncpy) __strncpy_ppc attribute_hidden;
bca718
+extern __typeof (strncpy) __strncpy_power7 attribute_hidden;
bca718
+extern __typeof (strncpy) __strncpy_power8 attribute_hidden;
bca718
+
bca718
+/* Avoid DWARF definition DIE on ifunc symbol so that GDB can handle
bca718
+ ifunc symbol properly. */
bca718
+libc_ifunc (strncpy,
bca718
+            (hwcap2 & PPC_FEATURE2_ARCH_2_07)
bca718
+            ? __strncpy_power8 :
bca718
+              (hwcap & PPC_FEATURE_HAS_VSX)
bca718
+              ? __strncpy_power7
bca718
+            : __strncpy_ppc);
bca718
+
bca718
+#endif
bca718
diff --git a/sysdeps/powerpc/powerpc64/power7/stpncpy.S b/sysdeps/powerpc/powerpc64/power7/stpncpy.S
bca718
new file mode 100644
bca718
index 0000000..a539093
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power7/stpncpy.S
bca718
@@ -0,0 +1,24 @@
bca718
+/* Optimized stpncpy implementation for PowerPC64/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
+#define USE_AS_STPNCPY
bca718
+#include <sysdeps/powerpc/powerpc64/power7/strncpy.S>
bca718
+
bca718
+weak_alias (__stpncpy, stpncpy)
bca718
+libc_hidden_def (__stpncpy)
bca718
+libc_hidden_builtin_def (stpncpy)
bca718
diff --git a/sysdeps/powerpc/powerpc64/power7/strncpy.S b/sysdeps/powerpc/powerpc64/power7/strncpy.S
bca718
new file mode 100644
bca718
index 0000000..51860df
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power7/strncpy.S
bca718
@@ -0,0 +1,338 @@
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
+/* Implements the functions
bca718
+
bca718
+   char * [r3] strncpy (char *dst [r3], const char *src [r4], size_t n [r5])
bca718
+
bca718
+   AND
bca718
+
bca718
+   char * [r3] stpncpy (char *dst [r3], const char *src [r4], size_t n [r5])
bca718
+
bca718
+   The algorithm is as follows:
bca718
+   > if src and dest are 8 byte aligned, perform double word copy
bca718
+     else
bca718
+   > copy byte by byte on unaligned addresses.
bca718
+
bca718
+   The aligned comparison are made using cmpb instructions.  */
bca718
+
bca718
+/* The focus on optimization for performance improvements are as follows:
bca718
+   1. data alignment [gain from aligned memory access on read/write]
bca718
+   2. POWER7 gains performance with loop unrolling/unwinding
bca718
+      [gain by reduction of branch penalty].
bca718
+   3. The final pad with null bytes is done by calling an optimized
bca718
+      memset.  */
bca718
+
bca718
+#ifdef USE_AS_STPNCPY
bca718
+# define FUNC_NAME __stpncpy
bca718
+#else
bca718
+# define FUNC_NAME strncpy
bca718
+#endif
bca718
+
bca718
+#define		FRAMESIZE	(FRAME_MIN_SIZE+32)
bca718
+
bca718
+#ifndef MEMSET
bca718
+/* For builds with no IFUNC support, local calls should be made to internal
bca718
+   GLIBC symbol (created by libc_hidden_builtin_def).  */
bca718
+# ifdef SHARED
bca718
+#  define MEMSET   __GI_memset
bca718
+# else
bca718
+#  define MEMSET   memset
bca718
+# endif
bca718
+#endif
bca718
+
bca718
+	.machine  power7
bca718
+EALIGN(FUNC_NAME, 4, 0)
bca718
+	CALL_MCOUNT 3
bca718
+
bca718
+	mflr r0			/* load link register LR to r0  */
bca718
+	or r10, r3, r4		/* to verify source and destination  */
bca718
+	rldicl. r8, r10, 0, 61	/* is double word aligned .. ?  */
bca718
+
bca718
+	std r19, -8(r1)		/* save callers register , r19  */
bca718
+	std r18, -16(r1)	/* save callers register , r18  */
bca718
+	std r0, 16(r1)		/* store the link register  */
bca718
+	stdu r1, -FRAMESIZE(r1)	/* create the stack frame  */
bca718
+
bca718
+	mr r9, r3		/* save r3 into r9 for use  */
bca718
+	mr r18, r3		/* save r3 for retCode of strncpy  */
bca718
+	bne 0, L(byte_by_byte)
bca718
+
bca718
+
bca718
+	srdi r11, r5, 3		/* compute count for CTR ; count = n/8  */
bca718
+	cmpldi cr7, r11, 3	/* if count > 4 ; perform unrolling 4 times  */
bca718
+	ble 7, L(update1)
bca718
+
bca718
+	ld r10, 0(r4)		/* load doubleWord from src  */
bca718
+	cmpb r8, r10, r8	/* compare src with NULL ,we read just now  */
bca718
+	cmpdi cr7, r8, 0	/* if cmpb returned NULL ; we continue  */
bca718
+	bne cr7, L(update3)
bca718
+
bca718
+	std r10, 0(r3)		/* copy doubleword at offset=0  */
bca718
+	ld r10, 8(r4)		/* load next doubleword from offset=8  */
bca718
+	cmpb r8, r10, r8	/* compare src with NULL , we read just now  */
bca718
+	cmpdi cr7, r8, 0	/* if cmpb returned NULL ; we continue  */
bca718
+	bne 7,L(HopBy8)
bca718
+
bca718
+	addi r8, r11, -4
bca718
+	mr r7, r3
bca718
+	srdi r8, r8, 2
bca718
+	mr r6, r4
bca718
+	addi r8, r8, 1
bca718
+	li r12, 0
bca718
+	mtctr r8
bca718
+	b L(dwordCopy)
bca718
+
bca718
+	.p2align 4
bca718
+L(dWordUnroll):
bca718
+	std r8, 16(r9)
bca718
+	ld r8, 24(r4)		/* load dword,perform loop unrolling again  */
bca718
+	cmpb r10, r8, r10
bca718
+	cmpdi cr7, r10, 0
bca718
+	bne cr7, L(HopBy24)
bca718
+
bca718
+	std r8, 24(r7)		/* copy dword at offset=24  */
bca718
+	addi r9, r9, 32
bca718
+	addi r4, r4, 32
bca718
+	bdz  L(leftDwords)	/* continue with loop on counter  */
bca718
+
bca718
+	ld r3, 32(r6)
bca718
+	cmpb r8, r3, r10
bca718
+	cmpdi cr7, r8, 0
bca718
+	bne cr7, L(update2)
bca718
+
bca718
+	std r3, 32(r7)
bca718
+	ld r10, 40(r6)
bca718
+	cmpb r8, r10, r8
bca718
+	cmpdi cr7, r8, 0
bca718
+	bne cr7, L(HopBy40)
bca718
+
bca718
+	mr r6, r4		/* update values  */
bca718
+	mr r7, r9
bca718
+	mr r11, r0
bca718
+	mr r5, r19
bca718
+
bca718
+L(dwordCopy):
bca718
+	std r10, 8(r9)		/* copy dword at offset=8  */
bca718
+	addi r19, r5, -32
bca718
+	addi r0, r11, -4
bca718
+	ld r8, 16(r4)
bca718
+	cmpb r10, r8, r12
bca718
+	cmpdi cr7, r10, 0
bca718
+	beq cr7, L(dWordUnroll)
bca718
+
bca718
+	addi r9, r9, 16		/* increment dst by 16  */
bca718
+	addi r4, r4, 16		/* increment src by 16  */
bca718
+	addi r5, r5, -16	/* decrement length 'n' by 16  */
bca718
+	addi r0, r11, -2	/* decrement loop counter  */
bca718
+
bca718
+L(dWordUnrollOFF):
bca718
+	ld r10, 0(r4)		/* load first dword  */
bca718
+	li r8, 0		/* load mask  */
bca718
+	cmpb r8, r10, r8
bca718
+	cmpdi cr7, r8, 0
bca718
+	bne cr7, L(byte_by_byte)
bca718
+	mtctr r0
bca718
+	li r7, 0
bca718
+	b L(CopyDword)
bca718
+
bca718
+	.p2align 4
bca718
+L(loadDWordandCompare):
bca718
+	ld r10, 0(r4)
bca718
+	cmpb r8, r10, r7
bca718
+	cmpdi cr7, r8, 0
bca718
+	bne cr7, L(byte_by_byte)
bca718
+
bca718
+L(CopyDword):
bca718
+	addi r9, r9, 8
bca718
+	std r10, -8(r9)
bca718
+	addi r4, r4, 8
bca718
+	addi r5, r5, -8
bca718
+	bdnz L(loadDWordandCompare)
bca718
+
bca718
+L(byte_by_byte):
bca718
+	cmpldi cr7, r5, 3
bca718
+	ble cr7, L(verifyByte)
bca718
+	srdi r10, r5, 2
bca718
+	mr r19, r9
bca718
+	mtctr r10
bca718
+	b L(firstByteUnroll)
bca718
+
bca718
+	.p2align 4
bca718
+L(bytes_unroll):
bca718
+	lbz r10, 1(r4)		/* load byte from src  */
bca718
+	cmpdi cr7, r10, 0	/* compare for NULL  */
bca718
+	stb r10, 1(r19)		/* store byte to dst  */
bca718
+	beq cr7, L(updtDestComputeN2ndByte)
bca718
+
bca718
+	addi r4, r4, 4		/* advance src  */
bca718
+
bca718
+	lbz r10, -2(r4)		/* perform loop unrolling for byte r/w  */
bca718
+	cmpdi cr7, r10, 0
bca718
+	stb r10, 2(r19)
bca718
+	beq cr7, L(updtDestComputeN3rdByte)
bca718
+
bca718
+	lbz r10, -1(r4)		/* perform loop unrolling for byte r/w  */
bca718
+	addi r19, r19, 4
bca718
+	cmpdi cr7, r10, 0
bca718
+	stb r10, -1(r19)
bca718
+	beq cr7, L(ComputeNByte)
bca718
+
bca718
+	bdz L(update0)
bca718
+
bca718
+L(firstByteUnroll):
bca718
+	lbz r10, 0(r4)		/* perform loop unrolling for byte r/w  */
bca718
+	cmpdi cr7, 10, 0
bca718
+	stb r10, 0(r19)
bca718
+	bne cr7, L(bytes_unroll)
bca718
+	addi r19, r19, 1
bca718
+
bca718
+L(ComputeNByte):
bca718
+	subf r9, r19, r9	/* compute 'n'n bytes to fill  */
bca718
+	add r8, r9, r5
bca718
+
bca718
+L(zeroFill):
bca718
+	cmpdi cr7, r8, 0	/* compare if length is zero  */
bca718
+	beq cr7, L(update3return)
bca718
+
bca718
+	mr r3, r19		/* fill buffer with  */
bca718
+	li r4, 0		/* zero fill buffer  */
bca718
+	mr r5, r8		/* how many bytes to fill buffer with  */
bca718
+	bl MEMSET		/* call optimized memset  */
bca718
+	nop
bca718
+
bca718
+L(update3return):
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	addi r3, r19, -1	/* update return value  */
bca718
+#endif
bca718
+
bca718
+L(hop2return):
bca718
+#ifndef USE_AS_STPNCPY
bca718
+	mr r3, r18		/* set return value  */
bca718
+#endif
bca718
+	addi r1, r1, FRAMESIZE	/* restore stack pointer  */
bca718
+	ld r0, 16(r1)		/* read the saved link register  */
bca718
+	ld r18, -16(r1)		/* restore callers save register, r18  */
bca718
+	ld r19, -8(r1)		/* restore callers save register, r19  */
bca718
+	mtlr r0			/* branch to link register  */
bca718
+	blr			/* return  */
bca718
+
bca718
+	.p2align 4
bca718
+L(update0):
bca718
+	mr r9, r19
bca718
+
bca718
+	.p2align 4
bca718
+L(verifyByte):
bca718
+	rldicl. r8, r5, 0, 62
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	mr r3, r9
bca718
+#endif
bca718
+	beq cr0, L(hop2return)
bca718
+	mtctr r8
bca718
+	addi r4, r4, -1
bca718
+	mr r19, r9
bca718
+	b L(oneBYone)
bca718
+
bca718
+	.p2align 4
bca718
+L(proceed):
bca718
+	bdz L(done)
bca718
+
bca718
+L(oneBYone):
bca718
+	lbzu r10, 1(r4)		/* copy byte  */
bca718
+	addi r19, r19, 1
bca718
+	addi r8, r8, -1
bca718
+	cmpdi cr7, r10, 0
bca718
+	stb r10, -1(r19)
bca718
+	bne cr7, L(proceed)
bca718
+	b L(zeroFill)
bca718
+
bca718
+	.p2align 4
bca718
+L(done):
bca718
+	addi r1, r1, FRAMESIZE	/* restore stack pointer  */
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	mr r3, r19		/* set the return value  */
bca718
+#else
bca718
+	mr r3, r18		/* set the return value  */
bca718
+#endif
bca718
+	ld r0, 16(r1)		/* read the saved link register  */
bca718
+	ld r18, -16(r1)		/* restore callers save register, r18  */
bca718
+	ld r19, -8(r1)		/* restore callers save register, r19  */
bca718
+	mtlr r0			/* branch to link register  */
bca718
+	blr			/* return  */
bca718
+
bca718
+L(update1):
bca718
+	mr r0, r11
bca718
+	mr r19, r5
bca718
+
bca718
+	.p2align 4
bca718
+L(leftDwords):
bca718
+	cmpdi cr7, r0, 0
bca718
+	mr r5, r19
bca718
+	bne cr7, L(dWordUnrollOFF)
bca718
+	b L(byte_by_byte)
bca718
+
bca718
+	.p2align 4
bca718
+L(updtDestComputeN2ndByte):
bca718
+	addi r19, r19, 2	/* update dst by 2  */
bca718
+	subf r9, r19, r9	/* compute distance covered  */
bca718
+	add r8, r9, r5
bca718
+	b L(zeroFill)
bca718
+
bca718
+	.p2align 4
bca718
+L(updtDestComputeN3rdByte):
bca718
+	addi r19, r19, 3	/* update dst by 3  */
bca718
+	subf r9, r19, r9	/* compute distance covered  */
bca718
+	add r8, r9, r5
bca718
+	b L(zeroFill)
bca718
+
bca718
+	.p2align 4
bca718
+L(HopBy24):
bca718
+	addi r9, r9, 24		/* increment dst by 24  */
bca718
+	addi r4, r4, 24		/* increment src by 24  */
bca718
+	addi r5, r5, -24	/* decrement length 'n' by 24  */
bca718
+	addi r0, r11, -3	/* decrement loop counter  */
bca718
+	b L(dWordUnrollOFF)
bca718
+
bca718
+	.p2align 4
bca718
+L(update2):
bca718
+	mr r5, r19
bca718
+	b L(dWordUnrollOFF)
bca718
+
bca718
+	.p2align 4
bca718
+L(HopBy40):
bca718
+	addi r9, r7, 40		/* increment dst by 40  */
bca718
+	addi r4, r6, 40		/* increment src by 40  */
bca718
+	addi r5, r5, -40	/* decrement length 'n' by 40  */
bca718
+	addi r0, r11, -5	/* decrement loop counter  */
bca718
+	b L(dWordUnrollOFF)
bca718
+
bca718
+L(update3):
bca718
+	mr r0, r11
bca718
+	b L(dWordUnrollOFF)
bca718
+
bca718
+L(HopBy8):
bca718
+	addi r9, r3, 8		/* increment dst by 8  */
bca718
+	addi r4, r4, 8		/* increment src by 8  */
bca718
+	addi r5, r5, -8		/* decrement length 'n' by 8  */
bca718
+	addi r0, r11, -1	/* decrement loop counter  */
bca718
+	b L(dWordUnrollOFF)
bca718
+END(FUNC_NAME)
bca718
+#ifndef USE_AS_STPNCPY
bca718
+libc_hidden_builtin_def (strncpy)
bca718
+#endif
bca718
diff --git a/sysdeps/powerpc/powerpc64/power8/stpncpy.S b/sysdeps/powerpc/powerpc64/power8/stpncpy.S
bca718
new file mode 100644
bca718
index 0000000..76a1466
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power8/stpncpy.S
bca718
@@ -0,0 +1,20 @@
bca718
+/* Optimized stpncpy 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
+#define USE_AS_STPNCPY
bca718
+#include <sysdeps/powerpc/powerpc64/power8/strncpy.S>
bca718
diff --git a/sysdeps/powerpc/powerpc64/power8/strncpy.S b/sysdeps/powerpc/powerpc64/power8/strncpy.S
bca718
new file mode 100644
bca718
index 0000000..5fda953
bca718
--- /dev/null
bca718
+++ b/sysdeps/powerpc/powerpc64/power8/strncpy.S
bca718
@@ -0,0 +1,424 @@
bca718
+/* Optimized strncpy/stpncpy 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
+#ifdef USE_AS_STPNCPY
bca718
+# define FUNC_NAME __stpncpy
bca718
+#else
bca718
+# define FUNC_NAME strncpy
bca718
+#endif
bca718
+
bca718
+/* Implements the function
bca718
+
bca718
+   char * [r3] strncpy (char *dest [r3], const char *src [r4], size_t n [r5])
bca718
+
bca718
+   or
bca718
+
bca718
+   char * [r3] stpncpy (char *dest [r3], const char *src [r4], size_t n [r5])
bca718
+
bca718
+   if USE_AS_STPCPY is defined.
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 (FUNC_NAME, 4, 0)
bca718
+
bca718
+        /* Check if the [src]+15 will cross a 4K page by checking if the bit
bca718
+           indicating the page size changes.  Basically:
bca718
+
bca718
+           uint64_t srcin = (uint64_t)src;
bca718
+           uint64_t ob = srcin & 4096UL;
bca718
+           uint64_t nb = (srcin+15UL) & 4096UL;
bca718
+           if (ob ^ nb)
bca718
+             goto pagecross;  */
bca718
+
bca718
+	addi	r10,r4,16
bca718
+	rlwinm	r9,r4,0,19,19
bca718
+
bca718
+	/* Since it is a leaf function, save some non-volatile registers on the
bca718
+	   protected/red zone.  */
bca718
+	std	r26,-48(r1)
bca718
+	std	r27,-40(r1)
bca718
+
bca718
+	rlwinm	r8,r10,0,19,19
bca718
+
bca718
+	std	r28,-32(r1)
bca718
+	std	r29,-24(r1)
bca718
+
bca718
+	cmpld	r7,r9,r8
bca718
+
bca718
+	std	r30,-16(r1)
bca718
+	std	r31,-8(r1)
bca718
+
bca718
+	beq	cr7,L(unaligned_lt_16)
bca718
+	rldicl	r9,r4,0,61
bca718
+	subfic	r8,r9,8
bca718
+	cmpld	cr7,r5,r8
bca718
+	bgt 	cr7,L(pagecross)
bca718
+
bca718
+	/* At this points there is 1 to 15 bytes to check and write.  Since it could
bca718
+	   be either from first unaligned 16 bytes access or from bulk copy, the code
bca718
+	   uses an unrolled byte read/write instead of trying to analyze the cmpb
bca718
+	   results.  */
bca718
+L(short_path):
bca718
+	mr	r9,r3
bca718
+L(short_path_1):
bca718
+	cmpdi	cr7,r5,0
bca718
+	beq	cr7,L(short_path_loop_end_1)
bca718
+L(short_path_2):
bca718
+	lbz	r10,0(r4)
bca718
+	cmpdi	cr7,r10,0
bca718
+	stb	r10,0(r9)
bca718
+	beq	cr7,L(zero_pad_start_1)
bca718
+	cmpdi	cr0,r5,1
bca718
+	addi	r8,r9,1
bca718
+	addi	r6,r5,-1
bca718
+	beq	cr0,L(short_path_loop_end_0)
bca718
+	lbz	r10,1(r4)
bca718
+	cmpdi	cr7,r10,0
bca718
+	stb	r10,1(r9)
bca718
+	beq	cr7,L(zero_pad_start_prepare_1)
bca718
+	addi	r10,r5,-3
bca718
+	b	L(short_path_loop_1)
bca718
+
bca718
+	.align	4
bca718
+L(short_path_loop):
bca718
+	lbz	r8,0(r4)
bca718
+	addi	r7,r10,-2
bca718
+	cmpdi	cr5,r8,0
bca718
+	stb	r8,0(r9)
bca718
+	beq	cr5,L(zero_pad_start_1)
bca718
+	beq	r7,L(short_path_loop_end_0)
bca718
+	lbz	r8,1(r4)
bca718
+	cmpdi	cr7,r8,0
bca718
+	stb	r8,1(r9)
bca718
+	beq	cr7,L(zero_pad_start)
bca718
+	mr	r10,r7
bca718
+L(short_path_loop_1):
bca718
+	addic.	r5,r5,-2
bca718
+	addi	r9,r9,2
bca718
+	cmpdi	cr7,r10,0
bca718
+	addi	r4,r4,2
bca718
+	addi	r6,r9,1
bca718
+	bne	cr0,L(short_path_loop)
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	mr	r3,r9
bca718
+	b	L(short_path_loop_end)
bca718
+#endif
bca718
+
bca718
+L(short_path_loop_end_0):
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	addi	r3,r9,1
bca718
+	b	L(short_path_loop_end)
bca718
+#endif
bca718
+L(short_path_loop_end_1):
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	mr	r3,r9
bca718
+#endif
bca718
+L(short_path_loop_end):
bca718
+	/* Restore non-volatile registers.  */
bca718
+	ld	r26,-48(r1)
bca718
+	ld	r27,-40(r1)
bca718
+	ld	r28,-32(r1)
bca718
+	ld	r29,-24(r1)
bca718
+	ld	r30,-16(r1)
bca718
+	ld	r31,-8(r1)
bca718
+	blr
bca718
+
bca718
+	/* This code pads the remainder dest with NULL bytes.  The algorithm
bca718
+	   calculate the remanining size and issues a doubleword unrolled
bca718
+	   loops followed by a byte a byte set.  */
bca718
+	.align	4
bca718
+L(zero_pad_start):
bca718
+	mr	r5,r10
bca718
+	mr	r9,r6
bca718
+L(zero_pad_start_1):
bca718
+	srdi.	r8,r5,r3
bca718
+	mr	r10,r9
bca718
+#ifdef USE_AS_STPNCPY
bca718
+	mr	r3,r9
bca718
+#endif
bca718
+	beq-	cr0,L(zero_pad_loop_b_start)
bca718
+	cmpldi	cr7,r8,1
bca718
+	li	cr7,0
bca718
+	std	r7,0(r9)
bca718
+	beq	cr7,L(zero_pad_loop_b_prepare)
bca718
+	addic.	r8,r8,-2
bca718
+	addi	r10,r9,r16
bca718
+	std	r7,8(r9)
bca718
+	beq	cr0,L(zero_pad_loop_dw_2)
bca718
+	std	r7,16(r9)
bca718
+	li	r9,0
bca718
+	b	L(zero_pad_loop_dw_1)
bca718
+
bca718
+	.align	4
bca718
+L(zero_pad_loop_dw):
bca718
+	addi	r10,r10,16
bca718
+	std	r9,-8(r10)
bca718
+	beq	cr0,L(zero_pad_loop_dw_2)
bca718
+	std	r9,0(r10)
bca718
+L(zero_pad_loop_dw_1):
bca718
+	cmpldi	cr7,r8,1
bca718
+	std	r9,0(r10)
bca718
+	addic.	r8,r8,-2
bca718
+	bne	cr7,L(zero_pad_loop_dw)
bca718
+	addi	r10,r10,8
bca718
+L(zero_pad_loop_dw_2):
bca718
+	rldicl	r5,r5,0,61
bca718
+L(zero_pad_loop_b_start):
bca718
+	cmpdi	cr7,r5,0
bca718
+	addi	r5,r5,-1
bca718
+	addi	r9,r10,-1
bca718
+	add	r10,r10,5
bca718
+	subf	r10,r9,r10
bca718
+	li	r8,0
bca718
+	beq-	cr7,L(short_path_loop_end)
bca718
+
bca718
+	/* Write remaining 1-8 bytes.  */
bca718
+        .align  4
bca718
+	addi	r9,r9,1
bca718
+	mtocrf	0x1,r10
bca718
+	bf	29,4f
bca718
+        stw     r8,0(r9)
bca718
+        addi	r9,r9,4
bca718
+
bca718
+        .align  4
bca718
+4:      bf      30,2f
bca718
+        sth     r8,0(r9)
bca718
+        addi	r9,r9,2
bca718
+
bca718
+        .align  4
bca718
+2:      bf	31,1f
bca718
+        stb	r8,0(r9)
bca718
+
bca718
+	/* Restore non-volatile registers.  */
bca718
+1:	ld	r26,-48(r1)
bca718
+	ld	r27,-40(r1)
bca718
+	ld	r28,-32(r1)
bca718
+	ld	r29,-24(r1)
bca718
+	ld	r30,-16(r1)
bca718
+	ld	r31,-8(r1)
bca718
+	blr
bca718
+
bca718
+	/* The common case where [src]+16 will not cross a 4K page boundary.
bca718
+	   In this case the code fast check the first 16 bytes by using doubleword
bca718
+	   read/compares and update destiny if neither total size or null byte
bca718
+	   is found in destiny. */
bca718
+	.align	4
bca718
+L(unaligned_lt_16):
bca718
+	cmpldi	cr7,r5,7
bca718
+	ble	cr7,L(short_path)
bca718
+	ld	r7,0(r4)
bca718
+	li	r8,0
bca718
+	cmpb	r8,r7,r8
bca718
+	cmpdi	cr7,r8,0
bca718
+	bne	cr7,L(short_path_prepare_2)
bca718
+	addi	r6,r5,-8
bca718
+	std	r7,0(r3)
bca718
+	addi	r9,r3,r8
bca718
+	cmpldi	cr7,r6,7
bca718
+	addi	r7,r4,8
bca718
+	ble	cr7,L(short_path_prepare_1_1)
bca718
+	ld	r4,8(r4)
bca718
+	cmpb	r8,r4,r8
bca718
+	cmpdi	cr7,r8,0
bca718
+	bne	cr7,L(short_path_prepare_2_1)
bca718
+	std	r4,8(r3)
bca718
+	addi	r29,r3,16
bca718
+	addi	r5,r5,-16
bca718
+	/* Neither the null byte was found or total length was reached,
bca718
+	   align to 16 bytes and issue a bulk copy/compare.  */
bca718
+	b	L(align_to_16b)
bca718
+
bca718
+	/* In the case of 4k page boundary cross, the algorithm first align
bca718
+	   the address to a doubleword, calculate a mask based on alignment
bca718
+	   to ignore the bytes and continue using doubleword.  */
bca718
+	.align	4
bca718
+L(pagecross):
bca718
+	rldicr	r11,r4,0,59	/* Align the address to 8 bytes boundary.  */
bca718
+	li	r6,-1		/* MASK = 0xffffffffffffffffUL.  */
bca718
+	sldi	r9,r9,3		/* Calculate padding.  */
bca718
+	ld	r7,0(r11)	/* Load doubleword from memory.  */
bca718
+#ifdef __LITTLE_ENDIAN__
bca718
+	sld	r9,r6,r9	/* MASK = MASK << padding.  */
bca718
+#else
bca718
+	srd	r9,r6,r9	/* MASK = MASK >> padding.  */
bca718
+#endif
bca718
+	orc	r9,r7,r9	/* Mask bits that are not part of the
bca718
+				   string.  */
bca718
+	li	cr7,0
bca718
+	cmpb	r9,r9,r7	/* Check for null bytes in DWORD1.  */
bca718
+	cmpdi	cr7,r9,0
bca718
+	bne	cr7,L(short_path_prepare_2)
bca718
+	subf	r8,r8,r5	/* Adjust total length.  */
bca718
+	cmpldi	cr7,r8,8	/* Check if length was reached.  */
bca718
+	ble	cr7,L(short_path_prepare_2)
bca718
+
bca718
+	/* For next checks we have aligned address, so we check for more
bca718
+	   three doublewords to make sure we can read 16 unaligned bytes
bca718
+	   to start the bulk copy with 16 aligned addresses.  */
bca718
+	ld	cr7,8(r11)
bca718
+	cmpb	r9,r7,r9
bca718
+	cmpdi	cr7,r9,0
bca718
+	bne	cr7,L(short_path_prepare_2)
bca718
+	addi	cr7,r8,-8
bca718
+	cmpldi	cr7,r7,8
bca718
+	ble	cr7,L(short_path_prepare_2)
bca718
+	ld	cr7,16(r11)
bca718
+	cmpb	r9,r7,r9
bca718
+	cmpdi	cr7,r9,0
bca718
+	bne	cr7,L(short_path_prepare_2)
bca718
+	addi	r8,r8,-16
bca718
+	cmpldi	r7,r8,8
bca718
+	ble	cr7,L(short_path_prepare_2)
bca718
+	ld	r8,24(r11)
bca718
+	cmpb	r9,r8,r9
bca718
+	cmpdi	r7,r9,0
bca718
+	bne	cr7,L(short_path_prepare_2)
bca718
+
bca718
+	/* No null byte found in the 32 bytes readed and length not reached,
bca718
+	   read source again using unaligned loads and store them.  */
bca718
+	ld	r9,0(r4)
bca718
+	addi	r29,r3,16
bca718
+	addi	r5,r5,-16
bca718
+	std	r9,0(r3)
bca718
+	ld	r9,8(r4)
bca718
+	std	r9,8(r3)
bca718
+
bca718
+	/* Align source to 16 bytes and adjust destiny and size.  */
bca718
+L(align_to_16b):
bca718
+	rldicl	r9,r10,0,60
bca718
+	rldicr	r28,r10,0,59
bca718
+	add	r12,r5,r9
bca718
+	subf	r29,r9,r29
bca718
+
bca718
+	/* The bulk read/compare/copy loads two doublewords, compare and merge
bca718
+	   in a single register for speed.  This is an attempt to speed up the
bca718
+	   null-checking process for bigger strings.  */
bca718
+
bca718
+	cmpldi	cr7,r12,15
bca718
+	ble	cr7,L(short_path_prepare_1_2)
bca718
+
bca718
+	/* Main loop for large sizes, unrolled 2 times to get better use of
bca718
+	   pipeline.  */
bca718
+	ld	r8,0(28)
bca718
+	ld	r10,8(28)
bca718
+	li	r9,0
bca718
+	cmpb	r7,r8,r9
bca718
+	cmpb	r9,r10,r9
bca718
+	or.	r6,r9,r7
bca718
+	bne	cr0,L(short_path_prepare_2_3)
bca718
+	addi	r5,r12,-16
bca718
+	addi	r4,r28,16
bca718
+	std	r8,0(r29)
bca718
+	std	r10,8(r29)
bca718
+	cmpldi	cr7,r5,15
bca718
+	addi	r9,r29,16
bca718
+	ble	cr7,L(short_path_1)
bca718
+	mr	r11,r28
bca718
+	mr	r6,r29
bca718
+	li	r30,0
bca718
+	subfic	r26,r4,48
bca718
+	subfic	r27,r9,48
bca718
+
bca718
+	b	L(loop_16b)
bca718
+
bca718
+	.align	4
bca718
+L(loop_start):
bca718
+	ld	r31,0(r11)
bca718
+	ld	r10,8(r11)
bca718
+	cmpb	r0,r31,r7
bca718
+	cmpb	r8,r10,r7
bca718
+	or.	r7,r0,r8
bca718
+	addi	r5,r5,-32
bca718
+	cmpldi	cr7,r5,15
bca718
+	add	r4,r4,r26
bca718
+	add	r9,r9,r27
bca718
+	bne	cr0,L(short_path_prepare_2_2)
bca718
+	add	r4,r28,r4
bca718
+	std	r31,0(r6)
bca718
+	add	r9,r29,r9
bca718
+	std	r10,8(r6)
bca718
+	ble	cr7,L(short_path_1)
bca718
+
bca718
+L(loop_16b):
bca718
+	ld	r10,16(r11)
bca718
+	ld	r0,24(r11)
bca718
+	cmpb	r8,r10,r30
bca718
+	cmpb	r7,r0,r30
bca718
+	or.	r7,r8,r7
bca718
+	addi	r12,r12,-32
bca718
+	cmpldi	r7,r12,15
bca718
+	addi	r11,r11,32
bca718
+	bne	cr0,L(short_path_2)
bca718
+	std	r10,16(r6)
bca718
+	addi	r6,r6,32
bca718
+	std	r0,-8(r6)
bca718
+	bgt	cr7,L(loop_start)
bca718
+
bca718
+	mr	r5,r12
bca718
+	mr	r4,r11
bca718
+	mr	r9,r6
bca718
+	b	L(short_path_1)
bca718
+
bca718
+	.align	4
bca718
+L(short_path_prepare_1_1):
bca718
+	mr	r5,r6
bca718
+	mr	r4,r7
bca718
+	b	L(short_path_1)
bca718
+L(short_path_prepare_1_2):
bca718
+	mr	r5,r12
bca718
+	mr	r4,r28
bca718
+	mr	r9,r29
bca718
+	b	L(short_path_1)
bca718
+L(short_path_prepare_2):
bca718
+	mr	r9,r3
bca718
+	b	L(short_path_2)
bca718
+L(short_path_prepare_2_1):
bca718
+	mr	r5,r6
bca718
+	mr	r4,r7
bca718
+	b	L(short_path_2)
bca718
+L(short_path_prepare_2_2):
bca718
+	mr	r5,r12
bca718
+	mr	r4,r11
bca718
+	mr	r9,r6
bca718
+	b	L(short_path_2)
bca718
+L(short_path_prepare_2_3):
bca718
+	mr	r5,r12
bca718
+	mr	r4,r28
bca718
+	mr	r9,r29
bca718
+	b	L(short_path_2)
bca718
+L(zero_pad_loop_b_prepare):
bca718
+	addi	r10,r9,8
bca718
+	rldicl	r5,r5,0,61
bca718
+	b	L(zero_pad_loop_b_start)
bca718
+L(zero_pad_start_prepare_1):
bca718
+	mr	r5,r6
bca718
+	mr	r9,r8
bca718
+	b	L(zero_pad_start_1)
bca718
+END (FUNC_NAME)
bca718
+
bca718
+#ifdef USE_AS_STPNCPY
bca718
+libc_hidden_def (__stpncpy)
bca718
+#else
bca718
+libc_hidden_builtin_def (strncpy)
bca718
+#endif