From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7D272D1931 for ; Wed, 15 Apr 2026 11:32:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776252778; cv=none; b=fz6gACeX2d1k86oY7TUPO9N0JFrLa6PneC6uA4/FRhJw+GnHifOx50ElM5jxu8qjIg2bchu6HXEbF2Y8XK+X4yRtkzUPrd/2ljkQ8cg9h9xm0VUkbY0inpqDM8ghFbVkPtZf76+CcyxsJ7akWdFGC7e4dwuv1NQ7geGVLtxgp3Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776252778; c=relaxed/simple; bh=gR41Pja/K6i9C53vPaLlSLrKK2cEtWKsCTH/bEJcXqs=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=k6P49KM35P2f9Mk8K6f4kip4vxewoaUg4cAlVnNmUXan6EsMnkTVOo+Ja0dQKh6lkMjHmFQQTXd5C02QZ9tksZCuodC9C8asCdDPI2Pu/MDYn+vrumGALw+SOG6PNdT58d8EGOvB+uyEeVC+XY3AK4TdjE0o5zEzE9PuZ794yf0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lhpRK7eo; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lhpRK7eo" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-488aa77a06eso109678475e9.0 for ; Wed, 15 Apr 2026 04:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776252775; x=1776857575; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=u2FCUrZRaW+mZzvA8FAeIT0/MW/6T/TDKPdP+BgE9zU=; b=lhpRK7eo5WnGzcitBuuWB3DYG+NIzVP3cLUDy+5EG0ZmBVUY2oUfDbaemslF09+Bpg 1YIzthqzBnk3+EthgXR3dNx2RUZVU5q+amG+pteQOay6e0qp+j69ZVh4pG/edy/AfTS2 SDtDMXBNq56nbH5+XTnb4cCIhtxbvx+ZgOgi4c8XDpSbGNwOBctoUJYfHiC958nkmzoj CcJtb890r9tlcjGFEDEqjNDEjYh4tHqtmjB2f9tZ3xgUovZFX0qiTFhHHSTD2ct6yJYh PlE88eayMn3PQyWVot1UIlKiTh+3OEZ3YwELN8KJ5yhl2r87l714yI5Xzx1IeujbDkt/ AJIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776252775; x=1776857575; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=u2FCUrZRaW+mZzvA8FAeIT0/MW/6T/TDKPdP+BgE9zU=; b=D61ik9sXimYjRqNZtiJRnXKUGm2hcitsKjvIR/EWScEUk88CsmLlhVc/VtIvHf/d2D W5WDL2WEq9uTUeuFALBf0LmCve1JfehOSvlEgxpG2QU2j4BYYsxRjH6rAbS/kT4Gp3YQ LhdJmijzbjgiQJUzYuNib+C3ehrh9XiKhsekbRAY/fSdLDd3DYwYBpvzRc1ycBGIpPxt Rzx138VSwnVYvnJqq/SbioscZp1olveIHmD0Ppv8XjOl+WpuKIO04HRdTqax2QiAuwnC xPpX3ZxP3b6niTkqmA8HqVFCBtIvdDakCKd6N7BcYCRZUd75KdbNCP/xZFjnl2nap93C CIOQ== X-Forwarded-Encrypted: i=1; AFNElJ+5cGaRXEFudF51rZGMFkO3khXSouRNeXyXHiWYf1W8XSpcWNfXHvSzX42vEi/zKvcxx818TKU5jOGdzug=@vger.kernel.org X-Gm-Message-State: AOJu0YwWWZNPwx7zZDqG0vZfrAfXfZOXnQlEBBOEk2uoSyT8XWibtdOw 5IKEohVXJ9pnlVzOvaHUjAHyLwHrv70P4v3+McUWMI8KLgTufAPuBjH2 X-Gm-Gg: AeBDiesWSkcGu0bd+G3wOhXrWq9yWlIoPv/CDjjmKfS/8pWLnQlL4C7WyYn5970JXxJ DubXVf2pZCEkHW4wgrBS4/0KCt9z8U8RzEEf/YEHJ2OVCy0f2/Vuy1ApR4MMTWVa8LEIXLQnOjZ A5kueO76KWa5vbs7DWA1KFNIk6jFwdVzdGDMzEYeuVaFXuPT68EdO134/4cM9wQozolSYvI5ZUv 5XPiBwRBYFGcfezprWUSD5Gdiux+vdOCmE8tC2MHsRgs28C5y+d0AZ/6Mdg/w0ZfjQYAWo1ET4o GtgH/lCG/lXsxTklx4nnc3uNr+E4uwMrfaqKCzgogNKnyXqDB3Vr384fzKdn5eSwZIhqgr8pK20 DsRSYGTGkafmCHLJJ2WOqC2acZqfkb/bVOe0bmXC26yztCObdnenFr6edsEtySDVrI+hio5z4dS 4K/zmQPhc6KkaDUqnywjBeVuc5CV5lk0nkcXOnZbSpZvRqvSaxTkNGzcBXasYgm2tq X-Received: by 2002:a05:6000:240e:b0:43e:aefa:db84 with SMTP id ffacd0b85a97d-43eaefadc12mr935013f8f.34.1776252775085; Wed, 15 Apr 2026 04:32:55 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43ead3e00b3sm4261659f8f.27.2026.04.15.04.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2026 04:32:54 -0700 (PDT) Date: Wed, 15 Apr 2026 12:32:52 +0100 From: David Laight To: Jinjie Ruan Cc: , , , , , , , , , , Subject: Re: [PATCH v2 2/2] arch/riscv: Add bitrev.h file to support rev8 and brev8 Message-ID: <20260415123252.017bd5e2@pumpkin> In-Reply-To: <20260415093827.2776328-3-ruanjinjie@huawei.com> References: <20260415093827.2776328-1-ruanjinjie@huawei.com> <20260415093827.2776328-3-ruanjinjie@huawei.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 15 Apr 2026 17:38:27 +0800 Jinjie Ruan wrote: > The RISC-V Bit-manipulation Extension for Cryptography (Zbkb) provides > the 'brev8' instruction, which reverses the bits within each byte. > Combined with the 'rev8' instruction (from Zbb or Zbkb), which reverses > the byte order of a register, we can efficiently implement 16-bit, > 32-bit, and (on RV64) 64-bit bit reversal. > > This is significantly faster than the default software table-lookup > implementation in lib/bitrev.c, as it replaces memory accesses and > multiple arithmetic operations with just two or three hardware > instructions. > > Select HAVE_ARCH_BITREVERSE and provide to utilize > these instructions when the Zbkb extension is available at runtime > via the alternatives mechanism. > > Signed-off-by: Jinjie Ruan > --- > arch/riscv/Kconfig | 1 + > arch/riscv/include/asm/bitrev.h | 41 +++++++++++++++++++++++++++++++++ > 2 files changed, 42 insertions(+) > create mode 100644 arch/riscv/include/asm/bitrev.h > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > index 90c531e6abf5..05f2b2166a83 100644 > --- a/arch/riscv/Kconfig > +++ b/arch/riscv/Kconfig > @@ -128,6 +128,7 @@ config RISCV > select HAS_IOPORT if MMU > select HAVE_ALIGNED_STRUCT_PAGE > select HAVE_ARCH_AUDITSYSCALL > + select HAVE_ARCH_BITREVERSE if RISCV_ISA_ZBKB > select HAVE_ARCH_HUGE_VMALLOC if HAVE_ARCH_HUGE_VMAP > select HAVE_ARCH_HUGE_VMAP if MMU && 64BIT > select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL > diff --git a/arch/riscv/include/asm/bitrev.h b/arch/riscv/include/asm/bitrev.h > new file mode 100644 > index 000000000000..9f205ac84796 > --- /dev/null > +++ b/arch/riscv/include/asm/bitrev.h > @@ -0,0 +1,41 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef __ASM_BITREV_H > +#define __ASM_BITREV_H > + > +#include > +#include > +#include > +#include > + > +static __always_inline __attribute_const__ u32 __arch_bitrev32(u32 x) > +{ > + unsigned long result = x; > + > + if (!riscv_has_extension_likely(RISCV_ISA_EXT_ZBKB)) > + return generic___bitrev32(x); > + > + asm volatile( > + ".option push\n" > + ".option arch,+zbkb\n" > + "rev8 %0, %0\n" It would be better to pass (long)x in for the source. Might save the compiler doing a register-register move. > + "brev8 %0, %0\n" > + ".option pop" > + : "+r" (result) > + ); > + > + if (__riscv_xlen == 64) > + return (u32)(result >> 32); Is that right? ACAICT __riscv_xlen is 32 for 32bit builds and 64 otherwise. (No idea why riscv has its own private constant for that.) I'm guessing that 'brev' is bit-reverse (or each byte) and 'rev' a byteswap, the '8' suffix rather implies it acts on 8 bytes which makes is 64bit only. So does 'rev8' even compile for 32bit. You are also likely to get a warning on 32bit for 'result >> 32'. > + > + return (u32)result; > +} I'm not sure is is a good idea inline that into its callers. But I can't think of a way to get either the instructions or a call patched in at the call site. > + > +static __always_inline __attribute_const__ u16 __arch_bitrev16(u16 x) > +{ > + return __arch_bitrev32((u32)x) >> 16; > +} > + > +static __always_inline __attribute_const__ u8 __arch_bitrev8(u8 x) > +{ > + return __arch_bitrev32((u32)x) >> 24; That seems excessive when it could just be a 'brev' instruction. Oh, and none of the casts on the function call parameters or results are needed - they are all implied. David > +} > +#endif