]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu/bitops.h
Merge tag 'misc-fixes-20231113' of https://github.com/philmd/qemu into staging
[mirror_qemu.git] / include / qemu / bitops.h
CommitLineData
e0e53b2f
CC
1/*
2 * Bitops Module
3 *
4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
5 *
6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
7 *
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.
10 */
11
12#ifndef BITOPS_H
13#define BITOPS_H
14
afeeead1 15
fbeadf50 16#include "host-utils.h"
9f02cfc8 17#include "atomic.h"
e0e53b2f
CC
18
19#define BITS_PER_BYTE CHAR_BIT
20#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
21
4188e390 22#define BIT(nr) (1UL << (nr))
0df9142d 23#define BIT_ULL(nr) (1ULL << (nr))
4188e390
CMC
24#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
25#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
26#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
e0e53b2f 27
ae2923b5
AF
28#define MAKE_64BIT_MASK(shift, length) \
29 (((~0ULL) >> (64 - (length))) << (shift))
30
e0e53b2f
CC
31/**
32 * set_bit - Set a bit in memory
33 * @nr: the bit to set
34 * @addr: the address to start counting from
35 */
9c22687e 36static inline void set_bit(long nr, unsigned long *addr)
e0e53b2f 37{
4188e390
CMC
38 unsigned long mask = BIT_MASK(nr);
39 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f 40
4188e390 41 *p |= mask;
e0e53b2f
CC
42}
43
9f02cfc8
SH
44/**
45 * set_bit_atomic - Set a bit in memory atomically
46 * @nr: the bit to set
47 * @addr: the address to start counting from
48 */
49static inline void set_bit_atomic(long nr, unsigned long *addr)
50{
51 unsigned long mask = BIT_MASK(nr);
52 unsigned long *p = addr + BIT_WORD(nr);
53
d73415a3 54 qatomic_or(p, mask);
9f02cfc8
SH
55}
56
e0e53b2f
CC
57/**
58 * clear_bit - Clears a bit in memory
59 * @nr: Bit to clear
60 * @addr: Address to start counting from
61 */
9c22687e 62static inline void clear_bit(long nr, unsigned long *addr)
e0e53b2f 63{
4188e390
CMC
64 unsigned long mask = BIT_MASK(nr);
65 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f 66
4188e390 67 *p &= ~mask;
e0e53b2f
CC
68}
69
70/**
71 * change_bit - Toggle a bit in memory
72 * @nr: Bit to change
73 * @addr: Address to start counting from
74 */
9c22687e 75static inline void change_bit(long nr, unsigned long *addr)
e0e53b2f 76{
4188e390
CMC
77 unsigned long mask = BIT_MASK(nr);
78 unsigned long *p = addr + BIT_WORD(nr);
e0e53b2f 79
4188e390 80 *p ^= mask;
e0e53b2f
CC
81}
82
83/**
84 * test_and_set_bit - Set a bit and return its old value
85 * @nr: Bit to set
86 * @addr: Address to count from
87 */
9c22687e 88static inline int test_and_set_bit(long nr, unsigned long *addr)
e0e53b2f 89{
4188e390
CMC
90 unsigned long mask = BIT_MASK(nr);
91 unsigned long *p = addr + BIT_WORD(nr);
92 unsigned long old = *p;
e0e53b2f 93
4188e390
CMC
94 *p = old | mask;
95 return (old & mask) != 0;
e0e53b2f
CC
96}
97
98/**
99 * test_and_clear_bit - Clear a bit and return its old value
100 * @nr: Bit to clear
101 * @addr: Address to count from
102 */
9c22687e 103static inline int test_and_clear_bit(long nr, unsigned long *addr)
e0e53b2f 104{
4188e390
CMC
105 unsigned long mask = BIT_MASK(nr);
106 unsigned long *p = addr + BIT_WORD(nr);
107 unsigned long old = *p;
e0e53b2f 108
4188e390
CMC
109 *p = old & ~mask;
110 return (old & mask) != 0;
e0e53b2f
CC
111}
112
113/**
114 * test_and_change_bit - Change a bit and return its old value
115 * @nr: Bit to change
116 * @addr: Address to count from
117 */
9c22687e 118static inline int test_and_change_bit(long nr, unsigned long *addr)
e0e53b2f 119{
4188e390
CMC
120 unsigned long mask = BIT_MASK(nr);
121 unsigned long *p = addr + BIT_WORD(nr);
122 unsigned long old = *p;
e0e53b2f 123
4188e390
CMC
124 *p = old ^ mask;
125 return (old & mask) != 0;
e0e53b2f
CC
126}
127
128/**
129 * test_bit - Determine whether a bit is set
130 * @nr: bit number to test
131 * @addr: Address to start counting from
132 */
9c22687e 133static inline int test_bit(long nr, const unsigned long *addr)
e0e53b2f 134{
4188e390 135 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
e0e53b2f
CC
136}
137
138/**
139 * find_last_bit - find the last set bit in a memory region
140 * @addr: The address to start the search at
141 * @size: The maximum size to search
142 *
5c6ae58d
PMD
143 * Returns the bit number of the last set bit,
144 * or @size if there is no set bit in the bitmap.
e0e53b2f
CC
145 */
146unsigned long find_last_bit(const unsigned long *addr,
147 unsigned long size);
148
149/**
150 * find_next_bit - find the next set bit in a memory region
151 * @addr: The address to base the search on
152 * @offset: The bitnumber to start searching at
153 * @size: The bitmap size in bits
5c6ae58d
PMD
154 *
155 * Returns the bit number of the next set bit,
156 * or @size if there are no further set bits in the bitmap.
e0e53b2f
CC
157 */
158unsigned long find_next_bit(const unsigned long *addr,
4188e390
CMC
159 unsigned long size,
160 unsigned long offset);
e0e53b2f
CC
161
162/**
163 * find_next_zero_bit - find the next cleared bit in a memory region
164 * @addr: The address to base the search on
165 * @offset: The bitnumber to start searching at
166 * @size: The bitmap size in bits
5c6ae58d
PMD
167 *
168 * Returns the bit number of the next cleared bit,
169 * or @size if there are no further clear bits in the bitmap.
e0e53b2f
CC
170 */
171
172unsigned long find_next_zero_bit(const unsigned long *addr,
173 unsigned long size,
174 unsigned long offset);
175
176/**
177 * find_first_bit - find the first set bit in a memory region
178 * @addr: The address to start the search at
179 * @size: The maximum size to search
180 *
5c6ae58d
PMD
181 * Returns the bit number of the first set bit,
182 * or @size if there is no set bit in the bitmap.
e0e53b2f
CC
183 */
184static inline unsigned long find_first_bit(const unsigned long *addr,
185 unsigned long size)
186{
739b7a90
AJ
187 unsigned long result, tmp;
188
189 for (result = 0; result < size; result += BITS_PER_LONG) {
190 tmp = *addr++;
191 if (tmp) {
192 result += ctzl(tmp);
193 return result < size ? result : size;
194 }
195 }
196 /* Not found */
197 return size;
e0e53b2f
CC
198}
199
200/**
201 * find_first_zero_bit - find the first cleared bit in a memory region
202 * @addr: The address to start the search at
203 * @size: The maximum size to search
204 *
5c6ae58d
PMD
205 * Returns the bit number of the first cleared bit,
206 * or @size if there is no clear bit in the bitmap.
e0e53b2f
CC
207 */
208static inline unsigned long find_first_zero_bit(const unsigned long *addr,
209 unsigned long size)
210{
211 return find_next_zero_bit(addr, size, 0);
212}
213
6aa25b4a
SW
214/**
215 * rol8 - rotate an 8-bit value left
216 * @word: value to rotate
217 * @shift: bits to roll
218 */
219static inline uint8_t rol8(uint8_t word, unsigned int shift)
220{
8841c815 221 return (word << (shift & 7)) | (word >> (-shift & 7));
6aa25b4a
SW
222}
223
224/**
225 * ror8 - rotate an 8-bit value right
226 * @word: value to rotate
227 * @shift: bits to roll
228 */
229static inline uint8_t ror8(uint8_t word, unsigned int shift)
230{
8841c815 231 return (word >> (shift & 7)) | (word << (-shift & 7));
6aa25b4a
SW
232}
233
234/**
235 * rol16 - rotate a 16-bit value left
236 * @word: value to rotate
237 * @shift: bits to roll
238 */
239static inline uint16_t rol16(uint16_t word, unsigned int shift)
240{
8841c815 241 return (word << (shift & 15)) | (word >> (-shift & 15));
6aa25b4a
SW
242}
243
244/**
245 * ror16 - rotate a 16-bit value right
246 * @word: value to rotate
247 * @shift: bits to roll
248 */
249static inline uint16_t ror16(uint16_t word, unsigned int shift)
250{
8841c815 251 return (word >> (shift & 15)) | (word << (-shift & 15));
6aa25b4a
SW
252}
253
254/**
255 * rol32 - rotate a 32-bit value left
256 * @word: value to rotate
257 * @shift: bits to roll
258 */
259static inline uint32_t rol32(uint32_t word, unsigned int shift)
260{
8841c815 261 return (word << (shift & 31)) | (word >> (-shift & 31));
6aa25b4a
SW
262}
263
264/**
265 * ror32 - rotate a 32-bit value right
266 * @word: value to rotate
267 * @shift: bits to roll
268 */
269static inline uint32_t ror32(uint32_t word, unsigned int shift)
270{
8841c815 271 return (word >> (shift & 31)) | (word << (-shift & 31));
6aa25b4a
SW
272}
273
274/**
275 * rol64 - rotate a 64-bit value left
276 * @word: value to rotate
277 * @shift: bits to roll
278 */
279static inline uint64_t rol64(uint64_t word, unsigned int shift)
280{
8841c815 281 return (word << (shift & 63)) | (word >> (-shift & 63));
6aa25b4a
SW
282}
283
284/**
285 * ror64 - rotate a 64-bit value right
286 * @word: value to rotate
287 * @shift: bits to roll
288 */
289static inline uint64_t ror64(uint64_t word, unsigned int shift)
290{
8841c815 291 return (word >> (shift & 63)) | (word << (-shift & 63));
6aa25b4a
SW
292}
293
dbcf6f93
PM
294/**
295 * hswap32 - swap 16-bit halfwords within a 32-bit value
296 * @h: value to swap
297 */
298static inline uint32_t hswap32(uint32_t h)
299{
300 return rol32(h, 16);
301}
302
303/**
304 * hswap64 - swap 16-bit halfwords within a 64-bit value
305 * @h: value to swap
306 */
307static inline uint64_t hswap64(uint64_t h)
308{
309 uint64_t m = 0x0000ffff0000ffffull;
310 h = rol64(h, 32);
311 return ((h & m) << 16) | ((h >> 16) & m);
312}
313
314/**
315 * wswap64 - swap 32-bit words within a 64-bit value
316 * @h: value to swap
317 */
318static inline uint64_t wswap64(uint64_t h)
319{
320 return rol64(h, 32);
321}
322
84988cf9
PM
323/**
324 * extract32:
325 * @value: the value to extract the bit field from
326 * @start: the lowest bit in the bit field (numbered from 0)
327 * @length: the length of the bit field
328 *
329 * Extract from the 32 bit input @value the bit field specified by the
330 * @start and @length parameters, and return it. The bit field must
331 * lie entirely within the 32 bit word. It is valid to request that
332 * all 32 bits are returned (ie @length 32 and @start 0).
333 *
334 * Returns: the value of the bit field extracted from the input value.
335 */
336static inline uint32_t extract32(uint32_t value, int start, int length)
337{
338 assert(start >= 0 && length > 0 && length <= 32 - start);
339 return (value >> start) & (~0U >> (32 - length));
340}
341
ed04c8b1
YS
342/**
343 * extract8:
344 * @value: the value to extract the bit field from
345 * @start: the lowest bit in the bit field (numbered from 0)
346 * @length: the length of the bit field
347 *
348 * Extract from the 8 bit input @value the bit field specified by the
349 * @start and @length parameters, and return it. The bit field must
350 * lie entirely within the 8 bit word. It is valid to request that
351 * all 8 bits are returned (ie @length 8 and @start 0).
352 *
353 * Returns: the value of the bit field extracted from the input value.
354 */
355static inline uint8_t extract8(uint8_t value, int start, int length)
356{
357 assert(start >= 0 && length > 0 && length <= 8 - start);
358 return extract32(value, start, length);
359}
360
361/**
362 * extract16:
363 * @value: the value to extract the bit field from
364 * @start: the lowest bit in the bit field (numbered from 0)
365 * @length: the length of the bit field
366 *
367 * Extract from the 16 bit input @value the bit field specified by the
368 * @start and @length parameters, and return it. The bit field must
369 * lie entirely within the 16 bit word. It is valid to request that
370 * all 16 bits are returned (ie @length 16 and @start 0).
371 *
372 * Returns: the value of the bit field extracted from the input value.
373 */
374static inline uint16_t extract16(uint16_t value, int start, int length)
375{
376 assert(start >= 0 && length > 0 && length <= 16 - start);
377 return extract32(value, start, length);
378}
379
84988cf9
PM
380/**
381 * extract64:
382 * @value: the value to extract the bit field from
383 * @start: the lowest bit in the bit field (numbered from 0)
384 * @length: the length of the bit field
385 *
386 * Extract from the 64 bit input @value the bit field specified by the
387 * @start and @length parameters, and return it. The bit field must
388 * lie entirely within the 64 bit word. It is valid to request that
389 * all 64 bits are returned (ie @length 64 and @start 0).
390 *
391 * Returns: the value of the bit field extracted from the input value.
392 */
393static inline uint64_t extract64(uint64_t value, int start, int length)
394{
395 assert(start >= 0 && length > 0 && length <= 64 - start);
396 return (value >> start) & (~0ULL >> (64 - length));
397}
398
2dc6bebd
PM
399/**
400 * sextract32:
401 * @value: the value to extract the bit field from
402 * @start: the lowest bit in the bit field (numbered from 0)
403 * @length: the length of the bit field
404 *
405 * Extract from the 32 bit input @value the bit field specified by the
406 * @start and @length parameters, and return it, sign extended to
407 * an int32_t (ie with the most significant bit of the field propagated
408 * to all the upper bits of the return value). The bit field must lie
409 * entirely within the 32 bit word. It is valid to request that
410 * all 32 bits are returned (ie @length 32 and @start 0).
411 *
412 * Returns: the sign extended value of the bit field extracted from the
413 * input value.
414 */
415static inline int32_t sextract32(uint32_t value, int start, int length)
416{
417 assert(start >= 0 && length > 0 && length <= 32 - start);
418 /* Note that this implementation relies on right shift of signed
419 * integers being an arithmetic shift.
420 */
421 return ((int32_t)(value << (32 - length - start))) >> (32 - length);
422}
423
424/**
425 * sextract64:
426 * @value: the value to extract the bit field from
427 * @start: the lowest bit in the bit field (numbered from 0)
428 * @length: the length of the bit field
429 *
430 * Extract from the 64 bit input @value the bit field specified by the
431 * @start and @length parameters, and return it, sign extended to
432 * an int64_t (ie with the most significant bit of the field propagated
433 * to all the upper bits of the return value). The bit field must lie
434 * entirely within the 64 bit word. It is valid to request that
435 * all 64 bits are returned (ie @length 64 and @start 0).
436 *
437 * Returns: the sign extended value of the bit field extracted from the
438 * input value.
439 */
4f995052 440static inline int64_t sextract64(uint64_t value, int start, int length)
2dc6bebd
PM
441{
442 assert(start >= 0 && length > 0 && length <= 64 - start);
443 /* Note that this implementation relies on right shift of signed
444 * integers being an arithmetic shift.
445 */
446 return ((int64_t)(value << (64 - length - start))) >> (64 - length);
447}
448
84988cf9
PM
449/**
450 * deposit32:
451 * @value: initial value to insert bit field into
452 * @start: the lowest bit in the bit field (numbered from 0)
453 * @length: the length of the bit field
454 * @fieldval: the value to insert into the bit field
455 *
456 * Deposit @fieldval into the 32 bit @value at the bit field specified
457 * by the @start and @length parameters, and return the modified
458 * @value. Bits of @value outside the bit field are not modified.
459 * Bits of @fieldval above the least significant @length bits are
460 * ignored. The bit field must lie entirely within the 32 bit word.
ab411770
SW
461 * It is valid to request that all 32 bits are modified (ie @length
462 * 32 and @start 0).
84988cf9
PM
463 *
464 * Returns: the modified @value.
465 */
466static inline uint32_t deposit32(uint32_t value, int start, int length,
467 uint32_t fieldval)
468{
469 uint32_t mask;
470 assert(start >= 0 && length > 0 && length <= 32 - start);
471 mask = (~0U >> (32 - length)) << start;
472 return (value & ~mask) | ((fieldval << start) & mask);
473}
474
475/**
ab411770 476 * deposit64:
84988cf9
PM
477 * @value: initial value to insert bit field into
478 * @start: the lowest bit in the bit field (numbered from 0)
479 * @length: the length of the bit field
480 * @fieldval: the value to insert into the bit field
481 *
482 * Deposit @fieldval into the 64 bit @value at the bit field specified
483 * by the @start and @length parameters, and return the modified
484 * @value. Bits of @value outside the bit field are not modified.
485 * Bits of @fieldval above the least significant @length bits are
ab411770 486 * ignored. The bit field must lie entirely within the 64 bit word.
84988cf9
PM
487 * It is valid to request that all 64 bits are modified (ie @length
488 * 64 and @start 0).
489 *
490 * Returns: the modified @value.
491 */
492static inline uint64_t deposit64(uint64_t value, int start, int length,
493 uint64_t fieldval)
494{
495 uint64_t mask;
496 assert(start >= 0 && length > 0 && length <= 64 - start);
497 mask = (~0ULL >> (64 - length)) << start;
498 return (value & ~mask) | ((fieldval << start) & mask);
499}
500
b355438d
PM
501/**
502 * half_shuffle32:
7d41d764
PM
503 * @x: 32-bit value (of which only the bottom 16 bits are of interest)
504 *
505 * Given an input value::
506 *
507 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
b355438d 508 *
b355438d 509 * return the value where the bottom 16 bits are spread out into
7d41d764
PM
510 * the odd bits in the word, and the even bits are zeroed::
511 *
512 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
b355438d
PM
513 *
514 * Any bits set in the top half of the input are ignored.
515 *
516 * Returns: the shuffled bits.
517 */
518static inline uint32_t half_shuffle32(uint32_t x)
519{
520 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
521 * It ignores any bits set in the top half of the input.
522 */
523 x = ((x & 0xFF00) << 8) | (x & 0x00FF);
524 x = ((x << 4) | x) & 0x0F0F0F0F;
525 x = ((x << 2) | x) & 0x33333333;
526 x = ((x << 1) | x) & 0x55555555;
527 return x;
528}
529
530/**
531 * half_shuffle64:
7d41d764
PM
532 * @x: 64-bit value (of which only the bottom 32 bits are of interest)
533 *
534 * Given an input value::
535 *
536 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
b355438d 537 *
b355438d 538 * return the value where the bottom 32 bits are spread out into
7d41d764
PM
539 * the odd bits in the word, and the even bits are zeroed::
540 *
541 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
b355438d
PM
542 *
543 * Any bits set in the top half of the input are ignored.
544 *
545 * Returns: the shuffled bits.
546 */
547static inline uint64_t half_shuffle64(uint64_t x)
548{
549 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
550 * It ignores any bits set in the top half of the input.
551 */
552 x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
553 x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
554 x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
555 x = ((x << 2) | x) & 0x3333333333333333ULL;
556 x = ((x << 1) | x) & 0x5555555555555555ULL;
557 return x;
558}
559
560/**
561 * half_unshuffle32:
7d41d764
PM
562 * @x: 32-bit value (of which only the odd bits are of interest)
563 *
564 * Given an input value::
565 *
566 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
b355438d 567 *
b355438d 568 * return the value where all the odd bits are compressed down
7d41d764
PM
569 * into the low half of the word, and the high half is zeroed::
570 *
571 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
b355438d
PM
572 *
573 * Any even bits set in the input are ignored.
574 *
575 * Returns: the unshuffled bits.
576 */
577static inline uint32_t half_unshuffle32(uint32_t x)
578{
579 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
580 * where it is called an inverse half shuffle.
581 */
582 x &= 0x55555555;
583 x = ((x >> 1) | x) & 0x33333333;
584 x = ((x >> 2) | x) & 0x0F0F0F0F;
585 x = ((x >> 4) | x) & 0x00FF00FF;
586 x = ((x >> 8) | x) & 0x0000FFFF;
587 return x;
588}
589
590/**
591 * half_unshuffle64:
7d41d764
PM
592 * @x: 64-bit value (of which only the odd bits are of interest)
593 *
594 * Given an input value::
595 *
596 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
b355438d 597 *
b355438d 598 * return the value where all the odd bits are compressed down
7d41d764
PM
599 * into the low half of the word, and the high half is zeroed::
600 *
601 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
b355438d
PM
602 *
603 * Any even bits set in the input are ignored.
604 *
605 * Returns: the unshuffled bits.
606 */
607static inline uint64_t half_unshuffle64(uint64_t x)
608{
609 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
610 * where it is called an inverse half shuffle.
611 */
612 x &= 0x5555555555555555ULL;
613 x = ((x >> 1) | x) & 0x3333333333333333ULL;
614 x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
615 x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
616 x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
617 x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
618 return x;
619}
620
e0e53b2f 621#endif