xref: /OK3568_Linux_fs/kernel/tools/include/asm-generic/bitops/fls64.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_GENERIC_BITOPS_FLS64_H_
3*4882a593Smuzhiyun #define _ASM_GENERIC_BITOPS_FLS64_H_
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <asm/types.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun /**
8*4882a593Smuzhiyun  * fls64 - find last set bit in a 64-bit word
9*4882a593Smuzhiyun  * @x: the word to search
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This is defined in a similar way as the libc and compiler builtin
12*4882a593Smuzhiyun  * ffsll, but returns the position of the most significant set bit.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * fls64(value) returns 0 if value is 0 or the position of the last
15*4882a593Smuzhiyun  * set bit if value is nonzero. The last (most significant) bit is
16*4882a593Smuzhiyun  * at position 64.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun #if BITS_PER_LONG == 32
fls64(__u64 x)19*4882a593Smuzhiyun static __always_inline int fls64(__u64 x)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	__u32 h = x >> 32;
22*4882a593Smuzhiyun 	if (h)
23*4882a593Smuzhiyun 		return fls(h) + 32;
24*4882a593Smuzhiyun 	return fls(x);
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun #elif BITS_PER_LONG == 64
fls64(__u64 x)27*4882a593Smuzhiyun static __always_inline int fls64(__u64 x)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	if (x == 0)
30*4882a593Smuzhiyun 		return 0;
31*4882a593Smuzhiyun 	return __fls(x) + 1;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun #else
34*4882a593Smuzhiyun #error BITS_PER_LONG not 32 or 64
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */
38