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 */ 36 static 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 */ 49 static 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 atomic_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 */ 62 static 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 */ 75 static 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 */ 88 static 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 */ 103 static 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 */ 118 static 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 */ 133 static 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 */ 145 unsigned 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 */ 154 unsigned 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 165 unsigned 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 */ 176 static 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 */ 199 static 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 */ 210 static 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 */ 220 static 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 */ 230 static 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 */ 240 static 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 */ 250 static 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 */ 260 static 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 */ 270 static 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 */ 280 static 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 */ 298 static 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 * extract64: 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 64 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 64 bit word. It is valid to request that 313 * all 64 bits are returned (ie @length 64 and @start 0). 314 * 315 * Returns: the value of the bit field extracted from the input value. 316 */ 317 static inline uint64_t extract64(uint64_t value, int start, int length) 318 { 319 assert(start >= 0 && length > 0 && length <= 64 - start); 320 return (value >> start) & (~0ULL >> (64 - length)); 321 } 322 323 /** 324 * sextract32: 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, sign extended to 331 * an int32_t (ie with the most significant bit of the field propagated 332 * to all the upper bits of the return value). The bit field must lie 333 * entirely within the 32 bit word. It is valid to request that 334 * all 32 bits are returned (ie @length 32 and @start 0). 335 * 336 * Returns: the sign extended value of the bit field extracted from the 337 * input value. 338 */ 339 static inline int32_t sextract32(uint32_t value, int start, int length) 340 { 341 assert(start >= 0 && length > 0 && length <= 32 - start); 342 /* Note that this implementation relies on right shift of signed 343 * integers being an arithmetic shift. 344 */ 345 return ((int32_t)(value << (32 - length - start))) >> (32 - length); 346 } 347 348 /** 349 * sextract64: 350 * @value: the value to extract the bit field from 351 * @start: the lowest bit in the bit field (numbered from 0) 352 * @length: the length of the bit field 353 * 354 * Extract from the 64 bit input @value the bit field specified by the 355 * @start and @length parameters, and return it, sign extended to 356 * an int64_t (ie with the most significant bit of the field propagated 357 * to all the upper bits of the return value). The bit field must lie 358 * entirely within the 64 bit word. It is valid to request that 359 * all 64 bits are returned (ie @length 64 and @start 0). 360 * 361 * Returns: the sign extended value of the bit field extracted from the 362 * input value. 363 */ 364 static inline int64_t sextract64(uint64_t value, int start, int length) 365 { 366 assert(start >= 0 && length > 0 && length <= 64 - start); 367 /* Note that this implementation relies on right shift of signed 368 * integers being an arithmetic shift. 369 */ 370 return ((int64_t)(value << (64 - length - start))) >> (64 - length); 371 } 372 373 /** 374 * deposit32: 375 * @value: initial value to insert bit field into 376 * @start: the lowest bit in the bit field (numbered from 0) 377 * @length: the length of the bit field 378 * @fieldval: the value to insert into the bit field 379 * 380 * Deposit @fieldval into the 32 bit @value at the bit field specified 381 * by the @start and @length parameters, and return the modified 382 * @value. Bits of @value outside the bit field are not modified. 383 * Bits of @fieldval above the least significant @length bits are 384 * ignored. The bit field must lie entirely within the 32 bit word. 385 * It is valid to request that all 32 bits are modified (ie @length 386 * 32 and @start 0). 387 * 388 * Returns: the modified @value. 389 */ 390 static inline uint32_t deposit32(uint32_t value, int start, int length, 391 uint32_t fieldval) 392 { 393 uint32_t mask; 394 assert(start >= 0 && length > 0 && length <= 32 - start); 395 mask = (~0U >> (32 - length)) << start; 396 return (value & ~mask) | ((fieldval << start) & mask); 397 } 398 399 /** 400 * deposit64: 401 * @value: initial value to insert bit field into 402 * @start: the lowest bit in the bit field (numbered from 0) 403 * @length: the length of the bit field 404 * @fieldval: the value to insert into the bit field 405 * 406 * Deposit @fieldval into the 64 bit @value at the bit field specified 407 * by the @start and @length parameters, and return the modified 408 * @value. Bits of @value outside the bit field are not modified. 409 * Bits of @fieldval above the least significant @length bits are 410 * ignored. The bit field must lie entirely within the 64 bit word. 411 * It is valid to request that all 64 bits are modified (ie @length 412 * 64 and @start 0). 413 * 414 * Returns: the modified @value. 415 */ 416 static inline uint64_t deposit64(uint64_t value, int start, int length, 417 uint64_t fieldval) 418 { 419 uint64_t mask; 420 assert(start >= 0 && length > 0 && length <= 64 - start); 421 mask = (~0ULL >> (64 - length)) << start; 422 return (value & ~mask) | ((fieldval << start) & mask); 423 } 424 425 /** 426 * half_shuffle32: 427 * @value: 32-bit value (of which only the bottom 16 bits are of interest) 428 * 429 * Given an input value: 430 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP 431 * return the value where the bottom 16 bits are spread out into 432 * the odd bits in the word, and the even bits are zeroed: 433 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P 434 * 435 * Any bits set in the top half of the input are ignored. 436 * 437 * Returns: the shuffled bits. 438 */ 439 static inline uint32_t half_shuffle32(uint32_t x) 440 { 441 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits". 442 * It ignores any bits set in the top half of the input. 443 */ 444 x = ((x & 0xFF00) << 8) | (x & 0x00FF); 445 x = ((x << 4) | x) & 0x0F0F0F0F; 446 x = ((x << 2) | x) & 0x33333333; 447 x = ((x << 1) | x) & 0x55555555; 448 return x; 449 } 450 451 /** 452 * half_shuffle64: 453 * @value: 64-bit value (of which only the bottom 32 bits are of interest) 454 * 455 * Given an input value: 456 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef 457 * return the value where the bottom 32 bits are spread out into 458 * the odd bits in the word, and the even bits are zeroed: 459 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f 460 * 461 * Any bits set in the top half of the input are ignored. 462 * 463 * Returns: the shuffled bits. 464 */ 465 static inline uint64_t half_shuffle64(uint64_t x) 466 { 467 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits". 468 * It ignores any bits set in the top half of the input. 469 */ 470 x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF); 471 x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL; 472 x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL; 473 x = ((x << 2) | x) & 0x3333333333333333ULL; 474 x = ((x << 1) | x) & 0x5555555555555555ULL; 475 return x; 476 } 477 478 /** 479 * half_unshuffle32: 480 * @value: 32-bit value (of which only the odd bits are of interest) 481 * 482 * Given an input value: 483 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP 484 * return the value where all the odd bits are compressed down 485 * into the low half of the word, and the high half is zeroed: 486 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP 487 * 488 * Any even bits set in the input are ignored. 489 * 490 * Returns: the unshuffled bits. 491 */ 492 static inline uint32_t half_unshuffle32(uint32_t x) 493 { 494 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits". 495 * where it is called an inverse half shuffle. 496 */ 497 x &= 0x55555555; 498 x = ((x >> 1) | x) & 0x33333333; 499 x = ((x >> 2) | x) & 0x0F0F0F0F; 500 x = ((x >> 4) | x) & 0x00FF00FF; 501 x = ((x >> 8) | x) & 0x0000FFFF; 502 return x; 503 } 504 505 /** 506 * half_unshuffle64: 507 * @value: 64-bit value (of which only the odd bits are of interest) 508 * 509 * Given an input value: 510 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf 511 * return the value where all the odd bits are compressed down 512 * into the low half of the word, and the high half is zeroed: 513 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef 514 * 515 * Any even bits set in the input are ignored. 516 * 517 * Returns: the unshuffled bits. 518 */ 519 static inline uint64_t half_unshuffle64(uint64_t x) 520 { 521 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits". 522 * where it is called an inverse half shuffle. 523 */ 524 x &= 0x5555555555555555ULL; 525 x = ((x >> 1) | x) & 0x3333333333333333ULL; 526 x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL; 527 x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL; 528 x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL; 529 x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL; 530 return x; 531 } 532 533 #endif 534