From patchwork Fri Aug 14 02:26:54 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pincheng Wang X-Patchwork-Id: 141365 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2BDD4BA798F for ; Fri, 14 Aug 2026 02:34:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2BDD4BA798F X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from cstnet.cn (smtp81.cstnet.cn [159.226.251.81]) by sourceware.org (Postfix) with ESMTPS id 4784C4BA79AF for ; Fri, 14 Aug 2026 02:34:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4784C4BA79AF 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 4784C4BA79AF Authentication-Results: sourceware.org; arc=none smtp.remote-ip=159.226.251.81 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786674856; cv=none; b=HjjO2cvzJkvphlif67xbK54wV/2gXFd8DmW2AOsIs91I80lqR4MuRkH6vC8hqE9Vg5s5dgfB9QdAsj+PzJ6LbpgC6npqfBwt0kuBNeeECwhDRwRcAHi32Q4dgk5KNltZJfRi7a0FxLtypf13vXEfIee8k/oxyXFnsXwWE36Wbfg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786674856; c=relaxed/simple; bh=u/l6Y1lBQsp87zzVVWWGN1ivSB0VszSZXU3pUykr1Zk=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=i+ZuA9ioIapYAawb8l+wVAX4xe0BZBap5sH793cNkA4SNpWT26eV0ApUWTQwZyvGDtVxkEZ5xq2h+69yqbu3lYlRCZPFn2S3QGplueMCnUadFX2lAPMtEiYaTLi7R2w2V4hXBXhlTR61cl1sGNGxnt9bHkNk3wl+WkTp9ulozMU= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4784C4BA79AF Received: from ROG.localdomain (unknown [183.134.225.12]) by APP-03 (Coremail) with SMTP id rQCowACnsT_xfH5qsxNIBQ--.31342S15; Fri, 14 Aug 2026 10:27:01 +0800 (CST) From: Pincheng Wang To: newlib@sourceware.org Cc: Pincheng Wang Subject: [PATCH v3 13/15] riscv: add vectorized strcspn Date: Fri, 14 Aug 2026 10:26:54 +0800 Message-Id: <20260814022656.1071643-14-pincheng.plct@isrc.iscas.ac.cn> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260814022656.1071643-1-pincheng.plct@isrc.iscas.ac.cn> References: <20260814022656.1071643-1-pincheng.plct@isrc.iscas.ac.cn> MIME-Version: 1.0 X-CM-TRANSID: rQCowACnsT_xfH5qsxNIBQ--.31342S15 X-Coremail-Antispam: 1UD129KBjvJXoWxXr1DGr4fKF4fZw1UAw43ZFb_yoW5tF4xpF srKanIyr1fJrn3CrZ7W3W0vFW5J3s09r13GFWxZa1jy3ykG345WFs8tr43A3WxXFyvvFn7 u3Wjkr1Y9w1DXw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9G14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr 1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1U M2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjx v20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1l F7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxAIw28IcxkI7VAKI48JMx C20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAF wI0_JrI_JrWlx4CE17CEb7AF67AKxVWUXVWUAwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20x vE14v26r4j6ryUMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v2 0xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxV W8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbmZX7UUUUU== X-Originating-IP: [183.134.225.12] X-CM-SenderInfo: pslquxhhqjh1xofwqxxvufhxpvfd2hldfou0/ X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_PBL, SPF_HELO_PASS, SPF_PASS, TXREP shortcircuit=no 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org The vector implementation builds a 256-byte stop table on the stack (the reject bytes plus NUL) and scans the string in vector-length chunks using fault-only-first loads (vle8ff.v) with an indexed gather into the table, so the scan never crosses into an unmapped page and the gather only touches the fully-mapped table. This provides significant performance improvements on RVV-capable hardware. Use conditional compilation to fall back to the generic implementation when __riscv_vector is not available, maintaining compatibility with non-vector RISC-V systems. Signed-off-by: Pincheng Wang --- newlib/libc/machine/riscv/Makefile.inc | 2 + newlib/libc/machine/riscv/strcspn-asm.S | 51 +++++++++++++++++++++++++ newlib/libc/machine/riscv/strcspn.c | 5 +++ 3 files changed, 58 insertions(+) create mode 100644 newlib/libc/machine/riscv/strcspn-asm.S create mode 100644 newlib/libc/machine/riscv/strcspn.c diff --git a/newlib/libc/machine/riscv/Makefile.inc b/newlib/libc/machine/riscv/Makefile.inc index 55f137a3c..1e35cf17d 100644 --- a/newlib/libc/machine/riscv/Makefile.inc +++ b/newlib/libc/machine/riscv/Makefile.inc @@ -30,6 +30,8 @@ libc_a_SOURCES += \ %D%/strcmp.S \ %D%/strcpy-asm.S \ %D%/strcpy.c \ + %D%/strcspn-asm.S \ + %D%/strcspn.c \ %D%/strlen-asm.S \ %D%/strlen.c \ %D%/strncat-asm.S \ diff --git a/newlib/libc/machine/riscv/strcspn-asm.S b/newlib/libc/machine/riscv/strcspn-asm.S new file mode 100644 index 000000000..106b46e43 --- /dev/null +++ b/newlib/libc/machine/riscv/strcspn-asm.S @@ -0,0 +1,51 @@ +#include + +#if defined(__riscv_vector) && !defined(__riscv_e) && !defined(__OPTIMIZE_SIZE__) && !defined(PREFER_SIZE_OVER_SPEED) +/* size_t strcspn(const char *a0, const char *a1) + Length of the initial segment of a0 made up of bytes NOT in a1. + Build a 256-byte "stop" table on the stack (a1's bytes plus NUL marked), + then scan a0: a gathered table entry != 0 ends the segment. The scan of + a0 uses vle8ff so it never loads across a page boundary; the indexed + gather only touches the fully-mapped 256-byte table. */ +ENTRY(strcspn) + addi sp, sp, -256 + /* zero the table */ + li t1, 256 + mv t0, sp +.Lzero: + vsetvli t2, t1, e8, m8, ta, ma + vmv.v.i v8, 0 + vse8.v v8, (t0) + add t0, t0, t2 + sub t1, t1, t2 + bnez t1, .Lzero + /* mark NUL as a stopper so the scan halts at end of string */ + li t1, 1 + sb t1, 0(sp) + /* mark every byte of a1 */ +.Lbuild: + lbu t0, 0(a1) + beqz t0, .Lscan + add t2, sp, t0 + sb t1, 0(t2) + addi a1, a1, 1 + j .Lbuild +.Lscan: + mv a2, a0 /* remember start */ +.Lloop: + vsetvli t3, zero, e8, m1, ta, ma + vle8ff.v v8, (a0) + csrr t3, vl + vluxei8.v v16, (sp), v8 /* gather table[a0[i]] */ + vmsne.vi v0, v16, 0 /* lanes that are stoppers */ + vfirst.m t4, v0 + bgez t4, .Lfound + add a0, a0, t3 + j .Lloop +.Lfound: + add a0, a0, t4 + sub a0, a0, a2 + addi sp, sp, 256 + ret +END(strcspn) +#endif diff --git a/newlib/libc/machine/riscv/strcspn.c b/newlib/libc/machine/riscv/strcspn.c new file mode 100644 index 000000000..eb3acf829 --- /dev/null +++ b/newlib/libc/machine/riscv/strcspn.c @@ -0,0 +1,5 @@ +#if defined(__OPTIMIZE_SIZE__) || defined(PREFER_SIZE_OVER_SPEED) || !defined(__riscv_vector) || defined(__riscv_e) +# include "../../string/strcspn.c" +#else +/* strcspn defined in strcspn-asm.S */ +#endif