[PATCH v5 11/18] riscv: Add RVV strcmp for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Feb 3 15:05:35 GMT 2026
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/strcmp-generic.c | 24 +++++
sysdeps/riscv/multiarch/strcmp-vector.S | 24 +++++
sysdeps/riscv/rvv/strcmp.S | 93 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 +
.../unix/sysv/linux/riscv/multiarch/strcmp.c | 56 +++++++++++
6 files changed, 205 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/strcmp-generic.c
create mode 100644 sysdeps/riscv/multiarch/strcmp-vector.S
create mode 100644 sysdeps/riscv/rvv/strcmp.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
diff --git a/sysdeps/riscv/multiarch/strcmp-generic.c b/sysdeps/riscv/multiarch/strcmp-generic.c
new file mode 100644
index 0000000000..8ef46a44b9
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strcmp-generic.c
@@ -0,0 +1,24 @@
+/* Re-include the default strcmp 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 STRCMP __strcmp_generic
+# include <string/strcmp.c>
+#endif
diff --git a/sysdeps/riscv/multiarch/strcmp-vector.S b/sysdeps/riscv/multiarch/strcmp-vector.S
new file mode 100644
index 0000000000..b9d9074b24
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strcmp-vector.S
@@ -0,0 +1,24 @@
+/* Re-include the RISC-V RVV based strcmp 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 STRCMP __strcmp_vector
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(name)
+# include <sysdeps/riscv/rvv/strcmp.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strcmp.S b/sysdeps/riscv/rvv/strcmp.S
new file mode 100644
index 0000000000..ad672591d4
--- /dev/null
+++ b/sysdeps/riscv/rvv/strcmp.S
@@ -0,0 +1,93 @@
+/* RISC-V RVV based strcmp.
+ 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 STRCMP
+# define STRCMP strcmp
+#endif
+
+#define result a0
+
+#define str1 a0
+#define str2 a1
+
+#define ivl a2
+#define temp1 a3
+#define temp2 a4
+
+#define vstr1 v0
+#define vstr2 v8
+#define vmask1 v16
+#define vmask2 v17
+
+ENTRY (STRCMP)
+.option push
+.option arch, +v
+ /* lmul=1 */
+L(Loop):
+ vsetvli ivl, zero, e8, m1, ta, ma
+ vle8ff.v vstr1, (str1)
+ /* check if vstr1[i] == 0 */
+ vmseq.vx vmask1, vstr1, zero
+
+ vle8ff.v vstr2, (str2)
+ /* check if vstr1[i] != vstr2[i] */
+ vmsne.vv vmask2, vstr1, vstr2
+
+ /* find the index x for vstr1[x]==0 */
+ vfirst.m temp1, vmask1
+ /* find the index x for vstr1[x]!=vstr2[x] */
+ vfirst.m temp2, vmask2
+
+ bgez temp1, L(check1)
+ bgez temp2, L(check2)
+
+ /* get the current vl updated by vle8ff. */
+ csrr ivl, vl
+ add str1, str1, ivl
+ add str2, str2, ivl
+ j L(Loop)
+
+ /* temp1>=0 */
+L(check1):
+ bltz temp2, L(return_at_nul)
+ blt temp2, temp1, L(check2)
+L(return_at_nul):
+ /* temp2<0 */
+ /* temp2>=0 && temp1<temp2 */
+ add str1, str1, temp1
+ add str2, str2, temp1
+ lbu temp1, 0(str1)
+ lbu temp2, 0(str2)
+ sub result, temp1, temp2
+ ret
+
+ /* temp1<0 */
+ /* temp2>=0 */
+L(check2):
+ add str1, str1, temp2
+ add str2, str2, temp2
+ lbu temp1, 0(str1)
+ lbu temp2, 0(str2)
+ sub result, temp1, temp2
+ ret
+.option pop
+END (STRCMP)
+libc_hidden_builtin_def (strcmp)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index f3155f41c4..3dfee5799c 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -34,6 +34,9 @@ sysdep_routines += \
strchr \
strchr-generic \
strchr-vector \
+ strcmp \
+ strcmp-generic \
+ strcmp-vector \
# sysdep_routines
CFLAGS-memcpy_noalignment.c += -mno-strict-align
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 3d059a33e4..872e8d12a7 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -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, strcmp,
+ IFUNC_IMPL_ADD (array, i, strcmp, rvv_enabled,
+ __strcmp_vector)
+ IFUNC_IMPL_ADD (array, i, strcmp, 1, __strcmp_generic))
+
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
new file mode 100644
index 0000000000..9398ad4024
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strcmp.c
@@ -0,0 +1,56 @@
+/* Multiple versions of strcmp.
+ 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 strcmp so that the compiler won't complain about the type
+ mismatch with the IFUNC selector in strong_alias, below. */
+# undef strcmp
+# define strcmp __redirect_strcmp
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strcmp) __libc_strcmp;
+
+extern __typeof (__redirect_strcmp) __strcmp_generic attribute_hidden;
+extern __typeof (__redirect_strcmp) __strcmp_vector attribute_hidden;
+
+static inline __typeof (__redirect_strcmp) *
+select_strcmp_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 __strcmp_vector;
+ return __strcmp_generic;
+}
+
+riscv_libc_ifunc (__libc_strcmp, select_strcmp_ifunc);
+
+# undef strcmp
+strong_alias (__libc_strcmp, strcmp);
+# ifdef SHARED
+__hidden_ver1 (strcmp, __GI_strcmp, __redirect_strcmp)
+ __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strcmp);
+# endif
+#else
+# include <string/strcmp.c>
+#endif
--
2.47.3
More information about the Libc-alpha
mailing list