···11+#ifndef _ASM_GENERIC_BITOPS___FFS_H_
22+#define _ASM_GENERIC_BITOPS___FFS_H_
33+44+#include <asm/types.h>
55+66+/**
77+ * __ffs - find first bit in word.
88+ * @word: The word to search
99+ *
1010+ * Undefined if no bit exists, so code should check against 0 first.
1111+ */
1212+static __always_inline unsigned long __ffs(unsigned long word)
1313+{
1414+ int num = 0;
1515+1616+#if BITS_PER_LONG == 64
1717+ if ((word & 0xffffffff) == 0) {
1818+ num += 32;
1919+ word >>= 32;
2020+ }
2121+#endif
2222+ if ((word & 0xffff) == 0) {
2323+ num += 16;
2424+ word >>= 16;
2525+ }
2626+ if ((word & 0xff) == 0) {
2727+ num += 8;
2828+ word >>= 8;
2929+ }
3030+ if ((word & 0xf) == 0) {
3131+ num += 4;
3232+ word >>= 4;
3333+ }
3434+ if ((word & 0x3) == 0) {
3535+ num += 2;
3636+ word >>= 2;
3737+ }
3838+ if ((word & 0x1) == 0)
3939+ num += 1;
4040+ return num;
4141+}
4242+4343+#endif /* _ASM_GENERIC_BITOPS___FFS_H_ */
+43
include/asm-generic/bitops/__fls.h
···11+#ifndef _ASM_GENERIC_BITOPS___FLS_H_
22+#define _ASM_GENERIC_BITOPS___FLS_H_
33+44+#include <asm/types.h>
55+66+/**
77+ * __fls - find last (most-significant) set bit in a long word
88+ * @word: the word to search
99+ *
1010+ * Undefined if no set bit exists, so code should check against 0 first.
1111+ */
1212+static __always_inline unsigned long __fls(unsigned long word)
1313+{
1414+ int num = BITS_PER_LONG - 1;
1515+1616+#if BITS_PER_LONG == 64
1717+ if (!(word & (~0ul << 32))) {
1818+ num -= 32;
1919+ word <<= 32;
2020+ }
2121+#endif
2222+ if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
2323+ num -= 16;
2424+ word <<= 16;
2525+ }
2626+ if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
2727+ num -= 8;
2828+ word <<= 8;
2929+ }
3030+ if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
3131+ num -= 4;
3232+ word <<= 4;
3333+ }
3434+ if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
3535+ num -= 2;
3636+ word <<= 2;
3737+ }
3838+ if (!(word & (~0ul << (BITS_PER_LONG-1))))
3939+ num -= 1;
4040+ return num;
4141+}
4242+4343+#endif /* _ASM_GENERIC_BITOPS___FLS_H_ */
+41
include/asm-generic/bitops/fls.h
···11+#ifndef _ASM_GENERIC_BITOPS_FLS_H_
22+#define _ASM_GENERIC_BITOPS_FLS_H_
33+44+/**
55+ * fls - find last (most-significant) bit set
66+ * @x: the word to search
77+ *
88+ * This is defined the same way as ffs.
99+ * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
1010+ */
1111+1212+static __always_inline int fls(int x)
1313+{
1414+ int r = 32;
1515+1616+ if (!x)
1717+ return 0;
1818+ if (!(x & 0xffff0000u)) {
1919+ x <<= 16;
2020+ r -= 16;
2121+ }
2222+ if (!(x & 0xff000000u)) {
2323+ x <<= 8;
2424+ r -= 8;
2525+ }
2626+ if (!(x & 0xf0000000u)) {
2727+ x <<= 4;
2828+ r -= 4;
2929+ }
3030+ if (!(x & 0xc0000000u)) {
3131+ x <<= 2;
3232+ r -= 2;
3333+ }
3434+ if (!(x & 0x80000000u)) {
3535+ x <<= 1;
3636+ r -= 1;
3737+ }
3838+ return r;
3939+}
4040+4141+#endif /* _ASM_GENERIC_BITOPS_FLS_H_ */
+36
include/asm-generic/bitops/fls64.h
···11+#ifndef _ASM_GENERIC_BITOPS_FLS64_H_
22+#define _ASM_GENERIC_BITOPS_FLS64_H_
33+44+#include <asm/types.h>
55+66+/**
77+ * fls64 - find last set bit in a 64-bit word
88+ * @x: the word to search
99+ *
1010+ * This is defined in a similar way as the libc and compiler builtin
1111+ * ffsll, but returns the position of the most significant set bit.
1212+ *
1313+ * fls64(value) returns 0 if value is 0 or the position of the last
1414+ * set bit if value is nonzero. The last (most significant) bit is
1515+ * at position 64.
1616+ */
1717+#if BITS_PER_LONG == 32
1818+static __always_inline int fls64(__u64 x)
1919+{
2020+ __u32 h = x >> 32;
2121+ if (h)
2222+ return fls(h) + 32;
2323+ return fls(x);
2424+}
2525+#elif BITS_PER_LONG == 64
2626+static __always_inline int fls64(__u64 x)
2727+{
2828+ if (x == 0)
2929+ return 0;
3030+ return __fls(x) + 1;
3131+}
3232+#else
3333+#error BITS_PER_LONG not 32 or 64
3434+#endif
3535+3636+#endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */