xref: /kvm-unit-tests/lib/bitops.h (revision 9e801bd9d2d4b37bfad26123bcd58548adb0d82c)
1 #ifndef _BITOPS_H_
2 #define _BITOPS_H_
3 
4 /*
5  * Adapted from
6  *   include/linux/bitops.h
7  *
8  * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.
11  */
12 
13 #define BITS_PER_LONG_LONG	64
14 #define BIT(nr)			(1UL << (nr))
15 #define BIT_ULL(nr)		(1ULL << (nr))
16 #define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
17 #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
18 #define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
19 #define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
20 #define BITS_PER_BYTE		8
21 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
22 
23 #include <asm/bitops.h>
24 
25 /*
26  * Create a contiguous bitmask starting at bit position @l and ending at
27  * position @h. For example
28  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
29  */
30 #define GENMASK(h, l) \
31 	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
32 
33 #define GENMASK_ULL(h, l) \
34 	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
35 
36 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
37 #define lower_32_bits(n) ((u32)(n))
38 
39 #ifndef HAVE_BUILTIN_FLS
40 static inline unsigned long fls(unsigned long word)
41 {
42 	int num = BITS_PER_LONG - 1;
43 
44 #if BITS_PER_LONG == 64
45 	if (!(word & (~0ul << 32))) {
46 		num -= 32;
47 		word <<= 32;
48 	}
49 #endif
50 	if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
51 		num -= 16;
52 		word <<= 16;
53 	}
54 	if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
55 		num -= 8;
56 		word <<= 8;
57 	}
58 	if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
59 		num -= 4;
60 		word <<= 4;
61 	}
62 	if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
63 		num -= 2;
64 		word <<= 2;
65 	}
66 	if (!(word & (~0ul << (BITS_PER_LONG-1))))
67 		num -= 1;
68 	return num;
69 }
70 #else
71 static inline unsigned long fls(unsigned long word)
72 {
73 	return BITS_PER_LONG - __builtin_clzl(word) - 1;
74 }
75 #endif
76 
77 static inline bool is_power_of_2(unsigned long n)
78 {
79 	return n && !(n & (n - 1));
80 }
81 
82 static inline unsigned int get_order(size_t size)
83 {
84 	return size ? fls(size) + !is_power_of_2(size) : 0;
85 }
86 
87 #endif
88