From nobody Fri Oct 31 12:48:38 2025 X-Original-To: dev-commits-src-main@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 4cyglD0Q69z6Fm6g; Fri, 31 Oct 2025 12:48:40 +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 4cyglB6pPfz42d6; Fri, 31 Oct 2025 12:48:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761914919; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LD5PIcOPAYZ0O0nU7rSP9zGKMeibaciOF8QVC4lJh0g=; b=vZ4e4ttoSsy31zbbmjAj4IHGcZWhGXZEBciC+CwMHDawLWzn0dsJ+3MNo5PTMT1JvJs7b/ b/0x2gsDh2yzlvxLqDWy8OcrTRAFjVkTzeXHujkuspxqZIFuOjatMMTc6knNFhe8migjnY t5TIVTCZoPImT3N8UvCDGwdNd//PzAToFjM002N63B1C7OMqkyT5DuUYjGAIC60fRz2mSV cesgjNm6PWS1o6KVOhYRa4yEA9fntClMBkEPAHBJQATbuVWhJVOAKLKKbdLx+lCjtVNioW oATHUsHxpvnHXykw0VQKmFrW2oBaj5pt5jL0hZAKJW1czhrklGxY4HnzK0S2Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761914919; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LD5PIcOPAYZ0O0nU7rSP9zGKMeibaciOF8QVC4lJh0g=; b=q1Y0GWD+gCv40vMx1dcnA61KAMFCuv9o6UdBYDI6l/AVyhcK+oGKkslwHKiUBw8VrQb04v lPAFWvtoKSczfwRwAbh5/tehq6ffxeJclYIt9AtLN2zBmyWNKLG+sz5YXQqo5rnhXEVE4z AzWqqSB17XY3BelCLD7vbm71/ENIx9z7H6y0nq0pPcX+OJbBGOvIrmm0ygdLXUaZjMSshR 4AA03919715Vr1dWwoI4b9rcZmHSWPqymGSJuL9buDFcJHjqmXtAspkkMaQdaK30uRPLoo 08CmjzjV7HTPYQgWxBm+HPhjlCbFjPLGofbjjr6MS8QLX3GxX/Hg2XQO78neeg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1761914919; a=rsa-sha256; cv=none; b=azSFflpFRaPQqxz1fskfWeU84U28mty5l9iUNCFWfJORWSaxmmJJngKKiIxN+YYtZHg6Wc rK8xplVqPy1yyM9jGa05/3ow/gHAZTRd08XpF+Qj+2nxAuhByyqo8xyOKha+cmg+/9a7Od T/ymwWtZ7M85hUaCDzf4IZ7Mnu6lJe5O7GtVIOxbqMwnLDBAsx7w/77a8rnrUq9xmhCSGq 6DnLnDDegniBNKkOvFn2Yg4PM6hsm6LE0lroYLz3GZwBCEnz+JZ7ctnOMx8oCw+g+NG+55 WhY/RVKJiX6wYx0uIMAIvVCNT26VMKwI5REhWxqLaof4Nmu/AL+GgPXSGPF4pQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cyglB3S2fz17x8; Fri, 31 Oct 2025 12:48:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 59VCmckt042752; Fri, 31 Oct 2025 12:48:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 59VCmceD042749; Fri, 31 Oct 2025 12:48:38 GMT (envelope-from git) Date: Fri, 31 Oct 2025 12:48:38 GMT Message-Id: <202510311248.59VCmceD042749@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 5a52f0704435 - main - libc: scalar strnlen() in RISC-V assembly List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 5a52f0704435b089199201be0029e0d7c9ef2fce Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=5a52f0704435b089199201be0029e0d7c9ef2fce commit 5a52f0704435b089199201be0029e0d7c9ef2fce Author: Strahinja Stanišić AuthorDate: 2024-08-04 15:12:00 +0000 Commit: Robert Clausecker CommitDate: 2025-10-31 12:47:59 +0000 libc: scalar strnlen() in RISC-V assembly Optimized implementation of strnlen() in RISC-V assembly Performance was measured using strperf on a HiFive Unmatched (SiFive HF105-001) board. os: FreeBSD arch: riscv │ strnlen_baseline │ strnlen_scalar │ │ sec/op │ sec/op vs base │ Short 787.0µ ± 0% 430.9µ ± 1% -45.24% (p=0.000 n=20) Mid 621.6µ ± 0% 195.1µ ± 1% -68.61% (p=0.000 n=20) Long 569.4µ ± 1% 100.6µ ± 0% -82.34% (p=0.000 n=20) geomean 653.1µ 203.7µ -68.81% │ strnlen_baseline │ strnlen_scalar │ │ MiB/s │ MiB/s vs base │ Short 158.8 ± 0% 290.1 ± 1% +82.62% (p=0.000 n=20) Mid 201.1 ± 0% 640.6 ± 1% +218.59% (p=0.000 n=20) Long 219.5 ± 1% 1242.9 ± 0% +466.19% (p=0.000 n=20) geomean 191.4 613.5 +220.57% MFC after: 1 month MFC to: stable/15 Approved by: mhorne, markj (mentor) Reviewed by: fuz, Jari Sihvola Sponsored by: Google LLC (GSoC 2024) Differential Revision: https://reviews.freebsd.org/D46230 --- lib/libc/riscv/string/Makefile.inc | 1 + lib/libc/riscv/string/strnlen.S | 143 +++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/lib/libc/riscv/string/Makefile.inc b/lib/libc/riscv/string/Makefile.inc index ebea8d1d3412..4b97490a5494 100644 --- a/lib/libc/riscv/string/Makefile.inc +++ b/lib/libc/riscv/string/Makefile.inc @@ -3,4 +3,5 @@ MDSRCS+= \ memcpy.S \ memset.S \ strlen.S \ + strnlen.S \ strrchr.S diff --git a/lib/libc/riscv/string/strnlen.S b/lib/libc/riscv/string/strnlen.S new file mode 100644 index 000000000000..c0fd959548ff --- /dev/null +++ b/lib/libc/riscv/string/strnlen.S @@ -0,0 +1,143 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic + */ + +#include + +/* + * a0 - const char *s + * a1 - size_t maxlen; + */ +ENTRY(strnlen) + /* + * a0 - const char *s; + * a1 - size_t maxlen; + * a2 - uint64_t *ptr; + * a3 - char iter[8]; + * a4 - uint64_t *end_align; + * a5 - uint64_t *end_unroll; + */ + + beqz a1, .Lnot_found + + /* ptr = s & ~0b111 */ + /* t0 = 0x0101010101010101 */ + /* t1 = 0x8080808080808080 */ + /* end_align = (s + maxlen + 7) & ~0b111 */ + /* mask_start = t0 >> ((-s.value) << 3) */ + add a4, a0, a1 + li t0, 0x01010101 + addi a4, a4, 7 + slli t1, t0, 32 + neg t2, a0 + andi a4, a4, ~0b111 + or t0, t0, t1 + slli t2, t2, 3 + andi a2, a0, ~0b111 + slli t1, t0, 7 + srl t2, t0, t2 + + /* if pointer is aligned skip to loop */ + beq a0, a2, .Lskip_start + + /* iter = *ptr */ + ld a3, (a2) + + /* iter = iter | mask_start */ + or a3, a3, t2 + + /* has_zero */ + not t2, a3 + sub a3, a3, t0 + and t2, t2, t1 + and a3, a3, t2 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + +.Lskip_start: + /* end_unroll */ + sub t2, a4, a2 + andi t2, t2, ~0b1111 + add a5, a2, t2 + + /* while (ptr != end_unroll) */ + beq a2, a5, .Lskip_loop +.Lloop: + ld a3, (a2) + ld a6, 8(a2) + + /* has_zero */ + not t2, a3 + not t3, a6 + sub a3, a3, t0 + sub a6, a6, t0 + and t2, t2, t1 + and t3, t3, t1 + and a3, a3, t2 + and a6, a6, t3 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + + mv a3, a6 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + + bne a2, a5, .Lloop + +.Lskip_loop: + + beq a2, a4, .Lnot_found + + ld a3, (a2) + + /* has_zero */ + not t2, a3 + sub a3, a3, t0 + and t2, t2, t1 + and a3, a3, t2 + + + addi a2, a2, 8 + beqz a3, .Lnot_found + +.Lfind_zero: + + /* move ptr back */ + addi a2, a2, -8 + + /* isolate lowest set bit */ + neg t0, a3 + and a3, a3, t0 + + li t0, 0x0001020304050607 + srli a3, a3, 7 + + /* lowest set bit is 2^(8*k) + * multiplying by it shifts the idx array in t0 by k bytes to the left */ + mul a3, a3, t0 + + /* highest byte contains idx of first zero */ + srli a3, a3, 56 + + /* zero_idx */ + sub a2, a2, a0 + add a2, a2, a3 + + /* min(zero_idx, maxlen) */ + sub a2, a2, a1 + srai t1, a2, 63 + and a2, a2, t1 + add a0, a1, a2 + + ret + +.Lnot_found: + mv a0, a1 + ret + +END(strnlen)