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