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 first set bit, or size.
145 unsigned long find_last_bit(const unsigned long *addr
,
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
154 unsigned long find_next_bit(const unsigned long *addr
,
156 unsigned long offset
);
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
165 unsigned long find_next_zero_bit(const unsigned long *addr
,
167 unsigned long offset
);
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
174 * Returns the bit number of the first set bit.
176 static inline unsigned long find_first_bit(const unsigned long *addr
,
179 unsigned long result
, tmp
;
181 for (result
= 0; result
< size
; result
+= BITS_PER_LONG
) {
185 return result
< size
? result
: size
;
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
197 * Returns the bit number of the first cleared bit.
199 static inline unsigned long find_first_zero_bit(const unsigned long *addr
,
202 return find_next_zero_bit(addr
, size
, 0);
206 * rol8 - rotate an 8-bit value left
207 * @word: value to rotate
208 * @shift: bits to roll
210 static inline uint8_t rol8(uint8_t word
, unsigned int shift
)
212 return (word
<< shift
) | (word
>> ((8 - shift
) & 7));
216 * ror8 - rotate an 8-bit value right
217 * @word: value to rotate
218 * @shift: bits to roll
220 static inline uint8_t ror8(uint8_t word
, unsigned int shift
)
222 return (word
>> shift
) | (word
<< ((8 - shift
) & 7));
226 * rol16 - rotate a 16-bit value left
227 * @word: value to rotate
228 * @shift: bits to roll
230 static inline uint16_t rol16(uint16_t word
, unsigned int shift
)
232 return (word
<< shift
) | (word
>> ((16 - shift
) & 15));
236 * ror16 - rotate a 16-bit value right
237 * @word: value to rotate
238 * @shift: bits to roll
240 static inline uint16_t ror16(uint16_t word
, unsigned int shift
)
242 return (word
>> shift
) | (word
<< ((16 - shift
) & 15));
246 * rol32 - rotate a 32-bit value left
247 * @word: value to rotate
248 * @shift: bits to roll
250 static inline uint32_t rol32(uint32_t word
, unsigned int shift
)
252 return (word
<< shift
) | (word
>> ((32 - shift
) & 31));
256 * ror32 - rotate a 32-bit value right
257 * @word: value to rotate
258 * @shift: bits to roll
260 static inline uint32_t ror32(uint32_t word
, unsigned int shift
)
262 return (word
>> shift
) | (word
<< ((32 - shift
) & 31));
266 * rol64 - rotate a 64-bit value left
267 * @word: value to rotate
268 * @shift: bits to roll
270 static inline uint64_t rol64(uint64_t word
, unsigned int shift
)
272 return (word
<< shift
) | (word
>> ((64 - shift
) & 63));
276 * ror64 - rotate a 64-bit value right
277 * @word: value to rotate
278 * @shift: bits to roll
280 static inline uint64_t ror64(uint64_t word
, unsigned int shift
)
282 return (word
>> shift
) | (word
<< ((64 - shift
) & 63));
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
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).
296 * Returns: the value of the bit field extracted from the input value.
298 static inline uint32_t extract32(uint32_t value
, int start
, int length
)
300 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
301 return (value
>> start
) & (~0U >> (32 - length
));
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
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).
315 * Returns: the value of the bit field extracted from the input value.
317 static inline uint8_t extract8(uint8_t value
, int start
, int length
)
319 assert(start
>= 0 && length
> 0 && length
<= 8 - start
);
320 return extract32(value
, start
, length
);
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 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).
334 * Returns: the value of the bit field extracted from the input value.
336 static inline uint16_t extract16(uint16_t value
, int start
, int length
)
338 assert(start
>= 0 && length
> 0 && length
<= 16 - start
);
339 return extract32(value
, start
, 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 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).
353 * Returns: the value of the bit field extracted from the input value.
355 static inline uint64_t extract64(uint64_t value
, int start
, int length
)
357 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
358 return (value
>> start
) & (~0ULL >> (64 - 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 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).
374 * Returns: the sign extended value of the bit field extracted from the
377 static inline int32_t sextract32(uint32_t value
, int start
, int length
)
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.
383 return ((int32_t)(value
<< (32 - length
- start
))) >> (32 - length
);
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
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).
399 * Returns: the sign extended value of the bit field extracted from the
402 static inline int64_t sextract64(uint64_t value
, int start
, int length
)
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.
408 return ((int64_t)(value
<< (64 - length
- start
))) >> (64 - length
);
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
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
426 * Returns: the modified @value.
428 static inline uint32_t deposit32(uint32_t value
, int start
, int length
,
432 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
433 mask
= (~0U >> (32 - length
)) << start
;
434 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
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
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
452 * Returns: the modified @value.
454 static inline uint64_t deposit64(uint64_t value
, int start
, int length
,
458 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
459 mask
= (~0ULL >> (64 - length
)) << start
;
460 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
465 * @x: 32-bit value (of which only the bottom 16 bits are of interest)
467 * Given an input value::
469 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
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::
474 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
476 * Any bits set in the top half of the input are ignored.
478 * Returns: the shuffled bits.
480 static inline uint32_t half_shuffle32(uint32_t x
)
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.
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;
494 * @x: 64-bit value (of which only the bottom 32 bits are of interest)
496 * Given an input value::
498 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
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::
503 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
505 * Any bits set in the top half of the input are ignored.
507 * Returns: the shuffled bits.
509 static inline uint64_t half_shuffle64(uint64_t x
)
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.
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
;
524 * @x: 32-bit value (of which only the odd bits are of interest)
526 * Given an input value::
528 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
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::
533 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
535 * Any even bits set in the input are ignored.
537 * Returns: the unshuffled bits.
539 static inline uint32_t half_unshuffle32(uint32_t x
)
541 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
542 * where it is called an inverse half shuffle.
545 x
= ((x
>> 1) | x
) & 0x33333333;
546 x
= ((x
>> 2) | x
) & 0x0F0F0F0F;
547 x
= ((x
>> 4) | x
) & 0x00FF00FF;
548 x
= ((x
>> 8) | x
) & 0x0000FFFF;
554 * @x: 64-bit value (of which only the odd bits are of interest)
556 * Given an input value::
558 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
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::
563 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
565 * Any even bits set in the input are ignored.
567 * Returns: the unshuffled bits.
569 static inline uint64_t half_unshuffle64(uint64_t x
)
571 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
572 * where it is called an inverse half shuffle.
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
;