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