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