[PATCH v3 16/17] riscv: Add RVV strnlen for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:43 GMT 2025
Co-authored-by: Hau Hsu <hau.hsu@sifive.com>
Co-authored-by: Nick Knight <nick.knight@sifive.com>
Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
sysdeps/riscv/multiarch/strnlen-generic.c | 24 ++++++++
sysdeps/riscv/multiarch/strnlen-vector.S | 30 +++++++++
sysdeps/riscv/rvv/strnlen.S | 61 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++
.../unix/sysv/linux/riscv/multiarch/strnlen.c | 56 +++++++++++++++++
6 files changed, 179 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/strnlen-generic.c
create mode 100644 sysdeps/riscv/multiarch/strnlen-vector.S
create mode 100644 sysdeps/riscv/rvv/strnlen.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c
diff --git a/sysdeps/riscv/multiarch/strnlen-generic.c b/sysdeps/riscv/multiarch/strnlen-generic.c
new file mode 100644
index 0000000000..20268bd444
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strnlen-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default strnlen 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 STRNLEN __strnlen_generic
+#endif
+#include <string/strnlen.c>
diff --git a/sysdeps/riscv/multiarch/strnlen-vector.S b/sysdeps/riscv/multiarch/strnlen-vector.S
new file mode 100644
index 0000000000..ebaddbeb6a
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strnlen-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based strnlen 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 STRNLEN __strnlen_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/strnlen.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strnlen.S b/sysdeps/riscv/rvv/strnlen.S
new file mode 100644
index 0000000000..15edbbcc5c
--- /dev/null
+++ b/sysdeps/riscv/rvv/strnlen.S
@@ -0,0 +1,61 @@
+/* RISC-V RVV based strnlen.
+ 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 STRNLEN
+# define STRNLEN __strnlen
+#endif
+
+#define str a0
+#define copy_str a2
+#define ret_value a0
+#define max_len a1
+#define cur_vl a3
+#define end_offset a4
+
+#define ELEM_LMUL_SETTING m1
+#define vstr v0
+#define vmask_end v8
+
+ENTRY (STRNLEN)
+.option push
+.option arch, +v
+ mv copy_str, str
+ mv ret_value, max_len
+L(strnlen_vector_loop):
+ beqz max_len, L(end_strnlen_vector_loop)
+ vsetvli zero, max_len, e8, ELEM_LMUL_SETTING, ta, ma
+ vle8ff.v vstr, (copy_str)
+ vmseq.vi vmask_end, vstr, 0
+ vfirst.m end_offset, vmask_end /* first occurence of \0 */
+ csrr cur_vl, vl
+ add copy_str, copy_str, cur_vl
+ sub max_len, max_len, cur_vl
+ bltz end_offset, L(strnlen_vector_loop)
+ add max_len, max_len, cur_vl
+ sub ret_value, ret_value, max_len
+ add ret_value, ret_value, end_offset
+L(end_strnlen_vector_loop):
+ ret
+.option pop
+END (STRNLEN)
+weak_alias (STRNLEN, strnlen)
+libc_hidden_def (STRNLEN)
+libc_hidden_def (strnlen)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index c1854be887..06a8eedc52 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -49,6 +49,9 @@ sysdep_routines += \
strncpy \
strncpy-generic \
strncpy-vector \
+ strnlen \
+ strnlen-generic \
+ strnlen-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 8e2a030115..8265bea2a4 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,
__strncpy_vector)
IFUNC_IMPL_ADD (array, i, strncpy, 1, __strncpy_generic))
+ IFUNC_IMPL (i, name, strnlen,
+ IFUNC_IMPL_ADD (array, i, strnlen, rvv_enabled,
+ __strnlen_vector)
+ IFUNC_IMPL_ADD (array, i, strnlen, 1, __strnlen_generic))
+
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c
new file mode 100644
index 0000000000..f9ca3aab9d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c
@@ -0,0 +1,56 @@
+/* Multiple versions of strnlen.
+ 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 strnlen so that the compiler won't complain about the type
+ mismatch with the IFUNC selector in strong_alias, below. */
+# define strnlen __redirect_strnlen
+# define __strnlen __redirect___strnlen
+# include <stdint.h>
+# include <string.h>
+# undef __strnlen
+# undef strnlen
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strnlen) __strnlen_generic attribute_hidden;
+extern __typeof (__redirect_strnlen) __strnlen_vector attribute_hidden;
+
+static inline __typeof (__redirect_strnlen) *
+select_strnlen_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 __strnlen_vector;
+ return __strnlen_generic;
+}
+
+riscv_libc_ifunc_redirected (__redirect_strnlen, __strnlen, select_strnlen_ifunc);
+weak_alias (__strnlen, strnlen);
+# ifdef SHARED
+__hidden_ver1 (__strnlen, __GI___strnlen, __redirect___strnlen)
+ __attribute__((visibility ("hidden"))) __attribute_copy__ (strnlen);
+__hidden_ver1 (__strnlen, __GI_strnlen, __redirect_strnlen)
+ __attribute__((weak, visibility ("hidden"))) __attribute_copy__ (strnlen);
+# endif
+#else
+# include <string/strnlen.c>
+#endif
--
2.47.2
More information about the Libc-alpha
mailing list