[PATCH v3 14/17] riscv: Add RVV strncmp for multiarch and non-multiarch
Yao Zihong
zihong.plct@isrc.iscas.ac.cn
Tue Dec 30 15:30:41 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/strncmp-generic.c | 26 ++++++
sysdeps/riscv/multiarch/strncmp-vector.S | 30 +++++++
sysdeps/riscv/rvv/strncmp.S | 88 +++++++++++++++++++
.../unix/sysv/linux/riscv/multiarch/Makefile | 3 +
.../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++
.../unix/sysv/linux/riscv/multiarch/strncmp.c | 56 ++++++++++++
6 files changed, 208 insertions(+)
create mode 100644 sysdeps/riscv/multiarch/strncmp-generic.c
create mode 100644 sysdeps/riscv/multiarch/strncmp-vector.S
create mode 100644 sysdeps/riscv/rvv/strncmp.S
create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
diff --git a/sysdeps/riscv/multiarch/strncmp-generic.c b/sysdeps/riscv/multiarch/strncmp-generic.c
new file mode 100644
index 0000000000..6477713a1c
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncmp-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strncmp 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 STRNCMP __strncmp_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/strncmp.c>
diff --git a/sysdeps/riscv/multiarch/strncmp-vector.S b/sysdeps/riscv/multiarch/strncmp-vector.S
new file mode 100644
index 0000000000..581a271762
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strncmp-vector.S
@@ -0,0 +1,30 @@
+/* Re-include the RISC-V RVV based strncmp 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 STRNCMP __strncmp_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/strncmp.S>
+#endif
diff --git a/sysdeps/riscv/rvv/strncmp.S b/sysdeps/riscv/rvv/strncmp.S
new file mode 100644
index 0000000000..54dcb306be
--- /dev/null
+++ b/sysdeps/riscv/rvv/strncmp.S
@@ -0,0 +1,88 @@
+/* RISC-V RVV based strncmp.
+ 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 STRNCMP
+# define STRNCMP strncmp
+#endif
+
+#define result a0
+
+#define str1 a0
+#define str2 a1
+#define length a2
+
+#define ivl a3
+#define temp1 a4
+#define temp2 a5
+
+#define ELEM_LMUL_SETTING m1
+#define vstr1 v0
+#define vstr2 v4
+#define vmask1 v8
+#define vmask2 v9
+
+ENTRY (STRNCMP)
+.option push
+.option arch, +v
+ beqz length, L(zero_length)
+L(loop):
+ vsetvli zero, length, e8, ELEM_LMUL_SETTING, ta, ma
+
+ vle8ff.v vstr1, (str1)
+ /* vstr1[i] == 0. */
+ vmseq.vx vmask1, vstr1, zero
+
+ vle8ff.v vstr2, (str2)
+ /* vstr1[i] != vstr2[i]. */
+ vmsne.vv vmask2, vstr1, vstr2
+
+ csrr ivl, vl
+
+ /* r = mask1 | mask2
+ We could use vfirst.m to get the first zero char or the
+ first different char between str1 and str2. */
+ vmor.mm vmask1, vmask1, vmask2
+
+ sub length, length, ivl
+
+ vfirst.m temp1, vmask1
+
+ bgez temp1, L(end_loop)
+
+ add str1, str1, ivl
+ add str2, str2, ivl
+ bnez length, L(loop)
+L(end_loop):
+
+ add str1, str1, temp1
+ add str2, str2, temp1
+ lbu temp1, 0(str1)
+ lbu temp2, 0(str2)
+
+ sub result, temp1, temp2
+ ret
+
+L(zero_length):
+ li result, 0
+ ret
+.option pop
+END (STRNCMP)
+libc_hidden_builtin_def (strncmp)
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index 7814076015..01ad5a03ee 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -43,6 +43,9 @@ sysdep_routines += \
strncat \
strncat-generic \
strncat-vector \
+ strncmp \
+ strncmp-generic \
+ strncmp-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 3432c036d7..72829c28eb 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -115,5 +115,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
__strncat_vector)
IFUNC_IMPL_ADD (array, i, strncat, 1, __strncat_generic))
+ IFUNC_IMPL (i, name, strncmp,
+ IFUNC_IMPL_ADD (array, i, strncmp, rvv_enabled,
+ __strncmp_vector)
+ IFUNC_IMPL_ADD (array, i, strncmp, 1, __strncmp_generic))
+
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
new file mode 100644
index 0000000000..c173f767ef
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncmp.c
@@ -0,0 +1,56 @@
+/* Multiple versions of strncmp.
+ 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 strncmp so that the compiler won't complain about the type
+ mismatch with the IFUNC selector in strong_alias, below. */
+# undef strncmp
+# define strncmp __redirect_strncmp
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+
+extern __typeof (__redirect_strncmp) __libc_strncmp;
+
+extern __typeof (__redirect_strncmp) __strncmp_generic attribute_hidden;
+extern __typeof (__redirect_strncmp) __strncmp_vector attribute_hidden;
+
+static inline __typeof (__redirect_strncmp) *
+select_strncmp_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 __strncmp_vector;
+ return __strncmp_generic;
+}
+
+riscv_libc_ifunc (__libc_strncmp, select_strncmp_ifunc);
+
+# undef strncmp
+strong_alias (__libc_strncmp, strncmp);
+# ifdef SHARED
+__hidden_ver1 (strncmp, __GI_strncmp, __redirect_strncmp)
+ __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strncmp);
+# endif
+#else
+# include <string/strncmp.c>
+#endif
--
2.47.2
More information about the Libc-alpha
mailing list