xref: /kvmtool/include/linux/bitops.h (revision 0592f8f829c843ff5cb2d108c309e32f4f6f5379)
1 #ifndef _KVM_LINUX_BITOPS_H_
2 #define _KVM_LINUX_BITOPS_H_
3 
4 #include <linux/kernel.h>
5 #include <linux/compiler.h>
6 #include <asm/hweight.h>
7 
8 #define BITS_PER_BYTE           8
9 #define BITS_PER_LONG           (BITS_PER_BYTE * sizeof(long))
10 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_LONG)
11 
12 #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
13 
set_bit(int nr,unsigned long * addr)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 
clear_bit(int nr,unsigned long * addr)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 
test_bit(unsigned int nr,const unsigned long * addr)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 
hweight_long(unsigned long w)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