From: Yury Norov <yury.norov@gmail.com>
To: cp0613@linux.alibaba.com
Cc: linux@rasmusvillemoes.dk, arnd@arndb.de,
paul.walmsley@sifive.com, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr,
linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] bitops: rotate: Add riscv implementation using Zbb extension
Date: Fri, 20 Jun 2025 12:20:47 -0400 [thread overview]
Message-ID: <aFWKX4rpuNCDBP67@yury> (raw)
In-Reply-To: <20250620111610.52750-3-cp0613@linux.alibaba.com>
On Fri, Jun 20, 2025 at 07:16:10PM +0800, cp0613@linux.alibaba.com wrote:
> From: Chen Pei <cp0613@linux.alibaba.com>
>
> The RISC-V Zbb extension[1] defines bitwise rotation instructions,
> which can be used to implement rotate related functions.
>
> [1] https://github.com/riscv/riscv-bitmanip/
>
> Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
> ---
> arch/riscv/include/asm/bitops.h | 172 ++++++++++++++++++++++++++++++++
> 1 file changed, 172 insertions(+)
>
> diff --git a/arch/riscv/include/asm/bitops.h b/arch/riscv/include/asm/bitops.h
> index d59310f74c2b..be247ef9e686 100644
> --- a/arch/riscv/include/asm/bitops.h
> +++ b/arch/riscv/include/asm/bitops.h
> @@ -20,17 +20,20 @@
> #include <asm-generic/bitops/__fls.h>
> #include <asm-generic/bitops/ffs.h>
> #include <asm-generic/bitops/fls.h>
> +#include <asm-generic/bitops/rotate.h>
>
> #else
> #define __HAVE_ARCH___FFS
> #define __HAVE_ARCH___FLS
> #define __HAVE_ARCH_FFS
> #define __HAVE_ARCH_FLS
> +#define __HAVE_ARCH_ROTATE
>
> #include <asm-generic/bitops/__ffs.h>
> #include <asm-generic/bitops/__fls.h>
> #include <asm-generic/bitops/ffs.h>
> #include <asm-generic/bitops/fls.h>
> +#include <asm-generic/bitops/rotate.h>
>
> #include <asm/alternative-macros.h>
> #include <asm/hwcap.h>
> @@ -175,6 +178,175 @@ static __always_inline int variable_fls(unsigned int x)
> variable_fls(x_); \
> })
...
> +static inline u8 variable_ror8(u8 word, unsigned int shift)
> +{
> + u32 word32 = ((u32)word << 24) | ((u32)word << 16) | ((u32)word << 8) | word;
Can you add a comment about what is happening here? Are you sure it's
optimized out in case of the 'legacy' alternative?
> +
> + asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
> + RISCV_ISA_EXT_ZBB, 1)
> + : : : : legacy);
> +
> + asm volatile(
> + ".option push\n"
> + ".option arch,+zbb\n"
> + "rorw %0, %1, %2\n"
> + ".option pop\n"
> + : "=r" (word32) : "r" (word32), "r" (shift) :);
> +
> + return (u8)word32;
> +
> +legacy:
> + return generic_ror8(word, shift);
> +}
> +
> +#define rol64(word, shift) variable_rol64(word, shift)
> +#define ror64(word, shift) variable_ror64(word, shift)
> +#define rol32(word, shift) variable_rol32(word, shift)
> +#define ror32(word, shift) variable_ror32(word, shift)
> +#define rol16(word, shift) variable_rol16(word, shift)
> +#define ror16(word, shift) variable_ror16(word, shift)
> +#define rol8(word, shift) variable_rol8(word, shift)
> +#define ror8(word, shift) variable_ror8(word, shift)
Here you wire ror/rol() to the variable_ror/rol() unconditionally, and
that breaks compile-time rotation if the parameter is known at compile
time.
I believe, generic implementation will allow compiler to handle this
case better. Can you do a similar thing to what fls() does in the same
file?
Thanks,
Yury
> +
> #endif /* !(defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB)) || defined(NO_ALTERNATIVE) */
>
> #include <asm-generic/bitops/ffz.h>
> --
> 2.49.0
next prev parent reply other threads:[~2025-06-20 16:20 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 11:16 [PATCH 0/2] Implementing bitops rotate using riscv Zbb extension cp0613
2025-06-20 11:16 ` [PATCH 1/2] bitops: generic rotate cp0613
2025-06-20 15:47 ` kernel test robot
2025-06-23 11:59 ` kernel test robot
2025-06-20 11:16 ` [PATCH 2/2] bitops: rotate: Add riscv implementation using Zbb extension cp0613
2025-06-20 16:20 ` Yury Norov [this message]
2025-06-25 16:02 ` David Laight
2025-06-28 12:08 ` cp0613
2025-06-29 10:38 ` David Laight
2025-06-30 12:14 ` cp0613
2025-06-30 17:35 ` David Laight
2025-07-01 13:01 ` cp0613
2025-06-28 11:13 ` cp0613
2025-06-29 1:48 ` Yury Norov
2025-06-30 12:04 ` cp0613
2025-06-30 16:53 ` Yury Norov
2025-07-01 12:47 ` cp0613
2025-07-01 18:32 ` Yury Norov
2025-07-02 10:11 ` David Laight
2025-07-03 16:58 ` Yury Norov
2025-07-02 12:30 ` cp0613
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aFWKX4rpuNCDBP67@yury \
--to=yury.norov@gmail.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=arnd@arndb.de \
--cc=cp0613@linux.alibaba.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux@rasmusvillemoes.dk \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).