]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
riscv: Add RVV strcmp for both multiarch and non-multiarch builds
authorYao Zihong <zihong.plct@isrc.iscas.ac.cn>
Tue, 5 May 2026 21:12:37 +0000 (16:12 -0500)
committerPeter Bergner <bergner@tenstorrent.com>
Fri, 8 May 2026 14:32:11 +0000 (09:32 -0500)
This patch adds an RVV-optimized implementation of strcmp 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 (__strcmp_vector) is added
alongside the generic fallback (__strcmp_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 strcmp().

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/strcmp-generic.c [new file with mode: 0644]
sysdeps/riscv/multiarch/strcmp-vector.S [new file with mode: 0644]
sysdeps/riscv/rvv/strcmp.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/strcmp.c [new file with mode: 0644]

diff --git a/sysdeps/riscv/multiarch/strcmp-generic.c b/sysdeps/riscv/multiarch/strcmp-generic.c
new file mode 100644 (file)
index 0000000..8ef46a4
--- /dev/null
@@ -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 (file)
index 0000000..b9d9074
--- /dev/null
@@ -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 (file)
index 0000000..a2f6865
--- /dev/null
@@ -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)
index 2a2b90960f4c437ea70be88f244ed08aede78bf5..3da0306c5c5923d13b00cd222c1f1f908745d3dd 100644 (file)
@@ -10,6 +10,9 @@ sysdep_routines += \
   strcat \
   strcat-generic \
   strcat-vector \
+  strcmp \
+  strcmp-generic \
+  strcmp-vector \
   strcpy \
   strcpy-generic \
   strcpy-vector \
index 2501546665a548edca3b51ee017320ae94adc0f6..76f68c1650bc68c95f10a81e95f88c36c1234596 100644 (file)
@@ -70,5 +70,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
                              __strlen_vector)
              IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_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 (file)
index 0000000..9398ad4
--- /dev/null
@@ -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