GNU Linux-libre 4.19.264-gnu1
[releases.git] / tools / include / asm-generic / bitops / __ffs.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_
3 #define _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_
4
5 #include <asm/types.h>
6 #include <asm/bitsperlong.h>
7
8 /**
9  * __ffs - find first bit in word.
10  * @word: The word to search
11  *
12  * Undefined if no bit exists, so code should check against 0 first.
13  */
14 static __always_inline unsigned long __ffs(unsigned long word)
15 {
16         int num = 0;
17
18 #if __BITS_PER_LONG == 64
19         if ((word & 0xffffffff) == 0) {
20                 num += 32;
21                 word >>= 32;
22         }
23 #endif
24         if ((word & 0xffff) == 0) {
25                 num += 16;
26                 word >>= 16;
27         }
28         if ((word & 0xff) == 0) {
29                 num += 8;
30                 word >>= 8;
31         }
32         if ((word & 0xf) == 0) {
33                 num += 4;
34                 word >>= 4;
35         }
36         if ((word & 0x3) == 0) {
37                 num += 2;
38                 word >>= 2;
39         }
40         if ((word & 0x1) == 0)
41                 num += 1;
42         return num;
43 }
44
45 #endif /* _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_ */