]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
riscv: Add RVV memchr for both multiarch and non-multiarch builds
authorYao Zihong <zihong.plct@isrc.iscas.ac.cn>
Fri, 5 Jun 2026 00:34:33 +0000 (19:34 -0500)
committerPeter Bergner <bergner@tenstorrent.com>
Fri, 5 Jun 2026 00:48:01 +0000 (00:48 +0000)
This patch adds an RVV-optimized implementation of memchr 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 (__memchr_vector) is added
alongside the generic fallback (__memchr_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 memchr().

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/memchr-generic.c [new file with mode: 0644]
sysdeps/riscv/multiarch/memchr-vector.S [new file with mode: 0644]
sysdeps/riscv/rvv/memchr.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/memchr.c [new file with mode: 0644]

diff --git a/sysdeps/riscv/multiarch/memchr-generic.c b/sysdeps/riscv/multiarch/memchr-generic.c
new file mode 100644 (file)
index 0000000..a95e16e
--- /dev/null
@@ -0,0 +1,24 @@
+/* Re-include the default memchr 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 MEMCHR __memchr_generic
+# include <string/memchr.c>
+#endif
diff --git a/sysdeps/riscv/multiarch/memchr-vector.S b/sysdeps/riscv/multiarch/memchr-vector.S
new file mode 100644 (file)
index 0000000..951f256
--- /dev/null
@@ -0,0 +1,26 @@
+/* Re-include the RISC-V RVV based memchr 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 MEMCHR __memchr_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# undef weak_alias
+# define weak_alias(name, alias)
+# include <sysdeps/riscv/rvv/memchr.S>
+#endif
diff --git a/sysdeps/riscv/rvv/memchr.S b/sysdeps/riscv/rvv/memchr.S
new file mode 100644 (file)
index 0000000..609b2f9
--- /dev/null
@@ -0,0 +1,67 @@
+/* RISC-V RVV based memchr.
+   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 MEMCHR
+# define MEMCHR __memchr
+#endif
+
+#define result a0
+
+#define src a0
+#define value a1
+#define num a2
+
+#define ivl a3
+#define temp a4
+
+#define ELEM_LMUL_SETTING m8
+#define vdata v0
+#define vmask v8
+
+ENTRY (MEMCHR)
+.option push
+.option arch, +v
+L(loop):
+    vsetvli zero, num, e8, ELEM_LMUL_SETTING, ta, ma
+
+    vle8ff.v vdata, (src)
+    /* Find the value inside the loaded data.  */
+    vmseq.vx vmask, vdata, value
+    vfirst.m temp, vmask
+
+    /* Skip the loop if we find the matched value.  */
+    bgez temp, L(found)
+
+    csrr ivl, vl
+    sub num, num, ivl
+    add src, src, ivl
+
+    bnez num, L(loop)
+
+    li result, 0
+    ret
+L(found):
+    add result, src, temp
+    ret
+.option pop
+END (MEMCHR)
+weak_alias (MEMCHR, memchr)
+libc_hidden_builtin_def (memchr)
index 52730cf01c3d6c02462875ae0c1dee917cb8c448..c74b470bd634cec58e1c0ae7a987c1f1becfbd03 100644 (file)
@@ -3,6 +3,9 @@ sysdep_routines += \
   memccpy \
   memccpy-generic \
   memccpy-vector \
+  memchr \
+  memchr-generic \
+  memchr-vector \
   memcmp \
   memcmp-generic \
   memcmp-vector \
index a373de6c89eaf16f917f68a4bb66a93135338a2d..caea83b7d761e0d6f454a7dda84b550c1c1efc69 100644 (file)
@@ -90,5 +90,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
                              __memcmp_vector)
              IFUNC_IMPL_ADD (array, i, memcmp, 1, __memcmp_generic))
 
+  IFUNC_IMPL (i, name, memchr,
+             IFUNC_IMPL_ADD (array, i, memchr, rvv_enabled,
+                             __memchr_vector)
+             IFUNC_IMPL_ADD (array, i, memchr, 1, __memchr_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c
new file mode 100644 (file)
index 0000000..0844a33
--- /dev/null
@@ -0,0 +1,57 @@
+/* Multiple versions of memchr.
+   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 memchr so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef memchr
+# define memchr __redirect_memchr
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_memchr) __libc_memchr;
+
+extern __typeof (__redirect_memchr) __memchr_generic attribute_hidden;
+extern __typeof (__redirect_memchr) __memchr_vector attribute_hidden;
+
+static inline __typeof (__redirect_memchr) *
+select_memchr_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 __memchr_vector;
+  return __memchr_generic;
+}
+
+riscv_libc_ifunc (__libc_memchr, select_memchr_ifunc);
+
+# undef memchr
+strong_alias (__libc_memchr, memchr);
+strong_alias (memchr, __memchr)
+# ifdef SHARED
+__hidden_ver1 (memchr, __GI_memchr, __redirect_memchr)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memchr);
+# endif
+#else
+# include <string/memchr.c>
+#endif