From nobody Sun Feb 8 11:45:43 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84D7638FEF5 for ; Tue, 13 Jan 2026 12:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768308198; cv=none; b=kSm+wMe0DTmrm73d9bEkRcWfPNbjE82nU/ojafc+i4JMv0X7YzFDoWfGGDItx8KqRT4qoy/nwp9G0lc8Kb9w20L1LmqDqh6vErpyY89SoZYexjtaKFOVZQ/XSNedacu6b2bxyYFR7I2Y424IhFIMwP32a/vj8mJMHhrOzRyCXGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768308198; c=relaxed/simple; bh=jy33/Tc52BzbW3MkRv/+6XiOGSKTPkOy55r39xq3KKA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BgqTQhAcq87iWaDMI+Y1LEVCJsHJaug9ZrMvxy1rCWhVlnB1LErzFsCjw+2IpmEOvMb+Zzs763BXxGHSjzR6Ipu5Sbo2y0tp/CP4DPWwoyDkHNaaiY5BxnVOQi26m52oX4p7ot+1i6USstsznJVYVg4eMfoE2VF/28oFQbjseUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uG9oa/Hx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uG9oa/Hx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68582C16AAE; Tue, 13 Jan 2026 12:43:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768308198; bh=jy33/Tc52BzbW3MkRv/+6XiOGSKTPkOy55r39xq3KKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uG9oa/Hxj5wlTi9C1TTXNuar1ADxBHwTZ9/+NKGpAPP0iWkeGAQC0MD0s7ueD369n rmxQny8MbrK8k202M8b+53cwRA9XBgV+14xrR6C5n6Ucrr+8KHSHyYQy+IIT260BTI 1mR6Em0EXndaYD8d52tQ5uzCkCzOwCipUYiQdIXs5+dw1EiBZULKL5TTJ0wBv+muc3 SX2Nl1h/Zc+DR4XhEczZhsbsINYwsA/YBWNRbyYWUV7PpwXEDMQQ7mbwy4MK7oefAn wR4EWBYHiTqspb4vIpDxlYu4yOxgwqFwbVmPIU9ekDXPSIEKFl0m14pDbDUOumyLXp F8sh41y6Erkxg== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] riscv: word-at-a-time: improve find_zero() without Zbb Date: Tue, 13 Jan 2026 20:24:56 +0800 Message-ID: <20260113122457.27507-3-jszhang@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260113122457.27507-1-jszhang@kernel.org> References: <20260113122457.27507-1-jszhang@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Previous commit improved the find_zero() performance for !RISCV_ISA_ZBB. What about RISCV_ISA_ZBB=3Dy but the HW doesn't support Zbb? We have the same heavy generic fls64() issue. Let's improve this situation by checking Zbb extension and fall back to generic count_masked_bytes() if Zbb isn't supported. To remove non-necessary zero bits couting on RV32, we also replace the 'fls64(mask) >> 3' with '!mask ? 0 : ((__fls(mask) + 1) >> 3);' We will get similar performance improvement as previous commit for RISCV_ISA_ZBB=3Dy but HW doesn't support Zbb. Signed-off-by: Jisheng Zhang --- arch/riscv/include/asm/word-at-a-time.h | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/word-at-a-time.h b/arch/riscv/include/a= sm/word-at-a-time.h index 0c8a9b337f93..ca3d30741ed1 100644 --- a/arch/riscv/include/asm/word-at-a-time.h +++ b/arch/riscv/include/asm/word-at-a-time.h @@ -42,9 +42,36 @@ static inline unsigned long create_zero_mask(unsigned lo= ng bits) return bits >> 7; } =20 +#ifdef CONFIG_64BIT +/* + * Jan Achrenius on G+: microoptimized version of + * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56" + * that works for the bytemasks without having to + * mask them first. + */ +static inline long count_masked_bytes(unsigned long mask) +{ + return mask*0x0001020304050608ul >> 56; +} + +#else /* 32-bit case */ + +/* Carl Chatfield / Jan Achrenius G+ version for 32-bit */ +static inline long count_masked_bytes(long mask) +{ + /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */ + long a =3D (0x0ff0001+mask) >> 23; + /* Fix the 1 for 00 case */ + return a & mask; +} +#endif + static inline unsigned long find_zero(unsigned long mask) { - return fls64(mask) >> 3; + if (riscv_has_extension_likely(RISCV_ISA_EXT_ZBB)) + return !mask ? 0 : ((__fls(mask) + 1) >> 3); + + return count_masked_bytes(mask); } =20 /* The mask we created is directly usable as a bytemask */ --=20 2.51.0