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_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))
  26
  27#define MAKE_64BIT_MASK(shift, length) \
  28    (((~0ULL) >> (64 - (length))) << (shift))
  29
  30/**
  31 * set_bit - Set a bit in memory
  32 * @nr: the bit to set
  33 * @addr: the address to start counting from
  34 */
  35static inline void set_bit(long nr, unsigned long *addr)
  36{
  37    unsigned long mask = BIT_MASK(nr);
  38    unsigned long *p = addr + BIT_WORD(nr);
  39
  40    *p  |= mask;
  41}
  42
  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
  47 */
  48static inline void set_bit_atomic(long nr, unsigned long *addr)
  49{
  50    unsigned long mask = BIT_MASK(nr);
  51    unsigned long *p = addr + BIT_WORD(nr);
  52
  53    atomic_or(p, mask);
  54}
  55
  56/**
  57 * clear_bit - Clears a bit in memory
  58 * @nr: Bit to clear
  59 * @addr: Address to start counting from
  60 */
  61static inline void clear_bit(long nr, unsigned long *addr)
  62{
  63    unsigned long mask = BIT_MASK(nr);
  64    unsigned long *p = addr + BIT_WORD(nr);
  65
  66    *p &= ~mask;
  67}
  68
  69/**
  70 * change_bit - Toggle a bit in memory
  71 * @nr: Bit to change
  72 * @addr: Address to start counting from
  73 */
  74static inline void change_bit(long nr, unsigned long *addr)
  75{
  76    unsigned long mask = BIT_MASK(nr);
  77    unsigned long *p = addr + BIT_WORD(nr);
  78
  79    *p ^= mask;
  80}
  81
  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
  86 */
  87static inline int test_and_set_bit(long nr, unsigned long *addr)
  88{
  89    unsigned long mask = BIT_MASK(nr);
  90    unsigned long *p = addr + BIT_WORD(nr);
  91    unsigned long old = *p;
  92
  93    *p = old | mask;
  94    return (old & mask) != 0;
  95}
  96
  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
 101 */
 102static inline int test_and_clear_bit(long nr, unsigned long *addr)
 103{
 104    unsigned long mask = BIT_MASK(nr);
 105    unsigned long *p = addr + BIT_WORD(nr);
 106    unsigned long old = *p;
 107
 108    *p = old & ~mask;
 109    return (old & mask) != 0;
 110}
 111
 112/**
 113 * test_and_change_bit - Change a bit and return its old value
 114 * @nr: Bit to change
 115 * @addr: Address to count from
 116 */
 117static inline int test_and_change_bit(long nr, unsigned long *addr)
 118{
 119    unsigned long mask = BIT_MASK(nr);
 120    unsigned long *p = addr + BIT_WORD(nr);
 121    unsigned long old = *p;
 122
 123    *p = old ^ mask;
 124    return (old & mask) != 0;
 125}
 126
 127/**
 128 * test_bit - Determine whether a bit is set
 129 * @nr: bit number to test
 130 * @addr: Address to start counting from
 131 */
 132static inline int test_bit(long nr, const unsigned long *addr)
 133{
 134    return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
 135}
 136
 137/**
 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
 141 *
 142 * Returns the bit number of the first set bit, or size.
 143 */
 144unsigned long find_last_bit(const unsigned long *addr,
 145                            unsigned long size);
 146
 147/**
 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
 152 */
 153unsigned long find_next_bit(const unsigned long *addr,
 154                            unsigned long size,
 155                            unsigned long offset);
 156
 157/**
 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
 162 */
 163
 164unsigned long find_next_zero_bit(const unsigned long *addr,
 165                                 unsigned long size,
 166                                 unsigned long offset);
 167
 168/**
 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
 172 *
 173 * Returns the bit number of the first set bit.
 174 */
 175static inline unsigned long find_first_bit(const unsigned long *addr,
 176                                           unsigned long size)
 177{
 178    unsigned long result, tmp;
 179
 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;
 185        }
 186    }
 187    /* Not found */
 188    return size;
 189}
 190
 191/**
 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
 195 *
 196 * Returns the bit number of the first cleared bit.
 197 */
 198static inline unsigned long find_first_zero_bit(const unsigned long *addr,
 199                                                unsigned long size)
 200{
 201    return find_next_zero_bit(addr, size, 0);
 202}
 203
 204static inline unsigned long hweight_long(unsigned long w)
 205{
 206    unsigned long count;
 207
 208    for (count = 0; w; w >>= 1) {
 209        count += w & 1;
 210    }
 211    return count;
 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) | (word >> ((8 - 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) | (word << ((8 - 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) | (word >> ((16 - 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) | (word << ((16 - 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) | (word >> ((32 - 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) | (word << ((32 - 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) | (word >> ((64 - 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) | (word << ((64 - shift) & 63));
 292}
 293
 294/**
 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
 299 *
 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).
 304 *
 305 * Returns: the value of the bit field extracted from the input value.
 306 */
 307static inline uint32_t extract32(uint32_t value, int start, int length)
 308{
 309    assert(start >= 0 && length > 0 && length <= 32 - start);
 310    return (value >> start) & (~0U >> (32 - length));
 311}
 312
 313/**
 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
 318 *
 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).
 323 *
 324 * Returns: the value of the bit field extracted from the input value.
 325 */
 326static inline uint64_t extract64(uint64_t value, int start, int length)
 327{
 328    assert(start >= 0 && length > 0 && length <= 64 - start);
 329    return (value >> start) & (~0ULL >> (64 - length));
 330}
 331
 332/**
 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
 337 *
 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).
 344 *
 345 * Returns: the sign extended value of the bit field extracted from the
 346 * input value.
 347 */
 348static inline int32_t sextract32(uint32_t value, int start, int length)
 349{
 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.
 353     */
 354    return ((int32_t)(value << (32 - length - start))) >> (32 - length);
 355}
 356
 357/**
 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
 362 *
 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).
 369 *
 370 * Returns: the sign extended value of the bit field extracted from the
 371 * input value.
 372 */
 373static inline int64_t sextract64(uint64_t value, int start, int length)
 374{
 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.
 378     */
 379    return ((int64_t)(value << (64 - length - start))) >> (64 - length);
 380}
 381
 382/**
 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
 388 *
 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).
 396 *
 397 * Returns: the modified @value.
 398 */
 399static inline uint32_t deposit32(uint32_t value, int start, int length,
 400                                 uint32_t fieldval)
 401{
 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);
 406}
 407
 408/**
 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
 414 *
 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).
 422 *
 423 * Returns: the modified @value.
 424 */
 425static inline uint64_t deposit64(uint64_t value, int start, int length,
 426                                 uint64_t fieldval)
 427{
 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);
 432}
 433
 434/**
 435 * half_shuffle32:
 436 * @value: 32-bit value (of which only the bottom 16 bits are of interest)
 437 *
 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
 443 *
 444 * Any bits set in the top half of the input are ignored.
 445 *
 446 * Returns: the shuffled bits.
 447 */
 448static inline uint32_t half_shuffle32(uint32_t x)
 449{
 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.
 452     */
 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;
 458}
 459
 460/**
 461 * half_shuffle64:
 462 * @value: 64-bit value (of which only the bottom 32 bits are of interest)
 463 *
 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
 469 *
 470 * Any bits set in the top half of the input are ignored.
 471 *
 472 * Returns: the shuffled bits.
 473 */
 474static inline uint64_t half_shuffle64(uint64_t x)
 475{
 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.
 478     */
 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;
 485}
 486
 487/**
 488 * half_unshuffle32:
 489 * @value: 32-bit value (of which only the odd bits are of interest)
 490 *
 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
 496 *
 497 * Any even bits set in the input are ignored.
 498 *
 499 * Returns: the unshuffled bits.
 500 */
 501static inline uint32_t half_unshuffle32(uint32_t x)
 502{
 503    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 504     * where it is called an inverse half shuffle.
 505     */
 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;
 512}
 513
 514/**
 515 * half_unshuffle64:
 516 * @value: 64-bit value (of which only the odd bits are of interest)
 517 *
 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
 523 *
 524 * Any even bits set in the input are ignored.
 525 *
 526 * Returns: the unshuffled bits.
 527 */
 528static inline uint64_t half_unshuffle64(uint64_t x)
 529{
 530    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 531     * where it is called an inverse half shuffle.
 532     */
 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;
 540}
 541
 542#endif
 543