qemu/include/qemu/bitops.h
<<
>>
Prefs
   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
  15
  16#include "host-utils.h"
  17#include "atomic.h"
  18
  19#define BITS_PER_BYTE           CHAR_BIT
  20#define BITS_PER_LONG           (sizeof (unsigned long) * BITS_PER_BYTE)
  21
  22#define BIT(nr)                 (1UL << (nr))
  23#define BIT_ULL(nr)             (1ULL << (nr))
  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))
  27
  28#define MAKE_64BIT_MASK(shift, length) \
  29    (((~0ULL) >> (64 - (length))) << (shift))
  30
  31/**
  32 * set_bit - Set a bit in memory
  33 * @nr: the bit to set
  34 * @addr: the address to start counting from
  35 */
  36static inline void set_bit(long nr, unsigned long *addr)
  37{
  38    unsigned long mask = BIT_MASK(nr);
  39    unsigned long *p = addr + BIT_WORD(nr);
  40
  41    *p  |= mask;
  42}
  43
  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
  54    qatomic_or(p, mask);
  55}
  56
  57/**
  58 * clear_bit - Clears a bit in memory
  59 * @nr: Bit to clear
  60 * @addr: Address to start counting from
  61 */
  62static inline void clear_bit(long nr, unsigned long *addr)
  63{
  64    unsigned long mask = BIT_MASK(nr);
  65    unsigned long *p = addr + BIT_WORD(nr);
  66
  67    *p &= ~mask;
  68}
  69
  70/**
  71 * change_bit - Toggle a bit in memory
  72 * @nr: Bit to change
  73 * @addr: Address to start counting from
  74 */
  75static inline void change_bit(long nr, unsigned long *addr)
  76{
  77    unsigned long mask = BIT_MASK(nr);
  78    unsigned long *p = addr + BIT_WORD(nr);
  79
  80    *p ^= mask;
  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 */
  88static inline int test_and_set_bit(long nr, unsigned long *addr)
  89{
  90    unsigned long mask = BIT_MASK(nr);
  91    unsigned long *p = addr + BIT_WORD(nr);
  92    unsigned long old = *p;
  93
  94    *p = old | mask;
  95    return (old & mask) != 0;
  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 */
 103static inline int test_and_clear_bit(long nr, unsigned long *addr)
 104{
 105    unsigned long mask = BIT_MASK(nr);
 106    unsigned long *p = addr + BIT_WORD(nr);
 107    unsigned long old = *p;
 108
 109    *p = old & ~mask;
 110    return (old & mask) != 0;
 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 */
 118static inline int test_and_change_bit(long nr, unsigned long *addr)
 119{
 120    unsigned long mask = BIT_MASK(nr);
 121    unsigned long *p = addr + BIT_WORD(nr);
 122    unsigned long old = *p;
 123
 124    *p = old ^ mask;
 125    return (old & mask) != 0;
 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 */
 133static inline int test_bit(long nr, const unsigned long *addr)
 134{
 135    return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
 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 *
 143 * Returns the bit number of the last set bit,
 144 * or @size if there is no set bit in the bitmap.
 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
 154 *
 155 * Returns the bit number of the next set bit,
 156 * or @size if there are no further set bits in the bitmap.
 157 */
 158unsigned long find_next_bit(const unsigned long *addr,
 159                            unsigned long size,
 160                            unsigned long offset);
 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
 167 *
 168 * Returns the bit number of the next cleared bit,
 169 * or @size if there are no further clear bits in the bitmap.
 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 *
 181 * Returns the bit number of the first set bit,
 182 * or @size if there is no set bit in the bitmap.
 183 */
 184static inline unsigned long find_first_bit(const unsigned long *addr,
 185                                           unsigned long size)
 186{
 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;
 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 *
 205 * Returns the bit number of the first cleared bit,
 206 * or @size if there is no clear bit in the bitmap.
 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
 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{
 221    return (word << (shift & 7)) | (word >> (-shift & 7));
 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{
 231    return (word >> (shift & 7)) | (word << (-shift & 7));
 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{
 241    return (word << (shift & 15)) | (word >> (-shift & 15));
 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{
 251    return (word >> (shift & 15)) | (word << (-shift & 15));
 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{
 261    return (word << (shift & 31)) | (word >> (-shift & 31));
 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{
 271    return (word >> (shift & 31)) | (word << (-shift & 31));
 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{
 281    return (word << (shift & 63)) | (word >> (-shift & 63));
 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{
 291    return (word >> (shift & 63)) | (word << (-shift & 63));
 292}
 293
 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
 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
 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
 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
 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 */
 440static inline int64_t sextract64(uint64_t value, int start, int length)
 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
 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.
 461 * It is valid to request that all 32 bits are modified (ie @length
 462 * 32 and @start 0).
 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/**
 476 * deposit64:
 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
 486 * ignored. The bit field must lie entirely within the 64 bit word.
 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
 501/**
 502 * half_shuffle32:
 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
 508 *
 509 * return the value where the bottom 16 bits are spread out into
 510 * the odd bits in the word, and the even bits are zeroed::
 511 *
 512 *   0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
 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:
 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
 537 *
 538 * return the value where the bottom 32 bits are spread out into
 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
 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:
 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
 567 *
 568 * return the value where all the odd bits are compressed down
 569 * into the low half of the word, and the high half is zeroed::
 570 *
 571 *   0000 0000 0000 0000 ABCD EFGH IJKL MNOP
 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:
 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
 597 *
 598 * return the value where all the odd bits are compressed down
 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
 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
 621#endif
 622