From nobody Sun Nov 30 01:45:59 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dJqcl5r89z6JfDc for ; Sun, 30 Nov 2025 01:45:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dJqcl34fyz3mhN for ; Sun, 30 Nov 2025 01:45:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1764467159; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6diB2hHvaOOPB2XdMHY2fvgdHJI0CpUp65t8XUPG97k=; b=etwQI1hYi3/87OExW4SUW3LjkqnPmsjk/kJ4VXyOaHhM3QdtcoYqamfwe3pT3uHDk3/RNu ij2F1MRSTrSZRAEWuOyOJZMFt1vHLaDPBmcclyDPItvLaDu705xu4Zw2TjhYe5wnVMEpux SH0UNyFX7N1RgV2y7/IIMmVd+BfRh1YEwCOnpuHci2hw6TEeXU4T0ZOgpGRzwrhWB7l7EL Rx0a0tk5mJKBA3jSb0dFUJOlsXKe0cJE/H7WFTH1KQDE+pvdkiKsx6kKJrds08bAafHSQn NmeheUu48TxZa9iAMy4jhwGMUKEMeZsbuneDXWr9s4eI44/gjGxZ5XqsukGGWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1764467159; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6diB2hHvaOOPB2XdMHY2fvgdHJI0CpUp65t8XUPG97k=; b=F6DcMFqfMP0Kwp+l6+EBDTXkM4lI9pbDNXq2sWdkOCEiVvBlWkI9CIw6fQEUhgoHtaN7jL eZQeXQPR5q/iDSIZ0o3co+KAh4AExNm0/xsJpqBN1+qjTAA0FZvHqm2qWMtuu9I2sF+JgU JecpWIda8efVWzXnweShRDmQrONMxPb9UfZ16NwwK9OhsMCwP044FHLtdZRT0qxfo8o/gS ATkaMln4tUEs2z2mkHtbMxXSAPJ2pKvjJy06BlkA++1xU6+4UOJcDMZ5yArvao70Jltnhp OizoLKzh7EkBNKUmXZRgnFtwmHPcC3/yeMXzRYsG2ckiD9DtC5EI1/qQjkiUDA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1764467159; a=rsa-sha256; cv=none; b=HuOD237z6qj1L1Pm4a3zmfgkE9XrNUTulPNK5zSs5JmVbw63IUtYelIa5N+Ru78YXHE/6k 1LP9fC8l9Aqa9CwbJgD30ayVw3OSdchDjWZASoH/vARji+o+WbCQPs3zha0Q9EF/GKPvrw 8QdIKAYY8EHCWqfQNzmt/h72+MPZAckQSTgNNvvv1AEWOteHmlxo7QMs8xdFqAz/TeXlJa U8dHdfqqxsv1gbL2xvJshsjot5kndu1QFJ7Qg49tzd13Xd1haLzA/SDYrvugUgjjpOlRlc 7LxM0JOOGmn7JwGWdNssp9tudX5Xm2Aqa3DZkDORlDEzTLPJvHwBmxjP9kbSyA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dJqcl2Hw6zR9 for ; Sun, 30 Nov 2025 01:45:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 28665 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 30 Nov 2025 01:45:59 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Cc: Strahinja =?utf-8?Q?Stani=C5=A1?==?utf-8?Q?i=C4=87?= From: Robert Clausecker Subject: git: f56c68448d42 - stable/15 - libc: scalar strchrnul() in RISC-V assembly List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: f56c68448d429687a81ce4d97b16e7b9d7c57808 Auto-Submitted: auto-generated Date: Sun, 30 Nov 2025 01:45:59 +0000 Message-Id: <692ba1d7.28665.9efa142@gitrepo.freebsd.org> The branch stable/15 has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=f56c68448d429687a81ce4d97b16e7b9d7c57808 commit f56c68448d429687a81ce4d97b16e7b9d7c57808 Author: Strahinja Stanišić AuthorDate: 2024-07-19 17:58:04 +0000 Commit: Robert Clausecker CommitDate: 2025-11-30 00:43:06 +0000 libc: scalar strchrnul() in RISC-V assembly Scalar implementation of strchrnul() in RISC-V assembly and changes to the corresponding manpage. Performance was benchmarked on a HiFive Unmatched (SiFive HF105-001) board using: https://github.com/clausecker/strperf os: FreeBSD arch: riscv │ strchrnul_baseline │ strchrnul_scalar │ │ sec/op │ sec/op vs base │ Short 680.2µ ± 5% 435.3µ ± 0% -36.01% (p=0.000 n=20) Mid 314.7µ ± 3% 221.4µ ± 0% -29.63% (p=0.000 n=20) Long 152.3µ ± 0% 138.5µ ± 0% -9.08% (p=0.000 n=20) geomean 319.5µ 237.2µ -25.75% │ strchrnul_baseline │ strchrnul_scalar │ │ MiB/s │ MiB/s vs base │ Short 183.8 ± 5% 287.2 ± 0% +56.27% (p=0.000 n=20) Mid 397.3 ± 3% 564.6 ± 0% +42.12% (p=0.000 n=20) Long 820.5 ± 0% 902.5 ± 0% +9.99% (p=0.000 n=20) geomean 391.3 527.0 +34.68% MFC after: 1 month MFC to: stable/15 Approved by: markj (mentor) Reviewed by: fuz Sponsored by: Google LLC (GSoC 2024) Differential Revision: https://reviews.freebsd.org/D46047 (cherry picked from commit 08af0bbc9c7d71bbaadb31ad31f8492f40537c5c) --- lib/libc/riscv/string/Makefile.inc | 1 + lib/libc/riscv/string/strchrnul.S | 116 +++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/lib/libc/riscv/string/Makefile.inc b/lib/libc/riscv/string/Makefile.inc index 4b97490a5494..719f22f6077f 100644 --- a/lib/libc/riscv/string/Makefile.inc +++ b/lib/libc/riscv/string/Makefile.inc @@ -4,4 +4,5 @@ MDSRCS+= \ memset.S \ strlen.S \ strnlen.S \ + strchrnul.S \ strrchr.S diff --git a/lib/libc/riscv/string/strchrnul.S b/lib/libc/riscv/string/strchrnul.S new file mode 100644 index 000000000000..8abba71c4199 --- /dev/null +++ b/lib/libc/riscv/string/strchrnul.S @@ -0,0 +1,116 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic + */ + +#include + + .weak strchrnul + .set strchrnul, __strchrnul + +/* + * a0 - const char *str + * a1 - int c; + */ +ENTRY(__strchrnul) + /* + * a0 - const char *ptr; + * a1 - char cccccccc[8]; + * a2 - char iter[8]; + * a3 - char mask_end + */ + + /* int to char */ + andi a1, a1, 0xFF + + /* t0 = 0x0101010101010101 */ + li t0, 0x01010101 + slli t1, t0, 32 + or t0, t0, t1 + + /* t1 = 0x8080808080808080 */ + slli t1, t0, 7 + + /* spread char across bytes */ + mul a1, a1, t0 + + /* align_offset */ + andi t2, a0, 0b111 + + /* align pointer */ + andi a0, a0, ~0b111 + + /* if pointer is aligned skip to loop */ + beqz t2, .Lloop + + ld a2, (a0) + + /* mask_start calculation */ + slli t2, t2, 3 + neg t2, t2 + srl t2, t0, t2 + + /* fill bytes before start with non-zero */ + or a3, a2, t2 + + xor a2, a2, a1 + or a2, a2, t2 + + /* has_zero for \0 */ + not t3, a3 + not t2, a2 + sub a3, a3, t0 + sub a2, a2, t0 + and a3, a3, t3 + and a2, a2, t2 + and a3, a3, t1 + and a2, a2, t1 + + + /* if \0 or c was found, exit */ + or a2, a2, a3 + addi a0, a0, 8 + bnez a2, .Lfind_char + + +.Lloop: + ld a2, (a0) + + /* has_zero for both \0 or c */ + xor a3, a2, a1 + + not t2, a2 + not t3, a3 + sub a2, a2, t0 + sub a3, a3, t0 + and a2, a2, t2 + and a3, a3, t3 + and a2, a2, t1 + and a3, a3, t1 + + /* if \0 or c was found, exit */ + or a2, a2, a3 + addi a0, a0, 8 + beqz a2, .Lloop + +.Lfind_char: + addi a0, a0, -8 + + /* isolate lowest set bit */ + neg t0, a2 + and a2, a2, t0 + + li t0, 0x0001020304050607 + srli a2, a2, 7 + + /* lowest set bit is 2^(8*k) + * multiplying by it shifts the idx array in t0 by k bytes to the left */ + mul a2, a2, t0 + + /* highest byte contains idx of first zero */ + srli a2, a2, 56 + + add a0, a0, a2 + ret +END(__strchrnul)