]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
riscv: Add RVV strrchr for both multiarch and non-multiarch builds
authorYao Zihong <zihong.plct@isrc.iscas.ac.cn>
Fri, 5 Jun 2026 00:39:31 +0000 (19:39 -0500)
committerPeter Bergner <bergner@tenstorrent.com>
Fri, 5 Jun 2026 00:48:14 +0000 (00:48 +0000)
This patch adds an RVV-optimized implementation of strrchr for RISC-V and
enables it for both multiarch (IFUNC) and non-multiarch builds.

The implementation integrates Hau Hsu's 2023 RVV work under a unified
ifunc-based framework. A vectorized version (__strrchr_vector) is added
alongside the generic fallback (__strrchr_generic). The runtime resolver
selects the RVV variant when RISCV_HWPROBE_KEY_IMA_EXT_0 reports vector
support (RVV).

Currently, the resolver still selects the RVV variant even when the RVV
extension is disabled via prctl(). As a consequence, any process that
has RVV disabled via prctl() will receive SIGILL when calling strrchr().

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>
Reviewed-by: Peter Bergner <bergner@tenstorrent.com>
sysdeps/riscv/multiarch/strrchr-generic.c [new file with mode: 0644]
sysdeps/riscv/multiarch/strrchr-vector.S [new file with mode: 0644]
sysdeps/riscv/rvv/strrchr.S [new file with mode: 0644]
sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c [new file with mode: 0644]

diff --git a/sysdeps/riscv/multiarch/strrchr-generic.c b/sysdeps/riscv/multiarch/strrchr-generic.c
new file mode 100644 (file)
index 0000000..c61c6e1
--- /dev/null
@@ -0,0 +1,28 @@
+/* Re-include the default strrchr 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 STRRCHR __strrchr_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+# undef weak_alias
+# define weak_alias(x, x2)
+# include <string/strrchr.c>
+#endif
diff --git a/sysdeps/riscv/multiarch/strrchr-vector.S b/sysdeps/riscv/multiarch/strrchr-vector.S
new file mode 100644 (file)
index 0000000..8fef68e
--- /dev/null
@@ -0,0 +1,26 @@
+/* Re-include the RISC-V RVV based strrchr 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 STRRCHR __strrchr_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# undef weak_alias
+# define weak_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 (file)
index 0000000..f2da8a4
--- /dev/null
@@ -0,0 +1,123 @@
+/* RISC-V RVV based strrchr.
+   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 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)
index 2714b9e2d85cf778c8f71878d5c5147422cb5291..30d92c1ba92a993edc0a0f10f7a74fc4dc0e2ae7 100644 (file)
@@ -34,6 +34,9 @@ sysdep_routines += \
   strncmp \
   strncmp-generic \
   strncmp-vector \
+  strrchr \
+  strrchr-generic \
+  strrchr-vector \
   # sysdep_routines
 
 CFLAGS-memcpy_noalignment.c += -mno-strict-align
index f00001097b768d76f958f178fed0e62b4868dc9c..80275785293ef3f4789ebb704460b26c4e7a8e6d 100644 (file)
@@ -100,5 +100,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
                              __strchr_vector)
              IFUNC_IMPL_ADD (array, i, strchr, 1, __strchr_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 (file)
index 0000000..3532af0
--- /dev/null
@@ -0,0 +1,59 @@
+/* Multiple versions of strrchr.
+   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 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