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