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