GNU Linux-libre 4.4.294-gnu1
[releases.git] / tools / include / linux / bitops.h
1 #ifndef _TOOLS_LINUX_BITOPS_H_
2 #define _TOOLS_LINUX_BITOPS_H_
3
4 #include <asm/types.h>
5 #include <linux/kernel.h>
6 #ifndef __WORDSIZE
7 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
8 #endif
9
10 #define BITS_PER_LONG __WORDSIZE
11 #include <linux/bits.h>
12 #include <linux/compiler.h>
13
14 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
15 #define BITS_TO_U64(nr)         DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
16 #define BITS_TO_U32(nr)         DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
17 #define BITS_TO_BYTES(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE)
18
19 extern unsigned int __sw_hweight8(unsigned int w);
20 extern unsigned int __sw_hweight16(unsigned int w);
21 extern unsigned int __sw_hweight32(unsigned int w);
22 extern unsigned long __sw_hweight64(__u64 w);
23
24 /*
25  * Include this here because some architectures need generic_ffs/fls in
26  * scope
27  *
28  * XXX: this needs to be asm/bitops.h, when we get to per arch optimizations
29  */
30 #include <asm-generic/bitops.h>
31
32 #define for_each_set_bit(bit, addr, size) \
33         for ((bit) = find_first_bit((addr), (size));            \
34              (bit) < (size);                                    \
35              (bit) = find_next_bit((addr), (size), (bit) + 1))
36
37 /* same as for_each_set_bit() but use bit as value to start with */
38 #define for_each_set_bit_from(bit, addr, size) \
39         for ((bit) = find_next_bit((addr), (size), (bit));      \
40              (bit) < (size);                                    \
41              (bit) = find_next_bit((addr), (size), (bit) + 1))
42
43 static inline unsigned long hweight_long(unsigned long w)
44 {
45         return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
46 }
47
48 static inline unsigned fls_long(unsigned long l)
49 {
50         if (sizeof(l) == 4)
51                 return fls(l);
52         return fls64(l);
53 }
54
55 #endif