4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
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.
16 #include "host-utils.h"
19 #define BITS_PER_BYTE CHAR_BIT
20 #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
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))
28 #define MAKE_64BIT_MASK(shift, length) \
29 (((~0ULL) >> (64 - (length))) << (shift))
32 * set_bit - Set a bit in memory
34 * @addr: the address to start counting from
36 static inline void set_bit(long nr
, unsigned long *addr
)
38 unsigned long mask
= BIT_MASK(nr
);
39 unsigned long *p
= addr
+ BIT_WORD(nr
);
45 * set_bit_atomic - Set a bit in memory atomically
47 * @addr: the address to start counting from
49 static inline void set_bit_atomic(long nr
, unsigned long *addr
)
51 unsigned long mask
= BIT_MASK(nr
);
52 unsigned long *p
= addr
+ BIT_WORD(nr
);
58 * clear_bit - Clears a bit in memory
60 * @addr: Address to start counting from
62 static inline void clear_bit(long nr
, unsigned long *addr
)
64 unsigned long mask
= BIT_MASK(nr
);
65 unsigned long *p
= addr
+ BIT_WORD(nr
);
71 * change_bit - Toggle a bit in memory
73 * @addr: Address to start counting from
75 static inline void change_bit(long nr
, unsigned long *addr
)
77 unsigned long mask
= BIT_MASK(nr
);
78 unsigned long *p
= addr
+ BIT_WORD(nr
);
84 * test_and_set_bit - Set a bit and return its old value
86 * @addr: Address to count from
88 static inline int test_and_set_bit(long nr
, unsigned long *addr
)
90 unsigned long mask
= BIT_MASK(nr
);
91 unsigned long *p
= addr
+ BIT_WORD(nr
);
92 unsigned long old
= *p
;
95 return (old
& mask
) != 0;
99 * test_and_clear_bit - Clear a bit and return its old value
101 * @addr: Address to count from
103 static inline int test_and_clear_bit(long nr
, unsigned long *addr
)
105 unsigned long mask
= BIT_MASK(nr
);
106 unsigned long *p
= addr
+ BIT_WORD(nr
);
107 unsigned long old
= *p
;
110 return (old
& mask
) != 0;
114 * test_and_change_bit - Change a bit and return its old value
116 * @addr: Address to count from
118 static inline int test_and_change_bit(long nr
, unsigned long *addr
)
120 unsigned long mask
= BIT_MASK(nr
);
121 unsigned long *p
= addr
+ BIT_WORD(nr
);
122 unsigned long old
= *p
;
125 return (old
& mask
) != 0;
129 * test_bit - Determine whether a bit is set
130 * @nr: bit number to test
131 * @addr: Address to start counting from
133 static inline int test_bit(long nr
, const unsigned long *addr
)
135 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
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
143 * Returns the bit number of the last set bit,
144 * or @size if there is no set bit in the bitmap.
146 unsigned long find_last_bit(const unsigned long *addr
,
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
155 * Returns the bit number of the next set bit,
156 * or @size if there are no further set bits in the bitmap.
158 unsigned long find_next_bit(const unsigned long *addr
,
160 unsigned long offset
);
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
168 * Returns the bit number of the next cleared bit,
169 * or @size if there are no further clear bits in the bitmap.
172 unsigned long find_next_zero_bit(const unsigned long *addr
,
174 unsigned long offset
);
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
181 * Returns the bit number of the first set bit,
182 * or @size if there is no set bit in the bitmap.
184 static inline unsigned long find_first_bit(const unsigned long *addr
,
187 unsigned long result
, tmp
;
189 for (result
= 0; result
< size
; result
+= BITS_PER_LONG
) {
193 return result
< size
? result
: size
;
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
205 * Returns the bit number of the first cleared bit,
206 * or @size if there is no clear bit in the bitmap.
208 static inline unsigned long find_first_zero_bit(const unsigned long *addr
,
211 return find_next_zero_bit(addr
, size
, 0);
215 * rol8 - rotate an 8-bit value left
216 * @word: value to rotate
217 * @shift: bits to roll
219 static inline uint8_t rol8(uint8_t word
, unsigned int shift
)
221 return (word
<< shift
) | (word
>> ((8 - shift
) & 7));
225 * ror8 - rotate an 8-bit value right
226 * @word: value to rotate
227 * @shift: bits to roll
229 static inline uint8_t ror8(uint8_t word
, unsigned int shift
)
231 return (word
>> shift
) | (word
<< ((8 - shift
) & 7));
235 * rol16 - rotate a 16-bit value left
236 * @word: value to rotate
237 * @shift: bits to roll
239 static inline uint16_t rol16(uint16_t word
, unsigned int shift
)
241 return (word
<< shift
) | (word
>> ((16 - shift
) & 15));
245 * ror16 - rotate a 16-bit value right
246 * @word: value to rotate
247 * @shift: bits to roll
249 static inline uint16_t ror16(uint16_t word
, unsigned int shift
)
251 return (word
>> shift
) | (word
<< ((16 - shift
) & 15));
255 * rol32 - rotate a 32-bit value left
256 * @word: value to rotate
257 * @shift: bits to roll
259 static inline uint32_t rol32(uint32_t word
, unsigned int shift
)
261 return (word
<< shift
) | (word
>> ((32 - shift
) & 31));
265 * ror32 - rotate a 32-bit value right
266 * @word: value to rotate
267 * @shift: bits to roll
269 static inline uint32_t ror32(uint32_t word
, unsigned int shift
)
271 return (word
>> shift
) | (word
<< ((32 - shift
) & 31));
275 * rol64 - rotate a 64-bit value left
276 * @word: value to rotate
277 * @shift: bits to roll
279 static inline uint64_t rol64(uint64_t word
, unsigned int shift
)
281 return (word
<< shift
) | (word
>> ((64 - shift
) & 63));
285 * ror64 - rotate a 64-bit value right
286 * @word: value to rotate
287 * @shift: bits to roll
289 static inline uint64_t ror64(uint64_t word
, unsigned int shift
)
291 return (word
>> shift
) | (word
<< ((64 - shift
) & 63));
295 * hswap32 - swap 16-bit halfwords within a 32-bit value
298 static inline uint32_t hswap32(uint32_t h
)
304 * hswap64 - swap 16-bit halfwords within a 64-bit value
307 static inline uint64_t hswap64(uint64_t h
)
309 uint64_t m
= 0x0000ffff0000ffffull
;
311 return ((h
& m
) << 16) | ((h
>> 16) & m
);
315 * wswap64 - swap 32-bit words within a 64-bit value
318 static inline uint64_t wswap64(uint64_t h
)
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
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).
334 * Returns: the value of the bit field extracted from the input value.
336 static inline uint32_t extract32(uint32_t value
, int start
, int length
)
338 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
339 return (value
>> start
) & (~0U >> (32 - length
));
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
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).
353 * Returns: the value of the bit field extracted from the input value.
355 static inline uint8_t extract8(uint8_t value
, int start
, int length
)
357 assert(start
>= 0 && length
> 0 && length
<= 8 - start
);
358 return extract32(value
, start
, length
);
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
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).
372 * Returns: the value of the bit field extracted from the input value.
374 static inline uint16_t extract16(uint16_t value
, int start
, int length
)
376 assert(start
>= 0 && length
> 0 && length
<= 16 - start
);
377 return extract32(value
, start
, length
);
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
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).
391 * Returns: the value of the bit field extracted from the input value.
393 static inline uint64_t extract64(uint64_t value
, int start
, int length
)
395 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
396 return (value
>> start
) & (~0ULL >> (64 - length
));
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
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).
412 * Returns: the sign extended value of the bit field extracted from the
415 static inline int32_t sextract32(uint32_t value
, int start
, int length
)
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.
421 return ((int32_t)(value
<< (32 - length
- start
))) >> (32 - length
);
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
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).
437 * Returns: the sign extended value of the bit field extracted from the
440 static inline int64_t sextract64(uint64_t value
, int start
, int length
)
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.
446 return ((int64_t)(value
<< (64 - length
- start
))) >> (64 - length
);
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
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
464 * Returns: the modified @value.
466 static inline uint32_t deposit32(uint32_t value
, int start
, int length
,
470 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
471 mask
= (~0U >> (32 - length
)) << start
;
472 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
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
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
490 * Returns: the modified @value.
492 static inline uint64_t deposit64(uint64_t value
, int start
, int length
,
496 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
497 mask
= (~0ULL >> (64 - length
)) << start
;
498 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
503 * @x: 32-bit value (of which only the bottom 16 bits are of interest)
505 * Given an input value::
507 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
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::
512 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
514 * Any bits set in the top half of the input are ignored.
516 * Returns: the shuffled bits.
518 static inline uint32_t half_shuffle32(uint32_t x
)
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.
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;
532 * @x: 64-bit value (of which only the bottom 32 bits are of interest)
534 * Given an input value::
536 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
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::
541 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
543 * Any bits set in the top half of the input are ignored.
545 * Returns: the shuffled bits.
547 static inline uint64_t half_shuffle64(uint64_t x
)
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.
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
;
562 * @x: 32-bit value (of which only the odd bits are of interest)
564 * Given an input value::
566 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
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::
571 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
573 * Any even bits set in the input are ignored.
575 * Returns: the unshuffled bits.
577 static inline uint32_t half_unshuffle32(uint32_t x
)
579 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
580 * where it is called an inverse half shuffle.
583 x
= ((x
>> 1) | x
) & 0x33333333;
584 x
= ((x
>> 2) | x
) & 0x0F0F0F0F;
585 x
= ((x
>> 4) | x
) & 0x00FF00FF;
586 x
= ((x
>> 8) | x
) & 0x0000FFFF;
592 * @x: 64-bit value (of which only the odd bits are of interest)
594 * Given an input value::
596 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
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::
601 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
603 * Any even bits set in the input are ignored.
605 * Returns: the unshuffled bits.
607 static inline uint64_t half_unshuffle64(uint64_t x
)
609 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
610 * where it is called an inverse half shuffle.
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
;
623 * @x: 8-bit value to be reversed
625 * Given an input value with bits::
629 * return the value with its bits reversed from left to right::
633 * Returns: the bit-reversed value.
635 static inline uint8_t bitrev8(uint8_t x
)
637 x
= ((x
>> 1) & 0x55) | ((x
<< 1) & 0xaa);
638 x
= ((x
>> 2) & 0x33) | ((x
<< 2) & 0xcc);
639 x
= (x
>> 4) | (x
<< 4) ;