fls in asm for i386
PATCH 1/1 fls in asm for i386
There is a single instruction on i386 to find largest set bit; so it makes sense to use it (like we use bfs for ffs()).
Signed-off-by Stephen Hemminger <[email protected]>
include/asm-i386/bitops.h +17 -6
@@ -367,12 +367,6 @@ static inline unsigned long ffz(unsignedreturn word; } -/* -* fls: find last bit set. -*/ - -#define fls (x) generic_fls(x) -#ifdef __KERNEL__/*
@@ -414,6 +408,23 @@ static inline int ffs(int x) }/** +* fls - find last bit set +* @x: the word to search +* +* This is defined the same way as ffs. +*/ +static inline int fls (int x) +{ +int r; + + __asm__("bsrl %1,%0\n\t" +"jnz 1f\n\t" +"movl $-1,%0\n" +"1:" :"=r" (r) :"rm" (x)); +return r+1 ; +} + +/** * hweightN - returns the hamming weight of a N-bit word * @x: the word to weigh *
PATCH 1/1 fls in asm for x86_64
Use single instruction for find largest set bit on x86_64.
Signed-off-by Stephen Hemminger <[email protected]>
include/asm-x86_64/bitops.h +16 -3
@@ -370,6 +370,22 @@ static __inline__ int ffs(int x) }/** +* fls - find last bit set +* @x: the word to search +* +* This is defined the same way as ffs. +*/ +static __inline__int fls (int x) +{ +int r; + + __asm__("bsrl %1,%0\n\t" +"cmovzl %2,%0" + :"=r" (r) :"rm" (x),"r" (-1 )); +return r+1 ; +} + +/** * hweightN - returns the hamming weight of a N-bit word * @x: the word to weigh *
@@ -407,9 +423,6 @@ static __inline__ int ffs(int x)#define minix_find_first_zero_bit (addr,size) \ find_first_zero_bit((void*)addr,size) -/* find last set bit */ -#define fls (x) generic_fls(x) -#endif /* __KERNEL__ */ #endif /* _X86_64_BITOPS_H */
Re: [PATCH] fls in asm for x86_64
Quoted 1 line · Stephen Hemminger
Use single instruction for find largest set bit on
x86_64.
Thanks queued.
-Andi
Quoted 4 lines · Stephen Hemminger
There is a single instruction on i386 to find largest set bit; so it makes sense to use it (like we use bfs for
ffs()).
Interesting, I thought this had already been done:
http://lkml.org/lkml/2003/1/28/296
http://lkml.org/lkml/2003/4/29/173
Pádraig.