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