9bb5d6
From 7beee7b39adeda657f45989b0635033dae25a1fd Mon Sep 17 00:00:00 2001
9bb5d6
From: Raphael M Zinsly <rzinsly@linux.ibm.com>
9bb5d6
Date: Thu, 12 Nov 2020 13:12:24 -0300
9bb5d6
Subject: powerpc: Add optimized stpncpy for POWER9
9bb5d6
9bb5d6
Add stpncpy support into the POWER9 strncpy.
9bb5d6
9bb5d6
Reviewed-by: Matheus Castanho <msc@linux.ibm.com>
9bb5d6
Reviewed-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
9bb5d6
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/le/power9/stpncpy.S b/sysdeps/powerpc/powerpc64/le/power9/stpncpy.S
9bb5d6
new file mode 100644
9bb5d6
index 0000000000..81d9673d8b
9bb5d6
--- /dev/null
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/le/power9/stpncpy.S
9bb5d6
@@ -0,0 +1,24 @@
9bb5d6
+/* Optimized stpncpy implementation for POWER9 LE.
9bb5d6
+   Copyright (C) 2020 Free Software Foundation, Inc.
9bb5d6
+   This file is part of the GNU C Library.
9bb5d6
+
9bb5d6
+   The GNU C Library is free software; you can redistribute it and/or
9bb5d6
+   modify it under the terms of the GNU Lesser General Public
9bb5d6
+   License as published by the Free Software Foundation; either
9bb5d6
+   version 2.1 of the License, or (at your option) any later version.
9bb5d6
+
9bb5d6
+   The GNU C Library is distributed in the hope that it will be useful,
9bb5d6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
9bb5d6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9bb5d6
+   Lesser General Public License for more details.
9bb5d6
+
9bb5d6
+   You should have received a copy of the GNU Lesser General Public
9bb5d6
+   License along with the GNU C Library; if not, see
9bb5d6
+   <https://www.gnu.org/licenses/>.  */
9bb5d6
+
9bb5d6
+#define USE_AS_STPNCPY
9bb5d6
+#include <sysdeps/powerpc/powerpc64/le/power9/strncpy.S>
9bb5d6
+
9bb5d6
+weak_alias (__stpncpy, stpncpy)
9bb5d6
+libc_hidden_def (__stpncpy)
9bb5d6
+libc_hidden_builtin_def (stpncpy)
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/le/power9/strncpy.S b/sysdeps/powerpc/powerpc64/le/power9/strncpy.S
9bb5d6
index cbfc37bda3..b4ba428662 100644
9bb5d6
--- a/sysdeps/powerpc/powerpc64/le/power9/strncpy.S
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/le/power9/strncpy.S
9bb5d6
@@ -18,11 +18,19 @@
9bb5d6
 
9bb5d6
 #include <sysdep.h>
9bb5d6
 
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+# ifndef STPNCPY
9bb5d6
+#   define FUNC_NAME __stpncpy
9bb5d6
+# else
9bb5d6
+#   define FUNC_NAME STPNCPY
9bb5d6
+# endif
9bb5d6
+#else
9bb5d6
 # ifndef STRNCPY
9bb5d6
 #  define FUNC_NAME strncpy
9bb5d6
 # else
9bb5d6
 #  define FUNC_NAME STRNCPY
9bb5d6
 # endif
9bb5d6
+#endif  /* !USE_AS_STPNCPY  */
9bb5d6
 
9bb5d6
 #ifndef MEMSET
9bb5d6
 /* For builds without IFUNC support, local calls should be made to internal
9bb5d6
@@ -41,6 +49,12 @@
9bb5d6
 
9bb5d6
    char * [r3] strncpy (char *dest [r3], const char *src [r4], size_t n [r5])
9bb5d6
 
9bb5d6
+   or
9bb5d6
+
9bb5d6
+   char * [r3] stpncpy (char *dest [r3], const char *src [r4], size_t n [r5])
9bb5d6
+
9bb5d6
+   if USE_AS_STPNCPY is defined.
9bb5d6
+
9bb5d6
    The implementation can load bytes past a null terminator, but only
9bb5d6
    up to the next 16-byte aligned address, so it never crosses a page.  */
9bb5d6
 
9bb5d6
@@ -66,7 +80,15 @@ ENTRY (FUNC_NAME, 4)
9bb5d6
 
9bb5d6
 	/* Empty/1-byte string optimization  */
9bb5d6
 	cmpdi	r5,0
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	bgt	L(cont)
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	addi	r3,r3,1
9bb5d6
+	blr
9bb5d6
+L(cont):
9bb5d6
+#else
9bb5d6
 	beqlr
9bb5d6
+#endif
9bb5d6
 
9bb5d6
 	addi	r4,r4,1
9bb5d6
 	neg	r7,r4
9bb5d6
@@ -96,12 +118,20 @@ ENTRY (FUNC_NAME, 4)
9bb5d6
 	sldi	r10,r5,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	stxvl	32+v0,r11,r10	/* Partial store  */
9bb5d6
 
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r5
9bb5d6
+#endif
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 L(null):
9bb5d6
 	sldi	r10,r8,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	stxvl	32+v0,r11,r10	/* Partial store  */
9bb5d6
 
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r7
9bb5d6
+#endif
9bb5d6
 	add	r11,r11,r8
9bb5d6
 	sub	r5,r5,r8
9bb5d6
 	b L(zero_padding)
9bb5d6
@@ -185,6 +215,10 @@ L(n_tail4):
9bb5d6
 	sldi	r10,r5,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,48	/* Offset */
9bb5d6
 	stxvl	32+v3,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r5
9bb5d6
+#endif
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 L(prep_n_tail1):
9bb5d6
@@ -196,6 +230,10 @@ L(prep_n_tail1):
9bb5d6
 L(n_tail1):
9bb5d6
 	sldi	r10,r5,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	stxvl	32+v0,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r5
9bb5d6
+#endif
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 L(prep_n_tail2):
9bb5d6
@@ -209,6 +247,10 @@ L(n_tail2):
9bb5d6
 	sldi	r10,r5,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,16	/* offset */
9bb5d6
 	stxvl	32+v1,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r5
9bb5d6
+#endif
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 L(prep_n_tail3):
9bb5d6
@@ -223,6 +265,10 @@ L(n_tail3):
9bb5d6
 	sldi	r10,r5,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,32	/* Offset */
9bb5d6
 	stxvl	32+v2,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r5
9bb5d6
+#endif
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 L(prep_tail1):
9bb5d6
@@ -232,6 +278,10 @@ L(tail1):
9bb5d6
 	addi	r9,r8,1		/* Add null terminator  */
9bb5d6
 	sldi	r10,r9,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	stxvl	32+v0,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r8
9bb5d6
+#endif
9bb5d6
 	add	r11,r11,r9
9bb5d6
 	sub	r5,r5,r9
9bb5d6
 	b L(zero_padding)
9bb5d6
@@ -246,6 +296,10 @@ L(tail2):
9bb5d6
 	sldi	r10,r9,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,16	/* offset */
9bb5d6
 	stxvl	32+v1,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r8
9bb5d6
+#endif
9bb5d6
 	add	r11,r11,r9
9bb5d6
 	sub	r5,r5,r9
9bb5d6
 	b L(zero_padding)
9bb5d6
@@ -261,6 +315,10 @@ L(tail3):
9bb5d6
 	sldi	r10,r9,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,32	/* offset */
9bb5d6
 	stxvl	32+v2,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r8
9bb5d6
+#endif
9bb5d6
 	add	r11,r11,r9
9bb5d6
 	sub	r5,r5,r9
9bb5d6
 	b L(zero_padding)
9bb5d6
@@ -276,6 +334,10 @@ L(tail4):
9bb5d6
 	sldi	r10,r9,56	/* stxvl wants size in top 8 bits  */
9bb5d6
 	addi	r11,r11,48	/* offset */
9bb5d6
 	stxvl	32+v3,r11,r10	/* Partial store  */
9bb5d6
+#ifdef USE_AS_STPNCPY
9bb5d6
+	/* Compute pointer to last byte copied into dest.  */
9bb5d6
+	add	r3,r11,r8
9bb5d6
+#endif
9bb5d6
 	add	r11,r11,r9
9bb5d6
 	sub	r5,r5,r9
9bb5d6
 
9bb5d6
@@ -331,7 +393,8 @@ L(zero_padding_memset):
9bb5d6
 	ld	r0,FRAMESIZE+16(r1)
9bb5d6
 
9bb5d6
 	mr	r3,r30       /* Restore the return value of strncpy, i.e.:
9bb5d6
-				dest.  */
9bb5d6
+				dest.  For stpncpy, the return value is the
9bb5d6
+				same as return value of memset.  */
9bb5d6
 	ld	r30,FRAMESIZE-8(r1) /* Restore r30.  */
9bb5d6
 	/* Restore the stack frame.  */
9bb5d6
 	addi	r1,r1,FRAMESIZE
9bb5d6
@@ -342,3 +405,6 @@ L(zero_padding_memset):
9bb5d6
 	blr
9bb5d6
 
9bb5d6
 END (FUNC_NAME)
9bb5d6
+#ifndef USE_AS_STPNCPY
9bb5d6
+libc_hidden_builtin_def (strncpy)
9bb5d6
+#endif
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
9bb5d6
index cd2b47b403..f46bf50732 100644
9bb5d6
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
9bb5d6
@@ -33,7 +33,7 @@ sysdep_routines += memcpy-power8-cached memcpy-power7 memcpy-a2 memcpy-power6 \
9bb5d6
 
9bb5d6
 ifneq (,$(filter %le,$(config-machine)))
9bb5d6
 sysdep_routines += strcmp-power9 strncmp-power9 strcpy-power9 stpcpy-power9 \
9bb5d6
-		   rawmemchr-power9 strlen-power9 strncpy-power9
9bb5d6
+		   rawmemchr-power9 strlen-power9 strncpy-power9 stpncpy-power9
9bb5d6
 endif
9bb5d6
 CFLAGS-strncase-power7.c += -mcpu=power7 -funroll-loops
9bb5d6
 CFLAGS-strncase_l-power7.c += -mcpu=power7 -funroll-loops
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
9bb5d6
index 135326c97a..8e19ebbf09 100644
9bb5d6
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
9bb5d6
@@ -318,6 +318,12 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
9bb5d6
 
9bb5d6
   /* Support sysdeps/powerpc/powerpc64/multiarch/stpncpy.c.  */
9bb5d6
   IFUNC_IMPL (i, name, stpncpy,
9bb5d6
+#ifdef __LITTLE_ENDIAN__
9bb5d6
+	      IFUNC_IMPL_ADD (array, i, stpncpy,
9bb5d6
+			      (hwcap2 & PPC_FEATURE2_ARCH_3_00)
9bb5d6
+			      && (hwcap & PPC_FEATURE_HAS_VSX),
9bb5d6
+			      __stpncpy_power9)
9bb5d6
+#endif
9bb5d6
 	      IFUNC_IMPL_ADD (array, i, stpncpy,
9bb5d6
 			      hwcap2 & PPC_FEATURE2_ARCH_2_07,
9bb5d6
 			      __stpncpy_power8)
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power9.S b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power9.S
9bb5d6
new file mode 100644
9bb5d6
index 0000000000..1188bd0894
9bb5d6
--- /dev/null
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy-power9.S
9bb5d6
@@ -0,0 +1,29 @@
9bb5d6
+/* Optimized stpncpy implementation for POWER9 LE.
9bb5d6
+   Copyright (C) 2020 Free Software Foundation, Inc.
9bb5d6
+   This file is part of the GNU C Library.
9bb5d6
+
9bb5d6
+   The GNU C Library is free software; you can redistribute it and/or
9bb5d6
+   modify it under the terms of the GNU Lesser General Public
9bb5d6
+   License as published by the Free Software Foundation; either
9bb5d6
+   version 2.1 of the License, or (at your option) any later version.
9bb5d6
+
9bb5d6
+   The GNU C Library is distributed in the hope that it will be useful,
9bb5d6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
9bb5d6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9bb5d6
+   Lesser General Public License for more details.
9bb5d6
+
9bb5d6
+   You should have received a copy of the GNU Lesser General Public
9bb5d6
+   License along with the GNU C Library; if not, see
9bb5d6
+   <https://www.gnu.org/licenses/>.  */
9bb5d6
+
9bb5d6
+#define STPNCPY __stpncpy_power9
9bb5d6
+
9bb5d6
+#undef libc_hidden_builtin_def
9bb5d6
+#define libc_hidden_builtin_def(name)
9bb5d6
+
9bb5d6
+#define MEMSET __memset_power8
9bb5d6
+#ifdef SHARED
9bb5d6
+# define MEMSET_is_local
9bb5d6
+#endif
9bb5d6
+
9bb5d6
+#include <sysdeps/powerpc/powerpc64/le/power9/stpncpy.S>
9bb5d6
diff --git a/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c b/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c
9bb5d6
index 17df886431..3758f29ad1 100644
9bb5d6
--- a/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c
9bb5d6
+++ b/sysdeps/powerpc/powerpc64/multiarch/stpncpy.c
9bb5d6
@@ -26,10 +26,18 @@
9bb5d6
 extern __typeof (__stpncpy) __stpncpy_ppc attribute_hidden;
9bb5d6
 extern __typeof (__stpncpy) __stpncpy_power7 attribute_hidden;
9bb5d6
 extern __typeof (__stpncpy) __stpncpy_power8 attribute_hidden;
9bb5d6
+# ifdef __LITTLE_ENDIAN__
9bb5d6
+extern __typeof (__stpncpy) __stpncpy_power9 attribute_hidden;
9bb5d6
+# endif
9bb5d6
 # undef stpncpy
9bb5d6
 # undef __stpncpy
9bb5d6
 
9bb5d6
 libc_ifunc_redirected (__redirect___stpncpy, __stpncpy,
9bb5d6
+# ifdef __LITTLE_ENDIAN__
9bb5d6
+		     (hwcap2 & PPC_FEATURE2_ARCH_3_00) &&
9bb5d6
+		     (hwcap & PPC_FEATURE_HAS_VSX)
9bb5d6
+		     ? __stpncpy_power9 :
9bb5d6
+# endif
9bb5d6
 		       (hwcap2 & PPC_FEATURE2_ARCH_2_07)
9bb5d6
 		       ? __stpncpy_power8
9bb5d6
 		       : (hwcap & PPC_FEATURE_HAS_VSX)