From patchwork Tue Feb 3 15:05:33 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zihong Yao X-Patchwork-Id: 129492 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 179E84BA2E18 for ; Tue, 3 Feb 2026 15:17:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 179E84BA2E18 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp21.cstnet.cn [159.226.251.21]) by sourceware.org (Postfix) with ESMTPS id 7092B4BA2E0B for ; Tue, 3 Feb 2026 15:14:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7092B4BA2E0B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=isrc.iscas.ac.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=isrc.iscas.ac.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7092B4BA2E0B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.21 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770131700; cv=none; b=SgfMt4gTVVtJGjINQKGttkVCtKUUCJeKB/LbFhsIqirXQLhSAYb0LqhCFgARXmK1JrNhFZo7kEGLRItwee3kHpsEaWwv72QPr6QuTvkkbGEke44ESqsyF3Vf3BdcWyeYK+Pt39ZZL+4aU7GkMvuja8WNwcK3m7yyyof4tH/g5s8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770131700; c=relaxed/simple; bh=7S2Ez6KTEvvHmgM3QH4HjbXm9jXKks+aEb6dtvnwKns=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=n2Jwy1S7uioodmvqkkGaCcbFh1FrxIBXw0oLGjVWhcpnDRF+hpiD151KVQOuzYEwnzz27cZvXMi/cEsMZuVAvw4focy0oUzr5TD6hX1JS4ZJFo80tQHkcaeqwDNP+GzcW4EDqYtLY6CVt/KsOFUJZHAWvRJQL4AijIrQ45sqGF0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7092B4BA2E0B Received: from Mobilestation.localdomain (unknown [183.6.59.140]) by APP-01 (Coremail) with SMTP id qwCowADHbmriEIJpZ2EVBw--.24930S11; Tue, 03 Feb 2026 23:14:52 +0800 (CST) From: Yao Zihong To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, andrew@sifive.com, schwab@linux-m68k.org, bergner@tenstorrent.com, jlaw@ventanamicro.com, zhangyin2018@iscas.ac.cn, enh@google.com, zihongyao@outlook.com, Yao Zihong , Hau Hsu , Jerry Shih Subject: [PATCH v5 09/18] riscv: Add RVV strcat for multiarch and non-multiarch Date: Tue, 3 Feb 2026 23:05:33 +0800 Message-ID: <20260203151406.27450-10-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260203151406.27450-1-zihong.plct@isrc.iscas.ac.cn> References: <20260203151406.27450-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: qwCowADHbmriEIJpZ2EVBw--.24930S11 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fArW5tr4rWr47CFg_yoWfCw45pF Z5u3W7WF4kJr4xG3yI9F4Yq3W5GrZ5J3Z8KryY9w4UJw4DJrWxXFsF9w1rWFyxGFWrurW8 ZFn8uFyDuF18ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4U MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VUbPC7UUUUUU== X-Originating-IP: [183.6.59.140] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Co-authored-by: Hau Hsu Co-authored-by: Jerry Shih Signed-off-by: Yao Zihong Reviewed-by: Peter Bergner --- sysdeps/riscv/multiarch/strcat-generic.c | 26 +++++++ sysdeps/riscv/multiarch/strcat-vector.S | 24 ++++++ sysdeps/riscv/rvv/strcat.S | 75 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strcat.c | 56 ++++++++++++++ 6 files changed, 189 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strcat-generic.c create mode 100644 sysdeps/riscv/multiarch/strcat-vector.S create mode 100644 sysdeps/riscv/rvv/strcat.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcat.c diff --git a/sysdeps/riscv/multiarch/strcat-generic.c b/sysdeps/riscv/multiarch/strcat-generic.c new file mode 100644 index 0000000000..e28f8f012c --- /dev/null +++ b/sysdeps/riscv/multiarch/strcat-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default strcat 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 + . */ + +#include + +#if IS_IN(libc) +# define STRCAT __strcat_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +# include +#endif diff --git a/sysdeps/riscv/multiarch/strcat-vector.S b/sysdeps/riscv/multiarch/strcat-vector.S new file mode 100644 index 0000000000..581f075fef --- /dev/null +++ b/sysdeps/riscv/multiarch/strcat-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based strcat 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 + . */ + +#if IS_IN(libc) +# define STRCAT __strcat_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/strcat.S b/sysdeps/riscv/rvv/strcat.S new file mode 100644 index 0000000000..af08ee561f --- /dev/null +++ b/sysdeps/riscv/rvv/strcat.S @@ -0,0 +1,75 @@ +/* RISC-V RVV based strcat. + 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 + . */ + +#include +#include + +#ifndef STRCAT +# define STRCAT strcat +#endif + +#define dst a0 +#define src a1 +#define dst_ptr a2 + +#define ivl a3 +#define cur_vl a4 +#define active_elem_pos a5 + +#define ELEM_LMUL_SETTING m1 +#define vmask1 v0 +#define vmask2 v1 +#define vstr1 v8 +#define vstr2 v16 + +ENTRY (STRCAT) +.option push +.option arch, +v + mv dst_ptr, dst + /* Perform `strlen(dst)`. */ +L(strlen_loop): + vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vstr1, (dst_ptr) + vmseq.vx vmask1, vstr1, zero + csrr cur_vl, vl + vfirst.m active_elem_pos, vmask1 + add dst_ptr, dst_ptr, cur_vl + bltz active_elem_pos, L(strlen_loop) + + sub dst_ptr, dst_ptr, cur_vl + add dst_ptr, dst_ptr, active_elem_pos + + /* Perform `strcpy(dst, src)`. */ +L(strcpy_loop): + vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vstr1, (src) + vmseq.vx vmask2, vstr1, zero + csrr cur_vl, vl + vfirst.m active_elem_pos, vmask2 + vmsif.m vmask1, vmask2 + add src, src, cur_vl + vse8.v vstr1, (dst_ptr), vmask1.t + add dst_ptr, dst_ptr, cur_vl + bltz active_elem_pos, L(strcpy_loop) + + ret +.option pop +END (STRCAT) +libc_hidden_builtin_def (strcat) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 38d96e8b11..17824370f7 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -28,6 +28,9 @@ sysdep_routines += \ stpncpy \ stpncpy-generic \ stpncpy-vector \ + strcat \ + strcat-generic \ + strcat-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 ed3176d1cc..113a0f9c8f 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -90,5 +90,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __stpncpy_vector) IFUNC_IMPL_ADD (array, i, stpncpy, 1, __stpncpy_generic)) + IFUNC_IMPL (i, name, strcat, + IFUNC_IMPL_ADD (array, i, strcat, rvv_enabled, + __strcat_vector) + IFUNC_IMPL_ADD (array, i, strcat, 1, __strcat_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strcat.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strcat.c new file mode 100644 index 0000000000..781b0d4cb1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strcat.c @@ -0,0 +1,56 @@ +/* Multiple versions of strcat. + 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 + . */ + +#if IS_IN (libc) +/* Redefine strcat so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strcat +# define strcat __redirect_strcat +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strcat) __libc_strcat; + +extern __typeof (__redirect_strcat) __strcat_generic attribute_hidden; +extern __typeof (__redirect_strcat) __strcat_vector attribute_hidden; + +static inline __typeof (__redirect_strcat) * +select_strcat_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 __strcat_vector; + return __strcat_generic; +} + +riscv_libc_ifunc (__libc_strcat, select_strcat_ifunc); + +# undef strcat +strong_alias (__libc_strcat, strcat); +# ifdef SHARED +__hidden_ver1 (strcat, __GI_strcat, __redirect_strcat) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strcat); +# endif +#else +# include +#endif