Skip to content

Commit cb4ede9

Browse files
XiaoWang1772palmer-dabbelt
authored andcommitted
riscv: Avoid code duplication with generic bitops implementation
There's code duplication between the fallback implementation for bitops __ffs/__fls/ffs/fls API and the generic C implementation in include/asm-generic/bitops/. To avoid this duplication, this patch renames the generic C implementation by adding a "generic_" prefix to them, then we can use these generic APIs as fallback. Suggested-by: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Xiao Wang <xiao.w.wang@intel.com> Reviewed-by: Charlie Jenkins <charlie@rivosinc.com> Link: https://lore.kernel.org/r/20231112094421.4014931-1-xiao.w.wang@intel.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 05d450a commit cb4ede9

File tree

5 files changed

+48
-122
lines changed

5 files changed

+48
-122
lines changed

arch/riscv/include/asm/bitops.h

Lines changed: 24 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#include <asm-generic/bitops/fls.h>
2323

2424
#else
25+
#define __HAVE_ARCH___FFS
26+
#define __HAVE_ARCH___FLS
27+
#define __HAVE_ARCH_FFS
28+
#define __HAVE_ARCH_FLS
29+
30+
#include <asm-generic/bitops/__ffs.h>
31+
#include <asm-generic/bitops/__fls.h>
32+
#include <asm-generic/bitops/ffs.h>
33+
#include <asm-generic/bitops/fls.h>
34+
2535
#include <asm/alternative-macros.h>
2636
#include <asm/hwcap.h>
2737

@@ -37,8 +47,6 @@
3747

3848
static __always_inline unsigned long variable__ffs(unsigned long word)
3949
{
40-
int num;
41-
4250
asm_volatile_goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
4351
RISCV_ISA_EXT_ZBB, 1)
4452
: : : : legacy);
@@ -52,32 +60,7 @@ static __always_inline unsigned long variable__ffs(unsigned long word)
5260
return word;
5361

5462
legacy:
55-
num = 0;
56-
#if BITS_PER_LONG == 64
57-
if ((word & 0xffffffff) == 0) {
58-
num += 32;
59-
word >>= 32;
60-
}
61-
#endif
62-
if ((word & 0xffff) == 0) {
63-
num += 16;
64-
word >>= 16;
65-
}
66-
if ((word & 0xff) == 0) {
67-
num += 8;
68-
word >>= 8;
69-
}
70-
if ((word & 0xf) == 0) {
71-
num += 4;
72-
word >>= 4;
73-
}
74-
if ((word & 0x3) == 0) {
75-
num += 2;
76-
word >>= 2;
77-
}
78-
if ((word & 0x1) == 0)
79-
num += 1;
80-
return num;
63+
return generic___ffs(word);
8164
}
8265

8366
/**
@@ -93,8 +76,6 @@ static __always_inline unsigned long variable__ffs(unsigned long word)
9376

9477
static __always_inline unsigned long variable__fls(unsigned long word)
9578
{
96-
int num;
97-
9879
asm_volatile_goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
9980
RISCV_ISA_EXT_ZBB, 1)
10081
: : : : legacy);
@@ -108,32 +89,7 @@ static __always_inline unsigned long variable__fls(unsigned long word)
10889
return BITS_PER_LONG - 1 - word;
10990

11091
legacy:
111-
num = BITS_PER_LONG - 1;
112-
#if BITS_PER_LONG == 64
113-
if (!(word & (~0ul << 32))) {
114-
num -= 32;
115-
word <<= 32;
116-
}
117-
#endif
118-
if (!(word & (~0ul << (BITS_PER_LONG - 16)))) {
119-
num -= 16;
120-
word <<= 16;
121-
}
122-
if (!(word & (~0ul << (BITS_PER_LONG - 8)))) {
123-
num -= 8;
124-
word <<= 8;
125-
}
126-
if (!(word & (~0ul << (BITS_PER_LONG - 4)))) {
127-
num -= 4;
128-
word <<= 4;
129-
}
130-
if (!(word & (~0ul << (BITS_PER_LONG - 2)))) {
131-
num -= 2;
132-
word <<= 2;
133-
}
134-
if (!(word & (~0ul << (BITS_PER_LONG - 1))))
135-
num -= 1;
136-
return num;
92+
return generic___fls(word);
13793
}
13894

13995
/**
@@ -149,46 +105,23 @@ static __always_inline unsigned long variable__fls(unsigned long word)
149105

150106
static __always_inline int variable_ffs(int x)
151107
{
152-
int r;
153-
154-
if (!x)
155-
return 0;
156-
157108
asm_volatile_goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
158109
RISCV_ISA_EXT_ZBB, 1)
159110
: : : : legacy);
160111

112+
if (!x)
113+
return 0;
114+
161115
asm volatile (".option push\n"
162116
".option arch,+zbb\n"
163117
CTZW "%0, %1\n"
164118
".option pop\n"
165-
: "=r" (r) : "r" (x) :);
119+
: "=r" (x) : "r" (x) :);
166120

167-
return r + 1;
121+
return x + 1;
168122

169123
legacy:
170-
r = 1;
171-
if (!(x & 0xffff)) {
172-
x >>= 16;
173-
r += 16;
174-
}
175-
if (!(x & 0xff)) {
176-
x >>= 8;
177-
r += 8;
178-
}
179-
if (!(x & 0xf)) {
180-
x >>= 4;
181-
r += 4;
182-
}
183-
if (!(x & 3)) {
184-
x >>= 2;
185-
r += 2;
186-
}
187-
if (!(x & 1)) {
188-
x >>= 1;
189-
r += 1;
190-
}
191-
return r;
124+
return generic_ffs(x);
192125
}
193126

194127
/**
@@ -204,46 +137,23 @@ static __always_inline int variable_ffs(int x)
204137

205138
static __always_inline int variable_fls(unsigned int x)
206139
{
207-
int r;
208-
209-
if (!x)
210-
return 0;
211-
212140
asm_volatile_goto(ALTERNATIVE("j %l[legacy]", "nop", 0,
213141
RISCV_ISA_EXT_ZBB, 1)
214142
: : : : legacy);
215143

144+
if (!x)
145+
return 0;
146+
216147
asm volatile (".option push\n"
217148
".option arch,+zbb\n"
218149
CLZW "%0, %1\n"
219150
".option pop\n"
220-
: "=r" (r) : "r" (x) :);
151+
: "=r" (x) : "r" (x) :);
221152

222-
return 32 - r;
153+
return 32 - x;
223154

224155
legacy:
225-
r = 32;
226-
if (!(x & 0xffff0000u)) {
227-
x <<= 16;
228-
r -= 16;
229-
}
230-
if (!(x & 0xff000000u)) {
231-
x <<= 8;
232-
r -= 8;
233-
}
234-
if (!(x & 0xf0000000u)) {
235-
x <<= 4;
236-
r -= 4;
237-
}
238-
if (!(x & 0xc0000000u)) {
239-
x <<= 2;
240-
r -= 2;
241-
}
242-
if (!(x & 0x80000000u)) {
243-
x <<= 1;
244-
r -= 1;
245-
}
246-
return r;
156+
return generic_fls(x);
247157
}
248158

249159
/**

include/asm-generic/bitops/__ffs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <asm/types.h>
66

77
/**
8-
* __ffs - find first bit in word.
8+
* generic___ffs - find first bit in word.
99
* @word: The word to search
1010
*
1111
* Undefined if no bit exists, so code should check against 0 first.
1212
*/
13-
static __always_inline unsigned long __ffs(unsigned long word)
13+
static __always_inline unsigned long generic___ffs(unsigned long word)
1414
{
1515
int num = 0;
1616

@@ -41,4 +41,8 @@ static __always_inline unsigned long __ffs(unsigned long word)
4141
return num;
4242
}
4343

44+
#ifndef __HAVE_ARCH___FFS
45+
#define __ffs(word) generic___ffs(word)
46+
#endif
47+
4448
#endif /* _ASM_GENERIC_BITOPS___FFS_H_ */

include/asm-generic/bitops/__fls.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <asm/types.h>
66

77
/**
8-
* __fls - find last (most-significant) set bit in a long word
8+
* generic___fls - find last (most-significant) set bit in a long word
99
* @word: the word to search
1010
*
1111
* Undefined if no set bit exists, so code should check against 0 first.
1212
*/
13-
static __always_inline unsigned long __fls(unsigned long word)
13+
static __always_inline unsigned long generic___fls(unsigned long word)
1414
{
1515
int num = BITS_PER_LONG - 1;
1616

@@ -41,4 +41,8 @@ static __always_inline unsigned long __fls(unsigned long word)
4141
return num;
4242
}
4343

44+
#ifndef __HAVE_ARCH___FLS
45+
#define __fls(word) generic___fls(word)
46+
#endif
47+
4448
#endif /* _ASM_GENERIC_BITOPS___FLS_H_ */

include/asm-generic/bitops/ffs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#define _ASM_GENERIC_BITOPS_FFS_H_
44

55
/**
6-
* ffs - find first bit set
6+
* generic_ffs - find first bit set
77
* @x: the word to search
88
*
99
* This is defined the same way as
1010
* the libc and compiler builtin ffs routines, therefore
1111
* differs in spirit from ffz (man ffs).
1212
*/
13-
static inline int ffs(int x)
13+
static inline int generic_ffs(int x)
1414
{
1515
int r = 1;
1616

@@ -39,4 +39,8 @@ static inline int ffs(int x)
3939
return r;
4040
}
4141

42+
#ifndef __HAVE_ARCH_FFS
43+
#define ffs(x) generic_ffs(x)
44+
#endif
45+
4246
#endif /* _ASM_GENERIC_BITOPS_FFS_H_ */

include/asm-generic/bitops/fls.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#define _ASM_GENERIC_BITOPS_FLS_H_
44

55
/**
6-
* fls - find last (most-significant) bit set
6+
* generic_fls - find last (most-significant) bit set
77
* @x: the word to search
88
*
99
* This is defined the same way as ffs.
1010
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
1111
*/
1212

13-
static __always_inline int fls(unsigned int x)
13+
static __always_inline int generic_fls(unsigned int x)
1414
{
1515
int r = 32;
1616

@@ -39,4 +39,8 @@ static __always_inline int fls(unsigned int x)
3939
return r;
4040
}
4141

42+
#ifndef __HAVE_ARCH_FLS
43+
#define fls(x) generic_fls(x)
44+
#endif
45+
4246
#endif /* _ASM_GENERIC_BITOPS_FLS_H_ */

0 commit comments

Comments
 (0)