From: Alexander van Heukelum This series of patches: [1/3] adds __fls.h to asm-generic [2/3] modifies asm-*/bitops.h for 64-bit archs to implement __fls [3/3] modifies asm-generic/fls64.h to make use of __fls I have compiled i386 and x86_64, and they generate the same code as before the change. These patches make one tiny bit of the x86-unification a tiny bit cleaner. This patch: Add a generic __fls implementation in the same spirit as the generic __ffs one. It finds the last (most significant) set bit in the given long value. Signed-off-by: Alexander van Heukelum Cc: Richard Henderson Cc: Ivan Kokshaysky Cc: "Luck, Tony" Cc: Ralf Baechle Cc: Kyle McMartin Cc: Grant Grundler Cc: Matthew Wilcox Cc: Paul Mackerras Cc: Benjamin Herrenschmidt Cc: Heiko Carstens Cc: Martin Schwidefsky Cc: Paul Mundt Cc: "David S. Miller" Cc: Ingo Molnar Cc: Thomas Gleixner Signed-off-by: Andrew Morton --- include/asm-generic/bitops/__fls.h | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff -puN /dev/null include/asm-generic/bitops/__fls.h --- /dev/null +++ a/include/asm-generic/bitops/__fls.h @@ -0,0 +1,43 @@ +#ifndef _ASM_GENERIC_BITOPS___FLS_H_ +#define _ASM_GENERIC_BITOPS___FLS_H_ + +#include + +/** + * __fls - find last (most-significant) set bit in a long word + * @word: the word to search + * + * Undefined if no set bit exists, so code should check against 0 first. + */ +static inline unsigned long __fls(unsigned long word) +{ + int num = BITS_PER_LONG - 1; + +#if BITS_PER_LONG == 64 + if (!(word & (~0ul << 32))) { + num -= 32; + word <<= 32; + } +#endif + if (!(word & (~0ul << (BITS_PER_LONG-16)))) { + num -= 16; + word <<= 16; + } + if (!(word & (~0ul << (BITS_PER_LONG-8)))) { + num -= 8; + word <<= 8; + } + if (!(word & (~0ul << (BITS_PER_LONG-4)))) { + num -= 4; + word <<= 4; + } + if (!(word & (~0ul << (BITS_PER_LONG-2)))) { + num -= 2; + word <<= 2; + } + if (!(word & (~0ul << (BITS_PER_LONG-1)))) + num -= 1; + return num; +} + +#endif /* _ASM_GENERIC_BITOPS___FLS_H_ */ _