[PATCH v3 15/17] riscv: Add RVV strncpy for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:42 GMT 2025
Co-authored-by: Hau Hsu <hau.hsu@sifive.com>
Co-authored-by: Jerry Shih <jerry.shih@sifive.com>
Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
sysdeps/riscv/multiarch/strncpy-generic.c | 26 ++++++
sysdeps/riscv/multiarch/strncpy-vector.S | 30 +++++++
sysdeps/riscv/rvv/strncpy.S | 89 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++
.../unix/sysv/linux/riscv/multiarch/strncpy.c | 56 ++++++++++++
6 files changed, 209 insertions(+)
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..8833eddd5b
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncpy-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strncpy implementation.
+ Copyright (C) 2025 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..e46543e5b1
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncpy-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based strncpy implementation.
+ Copyright (C) 2025 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
+ <http://www.gnu.org/licenses/>. */
+
+#if IS_IN(libc)
+# define STRNCPY __strncpy_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# undef libc_hidden_def
+# define libc_hidden_def(name)
+# undef weak_alias
+# define weak_alias(name, alias)
+# undef strong_alias
+# define strong_alias(name, alias)
+# include <sysdeps/riscv/rvv/strncpy.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strncpy.S b/sysdeps/riscv/rvv/strncpy.S
new file mode 100644
index 0000000000..a435240cd7
--- /dev/null
+++ b/sysdeps/riscv/rvv/strncpy.S
@@ -0,0 +1,89 @@
+/* RISC-V RVV based strncpy.
+ Copyright (C) 2025 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
+ <http://www.gnu.org/licenses/>. */
+
+#include <sysdep.h>
+#include <sys/asm.h>
+
+#ifndef STRNCPY
+# define STRNCPY strncpy
+#endif
+
+#define dst a0
+#define src a1
+#define length a2
+#define dst_ptr a3
+
+#define active_elem_pos a4
+#define temp a4
+#define cur_vl a5
+#define ivl a6
+
+#define ELEM_LMUL_SETTING m1
+#define vmask1 v0
+#define vmask2 v1
+#define ZERO_FILL_ELEM_LMUL_SETTING m8
+#define vstr1 v8
+#define vstr2 v16
+
+ENTRY (STRNCPY)
+.option push
+.option arch, +v
+ mv dst_ptr, dst
+ /* Copy src to dst_ptr. */
+L(strcpy_loop):
+ vsetvli zero, length, e8, ELEM_LMUL_SETTING, ta, ma
+ vle8ff.v vstr1, (src)
+ vmseq.vx vmask2, vstr1, zero
+ csrr cur_vl, vl
+ vfirst.m active_elem_pos, vmask2
+ vmsif.m vmask1, vmask2
+ add src, src, cur_vl
+ sub length, length, cur_vl
+ vse8.v vstr1, (dst_ptr), vmask1.t
+ add dst_ptr, dst_ptr, cur_vl
+ bgez active_elem_pos, L(fill_zero)
+ bnez length, L(strcpy_loop)
+ ret
+
+ /* Fill the tail zero. */
+L(fill_zero):
+ /* We already copy the `\0` to dst. But we use `vfirst.m` to
+ get the `index` of `\0` position. We need to adjust `-1`
+ to get the correct remaining length for zero filling. */
+ sub temp, cur_vl, active_elem_pos
+ addi temp, temp, -1
+ add length, length, temp
+ /* Have an earily return for `strlen(src) + 1 == count` case. */
+ bnez length, L(do_fill_zero)
+ ret
+L(do_fill_zero):
+ sub dst_ptr, dst_ptr, temp
+ vsetvli zero, length, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma
+ vmv.v.x vstr2, zero
+
+L(fill_zero_loop):
+ vsetvli ivl, length, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma
+ vse8.v vstr2, (dst_ptr)
+ sub length, length, ivl
+ add dst_ptr, dst_ptr, ivl
+ bnez length, L(fill_zero_loop)
+
+ ret
+.option pop
+END (STRNCPY)
+libc_hidden_builtin_def (strncpy)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 01ad5a03ee..c1854be887 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -46,6 +46,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 72829c28eb..8e2a030115 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -120,5 +120,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/strncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c
new file mode 100644
index 0000000000..e186be8c5f
--- /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) 2025 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