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
 204/**
 205 * rol8 - rotate an 8-bit value left
 206 * @word: value to rotate
 207 * @shift: bits to roll
 208 */
 209static inline uint8_t rol8(uint8_t word, unsigned int shift)
 210{
 211    return (word << shift) | (word >> ((8 - shift) & 7));
 212}
 213
 214/**
 215 * ror8 - rotate an 8-bit value right
 216 * @word: value to rotate
 217 * @shift: bits to roll
 218 */
 219static inline uint8_t ror8(uint8_t word, unsigned int shift)
 220{
 221    return (word >> shift) | (word << ((8 - shift) & 7));
 222}
 223
 224/**
 225 * rol16 - rotate a 16-bit value left
 226 * @word: value to rotate
 227 * @shift: bits to roll
 228 */
 229static inline uint16_t rol16(uint16_t word, unsigned int shift)
 230{
 231    return (word << shift) | (word >> ((16 - shift) & 15));
 232}
 233
 234/**
 235 * ror16 - rotate a 16-bit value right
 236 * @word: value to rotate
 237 * @shift: bits to roll
 238 */
 239static inline uint16_t ror16(uint16_t word, unsigned int shift)
 240{
 241    return (word >> shift) | (word << ((16 - shift) & 15));
 242}
 243
 244/**
 245 * rol32 - rotate a 32-bit value left
 246 * @word: value to rotate
 247 * @shift: bits to roll
 248 */
 249static inline uint32_t rol32(uint32_t word, unsigned int shift)
 250{
 251    return (word << shift) | (word >> ((32 - shift) & 31));
 252}
 253
 254/**
 255 * ror32 - rotate a 32-bit value right
 256 * @word: value to rotate
 257 * @shift: bits to roll
 258 */
 259static inline uint32_t ror32(uint32_t word, unsigned int shift)
 260{
 261    return (word >> shift) | (word << ((32 - shift) & 31));
 262}
 263
 264/**
 265 * rol64 - rotate a 64-bit value left
 266 * @word: value to rotate
 267 * @shift: bits to roll
 268 */
 269static inline uint64_t rol64(uint64_t word, unsigned int shift)
 270{
 271    return (word << shift) | (word >> ((64 - shift) & 63));
 272}
 273
 274/**
 275 * ror64 - rotate a 64-bit value right
 276 * @word: value to rotate
 277 * @shift: bits to roll
 278 */
 279static inline uint64_t ror64(uint64_t word, unsigned int shift)
 280{
 281    return (word >> shift) | (word << ((64 - shift) & 63));
 282}
 283
 284/**
 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
 289 *
 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).
 294 *
 295 * Returns: the value of the bit field extracted from the input value.
 296 */
 297static inline uint32_t extract32(uint32_t value, int start, int length)
 298{
 299    assert(start >= 0 && length > 0 && length <= 32 - start);
 300    return (value >> start) & (~0U >> (32 - length));
 301}
 302
 303/**
 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
 308 *
 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).
 313 *
 314 * Returns: the value of the bit field extracted from the input value.
 315 */
 316static inline uint64_t extract64(uint64_t value, int start, int length)
 317{
 318    assert(start >= 0 && length > 0 && length <= 64 - start);
 319    return (value >> start) & (~0ULL >> (64 - length));
 320}
 321
 322/**
 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
 327 *
 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).
 334 *
 335 * Returns: the sign extended value of the bit field extracted from the
 336 * input value.
 337 */
 338static inline int32_t sextract32(uint32_t value, int start, int length)
 339{
 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.
 343     */
 344    return ((int32_t)(value << (32 - length - start))) >> (32 - length);
 345}
 346
 347/**
 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
 352 *
 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).
 359 *
 360 * Returns: the sign extended value of the bit field extracted from the
 361 * input value.
 362 */
 363static inline int64_t sextract64(uint64_t value, int start, int length)
 364{
 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.
 368     */
 369    return ((int64_t)(value << (64 - length - start))) >> (64 - length);
 370}
 371
 372/**
 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
 378 *
 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).
 386 *
 387 * Returns: the modified @value.
 388 */
 389static inline uint32_t deposit32(uint32_t value, int start, int length,
 390                                 uint32_t fieldval)
 391{
 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);
 396}
 397
 398/**
 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
 404 *
 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).
 412 *
 413 * Returns: the modified @value.
 414 */
 415static inline uint64_t deposit64(uint64_t value, int start, int length,
 416                                 uint64_t fieldval)
 417{
 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);
 422}
 423
 424/**
 425 * half_shuffle32:
 426 * @value: 32-bit value (of which only the bottom 16 bits are of interest)
 427 *
 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
 433 *
 434 * Any bits set in the top half of the input are ignored.
 435 *
 436 * Returns: the shuffled bits.
 437 */
 438static inline uint32_t half_shuffle32(uint32_t x)
 439{
 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.
 442     */
 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;
 448}
 449
 450/**
 451 * half_shuffle64:
 452 * @value: 64-bit value (of which only the bottom 32 bits are of interest)
 453 *
 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
 459 *
 460 * Any bits set in the top half of the input are ignored.
 461 *
 462 * Returns: the shuffled bits.
 463 */
 464static inline uint64_t half_shuffle64(uint64_t x)
 465{
 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.
 468     */
 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;
 475}
 476
 477/**
 478 * half_unshuffle32:
 479 * @value: 32-bit value (of which only the odd bits are of interest)
 480 *
 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
 486 *
 487 * Any even bits set in the input are ignored.
 488 *
 489 * Returns: the unshuffled bits.
 490 */
 491static inline uint32_t half_unshuffle32(uint32_t x)
 492{
 493    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 494     * where it is called an inverse half shuffle.
 495     */
 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;
 502}
 503
 504/**
 505 * half_unshuffle64:
 506 * @value: 64-bit value (of which only the odd bits are of interest)
 507 *
 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
 513 *
 514 * Any even bits set in the input are ignored.
 515 *
 516 * Returns: the unshuffled bits.
 517 */
 518static inline uint64_t half_unshuffle64(uint64_t x)
 519{
 520    /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
 521     * where it is called an inverse half shuffle.
 522     */
 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;
 530}
 531
 532#endif
 533