[PATCH v5 07/18] riscv: Add RVV memrchr for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Feb 3 15:05:31 GMT 2026
Co-authored-by: daichengrong <daichengrong@iscas.ac.cn>
Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
sysdeps/riscv/multiarch/memrchr-generic.c | 28 ++++++++
sysdeps/riscv/multiarch/memrchr-vector.S | 28 ++++++++
sysdeps/riscv/rvv/memrchr.S | 71 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++
.../unix/sysv/linux/riscv/multiarch/memrchr.c | 50 +++++++++++++
6 files changed, 185 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c
create mode 100644 sysdeps/riscv/multiarch/memrchr-vector.S
create mode 100644 sysdeps/riscv/rvv/memrchr.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
diff --git a/sysdeps/riscv/multiarch/memrchr-generic.c b/sysdeps/riscv/multiarch/memrchr-generic.c
new file mode 100644
index 0000000000..09766ac565
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memrchr-generic.c
@@ -0,0 +1,28 @@
+/* Re-include the default memrchr 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 MEMRCHR __memrchr_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# undef weak_alias
+# define weak_alias(a, b)
+# include <string/memrchr.c>
+#endif
diff --git a/sysdeps/riscv/multiarch/memrchr-vector.S b/sysdeps/riscv/multiarch/memrchr-vector.S
new file mode 100644
index 0000000000..1554c066aa
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memrchr-vector.S
@@ -0,0 +1,28 @@
+/* Re-include the RISC-V RVV based memrchr 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 MEMRCHR __memrchr_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)
+# include <sysdeps/riscv/rvv/memrchr.S>
+#endif
diff --git a/sysdeps/riscv/rvv/memrchr.S b/sysdeps/riscv/rvv/memrchr.S
new file mode 100644
index 0000000000..00df77e5d7
--- /dev/null
+++ b/sysdeps/riscv/rvv/memrchr.S
@@ -0,0 +1,71 @@
+/* RISC-V RVV based memrchr.
+ 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 MEMRCHR
+# define MEMRCHR __memrchr
+#endif
+
+#define src a0
+#define ch a1
+#define num a2
+#define vl a3
+#define first_idx a4
+#define idx a5
+
+#define vmask v0
+#define vdata v8
+#define vidx v16
+#define vmax v24
+
+ENTRY (MEMRCHR)
+.option push
+.option arch, +v
+ add src, src, num
+L(loop):
+ beqz num, L(nohit)
+
+ vsetvli vl, num, e8, m4, ta, ma
+ sub src, src, vl
+ vle8.v vdata, (src)
+ sub num, num, vl
+
+ vmseq.vx vmask, vdata, ch
+ vfirst.m first_idx, vmask
+ bltz first_idx, L(loop)
+L(hit):
+ vsetvli zero, vl, e16, m8, ta, ma
+ vid.v vidx
+
+ /* reduce max index over hit lanes */
+ vmv.s.x vmax, x0
+ vredmaxu.vs vmax, vidx, vmax, vmask.t
+ vmv.x.s idx, vmax
+
+ add a0, src, idx
+ ret
+L(nohit):
+ li a0, 0
+ ret
+.option pop
+END (MEMRCHR)
+libc_hidden_def (MEMRCHR)
+weak_alias (MEMRCHR, memrchr)
+libc_hidden_builtin_def (memrchr)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index a0990c9ff1..190e853e6e 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -19,6 +19,9 @@ sysdep_routines += \
memmove \
memmove-generic \
memmove-vector \
+ memrchr \
+ memrchr-generic \
+ memrchr-vector \
memset \
memset-generic \
memset-vector \
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 d955f56dba..53e76705df 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -80,5 +80,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
__memmove_vector)
IFUNC_IMPL_ADD (array, i, memmove, 1, __memmove_generic))
+ IFUNC_IMPL (i, name, memrchr,
+ IFUNC_IMPL_ADD (array, i, memrchr, rvv_enabled,
+ __memrchr_vector)
+ IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic))
+
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
new file mode 100644
index 0000000000..310b8e14f0
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c
@@ -0,0 +1,50 @@
+/* Multiple versions of memrchr.
+ 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 memrchr so that the compiler won't complain about the type
+ mismatch with the IFUNC selector in strong_alias, below. */
+# define memrchr __redirect_memrchr
+# include <stdint.h>
+# include <string.h>
+# undef memrchr
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_memrchr) __memrchr_generic attribute_hidden;
+extern __typeof (__redirect_memrchr) __memrchr_vector attribute_hidden;
+
+static inline __typeof (__redirect_memrchr) *
+select_memrchr_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 __memrchr_vector;
+ return __memrchr_generic;
+}
+
+riscv_libc_ifunc_redirected (__redirect_memrchr, __memrchr, select_memrchr_ifunc);
+libc_hidden_def (__memrchr)
+weak_alias (__memrchr, memrchr)
+#else
+# include <string/memrchr.c>
+#endif
--
2.47.3
More information about the Libc-alpha
mailing list