[PATCH v4 16/18] riscv: Add RVV strncpy for multiarch and non-multiarch

Yao Zihong zihong.plct@isrc.iscas.ac.cn
Mon Jan 19 07:03:23 GMT 2026


Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
 sysdeps/riscv/multiarch/strncpy-generic.c     | 26 +++++++++
 sysdeps/riscv/multiarch/strncpy-vector.S      | 24 ++++++++
 sysdeps/riscv/rvv/stpncpy.S                   | 27 +++++++--
 sysdeps/riscv/rvv/strncpy.S                   | 20 +++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  5 ++
 .../unix/sysv/linux/riscv/multiarch/stpncpy.c | 15 ++---
 .../unix/sysv/linux/riscv/multiarch/strncpy.c | 56 +++++++++++++++++++
 8 files changed, 164 insertions(+), 12 deletions(-)
 create mode 100644 sysdeps/riscv/multiarch/strncpy-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strncpy-vector.S
 create mode 100644 sysdeps/riscv/rvv/strncpy.S
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c

diff --git a/sysdeps/riscv/multiarch/strncpy-generic.c b/sysdeps/riscv/multiarch/strncpy-generic.c
new file mode 100644
index 0000000000..ab0eb9af42
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncpy-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strncpy implementation.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <string.h>
+
+#if IS_IN(libc)
+# define STRNCPY __strncpy_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/strncpy.c>
diff --git a/sysdeps/riscv/multiarch/strncpy-vector.S b/sysdeps/riscv/multiarch/strncpy-vector.S
new file mode 100644
index 0000000000..32256f7695
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncpy-vector.S
@@ -0,0 +1,24 @@
+/* Re-include the RISC-V RVV based strncpy implementation.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if IS_IN(libc)
+# define STRNCPY __strncpy_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+# include <sysdeps/riscv/rvv/strncpy.S>
+#endif
diff --git a/sysdeps/riscv/rvv/stpncpy.S b/sysdeps/riscv/rvv/stpncpy.S
index 57406685b0..d9004f2fba 100644
--- a/sysdeps/riscv/rvv/stpncpy.S
+++ b/sysdeps/riscv/rvv/stpncpy.S
@@ -19,12 +19,20 @@
 #include <sysdep.h>
 #include <sys/asm.h>
 
-#ifndef STPNCPY
-# ifdef weak_alias
-#  define STPNCPY __stpncpy
+#ifndef USE_AS_STRNCPY
+# ifndef STPNCPY
+#  ifdef weak_alias
+#   define STPNCPY __stpncpy
 weak_alias (__stpncpy, stpncpy)
+#  else
+#   define STPNCPY stpncpy
+#  endif
+# endif
+#else
+# ifndef STRNCPY
+#  define STPNCPY strncpy
 # else
-#  define STPNCPY stpncpy
+#  define STPNCPY STRNCPY
 # endif
 #endif
 
@@ -62,7 +70,9 @@ L(stpcpy_loop):
     add dst_ptr, dst_ptr, cur_vl
     bgez active_elem_pos, L(fill_zero)
     bnez length, L(stpcpy_loop)
+#ifndef USE_AS_STRNCPY
     mv dst, dst_ptr
+#endif
     ret
 
     /* Fill the tail zero.  */
@@ -72,8 +82,10 @@ L(fill_zero):
     to get the correct remaining length for zero filling. */
     sub temp, cur_vl, active_elem_pos
     addi temp, temp, -1
+#ifndef USE_AS_STRNCPY
     sub dst, dst_ptr, cur_vl
     add dst, dst, active_elem_pos
+#endif
     add length, length, temp
     /* Have an earily return for `strlen(src) + 1 == count` case.  */
     bnez length, L(do_fill_zero)
@@ -92,6 +104,11 @@ L(fill_zero_loop):
     ret
 .option pop
 END (STPNCPY)
-#ifdef weak_alias
+
+#ifndef USE_AS_STRNCPY
+# ifdef weak_alias
 libc_hidden_def (__stpncpy)
+# endif
+#else
+libc_hidden_builtin_def (strncpy)
 #endif
diff --git a/sysdeps/riscv/rvv/strncpy.S b/sysdeps/riscv/rvv/strncpy.S
new file mode 100644
index 0000000000..961c16416e
--- /dev/null
+++ b/sysdeps/riscv/rvv/strncpy.S
@@ -0,0 +1,20 @@
+/* RISC-V RVV based strncpy.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#define USE_AS_STRNCPY
+#include <sysdeps/riscv/rvv/stpncpy.S>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index bb3488e849..df33147de9 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -49,6 +49,9 @@ sysdep_routines += \
   strncmp \
   strncmp-generic \
   strncmp-vector \
+  strncpy \
+  strncpy-generic \
+  strncpy-vector \
   # sysdep_routines
 
 CFLAGS-memcpy_noalignment.c += -mno-strict-align
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index 025b026cf3..26444cc99d 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -125,5 +125,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 			      __strncmp_vector)
 	      IFUNC_IMPL_ADD (array, i, strncmp, 1, __strncmp_generic))
 
+  IFUNC_IMPL (i, name, strncpy,
+	      IFUNC_IMPL_ADD (array, i, strncpy, rvv_enabled,
+			      __strncpy_vector)
+	      IFUNC_IMPL_ADD (array, i, strncpy, 1, __strncpy_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c
index e451391f1a..190b479a1b 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c
@@ -22,14 +22,15 @@
    mismatch with the IFUNC selector in strong_alias, below.  */
 # undef stpncpy
 # define stpncpy __redirect_stpncpy
+# define __stpncpy __redirect___stpncpy
 # include <stdint.h>
 # include <string.h>
+# undef stpncpy
+# undef __stpncpy
 # include <ifunc-init.h>
 # include <riscv-ifunc.h>
 # include <sys/hwprobe.h>
 
-extern __typeof (__redirect_stpncpy) __libc_stpncpy;
-
 extern __typeof (__redirect_stpncpy) __stpncpy_generic attribute_hidden;
 extern __typeof (__redirect_stpncpy) __stpncpy_vector attribute_hidden;
 
@@ -43,14 +44,14 @@ select_stpncpy_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
   return __stpncpy_generic;
 }
 
-riscv_libc_ifunc (__libc_stpncpy, select_stpncpy_ifunc);
+riscv_libc_ifunc_redirected (__redirect___stpncpy, __stpncpy,
+			     select_stpncpy_ifunc);
 
 # undef stpncpy
-weak_alias (__libc_stpncpy, stpncpy);
-libc_hidden_def (__stpncpy);
+weak_alias (__stpncpy, stpncpy);
 # ifdef SHARED
-__hidden_ver1 (stpncpy, __GI_stpncpy, __redirect_stpncpy)
-  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (stpncpy);
+__hidden_ver1 (__stpncpy, __GI___stpncpy, __redirect___stpncpy)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (__stpncpy);
 # endif
 #else
 # include <string/stpncpy.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c
new file mode 100644
index 0000000000..459032312f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c
@@ -0,0 +1,56 @@
+/* Multiple versions of strncpy.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if IS_IN (libc)
+/* Redefine strncpy so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strncpy
+# define strncpy __redirect_strncpy
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strncpy) __libc_strncpy;
+
+extern __typeof (__redirect_strncpy) __strncpy_generic attribute_hidden;
+extern __typeof (__redirect_strncpy) __strncpy_vector attribute_hidden;
+
+static inline __typeof (__redirect_strncpy) *
+select_strncpy_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+  unsigned long long int v;
+  if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_IMA_EXT_0, &v) == 0
+      && (v & RISCV_HWPROBE_IMA_V) == RISCV_HWPROBE_IMA_V)
+    return __strncpy_vector;
+  return __strncpy_generic;
+}
+
+riscv_libc_ifunc (__libc_strncpy, select_strncpy_ifunc);
+
+# undef strncpy
+strong_alias (__libc_strncpy, strncpy);
+# ifdef SHARED
+__hidden_ver1 (strncpy, __GI_strncpy, __redirect_strncpy)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strncpy);
+# endif
+#else
+# include <string/strncpy.c>
+#endif
-- 
2.47.2



More information about the Libc-alpha mailing list