[PATCH v3 06/17] riscv: Add RVV memmove for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:33 GMT 2025
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>
---
sysdeps/riscv/multiarch/memmove-generic.c | 26 +++++++
sysdeps/riscv/multiarch/memmove-vector.S | 30 ++++++++
sysdeps/riscv/rvv/memmove.S | 74 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++
.../unix/sysv/linux/riscv/multiarch/memmove.c | 56 ++++++++++++++
6 files changed, 194 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/memmove-generic.c
create mode 100644 sysdeps/riscv/multiarch/memmove-vector.S
create mode 100644 sysdeps/riscv/rvv/memmove.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c
diff --git a/sysdeps/riscv/multiarch/memmove-generic.c b/sysdeps/riscv/multiarch/memmove-generic.c
new file mode 100644
index 0000000000..0f41a23fac
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memmove-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default memmove 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 MEMMOVE __memmove_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/memmove.c>
diff --git a/sysdeps/riscv/multiarch/memmove-vector.S b/sysdeps/riscv/multiarch/memmove-vector.S
new file mode 100644
index 0000000000..be77c5957c
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memmove-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based memmove 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 MEMMOVE __memmove_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/memmove.S>
+#endif
diff --git a/sysdeps/riscv/rvv/memmove.S b/sysdeps/riscv/rvv/memmove.S
new file mode 100644
index 0000000000..8c2af67946
--- /dev/null
+++ b/sysdeps/riscv/rvv/memmove.S
@@ -0,0 +1,74 @@
+/* RISC-V RVV based memmove.
+ 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 MEMMOVE
+# define MEMMOVE memmove
+#endif
+
+#define dst a0
+#define src a1
+#define num a2
+
+#define ivl a3
+#define dst_ptr a4
+#define src_backward_ptr a5
+#define dst_backward_ptr a6
+
+#define ELEM_LMUL_SETTING m8
+#define vdata v0
+
+ENTRY (MEMMOVE)
+.option push
+.option arch, +v
+ mv dst_ptr, dst
+
+ /* If src is equal or after dst, all data in src will be loaded before
+ overwrited for the overlapping case. We could use faster `forward-copy`. */
+ bgeu src, dst, L(forward_copy_loop)
+ add src_backward_ptr, src, num
+ add dst_backward_ptr, dst, num
+ /* If dst inside source data range, we need to use `backward_copy_loop` to
+ handle the overlapping issue. */
+ bltu dst, src_backward_ptr, L(backward_copy_loop)
+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)
+ ret
+L(backward_copy_loop):
+ vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma
+
+ sub src_backward_ptr, src_backward_ptr, ivl
+ vle8.v vdata, (src_backward_ptr)
+ sub num, num, ivl
+ sub dst_backward_ptr, dst_backward_ptr, ivl
+ vse8.v vdata, (dst_backward_ptr)
+ bnez num, L(backward_copy_loop)
+ ret
+.option pop
+END (MEMMOVE)
+libc_hidden_builtin_def (memmove)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 0bf7c8e37d..a0990c9ff1 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -16,6 +16,9 @@ sysdep_routines += \
memcpy-generic \
memcpy-vector \
memcpy_noalignment \
+ memmove \
+ memmove-generic \
+ memmove-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 1c43a5b1f9..b931d3ff52 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -75,5 +75,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
____memcmpeq_vector)
IFUNC_IMPL_ADD (array, i, __memcmpeq, 1, ____memcmpeq_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
index 0000000000..a5d9d6efbd
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c
@@ -0,0 +1,56 @@
+/* Multiple versions of memmove.
+ 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 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
--
2.47.2
More information about the Libc-alpha
mailing list