6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu-common.h"
16 #include "host-utils.h"
18 #define BITS_PER_BYTE CHAR_BIT
19 #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
21 #define BIT(nr) (1UL << (nr))
22 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
23 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
24 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
27 * set_bit - Set a bit in memory
29 * @addr: the address to start counting from
31 static inline void set_bit(int nr, unsigned long *addr)
33 unsigned long mask = BIT_MASK(nr);
34 unsigned long *p = addr + BIT_WORD(nr);
40 * clear_bit - Clears a bit in memory
42 * @addr: Address to start counting from
44 static inline void clear_bit(int nr, unsigned long *addr)
46 unsigned long mask = BIT_MASK(nr);
47 unsigned long *p = addr + BIT_WORD(nr);
53 * change_bit - Toggle a bit in memory
55 * @addr: Address to start counting from
57 static inline void change_bit(int nr, unsigned long *addr)
59 unsigned long mask = BIT_MASK(nr);
60 unsigned long *p = addr + BIT_WORD(nr);
66 * test_and_set_bit - Set a bit and return its old value
68 * @addr: Address to count from
70 static inline int test_and_set_bit(int nr, unsigned long *addr)
72 unsigned long mask = BIT_MASK(nr);
73 unsigned long *p = addr + BIT_WORD(nr);
74 unsigned long old = *p;
77 return (old & mask) != 0;
81 * test_and_clear_bit - Clear a bit and return its old value
83 * @addr: Address to count from
85 static inline int test_and_clear_bit(int nr, unsigned long *addr)
87 unsigned long mask = BIT_MASK(nr);
88 unsigned long *p = addr + BIT_WORD(nr);
89 unsigned long old = *p;
92 return (old & mask) != 0;
96 * test_and_change_bit - Change a bit and return its old value
98 * @addr: Address to count from
100 static inline int test_and_change_bit(int nr, unsigned long *addr)
102 unsigned long mask = BIT_MASK(nr);
103 unsigned long *p = addr + BIT_WORD(nr);
104 unsigned long old = *p;
107 return (old & mask) != 0;
111 * test_bit - Determine whether a bit is set
112 * @nr: bit number to test
113 * @addr: Address to start counting from
115 static inline int test_bit(int nr, const unsigned long *addr)
117 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
121 * find_last_bit - find the last set bit in a memory region
122 * @addr: The address to start the search at
123 * @size: The maximum size to search
125 * Returns the bit number of the first set bit, or size.
127 unsigned long find_last_bit(const unsigned long *addr,
131 * find_next_bit - find the next set bit in a memory region
132 * @addr: The address to base the search on
133 * @offset: The bitnumber to start searching at
134 * @size: The bitmap size in bits
136 unsigned long find_next_bit(const unsigned long *addr,
137 unsigned long size, unsigned long offset);
140 * find_next_zero_bit - find the next cleared bit in a memory region
141 * @addr: The address to base the search on
142 * @offset: The bitnumber to start searching at
143 * @size: The bitmap size in bits
146 unsigned long find_next_zero_bit(const unsigned long *addr,
148 unsigned long offset);
151 * find_first_bit - find the first set bit in a memory region
152 * @addr: The address to start the search at
153 * @size: The maximum size to search
155 * Returns the bit number of the first set bit.
157 static inline unsigned long find_first_bit(const unsigned long *addr,
160 return find_next_bit(addr, size, 0);
164 * find_first_zero_bit - find the first cleared bit in a memory region
165 * @addr: The address to start the search at
166 * @size: The maximum size to search
168 * Returns the bit number of the first cleared bit.
170 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
173 return find_next_zero_bit(addr, size, 0);
176 static inline unsigned long hweight_long(unsigned long w)
180 for (count = 0; w; w >>= 1) {
188 * @value: the value to extract the bit field from
189 * @start: the lowest bit in the bit field (numbered from 0)
190 * @length: the length of the bit field
192 * Extract from the 32 bit input @value the bit field specified by the
193 * @start and @length parameters, and return it. The bit field must
194 * lie entirely within the 32 bit word. It is valid to request that
195 * all 32 bits are returned (ie @length 32 and @start 0).
197 * Returns: the value of the bit field extracted from the input value.
199 static inline uint32_t extract32(uint32_t value, int start, int length)
201 assert(start >= 0 && length > 0 && length <= 32 - start);
202 return (value >> start) & (~0U >> (32 - length));
207 * @value: the value to extract the bit field from
208 * @start: the lowest bit in the bit field (numbered from 0)
209 * @length: the length of the bit field
211 * Extract from the 64 bit input @value the bit field specified by the
212 * @start and @length parameters, and return it. The bit field must
213 * lie entirely within the 64 bit word. It is valid to request that
214 * all 64 bits are returned (ie @length 64 and @start 0).
216 * Returns: the value of the bit field extracted from the input value.
218 static inline uint64_t extract64(uint64_t value, int start, int length)
220 assert(start >= 0 && length > 0 && length <= 64 - start);
221 return (value >> start) & (~0ULL >> (64 - length));
226 * @value: the value to extract the bit field from
227 * @start: the lowest bit in the bit field (numbered from 0)
228 * @length: the length of the bit field
230 * Extract from the 32 bit input @value the bit field specified by the
231 * @start and @length parameters, and return it, sign extended to
232 * an int32_t (ie with the most significant bit of the field propagated
233 * to all the upper bits of the return value). The bit field must lie
234 * entirely within the 32 bit word. It is valid to request that
235 * all 32 bits are returned (ie @length 32 and @start 0).
237 * Returns: the sign extended value of the bit field extracted from the
240 static inline int32_t sextract32(uint32_t value, int start, int length)
242 assert(start >= 0 && length > 0 && length <= 32 - start);
243 /* Note that this implementation relies on right shift of signed
244 * integers being an arithmetic shift.
246 return ((int32_t)(value << (32 - length - start))) >> (32 - length);
251 * @value: the value to extract the bit field from
252 * @start: the lowest bit in the bit field (numbered from 0)
253 * @length: the length of the bit field
255 * Extract from the 64 bit input @value the bit field specified by the
256 * @start and @length parameters, and return it, sign extended to
257 * an int64_t (ie with the most significant bit of the field propagated
258 * to all the upper bits of the return value). The bit field must lie
259 * entirely within the 64 bit word. It is valid to request that
260 * all 64 bits are returned (ie @length 64 and @start 0).
262 * Returns: the sign extended value of the bit field extracted from the
265 static inline uint64_t sextract64(uint64_t value, int start, int length)
267 assert(start >= 0 && length > 0 && length <= 64 - start);
268 /* Note that this implementation relies on right shift of signed
269 * integers being an arithmetic shift.
271 return ((int64_t)(value << (64 - length - start))) >> (64 - length);
276 * @value: initial value to insert bit field into
277 * @start: the lowest bit in the bit field (numbered from 0)
278 * @length: the length of the bit field
279 * @fieldval: the value to insert into the bit field
281 * Deposit @fieldval into the 32 bit @value at the bit field specified
282 * by the @start and @length parameters, and return the modified
283 * @value. Bits of @value outside the bit field are not modified.
284 * Bits of @fieldval above the least significant @length bits are
285 * ignored. The bit field must lie entirely within the 32 bit word.
286 * It is valid to request that all 32 bits are modified (ie @length
289 * Returns: the modified @value.
291 static inline uint32_t deposit32(uint32_t value, int start, int length,
295 assert(start >= 0 && length > 0 && length <= 32 - start);
296 mask = (~0U >> (32 - length)) << start;
297 return (value & ~mask) | ((fieldval << start) & mask);
302 * @value: initial value to insert bit field into
303 * @start: the lowest bit in the bit field (numbered from 0)
304 * @length: the length of the bit field
305 * @fieldval: the value to insert into the bit field
307 * Deposit @fieldval into the 64 bit @value at the bit field specified
308 * by the @start and @length parameters, and return the modified
309 * @value. Bits of @value outside the bit field are not modified.
310 * Bits of @fieldval above the least significant @length bits are
311 * ignored. The bit field must lie entirely within the 64 bit word.
312 * It is valid to request that all 64 bits are modified (ie @length
315 * Returns: the modified @value.
317 static inline uint64_t deposit64(uint64_t value, int start, int length,
321 assert(start >= 0 && length > 0 && length <= 64 - start);
322 mask = (~0ULL >> (64 - length)) << start;
323 return (value & ~mask) | ((fieldval << start) & mask);