]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
riscv: Add RVV memmove for both multiarch and non-multiarch builds master
authorYao Zihong <zihong.plct@isrc.iscas.ac.cn>
Sun, 21 Jun 2026 03:54:22 +0000 (22:54 -0500)
committerPeter Bergner <bergner@tenstorrent.com>
Sun, 21 Jun 2026 03:56:09 +0000 (22:56 -0500)
This patch adds an RVV-optimized implementation of memmove 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 (__memmove_vector) is added
alongside the generic fallback (__memmove_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 memmove().

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/dl-symbol-redir-ifunc.h
sysdeps/riscv/multiarch/memmove-generic.c [new file with mode: 0644]
sysdeps/riscv/multiarch/memmove-vector.S [new file with mode: 0644]
sysdeps/riscv/rvv/memmove.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/memmove.c [new file with mode: 0644]

index f81e5b7e6de11c30537f08893a134f1fbbc99e7e..852d3f1c1418fd516289038906befed42b82f624 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef SHARED
 asm ("memcmp = __memcmp_generic");
 asm ("memcpy = __memcpy_generic");
+asm ("memmove = __memmove_generic");
 asm ("memset = __memset_generic");
 asm ("strlen = __strlen_generic");
 #endif
diff --git a/sysdeps/riscv/multiarch/memmove-generic.c b/sysdeps/riscv/multiarch/memmove-generic.c
new file mode 100644 (file)
index 0000000..44c01c8
--- /dev/null
@@ -0,0 +1,26 @@
+/* Re-include the default memmove 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 MEMMOVE __memmove_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+# include <string/memmove.c>
+#endif
diff --git a/sysdeps/riscv/multiarch/memmove-vector.S b/sysdeps/riscv/multiarch/memmove-vector.S
new file mode 100644 (file)
index 0000000..6bf2d9f
--- /dev/null
@@ -0,0 +1,24 @@
+/* Re-include the RISC-V RVV based memmove 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 MEMMOVE __memmove_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# include <sysdeps/riscv/rvv/memmove.S>
+#endif
diff --git a/sysdeps/riscv/rvv/memmove.S b/sysdeps/riscv/rvv/memmove.S
new file mode 100644 (file)
index 0000000..71ebb6a
--- /dev/null
@@ -0,0 +1,79 @@
+/* RISC-V RVV based memmove.
+   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 MEMMOVE
+# define MEMMOVE memmove
+#endif
+
+#define dst a0
+#define src a1
+#define num a2
+
+#define ivl a3
+#define dst_ptr a4
+#define diff a5
+
+#define ELEM_LMUL_SETTING m8
+#define vdata v0
+
+ENTRY (MEMMOVE)
+.option push
+.option arch, +v
+    /* There is nothing to do if we are copying zero bytes or DST == SRC.  */
+    beqz num, L(done)
+    sub diff, dst, src
+    beqz diff, L(done)
+
+    /* If (unsigned)(dst - src) < num, then we have an overlap scenario that
+       requires a backward copy loop.  Otherwise, we can use a faster forward
+       copy loop.  */
+    bltu diff, num, L(do_backward_loop)
+
+    mv dst_ptr, dst
+
+L(forward_copy_loop):
+    vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma
+    vle8.v vdata, (src)
+    sub num, num, ivl
+    add src, src, ivl
+    vse8.v vdata, (dst_ptr)
+    add dst_ptr, dst_ptr, ivl
+    bnez num, L(forward_copy_loop)
+L(done):
+    ret
+
+L(do_backward_loop):
+    add src, src, num
+    add dst_ptr, dst, num
+
+L(backward_copy_loop):
+    vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma
+    sub src, src, ivl
+    sub dst_ptr, dst_ptr, ivl
+    vle8.v vdata, (src)
+    sub num, num, ivl
+    vse8.v vdata, (dst_ptr)
+    bnez num, L(backward_copy_loop)
+    ret
+
+.option pop
+END (MEMMOVE)
+libc_hidden_builtin_def (memmove)
index 30d92c1ba92a993edc0a0f10f7a74fc4dc0e2ae7..929df14a6f172491aebf7be46077b7b498855044 100644 (file)
@@ -13,6 +13,9 @@ sysdep_routines += \
   memcpy-generic \
   memcpy-vector \
   memcpy_noalignment \
+  memmove \
+  memmove-generic \
+  memmove-vector \
   memset \
   memset-generic \
   memset-vector \
index 80275785293ef3f4789ebb704460b26c4e7a8e6d..4c28e226061d10c101a94e446e165d6527078f5a 100644 (file)
@@ -105,5 +105,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
                              __strrchr_vector)
              IFUNC_IMPL_ADD (array, i, strrchr, 1, __strrchr_generic))
 
+  IFUNC_IMPL (i, name, memmove,
+             IFUNC_IMPL_ADD (array, i, memmove, rvv_enabled,
+                             __memmove_vector)
+             IFUNC_IMPL_ADD (array, i, memmove, 1, __memmove_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c
new file mode 100644 (file)
index 0000000..670f108
--- /dev/null
@@ -0,0 +1,56 @@
+/* Multiple versions of memmove.
+   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 memmove so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef memmove
+# define memmove __redirect_memmove
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_memmove) __libc_memmove;
+
+extern __typeof (__redirect_memmove) __memmove_generic attribute_hidden;
+extern __typeof (__redirect_memmove) __memmove_vector attribute_hidden;
+
+static inline __typeof (__redirect_memmove) *
+select_memmove_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 __memmove_vector;
+  return __memmove_generic;
+}
+
+riscv_libc_ifunc (__libc_memmove, select_memmove_ifunc);
+
+# undef memmove
+strong_alias (__libc_memmove, memmove);
+# ifdef SHARED
+__hidden_ver1 (memmove, __GI_memmove, __redirect_memmove)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memmove);
+# endif
+#else
+# include <string/memmove.c>
+#endif