From nobody Fri Oct 31 12:48:29 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 4cygl21c5Jz6FmRD; Fri, 31 Oct 2025 12:48:30 +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 4cygl14wS4z42Qf; Fri, 31 Oct 2025 12:48:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761914909; 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=DkfuTKTQQMbOdniLvydocHvXn+FV4HPgdipfuUXoGF4=; b=Nzwbr2/n8jVfwyfU0vnkH1g7Tbr31AWUfoJfyCDMpIHg/49JRQ/Bwp8gZWvdKeuSn3Qsm1 HKogqh4Uhp+46HLTxDzj8CEwuyZQT1Ckc/m85WgWwgV62hx4RJZIPXwwTHUyQbRNYDNolw TZm8QaMTnu4qgNnxOg8EA3OBwkowkWIAXzxLGAPmMlJ8200TTChA0zvdcxjz+L6rTLbKd7 oQ49sxOONw5FEICTyONTkH+AD4uIItdXcVVu9LSRw8MHZG1Ym3KHJa124BII1fm+TKrGl5 YNiXD1tMK/yy55+AB8WiS8HYAwhj5paLJkBoidfnibdhPdGndKwLwQOwF5Efaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1761914909; 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=DkfuTKTQQMbOdniLvydocHvXn+FV4HPgdipfuUXoGF4=; b=kGqsTIZCdNjCfUpVyuu0jecO8RvWLos+pUe0J8++6nNGHGSJR6WIco63ywgznCQQqnrTud oZ+9ldVv1tLXlfnqcHVLK72phMc9NqQWiLxoD8QkdYL+G15TmpJFVBavg7EHfd2yJyZeHK kEOOJIaojleRbMCgcofbp5ftVuJ9/90tdsn6L0+Yin5xAU4qshqJL+Se1knbMQ5AiGO5nA NU6NPvUflrtlV8AYSH04v1/Vi9XzdDCXTKfZz/ynPxnclnD0uD8U9MzSHUhooqEWpFM5+x fFIEngwDOzzOEQuM6Dtqy32OSar4nLoyzAtoeYOFgb2z+R9be8rkQiAFCvHJ+Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1761914909; a=rsa-sha256; cv=none; b=VEMhUbx2zxlVehRECfAbRznVPHdmuS41Q20mXirwiRXqNDdUlGVQ4ziJhWJl8wCCwRqkhU Hqu6apdg1lI+S7bxd1c7qARcmn7CuuTHeVCMNV7ismOF4ENRcukgyrh5qtiFjnX+QPEi6I BLqjW29ykOb6NXla8J1dF+S+GmF1V5E8c7EbrWUR9yUmI+ENFFxgGgcF2LJbGX2b80Hsj0 VRCW3VwjHO0AOerEU19RPf/IC9s5Bx5N9QP+YmVYj4Rr138UAwleUQHQvAhWci5tbh6SmW CtUhW/dyW5mIb52g/ItOeWMaYNLLbO2MdUpa3Z8pobgDFUrhxqiLPrSt/I5bGA== 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 4cygl13rc0z17v0; Fri, 31 Oct 2025 12:48:29 +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 59VCmTmf042458; Fri, 31 Oct 2025 12:48:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 59VCmTRd042455; Fri, 31 Oct 2025 12:48:29 GMT (envelope-from git) Date: Fri, 31 Oct 2025 12:48:29 GMT Message-Id: <202510311248.59VCmTRd042455@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: 563efdd3bd5d - main - libc: scalar memchr() 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/main X-Git-Reftype: branch X-Git-Commit: 563efdd3bd5d5f94e356444bb64fd66e13dda5e1 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=563efdd3bd5d5f94e356444bb64fd66e13dda5e1 commit 563efdd3bd5d5f94e356444bb64fd66e13dda5e1 Author: Strahinja Stanišić AuthorDate: 2024-07-17 11:19:52 +0000 Commit: Robert Clausecker CommitDate: 2025-10-31 12:47:58 +0000 libc: scalar memchr() in RISC-V assembly Added an optimized memchr() implementation in RISC-V assembly and updated the relevant manpage. │ memchr_baseline │ memchr_scalar │ │ sec/op │ sec/op vs base │ Short 636.6µ ± 1% 495.9µ ± 1% -22.10% (p=0.000 n=20) Mid 279.7µ ± 1% 224.1µ ± 1% -19.87% (p=0.000 n=20) Long 138.8µ ± 0% 124.9µ ± 0% -10.00% (p=0.000 n=20) geomean 291.3µ 240.3µ -17.48% │ memchr_baseline │ memchr_scalar │ │ B/s │ B/s vs base │ Short 187.3Mi ± 1% 240.4Mi ± 1% +28.37% (p=0.000 n=20) Mid 426.2Mi ± 1% 531.9Mi ± 1% +24.79% (p=0.000 n=20) Long 859.0Mi ± 0% 954.4Mi ± 0% +11.11% (p=0.000 n=20) geomean 409.3Mi 496.0Mi +21.19% MFC after: 1 month MFC to: stable/15 Approved by: mhorne, markj (mentor) Reviewed by: fuz Sponsored by: Google LLC (GSoC 2024) Differential Revision: https://reviews.freebsd.org/D46023 --- lib/libc/riscv/string/Makefile.inc | 1 + lib/libc/riscv/string/memchr.S | 188 +++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) diff --git a/lib/libc/riscv/string/Makefile.inc b/lib/libc/riscv/string/Makefile.inc index a9cf8bf52481..cdbc19d286fc 100644 --- a/lib/libc/riscv/string/Makefile.inc +++ b/lib/libc/riscv/string/Makefile.inc @@ -1,2 +1,3 @@ MDSRCS+= \ + memchr.S \ strrchr.S diff --git a/lib/libc/riscv/string/memchr.S b/lib/libc/riscv/string/memchr.S new file mode 100644 index 000000000000..e6e04bfae96a --- /dev/null +++ b/lib/libc/riscv/string/memchr.S @@ -0,0 +1,188 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic + */ + +#include + +/* + * a0 - const void *b + * a1 - int c + * a2 - size_t len + */ +ENTRY(memchr) + /* + * a0 - const char *ptr + * a1 - char cccccccc[8] + * a2 - char iter[8] + * a3 - uint8_t *end + * a4 - uint64_t *end_align + * a5 - uint64_t *end_unroll + */ + + beqz a2, .Lno_match + + /* c = (uint8_t) c */ + andi a1, a1, 0xFF + + /* + * t0 = 0x0101010101010101 + * t1 = 0x8080808080808080 + * t2 = b << 3 + * cccccccc = (uint8_t)c * t0 + * end = b + len; + * ptr = b & ~0b111 + */ + add a3, a0, a2 + li t0, 0x01010101 + sltu t2, a0, a3 + slli t1, t0, 32 + neg t2, t2 + or t0, t0, t1 + and a3, a3, t2 + slli t1, t0, 7 + slli t2, a0, 3 + and a0, a0, ~0b111 + mul a1, t0, a1 + + ld a2, (a0) + + /* + * mask_start = REP8_0x01 ^ (REP8_0x01 << t2) + * iter = iter ^ cccccccc + * iter = iter | mask_start + */ + sll t2, t0, t2 + xor a2, a2, a1 + xor t2, t2, t0 + or a2, a2, t2 + + /* has_zero(iter) + * end_align = (end + 7) & ~0b111; + */ + addi a4, a3, 7 + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + andi a4, a4, ~0b111 + and a2, a2, t2 + + /* ptr = ptr + 8 */ + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + /* if(ptr == end_align) */ + beq a0, a4, .Lno_match + + /* end_unroll = end_align & ~0b1111 */ + andi a5, a4, ~0b1111 + + /* + * Instead of branching to check if `ptr` is 16-byte aligned: + * - Probe the next 8 bytes for `c` + * - Align `ptr` down to the nearest 16-byte boundary + * + * If `ptr` was already 16-byte aligned, those 8 bytes will be + * checked again inside the unrolled loop. + * + * This removes an unpredictable branch and improves performance. + */ + + ld a2, (a0) + xor a2, a2, a1 + + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + and a2, a2, t2 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + andi a0, a0, ~0b1111 + + /* while(ptr != end_unroll) */ + beq a0, a5, .Lskip_loop +.Lloop: + ld a2, (a0) + ld t3, 8(a0) + + xor a2, a2, a1 + xor t3, t3, a1 + + not t2, a2 + not t4, t3 + sub a2, a2, t0 + sub t3, t3, t0 + and t2, t2, t1 + and t4, t4, t1 + and a2, a2, t2 + and t3, t3, t4 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + /* move into iter for find_zero */ + mv a2, t3 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + bne a0, a5, .Lloop +.Lskip_loop: + + /* there might be one 8byte left */ + beq a0, a4, .Lno_match + + ld a2, (a0) + xor a2, a2, a1 + + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + and a2, a2, t2 + + addi a0, a0, 8 + + beqz a2, .Lno_match + +.Lfind_zero: + /* + * ptr = ptr - 8 + * t1 = 0x0001020304050607 + * iter = iter & (-iter) + * iter = iter >> 7 + * iter = iter * t1 + * iter = iter >> 56 + */ + li t1, 0x10203000 + neg t0, a2 + slli t1, t1, 4 + and a2, a2, t0 + addi t1, t1, 0x405 + srli a2, a2, 7 + slli t1, t1, 16 + addi a0, a0, -8 + addi t1, t1, 0x607 + mul a2, a2, t1 + srli a2, a2, 56 + + /* left = end - ptr */ + sub t0, a3, a0 + + /* return iter < left ? ptr + iter : NULL */ + sltu t1, a2, t0 + neg t1, t1 + add a0, a0, a2 + and a0, a0, t1 + ret + +.Lno_match: + li a0, 0 + ret +END(memchr)