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