From patchwork Tue Feb 3 15:05:27 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zihong Yao X-Patchwork-Id: 129487 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 7CD1B4BB3B94 for ; Tue, 3 Feb 2026 15:15:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CD1B4BB3B94 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cstnet.cn (smtp21.cstnet.cn [159.226.251.21]) by sourceware.org (Postfix) with ESMTPS id C87E94BA2E08 for ; Tue, 3 Feb 2026 15:14:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C87E94BA2E08 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 C87E94BA2E08 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.21 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770131699; cv=none; b=EuBli3INK+DOibweWfKwKMYYOJtnEf5/VAbPyY/bjTVPR8m7/gXXPRNH+bA/VVMvUA691GGjv4ENEjsq/LLR3nmZoFccCvuvLREoP8tZ/nqHBgn2LXNTDamO4Ysi+Wemct6wrP4P9OTGdNU/Ji22gsfavYT8jRL7KMC8NxQdkMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1770131699; c=relaxed/simple; bh=2z72ysHU6r5C3EwrGWMPUb54E5LylORLbKCCKbuY5N8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ARBR/oZamUxK+cBwPvF/azgvdiTQPNV4AgB4bXgyxP0SbwJZ4fwjUQ4fpKpgbmOrsEmOnE8lH0bavSEtGUdgsPu4igzYTk2evQNMr/tvLW2p99I8ysKXks4EncH3nVZto/ndZuV7z7xjcR6CvFrYTHSJU/Hhb7fkfyxu28f5UzU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C87E94BA2E08 Received: from Mobilestation.localdomain (unknown [183.6.59.140]) by APP-01 (Coremail) with SMTP id qwCowADHbmriEIJpZ2EVBw--.24930S5; Tue, 03 Feb 2026 23:14:48 +0800 (CST) From: Yao Zihong To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, andrew@sifive.com, schwab@linux-m68k.org, bergner@tenstorrent.com, jlaw@ventanamicro.com, zhangyin2018@iscas.ac.cn, enh@google.com, zihongyao@outlook.com, Yao Zihong , Hau Hsu , Jerry Shih Subject: [PATCH v5 03/18] riscv: Add RVV memcmp for multiarch and non-multiarch Date: Tue, 3 Feb 2026 23:05:27 +0800 Message-ID: <20260203151406.27450-4-zihong.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260203151406.27450-1-zihong.plct@isrc.iscas.ac.cn> References: <20260203151406.27450-1-zihong.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: qwCowADHbmriEIJpZ2EVBw--.24930S5 X-Coremail-Antispam: 1UD129KBjvJXoW3JF17tw1fArWkur4xZr4fXwb_yoWfuFWfpF Z5u3WUGF4kG3yfGrWSk3WYg3W5ArZ5Jrn0kr1Y9w4UJrWUJw4fWa9Fyas5GF93JrWFk3y5 uFn8Wa4j9F4kAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmj14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1l84 ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UM2AI xVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20x vE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xv r2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxan2IY04 v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7VUbpwZ7UUUUU== X-Originating-IP: [183.6.59.140] X-CM-SenderInfo: p2lk00vjoszunw6l223fol2u1dvotugofq/ X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, 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 | 30 +++++++ 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, 212 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..14f6752406 --- /dev/null +++ b/sysdeps/riscv/multiarch/memcmp-generic.c @@ -0,0 +1,30 @@ +/* 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) +# include +#endif 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