[PATCH v3 17/17] riscv: Add RVV strrchr for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:44 GMT 2025
Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
sysdeps/riscv/multiarch/strrchr-generic.c | 28 ++++
sysdeps/riscv/multiarch/strrchr-vector.S | 30 +++++
sysdeps/riscv/rvv/strrchr.S | 123 ++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 +
.../unix/sysv/linux/riscv/multiarch/strrchr.c | 59 +++++++++
6 files changed, 248 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/strrchr-generic.c
create mode 100644 sysdeps/riscv/multiarch/strrchr-vector.S
create mode 100644 sysdeps/riscv/rvv/strrchr.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c
diff --git a/sysdeps/riscv/multiarch/strrchr-generic.c b/sysdeps/riscv/multiarch/strrchr-generic.c
new file mode 100644
index 0000000000..d5724283fe
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strrchr-generic.c
@@ -0,0 +1,28 @@
+/* Re-include the default strrchr 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 STRRCHR __strrchr_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+# undef weak_alias
+# define weak_alias(x, x2)
+#endif
+#include <string/strrchr.c>
diff --git a/sysdeps/riscv/multiarch/strrchr-vector.S b/sysdeps/riscv/multiarch/strrchr-vector.S
new file mode 100644
index 0000000000..b649702cdd
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strrchr-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based strrchr 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 STRRCHR __strrchr_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/strrchr.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strrchr.S b/sysdeps/riscv/rvv/strrchr.S
new file mode 100644
index 0000000000..f161b02140
--- /dev/null
+++ b/sysdeps/riscv/rvv/strrchr.S
@@ -0,0 +1,123 @@
+/* RISC-V RVV based strrchr.
+ 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 STRRCHR
+# define STRRCHR strrchr
+#endif
+
+#define str a0
+#define ch a1
+#define blk_base a2
+#define loc_max a3
+#define latest_ptr a4
+#define first_idx a5
+#define len_valid a5
+#define cur_vl a6
+
+#define vhit v0
+#define vstr v4
+#define vmask_end v8
+#define vmask_ch v10
+#define vvalid v12
+#define vidx v16
+#define vacc v20
+
+#if __riscv_xlen == 64
+# define XLEN_SHIFT 63
+#else
+# define XLEN_SHIFT 31
+#endif
+
+ENTRY (STRRCHR)
+.option push
+.option arch, +v
+ beqz ch, L(search_zero)
+ mv latest_ptr, zero
+L(loop):
+ mv blk_base, str
+
+ vsetvli cur_vl, zero, e8, m2, ta, ma
+ vle8ff.v vstr, (str)
+
+ vmseq.vi vmask_end, vstr, 0
+ vmsbf.m vvalid, vmask_end
+ vmseq.vx vmask_ch, vstr, ch
+ vmand.mm vhit, vmask_ch, vvalid
+
+ vfirst.m first_idx, vhit
+ csrr cur_vl, vl
+ bltz first_idx, L(no_hit_in_block)
+
+ vsetvli zero, cur_vl, e16, m4, ta, ma
+ vid.v vidx
+ vmv.s.x vacc, x0
+ /* Compute per-block last hit index (or -1 if none). */
+ vredmaxu.vs vacc, vidx, vacc, vhit.t
+ vmv.x.s loc_max, vacc
+ j L(loc_max_ready)
+
+L(no_hit_in_block):
+ li loc_max, -1
+
+L(loc_max_ready):
+ vsetvli zero, cur_vl, e8, m2, ta, ma
+ vcpop.m len_valid, vvalid
+
+ bne len_valid, cur_vl, L(tail_block)
+ bltz loc_max, L(advance)
+
+ add latest_ptr, blk_base, loc_max
+
+L(advance):
+ add str, str, cur_vl
+ j L(loop)
+
+L(tail_block):
+ add str, blk_base, loc_max
+
+ /* sign = -1 if loc_max < 0 else 0; then invert to
+ build selection mask */
+ srai loc_max, loc_max, XLEN_SHIFT
+ and len_valid, loc_max, latest_ptr
+ xori loc_max, loc_max, -1
+ and str, loc_max, str
+ or str, str, len_valid
+ ret
+
+L(search_zero):
+L(sz_loop):
+ vsetvli cur_vl, zero, e8, m4, ta, ma
+ vle8ff.v vstr, (str)
+
+ vmseq.vi vmask_end, vstr, 0
+ vfirst.m first_idx, vmask_end
+ bltz first_idx, L(sz_advance)
+ add str, str, first_idx
+ ret
+L(sz_advance):
+ csrr cur_vl, vl
+ add str, str, cur_vl
+ j L(sz_loop)
+
+.option pop
+END (STRRCHR)
+weak_alias (STRRCHR, rindex)
+libc_hidden_builtin_def (strrchr)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 06a8eedc52..483847e8f7 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -52,6 +52,9 @@ sysdep_routines += \
strnlen \
strnlen-generic \
strnlen-vector \
+ strrchr \
+ strrchr-generic \
+ strrchr-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 8265bea2a4..5bda7675b1 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -130,5 +130,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
__strnlen_vector)
IFUNC_IMPL_ADD (array, i, strnlen, 1, __strnlen_generic))
+ IFUNC_IMPL (i, name, strrchr,
+ IFUNC_IMPL_ADD (array, i, strrchr, rvv_enabled,
+ __strrchr_vector)
+ IFUNC_IMPL_ADD (array, i, strrchr, 1, __strrchr_generic))
+
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c
new file mode 100644
index 0000000000..d122434f3d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c
@@ -0,0 +1,59 @@
+/* Multiple versions of strrchr.
+ 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 strrchr so that the compiler won't complain about the type
+ mismatch with the IFUNC selector in strong_alias, below. */
+# undef strrchr
+# define strrchr __redirect_strrchr
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strrchr) __libc_strrchr;
+
+extern __typeof (__redirect_strrchr) __strrchr_generic attribute_hidden;
+extern __typeof (__redirect_strrchr) __strrchr_vector attribute_hidden;
+
+static inline __typeof (__redirect_strrchr) *
+select_strrchr_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+ unsigned long long 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 __strrchr_vector;
+ return __strrchr_generic;
+}
+
+riscv_libc_ifunc (__libc_strrchr, select_strrchr_ifunc);
+
+# undef strrchr
+# undef rindex
+strong_alias (__libc_strrchr, strrchr);
+weak_alias (strrchr, rindex);
+# ifdef SHARED
+__hidden_ver1 (strrchr, __GI_strrchr, __redirect_strrchr)
+ __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strrchr);
+# endif
+#else
+# include <string/strrchr.c>
+#endif
--
2.47.2
More information about the Libc-alpha
mailing list