From: cp0613@linux.alibaba.com
To: yury.norov@gmail.com, linux@rasmusvillemoes.dk, arnd@arndb.de,
paul.walmsley@sifive.com, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr
Cc: linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org,
linux-kernel@vger.kernel.org, Chen Pei <cp0613@linux.alibaba.com>
Subject: [PATCH 2/2] bitops: rotate: Add riscv implementation using Zbb extension
Date: Fri, 20 Jun 2025 19:16:10 +0800 [thread overview]
Message-ID: <20250620111610.52750-3-cp0613@linux.alibaba.com> (raw)
In-Reply-To: <20250620111610.52750-1-cp0613@linux.alibaba.com>
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 __always_inline u64 variable_rol64(u64 word, unsigned int shift)
+{
+ asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
+ RISCV_ISA_EXT_ZBB, 1)
+ : : : : legacy);
+
+ asm volatile(
+ ".option push\n"
+ ".option arch,+zbb\n"
+ "rol %0, %1, %2\n"
+ ".option pop\n"
+ : "=r" (word) : "r" (word), "r" (shift) :);
+
+ return word;
+
+legacy:
+ return generic_rol64(word, shift);
+}
+
+static inline u64 variable_ror64(u64 word, unsigned int shift)
+{
+ asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
+ RISCV_ISA_EXT_ZBB, 1)
+ : : : : legacy);
+
+ asm volatile(
+ ".option push\n"
+ ".option arch,+zbb\n"
+ "ror %0, %1, %2\n"
+ ".option pop\n"
+ : "=r" (word) : "r" (word), "r" (shift) :);
+
+ return word;
+
+legacy:
+ return generic_ror64(word, shift);
+}
+
+static inline u32 variable_rol32(u32 word, unsigned int shift)
+{
+ asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
+ RISCV_ISA_EXT_ZBB, 1)
+ : : : : legacy);
+
+ asm volatile(
+ ".option push\n"
+ ".option arch,+zbb\n"
+ "rolw %0, %1, %2\n"
+ ".option pop\n"
+ : "=r" (word) : "r" (word), "r" (shift) :);
+
+ return word;
+
+legacy:
+ return generic_rol32(word, shift);
+}
+
+static inline u32 variable_ror32(u32 word, unsigned int shift)
+{
+ 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" (word) : "r" (word), "r" (shift) :);
+
+ return word;
+
+legacy:
+ return generic_ror32(word, shift);
+}
+
+static inline u16 variable_rol16(u16 word, unsigned int shift)
+{
+ u32 word32 = ((u32)word << 16) | word;
+
+ asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
+ RISCV_ISA_EXT_ZBB, 1)
+ : : : : legacy);
+
+ asm volatile(
+ ".option push\n"
+ ".option arch,+zbb\n"
+ "rolw %0, %1, %2\n"
+ ".option pop\n"
+ : "=r" (word32) : "r" (word32), "r" (shift) :);
+
+ return (u16)word32;
+
+legacy:
+ return generic_rol16(word, shift);
+}
+
+static inline u16 variable_ror16(u16 word, unsigned int shift)
+{
+ u32 word32 = ((u32)word << 16) | word;
+
+ 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 (u16)word32;
+
+legacy:
+ return generic_ror16(word, shift);
+}
+
+static inline u8 variable_rol8(u8 word, unsigned int shift)
+{
+ u32 word32 = ((u32)word << 24) | ((u32)word << 16) | ((u32)word << 8) | word;
+
+ asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
+ RISCV_ISA_EXT_ZBB, 1)
+ : : : : legacy);
+
+ asm volatile(
+ ".option push\n"
+ ".option arch,+zbb\n"
+ "rolw %0, %1, %2\n"
+ ".option pop\n"
+ : "=r" (word32) : "r" (word32), "r" (shift) :);
+
+ return (u8)word32;
+
+legacy:
+ return generic_rol8(word, shift);
+}
+
+static inline u8 variable_ror8(u8 word, unsigned int shift)
+{
+ u32 word32 = ((u32)word << 24) | ((u32)word << 16) | ((u32)word << 8) | word;
+
+ 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)
+
#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 11:16 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 " 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 ` cp0613 [this message]
2025-06-20 16:20 ` [PATCH 2/2] bitops: rotate: Add riscv implementation using Zbb extension Yury Norov
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=20250620111610.52750-3-cp0613@linux.alibaba.com \
--to=cp0613@linux.alibaba.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=arnd@arndb.de \
--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 \
--cc=yury.norov@gmail.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
all inboxes | Powered by JetHome®