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
3747
3848static __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
5462legacy :
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
9477static __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
11091legacy :
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
150106static __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
169123legacy :
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
205138static __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
224155legacy :
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/**
0 commit comments