Clean up header guards that don't match their file name
[qemu/kevin.git] / include / qemu / bitops.h
blob98fb005aba393bfc91abf50144cc8e96a4ad86a8
1 /*
2 * Bitops Module
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.
12 #ifndef BITOPS_H
13 #define BITOPS_H
16 #include "host-utils.h"
17 #include "atomic.h"
19 #define BITS_PER_BYTE CHAR_BIT
20 #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
22 #define BIT(nr) (1UL << (nr))
23 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
24 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
25 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
27 #define MAKE_64BIT_MASK(shift, length) \
28 (((~0ULL) >> (64 - (length))) << (shift))
30 /**
31 * set_bit - Set a bit in memory
32 * @nr: the bit to set
33 * @addr: the address to start counting from
35 static inline void set_bit(long nr, unsigned long *addr)
37 unsigned long mask = BIT_MASK(nr);
38 unsigned long *p = addr + BIT_WORD(nr);
40 *p |= mask;
43 /**
44 * set_bit_atomic - Set a bit in memory atomically
45 * @nr: the bit to set
46 * @addr: the address to start counting from
48 static inline void set_bit_atomic(long nr, unsigned long *addr)
50 unsigned long mask = BIT_MASK(nr);
51 unsigned long *p = addr + BIT_WORD(nr);
53 atomic_or(p, mask);
56 /**
57 * clear_bit - Clears a bit in memory
58 * @nr: Bit to clear
59 * @addr: Address to start counting from
61 static inline void clear_bit(long nr, unsigned long *addr)
63 unsigned long mask = BIT_MASK(nr);
64 unsigned long *p = addr + BIT_WORD(nr);
66 *p &= ~mask;
69 /**
70 * change_bit - Toggle a bit in memory
71 * @nr: Bit to change
72 * @addr: Address to start counting from
74 static inline void change_bit(long nr, unsigned long *addr)
76 unsigned long mask = BIT_MASK(nr);
77 unsigned long *p = addr + BIT_WORD(nr);
79 *p ^= mask;
82 /**
83 * test_and_set_bit - Set a bit and return its old value
84 * @nr: Bit to set
85 * @addr: Address to count from
87 static inline int test_and_set_bit(long nr, unsigned long *addr)
89 unsigned long mask = BIT_MASK(nr);
90 unsigned long *p = addr + BIT_WORD(nr);
91 unsigned long old = *p;
93 *p = old | mask;
94 return (old & mask) != 0;
97 /**
98 * test_and_clear_bit - Clear a bit and return its old value
99 * @nr: Bit to clear
100 * @addr: Address to count from
102 static inline int test_and_clear_bit(long nr, unsigned long *addr)
104 unsigned long mask = BIT_MASK(nr);
105 unsigned long *p = addr + BIT_WORD(nr);
106 unsigned long old = *p;
108 *p = old & ~mask;
109 return (old & mask) != 0;
113 * test_and_change_bit - Change a bit and return its old value
114 * @nr: Bit to change
115 * @addr: Address to count from
117 static inline int test_and_change_bit(long nr, unsigned long *addr)
119 unsigned long mask = BIT_MASK(nr);
120 unsigned long *p = addr + BIT_WORD(nr);
121 unsigned long old = *p;
123 *p = old ^ mask;
124 return (old & mask) != 0;
128 * test_bit - Determine whether a bit is set
129 * @nr: bit number to test
130 * @addr: Address to start counting from
132 static inline int test_bit(long nr, const unsigned long *addr)
134 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
138 * find_last_bit - find the last set bit in a memory region
139 * @addr: The address to start the search at
140 * @size: The maximum size to search
142 * Returns the bit number of the first set bit, or size.
144 unsigned long find_last_bit(const unsigned long *addr,
145 unsigned long size);
148 * find_next_bit - find the next set bit in a memory region
149 * @addr: The address to base the search on
150 * @offset: The bitnumber to start searching at
151 * @size: The bitmap size in bits
153 unsigned long find_next_bit(const unsigned long *addr,
154 unsigned long size,
155 unsigned long offset);
158 * find_next_zero_bit - find the next cleared bit in a memory region
159 * @addr: The address to base the search on
160 * @offset: The bitnumber to start searching at
161 * @size: The bitmap size in bits
164 unsigned long find_next_zero_bit(const unsigned long *addr,
165 unsigned long size,
166 unsigned long offset);
169 * find_first_bit - find the first set bit in a memory region
170 * @addr: The address to start the search at
171 * @size: The maximum size to search
173 * Returns the bit number of the first set bit.
175 static inline unsigned long find_first_bit(const unsigned long *addr,
176 unsigned long size)
178 unsigned long result, tmp;
180 for (result = 0; result < size; result += BITS_PER_LONG) {
181 tmp = *addr++;
182 if (tmp) {
183 result += ctzl(tmp);
184 return result < size ? result : size;
187 /* Not found */
188 return size;
192 * find_first_zero_bit - find the first cleared bit in a memory region
193 * @addr: The address to start the search at
194 * @size: The maximum size to search
196 * Returns the bit number of the first cleared bit.
198 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
199 unsigned long size)
201 return find_next_zero_bit(addr, size, 0);
204 static inline unsigned long hweight_long(unsigned long w)
206 unsigned long count;
208 for (count = 0; w; w >>= 1) {
209 count += w & 1;
211 return count;
215 * rol8 - rotate an 8-bit value left
216 * @word: value to rotate
217 * @shift: bits to roll
219 static inline uint8_t rol8(uint8_t word, unsigned int shift)
221 return (word << shift) | (word >> (8 - shift));
225 * ror8 - rotate an 8-bit value right
226 * @word: value to rotate
227 * @shift: bits to roll
229 static inline uint8_t ror8(uint8_t word, unsigned int shift)
231 return (word >> shift) | (word << (8 - shift));
235 * rol16 - rotate a 16-bit value left
236 * @word: value to rotate
237 * @shift: bits to roll
239 static inline uint16_t rol16(uint16_t word, unsigned int shift)
241 return (word << shift) | (word >> (16 - shift));
245 * ror16 - rotate a 16-bit value right
246 * @word: value to rotate
247 * @shift: bits to roll
249 static inline uint16_t ror16(uint16_t word, unsigned int shift)
251 return (word >> shift) | (word << (16 - shift));
255 * rol32 - rotate a 32-bit value left
256 * @word: value to rotate
257 * @shift: bits to roll
259 static inline uint32_t rol32(uint32_t word, unsigned int shift)
261 return (word << shift) | (word >> (32 - shift));
265 * ror32 - rotate a 32-bit value right
266 * @word: value to rotate
267 * @shift: bits to roll
269 static inline uint32_t ror32(uint32_t word, unsigned int shift)
271 return (word >> shift) | (word << (32 - shift));
275 * rol64 - rotate a 64-bit value left
276 * @word: value to rotate
277 * @shift: bits to roll
279 static inline uint64_t rol64(uint64_t word, unsigned int shift)
281 return (word << shift) | (word >> (64 - shift));
285 * ror64 - rotate a 64-bit value right
286 * @word: value to rotate
287 * @shift: bits to roll
289 static inline uint64_t ror64(uint64_t word, unsigned int shift)
291 return (word >> shift) | (word << (64 - shift));
295 * extract32:
296 * @value: the value to extract the bit field from
297 * @start: the lowest bit in the bit field (numbered from 0)
298 * @length: the length of the bit field
300 * Extract from the 32 bit input @value the bit field specified by the
301 * @start and @length parameters, and return it. The bit field must
302 * lie entirely within the 32 bit word. It is valid to request that
303 * all 32 bits are returned (ie @length 32 and @start 0).
305 * Returns: the value of the bit field extracted from the input value.
307 static inline uint32_t extract32(uint32_t value, int start, int length)
309 assert(start >= 0 && length > 0 && length <= 32 - start);
310 return (value >> start) & (~0U >> (32 - length));
314 * extract64:
315 * @value: the value to extract the bit field from
316 * @start: the lowest bit in the bit field (numbered from 0)
317 * @length: the length of the bit field
319 * Extract from the 64 bit input @value the bit field specified by the
320 * @start and @length parameters, and return it. The bit field must
321 * lie entirely within the 64 bit word. It is valid to request that
322 * all 64 bits are returned (ie @length 64 and @start 0).
324 * Returns: the value of the bit field extracted from the input value.
326 static inline uint64_t extract64(uint64_t value, int start, int length)
328 assert(start >= 0 && length > 0 && length <= 64 - start);
329 return (value >> start) & (~0ULL >> (64 - length));
333 * sextract32:
334 * @value: the value to extract the bit field from
335 * @start: the lowest bit in the bit field (numbered from 0)
336 * @length: the length of the bit field
338 * Extract from the 32 bit input @value the bit field specified by the
339 * @start and @length parameters, and return it, sign extended to
340 * an int32_t (ie with the most significant bit of the field propagated
341 * to all the upper bits of the return value). The bit field must lie
342 * entirely within the 32 bit word. It is valid to request that
343 * all 32 bits are returned (ie @length 32 and @start 0).
345 * Returns: the sign extended value of the bit field extracted from the
346 * input value.
348 static inline int32_t sextract32(uint32_t value, int start, int length)
350 assert(start >= 0 && length > 0 && length <= 32 - start);
351 /* Note that this implementation relies on right shift of signed
352 * integers being an arithmetic shift.
354 return ((int32_t)(value << (32 - length - start))) >> (32 - length);
358 * sextract64:
359 * @value: the value to extract the bit field from
360 * @start: the lowest bit in the bit field (numbered from 0)
361 * @length: the length of the bit field
363 * Extract from the 64 bit input @value the bit field specified by the
364 * @start and @length parameters, and return it, sign extended to
365 * an int64_t (ie with the most significant bit of the field propagated
366 * to all the upper bits of the return value). The bit field must lie
367 * entirely within the 64 bit word. It is valid to request that
368 * all 64 bits are returned (ie @length 64 and @start 0).
370 * Returns: the sign extended value of the bit field extracted from the
371 * input value.
373 static inline int64_t sextract64(uint64_t value, int start, int length)
375 assert(start >= 0 && length > 0 && length <= 64 - start);
376 /* Note that this implementation relies on right shift of signed
377 * integers being an arithmetic shift.
379 return ((int64_t)(value << (64 - length - start))) >> (64 - length);
383 * deposit32:
384 * @value: initial value to insert bit field into
385 * @start: the lowest bit in the bit field (numbered from 0)
386 * @length: the length of the bit field
387 * @fieldval: the value to insert into the bit field
389 * Deposit @fieldval into the 32 bit @value at the bit field specified
390 * by the @start and @length parameters, and return the modified
391 * @value. Bits of @value outside the bit field are not modified.
392 * Bits of @fieldval above the least significant @length bits are
393 * ignored. The bit field must lie entirely within the 32 bit word.
394 * It is valid to request that all 32 bits are modified (ie @length
395 * 32 and @start 0).
397 * Returns: the modified @value.
399 static inline uint32_t deposit32(uint32_t value, int start, int length,
400 uint32_t fieldval)
402 uint32_t mask;
403 assert(start >= 0 && length > 0 && length <= 32 - start);
404 mask = (~0U >> (32 - length)) << start;
405 return (value & ~mask) | ((fieldval << start) & mask);
409 * deposit64:
410 * @value: initial value to insert bit field into
411 * @start: the lowest bit in the bit field (numbered from 0)
412 * @length: the length of the bit field
413 * @fieldval: the value to insert into the bit field
415 * Deposit @fieldval into the 64 bit @value at the bit field specified
416 * by the @start and @length parameters, and return the modified
417 * @value. Bits of @value outside the bit field are not modified.
418 * Bits of @fieldval above the least significant @length bits are
419 * ignored. The bit field must lie entirely within the 64 bit word.
420 * It is valid to request that all 64 bits are modified (ie @length
421 * 64 and @start 0).
423 * Returns: the modified @value.
425 static inline uint64_t deposit64(uint64_t value, int start, int length,
426 uint64_t fieldval)
428 uint64_t mask;
429 assert(start >= 0 && length > 0 && length <= 64 - start);
430 mask = (~0ULL >> (64 - length)) << start;
431 return (value & ~mask) | ((fieldval << start) & mask);
435 * half_shuffle32:
436 * @value: 32-bit value (of which only the bottom 16 bits are of interest)
438 * Given an input value:
439 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
440 * return the value where the bottom 16 bits are spread out into
441 * the odd bits in the word, and the even bits are zeroed:
442 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
444 * Any bits set in the top half of the input are ignored.
446 * Returns: the shuffled bits.
448 static inline uint32_t half_shuffle32(uint32_t x)
450 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
451 * It ignores any bits set in the top half of the input.
453 x = ((x & 0xFF00) << 8) | (x & 0x00FF);
454 x = ((x << 4) | x) & 0x0F0F0F0F;
455 x = ((x << 2) | x) & 0x33333333;
456 x = ((x << 1) | x) & 0x55555555;
457 return x;
461 * half_shuffle64:
462 * @value: 64-bit value (of which only the bottom 32 bits are of interest)
464 * Given an input value:
465 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
466 * return the value where the bottom 32 bits are spread out into
467 * the odd bits in the word, and the even bits are zeroed:
468 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
470 * Any bits set in the top half of the input are ignored.
472 * Returns: the shuffled bits.
474 static inline uint64_t half_shuffle64(uint64_t x)
476 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
477 * It ignores any bits set in the top half of the input.
479 x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
480 x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
481 x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
482 x = ((x << 2) | x) & 0x3333333333333333ULL;
483 x = ((x << 1) | x) & 0x5555555555555555ULL;
484 return x;
488 * half_unshuffle32:
489 * @value: 32-bit value (of which only the odd bits are of interest)
491 * Given an input value:
492 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
493 * return the value where all the odd bits are compressed down
494 * into the low half of the word, and the high half is zeroed:
495 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
497 * Any even bits set in the input are ignored.
499 * Returns: the unshuffled bits.
501 static inline uint32_t half_unshuffle32(uint32_t x)
503 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
504 * where it is called an inverse half shuffle.
506 x &= 0x55555555;
507 x = ((x >> 1) | x) & 0x33333333;
508 x = ((x >> 2) | x) & 0x0F0F0F0F;
509 x = ((x >> 4) | x) & 0x00FF00FF;
510 x = ((x >> 8) | x) & 0x0000FFFF;
511 return x;
515 * half_unshuffle64:
516 * @value: 64-bit value (of which only the odd bits are of interest)
518 * Given an input value:
519 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
520 * return the value where all the odd bits are compressed down
521 * into the low half of the word, and the high half is zeroed:
522 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
524 * Any even bits set in the input are ignored.
526 * Returns: the unshuffled bits.
528 static inline uint64_t half_unshuffle64(uint64_t x)
530 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
531 * where it is called an inverse half shuffle.
533 x &= 0x5555555555555555ULL;
534 x = ((x >> 1) | x) & 0x3333333333333333ULL;
535 x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
536 x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
537 x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
538 x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
539 return x;
542 #endif