[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250820-riscv-altn-helper-wip-v1-5-c3c626c1f7e6@iscas.ac.cn>
Date: Wed, 20 Aug 2025 21:44:49 +0800
From: Vivian Wang <wangruikang@...as.ac.cn>
To: Paul Walmsley <paul.walmsley@...ive.com>,
Palmer Dabbelt <palmer@...belt.com>, Albert Ou <aou@...s.berkeley.edu>,
Alexandre Ghiti <alex@...ti.fr>, Yury Norov <yury.norov@...il.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>
Cc: Vivian Wang <wangruikang@...as.ac.cn>, Vivian Wang <uwu@...m.page>,
linux-riscv@...ts.infradead.org, linux-kernel@...r.kernel.org
Subject: [PATCH 5/6] riscv: bitops: Convert to use_alternative_likely
Use use_alternative_likely() to check for RISCV_ISA_EXT_ZBB, replacing
the use of asm goto with ALTERNATIVE.
The "likely" variant is used to match the behavior of the original
implementation using ALTERNATIVE("j %l[legacy]", "nop", ...).
Signed-off-by: Vivian Wang <wangruikang@...as.ac.cn>
---
arch/riscv/include/asm/bitops.h | 112 ++++++++++++++++++----------------------
1 file changed, 50 insertions(+), 62 deletions(-)
diff --git a/arch/riscv/include/asm/bitops.h b/arch/riscv/include/asm/bitops.h
index d59310f74c2ba70caeb7b9b0e9221882117583f5..0257d547a96293909d90b017c8a48b508d0fd642 100644
--- a/arch/riscv/include/asm/bitops.h
+++ b/arch/riscv/include/asm/bitops.h
@@ -47,20 +47,17 @@
static __always_inline unsigned long variable__ffs(unsigned long word)
{
- asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
- RISCV_ISA_EXT_ZBB, 1)
- : : : : legacy);
-
- asm volatile (".option push\n"
- ".option arch,+zbb\n"
- "ctz %0, %1\n"
- ".option pop\n"
- : "=r" (word) : "r" (word) :);
-
- return word;
-
-legacy:
- return generic___ffs(word);
+ if (use_alternative_likely(0, RISCV_ISA_EXT_ZBB)) {
+ asm volatile (".option push\n"
+ ".option arch,+zbb\n"
+ "ctz %0, %1\n"
+ ".option pop\n"
+ : "=r" (word) : "r" (word) :);
+
+ return word;
+ } else {
+ return generic___ffs(word);
+ }
}
/**
@@ -76,20 +73,17 @@ static __always_inline unsigned long variable__ffs(unsigned long word)
static __always_inline unsigned long variable__fls(unsigned long word)
{
- asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
- RISCV_ISA_EXT_ZBB, 1)
- : : : : legacy);
-
- asm volatile (".option push\n"
- ".option arch,+zbb\n"
- "clz %0, %1\n"
- ".option pop\n"
- : "=r" (word) : "r" (word) :);
-
- return BITS_PER_LONG - 1 - word;
-
-legacy:
- return generic___fls(word);
+ if (use_alternative_likely(0, RISCV_ISA_EXT_ZBB)) {
+ asm volatile (".option push\n"
+ ".option arch,+zbb\n"
+ "clz %0, %1\n"
+ ".option pop\n"
+ : "=r" (word) : "r" (word) :);
+
+ return BITS_PER_LONG - 1 - word;
+ } else {
+ return generic___fls(word);
+ }
}
/**
@@ -105,23 +99,20 @@ static __always_inline unsigned long variable__fls(unsigned long word)
static __always_inline int variable_ffs(int x)
{
- asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
- RISCV_ISA_EXT_ZBB, 1)
- : : : : legacy);
-
- if (!x)
- return 0;
-
- asm volatile (".option push\n"
- ".option arch,+zbb\n"
- CTZW "%0, %1\n"
- ".option pop\n"
- : "=r" (x) : "r" (x) :);
-
- return x + 1;
-
-legacy:
- return generic_ffs(x);
+ if (use_alternative_likely(0, RISCV_ISA_EXT_ZBB)) {
+ if (!x)
+ return 0;
+
+ asm volatile (".option push\n"
+ ".option arch,+zbb\n"
+ CTZW "%0, %1\n"
+ ".option pop\n"
+ : "=r" (x) : "r" (x) :);
+
+ return x + 1;
+ } else {
+ return generic_ffs(x);
+ }
}
/**
@@ -137,23 +128,20 @@ static __always_inline int variable_ffs(int x)
static __always_inline int variable_fls(unsigned int x)
{
- asm goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
- RISCV_ISA_EXT_ZBB, 1)
- : : : : legacy);
-
- if (!x)
- return 0;
-
- asm volatile (".option push\n"
- ".option arch,+zbb\n"
- CLZW "%0, %1\n"
- ".option pop\n"
- : "=r" (x) : "r" (x) :);
-
- return 32 - x;
-
-legacy:
- return generic_fls(x);
+ if (use_alternative_likely(0, RISCV_ISA_EXT_ZBB)) {
+ if (!x)
+ return 0;
+
+ asm volatile (".option push\n"
+ ".option arch,+zbb\n"
+ CLZW "%0, %1\n"
+ ".option pop\n"
+ : "=r" (x) : "r" (x) :);
+
+ return 32 - x;
+ } else {
+ return generic_fls(x);
+ }
}
/**
--
2.50.1
Powered by blists - more mailing lists