^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _ASM_GENERIC_BITOPS___FLS_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_GENERIC_BITOPS___FLS_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * __fls - find last (most-significant) set bit in a long word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * @word: the word to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Undefined if no set bit exists, so code should check against 0 first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static __always_inline unsigned long __fls(unsigned long word)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int num = BITS_PER_LONG - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #if BITS_PER_LONG == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (!(word & (~0ul << 32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) num -= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) word <<= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) num -= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) word <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) num -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) word <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) num -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) word <<= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) num -= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) word <<= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!(word & (~0ul << (BITS_PER_LONG-1))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) num -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #endif /* _ASM_GENERIC_BITOPS___FLS_H_ */