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 first set bit, or size.
 144 */
 145unsigned long find_last_bit(const unsigned long *addr,
 146                            unsigned long size);
 147
 148/**
 149 * find_next_bit - find the next set bit in a memory region
 150 * @addr: The address to base the search on
 151 * @offset: The bitnumber to start searching at
 152 * @size: The bitmap size in bits
 153 */
 154unsigned long find_next_bit(const unsigned long *addr,
 155                            unsigned long size,
 156                            unsigned long offset);
 157
 158/**
 159 * find_next_zero_bit - find the next cleared bit in a memory region
 160 * @addr: The address to base the search on
 161 * @offset: The bitnumber to start searching at
 162 * @size: The bitmap size in bits
 163 */
 164
 165unsigned long find_next_zero_bit(const unsigned long *addr,
 166                                 unsigned long size,
 167                                 unsigned long offset);
 168
 169/**
 170 * find_first_bit - find the first set bit in a memory region
 171 * @addr: The address to start the search at
 172 * @size: The maximum size to search
 173 *
 174 * Returns the bit number of the first set bit.
 175 */
 176static inline unsigned long find_first_bit(const unsigned long *addr,
 177                                           unsigned long size)
 178{
 179    unsigned long result, tmp;
 180
 181    for (result = 0; result < size; result += BITS_PER_LONG) {
 182        tmp = *addr++;
 183        if (tmp) {
 184            result += ctzl(tmp);
 185            return result < size ? result : size;
 186        }
 187    }
 188    /* Not found */
 189    return size;
 190}
 191
 192/**
 193 * find_first_zero_bit - find the first cleared bit in a memory region
 194 * @addr: The address to start the search at
 195 * @size: The maximum size to search
 196 *
 197 * Returns the bit number of the first cleared bit.
 198 */
 199static inline unsigned long find_first_zero_bit(const unsigned long *addr,
 200                                                unsigned long size)
 201{
 202    return find_next_zero_bit(addr, size, 0);
 203}
 204
 205/**
 206 * rol8 - rotate an 8-bit value left
 207 * @word: value to rotate
 208 * @shift: bits to roll
 209 */
 210static inline uint8_t rol8(uint8_t word, unsigned int shift)
 211{
 212    return (word << shift) | (word >> ((8 - shift) & 7));
 213}
 214
 215/**
 216 * ror8 - rotate an 8-bit value right
 217 * @word: value to rotate
 218 * @shift: bits to roll
 219 */
 220static inline uint8_t ror8(uint8_t word, unsigned int shift)
 221{
 222    return (word >> shift) | (word << ((8 - shift) & 7));
 223}
 224
 225/**
 226 * rol16 - rotate a 16-bit value left
 227 * @word: value to rotate
 228 * @shift: bits to roll
 229 */
 230static inline uint16_t rol16(uint16_t word, unsigned int shift)
 231{
 232    return (word << shift) | (word >> ((16 - shift) & 15));
 233}
 234
 235/**
 236 * ror16 - rotate a 16-bit value right
 237 * @word: value to rotate
 238 * @shift: bits to roll
 239 */
 240static inline uint16_t ror16(uint16_t word, unsigned int shift)
 241{
 242    return (word >> shift) | (word << ((16 - shift) & 15));
 243}
 244
 245/**
 246 * rol32 - rotate a 32-bit value left
 247 * @word: value to rotate
 248 * @shift: bits to roll
 249 */
 250static inline uint32_t rol32(uint32_t word, unsigned int shift)
 251{
 252    return (word << shift) | (word >> ((32 - shift) & 31));
 253}
 254
 255/**
 256 * ror32 - rotate a 32-bit value right
 257 * @word: value to rotate
 258 * @shift: bits to roll
 259 */
 260static inline uint32_t ror32(uint32_t word, unsigned int shift)
 261{
 262    return (word >> shift) | (word << ((32 - shift) & 31));
 263}
 264
 265/**
 266 * rol64 - rotate a 64-bit value left
 267 * @word: value to rotate
 268 * @shift: bits to roll
 269 */
 270static inline uint64_t rol64(uint64_t word, unsigned int shift)
 271{
 272    return (word << shift) | (word >> ((64 - shift) & 63));
 273}
 274
 275/**
 276 * ror64 - rotate a 64-bit value right
 277 * @word: value to rotate
 278 * @shift: bits to roll
 279 */
 280static inline uint64_t ror64(uint64_t word, unsigned int shift)
 281{
 282    return (word >> shift) | (word << ((64 - shift) & 63));
 283}
 284
 285/**
 286 * extract32:
 287 * @value: the value to extract the bit field from
 288 * @start: the lowest bit in the bit field (numbered from 0)
 289 * @length: the length of the bit field
 290 *
 291 * Extract from the 32 bit input @value the bit field specified by the
 292 * @start and @length parameters, and return it. The bit field must
 293 * lie entirely within the 32 bit word. It is valid to request that
 294 * all 32 bits are returned (ie @length 32 and @start 0).
 295 *
 296 * Returns: the value of the bit field extracted from the input value.
 297 */
 298static inline uint32_t extract32(uint32_t value, int start, int length)
 299{
 300    assert(start >= 0 && length > 0 && length <= 32 - start);
 301    return (value >> start) & (~0U >> (32 - length));
 302}
 303
 304/**
 305 * extract8:
 306 * @value: the value to extract the bit field from
 307 * @start: the lowest bit in the bit field (numbered from 0)
 308 * @length: the length of the bit field
 309 *
 310 * Extract from the 8 bit input @value the bit field specified by the
 311 * @start and @length parameters, and return it. The bit field must
 312 * lie entirely within the 8 bit word. It is valid to request that
 313 * all 8 bits are returned (ie @length 8 and @start 0).
 314 *
 315 * Returns: the value of the bit field extracted from the input value.
 316 */
 317static inline uint8_t extract8(uint8_t value, int start, int length)
 318{
 319    assert(start >= 0 && length > 0 && length <= 8 - start);
 320    return extract32(value, start, length);
 321}
 322
 323/**
 324 * extract16:
 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 16 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 16 bit word. It is valid to request that
 332 * all 16 bits are returned (ie @length 16 and @start 0).
 333 *
 334 * Returns: the value of the bit field extracted from the input value.
 335 */
 336static inline uint16_t extract16(uint16_t value, int start, int length)
 337{
 338    assert(start >= 0 && length > 0 && length <= 16 - start);
 339    return extract32(value, start, length);
 340}
 341
 342/**
 343 * extract64:
 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 64 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 64 bit word. It is valid to request that
 351 * all 64 bits are returned (ie @length 64 and @start 0).
 352 *
 353 * Returns: the value of the bit field extracted from the input value.
 354 */
 355static inline uint64_t extract64(uint64_t value, int start, int length)
 356{
 357    assert(start >= 0 && length > 0 && length <= 64 - start);
 358    return (value >> start) & (~0ULL >> (64 - length));
 359}
 360
 361/**
 362 * sextract32:
 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 32 bit input @value the bit field specified by the
 368 * @start and @length parameters, and return it, sign extended to
 369 * an int32_t (ie with the most significant bit of the field propagated
 370 * to all the upper bits of the return value). The bit field must lie
 371 * entirely within the 32 bit word. It is valid to request that
 372 * all 32 bits are returned (ie @length 32 and @start 0).
 373 *
 374 * Returns: the sign extended value of the bit field extracted from the
 375 * input value.
 376 */
 377static inline int32_t sextract32(uint32_t value, int start, int length)
 378{
 379    assert(start >= 0 && length > 0 && length <= 32 - start);
 380    /* Note that this implementation relies on right shift of signed
 381     * integers being an arithmetic shift.
 382     */
 383    return ((int32_t)(value << (32 - length - start))) >> (32 - length);
 384}
 385
 386/**
 387 * sextract64:
 388 * @value: the value to extract the bit field from
 389 * @start: the lowest bit in the bit field (numbered from 0)
 390 * @length: the length of the bit field
 391 *
 392 * Extract from the 64 bit input @value the bit field specified by the
 393 * @start and @length parameters, and return it, sign extended to
 394 * an int64_t (ie with the most significant bit of the field propagated
 395 * to all the upper bits of the return value). The bit field must lie
 396 * entirely within the 64 bit word. It is valid to request that
 397 * all 64 bits are returned (ie @length 64 and @start 0).
 398 *
 399 * Returns: the sign extended value of the bit field extracted from the
 400 * input value.
 401 */
 402static inline int64_t sextract64(uint64_t value, int start, int length)
 403{
 404    assert(start >= 0 && length > 0 && length <= 64 - start);
 405    /* Note that this implementation relies on right shift of signed
 406     * integers being an arithmetic shift.
 407     */
 408    return ((int64_t)(value << (64 - length - start))) >> (64 - length);
 409}
 410
 411/**
 412 * deposit32:
 413 * @value: initial value to insert bit field into
 414 * @start: the lowest bit in the bit field (numbered from 0)
 415 * @length: the length of the bit field
 416 * @fieldval: the value to insert into the bit field
 417 *
 418 * Deposit @fieldval into the 32 bit @value at the bit field specified
 419 * by the @start and @length parameters, and return the modified
 420 * @value. Bits of @value outside the bit field are not modified.
 421 * Bits of @fieldval above the least significant @length bits are
 422 * ignored. The bit field must lie entirely within the 32 bit word.
 423 * It is valid to request that all 32 bits are modified (ie @length
 424 * 32 and @start 0).
 425 *
 426 * Returns: the modified @value.
 427 */
 428static inline uint32_t deposit32(uint32_t value, int start, int length,
 429                                 uint32_t fieldval)
 430{
 431    uint32_t mask;
 432    assert(start >= 0 && length > 0 && length <= 32 - start);
 433    mask = (~0U >> (32 - length)) << start;
 434    return (value & ~mask) | ((fieldval << start) & mask);
 435}
 436
 437/**
 438 * deposit64:
 439 * @value: initial value to insert bit field into
 440 * @start: the lowest bit in the bit field (numbered from 0)
 441 * @length: the length of the bit field
 442 * @fieldval: the value to insert into the bit field
 443 *
 444 * Deposit @fieldval into the 64 bit @value at the bit field specified
 445 * by the @start and @length parameters, and return the modified
 446 * @value. Bits of @value outside the bit field are not modified.
 447 * Bits of @fieldval above the least significant @length bits are
 448 * ignored. The bit field must lie entirely within the 64 bit word.
 449 * It is valid to request that all 64 bits are modified (ie @length
 450 * 64 and @start 0).
 451 *
 452 * Returns: the modified @value.
 453 */
 454static inline uint64_t deposit64(uint64_t value, int start, int length,
 455                                 uint64_t fieldval)
 456{
 457    uint64_t mask;
 458    assert(start >= 0 && length > 0 && length <= 64 - start);
 459    mask = (~0ULL >> (64 - length)) << start;
 460    return (value & ~mask) | ((fieldval << start) & mask);
 461}
 462
 463/**
 464 * half_shuffle32:
 465 * @x: 32-bit value (of which only the bottom 16 bits are of interest)
 466 *
 467 * Given an input value::
 468 *
 469 *   xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
 470 *
 471 * return the value where the bottom 16 bits are spread out into
 472 * the odd bits in the word, and the even bits are zeroed::
 473 *
 474 *   0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
 475 *
 476 * Any bits set in the top half of the input are ignored.
 477 *
 478 * Returns: the shuffled bits.
 479 */
 480static inline uint32_t half_shuffle32(uint32_t x)
 481{
 482    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 483     * It ignores any bits set in the top half of the input.
 484     */
 485    x = ((x & 0xFF00) << 8) | (x & 0x00FF);
 486    x = ((x << 4) | x) & 0x0F0F0F0F;
 487    x = ((x << 2) | x) & 0x33333333;
 488    x = ((x << 1) | x) & 0x55555555;
 489    return x;
 490}
 491
 492/**
 493 * half_shuffle64:
 494 * @x: 64-bit value (of which only the bottom 32 bits are of interest)
 495 *
 496 * Given an input value::
 497 *
 498 *   xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
 499 *
 500 * return the value where the bottom 32 bits are spread out into
 501 * the odd bits in the word, and the even bits are zeroed::
 502 *
 503 *   0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
 504 *
 505 * Any bits set in the top half of the input are ignored.
 506 *
 507 * Returns: the shuffled bits.
 508 */
 509static inline uint64_t half_shuffle64(uint64_t x)
 510{
 511    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 512     * It ignores any bits set in the top half of the input.
 513     */
 514    x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
 515    x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
 516    x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
 517    x = ((x << 2) | x) & 0x3333333333333333ULL;
 518    x = ((x << 1) | x) & 0x5555555555555555ULL;
 519    return x;
 520}
 521
 522/**
 523 * half_unshuffle32:
 524 * @x: 32-bit value (of which only the odd bits are of interest)
 525 *
 526 * Given an input value::
 527 *
 528 *   xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
 529 *
 530 * return the value where all the odd bits are compressed down
 531 * into the low half of the word, and the high half is zeroed::
 532 *
 533 *   0000 0000 0000 0000 ABCD EFGH IJKL MNOP
 534 *
 535 * Any even bits set in the input are ignored.
 536 *
 537 * Returns: the unshuffled bits.
 538 */
 539static inline uint32_t half_unshuffle32(uint32_t x)
 540{
 541    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 542     * where it is called an inverse half shuffle.
 543     */
 544    x &= 0x55555555;
 545    x = ((x >> 1) | x) & 0x33333333;
 546    x = ((x >> 2) | x) & 0x0F0F0F0F;
 547    x = ((x >> 4) | x) & 0x00FF00FF;
 548    x = ((x >> 8) | x) & 0x0000FFFF;
 549    return x;
 550}
 551
 552/**
 553 * half_unshuffle64:
 554 * @x: 64-bit value (of which only the odd bits are of interest)
 555 *
 556 * Given an input value::
 557 *
 558 *   xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
 559 *
 560 * return the value where all the odd bits are compressed down
 561 * into the low half of the word, and the high half is zeroed::
 562 *
 563 *   0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
 564 *
 565 * Any even bits set in the input are ignored.
 566 *
 567 * Returns: the unshuffled bits.
 568 */
 569static inline uint64_t half_unshuffle64(uint64_t x)
 570{
 571    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 572     * where it is called an inverse half shuffle.
 573     */
 574    x &= 0x5555555555555555ULL;
 575    x = ((x >> 1) | x) & 0x3333333333333333ULL;
 576    x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
 577    x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
 578    x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
 579    x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
 580    return x;
 581}
 582
 583#endif
 584