| Message ID | 20260716150048.216052-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 [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1A504BAE7F2 for <patchwork@sourceware.org>; Thu, 16 Jul 2026 15:01:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1A504BAE7F2 X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from cstnet.cn (smtp25.cstnet.cn [159.226.251.25]) by sourceware.org (Postfix) with ESMTPS id 7FC194BA2E1B for <newlib@sourceware.org>; Thu, 16 Jul 2026 15:00:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7FC194BA2E1B 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 7FC194BA2E1B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=159.226.251.25 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784214059; cv=none; b=OUysgGUPG9yLF8PYIcXaHJXZpbO/qsWpkST3RhnC4bclu7oH44HmGtQ/G7KN5TkK+R8iAsV6SHQ5CqKFaRoyldahxbwVTs87RVhd5jMG4L+P/22YoGv/HJubLOXiqOpCyqcZ5JC/fauN6p9RjIX8rH7y3o1SVcU0WlyZZtF+nDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784214059; c=relaxed/simple; bh=rL/if5bz28ahLAp83os03i31fwj4wn6Evivc2ZvClmg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=qDvGMZkO0W7pyWz7CFePV50/5EAfjLeMEvk6dKNf6nYYgmO6DOI75JbacqYOCTqt5yxyZr9EQ80S1yimvbSrg3nXlaQEqeyADDWH1H43gWiLB2sajKyGB4USTb5Wdw1l7Q3eMh0GvBcptwlMtA5u6Zv1jR7OQCok3GiwDWlkFLY= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FC194BA2E1B Received: from ROG.localdomain (unknown [39.185.147.11]) by APP-05 (Coremail) with SMTP id zQCowAB3zhEk8lhqzOVpGA--.48426S2; Thu, 16 Jul 2026 23:00:54 +0800 (CST) From: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> To: newlib@sourceware.org Cc: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> Subject: [PATCH 00/17] riscv: vectorized str* routines Date: Thu, 16 Jul 2026 23:00:31 +0800 Message-Id: <20260716150048.216052-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: zQCowAB3zhEk8lhqzOVpGA--.48426S2 X-Coremail-Antispam: 1UD129KBjvJXoW3Gw18KF1fCr4DKF1DJw1xuFg_yoWxZw4rpF s7Ga15Jw18trn3ArsxZF18ury5Ja93Ww13Kr4fAa1YqFZ7K3yrGrZ8Jw13trWfJFn0vF1x uF4kCr1Ygwn8XFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 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: [39.185.147.11] X-CM-SenderInfo: pslquxhhqjh1xofwqxxvufhxpvfd2hldfou0/ X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, 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 <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: vectorized str* routines
|
|
Message
Pincheng Wang
July 16, 2026, 3 p.m. UTC
Hi all,
This series adds RISC-V Vector Extension (RVV) implementations for the str*
family in Newlib, as a follow-up to the earlier mem* work (memset, memcpy,
memmove, and then memccpy, memchr, memcmp, mempcpy, memrchr, rawmemchr). It
covers the following 17 interfaces:
strlen, strnlen, strchr, strchrnul, strrchr, strcmp, strncmp, strcpy,
strncpy, strcat, strncat, strspn, strcspn, strpbrk, strstr, strcasecmp,
strncasecmp
Each routine follows the same integration pattern as the mem* series: a
hand-written <func>-asm.S guarded by
__riscv_vector && __riscv_xlen == 64
&& !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED
with a small <func>.c that falls back to the existing scalar or generic
implementation when the guard is not satisfied, so non-vector RISC-V targets are
unaffected. For strlen, strcmp, and strcpy the existing hand-optimized scalar
code is kept as the fallback.
Page-boundary safety was a primary concern. Every scan reads its input(s) with
fault-only-first loads (vle8ff.v): element 0 is always loaded and a fault on any
later element merely truncates vl, so a vector load is never forced across a
page boundary into an unmapped page. The bounded routines (strnlen, strncmp,
strncpy, strncat, strncasecmp) additionally cap vl by the remaining count so
they never read past the n-th byte. The set routines (strspn, strcspn, strpbrk)
build a 256-byte table on the stack and gather through it with vluxei8.v, which
only touches the fully-mapped table. strstr combines a vle8ff scan for the
needle's first byte with a byte-wise verify that stops at a NUL, so neither
phase crosses past the end of either string.
Please note that strcasecmp/strncasecmp fold only the ASCII letters A-Z, i.e.
they implement the C/POSIX locale. Targets that need locale-aware folding fall
through the guard to the generic implementation. I'm happy to drop these two if
the locale divergence is unwelcome.
As with the mem* series, this is a fair amount of hand-written assembly to
maintain; other architectures rarely optimize the whole str* family. I'm glad
to trim the set to whatever subset is considered worth carrying.
Comments and suggestions welcome.
Best regards,
Pincheng Wang
Pincheng Wang (17):
riscv: add vectorized strlen
riscv: add vectorized strnlen
riscv: add vectorized strchr
riscv: add vectorized strchrnul
riscv: add vectorized strrchr
riscv: add vectorized strcmp
riscv: add vectorized strncmp
riscv: add vectorized strcpy
riscv: add vectorized strncpy
riscv: add vectorized strcat
riscv: add vectorized strncat
riscv: add vectorized strspn
riscv: add vectorized strcspn
riscv: add vectorized strpbrk
riscv: add vectorized strstr
riscv: add vectorized strcasecmp
riscv: add vectorized strncasecmp
newlib/libc/machine/riscv/Makefile.inc | 33 ++++++++++-
newlib/libc/machine/riscv/strcasecmp-asm.S | 65 +++++++++++++++++++++
newlib/libc/machine/riscv/strcasecmp.c | 5 ++
newlib/libc/machine/riscv/strcat-asm.S | 40 +++++++++++++
newlib/libc/machine/riscv/strcat.c | 5 ++
newlib/libc/machine/riscv/strchr-asm.S | 29 +++++++++
newlib/libc/machine/riscv/strchr.c | 5 ++
newlib/libc/machine/riscv/strchrnul-asm.S | 31 ++++++++++
newlib/libc/machine/riscv/strchrnul.c | 5 ++
newlib/libc/machine/riscv/strcmp-asm.S | 55 +++++++++++++++++
newlib/libc/machine/riscv/strcmp.S | 4 ++
newlib/libc/machine/riscv/strcpy-asm.S | 24 ++++++++
newlib/libc/machine/riscv/strcpy.c | 5 ++
newlib/libc/machine/riscv/strcspn-asm.S | 55 +++++++++++++++++
newlib/libc/machine/riscv/strcspn.c | 5 ++
newlib/libc/machine/riscv/strlen-asm.S | 25 ++++++++
newlib/libc/machine/riscv/strlen.c | 5 ++
newlib/libc/machine/riscv/strncasecmp-asm.S | 63 ++++++++++++++++++++
newlib/libc/machine/riscv/strncasecmp.c | 5 ++
newlib/libc/machine/riscv/strncat-asm.S | 47 +++++++++++++++
newlib/libc/machine/riscv/strncat.c | 5 ++
newlib/libc/machine/riscv/strncmp-asm.S | 51 ++++++++++++++++
newlib/libc/machine/riscv/strncmp.c | 5 ++
newlib/libc/machine/riscv/strncpy-asm.S | 46 +++++++++++++++
newlib/libc/machine/riscv/strncpy.c | 5 ++
newlib/libc/machine/riscv/strnlen-asm.S | 28 +++++++++
newlib/libc/machine/riscv/strnlen.c | 5 ++
newlib/libc/machine/riscv/strpbrk-asm.S | 55 +++++++++++++++++
newlib/libc/machine/riscv/strpbrk.c | 5 ++
newlib/libc/machine/riscv/strrchr-asm.S | 42 +++++++++++++
newlib/libc/machine/riscv/strrchr.c | 5 ++
newlib/libc/machine/riscv/strspn-asm.S | 51 ++++++++++++++++
newlib/libc/machine/riscv/strspn.c | 5 ++
newlib/libc/machine/riscv/strstr-asm.S | 55 +++++++++++++++++
newlib/libc/machine/riscv/strstr.c | 5 ++
35 files changed, 878 insertions(+), 1 deletion(-)
create mode 100644 newlib/libc/machine/riscv/strcasecmp-asm.S
create mode 100644 newlib/libc/machine/riscv/strcasecmp.c
create mode 100644 newlib/libc/machine/riscv/strcat-asm.S
create mode 100644 newlib/libc/machine/riscv/strcat.c
create mode 100644 newlib/libc/machine/riscv/strchr-asm.S
create mode 100644 newlib/libc/machine/riscv/strchr.c
create mode 100644 newlib/libc/machine/riscv/strchrnul-asm.S
create mode 100644 newlib/libc/machine/riscv/strchrnul.c
create mode 100644 newlib/libc/machine/riscv/strcmp-asm.S
create mode 100644 newlib/libc/machine/riscv/strcpy-asm.S
create mode 100644 newlib/libc/machine/riscv/strcspn-asm.S
create mode 100644 newlib/libc/machine/riscv/strcspn.c
create mode 100644 newlib/libc/machine/riscv/strlen-asm.S
create mode 100644 newlib/libc/machine/riscv/strncasecmp-asm.S
create mode 100644 newlib/libc/machine/riscv/strncasecmp.c
create mode 100644 newlib/libc/machine/riscv/strncat-asm.S
create mode 100644 newlib/libc/machine/riscv/strncat.c
create mode 100644 newlib/libc/machine/riscv/strncmp-asm.S
create mode 100644 newlib/libc/machine/riscv/strncmp.c
create mode 100644 newlib/libc/machine/riscv/strncpy-asm.S
create mode 100644 newlib/libc/machine/riscv/strncpy.c
create mode 100644 newlib/libc/machine/riscv/strnlen-asm.S
create mode 100644 newlib/libc/machine/riscv/strnlen.c
create mode 100644 newlib/libc/machine/riscv/strpbrk-asm.S
create mode 100644 newlib/libc/machine/riscv/strpbrk.c
create mode 100644 newlib/libc/machine/riscv/strrchr-asm.S
create mode 100644 newlib/libc/machine/riscv/strrchr.c
create mode 100644 newlib/libc/machine/riscv/strspn-asm.S
create mode 100644 newlib/libc/machine/riscv/strspn.c
create mode 100644 newlib/libc/machine/riscv/strstr-asm.S
create mode 100644 newlib/libc/machine/riscv/strstr.c
Comments
ack, this on my review queue :) Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn> 於 2026年7月16日週四 下午11:02寫道: > > Hi all, > > This series adds RISC-V Vector Extension (RVV) implementations for the str* > family in Newlib, as a follow-up to the earlier mem* work (memset, memcpy, > memmove, and then memccpy, memchr, memcmp, mempcpy, memrchr, rawmemchr). It > covers the following 17 interfaces: > > strlen, strnlen, strchr, strchrnul, strrchr, strcmp, strncmp, strcpy, > strncpy, strcat, strncat, strspn, strcspn, strpbrk, strstr, strcasecmp, > strncasecmp > > Each routine follows the same integration pattern as the mem* series: a > hand-written <func>-asm.S guarded by > > __riscv_vector && __riscv_xlen == 64 > && !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED > > with a small <func>.c that falls back to the existing scalar or generic > implementation when the guard is not satisfied, so non-vector RISC-V targets are > unaffected. For strlen, strcmp, and strcpy the existing hand-optimized scalar > code is kept as the fallback. > > Page-boundary safety was a primary concern. Every scan reads its input(s) with > fault-only-first loads (vle8ff.v): element 0 is always loaded and a fault on any > later element merely truncates vl, so a vector load is never forced across a > page boundary into an unmapped page. The bounded routines (strnlen, strncmp, > strncpy, strncat, strncasecmp) additionally cap vl by the remaining count so > they never read past the n-th byte. The set routines (strspn, strcspn, strpbrk) > build a 256-byte table on the stack and gather through it with vluxei8.v, which > only touches the fully-mapped table. strstr combines a vle8ff scan for the > needle's first byte with a byte-wise verify that stops at a NUL, so neither > phase crosses past the end of either string. > > > Please note that strcasecmp/strncasecmp fold only the ASCII letters A-Z, i.e. > they implement the C/POSIX locale. Targets that need locale-aware folding fall > through the guard to the generic implementation. I'm happy to drop these two if > the locale divergence is unwelcome. > > As with the mem* series, this is a fair amount of hand-written assembly to > maintain; other architectures rarely optimize the whole str* family. I'm glad > to trim the set to whatever subset is considered worth carrying. > > Comments and suggestions welcome. > > Best regards, > Pincheng Wang > > Pincheng Wang (17): > riscv: add vectorized strlen > riscv: add vectorized strnlen > riscv: add vectorized strchr > riscv: add vectorized strchrnul > riscv: add vectorized strrchr > riscv: add vectorized strcmp > riscv: add vectorized strncmp > riscv: add vectorized strcpy > riscv: add vectorized strncpy > riscv: add vectorized strcat > riscv: add vectorized strncat > riscv: add vectorized strspn > riscv: add vectorized strcspn > riscv: add vectorized strpbrk > riscv: add vectorized strstr > riscv: add vectorized strcasecmp > riscv: add vectorized strncasecmp > > newlib/libc/machine/riscv/Makefile.inc | 33 ++++++++++- > newlib/libc/machine/riscv/strcasecmp-asm.S | 65 +++++++++++++++++++++ > newlib/libc/machine/riscv/strcasecmp.c | 5 ++ > newlib/libc/machine/riscv/strcat-asm.S | 40 +++++++++++++ > newlib/libc/machine/riscv/strcat.c | 5 ++ > newlib/libc/machine/riscv/strchr-asm.S | 29 +++++++++ > newlib/libc/machine/riscv/strchr.c | 5 ++ > newlib/libc/machine/riscv/strchrnul-asm.S | 31 ++++++++++ > newlib/libc/machine/riscv/strchrnul.c | 5 ++ > newlib/libc/machine/riscv/strcmp-asm.S | 55 +++++++++++++++++ > newlib/libc/machine/riscv/strcmp.S | 4 ++ > newlib/libc/machine/riscv/strcpy-asm.S | 24 ++++++++ > newlib/libc/machine/riscv/strcpy.c | 5 ++ > newlib/libc/machine/riscv/strcspn-asm.S | 55 +++++++++++++++++ > newlib/libc/machine/riscv/strcspn.c | 5 ++ > newlib/libc/machine/riscv/strlen-asm.S | 25 ++++++++ > newlib/libc/machine/riscv/strlen.c | 5 ++ > newlib/libc/machine/riscv/strncasecmp-asm.S | 63 ++++++++++++++++++++ > newlib/libc/machine/riscv/strncasecmp.c | 5 ++ > newlib/libc/machine/riscv/strncat-asm.S | 47 +++++++++++++++ > newlib/libc/machine/riscv/strncat.c | 5 ++ > newlib/libc/machine/riscv/strncmp-asm.S | 51 ++++++++++++++++ > newlib/libc/machine/riscv/strncmp.c | 5 ++ > newlib/libc/machine/riscv/strncpy-asm.S | 46 +++++++++++++++ > newlib/libc/machine/riscv/strncpy.c | 5 ++ > newlib/libc/machine/riscv/strnlen-asm.S | 28 +++++++++ > newlib/libc/machine/riscv/strnlen.c | 5 ++ > newlib/libc/machine/riscv/strpbrk-asm.S | 55 +++++++++++++++++ > newlib/libc/machine/riscv/strpbrk.c | 5 ++ > newlib/libc/machine/riscv/strrchr-asm.S | 42 +++++++++++++ > newlib/libc/machine/riscv/strrchr.c | 5 ++ > newlib/libc/machine/riscv/strspn-asm.S | 51 ++++++++++++++++ > newlib/libc/machine/riscv/strspn.c | 5 ++ > newlib/libc/machine/riscv/strstr-asm.S | 55 +++++++++++++++++ > newlib/libc/machine/riscv/strstr.c | 5 ++ > 35 files changed, 878 insertions(+), 1 deletion(-) > create mode 100644 newlib/libc/machine/riscv/strcasecmp-asm.S > create mode 100644 newlib/libc/machine/riscv/strcasecmp.c > create mode 100644 newlib/libc/machine/riscv/strcat-asm.S > create mode 100644 newlib/libc/machine/riscv/strcat.c > create mode 100644 newlib/libc/machine/riscv/strchr-asm.S > create mode 100644 newlib/libc/machine/riscv/strchr.c > create mode 100644 newlib/libc/machine/riscv/strchrnul-asm.S > create mode 100644 newlib/libc/machine/riscv/strchrnul.c > create mode 100644 newlib/libc/machine/riscv/strcmp-asm.S > create mode 100644 newlib/libc/machine/riscv/strcpy-asm.S > create mode 100644 newlib/libc/machine/riscv/strcspn-asm.S > create mode 100644 newlib/libc/machine/riscv/strcspn.c > create mode 100644 newlib/libc/machine/riscv/strlen-asm.S > create mode 100644 newlib/libc/machine/riscv/strncasecmp-asm.S > create mode 100644 newlib/libc/machine/riscv/strncasecmp.c > create mode 100644 newlib/libc/machine/riscv/strncat-asm.S > create mode 100644 newlib/libc/machine/riscv/strncat.c > create mode 100644 newlib/libc/machine/riscv/strncmp-asm.S > create mode 100644 newlib/libc/machine/riscv/strncmp.c > create mode 100644 newlib/libc/machine/riscv/strncpy-asm.S > create mode 100644 newlib/libc/machine/riscv/strncpy.c > create mode 100644 newlib/libc/machine/riscv/strnlen-asm.S > create mode 100644 newlib/libc/machine/riscv/strnlen.c > create mode 100644 newlib/libc/machine/riscv/strpbrk-asm.S > create mode 100644 newlib/libc/machine/riscv/strpbrk.c > create mode 100644 newlib/libc/machine/riscv/strrchr-asm.S > create mode 100644 newlib/libc/machine/riscv/strrchr.c > create mode 100644 newlib/libc/machine/riscv/strspn-asm.S > create mode 100644 newlib/libc/machine/riscv/strspn.c > create mode 100644 newlib/libc/machine/riscv/strstr-asm.S > create mode 100644 newlib/libc/machine/riscv/strstr.c > > -- > 2.39.5 >
> > Each routine follows the same integration pattern as the mem* series: a > > hand-written <func>-asm.S guarded by > > > > __riscv_vector && __riscv_xlen == 64 > > && !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED Does any implementation not work on RV32 / __riscv_xlen == 32? I would suggest removing that limitation if the answer is no :) minor: some vectorized functions use xN where N > 16, that could lead to problems on RVE, e.g. strchr uses a6, which is not available on RVE, so I would suggest adding !__riscv_e for those functions. > > Please note that strcasecmp/strncasecmp fold only the ASCII letters A-Z, i.e. > > they implement the C/POSIX locale. Targets that need locale-aware folding fall > > through the guard to the generic implementation. I'm happy to drop these two if > > the locale divergence is unwelcome. I would prefer drop vectorized strcasecmp and strncasecmp since they do not handle the locale correctly.