| Message ID | 20251225160856.16010-1-pincheng.plct@isrc.iscas.ac.cn |
|---|---|
| Headers |
Return-Path: <newlib-bounces~patchwork=sourceware.org@sourceware.org> 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 01F584BA23D6 for <patchwork@sourceware.org>; Thu, 25 Dec 2025 16:09:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 01F584BA23D6 X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 1A4184BA2E04 for <newlib@sourceware.org>; Thu, 25 Dec 2025 16:09:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A4184BA2E04 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 1A4184BA2E04 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=1766678942; cv=none; b=Y3IRIlGxrUsIrLgKy+L01+hcIbu6FUrxIjcG8WSMLPmtup9ahEipyN671Q/wv1DQaZQHuzsCsk77+nQ8WIMNVF/cKDp96M5bIoMNfNlLXa0bAxqbZPTsri9TijN4/hdD8ppeEwTx4yu9Q5ZMHVrcSRIcMqyaI/VxFOKxxxg8lrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766678942; c=relaxed/simple; bh=WT8N8vP4Os1Oz8lmFWY8xFEzkL9oPFg9DWv+dL52Hlk=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=WCixhRCji8oLmDynk42QUhm7qDx2Ayh0U+xprC6TuaUh6Q5dG94ou7b7Uig5BgCiZTxOW2vOLIvNGarrqKM2JWUkMyhgrg1L7vN/VOCh3M+yCl+i3+1jh/5PMZMheAdAzwA5IfmBSlTwZIZvnzl8iNt2Zodyi9fttzx9u+k3aAY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1A4184BA2E04 Received: from ROG.lan (unknown [120.227.57.105]) by APP-05 (Coremail) with SMTP id zQCowAAHnA+aYU1pzB7sAQ--.14266S2; Fri, 26 Dec 2025 00:08:58 +0800 (CST) From: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> To: newlib@sourceware.org Cc: pincheng.plct@isrc.iscas.ac.cn Subject: [PATCH v2 0/1] riscv: add vectorized memset, memcpy and memmove Date: Fri, 26 Dec 2025 00:08:55 +0800 Message-Id: <20251225160856.16010-1-pincheng.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.39.5 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: zQCowAAHnA+aYU1pzB7sAQ--.14266S2 X-Coremail-Antispam: 1UD129KBjvJXoW7WryrKFy8JFy7ury8AryUtrb_yoW8uryDpF WfGFn0yrn7Jwn3Jr1Sya1kZ343u3s5Gw45JFy2k3s0qFs8Ga4FkFWqya13ZF98JrZ7Kw1f Ww40kry5uF1UZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyq14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r1j6r1xM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r1j 6r4UM28EF7xvwVC2z280aVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r1j6r 4UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xII jxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxAIw28IcxkI7VAKI48J MxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwV AFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUXVWUAwCIc40Y0x0EwIxGrwCI42IY6xIIjxv2 0xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AK xVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfU5WlkUUUUU X-Originating-IP: [120.227.57.105] X-CM-SenderInfo: pslquxhhqjh1xofwqxxvufhxpvfd2hldfou0/ X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
riscv: add vectorized memset, memcpy and memmove
|
|
Message
Pincheng Wang
Dec. 25, 2025, 4:08 p.m. UTC
Hi all, This v2 patch adds RISC-V Vector (RVV) optimized implementations for memset, memcpy and memmove. Changes since v1: - Switch the conditional compilation macro from __riscv_v to __riscv_vector. - Replaced '.option arch,+v' with '.option arch,+zve32x'. - Removed an unnecessary unconditional jump instruction in memmove-asm.S - In memcpy and memmove, when __riscv_misaligned_fast is not defined, the destination address is now aligned to SZREG to improve performance on systems with slow misaligned accesses. These implementations use the RVV extension with e8 element size and m8 LMUL, and are conditionally compiled only when __riscv_vector is defined, ensuring compatibility with non-vector RISC-V systems. Benchmark results on Spacemit X60 (Muse-pi) and Canaan K230 show significant improvements. memcpy: Up to 4.84x on Muse-pi and 4.66x on K230. memset: Up to 4.31x on Muse-pi and 3.14x on K230. memmove: Up to 2.87x on Muse-pi and 1.48x on K230. With newly added alignment handling, these improvements are consistent across both aligned and misaligned memory acesses. In the v1 review thread, Kito suggested removing the 'sub' instruction in the RVV memcpy loop and relying solely on the end-pointer-based loop condition. After careful consideration, I would like to clarify that in this specific context, the 'sub' cannot actually be eliminated since the RVV loop still requires the exact number of remaining bytes to be provided to vsetvli. Therefore, I have retained the orignial loop structure. Comments and suggestions are greatly appreciated, Thank you for your time and review! Thanks, Pincheng Wang Pincheng Wang (1): riscv: add vectorized memset, memcpy and memmove newlib/libc/machine/riscv/memcpy-asm.S | 54 +++++++++++++- newlib/libc/machine/riscv/memcpy.c | 2 +- newlib/libc/machine/riscv/memmove-asm.S | 95 ++++++++++++++++++++++++- newlib/libc/machine/riscv/memmove.c | 2 +- newlib/libc/machine/riscv/memset.S | 21 +++++- 5 files changed, 169 insertions(+), 5 deletions(-)
Comments
Hi all, Happy new year! Just a gentle ping on this patch series. Please let me know if there are any remaining concerns that I can help address. Happy to update the series if needed. :) Thanks, Pincheng Wang On 2025/12/26 0:08, Pincheng Wang wrote: > This v2 patch adds RISC-V Vector (RVV) optimized implementations for > memset, memcpy and memmove. > > Changes since v1: > - Switch the conditional compilation macro from __riscv_v to __riscv_vector. > - Replaced '.option arch,+v' with '.option arch,+zve32x'. > - Removed an unnecessary unconditional jump instruction in memmove-asm.S > - In memcpy and memmove, when __riscv_misaligned_fast is not defined, > the destination address is now aligned to SZREG to improve performance > on systems with slow misaligned accesses. > > These implementations use the RVV extension with e8 element size and m8 > LMUL, and are conditionally compiled only when __riscv_vector is > defined, ensuring compatibility with non-vector RISC-V systems. > > Benchmark results on Spacemit X60 (Muse-pi) and Canaan K230 show significant > improvements. > > memcpy: Up to 4.84x on Muse-pi and 4.66x on K230. > memset: Up to 4.31x on Muse-pi and 3.14x on K230. > memmove: Up to 2.87x on Muse-pi and 1.48x on K230. > > With newly added alignment handling, these improvements are consistent > across both aligned and misaligned memory acesses. > > In the v1 review thread, Kito suggested removing the 'sub' instruction in > the RVV memcpy loop and relying solely on the end-pointer-based loop > condition. After careful consideration, I would like to clarify that in > this specific context, the 'sub' cannot actually be eliminated since the > RVV loop still requires the exact number of remaining bytes to be > provided to vsetvli. Therefore, I have retained the orignial loop > structure. > > Comments and suggestions are greatly appreciated, Thank you for your > time and review! > > Thanks, > Pincheng Wang > > Pincheng Wang (1): > riscv: add vectorized memset, memcpy and memmove > > newlib/libc/machine/riscv/memcpy-asm.S | 54 +++++++++++++- > newlib/libc/machine/riscv/memcpy.c | 2 +- > newlib/libc/machine/riscv/memmove-asm.S | 95 ++++++++++++++++++++++++- > newlib/libc/machine/riscv/memmove.c | 2 +- > newlib/libc/machine/riscv/memset.S | 21 +++++- > 5 files changed, 169 insertions(+), 5 deletions(-) >
On Sat, 3 Jan 2026 at 16:22, Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> wrote: > Hi all, > > Happy new year! > > Just a gentle ping on this patch series. Please let me know if there are > any remaining concerns that I can help address. Happy to update the > series if needed. :) Dear Pincheng, I have some high-level questions from a purely user perspective. Please do not consider this as a "review". :) > On 2025/12/26 0:08, Pincheng Wang wrote: > > This v2 patch adds RISC-V Vector (RVV) optimized implementations for > > memset, memcpy and memmove. > > > > Changes since v1: > > - Switch the conditional compilation macro from __riscv_v to __riscv_vector. > > - Replaced '.option arch,+v' with '.option arch,+zve32x'. What is the implication of this change? I'm a newbie to RVV, and "Zve32x" looks like a subset implementation. Is it possible to compile the standard library post-patch if a target usually blanket-enables "+v"? ("rv64imafdcv") > > - Removed an unnecessary unconditional jump instruction in memmove-asm.S > > - In memcpy and memmove, when __riscv_misaligned_fast is not defined, > > the destination address is now aligned to SZREG to improve performance > > on systems with slow misaligned accesses. > > > > These implementations use the RVV extension with e8 element size and m8 > > LMUL, and are conditionally compiled only when __riscv_vector is > > defined, ensuring compatibility with non-vector RISC-V systems. Is the condition for this conditional compilation decided at the moment of compiling 'newlib'? So, if someone wants to create a distribution where it is possible to compile for both non-RVV and RVV-capable target configurations, this essentially means having to distribute two versions of the newlib binary archive? Am I right to think that "pre-compiling" newlib with vectorised mem{cpy,move,set} and linking it with a binary that is otherwise non-RVV, and execute the whole image on a non-RVV platform would simply result in a corrupted execution? > > Benchmark results on Spacemit X60 (Muse-pi) and Canaan K230 show significant > > improvements. > > > > memcpy: Up to 4.84x on Muse-pi and 4.66x on K230. > > memset: Up to 4.31x on Muse-pi and 3.14x on K230. > > memmove: Up to 2.87x on Muse-pi and 1.48x on K230. > > > > With newly added alignment handling, these improvements are consistent > > across both aligned and misaligned memory acesses.
Hi Richard, Thank you for the insightful questions, and sorry for the late reply. Below is the clarifications. About ".option arch, +zve32x": This directive tells the assembler to accept instructions from the Zve32x vector subset for this file only. It is a local override used when build systems do not pass "-march" flags that include vector extensions. The intent is to ensure that assembly succeeds as long as the toolchain supports Zve32x, without requireing full "+v". Why Zve32x instead of V: Zve32x is the *base* embedded-profile vector extension and guarantees availability of byte-vector operations(vle8 & vse8) used in my patch series. Full V extension is a superset and always compatible, but not all embedded targets implement it. Selecting Zve32x therefore avoids excluding MCUs and bare-metal systems that support Zve extensions but do not implement the full V extension. About conditional compilation and distribution implications: You are correct, "__riscv_vector" is evaluated at newlib build time. A binary compiled with the vectorized implementations assumes the presence of vector hardware. Executing such a binary on a non-RVV system would result in illegal-instruction traps or undefined behavior. Therefore, a distribution that wishes to support both RVV and non-RVV targets would typically need to ship two builds, or rely on a multi-lib strategy if their build workflows allow it. Best regards, Pincheng Wang On 2026/1/6 19:27, Richárd Szalay wrote: > On Sat, 3 Jan 2026 at 16:22, Pincheng Wang > <pincheng.plct@isrc.iscas.ac.cn> wrote: >> Hi all, >> >> Happy new year! >> >> Just a gentle ping on this patch series. Please let me know if there are >> any remaining concerns that I can help address. Happy to update the >> series if needed. :) > > Dear Pincheng, > > I have some high-level questions from a purely user perspective. > Please do not consider this as a "review". :) > >> On 2025/12/26 0:08, Pincheng Wang wrote: >>> This v2 patch adds RISC-V Vector (RVV) optimized implementations for >>> memset, memcpy and memmove. >>> >>> Changes since v1: >>> - Switch the conditional compilation macro from __riscv_v to __riscv_vector. >>> - Replaced '.option arch,+v' with '.option arch,+zve32x'. > What is the implication of this change? I'm a newbie to RVV, and > "Zve32x" looks like a subset implementation. Is it possible to compile > the standard library post-patch if a target usually blanket-enables > "+v"? ("rv64imafdcv") > >>> - Removed an unnecessary unconditional jump instruction in memmove-asm.S >>> - In memcpy and memmove, when __riscv_misaligned_fast is not defined, >>> the destination address is now aligned to SZREG to improve performance >>> on systems with slow misaligned accesses. >>> >>> These implementations use the RVV extension with e8 element size and m8 >>> LMUL, and are conditionally compiled only when __riscv_vector is >>> defined, ensuring compatibility with non-vector RISC-V systems. > Is the condition for this conditional compilation decided at the > moment of compiling 'newlib'? So, if someone wants to create a > distribution where it is possible to compile for both non-RVV and > RVV-capable target configurations, this essentially means having to > distribute two versions of the newlib binary archive? Am I right to > think that "pre-compiling" newlib with vectorised mem{cpy,move,set} > and linking it with a binary that is otherwise non-RVV, and execute > the whole image on a non-RVV platform would simply result in a > corrupted execution? > >>> Benchmark results on Spacemit X60 (Muse-pi) and Canaan K230 show significant >>> improvements. >>> >>> memcpy: Up to 4.84x on Muse-pi and 4.66x on K230. >>> memset: Up to 4.31x on Muse-pi and 3.14x on K230. >>> memmove: Up to 2.87x on Muse-pi and 1.48x on K230. >>> >>> With newly added alignment handling, these improvements are consistent >>> across both aligned and misaligned memory acesses.