specs/qcow2: Clarify that compressed clusters have the COPIED bit reset
[qemu.git] / include / qemu / bitops.h
blob3f0926cf40ca602037693a14cd86e8b195cc8ddf
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);
205 * rol8 - rotate an 8-bit value left
206 * @word: value to rotate
207 * @shift: bits to roll
209 static inline uint8_t rol8(uint8_t word, unsigned int shift)
211 return (word << shift) | (word >> ((8 - shift) & 7));
215 * ror8 - rotate an 8-bit value right
216 * @word: value to rotate
217 * @shift: bits to roll
219 static inline uint8_t ror8(uint8_t word, unsigned int shift)
221 return (word >> shift) | (word << ((8 - shift) & 7));
225 * rol16 - rotate a 16-bit value left
226 * @word: value to rotate
227 * @shift: bits to roll
229 static inline uint16_t rol16(uint16_t word, unsigned int shift)
231 return (word << shift) | (word >> ((16 - shift) & 15));
235 * ror16 - rotate a 16-bit value right
236 * @word: value to rotate
237 * @shift: bits to roll
239 static inline uint16_t ror16(uint16_t word, unsigned int shift)
241 return (word >> shift) | (word << ((16 - shift) & 15));
245 * rol32 - rotate a 32-bit value left
246 * @word: value to rotate
247 * @shift: bits to roll
249 static inline uint32_t rol32(uint32_t word, unsigned int shift)
251 return (word << shift) | (word >> ((32 - shift) & 31));
255 * ror32 - rotate a 32-bit value right
256 * @word: value to rotate
257 * @shift: bits to roll
259 static inline uint32_t ror32(uint32_t word, unsigned int shift)
261 return (word >> shift) | (word << ((32 - shift) & 31));
265 * rol64 - rotate a 64-bit value left
266 * @word: value to rotate
267 * @shift: bits to roll
269 static inline uint64_t rol64(uint64_t word, unsigned int shift)
271 return (word << shift) | (word >> ((64 - shift) & 63));
275 * ror64 - rotate a 64-bit value right
276 * @word: value to rotate
277 * @shift: bits to roll
279 static inline uint64_t ror64(uint64_t word, unsigned int shift)
281 return (word >> shift) | (word << ((64 - shift) & 63));
285 * extract32:
286 * @value: the value to extract the bit field from
287 * @start: the lowest bit in the bit field (numbered from 0)
288 * @length: the length of the bit field
290 * Extract from the 32 bit input @value the bit field specified by the
291 * @start and @length parameters, and return it. The bit field must
292 * lie entirely within the 32 bit word. It is valid to request that
293 * all 32 bits are returned (ie @length 32 and @start 0).
295 * Returns: the value of the bit field extracted from the input value.
297 static inline uint32_t extract32(uint32_t value, int start, int length)
299 assert(start >= 0 && length > 0 && length <= 32 - start);
300 return (value >> start) & (~0U >> (32 - length));
304 * extract64:
305 * @value: the value to extract the bit field from
306 * @start: the lowest bit in the bit field (numbered from 0)
307 * @length: the length of the bit field
309 * Extract from the 64 bit input @value the bit field specified by the
310 * @start and @length parameters, and return it. The bit field must
311 * lie entirely within the 64 bit word. It is valid to request that
312 * all 64 bits are returned (ie @length 64 and @start 0).
314 * Returns: the value of the bit field extracted from the input value.
316 static inline uint64_t extract64(uint64_t value, int start, int length)
318 assert(start >= 0 && length > 0 && length <= 64 - start);
319 return (value >> start) & (~0ULL >> (64 - length));
323 * sextract32:
324 * @value: the value to extract the bit field from
325 * @start: the lowest bit in the bit field (numbered from 0)
326 * @length: the length of the bit field
328 * Extract from the 32 bit input @value the bit field specified by the
329 * @start and @length parameters, and return it, sign extended to
330 * an int32_t (ie with the most significant bit of the field propagated
331 * to all the upper bits of the return value). The bit field must lie
332 * entirely within the 32 bit word. It is valid to request that
333 * all 32 bits are returned (ie @length 32 and @start 0).
335 * Returns: the sign extended value of the bit field extracted from the
336 * input value.
338 static inline int32_t sextract32(uint32_t value, int start, int length)
340 assert(start >= 0 && length > 0 && length <= 32 - start);
341 /* Note that this implementation relies on right shift of signed
342 * integers being an arithmetic shift.
344 return ((int32_t)(value << (32 - length - start))) >> (32 - length);
348 * sextract64:
349 * @value: the value to extract the bit field from
350 * @start: the lowest bit in the bit field (numbered from 0)
351 * @length: the length of the bit field
353 * Extract from the 64 bit input @value the bit field specified by the
354 * @start and @length parameters, and return it, sign extended to
355 * an int64_t (ie with the most significant bit of the field propagated
356 * to all the upper bits of the return value). The bit field must lie
357 * entirely within the 64 bit word. It is valid to request that
358 * all 64 bits are returned (ie @length 64 and @start 0).
360 * Returns: the sign extended value of the bit field extracted from the
361 * input value.
363 static inline int64_t sextract64(uint64_t value, int start, int length)
365 assert(start >= 0 && length > 0 && length <= 64 - start);
366 /* Note that this implementation relies on right shift of signed
367 * integers being an arithmetic shift.
369 return ((int64_t)(value << (64 - length - start))) >> (64 - length);
373 * deposit32:
374 * @value: initial value to insert bit field into
375 * @start: the lowest bit in the bit field (numbered from 0)
376 * @length: the length of the bit field
377 * @fieldval: the value to insert into the bit field
379 * Deposit @fieldval into the 32 bit @value at the bit field specified
380 * by the @start and @length parameters, and return the modified
381 * @value. Bits of @value outside the bit field are not modified.
382 * Bits of @fieldval above the least significant @length bits are
383 * ignored. The bit field must lie entirely within the 32 bit word.
384 * It is valid to request that all 32 bits are modified (ie @length
385 * 32 and @start 0).
387 * Returns: the modified @value.
389 static inline uint32_t deposit32(uint32_t value, int start, int length,
390 uint32_t fieldval)
392 uint32_t mask;
393 assert(start >= 0 && length > 0 && length <= 32 - start);
394 mask = (~0U >> (32 - length)) << start;
395 return (value & ~mask) | ((fieldval << start) & mask);
399 * deposit64:
400 * @value: initial value to insert bit field into
401 * @start: the lowest bit in the bit field (numbered from 0)
402 * @length: the length of the bit field
403 * @fieldval: the value to insert into the bit field
405 * Deposit @fieldval into the 64 bit @value at the bit field specified
406 * by the @start and @length parameters, and return the modified
407 * @value. Bits of @value outside the bit field are not modified.
408 * Bits of @fieldval above the least significant @length bits are
409 * ignored. The bit field must lie entirely within the 64 bit word.
410 * It is valid to request that all 64 bits are modified (ie @length
411 * 64 and @start 0).
413 * Returns: the modified @value.
415 static inline uint64_t deposit64(uint64_t value, int start, int length,
416 uint64_t fieldval)
418 uint64_t mask;
419 assert(start >= 0 && length > 0 && length <= 64 - start);
420 mask = (~0ULL >> (64 - length)) << start;
421 return (value & ~mask) | ((fieldval << start) & mask);
425 * half_shuffle32:
426 * @value: 32-bit value (of which only the bottom 16 bits are of interest)
428 * Given an input value:
429 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
430 * return the value where the bottom 16 bits are spread out into
431 * the odd bits in the word, and the even bits are zeroed:
432 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
434 * Any bits set in the top half of the input are ignored.
436 * Returns: the shuffled bits.
438 static inline uint32_t half_shuffle32(uint32_t x)
440 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
441 * It ignores any bits set in the top half of the input.
443 x = ((x & 0xFF00) << 8) | (x & 0x00FF);
444 x = ((x << 4) | x) & 0x0F0F0F0F;
445 x = ((x << 2) | x) & 0x33333333;
446 x = ((x << 1) | x) & 0x55555555;
447 return x;
451 * half_shuffle64:
452 * @value: 64-bit value (of which only the bottom 32 bits are of interest)
454 * Given an input value:
455 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
456 * return the value where the bottom 32 bits are spread out into
457 * the odd bits in the word, and the even bits are zeroed:
458 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
460 * Any bits set in the top half of the input are ignored.
462 * Returns: the shuffled bits.
464 static inline uint64_t half_shuffle64(uint64_t x)
466 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
467 * It ignores any bits set in the top half of the input.
469 x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
470 x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
471 x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
472 x = ((x << 2) | x) & 0x3333333333333333ULL;
473 x = ((x << 1) | x) & 0x5555555555555555ULL;
474 return x;
478 * half_unshuffle32:
479 * @value: 32-bit value (of which only the odd bits are of interest)
481 * Given an input value:
482 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
483 * return the value where all the odd bits are compressed down
484 * into the low half of the word, and the high half is zeroed:
485 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
487 * Any even bits set in the input are ignored.
489 * Returns: the unshuffled bits.
491 static inline uint32_t half_unshuffle32(uint32_t x)
493 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
494 * where it is called an inverse half shuffle.
496 x &= 0x55555555;
497 x = ((x >> 1) | x) & 0x33333333;
498 x = ((x >> 2) | x) & 0x0F0F0F0F;
499 x = ((x >> 4) | x) & 0x00FF00FF;
500 x = ((x >> 8) | x) & 0x0000FFFF;
501 return x;
505 * half_unshuffle64:
506 * @value: 64-bit value (of which only the odd bits are of interest)
508 * Given an input value:
509 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
510 * return the value where all the odd bits are compressed down
511 * into the low half of the word, and the high half is zeroed:
512 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
514 * Any even bits set in the input are ignored.
516 * Returns: the unshuffled bits.
518 static inline uint64_t half_unshuffle64(uint64_t x)
520 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
521 * where it is called an inverse half shuffle.
523 x &= 0x5555555555555555ULL;
524 x = ((x >> 1) | x) & 0x3333333333333333ULL;
525 x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
526 x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
527 x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
528 x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
529 return x;
532 #endif