Ingo Molnar | 2caa836 | 2011-05-05 10:00:45 +0200 | [diff] [blame] | 1 | #ifndef _KVM_LINUX_BITOPS_H_ |
| 2 | #define _KVM_LINUX_BITOPS_H_ |
| 3 | |
Alexandru Elisei | d9b64eb | 2022-04-12 14:32:22 +0100 | [diff] [blame] | 4 | #include <bits/wordsize.h> |
| 5 | |
Ingo Molnar | 2caa836 | 2011-05-05 10:00:45 +0200 | [diff] [blame] | 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 | |
Alexandru Elisei | 0febaae | 2022-04-12 14:32:28 +0100 | [diff] [blame] | 14 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
| 15 | |
Ingo Molnar | 2caa836 | 2011-05-05 10:00:45 +0200 | [diff] [blame] | 16 | static inline void set_bit(int nr, unsigned long *addr) |
| 17 | { |
| 18 | addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG); |
| 19 | } |
| 20 | |
| 21 | static inline void clear_bit(int nr, unsigned long *addr) |
| 22 | { |
| 23 | addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG)); |
| 24 | } |
| 25 | |
| 26 | static __always_inline int test_bit(unsigned int nr, const unsigned long *addr) |
| 27 | { |
| 28 | return ((1UL << (nr % BITS_PER_LONG)) & |
| 29 | (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; |
| 30 | } |
| 31 | |
| 32 | static inline unsigned long hweight_long(unsigned long w) |
| 33 | { |
| 34 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
| 35 | } |
| 36 | |
| 37 | #endif |