blob: ae33922f57436245ca1ccc491ae4637eb1602423 [file] [log] [blame]
Ingo Molnar2caa8362011-05-05 10:00:45 +02001#ifndef _KVM_LINUX_BITOPS_H_
2#define _KVM_LINUX_BITOPS_H_
3
Alexandru Eliseid9b64eb2022-04-12 14:32:22 +01004#include <bits/wordsize.h>
5
Ingo Molnar2caa8362011-05-05 10:00:45 +02006#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 Elisei0febaae2022-04-12 14:32:28 +010014#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
15
Ingo Molnar2caa8362011-05-05 10:00:45 +020016static inline void set_bit(int nr, unsigned long *addr)
17{
18 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
19}
20
21static inline void clear_bit(int nr, unsigned long *addr)
22{
23 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
24}
25
26static __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
32static inline unsigned long hweight_long(unsigned long w)
33{
34 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
35}
36
37#endif