xref: /kvm-unit-tests/lib/bitops.h (revision 4363f1d9a646a5c7ea673bee8fc33ca6f2cddbd8)
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 #endif
37