4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
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"
17 #define BITS_PER_BYTE CHAR_BIT
18 #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
20 #define BIT(nr) (1UL << (nr))
21 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
22 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
23 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
26 * bitops_ffs - find first bit in word.
27 * @word: The word to search
29 * Undefined if no bit exists, so code should check against 0 first.
31 static unsigned long bitops_ffsl(unsigned long word
)
35 #if LONG_MAX > 0x7FFFFFFF
36 if ((word
& 0xffffffff) == 0) {
41 if ((word
& 0xffff) == 0) {
45 if ((word
& 0xff) == 0) {
49 if ((word
& 0xf) == 0) {
53 if ((word
& 0x3) == 0) {
57 if ((word
& 0x1) == 0) {
64 * bitops_fls - find last (most-significant) set bit in a long word
65 * @word: the word to search
67 * Undefined if no set bit exists, so code should check against 0 first.
69 static inline unsigned long bitops_flsl(unsigned long word
)
71 int num
= BITS_PER_LONG
- 1;
73 #if LONG_MAX > 0x7FFFFFFF
74 if (!(word
& (~0ul << 32))) {
79 if (!(word
& (~0ul << (BITS_PER_LONG
-16)))) {
83 if (!(word
& (~0ul << (BITS_PER_LONG
-8)))) {
87 if (!(word
& (~0ul << (BITS_PER_LONG
-4)))) {
91 if (!(word
& (~0ul << (BITS_PER_LONG
-2)))) {
96 if (!(word
& (~0ul << (BITS_PER_LONG
-1))))
102 * ffz - find first zero in word.
103 * @word: The word to search
105 * Undefined if no zero exists, so code should check against ~0UL first.
107 static inline unsigned long ffz(unsigned long word
)
109 return bitops_ffsl(~word
);
113 * set_bit - Set a bit in memory
114 * @nr: the bit to set
115 * @addr: the address to start counting from
117 static inline void set_bit(int nr
, volatile unsigned long *addr
)
119 unsigned long mask
= BIT_MASK(nr
);
120 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
126 * clear_bit - Clears a bit in memory
128 * @addr: Address to start counting from
130 static inline void clear_bit(int nr
, volatile unsigned long *addr
)
132 unsigned long mask
= BIT_MASK(nr
);
133 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
139 * change_bit - Toggle a bit in memory
141 * @addr: Address to start counting from
143 static inline void change_bit(int nr
, volatile unsigned long *addr
)
145 unsigned long mask
= BIT_MASK(nr
);
146 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
152 * test_and_set_bit - Set a bit and return its old value
154 * @addr: Address to count from
156 static inline int test_and_set_bit(int nr
, volatile unsigned long *addr
)
158 unsigned long mask
= BIT_MASK(nr
);
159 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
160 unsigned long old
= *p
;
163 return (old
& mask
) != 0;
167 * test_and_clear_bit - Clear a bit and return its old value
169 * @addr: Address to count from
171 static inline int test_and_clear_bit(int nr
, volatile unsigned long *addr
)
173 unsigned long mask
= BIT_MASK(nr
);
174 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
175 unsigned long old
= *p
;
178 return (old
& mask
) != 0;
182 * test_and_change_bit - Change a bit and return its old value
184 * @addr: Address to count from
186 static inline int test_and_change_bit(int nr
, volatile unsigned long *addr
)
188 unsigned long mask
= BIT_MASK(nr
);
189 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
190 unsigned long old
= *p
;
193 return (old
& mask
) != 0;
197 * test_bit - Determine whether a bit is set
198 * @nr: bit number to test
199 * @addr: Address to start counting from
201 static inline int test_bit(int nr
, const volatile unsigned long *addr
)
203 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
207 * find_last_bit - find the last set bit in a memory region
208 * @addr: The address to start the search at
209 * @size: The maximum size to search
211 * Returns the bit number of the first set bit, or size.
213 unsigned long find_last_bit(const unsigned long *addr
,
217 * find_next_bit - find the next set bit in a memory region
218 * @addr: The address to base the search on
219 * @offset: The bitnumber to start searching at
220 * @size: The bitmap size in bits
222 unsigned long find_next_bit(const unsigned long *addr
,
223 unsigned long size
, unsigned long offset
);
226 * find_next_zero_bit - find the next cleared bit in a memory region
227 * @addr: The address to base the search on
228 * @offset: The bitnumber to start searching at
229 * @size: The bitmap size in bits
232 unsigned long find_next_zero_bit(const unsigned long *addr
,
234 unsigned long offset
);
237 * find_first_bit - find the first set bit in a memory region
238 * @addr: The address to start the search at
239 * @size: The maximum size to search
241 * Returns the bit number of the first set bit.
243 static inline unsigned long find_first_bit(const unsigned long *addr
,
246 return find_next_bit(addr
, size
, 0);
250 * find_first_zero_bit - find the first cleared bit in a memory region
251 * @addr: The address to start the search at
252 * @size: The maximum size to search
254 * Returns the bit number of the first cleared bit.
256 static inline unsigned long find_first_zero_bit(const unsigned long *addr
,
259 return find_next_zero_bit(addr
, size
, 0);
262 static inline unsigned long hweight_long(unsigned long w
)
266 for (count
= 0; w
; w
>>= 1) {
274 * @value: the value to extract the bit field from
275 * @start: the lowest bit in the bit field (numbered from 0)
276 * @length: the length of the bit field
278 * Extract from the 32 bit input @value the bit field specified by the
279 * @start and @length parameters, and return it. The bit field must
280 * lie entirely within the 32 bit word. It is valid to request that
281 * all 32 bits are returned (ie @length 32 and @start 0).
283 * Returns: the value of the bit field extracted from the input value.
285 static inline uint32_t extract32(uint32_t value
, int start
, int length
)
287 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
288 return (value
>> start
) & (~0U >> (32 - length
));
293 * @value: the value to extract the bit field from
294 * @start: the lowest bit in the bit field (numbered from 0)
295 * @length: the length of the bit field
297 * Extract from the 64 bit input @value the bit field specified by the
298 * @start and @length parameters, and return it. The bit field must
299 * lie entirely within the 64 bit word. It is valid to request that
300 * all 64 bits are returned (ie @length 64 and @start 0).
302 * Returns: the value of the bit field extracted from the input value.
304 static inline uint64_t extract64(uint64_t value
, int start
, int length
)
306 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
307 return (value
>> start
) & (~0ULL >> (64 - length
));
312 * @value: initial value to insert bit field into
313 * @start: the lowest bit in the bit field (numbered from 0)
314 * @length: the length of the bit field
315 * @fieldval: the value to insert into the bit field
317 * Deposit @fieldval into the 32 bit @value at the bit field specified
318 * by the @start and @length parameters, and return the modified
319 * @value. Bits of @value outside the bit field are not modified.
320 * Bits of @fieldval above the least significant @length bits are
321 * ignored. The bit field must lie entirely within the 32 bit word.
322 * It is valid to request that all 32 bits are modified (ie @length
325 * Returns: the modified @value.
327 static inline uint32_t deposit32(uint32_t value
, int start
, int length
,
331 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
332 mask
= (~0U >> (32 - length
)) << start
;
333 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
338 * @value: initial value to insert bit field into
339 * @start: the lowest bit in the bit field (numbered from 0)
340 * @length: the length of the bit field
341 * @fieldval: the value to insert into the bit field
343 * Deposit @fieldval into the 64 bit @value at the bit field specified
344 * by the @start and @length parameters, and return the modified
345 * @value. Bits of @value outside the bit field are not modified.
346 * Bits of @fieldval above the least significant @length bits are
347 * ignored. The bit field must lie entirely within the 64 bit word.
348 * It is valid to request that all 64 bits are modified (ie @length
351 * Returns: the modified @value.
353 static inline uint64_t deposit64(uint64_t value
, int start
, int length
,
357 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
358 mask
= (~0ULL >> (64 - length
)) << start
;
359 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);