[PATCH v4 14/18] riscv: Add RVV strncat for multiarch and non-multiarch

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


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/strncat-generic.c     | 26 ++++++
 sysdeps/riscv/multiarch/strncat-vector.S      | 24 ++++++
 sysdeps/riscv/rvv/strncat.S                   | 81 +++++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  5 ++
 .../unix/sysv/linux/riscv/multiarch/strncat.c | 52 ++++++++++++
 6 files changed, 191 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/strncat-generic.c
 create mode 100644 sysdeps/riscv/multiarch/strncat-vector.S
 create mode 100644 sysdeps/riscv/rvv/strncat.S
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c

diff --git a/sysdeps/riscv/multiarch/strncat-generic.c b/sysdeps/riscv/multiarch/strncat-generic.c
new file mode 100644
index 0000000000..66d7a17695
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncat-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strncat 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 STRNCAT __strncat_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/strncat.c>
diff --git a/sysdeps/riscv/multiarch/strncat-vector.S b/sysdeps/riscv/multiarch/strncat-vector.S
new file mode 100644
index 0000000000..c98820d7cd
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncat-vector.S
@@ -0,0 +1,24 @@
+/* Re-include the RISC-V RVV based strncat 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 STRNCAT __strncat_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# include <sysdeps/riscv/rvv/strncat.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strncat.S b/sysdeps/riscv/rvv/strncat.S
new file mode 100644
index 0000000000..12fe9aa8d8
--- /dev/null
+++ b/sysdeps/riscv/rvv/strncat.S
@@ -0,0 +1,81 @@
+/* RISC-V RVV based strncat.
+   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 <sysdep.h>
+#include <sys/asm.h>
+
+#ifndef STRNCAT
+# define STRNCAT strncat
+#endif
+
+#define dst a0
+#define src a1
+#define length a2
+#define dst_ptr a3
+#define cur_vl a4
+#define activate_elem_pos a5
+
+#define ELEM_LMUL_SETTING m1
+#define vmask1 v0
+#define vmask2 v1
+#define vstr1 v8
+#define vstr2 v16
+
+ENTRY (STRNCAT)
+.option push
+.option arch, +v
+    mv dst_ptr, dst
+    /* the strlen of dst.  */
+L(strlen_loop):
+    vsetvli cur_vl, zero, e8, ELEM_LMUL_SETTING, ta, ma
+
+    vle8ff.v vstr1, (dst_ptr)
+    /* find the '\0'.  */
+    vmseq.vx vmask1, vstr1, zero
+    csrr cur_vl, vl
+    vfirst.m activate_elem_pos, vmask1
+    add dst_ptr, dst_ptr, cur_vl
+    bltz activate_elem_pos, L(strlen_loop)
+
+    sub dst_ptr, dst_ptr, cur_vl
+    add dst_ptr, dst_ptr, activate_elem_pos
+
+    /* 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 activate_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
+    beqz length, L(fill_zero)
+    bltz activate_elem_pos, L(strcpy_loop)
+    ret
+L(fill_zero):
+    bgez activate_elem_pos, L(fill_zero_end)
+    sb zero, (dst_ptr)
+L(fill_zero_end):
+    ret
+.option pop
+END (STRNCAT)
+libc_hidden_builtin_def (strncat)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 47e16078c1..1b568bd3de 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -43,6 +43,9 @@ sysdep_routines += \
   strlen \
   strlen-generic \
   strlen-vector \
+  strncat \
+  strncat-generic \
+  strncat-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 fa5692a074..370d59d4c5 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -115,5 +115,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 			      __strlen_vector)
 	      IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_generic))
 
+  IFUNC_IMPL (i, name, strncat,
+	      IFUNC_IMPL_ADD (array, i, strncat, rvv_enabled,
+			      __strncat_vector)
+	      IFUNC_IMPL_ADD (array, i, strncat, 1, __strncat_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c
new file mode 100644
index 0000000000..a7acfdfe54
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c
@@ -0,0 +1,52 @@
+/* Multiple versions of strncat.
+   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 strncat so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# define strncat __redirect_strncat
+# include <stdint.h>
+# include <string.h>
+# undef strncat
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strncat) __strncat_generic attribute_hidden;
+extern __typeof (__redirect_strncat) __strncat_vector attribute_hidden;
+
+static inline __typeof (__redirect_strncat) *
+select_strncat_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 __strncat_vector;
+  return __strncat_generic;
+}
+
+riscv_libc_ifunc_redirected (__redirect_strncat, strncat, select_strncat_ifunc);
+strong_alias (strncat, __strncat);
+# ifdef SHARED
+__hidden_ver1 (strncat, __GI___strncat, __redirect_strncat)
+  __attribute__((visibility ("hidden"))) __attribute_copy__ (strncat);
+# endif
+#else
+# include <string/strncat.c>
+#endif
-- 
2.47.2



More information about the Libc-alpha mailing list