xref: /openbmc/linux/tools/include/asm-generic/bitops/fls.h (revision 597473720f4dc69749542bfcfed4a927a43d935e)
1fb7df12dSIngo Molnar /* SPDX-License-Identifier: GPL-2.0 */
2de1e17b1SArnaldo Carvalho de Melo #ifndef _ASM_GENERIC_BITOPS_FLS_H_
3de1e17b1SArnaldo Carvalho de Melo #define _ASM_GENERIC_BITOPS_FLS_H_
4de1e17b1SArnaldo Carvalho de Melo 
5de1e17b1SArnaldo Carvalho de Melo /**
6de1e17b1SArnaldo Carvalho de Melo  * fls - find last (most-significant) bit set
7de1e17b1SArnaldo Carvalho de Melo  * @x: the word to search
8de1e17b1SArnaldo Carvalho de Melo  *
9de1e17b1SArnaldo Carvalho de Melo  * This is defined the same way as ffs.
10de1e17b1SArnaldo Carvalho de Melo  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
11de1e17b1SArnaldo Carvalho de Melo  */
12de1e17b1SArnaldo Carvalho de Melo 
fls(unsigned int x)13*3fc2579eSMatthew Wilcox static __always_inline int fls(unsigned int x)
14de1e17b1SArnaldo Carvalho de Melo {
15de1e17b1SArnaldo Carvalho de Melo 	int r = 32;
16de1e17b1SArnaldo Carvalho de Melo 
17de1e17b1SArnaldo Carvalho de Melo 	if (!x)
18de1e17b1SArnaldo Carvalho de Melo 		return 0;
19de1e17b1SArnaldo Carvalho de Melo 	if (!(x & 0xffff0000u)) {
20de1e17b1SArnaldo Carvalho de Melo 		x <<= 16;
21de1e17b1SArnaldo Carvalho de Melo 		r -= 16;
22de1e17b1SArnaldo Carvalho de Melo 	}
23de1e17b1SArnaldo Carvalho de Melo 	if (!(x & 0xff000000u)) {
24de1e17b1SArnaldo Carvalho de Melo 		x <<= 8;
25de1e17b1SArnaldo Carvalho de Melo 		r -= 8;
26de1e17b1SArnaldo Carvalho de Melo 	}
27de1e17b1SArnaldo Carvalho de Melo 	if (!(x & 0xf0000000u)) {
28de1e17b1SArnaldo Carvalho de Melo 		x <<= 4;
29de1e17b1SArnaldo Carvalho de Melo 		r -= 4;
30de1e17b1SArnaldo Carvalho de Melo 	}
31de1e17b1SArnaldo Carvalho de Melo 	if (!(x & 0xc0000000u)) {
32de1e17b1SArnaldo Carvalho de Melo 		x <<= 2;
33de1e17b1SArnaldo Carvalho de Melo 		r -= 2;
34de1e17b1SArnaldo Carvalho de Melo 	}
35de1e17b1SArnaldo Carvalho de Melo 	if (!(x & 0x80000000u)) {
36de1e17b1SArnaldo Carvalho de Melo 		x <<= 1;
37de1e17b1SArnaldo Carvalho de Melo 		r -= 1;
38de1e17b1SArnaldo Carvalho de Melo 	}
39de1e17b1SArnaldo Carvalho de Melo 	return r;
40de1e17b1SArnaldo Carvalho de Melo }
41de1e17b1SArnaldo Carvalho de Melo 
42de1e17b1SArnaldo Carvalho de Melo #endif /* _ASM_GENERIC_BITOPS_FLS_H_ */
43