From patchwork Mon Jan 19 07:03:08 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128454 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 9A1434BA2E2F for ; Mon, 19 Jan 2026 07:04:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A1434BA2E2F X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id C5C494BA9010 for ; Mon, 19 Jan 2026 07:03:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C5C494BA9010 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 C5C494BA9010 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806237; cv=none; b=g69WZbk3mLtaifC7WRDZYh09F6IuBoYLWoNkZHTJzt+Dio8kU6id2PVR2a9Ku8xkIuUDFgC4xsGb9oxKmKzk6J0aeI6z5HjIbcn+I8eWsoDp+GvtyRahANN/JQziuzN28doNChdNfbuodWayppAD7mkV6eAlzzstLKE+NdyptLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806237; c=relaxed/simple; bh=E5DJZv7XgnnT1Y7wOvEACsYUUirFSkq+QtP9pUpKrlU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=cQTz9WYzVzCrCBsFMUTasFNWEQhGnGxTa2Bs7dL1UUqRIsGuwAFdJiOGADHktNlvvkTyCKiQYlj5pZhxPpQHPKHw97AeKJX9/Z6XfcTNjSfUz1ZelWs2OZb6v7WzsE2P8PbZFPP7RNNNJPIajKwtyOcjUYQ8hAW3VBxJlrIWts0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5C494BA9010 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S3; Mon, 19 Jan 2026 15:03:50 +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 Subject: [PATCH v4 01/18] riscv: Add RVV memccpy for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:08 +0800 Message-ID: <20260119070338.60322-2-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S3 X-Coremail-Antispam: 1UD129KBjvJXoW3Gw17CF1fArW5tr43CF45KFg_yoWfXF17pF Z5uF1UGF1kJr4xGrWSgw13X3WrAFn5Jrn8KryY9a1UJrWUJw4kWFsFvwn3KFykGrWFk3yx uFZ8uFyqkw4rAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmj14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1I6r4UM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUbloGJUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.1 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, URIBL_BLOCKED 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 Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/memccpy-generic.c | 26 +++++++ sysdeps/riscv/multiarch/memccpy-vector.S | 24 +++++++ sysdeps/riscv/rvv/memccpy.S | 67 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/memccpy.c | 58 ++++++++++++++++ 6 files changed, 183 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memccpy-generic.c create mode 100644 sysdeps/riscv/multiarch/memccpy-vector.S create mode 100644 sysdeps/riscv/rvv/memccpy.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memccpy.c diff --git a/sysdeps/riscv/multiarch/memccpy-generic.c b/sysdeps/riscv/multiarch/memccpy-generic.c new file mode 100644 index 0000000000..0831c47af8 --- /dev/null +++ b/sysdeps/riscv/multiarch/memccpy-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default memccpy 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 MEMCCPY __memccpy_generic +# undef weak_alias +# define weak_alias(x, x2) +#endif +#include diff --git a/sysdeps/riscv/multiarch/memccpy-vector.S b/sysdeps/riscv/multiarch/memccpy-vector.S new file mode 100644 index 0000000000..8ea121e444 --- /dev/null +++ b/sysdeps/riscv/multiarch/memccpy-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based memccpy 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 MEMCCPY __memccpy_vector +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/memccpy.S b/sysdeps/riscv/rvv/memccpy.S new file mode 100644 index 0000000000..5854d463c2 --- /dev/null +++ b/sysdeps/riscv/rvv/memccpy.S @@ -0,0 +1,67 @@ +/* RISC-V RVV based memccpy. + 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 MEMCCPY +# define MEMCCPY __memccpy +#endif + +#define dst a0 +#define src a1 +#define c_val a2 +#define num a3 + +#define ivl a4 +#define dst_ptr a5 +#define vfirst_idx a6 + +#define ELEM_LMUL_SETTING m8 +#define vdata v0 +#define vmask v8 + +ENTRY (MEMCCPY) +.option push +.option arch, +v + mv dst_ptr, dst +L(loop): + vsetvli zero, num, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vdata, (src) + + vmseq.vx vmask, vdata, c_val + vfirst.m vfirst_idx, vmask + csrr ivl, vl + bgez vfirst_idx, L(found) + vse8.v vdata, (dst_ptr) + + sub num, num, ivl + add src, src, ivl + add dst_ptr, dst_ptr, ivl + bnez num, L(loop) + mv a0, zero + ret +L(found): + addi vfirst_idx, vfirst_idx, 1 + vsetvli zero, vfirst_idx, e8, ELEM_LMUL_SETTING, ta, ma + vse8.v vdata, (dst_ptr) + add a0, dst_ptr, vfirst_idx + ret +.option pop +END (MEMCCPY) +weak_alias (MEMCCPY, memccpy) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 1d26966ded..0c1c2b4467 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -1,5 +1,8 @@ ifeq ($(subdir),string) sysdep_routines += \ + memccpy \ + memccpy-generic \ + memccpy-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 a3b5731411..cd9460f57e 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -53,5 +53,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __memset_vector) IFUNC_IMPL_ADD (array, i, memset, 1, __memset_generic)) + IFUNC_IMPL (i, name, memccpy, + IFUNC_IMPL_ADD (array, i, memccpy, rvv_enabled, + __memccpy_vector) + IFUNC_IMPL_ADD (array, i, memccpy, 1, __memccpy_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memccpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memccpy.c new file mode 100644 index 0000000000..89b49c6ba7 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memccpy.c @@ -0,0 +1,58 @@ +/* Multiple versions of memccpy. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2017-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 + . */ + +#if IS_IN (libc) +/* Redefine memccpy so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef memccpy +# define memccpy __redirect_memccpy +# include +# include +# include +# include +# include + +extern __typeof (__redirect_memccpy) __libc_memccpy; + +extern __typeof (__redirect_memccpy) __memccpy_generic attribute_hidden; +extern __typeof (__redirect_memccpy) __memccpy_vector attribute_hidden; + +static inline __typeof (__redirect_memccpy) * +select_memccpy_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 __memccpy_vector; + + return __memccpy_generic; +} + +riscv_libc_ifunc (__libc_memccpy, select_memccpy_ifunc); + +# undef memccpy +weak_alias (__libc_memccpy, memccpy); +# ifdef SHARED +__hidden_ver1 (memccpy, __GI_memccpy, __redirect_memccpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memccpy); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128459 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 7A6EB4BA901D for ; Mon, 19 Jan 2026 07:05:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7A6EB4BA901D X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 98D854BA9016 for ; Mon, 19 Jan 2026 07:03:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 98D854BA9016 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 98D854BA9016 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806239; cv=none; b=rQusNt13ouK5S93whWuqFOHS9x30ShIBjKKj0C6Ehlf4QAvaq2hIxLMZ9pPEF3NFocW0cBxNSWHGxqzJtPpzBaCkJSo3tNcN9Ct7heTDsOKDd9R0TI4RObwxmFJ9e6T6kJDFEpykFLO++QBRdRmZ6022ogvja5jvXi5y2lu1upk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806239; c=relaxed/simple; bh=qjhPru7x78wG6F8nACH2lQy3A+Ykluv3sT1MeZKRx/g=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=HzL+KZX6mVgXJV8UBSto7eDUBdvwF0q255YpmHg27WPhe9354qcCohA01uiI981jrVpOVsrupOU52j+5S65u+iijf/cf+bgpxBI/6i4zkl3qlCUxNtxlcYCXo60+pvv0l7du+Q7DQPEkqDD8UYjVh2ypEwc2qlVtDLXEWGPf+bw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 98D854BA9016 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S4; Mon, 19 Jan 2026 15:03:50 +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 v4 02/18] riscv: Add RVV memchr for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:09 +0800 Message-ID: <20260119070338.60322-3-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S4 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fAF1xtr1xtF47urg_yoWfXF47pF Z5u3WUGFWkGws7GrWSg3Wjq3W5AF95Jr15KryY9w4UJ3yUJrWxXFs2vw15GFZ3JrWF93y5 uFn8uFyDCF48AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmj14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUjpuWJUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.3 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/memchr-generic.c | 24 +++++++ sysdeps/riscv/multiarch/memchr-vector.S | 26 +++++++ sysdeps/riscv/rvv/memchr.S | 67 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/memchr.c | 57 ++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memchr-generic.c create mode 100644 sysdeps/riscv/multiarch/memchr-vector.S create mode 100644 sysdeps/riscv/rvv/memchr.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c diff --git a/sysdeps/riscv/multiarch/memchr-generic.c b/sysdeps/riscv/multiarch/memchr-generic.c new file mode 100644 index 0000000000..be189f5f53 --- /dev/null +++ b/sysdeps/riscv/multiarch/memchr-generic.c @@ -0,0 +1,24 @@ +/* Re-include the default memchr 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 MEMCHR __memchr_generic +#endif +#include diff --git a/sysdeps/riscv/multiarch/memchr-vector.S b/sysdeps/riscv/multiarch/memchr-vector.S new file mode 100644 index 0000000000..951f256245 --- /dev/null +++ b/sysdeps/riscv/multiarch/memchr-vector.S @@ -0,0 +1,26 @@ +/* Re-include the RISC-V RVV based memchr 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 MEMCHR __memchr_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/memchr.S b/sysdeps/riscv/rvv/memchr.S new file mode 100644 index 0000000000..609b2f94c1 --- /dev/null +++ b/sysdeps/riscv/rvv/memchr.S @@ -0,0 +1,67 @@ +/* RISC-V RVV based memchr. + 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 MEMCHR +# define MEMCHR __memchr +#endif + +#define result a0 + +#define src a0 +#define value a1 +#define num a2 + +#define ivl a3 +#define temp a4 + +#define ELEM_LMUL_SETTING m8 +#define vdata v0 +#define vmask v8 + +ENTRY (MEMCHR) +.option push +.option arch, +v +L(loop): + vsetvli zero, num, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vdata, (src) + /* Find the value inside the loaded data. */ + vmseq.vx vmask, vdata, value + vfirst.m temp, vmask + + /* Skip the loop if we find the matched value. */ + bgez temp, L(found) + + csrr ivl, vl + sub num, num, ivl + add src, src, ivl + + bnez num, L(loop) + + li result, 0 + ret +L(found): + add result, src, temp + ret +.option pop +END (MEMCHR) +weak_alias (MEMCHR, memchr) +libc_hidden_builtin_def (memchr) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 0c1c2b4467..4ad91c0744 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -3,6 +3,9 @@ sysdep_routines += \ memccpy \ memccpy-generic \ memccpy-vector \ + memchr \ + memchr-generic \ + memchr-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 cd9460f57e..65a4b664b2 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -58,5 +58,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __memccpy_vector) IFUNC_IMPL_ADD (array, i, memccpy, 1, __memccpy_generic)) + IFUNC_IMPL (i, name, memchr, + IFUNC_IMPL_ADD (array, i, memchr, rvv_enabled, + __memchr_vector) + IFUNC_IMPL_ADD (array, i, memchr, 1, __memchr_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c new file mode 100644 index 0000000000..0844a33760 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memchr.c @@ -0,0 +1,57 @@ +/* Multiple versions of memchr. + 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 memchr so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef memchr +# define memchr __redirect_memchr +# include +# include +# include +# include +# include + +extern __typeof (__redirect_memchr) __libc_memchr; + +extern __typeof (__redirect_memchr) __memchr_generic attribute_hidden; +extern __typeof (__redirect_memchr) __memchr_vector attribute_hidden; + +static inline __typeof (__redirect_memchr) * +select_memchr_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 __memchr_vector; + return __memchr_generic; +} + +riscv_libc_ifunc (__libc_memchr, select_memchr_ifunc); + +# undef memchr +strong_alias (__libc_memchr, memchr); +strong_alias (memchr, __memchr) +# ifdef SHARED +__hidden_ver1 (memchr, __GI_memchr, __redirect_memchr) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memchr); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:10 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128455 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 3D1034BA902C for ; Mon, 19 Jan 2026 07:04:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D1034BA902C X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 3F2814BA9014 for ; Mon, 19 Jan 2026 07:03:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F2814BA9014 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 3F2814BA9014 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806241; cv=none; b=ClyMSj2cUDDvMdKeE77muedAOYQtsF0wQOEwAXcP6cU15Ql/o3YTOn7ZTr5inQbCPeNE9Q2uFzTROdk+AyWvQFYYXulCCCcaJn6Vj+C73VT+5EAOdXKRsdfmqm5IENEFJStpB34AlLdY3frRudb0hhTkd+S6zPhOqtr2D3ohR/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806241; c=relaxed/simple; bh=ySYJ1MTTGO+M6dxLnVv3C3XJrJbrdBPS7RAF55rXpao=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=aRt+mnVyyAM9u4ohZI1FkaSCJyN51zCEuxrAwti4NoSfAvSVQ0mOStfVqTSmtk8iPDzqecqNjawJNtN6BWJcg0tfavKBqFx/8huJAKkKrYFGZ7o0yY2Ld3zKGdjDJK0cNN09+HHCAm/c+P89HF8Ad/oVur/ZbWooO7dAR74O2D8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F2814BA9014 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S5; Mon, 19 Jan 2026 15:03:51 +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 v4 03/18] riscv: Add RVV memcmp for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:10 +0800 Message-ID: <20260119070338.60322-4-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S5 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fArW8Ar1DXF4Utwb_yoWfuFWDpF Z5u3WUGF4kG3yfGrWSk3WYg3W5ArZ5Jrn0kr1Y9w4UJrWUJ3yfWa9Fyas5GF93JrWFk3y5 uFn8ua4j9F4kAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmY14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UM2 8EF7xvwVC2z280aVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j6r4UJwAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjfUF3kuDUUUU X-Originating-IP: [183.6.59.244] 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/memcmp-generic.c | 31 +++++++ sysdeps/riscv/multiarch/memcmp-vector.S | 30 +++++++ sysdeps/riscv/rvv/memcmp.S | 84 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/memcmp.c | 60 +++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memcmp-generic.c create mode 100644 sysdeps/riscv/multiarch/memcmp-vector.S create mode 100644 sysdeps/riscv/rvv/memcmp.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memcmp.c diff --git a/sysdeps/riscv/multiarch/memcmp-generic.c b/sysdeps/riscv/multiarch/memcmp-generic.c new file mode 100644 index 0000000000..6a5483e566 --- /dev/null +++ b/sysdeps/riscv/multiarch/memcmp-generic.c @@ -0,0 +1,31 @@ +/* Re-include the default memcmp 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 MEMCMP __memcmp_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 diff --git a/sysdeps/riscv/multiarch/memcmp-vector.S b/sysdeps/riscv/multiarch/memcmp-vector.S new file mode 100644 index 0000000000..b447e945a8 --- /dev/null +++ b/sysdeps/riscv/multiarch/memcmp-vector.S @@ -0,0 +1,30 @@ +/* Re-include the RISC-V RVV based memcmp 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 MEMCMP __memcmp_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 +#endif diff --git a/sysdeps/riscv/rvv/memcmp.S b/sysdeps/riscv/rvv/memcmp.S new file mode 100644 index 0000000000..004bf538f0 --- /dev/null +++ b/sysdeps/riscv/rvv/memcmp.S @@ -0,0 +1,84 @@ +/* RISC-V RVV based memcmp. + 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 MEMCMP +# define MEMCMP memcmp +#endif + +#define result a0 + +#define src1 a0 +#define src2 a1 +#define num a2 + +#define ivl a3 +#define temp a4 + +#define ELEM_LMUL_SETTING m8 +#define vdata1 v0 +#define vdata2 v8 +#define vmask v16 +#define vtemp1 v16 +#define vtemp2 v24 + +ENTRY (MEMCMP) +.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 + + bgez temp, L(found) + + add src1, src1, ivl + add src2, src2, ivl + + bnez num, L(loop) + + li result, 0 + ret +L(found): + vrgather.vx vtemp1, vdata1, temp + vrgather.vx vtemp2, vdata2, temp + vmv.x.s result, vtemp1 + vmv.x.s temp, vtemp2 + andi result, result, 0xff + andi temp, temp, 0xff + sub result, result, temp + ret +.option pop +END (MEMCMP) +libc_hidden_builtin_def(memcmp) +#ifdef weak_alias +#undef bcmp +weak_alias (MEMCMP, bcmp) +#endif + +#undef __memcmpeq +strong_alias (MEMCMP, __memcmpeq) +libc_hidden_builtin_def (MEMCMP) +libc_hidden_def (__memcmpeq) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 4ad91c0744..0edc677874 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -6,6 +6,9 @@ sysdep_routines += \ memchr \ memchr-generic \ memchr-vector \ + memcmp \ + memcmp-generic \ + memcmp-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 65a4b664b2..fe45e043e0 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -63,5 +63,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __memchr_vector) IFUNC_IMPL_ADD (array, i, memchr, 1, __memchr_generic)) + IFUNC_IMPL (i, name, memcmp, + IFUNC_IMPL_ADD (array, i, memcmp, rvv_enabled, + __memcmp_vector) + IFUNC_IMPL_ADD (array, i, memcmp, 1, __memcmp_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memcmp.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memcmp.c new file mode 100644 index 0000000000..20e7b046ab --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memcmp.c @@ -0,0 +1,60 @@ +/* Multiple versions of memcmp. + 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 memcmp so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef memcmp +# define memcmp __redirect_memcmp +# include +# include +# include +# include +# include + +extern __typeof (__redirect_memcmp) __libc_memcmp; + +extern __typeof (__redirect_memcmp) __memcmp_generic attribute_hidden; +extern __typeof (__redirect_memcmp) __memcmp_vector attribute_hidden; + +static inline __typeof (__redirect_memcmp) * +select_memcmp_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 __memcmp_vector; + + return __memcmp_generic; +} + +riscv_libc_ifunc (__libc_memcmp, select_memcmp_ifunc); + +# undef memcmp +# undef bcmp +strong_alias (__libc_memcmp, memcmp); +weak_alias (memcmp, bcmp); +# ifdef SHARED +__hidden_ver1 (memcmp, __GI_memcmp, __redirect_memcmp) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memcmp); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:11 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128461 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 1F0EA4BA903A for ; Mon, 19 Jan 2026 07:05:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F0EA4BA903A X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 5AC894BA901F for ; Mon, 19 Jan 2026 07:04:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AC894BA901F 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 5AC894BA901F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; cv=none; b=AknzH5DxA8AKap5IuzrTvNvpsAXckJwg2L7HSRc8Coy8bBMTBDpauB6PEP5ZvNced87CAddvmw1GnbtauJxRtbpUrByGWx2EEfCN6e5fI6pgDzgLa1c4VxkqzFC3vyP2eJE3QO1mvp6qxqusheOseebHWMhs6M2sB8yumG3Gvn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; c=relaxed/simple; bh=6/op1tqF9HJRIv2rLIa24tzMyhxVfDsE+yBFiyF13Vw=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=aAewcH6OhpCV1uvcVVDpNUEZivSpjbP/HdrQDdiOKFq0oRjUnfqtx8ZqPzL0IFXesyF9m7V9MjpyKRrQlb6yUMOnnBEsyqFSbp7xv2Uh/ItbuflNvIi6ZRxtY0cYtSy7ZoFj7QKhWNJ6Oc8eJvUHjdOcDZ6+CUtuaO/NWMrnSio= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AC894BA901F Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S6; Mon, 19 Jan 2026 15:03: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 , Yun Hsiang Subject: [PATCH v4 04/18] riscv: Add RVV memcmpeq for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:11 +0800 Message-ID: <20260119070338.60322-5-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S6 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17Jr15KF4DurykZrWDArb_yoWfKw48pF Z5uF15CF1kJrWfGrWSk3Wjg3W5AF95Gr15tr1Y9w4UJ3yUJrs3XFsFy3Z5GFyDJrWFkayr uFn8WFyjka1xAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr 1UM28EF7xvwVC2z280aVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j6r4U JwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjfUriihUUUUU X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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: Yun Hsiang Signed-off-by: Yao Zihong --- 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..6f539c287e --- /dev/null +++ b/sysdeps/riscv/multiarch/memcmpeq-generic.c @@ -0,0 +1,31 @@ +/* Re-include the default memcmpeq 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 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 diff --git a/sysdeps/riscv/multiarch/memcmpeq-vector.S b/sysdeps/riscv/multiarch/memcmpeq-vector.S new file mode 100644 index 0000000000..8619c9c968 --- /dev/null +++ b/sysdeps/riscv/multiarch/memcmpeq-vector.S @@ -0,0 +1,30 @@ +/* Re-include the RISC-V RVV based memcmpeq 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 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 +#endif diff --git a/sysdeps/riscv/riscv-ifunc.h b/sysdeps/riscv/riscv-ifunc.h index 7782738576..59419f504d 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..622c60a8d4 --- /dev/null +++ b/sysdeps/riscv/rvv/memcmpeq.S @@ -0,0 +1,67 @@ +/* RISC-V RVV based memcmpeq. + 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 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 fe45e043e0..6285a3cf0f 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..e2dee7e459 --- /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) 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 __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 +# include +# undef __memcmpeq +# include +# include +# include + +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 From patchwork Mon Jan 19 07:03:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128456 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 7A90F4BBCDF0 for ; Mon, 19 Jan 2026 07:04:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7A90F4BBCDF0 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 522EC4BA901A for ; Mon, 19 Jan 2026 07:04:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 522EC4BA901A 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 522EC4BA901A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806243; cv=none; b=j+K7b3kZIPilIlik7XFIhNnk92kqXxdkcHGj+LUyi/5+Xp2ySLOHzXB7nSXHZnd5I6G35z1FGST+GxByvHX9N0opepkRuI+m72eKr0lwYBWs0DkLefLHZD7KnG7Xn712bwL6SqzufG/bm44DD6DGmUJeJA8/4GgL7rljdO1R6e8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806243; c=relaxed/simple; bh=b6v6X3XiUbss1Hu7Xvjvf3lOz+fwZ+THAiXjFfc035w=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=qrZdFMKoCHm93xrCdzT1CZuXmFiAIgtyKVRxkQgBHDC8QjIYVy/TQrU3s7Lnq82w3DmBeJINLN31ofU8TKjiOSogmG+SVlbD9YLYy1/CE8HxEDWAZ7TZm9oXUxAh5muaZaX4t+nZSdDUnU4Abor/w3bR0Mn5a4ciqXTwjdzMxGA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 522EC4BA901A Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S7; Mon, 19 Jan 2026 15:03: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 v4 05/18] riscv: Add RVV memcpy for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:12 +0800 Message-ID: <20260119070338.60322-6-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S7 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw4rtr13urWxtw1kKrg_yoWxGF4xpF s5uF1jkF1kJr1xGrWSgr10g3W5Zr1rGrn0k34Ykw4UJ3yUXr4xWa9Fywn3GFykJrWSkay8 uFn5uFyDuayrAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr 1UM28EF7xvwVC2z280aVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j6r4U JwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjfUriihUUUUU X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.5 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, URIBL_BLOCKED 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 --- .../riscv/multiarch/dl-symbol-redir-ifunc.h | 1 + sysdeps/riscv/multiarch/memcpy-vector.S | 24 +++++++++ sysdeps/riscv/rvv/memcpy.S | 54 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 1 + .../linux/riscv/multiarch/ifunc-impl-list.c | 2 + .../unix/sysv/linux/riscv/multiarch/memcpy.c | 5 ++ 6 files changed, 87 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memcpy-vector.S create mode 100644 sysdeps/riscv/rvv/memcpy.S diff --git a/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h index 24b78711e3..230623a068 100644 --- a/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/riscv/multiarch/dl-symbol-redir-ifunc.h @@ -20,6 +20,7 @@ #define _DL_IFUNC_GENERIC_H #ifndef SHARED +asm ("memcpy = __memcpy_generic"); asm ("memset = __memset_generic"); #endif diff --git a/sysdeps/riscv/multiarch/memcpy-vector.S b/sysdeps/riscv/multiarch/memcpy-vector.S new file mode 100644 index 0000000000..97b80b8069 --- /dev/null +++ b/sysdeps/riscv/multiarch/memcpy-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based memcpy 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 MEMCPY __memcpy_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/memcpy.S b/sysdeps/riscv/rvv/memcpy.S new file mode 100644 index 0000000000..b4d600c59d --- /dev/null +++ b/sysdeps/riscv/rvv/memcpy.S @@ -0,0 +1,54 @@ +/* RISC-V RVV based memcpy. + 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 MEMCPY +# define MEMCPY memcpy +#endif + +#define dst a0 +#define src a1 +#define num a2 + +#define ivl a3 +#define dst_ptr a4 + +#define ELEM_LMUL_SETTING m8 +#define vdata v0 + +ENTRY (MEMCPY) +.option push +.option arch, +v + mv dst_ptr, dst +L(loop): + vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma + + vle8.v vdata, (src) + sub num, num, ivl + add src, src, ivl + vse8.v vdata, (dst_ptr) + add dst_ptr, dst_ptr, ivl + + bnez num, L(loop) + + ret +.option pop +END (MEMCPY) +libc_hidden_builtin_def (memcpy) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 7cfb4f04dc..0bf7c8e37d 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -14,6 +14,7 @@ sysdep_routines += \ memcmpeq-vector \ memcpy \ memcpy-generic \ + memcpy-vector \ memcpy_noalignment \ memset \ memset-generic \ 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 6285a3cf0f..27a7817308 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -44,6 +44,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, } IFUNC_IMPL (i, name, memcpy, + IFUNC_IMPL_ADD (array, i, memcpy, rvv_enabled, + __memcpy_vector) IFUNC_IMPL_ADD (array, i, memcpy, fast_unaligned, __memcpy_noalignment) IFUNC_IMPL_ADD (array, i, memcpy, 1, __memcpy_generic)) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memcpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memcpy.c index f096693431..11919d815a 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/memcpy.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memcpy.c @@ -32,11 +32,16 @@ extern __typeof (__redirect_memcpy) __libc_memcpy; extern __typeof (__redirect_memcpy) __memcpy_generic attribute_hidden; extern __typeof (__redirect_memcpy) __memcpy_noalignment attribute_hidden; +extern __typeof (__redirect_memcpy) __memcpy_vector attribute_hidden; static inline __typeof (__redirect_memcpy) * select_memcpy_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 __memcpy_vector; + if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_CPUPERF_0, &v) == 0 && (v & RISCV_HWPROBE_MISALIGNED_MASK) == RISCV_HWPROBE_MISALIGNED_FAST) return __memcpy_noalignment; From patchwork Mon Jan 19 07:03:13 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128460 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 0A2654BA9023 for ; Mon, 19 Jan 2026 07:05:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A2654BA9023 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 39FC14BA901D for ; Mon, 19 Jan 2026 07:04:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 39FC14BA901D 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 39FC14BA901D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; cv=none; b=q10znAL37ar/azOtByab9XPWvurhXoyqkI5Td/bAtghkW6saTlGGPbm6kVtEX9y5NpgaInUnVMnC/1dAbsXgoXf/dkblE2BT8rwM61Yp81Ays4GfTrb/p+6Qz18Rhvj2j/fsIJ67c1MQChqT2KE1PCk3j1BQaDVI/xB1sOS0IiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; c=relaxed/simple; bh=qhBVPWGYnlzzMgjgtSnnNvNvfS8Dl7xTx5rgu13YXpg=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=NBhP+MXFHWIs5PTeoI4bQ4vQVGr/pCe3t1I3dks9ix+oz/2TzcalkDmWHZmFD+d4sMvHNDdbmwfxh3ifNrbwDvlANDw2tX5xgpjFllU8AGeIav38yNdzGlv+axXz9pbNAdErr3uOmcxlKBg6hrCPOpj5D7u0w5ovQ1kPGXTewVY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 39FC14BA901D Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S8; Mon, 19 Jan 2026 15:03:53 +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 v4 06/18] riscv: Add RVV memmove for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:13 +0800 Message-ID: <20260119070338.60322-7-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S8 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1kGr4rAr15tF13urg_yoWftr1DpF Z5uF13Grn7Jr1xGrWSgF1Yv3W3tF4rGF1YkryY9a1UA3yUXryxXFZFvwsYgF95JrW8C3yr Zan8uF98CF18ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr 1UM28EF7xvwVC2z280aVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j6r4U JwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjfUriihUUUUU X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/memmove-generic.c | 26 +++++++ sysdeps/riscv/multiarch/memmove-vector.S | 24 ++++++ sysdeps/riscv/rvv/memmove.S | 74 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/memmove.c | 56 ++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memmove-generic.c create mode 100644 sysdeps/riscv/multiarch/memmove-vector.S create mode 100644 sysdeps/riscv/rvv/memmove.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c diff --git a/sysdeps/riscv/multiarch/memmove-generic.c b/sysdeps/riscv/multiarch/memmove-generic.c new file mode 100644 index 0000000000..2eb6cae613 --- /dev/null +++ b/sysdeps/riscv/multiarch/memmove-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default memmove 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 MEMMOVE __memmove_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +#endif +#include diff --git a/sysdeps/riscv/multiarch/memmove-vector.S b/sysdeps/riscv/multiarch/memmove-vector.S new file mode 100644 index 0000000000..6bf2d9f614 --- /dev/null +++ b/sysdeps/riscv/multiarch/memmove-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based memmove 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 MEMMOVE __memmove_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/memmove.S b/sysdeps/riscv/rvv/memmove.S new file mode 100644 index 0000000000..5e41b13ec5 --- /dev/null +++ b/sysdeps/riscv/rvv/memmove.S @@ -0,0 +1,74 @@ +/* RISC-V RVV based memmove. + 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 MEMMOVE +# define MEMMOVE memmove +#endif + +#define dst a0 +#define src a1 +#define num a2 + +#define ivl a3 +#define dst_ptr a4 +#define src_backward_ptr a5 +#define dst_backward_ptr a6 + +#define ELEM_LMUL_SETTING m8 +#define vdata v0 + +ENTRY (MEMMOVE) +.option push +.option arch, +v + mv dst_ptr, dst + + /* If src is equal or after dst, all data in src will be loaded before + overwrited for the overlapping case. We could use faster `forward-copy`. */ + bgeu src, dst, L(forward_copy_loop) + add src_backward_ptr, src, num + add dst_backward_ptr, dst, num + /* If dst inside source data range, we need to use `backward_copy_loop` to + handle the overlapping issue. */ + bltu dst, src_backward_ptr, L(backward_copy_loop) +L(forward_copy_loop): + vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma + + vle8.v vdata, (src) + sub num, num, ivl + add src, src, ivl + vse8.v vdata, (dst_ptr) + add dst_ptr, dst_ptr, ivl + + bnez num, L(forward_copy_loop) + ret +L(backward_copy_loop): + vsetvli ivl, num, e8, ELEM_LMUL_SETTING, ta, ma + + sub src_backward_ptr, src_backward_ptr, ivl + vle8.v vdata, (src_backward_ptr) + sub num, num, ivl + sub dst_backward_ptr, dst_backward_ptr, ivl + vse8.v vdata, (dst_backward_ptr) + bnez num, L(backward_copy_loop) + ret +.option pop +END (MEMMOVE) +libc_hidden_builtin_def (memmove) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 0bf7c8e37d..a0990c9ff1 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -16,6 +16,9 @@ sysdep_routines += \ memcpy-generic \ memcpy-vector \ memcpy_noalignment \ + memmove \ + memmove-generic \ + memmove-vector \ memset \ memset-generic \ memset-vector \ 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 27a7817308..d955f56dba 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -75,5 +75,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, ____memcmpeq_vector) IFUNC_IMPL_ADD (array, i, __memcmpeq, 1, ____memcmpeq_generic)) + IFUNC_IMPL (i, name, memmove, + IFUNC_IMPL_ADD (array, i, memmove, rvv_enabled, + __memmove_vector) + IFUNC_IMPL_ADD (array, i, memmove, 1, __memmove_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c new file mode 100644 index 0000000000..670f108029 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memmove.c @@ -0,0 +1,56 @@ +/* Multiple versions of memmove. + 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 memmove so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef memmove +# define memmove __redirect_memmove +# include +# include +# include +# include +# include + +extern __typeof (__redirect_memmove) __libc_memmove; + +extern __typeof (__redirect_memmove) __memmove_generic attribute_hidden; +extern __typeof (__redirect_memmove) __memmove_vector attribute_hidden; + +static inline __typeof (__redirect_memmove) * +select_memmove_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 __memmove_vector; + return __memmove_generic; +} + +riscv_libc_ifunc (__libc_memmove, select_memmove_ifunc); + +# undef memmove +strong_alias (__libc_memmove, memmove); +# ifdef SHARED +__hidden_ver1 (memmove, __GI_memmove, __redirect_memmove) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memmove); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128458 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 8FCF54BBCDFC for ; Mon, 19 Jan 2026 07:04:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8FCF54BBCDFC X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id C90AB4BA9012 for ; Mon, 19 Jan 2026 07:04:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C90AB4BA9012 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 C90AB4BA9012 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806242; cv=none; b=DaQIZHnkBKDDD8NiRfssQqGXIiruVbTWjx4xGoHq/aziqpGdSEXnSeSyywg3oqZYXH6tDc7O4y8I/QqCwfixYBlkbAKhjwFM0qGVFQYGurQmieQOfHzoWllx3yvw//LicFz8Sq7GZnk/8d4EqkjIt/LbmWb1N/c3CTNGRTAWD1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806242; c=relaxed/simple; bh=g792dM/25eWku8uZXSlZtUtkKnwUWRFhQbF3ZshOpkU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=YoDtDjoWjMm9bIqbEr0Nmfcc4skIP8lqtJn/H12kFHg3WrVXxELdCXmaIgnWS1Xk/NTpJ7Qc0h0fsYp39WloCSR+9AJ/xaLpe+p8sRGdEmNkoxYnBMNz9j1UENnS+NeTJ+g2HG2qN6qAa6lKL+R6ifK+1KTAfJdMlPM0OIm2tBU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C90AB4BA9012 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S9; Mon, 19 Jan 2026 15:03:54 +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 , daichengrong Subject: [PATCH v4 07/18] riscv: Add RVV memrchr for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:14 +0800 Message-ID: <20260119070338.60322-8-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S9 X-Coremail-Antispam: 1UD129KBjvJXoWxKw1fuF4Dur17ZF4UXw1rCrg_yoWfWr4fpF Z5uF15GFn3Cr1xWrWSgF1jv3W5tr95Gr1Yg34Y93yUJrWUJ397WFsFvw1rWFZ7ArWrC3y5 u3WDuFyqkFW8AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4U MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] 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_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_BLOCKED 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: daichengrong Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/memrchr-generic.c | 28 ++++++++ sysdeps/riscv/multiarch/memrchr-vector.S | 28 ++++++++ sysdeps/riscv/rvv/memrchr.S | 71 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/memrchr.c | 50 +++++++++++++ 6 files changed, 185 insertions(+) create mode 100644 sysdeps/riscv/multiarch/memrchr-generic.c create mode 100644 sysdeps/riscv/multiarch/memrchr-vector.S create mode 100644 sysdeps/riscv/rvv/memrchr.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c diff --git a/sysdeps/riscv/multiarch/memrchr-generic.c b/sysdeps/riscv/multiarch/memrchr-generic.c new file mode 100644 index 0000000000..2776e37adc --- /dev/null +++ b/sysdeps/riscv/multiarch/memrchr-generic.c @@ -0,0 +1,28 @@ +/* Re-include the default memrchr 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 MEMRCHR __memrchr_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# undef weak_alias +# define weak_alias(a, b) +#endif +#include diff --git a/sysdeps/riscv/multiarch/memrchr-vector.S b/sysdeps/riscv/multiarch/memrchr-vector.S new file mode 100644 index 0000000000..1554c066aa --- /dev/null +++ b/sysdeps/riscv/multiarch/memrchr-vector.S @@ -0,0 +1,28 @@ +/* Re-include the RISC-V RVV based memrchr 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 MEMRCHR __memrchr_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) +# include +#endif diff --git a/sysdeps/riscv/rvv/memrchr.S b/sysdeps/riscv/rvv/memrchr.S new file mode 100644 index 0000000000..00df77e5d7 --- /dev/null +++ b/sysdeps/riscv/rvv/memrchr.S @@ -0,0 +1,71 @@ +/* RISC-V RVV based memrchr. + 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 MEMRCHR +# define MEMRCHR __memrchr +#endif + +#define src a0 +#define ch a1 +#define num a2 +#define vl a3 +#define first_idx a4 +#define idx a5 + +#define vmask v0 +#define vdata v8 +#define vidx v16 +#define vmax v24 + +ENTRY (MEMRCHR) +.option push +.option arch, +v + add src, src, num +L(loop): + beqz num, L(nohit) + + vsetvli vl, num, e8, m4, ta, ma + sub src, src, vl + vle8.v vdata, (src) + sub num, num, vl + + vmseq.vx vmask, vdata, ch + vfirst.m first_idx, vmask + bltz first_idx, L(loop) +L(hit): + vsetvli zero, vl, e16, m8, ta, ma + vid.v vidx + + /* reduce max index over hit lanes */ + vmv.s.x vmax, x0 + vredmaxu.vs vmax, vidx, vmax, vmask.t + vmv.x.s idx, vmax + + add a0, src, idx + ret +L(nohit): + li a0, 0 + ret +.option pop +END (MEMRCHR) +libc_hidden_def (MEMRCHR) +weak_alias (MEMRCHR, memrchr) +libc_hidden_builtin_def (memrchr) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index a0990c9ff1..190e853e6e 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -19,6 +19,9 @@ sysdep_routines += \ memmove \ memmove-generic \ memmove-vector \ + memrchr \ + memrchr-generic \ + memrchr-vector \ memset \ memset-generic \ memset-vector \ 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 d955f56dba..53e76705df 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -80,5 +80,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __memmove_vector) IFUNC_IMPL_ADD (array, i, memmove, 1, __memmove_generic)) + IFUNC_IMPL (i, name, memrchr, + IFUNC_IMPL_ADD (array, i, memrchr, rvv_enabled, + __memrchr_vector) + IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c new file mode 100644 index 0000000000..310b8e14f0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/memrchr.c @@ -0,0 +1,50 @@ +/* Multiple versions of memrchr. + 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 memrchr so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# define memrchr __redirect_memrchr +# include +# include +# undef memrchr +# include +# include +# include + +extern __typeof (__redirect_memrchr) __memrchr_generic attribute_hidden; +extern __typeof (__redirect_memrchr) __memrchr_vector attribute_hidden; + +static inline __typeof (__redirect_memrchr) * +select_memrchr_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 __memrchr_vector; + return __memrchr_generic; +} + +riscv_libc_ifunc_redirected (__redirect_memrchr, __memrchr, select_memrchr_ifunc); +libc_hidden_def (__memrchr) +weak_alias (__memrchr, memrchr) +#else +# include +#endif From patchwork Mon Jan 19 07:03:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128457 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 6D12C4BA903B for ; Mon, 19 Jan 2026 07:04:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D12C4BA903B X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 4C4284BA9011 for ; Mon, 19 Jan 2026 07:04:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C4284BA9011 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 4C4284BA9011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806243; cv=none; b=wSJmz4oHuD29JIPy31wPHxNdFvaLnuK+vsAaPY2/qMTl8fjNTdifXJtiXwsSr+y2Za3FovKHsmNIJFrtFt+NZ//8hxiz2BUw0mnqBOaBvhwcRCyF/qA8mitDsKpwBrD/gIlYx5rCDZDKGT6aoMsKzCNT9mK2UWW704qGlYuCXlI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806243; c=relaxed/simple; bh=bJMsZW38yds6HCL5tU728PrlRO8Zg+kH/BPPeS1RKhk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Uxx44gestcr39HKHeWar3IgKy0Dj/2td5ek6EqdExk4jInG+7pZQwGY8mOt8Nqb22qg0oxrSmi3tCdKzzGqD5SS4dcFXWEvfQgPROxAvDhoI/62UXgMg3Nm+/A1bAoy1llBXgqPaL8/z4sBMU0q75JVgaibub8RqgwaFoXdbKK4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C4284BA9011 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S10; Mon, 19 Jan 2026 15:03:54 +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 v4 08/18] riscv: Add RVV stpncpy for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:15 +0800 Message-ID: <20260119070338.60322-9-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S10 X-Coremail-Antispam: 1UD129KBjvJXoWfGw1ftr4DGrWrCF4xJr15twb_yoWDWw47pF s5CF17GFs7Jrs7GryxKF4Yg3W3JrWrJrn8Kr1Y9w4Utw4jqr1xGFsF9rsaga4xJrWru3yU uF4DWFWUuF1rA3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.5 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/stpncpy-generic.c | 28 ++++++ sysdeps/riscv/multiarch/stpncpy-vector.S | 28 ++++++ sysdeps/riscv/rvv/stpncpy.S | 97 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 + .../unix/sysv/linux/riscv/multiarch/stpncpy.c | 57 +++++++++++ 6 files changed, 218 insertions(+) create mode 100644 sysdeps/riscv/multiarch/stpncpy-generic.c create mode 100644 sysdeps/riscv/multiarch/stpncpy-vector.S create mode 100644 sysdeps/riscv/rvv/stpncpy.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c diff --git a/sysdeps/riscv/multiarch/stpncpy-generic.c b/sysdeps/riscv/multiarch/stpncpy-generic.c new file mode 100644 index 0000000000..b8030403fc --- /dev/null +++ b/sysdeps/riscv/multiarch/stpncpy-generic.c @@ -0,0 +1,28 @@ +/* Re-include the default stpncpy 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 STPNCPY __stpncpy_generic +# undef libc_hidden_def +# define libc_hidden_def(name) +# undef weak_alias +# define weak_alias(x, x2) +#endif +#include diff --git a/sysdeps/riscv/multiarch/stpncpy-vector.S b/sysdeps/riscv/multiarch/stpncpy-vector.S new file mode 100644 index 0000000000..e84d28a1e4 --- /dev/null +++ b/sysdeps/riscv/multiarch/stpncpy-vector.S @@ -0,0 +1,28 @@ +/* Re-include the RISC-V RVV based stpncpy 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 STPNCPY __stpncpy_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) +# include +#endif diff --git a/sysdeps/riscv/rvv/stpncpy.S b/sysdeps/riscv/rvv/stpncpy.S new file mode 100644 index 0000000000..57406685b0 --- /dev/null +++ b/sysdeps/riscv/rvv/stpncpy.S @@ -0,0 +1,97 @@ +/* RISC-V RVV based stpncpy. + 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 STPNCPY +# ifdef weak_alias +# define STPNCPY __stpncpy +weak_alias (__stpncpy, stpncpy) +# else +# define STPNCPY stpncpy +# endif +#endif + +#define dst a0 +#define src a1 +#define length a2 +#define dst_ptr a3 +#define active_elem_pos a4 +#define cur_vl a5 +#define ivl a6 +#define temp a1 + +#define ELEM_LMUL_SETTING m1 +#define vmask1 v0 +#define vmask2 v1 +#define ZERO_FILL_ELEM_LMUL_SETTING m8 +#define vstr1 v8 +#define vstr2 v16 + +ENTRY (STPNCPY) +.option push +.option arch, +v + mv dst_ptr, dst + /* Copy src to dst_ptr. */ +L(stpcpy_loop): + vsetvli zero, length, 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 + sub length, length, cur_vl + vse8.v vstr1, (dst_ptr), vmask1.t + add dst_ptr, dst_ptr, cur_vl + bgez active_elem_pos, L(fill_zero) + bnez length, L(stpcpy_loop) + mv dst, dst_ptr + ret + + /* Fill the tail zero. */ +L(fill_zero): + /* We already copy the `\0` to dst. But we use `vfirst.m` to + get the `index` of `\0` position. We need to adjust `-1` + to get the correct remaining length for zero filling. */ + sub temp, cur_vl, active_elem_pos + addi temp, temp, -1 + sub dst, dst_ptr, cur_vl + add dst, dst, active_elem_pos + add length, length, temp + /* Have an earily return for `strlen(src) + 1 == count` case. */ + bnez length, L(do_fill_zero) + ret + +L(do_fill_zero): + sub dst_ptr, dst_ptr, temp + vsetvli zero, length, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma + vmv.v.x vstr2, zero +L(fill_zero_loop): + vsetvli ivl, length, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma + vse8.v vstr2, (dst_ptr) + sub length, length, ivl + add dst_ptr, dst_ptr, ivl + bnez length, L(fill_zero_loop) + ret +.option pop +END (STPNCPY) +#ifdef weak_alias +libc_hidden_def (__stpncpy) +#endif diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 190e853e6e..38d96e8b11 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -25,6 +25,9 @@ sysdep_routines += \ memset \ memset-generic \ memset-vector \ + stpncpy \ + stpncpy-generic \ + stpncpy-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 53e76705df..ed3176d1cc 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -85,5 +85,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __memrchr_vector) IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic)) + IFUNC_IMPL (i, name, stpncpy, + IFUNC_IMPL_ADD (array, i, stpncpy, rvv_enabled, + __stpncpy_vector) + IFUNC_IMPL_ADD (array, i, stpncpy, 1, __stpncpy_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c new file mode 100644 index 0000000000..e451391f1a --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c @@ -0,0 +1,57 @@ +/* Multiple versions of stpncpy. + 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 stpncpy so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef stpncpy +# define stpncpy __redirect_stpncpy +# include +# include +# include +# include +# include + +extern __typeof (__redirect_stpncpy) __libc_stpncpy; + +extern __typeof (__redirect_stpncpy) __stpncpy_generic attribute_hidden; +extern __typeof (__redirect_stpncpy) __stpncpy_vector attribute_hidden; + +static inline __typeof (__redirect_stpncpy) * +select_stpncpy_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 __stpncpy_vector; + return __stpncpy_generic; +} + +riscv_libc_ifunc (__libc_stpncpy, select_stpncpy_ifunc); + +# undef stpncpy +weak_alias (__libc_stpncpy, stpncpy); +libc_hidden_def (__stpncpy); +# ifdef SHARED +__hidden_ver1 (stpncpy, __GI_stpncpy, __redirect_stpncpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (stpncpy); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:16 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128465 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 9C0DE4BA900F for ; Mon, 19 Jan 2026 07:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9C0DE4BA900F X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 0DEAC4BA9019 for ; Mon, 19 Jan 2026 07:04:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0DEAC4BA9019 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 0DEAC4BA9019 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; cv=none; b=lsp/imoQDG9nEIOb8H1hXVD6/r+Q3mnJ/ampEq+us0DMh14bu9dRME0LgLpgj1hVg1VCFzMtv5evE53E4ccp2VALExafJlYzY+/S7CKLvJKsSwBNF6wFhCyK7vthB2BPCU7ia8oWhR+uN56fKf8KvlhMAWK0BixLEcYeBthmVOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; c=relaxed/simple; bh=mAyHV7ZU7iNcQk76imeTi970zCiezKbRZRzwuu5m9NM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=MYeQebP6eANZG9L17Ho1bc9PqQVacM3dkfq1WaDs1ZA1wnKPdOhRyrj9XsbztL3+KdPoZLIUS7FQWf+EP3FxzPiSzNDK9n27McHe7+GZToLpHa6gGj7wlZRftcRq14z5UfE6vUmCoaqUzWyXaBwak68nU3wVp4pmcc0NVEf5FyI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0DEAC4BA9019 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S11; Mon, 19 Jan 2026 15:03:55 +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 v4 09/18] riscv: Add RVV strcat for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:16 +0800 Message-ID: <20260119070338.60322-10-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S11 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fArW5Kr1xWFy8Krg_yoWfCw45pF Z5u3W7WF4kJr4xG3yI9F45X3W5GrZ5J3Z8KryY9w4UJw4DJrWxXFsF9w1rWFyxGFWrurW8 ZFn8uFyDuF18ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 --- 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..1e5baae4a9 --- /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) +#endif +#include 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 From patchwork Mon Jan 19 07:03:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128471 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 7B3754BA9007 for ; Mon, 19 Jan 2026 07:12:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B3754BA9007 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 355744BA2E06 for ; Mon, 19 Jan 2026 07:10:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 355744BA2E06 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 355744BA2E06 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806658; cv=none; b=KZP7V0i5yN/aMqfs5FhJFKWTg2mtnCOUr4RrXrJz0mV1+Rk1UAVmXhaBmmVuNEp9mdV9wPmoC1PCYi4yJRplMKKnbCdbN5wCArRfdpmN7UOj86exO9Qv+Bag99wEeIb9O/OU9MDAPrrKhu3yneE06qCIL66MrMM9/lEwHjl6doA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806658; c=relaxed/simple; bh=F7t9k6U3LQRajywoXFrGjIS3ObtH+t4aQXxYdFraLOM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=I5/uhhX6G8Snqd2zlM6zoJDztJQ1Tecyv+Phn8zPMqulSQ4pSfe6kAlczXqv0DTIzn9vjg+v0PEfoGrOkIBWo68+lngkxne/6z+i4OyQfu7KDhABdA8IJrH/C2YSRAa6yT7Do+zp4wpoW6e/ga/z4YGwNSWdEvPNoDOTUifMRcs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 355744BA2E06 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S12; Mon, 19 Jan 2026 15:03:56 +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 , Nick Knight Subject: [PATCH v4 10/18] riscv: Add RVV strchr for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:17 +0800 Message-ID: <20260119070338.60322-11-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S12 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17GF17uFyUGrWrCFy5twb_yoWfAw43pF Z5u3WUGF1rJr4fGrWIga10q3W5Jr95Jrn8KryY9w4UJF4DJrW8XFZFvwn5WFWxGrWrC3yU ZFs8uFyDuF1xAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.8 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, URIBL_BLOCKED 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: Nick Knight Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/strchr-generic.c | 24 +++++++ sysdeps/riscv/multiarch/strchr-vector.S | 26 ++++++++ sysdeps/riscv/rvv/strchr.S | 65 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strchr.c | 58 +++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strchr-generic.c create mode 100644 sysdeps/riscv/multiarch/strchr-vector.S create mode 100644 sysdeps/riscv/rvv/strchr.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strchr.c diff --git a/sysdeps/riscv/multiarch/strchr-generic.c b/sysdeps/riscv/multiarch/strchr-generic.c new file mode 100644 index 0000000000..f91b014152 --- /dev/null +++ b/sysdeps/riscv/multiarch/strchr-generic.c @@ -0,0 +1,24 @@ +/* Re-include the default strchr 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 STRCHR __strchr_generic +#endif +#include diff --git a/sysdeps/riscv/multiarch/strchr-vector.S b/sysdeps/riscv/multiarch/strchr-vector.S new file mode 100644 index 0000000000..c55a1f993f --- /dev/null +++ b/sysdeps/riscv/multiarch/strchr-vector.S @@ -0,0 +1,26 @@ +/* Re-include the RISC-V RVV based strchr 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 STRCHR __strchr_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/strchr.S b/sysdeps/riscv/rvv/strchr.S new file mode 100644 index 0000000000..2fa95c31ba --- /dev/null +++ b/sysdeps/riscv/rvv/strchr.S @@ -0,0 +1,65 @@ +/* RISC-V RVV based strchr. + 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 STRCHR +# define STRCHR strchr +#endif + +#define str a0 +#define ch a1 +#define end_offset a2 +#define ch_offset a3 +#define temp1 a4 +#define temp2 a5 +#define cur_vl a6 +#define ivl t0 + +#define ELEM_LMUL_SETTING m1 +#define vstr v0 +#define vmask_end v8 +#define vmask_ch v9 + +ENTRY (STRCHR) +.option push +.option arch, +v +L(strchr_loop): + vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vstr, (str) + vmseq.vi vmask_end, vstr, 0 + vmseq.vx vmask_ch, vstr, ch + vfirst.m end_offset, vmask_end /* first occurrence of \0 */ + vfirst.m ch_offset, vmask_ch /* first occurrence of ch */ + sltz temp1, ch_offset + sltu temp2, end_offset, ch_offset + or temp1, temp1, temp2 + beqz temp1, L(found_ch) /* Found ch, not preceded by \0? */ + csrr cur_vl, vl + add str, str, cur_vl + bltz end_offset, L(strchr_loop) /* Didn't find \0? */ + li str, 0 + ret +L(found_ch): + add str, str, ch_offset + ret +.option pop +END (STRCHR) +weak_alias (STRCHR, index) +libc_hidden_builtin_def (strchr) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 17824370f7..f3155f41c4 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -31,6 +31,9 @@ sysdep_routines += \ strcat \ strcat-generic \ strcat-vector \ + strchr \ + strchr-generic \ + strchr-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 113a0f9c8f..3d059a33e4 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -95,5 +95,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strcat_vector) IFUNC_IMPL_ADD (array, i, strcat, 1, __strcat_generic)) + IFUNC_IMPL (i, name, strchr, + IFUNC_IMPL_ADD (array, i, strchr, rvv_enabled, + __strchr_vector) + IFUNC_IMPL_ADD (array, i, strchr, 1, __strchr_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strchr.c new file mode 100644 index 0000000000..ef75a2b0a9 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strchr.c @@ -0,0 +1,58 @@ +/* Multiple versions of strchr. + 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 strchr so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strchr +# define strchr __redirect_strchr +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strchr) __libc_strchr; + +extern __typeof (__redirect_strchr) __strchr_generic attribute_hidden; +extern __typeof (__redirect_strchr) __strchr_vector attribute_hidden; + +static inline __typeof (__redirect_strchr) * +select_strchr_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 __strchr_vector; + return __strchr_generic; +} + +riscv_libc_ifunc (__libc_strchr, select_strchr_ifunc); + +# undef strchr +# undef index +strong_alias (__libc_strchr, strchr); +weak_alias (strchr, index); +# ifdef SHARED +__hidden_ver1 (strchr, __GI_strchr, __redirect_strchr) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strchr); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128462 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 59CFE4BA901D for ; Mon, 19 Jan 2026 07:05:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59CFE4BA901D X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 92D474BA9023 for ; Mon, 19 Jan 2026 07:04:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92D474BA9023 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 92D474BA9023 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; cv=none; b=rFPRSIftnffsXhdym9TqMorTBEoMh62vpALKate6/1Mu/vYVVVnh5Y6kcU1IKxZbnbiJFdGTnl4O7c1+0naTiH8azKB2Hr+XA6/VqbM1uK2bMfOuEamxjjjMpnHGLhJalDXDxUJy5SIj2BkgRUfMdXDLSRhx/P/x4K2dS2jYkRI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806244; c=relaxed/simple; bh=l9G6L7wa8lJVEjOP83avtjG2TvpSqLb5AlDjf374qm0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=FeUoVpzWhr3Fsjks63+bduoRHen1Upy3Dbt17l/ZQATCEl+8WnwaMterdeGSbzO7o0nnUAdBUwrgz8AGawJKKU7Zv+4cQt6yY7CDtEn4lGNOMJOrkxE9R9cskg4NdB/SmHHCjwBnt2pedQn+d0VcJ6E+Kx58JdnkSpnarcigjXk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 92D474BA9023 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S13; Mon, 19 Jan 2026 15:03:56 +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 v4 11/18] riscv: Add RVV strcmp for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:18 +0800 Message-ID: <20260119070338.60322-12-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S13 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw13KFyDtF17tF4ruFg_yoWfZF4xpF Z5u3WUGa1kJr4fGFWIgw10q3W5JFZ3tFn8KryY9w4UJr45XrWxXFZIv3s5GF97GrWfC3yU ZFs8uFyDCF1xAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 --- 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..1cb9e5a1ed --- /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 + . */ + +#include + +#if IS_IN(libc) +# define STRCMP __strcmp_generic +#endif +#include 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 + . */ + +#if IS_IN(libc) +# define STRCMP __strcmp_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#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 + . */ + +#include +#include + +#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=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 + . */ + +#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 +# include +# include +# include +# include + +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 +#endif From patchwork Mon Jan 19 07:03:19 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128467 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 273784BA9026 for ; Mon, 19 Jan 2026 07:07:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 273784BA9026 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 5A47F4BA902A for ; Mon, 19 Jan 2026 07:04:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A47F4BA902A 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 5A47F4BA902A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806245; cv=none; b=f35f026zKL+ykJT7m8CXdIT/OIlFOU++tnchxtIAfpkQWp9DI/iQRaL2/xiWSTbXeXi7m4IbMq+GA2eiX1WfkPs/ilNgEkq3d7QoSvKSvEAG4hMgXSQN1KZjcewsHwtQSlsPsSxkbCKGE9/U4QJcu9MkM8Gfv83owXH8+xft/Ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806245; c=relaxed/simple; bh=hx3QiFxIfTJC6pvuGO17OwU+Os4xtqODqj8pAGUs2v0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=E/YILr74C2/tDxQkAGpJcU1jeYcanpPbrJerUslIAU5rpRyOujwK7WiC/ZVOtH5s2us59P/mdFJzSJ4O1RupyshMqJlY2qCHu+o1EAXqwJnkwrqGzVTxsV1zSZRc9vWm4cJkQlNjjFAQ28HrFdCRTgwLoE+3ssVWA3CBZntLC5o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A47F4BA902A Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S14; Mon, 19 Jan 2026 15:03:57 +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 v4 12/18] riscv: Add RVV strcpy for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:19 +0800 Message-ID: <20260119070338.60322-13-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S14 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fAF1rJrWfXFyUtrb_yoWfXF4kpF Z5u3WUGa1kJr4fG3yIgF1Yq3W5JrZ5JFn8KrWY9w4UAr4qgrWxWFsFv3Z3GFyxGrWrC3y5 ZFnxuFyDC3Z3A3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/strcpy-generic.c | 26 ++++++++ sysdeps/riscv/multiarch/strcpy-vector.S | 24 ++++++++ sysdeps/riscv/rvv/strcpy.S | 59 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strcpy.c | 56 ++++++++++++++++++ 6 files changed, 173 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strcpy-generic.c create mode 100644 sysdeps/riscv/multiarch/strcpy-vector.S create mode 100644 sysdeps/riscv/rvv/strcpy.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c diff --git a/sysdeps/riscv/multiarch/strcpy-generic.c b/sysdeps/riscv/multiarch/strcpy-generic.c new file mode 100644 index 0000000000..9b3e073773 --- /dev/null +++ b/sysdeps/riscv/multiarch/strcpy-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default strcpy 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 STRCPY __strcpy_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +#endif +#include diff --git a/sysdeps/riscv/multiarch/strcpy-vector.S b/sysdeps/riscv/multiarch/strcpy-vector.S new file mode 100644 index 0000000000..f5381a98ce --- /dev/null +++ b/sysdeps/riscv/multiarch/strcpy-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based strcpy 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 STRCPY __strcpy_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/strcpy.S b/sysdeps/riscv/rvv/strcpy.S new file mode 100644 index 0000000000..e73558d812 --- /dev/null +++ b/sysdeps/riscv/rvv/strcpy.S @@ -0,0 +1,59 @@ +/* RISC-V RVV based strcpy. + 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 STRCPY +# define STRCPY strcpy +#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 (STRCPY) +.option push +.option arch, +v + mv dst_ptr, dst +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 (STRCPY) +libc_hidden_builtin_def (strcpy) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 3dfee5799c..5bbd789d46 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -37,6 +37,9 @@ sysdep_routines += \ strcmp \ strcmp-generic \ strcmp-vector \ + strcpy \ + strcpy-generic \ + strcpy-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 872e8d12a7..3f00942167 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -105,5 +105,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strcmp_vector) IFUNC_IMPL_ADD (array, i, strcmp, 1, __strcmp_generic)) + IFUNC_IMPL (i, name, strcpy, + IFUNC_IMPL_ADD (array, i, strcpy, rvv_enabled, + __strcpy_vector) + IFUNC_IMPL_ADD (array, i, strcpy, 1, __strcpy_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c new file mode 100644 index 0000000000..ba47e8f89c --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c @@ -0,0 +1,56 @@ +/* Multiple versions of strcpy. + 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 strcpy so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strcpy +# define strcpy __redirect_strcpy +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strcpy) __libc_strcpy; + +extern __typeof (__redirect_strcpy) __strcpy_generic attribute_hidden; +extern __typeof (__redirect_strcpy) __strcpy_vector attribute_hidden; + +static inline __typeof (__redirect_strcpy) * +select_strcpy_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 __strcpy_vector; + return __strcpy_generic; +} + +riscv_libc_ifunc (__libc_strcpy, select_strcpy_ifunc); + +# undef strcpy +strong_alias (__libc_strcpy, strcpy); +# ifdef SHARED +__hidden_ver1 (strcpy, __GI_strcpy, __redirect_strcpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strcpy); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:20 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128470 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 590874BA900B for ; Mon, 19 Jan 2026 07:11:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 590874BA900B X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 9A1784BA9007 for ; Mon, 19 Jan 2026 07:11:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A1784BA9007 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 9A1784BA9007 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806664; cv=none; b=epOYJeFmlJI5WQoHgU4RaPyTH9cnnGyVeP/RU1XUjogaym7JWy7lv3sTxfaiaXZC1cgtxmikgNm1zG1OvWQD7eFe56WWlQYR3faV3tuVXMEcrpHsuUL8Av66ApeYE/kquAZzw1pmdpSxViuLCYzPWSne6gnZh65JoATpp4h0LjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806664; c=relaxed/simple; bh=U2btjyfwioRkWKdbrxeLp8Luvne0xyXsqHufJTVX4xI=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=IWv3RzfRNHsG760a7F/Sqvu37GKoDnO/g85hXwIh/gwzYq6iDm4AN8YYdtfqEgM2sdJUbB/ubYUtLlblsdyUDt7Jld4fWg+rJ7R+yIS8rsScMUX89ORM2BRHoPumbb7rvSsuzMG3D9+Q0s04OMmIxVNsBylRbLpMA5/CkSnDB0M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A1784BA9007 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S15; Mon, 19 Jan 2026 15:03:58 +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 v4 13/18] riscv: Add RVV strlen for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:20 +0800 Message-ID: <20260119070338.60322-14-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S15 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fAF1kXw1DJFykGrg_yoWfXrW8pF s5u3WUGF1kJr4IkrySga45X3WrG395Jrn8KryY9w4UJw4DXrWxXFs2kwnYgFyxJrWfC3yr ZFnF9a4UCF1rZ3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/strlen-generic.c | 24 ++++++++ sysdeps/riscv/multiarch/strlen-vector.S | 26 +++++++++ sysdeps/riscv/rvv/strlen.S | 58 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strlen.c | 56 ++++++++++++++++++ 6 files changed, 172 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strlen-generic.c create mode 100644 sysdeps/riscv/multiarch/strlen-vector.S create mode 100644 sysdeps/riscv/rvv/strlen.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c diff --git a/sysdeps/riscv/multiarch/strlen-generic.c b/sysdeps/riscv/multiarch/strlen-generic.c new file mode 100644 index 0000000000..171f2bedd5 --- /dev/null +++ b/sysdeps/riscv/multiarch/strlen-generic.c @@ -0,0 +1,24 @@ +/* Re-include the default strlen 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 STRLEN __strlen_generic +#endif +#include diff --git a/sysdeps/riscv/multiarch/strlen-vector.S b/sysdeps/riscv/multiarch/strlen-vector.S new file mode 100644 index 0000000000..b7841ed295 --- /dev/null +++ b/sysdeps/riscv/multiarch/strlen-vector.S @@ -0,0 +1,26 @@ +/* Re-include the RISC-V RVV based strlen 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 STRLEN __strlen_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/strlen.S b/sysdeps/riscv/rvv/strlen.S new file mode 100644 index 0000000000..0a3b18eb0c --- /dev/null +++ b/sysdeps/riscv/rvv/strlen.S @@ -0,0 +1,58 @@ +/* RISC-V RVV based strlen. + 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 STRLEN +# define STRLEN __strlen +#endif + +#define result a0 +#define str a0 +#define copy_str a1 +#define ivl a2 +#define cur_vl a2 +#define end_offset a3 + +#define ELEM_LMUL_SETTING m2 +#define vstr v0 +#define vmask_end v2 + +ENTRY (STRLEN) +.option push +.option arch, +v + mv copy_str, str +L(loop): + vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vstr, (copy_str) + csrr cur_vl, vl + vmseq.vi vmask_end, vstr, 0 + vfirst.m end_offset, vmask_end + add copy_str, copy_str, cur_vl + bltz end_offset, L(loop) + + add str, str, cur_vl + add copy_str, copy_str, end_offset + sub result, copy_str, result + + ret +.option pop +END (STRLEN) +weak_alias (STRLEN, strlen) +libc_hidden_builtin_def (strlen) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 5bbd789d46..47e16078c1 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -40,6 +40,9 @@ sysdep_routines += \ strcpy \ strcpy-generic \ strcpy-vector \ + strlen \ + strlen-generic \ + strlen-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 3f00942167..fa5692a074 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -110,5 +110,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strcpy_vector) IFUNC_IMPL_ADD (array, i, strcpy, 1, __strcpy_generic)) + IFUNC_IMPL (i, name, strlen, + IFUNC_IMPL_ADD (array, i, strlen, rvv_enabled, + __strlen_vector) + IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c new file mode 100644 index 0000000000..9975286b85 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strlen.c @@ -0,0 +1,56 @@ +/* Multiple versions of strlen. + 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 strlen so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strlen +# define strlen __redirect_strlen +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strlen) __libc_strlen; + +extern __typeof (__redirect_strlen) __strlen_generic attribute_hidden; +extern __typeof (__redirect_strlen) __strlen_vector attribute_hidden; + +static inline __typeof (__redirect_strlen) * +select_strlen_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 __strlen_vector; + return __strlen_generic; +} + +riscv_libc_ifunc (__libc_strlen, select_strlen_ifunc); + +# undef strlen +strong_alias (__libc_strlen, strlen); +# ifdef SHARED +__hidden_ver1 (strlen, __GI_strlen, __redirect_strlen) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strlen); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:21 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128464 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 575C14BA9033 for ; Mon, 19 Jan 2026 07:06:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 575C14BA9033 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 06A784BA902B for ; Mon, 19 Jan 2026 07:04:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06A784BA902B 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 06A784BA902B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806247; cv=none; b=f0IiZqQUBfc4FlhwT/X3jcOWCucEe+mPonudckfWMsPJ7g7ILKLfyw/u+l5kTejibFGutBWTcrYXjgX+d1n+Qx+ssF2c76kJuhGUMkvsCWWgrd6S4kHOLK1zI16cfNaSYDSb2GMgDrqMZ2bBFO4BU/h/E71cUU/BD+eixdpp66o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806247; c=relaxed/simple; bh=E30/95NhFbiG9tjTlXedOxXmkUVp9RieC6tJeME7/XY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=FkELpFT3djRuVEtcrz/fUPljtNOvoDbTTee+EUNN7ZBO5u9zmh56IgmfOh2cdwN1e3VhrsJnPJmd2+A4TtIgPoS5EImkKTEx7IlexahW12NOw51CXIoQdUZW60fYGJH7f+tUGMti0dHYfbD2zxQc5ONo4apRoDw3DWDNWwKIAYo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06A784BA902B Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S16; Mon, 19 Jan 2026 15:03:59 +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 v4 14/18] riscv: Add RVV strncat for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:21 +0800 Message-ID: <20260119070338.60322-15-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S16 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fAryxXFW8tw1kKrg_yoWfuFykpF Z5ua1UGF4kJr1xGryI9F1jganxJFWfJrn8KryY9w45tw4qqrWxXFsFkwsYgF1xG3yrurWf ZFyq9FyDCF18AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/strncat-generic.c | 26 ++++++ sysdeps/riscv/multiarch/strncat-vector.S | 24 ++++++ sysdeps/riscv/rvv/strncat.S | 81 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strncat.c | 52 ++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strncat-generic.c create mode 100644 sysdeps/riscv/multiarch/strncat-vector.S create mode 100644 sysdeps/riscv/rvv/strncat.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c diff --git a/sysdeps/riscv/multiarch/strncat-generic.c b/sysdeps/riscv/multiarch/strncat-generic.c new file mode 100644 index 0000000000..66d7a17695 --- /dev/null +++ b/sysdeps/riscv/multiarch/strncat-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default strncat 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 STRNCAT __strncat_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +#endif +#include diff --git a/sysdeps/riscv/multiarch/strncat-vector.S b/sysdeps/riscv/multiarch/strncat-vector.S new file mode 100644 index 0000000000..c98820d7cd --- /dev/null +++ b/sysdeps/riscv/multiarch/strncat-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based strncat 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 STRNCAT __strncat_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/strncat.S b/sysdeps/riscv/rvv/strncat.S new file mode 100644 index 0000000000..12fe9aa8d8 --- /dev/null +++ b/sysdeps/riscv/rvv/strncat.S @@ -0,0 +1,81 @@ +/* RISC-V RVV based strncat. + 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 STRNCAT +# define STRNCAT strncat +#endif + +#define dst a0 +#define src a1 +#define length a2 +#define dst_ptr a3 +#define cur_vl a4 +#define activate_elem_pos a5 + +#define ELEM_LMUL_SETTING m1 +#define vmask1 v0 +#define vmask2 v1 +#define vstr1 v8 +#define vstr2 v16 + +ENTRY (STRNCAT) +.option push +.option arch, +v + mv dst_ptr, dst + /* the strlen of dst. */ +L(strlen_loop): + vsetvli cur_vl, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vstr1, (dst_ptr) + /* find the '\0'. */ + vmseq.vx vmask1, vstr1, zero + csrr cur_vl, vl + vfirst.m activate_elem_pos, vmask1 + add dst_ptr, dst_ptr, cur_vl + bltz activate_elem_pos, L(strlen_loop) + + sub dst_ptr, dst_ptr, cur_vl + add dst_ptr, dst_ptr, activate_elem_pos + + /* copy src to dst_ptr. */ +L(strcpy_loop): + vsetvli zero, length, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vstr1, (src) + vmseq.vx vmask2, vstr1, zero + csrr cur_vl, vl + vfirst.m activate_elem_pos, vmask2 + vmsif.m vmask1, vmask2 + add src, src, cur_vl + sub length, length, cur_vl + vse8.v vstr1, (dst_ptr), vmask1.t + add dst_ptr, dst_ptr, cur_vl + beqz length, L(fill_zero) + bltz activate_elem_pos, L(strcpy_loop) + ret +L(fill_zero): + bgez activate_elem_pos, L(fill_zero_end) + sb zero, (dst_ptr) +L(fill_zero_end): + ret +.option pop +END (STRNCAT) +libc_hidden_builtin_def (strncat) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 47e16078c1..1b568bd3de 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -43,6 +43,9 @@ sysdep_routines += \ strlen \ strlen-generic \ strlen-vector \ + strncat \ + strncat-generic \ + strncat-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 fa5692a074..370d59d4c5 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, __strlen_vector) IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_generic)) + IFUNC_IMPL (i, name, strncat, + IFUNC_IMPL_ADD (array, i, strncat, rvv_enabled, + __strncat_vector) + IFUNC_IMPL_ADD (array, i, strncat, 1, __strncat_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c new file mode 100644 index 0000000000..a7acfdfe54 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncat.c @@ -0,0 +1,52 @@ +/* Multiple versions of strncat. + 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 strncat so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# define strncat __redirect_strncat +# include +# include +# undef strncat +# include +# include +# include + +extern __typeof (__redirect_strncat) __strncat_generic attribute_hidden; +extern __typeof (__redirect_strncat) __strncat_vector attribute_hidden; + +static inline __typeof (__redirect_strncat) * +select_strncat_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 __strncat_vector; + return __strncat_generic; +} + +riscv_libc_ifunc_redirected (__redirect_strncat, strncat, select_strncat_ifunc); +strong_alias (strncat, __strncat); +# ifdef SHARED +__hidden_ver1 (strncat, __GI___strncat, __redirect_strncat) + __attribute__((visibility ("hidden"))) __attribute_copy__ (strncat); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:22 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128469 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 09BD04BA9018 for ; Mon, 19 Jan 2026 07:11:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09BD04BA9018 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 360FE4BA2E2F for ; Mon, 19 Jan 2026 07:10:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 360FE4BA2E2F 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 360FE4BA2E2F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806658; cv=none; b=T2uNclWiGC91Ra+ePVa1VnvfOdv6+AwEVr164SkqRp7LP6EwDOAkJGRjJ1brCdO46WUx904O3055HQif/mnVZwOo9ewKcgtWsVezFpe5BcgxHYYtw67obpou39ot8LIr/cmEbebE8gNJFTbF897bvK6LGnh4/Jyp3Ug25uxrLl0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806658; c=relaxed/simple; bh=j9oQVQ/O1SIBBDxF7LgRygg02014llEuO5/u+2gbm2o=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=BrHgpZswq+l+iYCnDO5da7wvjJq/zFBnKdsFfnn7HVDDuWh0EzHsW4IKScpClhM4p2rQQkIfQais5JxwgoZutgNg0Oid7BGwTTaTa9+mShEBubZDg0kk9T9mNSVW82PNHZot2I4M60EDrMsYvA1wSYwL8ncM27d8t081PL8g6i4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 360FE4BA2E2F Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S17; Mon, 19 Jan 2026 15:03:59 +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 v4 15/18] riscv: Add RVV strncmp for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:22 +0800 Message-ID: <20260119070338.60322-16-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S17 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fAF18tw18Cw1fXrb_yoWfZF4xpF Z5u3WUKF4kJr4xGryI93WY93W5JFWrtF15KryY9w45tr4jvrWxXFZ2yws3WFyxJrWfu3y3 ZF1DWF9rCF10y3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.7 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, URIBL_BLOCKED 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 --- sysdeps/riscv/multiarch/strncmp-generic.c | 26 ++++++ sysdeps/riscv/multiarch/strncmp-vector.S | 24 +++++ 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, 202 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..e79f8c7635 --- /dev/null +++ b/sysdeps/riscv/multiarch/strncmp-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default strncmp 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 STRNCMP __strncmp_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +#endif +#include diff --git a/sysdeps/riscv/multiarch/strncmp-vector.S b/sysdeps/riscv/multiarch/strncmp-vector.S new file mode 100644 index 0000000000..91e25ea826 --- /dev/null +++ b/sysdeps/riscv/multiarch/strncmp-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based strncmp 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 STRNCMP __strncmp_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# include +#endif diff --git a/sysdeps/riscv/rvv/strncmp.S b/sysdeps/riscv/rvv/strncmp.S new file mode 100644 index 0000000000..2e265fd25a --- /dev/null +++ b/sysdeps/riscv/rvv/strncmp.S @@ -0,0 +1,88 @@ +/* RISC-V RVV based strncmp. + 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 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 1b568bd3de..bb3488e849 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -46,6 +46,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 370d59d4c5..025b026cf3 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -120,5 +120,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..781e562629 --- /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) 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 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 +# include +# include +# include +# include + +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 +#endif From patchwork Mon Jan 19 07:03:23 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128466 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 E1E884BBCDEB for ; Mon, 19 Jan 2026 07:07:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1E884BBCDEB X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 6D0C74BA9035 for ; Mon, 19 Jan 2026 07:04:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D0C74BA9035 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 6D0C74BA9035 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806247; cv=none; b=XuEoiMDnNqJy/5C0r4S3ZSsYx28c6igyCN4ueX356OeQItl61i23kaOYBkciyHSc31cGHW0QZEMfJQ5YdJL3aSJcPRpP/9bunUJJDNx17D1Vyt2F9FX5DZrtEvpXeHe/Fh0TGsXKjSQgWtOReGuuGXU8CcxoK8BKDmUHe/8HPg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806247; c=relaxed/simple; bh=O9UJUND6AGqD3dB8O640kb4Gn6OBDCVGZRCAD4IyPzk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Tgvazv1X0Mp4RMq7G+A5NNUfGWNRCOPE83M3xcEnuanEKVU0Yu/GiXM8Lx5vIjq39zXtYZYq5cKLO/amQtIZm7lxhgHJ/MLVy4Zsd9ExRgeVU43T6VdwlHEazpaaTEudme6tuDMJaugcZXPcg5zXVh7bEFSNtN6splQ3CVoLbJc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D0C74BA9035 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S18; Mon, 19 Jan 2026 15:04:00 +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 Subject: [PATCH v4 16/18] riscv: Add RVV strncpy for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:23 +0800 Message-ID: <20260119070338.60322-17-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S18 X-Coremail-Antispam: 1UD129KBjvJXoW3Cw4xtry3AF4DArWfAr18Zrb_yoWkXw45pF Z5u3W7CF4kJryxGryIga1ag3W3JrWrJrn8Kr1Ykw4jyrWjqr4xGrsFvrs3WF1fJrWru3y7 uFn8WFyUuF18A3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.6 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, URIBL_BLOCKED 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 Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/strncpy-generic.c | 26 +++++++++ sysdeps/riscv/multiarch/strncpy-vector.S | 24 ++++++++ sysdeps/riscv/rvv/stpncpy.S | 27 +++++++-- sysdeps/riscv/rvv/strncpy.S | 20 +++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/stpncpy.c | 15 ++--- .../unix/sysv/linux/riscv/multiarch/strncpy.c | 56 +++++++++++++++++++ 8 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 sysdeps/riscv/multiarch/strncpy-generic.c create mode 100644 sysdeps/riscv/multiarch/strncpy-vector.S create mode 100644 sysdeps/riscv/rvv/strncpy.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c diff --git a/sysdeps/riscv/multiarch/strncpy-generic.c b/sysdeps/riscv/multiarch/strncpy-generic.c new file mode 100644 index 0000000000..ab0eb9af42 --- /dev/null +++ b/sysdeps/riscv/multiarch/strncpy-generic.c @@ -0,0 +1,26 @@ +/* Re-include the default strncpy 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 STRNCPY __strncpy_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +#endif +#include diff --git a/sysdeps/riscv/multiarch/strncpy-vector.S b/sysdeps/riscv/multiarch/strncpy-vector.S new file mode 100644 index 0000000000..32256f7695 --- /dev/null +++ b/sysdeps/riscv/multiarch/strncpy-vector.S @@ -0,0 +1,24 @@ +/* Re-include the RISC-V RVV based strncpy 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 STRNCPY __strncpy_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +# include +#endif diff --git a/sysdeps/riscv/rvv/stpncpy.S b/sysdeps/riscv/rvv/stpncpy.S index 57406685b0..d9004f2fba 100644 --- a/sysdeps/riscv/rvv/stpncpy.S +++ b/sysdeps/riscv/rvv/stpncpy.S @@ -19,12 +19,20 @@ #include #include -#ifndef STPNCPY -# ifdef weak_alias -# define STPNCPY __stpncpy +#ifndef USE_AS_STRNCPY +# ifndef STPNCPY +# ifdef weak_alias +# define STPNCPY __stpncpy weak_alias (__stpncpy, stpncpy) +# else +# define STPNCPY stpncpy +# endif +# endif +#else +# ifndef STRNCPY +# define STPNCPY strncpy # else -# define STPNCPY stpncpy +# define STPNCPY STRNCPY # endif #endif @@ -62,7 +70,9 @@ L(stpcpy_loop): add dst_ptr, dst_ptr, cur_vl bgez active_elem_pos, L(fill_zero) bnez length, L(stpcpy_loop) +#ifndef USE_AS_STRNCPY mv dst, dst_ptr +#endif ret /* Fill the tail zero. */ @@ -72,8 +82,10 @@ L(fill_zero): to get the correct remaining length for zero filling. */ sub temp, cur_vl, active_elem_pos addi temp, temp, -1 +#ifndef USE_AS_STRNCPY sub dst, dst_ptr, cur_vl add dst, dst, active_elem_pos +#endif add length, length, temp /* Have an earily return for `strlen(src) + 1 == count` case. */ bnez length, L(do_fill_zero) @@ -92,6 +104,11 @@ L(fill_zero_loop): ret .option pop END (STPNCPY) -#ifdef weak_alias + +#ifndef USE_AS_STRNCPY +# ifdef weak_alias libc_hidden_def (__stpncpy) +# endif +#else +libc_hidden_builtin_def (strncpy) #endif diff --git a/sysdeps/riscv/rvv/strncpy.S b/sysdeps/riscv/rvv/strncpy.S new file mode 100644 index 0000000000..961c16416e --- /dev/null +++ b/sysdeps/riscv/rvv/strncpy.S @@ -0,0 +1,20 @@ +/* RISC-V RVV based strncpy. + 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 + . */ + +#define USE_AS_STRNCPY +#include diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index bb3488e849..df33147de9 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -49,6 +49,9 @@ sysdep_routines += \ strncmp \ strncmp-generic \ strncmp-vector \ + strncpy \ + strncpy-generic \ + strncpy-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 025b026cf3..26444cc99d 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -125,5 +125,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strncmp_vector) IFUNC_IMPL_ADD (array, i, strncmp, 1, __strncmp_generic)) + IFUNC_IMPL (i, name, strncpy, + IFUNC_IMPL_ADD (array, i, strncpy, rvv_enabled, + __strncpy_vector) + IFUNC_IMPL_ADD (array, i, strncpy, 1, __strncpy_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c index e451391f1a..190b479a1b 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/stpncpy.c @@ -22,14 +22,15 @@ mismatch with the IFUNC selector in strong_alias, below. */ # undef stpncpy # define stpncpy __redirect_stpncpy +# define __stpncpy __redirect___stpncpy # include # include +# undef stpncpy +# undef __stpncpy # include # include # include -extern __typeof (__redirect_stpncpy) __libc_stpncpy; - extern __typeof (__redirect_stpncpy) __stpncpy_generic attribute_hidden; extern __typeof (__redirect_stpncpy) __stpncpy_vector attribute_hidden; @@ -43,14 +44,14 @@ select_stpncpy_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func) return __stpncpy_generic; } -riscv_libc_ifunc (__libc_stpncpy, select_stpncpy_ifunc); +riscv_libc_ifunc_redirected (__redirect___stpncpy, __stpncpy, + select_stpncpy_ifunc); # undef stpncpy -weak_alias (__libc_stpncpy, stpncpy); -libc_hidden_def (__stpncpy); +weak_alias (__stpncpy, stpncpy); # ifdef SHARED -__hidden_ver1 (stpncpy, __GI_stpncpy, __redirect_stpncpy) - __attribute__ ((visibility ("hidden"))) __attribute_copy__ (stpncpy); +__hidden_ver1 (__stpncpy, __GI___stpncpy, __redirect___stpncpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (__stpncpy); # endif #else # include diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c new file mode 100644 index 0000000000..459032312f --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strncpy.c @@ -0,0 +1,56 @@ +/* Multiple versions of strncpy. + 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 strncpy so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strncpy +# define strncpy __redirect_strncpy +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strncpy) __libc_strncpy; + +extern __typeof (__redirect_strncpy) __strncpy_generic attribute_hidden; +extern __typeof (__redirect_strncpy) __strncpy_vector attribute_hidden; + +static inline __typeof (__redirect_strncpy) * +select_strncpy_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 __strncpy_vector; + return __strncpy_generic; +} + +riscv_libc_ifunc (__libc_strncpy, select_strncpy_ifunc); + +# undef strncpy +strong_alias (__libc_strncpy, strncpy); +# ifdef SHARED +__hidden_ver1 (strncpy, __GI_strncpy, __redirect_strncpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strncpy); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:24 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128468 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 1F7574BA9016 for ; Mon, 19 Jan 2026 07:07:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F7574BA9016 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 1344C4BA9028 for ; Mon, 19 Jan 2026 07:04:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1344C4BA9028 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 1344C4BA9028 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806249; cv=none; b=Hve3wjwZLBtTW9+cqf+D/P0UjVjLujwJaT837x0FVFspMPle927wZuGgwhKC/+73WmA6zra+1AvvD/3P+JHXpSXRPJR+PKrJplGieSXXf7kpPry9mpxJ6JxsaVIMq1QVTFUzPsZ6WvyDpFK/XdT7W9wt7c9f3oKgQ0md1iKIXWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806249; c=relaxed/simple; bh=sQB2JFsKEHXRePtcKhSTX82IFZa7AbaZvXAnCAn1gvc=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=UH6apEONMq3GqJlH3YcNmolIHUbr5ySj5TU5ny938B/w9zXXnXSuROHee/MITJPXfaF9WnSyHS6EIJ0XPxU/aHOFOF98xO2o/6/pHIGOj/id3o1345CM9Pi6EdErmoL8KfrMdC5GUVeAWvIeQdt4rkGBUiGEhuaG7M59yfkzNUM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1344C4BA9028 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S19; Mon, 19 Jan 2026 15:04:00 +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 , Nick Knight Subject: [PATCH v4 17/18] riscv: Add RVV strnlen for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:24 +0800 Message-ID: <20260119070338.60322-18-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S19 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17GF18GF4fKr17Cr17Wrg_yoWfZrW3pF Z5ua4UCFs5Jr1xKryIgw10v3WrGrWfJrn8Kr909w4UJr4UXrWxXFsFvwsYgFyxJrWru3yD ZFnrWFyUCF48AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmv14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26r4j6F4UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr0_ Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x IIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_ Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8c xan2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCj c4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4 CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryU MIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJV WUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUv cSsGvfC2KfnxnUUI43ZEXa7VU1zpBDUUUUU== X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.7 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, URIBL_BLOCKED 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: Nick Knight Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/strnlen-generic.c | 24 ++++++++ sysdeps/riscv/multiarch/strnlen-vector.S | 26 ++++++++ sysdeps/riscv/rvv/strnlen.S | 61 +++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 ++ .../unix/sysv/linux/riscv/multiarch/strnlen.c | 56 +++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strnlen-generic.c create mode 100644 sysdeps/riscv/multiarch/strnlen-vector.S create mode 100644 sysdeps/riscv/rvv/strnlen.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c diff --git a/sysdeps/riscv/multiarch/strnlen-generic.c b/sysdeps/riscv/multiarch/strnlen-generic.c new file mode 100644 index 0000000000..c66e73b6bf --- /dev/null +++ b/sysdeps/riscv/multiarch/strnlen-generic.c @@ -0,0 +1,24 @@ +/* Re-include the default strnlen 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 STRNLEN __strnlen_generic +#endif +#include diff --git a/sysdeps/riscv/multiarch/strnlen-vector.S b/sysdeps/riscv/multiarch/strnlen-vector.S new file mode 100644 index 0000000000..8571af14ad --- /dev/null +++ b/sysdeps/riscv/multiarch/strnlen-vector.S @@ -0,0 +1,26 @@ +/* Re-include the RISC-V RVV based strnlen 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 STRNLEN __strnlen_vector +# undef libc_hidden_def +# define libc_hidden_def(name) +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/strnlen.S b/sysdeps/riscv/rvv/strnlen.S new file mode 100644 index 0000000000..0e114f8cd7 --- /dev/null +++ b/sysdeps/riscv/rvv/strnlen.S @@ -0,0 +1,61 @@ +/* RISC-V RVV based strnlen. + 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 STRNLEN +# define STRNLEN __strnlen +#endif + +#define str a0 +#define copy_str a2 +#define ret_value a0 +#define max_len a1 +#define cur_vl a3 +#define end_offset a4 + +#define ELEM_LMUL_SETTING m1 +#define vstr v0 +#define vmask_end v8 + +ENTRY (STRNLEN) +.option push +.option arch, +v + mv copy_str, str + mv ret_value, max_len +L(strnlen_vector_loop): + beqz max_len, L(end_strnlen_vector_loop) + vsetvli zero, max_len, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vstr, (copy_str) + vmseq.vi vmask_end, vstr, 0 + vfirst.m end_offset, vmask_end /* first occurence of \0 */ + csrr cur_vl, vl + add copy_str, copy_str, cur_vl + sub max_len, max_len, cur_vl + bltz end_offset, L(strnlen_vector_loop) + add max_len, max_len, cur_vl + sub ret_value, ret_value, max_len + add ret_value, ret_value, end_offset +L(end_strnlen_vector_loop): + ret +.option pop +END (STRNLEN) +weak_alias (STRNLEN, strnlen) +libc_hidden_def (STRNLEN) +libc_hidden_def (strnlen) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index df33147de9..4e10723636 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -52,6 +52,9 @@ sysdep_routines += \ strncpy \ strncpy-generic \ strncpy-vector \ + strnlen \ + strnlen-generic \ + strnlen-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 26444cc99d..dba3d852fd 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -130,5 +130,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strncpy_vector) IFUNC_IMPL_ADD (array, i, strncpy, 1, __strncpy_generic)) + IFUNC_IMPL (i, name, strnlen, + IFUNC_IMPL_ADD (array, i, strnlen, rvv_enabled, + __strnlen_vector) + IFUNC_IMPL_ADD (array, i, strnlen, 1, __strnlen_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c new file mode 100644 index 0000000000..6e570e6e4a --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strnlen.c @@ -0,0 +1,56 @@ +/* Multiple versions of strnlen. + 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 strnlen so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# define strnlen __redirect_strnlen +# define __strnlen __redirect___strnlen +# include +# include +# undef __strnlen +# undef strnlen +# include +# include +# include + +extern __typeof (__redirect_strnlen) __strnlen_generic attribute_hidden; +extern __typeof (__redirect_strnlen) __strnlen_vector attribute_hidden; + +static inline __typeof (__redirect_strnlen) * +select_strnlen_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 __strnlen_vector; + return __strnlen_generic; +} + +riscv_libc_ifunc_redirected (__redirect_strnlen, __strnlen, select_strnlen_ifunc); +weak_alias (__strnlen, strnlen); +# ifdef SHARED +__hidden_ver1 (__strnlen, __GI___strnlen, __redirect___strnlen) + __attribute__((visibility ("hidden"))) __attribute_copy__ (strnlen); +__hidden_ver1 (__strnlen, __GI_strnlen, __redirect_strnlen) + __attribute__((weak, visibility ("hidden"))) __attribute_copy__ (strnlen); +# endif +#else +# include +#endif From patchwork Mon Jan 19 07:03:25 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Zihong X-Patchwork-Id: 128463 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 AF1B64BA2E2E for ; Mon, 19 Jan 2026 07:05:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF1B64BA2E2E X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 24FB34BA9000 for ; Mon, 19 Jan 2026 07:04:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24FB34BA9000 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 24FB34BA9000 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806249; cv=none; b=LyVllGT4YiRsZsr+TqwHDvCPY6HX12Scrpdhd7XHB2YND+xyeWVlVd1CgV5SNcncxfxHl+GWiosf7BVeuM7+r6rtOuoWHsIkV6aFTSDwOcM81Wa0Jkqzfz0xnJYQsr5HLsGPmExMuuwqhKbwp4ApkdsLzhil9A/pIHv7LMviJpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768806249; c=relaxed/simple; bh=gFKcjZTzaYBmNedrLLiLiTs6ftTKuqZ6Bs5x3Z54F3A=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=qmSkXJ6aswqYW/Kea/EXqXH/UFviROkQH1UH7GhXh8BEVm4KC+vmm+xcGR0o4YKff2+dOnTABZA0lE1P0X5SR9lbaoGGq4AiAPKWOblppc0ZYQHnNI9KH8/3f6drR08v3Mv4t52Kn57mFcnNGHcUEoaFr8wVuDyMu7+UzFJdL60= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24FB34BA9000 Received: from Mobilestation.localdomain (unknown [183.6.59.244]) by APP-05 (Coremail) with SMTP id zQCowACnPBBN121p8yKSBQ--.36067S20; Mon, 19 Jan 2026 15:04:01 +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 Subject: [PATCH v4 18/18] riscv: Add RVV strrchr for multiarch and non-multiarch Date: Mon, 19 Jan 2026 15:03:25 +0800 Message-ID: <20260119070338.60322-19-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> References: <20260119070338.60322-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: zQCowACnPBBN121p8yKSBQ--.36067S20 X-Coremail-Antispam: 1UD129KBjvPXoW5ZFy5Gw4xZFyrGFWDuF45p5X_trWkAoZagF ZrXF42gr18CrW5Cw4DJasrXr4Ygr1jqw4YqaykXw1rK34rCryIv34DWrW3Gr4rWFs5AFy7 trW3JFsrGFn3n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3AaLa J3UjIYCTnIWjp_UUUOa7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_ Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7IE14v26r126s0DM28IrcIa0x kI8VCY1x0267AKxVW5JVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84AC jcxK6xIIjxv20xvE14v26ryj6F1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr 1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr1j6F4U JwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW5JVW7JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF 4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBI daVFxhVjvjDU0xZFpf9x0JUWMKtUUUUU= X-Originating-IP: [183.6.59.244] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.7 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, URIBL_BLOCKED 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 Signed-off-by: Yao Zihong --- sysdeps/riscv/multiarch/strrchr-generic.c | 28 ++++ sysdeps/riscv/multiarch/strrchr-vector.S | 26 ++++ sysdeps/riscv/rvv/strrchr.S | 123 ++++++++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 3 + .../linux/riscv/multiarch/ifunc-impl-list.c | 5 + .../unix/sysv/linux/riscv/multiarch/strrchr.c | 59 +++++++++ 6 files changed, 244 insertions(+) create mode 100644 sysdeps/riscv/multiarch/strrchr-generic.c create mode 100644 sysdeps/riscv/multiarch/strrchr-vector.S create mode 100644 sysdeps/riscv/rvv/strrchr.S create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c diff --git a/sysdeps/riscv/multiarch/strrchr-generic.c b/sysdeps/riscv/multiarch/strrchr-generic.c new file mode 100644 index 0000000000..65bdbf4da2 --- /dev/null +++ b/sysdeps/riscv/multiarch/strrchr-generic.c @@ -0,0 +1,28 @@ +/* Re-include the default strrchr 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 STRRCHR __strrchr_generic +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(x) +# undef weak_alias +# define weak_alias(x, x2) +#endif +#include diff --git a/sysdeps/riscv/multiarch/strrchr-vector.S b/sysdeps/riscv/multiarch/strrchr-vector.S new file mode 100644 index 0000000000..8fef68eae0 --- /dev/null +++ b/sysdeps/riscv/multiarch/strrchr-vector.S @@ -0,0 +1,26 @@ +/* Re-include the RISC-V RVV based strrchr 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 STRRCHR __strrchr_vector +# undef libc_hidden_builtin_def +# define libc_hidden_builtin_def(name) +# undef weak_alias +# define weak_alias(name, alias) +# include +#endif diff --git a/sysdeps/riscv/rvv/strrchr.S b/sysdeps/riscv/rvv/strrchr.S new file mode 100644 index 0000000000..f2da8a4b23 --- /dev/null +++ b/sysdeps/riscv/rvv/strrchr.S @@ -0,0 +1,123 @@ +/* RISC-V RVV based strrchr. + 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 STRRCHR +# define STRRCHR strrchr +#endif + +#define str a0 +#define ch a1 +#define blk_base a2 +#define loc_max a3 +#define latest_ptr a4 +#define first_idx a5 +#define len_valid a5 +#define cur_vl a6 + +#define vhit v0 +#define vstr v4 +#define vmask_end v8 +#define vmask_ch v10 +#define vvalid v12 +#define vidx v16 +#define vacc v20 + +#if __riscv_xlen == 64 +# define XLEN_SHIFT 63 +#else +# define XLEN_SHIFT 31 +#endif + +ENTRY (STRRCHR) +.option push +.option arch, +v + beqz ch, L(search_zero) + mv latest_ptr, zero +L(loop): + mv blk_base, str + + vsetvli cur_vl, zero, e8, m2, ta, ma + vle8ff.v vstr, (str) + + vmseq.vi vmask_end, vstr, 0 + vmsbf.m vvalid, vmask_end + vmseq.vx vmask_ch, vstr, ch + vmand.mm vhit, vmask_ch, vvalid + + vfirst.m first_idx, vhit + csrr cur_vl, vl + bltz first_idx, L(no_hit_in_block) + + vsetvli zero, cur_vl, e16, m4, ta, ma + vid.v vidx + vmv.s.x vacc, x0 + /* Compute per-block last hit index (or -1 if none). */ + vredmaxu.vs vacc, vidx, vacc, vhit.t + vmv.x.s loc_max, vacc + j L(loc_max_ready) + +L(no_hit_in_block): + li loc_max, -1 + +L(loc_max_ready): + vsetvli zero, cur_vl, e8, m2, ta, ma + vcpop.m len_valid, vvalid + + bne len_valid, cur_vl, L(tail_block) + bltz loc_max, L(advance) + + add latest_ptr, blk_base, loc_max + +L(advance): + add str, str, cur_vl + j L(loop) + +L(tail_block): + add str, blk_base, loc_max + + /* sign = -1 if loc_max < 0 else 0; then invert to + build selection mask */ + srai loc_max, loc_max, XLEN_SHIFT + and len_valid, loc_max, latest_ptr + xori loc_max, loc_max, -1 + and str, loc_max, str + or str, str, len_valid + ret + +L(search_zero): +L(sz_loop): + vsetvli cur_vl, zero, e8, m4, ta, ma + vle8ff.v vstr, (str) + + vmseq.vi vmask_end, vstr, 0 + vfirst.m first_idx, vmask_end + bltz first_idx, L(sz_advance) + add str, str, first_idx + ret +L(sz_advance): + csrr cur_vl, vl + add str, str, cur_vl + j L(sz_loop) + +.option pop +END (STRRCHR) +weak_alias (STRRCHR, rindex) +libc_hidden_builtin_def (strrchr) diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index 4e10723636..8e2b1632da 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -55,6 +55,9 @@ sysdep_routines += \ strnlen \ strnlen-generic \ strnlen-vector \ + strrchr \ + strrchr-generic \ + strrchr-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 dba3d852fd..d7db46cf81 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c @@ -135,5 +135,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, __strnlen_vector) IFUNC_IMPL_ADD (array, i, strnlen, 1, __strnlen_generic)) + IFUNC_IMPL (i, name, strrchr, + IFUNC_IMPL_ADD (array, i, strrchr, rvv_enabled, + __strrchr_vector) + IFUNC_IMPL_ADD (array, i, strrchr, 1, __strrchr_generic)) + return 0; } diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c new file mode 100644 index 0000000000..3532af0dc0 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strrchr.c @@ -0,0 +1,59 @@ +/* Multiple versions of strrchr. + 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 strrchr so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef strrchr +# define strrchr __redirect_strrchr +# include +# include +# include +# include +# include + +extern __typeof (__redirect_strrchr) __libc_strrchr; + +extern __typeof (__redirect_strrchr) __strrchr_generic attribute_hidden; +extern __typeof (__redirect_strrchr) __strrchr_vector attribute_hidden; + +static inline __typeof (__redirect_strrchr) * +select_strrchr_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 __strrchr_vector; + return __strrchr_generic; +} + +riscv_libc_ifunc (__libc_strrchr, select_strrchr_ifunc); + +# undef strrchr +# undef rindex +strong_alias (__libc_strrchr, strrchr); +weak_alias (strrchr, rindex); +# ifdef SHARED +__hidden_ver1 (strrchr, __GI_strrchr, __redirect_strrchr) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strrchr); +# endif +#else +# include +#endif