[PATCH v3 04/17] riscv: Add RVV memcmpeq for multiarch and non-multiarch

Yao Zihong zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:31 GMT 2025


Co-authored-by: Hau Hsu <hau.hsu@sifive.com>
Co-authored-by: Yun Hsiang <yun.hsiang@sifive.com>
Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
 sysdeps/riscv/multiarch/memcmpeq-generic.c    | 31 +++++++++
 sysdeps/riscv/multiarch/memcmpeq-vector.S     | 30 +++++++++
 sysdeps/riscv/riscv-ifunc.h                   |  4 ++
 sysdeps/riscv/rvv/memcmpeq.S                  | 67 +++++++++++++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  3 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  5 ++
 .../sysv/linux/riscv/multiarch/memcmpeq.c     | 50 ++++++++++++++
 7 files changed, 190 insertions(+)
 create mode 100644 sysdeps/riscv/multiarch/memcmpeq-generic.c
 create mode 100644 sysdeps/riscv/multiarch/memcmpeq-vector.S
 create mode 100644 sysdeps/riscv/rvv/memcmpeq.S
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memcmpeq.c

diff --git a/sysdeps/riscv/multiarch/memcmpeq-generic.c b/sysdeps/riscv/multiarch/memcmpeq-generic.c
new file mode 100644
index 0000000000..da8921f1fe
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memcmpeq-generic.c
@@ -0,0 +1,31 @@
+/* Re-include the default memcmpeq 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 MEMCMP ____memcmpeq_generic
+# undef libc_hidden_def
+# define libc_hidden_def(x)
+# undef weak_alias
+# define weak_alias(x, x2)
+# undef strong_alias
+# define strong_alias(x, x2)
+#endif
+
+#include <string/memcmp.c>
diff --git a/sysdeps/riscv/multiarch/memcmpeq-vector.S b/sysdeps/riscv/multiarch/memcmpeq-vector.S
new file mode 100644
index 0000000000..8ab1baa298
--- /dev/null
+++ b/sysdeps/riscv/multiarch/memcmpeq-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based memcmpeq 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 MEMCMPEQ ____memcmpeq_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/memcmpeq.S>
+#endif
diff --git a/sysdeps/riscv/riscv-ifunc.h b/sysdeps/riscv/riscv-ifunc.h
index 7c8e5dace7..6661ef9786 100644
--- a/sysdeps/riscv/riscv-ifunc.h
+++ b/sysdeps/riscv/riscv-ifunc.h
@@ -25,3 +25,7 @@
 #define riscv_libc_ifunc(name, expr)				\
   __ifunc_args (name, name, expr(hwcap, hwprobe), INIT_ARCH,	\
                 uint64_t hwcap, __riscv_hwprobe_t hwprobe)
+
+#define riscv_libc_ifunc_redirected(redirected_name, name, expr)	\
+  __ifunc_args (redirected_name, name, expr(hwcap, hwprobe), INIT_ARCH,	\
+                uint64_t hwcap, __riscv_hwprobe_t hwprobe)
diff --git a/sysdeps/riscv/rvv/memcmpeq.S b/sysdeps/riscv/rvv/memcmpeq.S
new file mode 100644
index 0000000000..d9ac0167b4
--- /dev/null
+++ b/sysdeps/riscv/rvv/memcmpeq.S
@@ -0,0 +1,67 @@
+/* RISC-V RVV based memcmpeq.
+   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 MEMCMPEQ
+# define MEMCMPEQ ____memcmpeq
+#endif
+
+#define result a0
+
+#define src1 a0
+#define src2 a1
+#define num a2
+
+#define ivl a3
+#define temp a4
+
+#define ELEM_LMUL_SETTING m1
+#define vdata1 v0
+#define vdata2 v8
+#define vmask v16
+
+ENTRY (MEMCMPEQ)
+.option push
+.option arch, +v
+L(loop):
+    vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma
+
+    vle8.v vdata1, (src1)
+    vle8.v vdata2, (src2)
+
+    vmsne.vv vmask, vdata1, vdata2
+    sub num, num, ivl
+    vfirst.m temp, vmask
+
+    /* Skip the loop if we find the different value between src1 and src2. */
+    bgez temp, L(found)
+
+    add src1, src1, ivl
+    add src2, src2, ivl
+
+    bnez num, L(loop)
+
+    li result, 0
+    ret
+L(found):
+    mv result, ivl
+    ret
+.option pop
+END (MEMCMPEQ)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 0edc677874..7cfb4f04dc 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -9,6 +9,9 @@ sysdep_routines += \
   memcmp \
   memcmp-generic \
   memcmp-vector \
+  memcmpeq \
+  memcmpeq-generic \
+  memcmpeq-vector \
   memcpy \
   memcpy-generic \
   memcpy_noalignment \
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 9f75541aa7..ae114f37e6 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -68,5 +68,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, __memcmpeq,
+	      IFUNC_IMPL_ADD (array, i, __memcmpeq, rvv_enabled,
+			      ____memcmpeq_vector)
+	      IFUNC_IMPL_ADD (array, i, __memcmpeq, 1, ____memcmpeq_generic))
+
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memcmpeq.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memcmpeq.c
new file mode 100644
index 0000000000..41758f5548
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memcmpeq.c
@@ -0,0 +1,50 @@
+/* Multiple versions of __memcmpeq.
+   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 __memcmpeq so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# define __memcmpeq __redirect___memcmpeq
+# include <stdint.h>
+# include <string.h>
+# undef __memcmpeq
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect___memcmpeq) ____memcmpeq_generic attribute_hidden;
+extern __typeof (__redirect___memcmpeq) ____memcmpeq_vector attribute_hidden;
+
+static inline __typeof (__redirect___memcmpeq) *
+select___memcmpeq_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 ____memcmpeq_vector;
+  return ____memcmpeq_generic;
+}
+
+riscv_libc_ifunc_redirected (__redirect___memcmpeq, __memcmpeq, select___memcmpeq_ifunc);
+# ifdef SHARED
+__hidden_ver1 (__memcmpeq, __GI___memcmpeq, __redirect___memcmpeq)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (__memcmpeq);
+# endif
+#endif
-- 
2.47.2



More information about the Libc-alpha mailing list