xref: /kvmtool/include/linux/bitops.h (revision d9b64eb6aa05e552d6c9abf26ae9d87d9fb9c4ed)
1 #ifndef _KVM_LINUX_BITOPS_H_
2 #define _KVM_LINUX_BITOPS_H_
3 
4 #include <bits/wordsize.h>
5 
6 #include <linux/kernel.h>
7 #include <linux/compiler.h>
8 #include <asm/hweight.h>
9 
10 #define BITS_PER_LONG __WORDSIZE
11 #define BITS_PER_BYTE           8
12 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
13 
14 static inline void set_bit(int nr, unsigned long *addr)
15 {
16 	addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
17 }
18 
19 static inline void clear_bit(int nr, unsigned long *addr)
20 {
21 	addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
22 }
23 
24 static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
25 {
26 	return ((1UL << (nr % BITS_PER_LONG)) &
27 		(((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
28 }
29 
30 static inline unsigned long hweight_long(unsigned long w)
31 {
32 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
33 }
34 
35 #endif
36