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_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))
28 * set_bit - Set a bit in memory
30 * @addr: the address to start counting from
32 static inline void set_bit(long nr
, unsigned long *addr
)
34 unsigned long mask
= BIT_MASK(nr
);
35 unsigned long *p
= addr
+ BIT_WORD(nr
);
41 * set_bit_atomic - Set a bit in memory atomically
43 * @addr: the address to start counting from
45 static inline void set_bit_atomic(long nr
, unsigned long *addr
)
47 unsigned long mask
= BIT_MASK(nr
);
48 unsigned long *p
= addr
+ BIT_WORD(nr
);
54 * clear_bit - Clears a bit in memory
56 * @addr: Address to start counting from
58 static inline void clear_bit(long nr
, unsigned long *addr
)
60 unsigned long mask
= BIT_MASK(nr
);
61 unsigned long *p
= addr
+ BIT_WORD(nr
);
67 * change_bit - Toggle a bit in memory
69 * @addr: Address to start counting from
71 static inline void change_bit(long nr
, unsigned long *addr
)
73 unsigned long mask
= BIT_MASK(nr
);
74 unsigned long *p
= addr
+ BIT_WORD(nr
);
80 * test_and_set_bit - Set a bit and return its old value
82 * @addr: Address to count from
84 static inline int test_and_set_bit(long nr
, unsigned long *addr
)
86 unsigned long mask
= BIT_MASK(nr
);
87 unsigned long *p
= addr
+ BIT_WORD(nr
);
88 unsigned long old
= *p
;
91 return (old
& mask
) != 0;
95 * test_and_clear_bit - Clear a bit and return its old value
97 * @addr: Address to count from
99 static inline int test_and_clear_bit(long nr
, unsigned long *addr
)
101 unsigned long mask
= BIT_MASK(nr
);
102 unsigned long *p
= addr
+ BIT_WORD(nr
);
103 unsigned long old
= *p
;
106 return (old
& mask
) != 0;
110 * test_and_change_bit - Change a bit and return its old value
112 * @addr: Address to count from
114 static inline int test_and_change_bit(long nr
, unsigned long *addr
)
116 unsigned long mask
= BIT_MASK(nr
);
117 unsigned long *p
= addr
+ BIT_WORD(nr
);
118 unsigned long old
= *p
;
121 return (old
& mask
) != 0;
125 * test_bit - Determine whether a bit is set
126 * @nr: bit number to test
127 * @addr: Address to start counting from
129 static inline int test_bit(long nr
, const unsigned long *addr
)
131 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
135 * find_last_bit - find the last set bit in a memory region
136 * @addr: The address to start the search at
137 * @size: The maximum size to search
139 * Returns the bit number of the first set bit, or size.
141 unsigned long find_last_bit(const unsigned long *addr
,
145 * find_next_bit - find the next set bit in a memory region
146 * @addr: The address to base the search on
147 * @offset: The bitnumber to start searching at
148 * @size: The bitmap size in bits
150 unsigned long find_next_bit(const unsigned long *addr
,
152 unsigned long offset
);
155 * find_next_zero_bit - find the next cleared bit in a memory region
156 * @addr: The address to base the search on
157 * @offset: The bitnumber to start searching at
158 * @size: The bitmap size in bits
161 unsigned long find_next_zero_bit(const unsigned long *addr
,
163 unsigned long offset
);
166 * find_first_bit - find the first set bit in a memory region
167 * @addr: The address to start the search at
168 * @size: The maximum size to search
170 * Returns the bit number of the first set bit.
172 static inline unsigned long find_first_bit(const unsigned long *addr
,
175 unsigned long result
, tmp
;
177 for (result
= 0; result
< size
; result
+= BITS_PER_LONG
) {
181 return result
< size
? result
: size
;
189 * find_first_zero_bit - find the first cleared bit in a memory region
190 * @addr: The address to start the search at
191 * @size: The maximum size to search
193 * Returns the bit number of the first cleared bit.
195 static inline unsigned long find_first_zero_bit(const unsigned long *addr
,
198 return find_next_zero_bit(addr
, size
, 0);
201 static inline unsigned long hweight_long(unsigned long w
)
205 for (count
= 0; w
; w
>>= 1) {
212 * rol8 - rotate an 8-bit value left
213 * @word: value to rotate
214 * @shift: bits to roll
216 static inline uint8_t rol8(uint8_t word
, unsigned int shift
)
218 return (word
<< shift
) | (word
>> (8 - shift
));
222 * ror8 - rotate an 8-bit value right
223 * @word: value to rotate
224 * @shift: bits to roll
226 static inline uint8_t ror8(uint8_t word
, unsigned int shift
)
228 return (word
>> shift
) | (word
<< (8 - shift
));
232 * rol16 - rotate a 16-bit value left
233 * @word: value to rotate
234 * @shift: bits to roll
236 static inline uint16_t rol16(uint16_t word
, unsigned int shift
)
238 return (word
<< shift
) | (word
>> (16 - shift
));
242 * ror16 - rotate a 16-bit value right
243 * @word: value to rotate
244 * @shift: bits to roll
246 static inline uint16_t ror16(uint16_t word
, unsigned int shift
)
248 return (word
>> shift
) | (word
<< (16 - shift
));
252 * rol32 - rotate a 32-bit value left
253 * @word: value to rotate
254 * @shift: bits to roll
256 static inline uint32_t rol32(uint32_t word
, unsigned int shift
)
258 return (word
<< shift
) | (word
>> (32 - shift
));
262 * ror32 - rotate a 32-bit value right
263 * @word: value to rotate
264 * @shift: bits to roll
266 static inline uint32_t ror32(uint32_t word
, unsigned int shift
)
268 return (word
>> shift
) | (word
<< (32 - shift
));
272 * rol64 - rotate a 64-bit value left
273 * @word: value to rotate
274 * @shift: bits to roll
276 static inline uint64_t rol64(uint64_t word
, unsigned int shift
)
278 return (word
<< shift
) | (word
>> (64 - shift
));
282 * ror64 - rotate a 64-bit value right
283 * @word: value to rotate
284 * @shift: bits to roll
286 static inline uint64_t ror64(uint64_t word
, unsigned int shift
)
288 return (word
>> shift
) | (word
<< (64 - shift
));
293 * @value: the value to extract the bit field from
294 * @start: the lowest bit in the bit field (numbered from 0)
295 * @length: the length of the bit field
297 * Extract from the 32 bit input @value the bit field specified by the
298 * @start and @length parameters, and return it. The bit field must
299 * lie entirely within the 32 bit word. It is valid to request that
300 * all 32 bits are returned (ie @length 32 and @start 0).
302 * Returns: the value of the bit field extracted from the input value.
304 static inline uint32_t extract32(uint32_t value
, int start
, int length
)
306 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
307 return (value
>> start
) & (~0U >> (32 - length
));
312 * @value: the value to extract the bit field from
313 * @start: the lowest bit in the bit field (numbered from 0)
314 * @length: the length of the bit field
316 * Extract from the 64 bit input @value the bit field specified by the
317 * @start and @length parameters, and return it. The bit field must
318 * lie entirely within the 64 bit word. It is valid to request that
319 * all 64 bits are returned (ie @length 64 and @start 0).
321 * Returns: the value of the bit field extracted from the input value.
323 static inline uint64_t extract64(uint64_t value
, int start
, int length
)
325 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
326 return (value
>> start
) & (~0ULL >> (64 - length
));
331 * @value: the value to extract the bit field from
332 * @start: the lowest bit in the bit field (numbered from 0)
333 * @length: the length of the bit field
335 * Extract from the 32 bit input @value the bit field specified by the
336 * @start and @length parameters, and return it, sign extended to
337 * an int32_t (ie with the most significant bit of the field propagated
338 * to all the upper bits of the return value). The bit field must lie
339 * entirely within the 32 bit word. It is valid to request that
340 * all 32 bits are returned (ie @length 32 and @start 0).
342 * Returns: the sign extended value of the bit field extracted from the
345 static inline int32_t sextract32(uint32_t value
, int start
, int length
)
347 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
348 /* Note that this implementation relies on right shift of signed
349 * integers being an arithmetic shift.
351 return ((int32_t)(value
<< (32 - length
- start
))) >> (32 - length
);
356 * @value: the value to extract the bit field from
357 * @start: the lowest bit in the bit field (numbered from 0)
358 * @length: the length of the bit field
360 * Extract from the 64 bit input @value the bit field specified by the
361 * @start and @length parameters, and return it, sign extended to
362 * an int64_t (ie with the most significant bit of the field propagated
363 * to all the upper bits of the return value). The bit field must lie
364 * entirely within the 64 bit word. It is valid to request that
365 * all 64 bits are returned (ie @length 64 and @start 0).
367 * Returns: the sign extended value of the bit field extracted from the
370 static inline int64_t sextract64(uint64_t value
, int start
, int length
)
372 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
373 /* Note that this implementation relies on right shift of signed
374 * integers being an arithmetic shift.
376 return ((int64_t)(value
<< (64 - length
- start
))) >> (64 - length
);
381 * @value: initial value to insert bit field into
382 * @start: the lowest bit in the bit field (numbered from 0)
383 * @length: the length of the bit field
384 * @fieldval: the value to insert into the bit field
386 * Deposit @fieldval into the 32 bit @value at the bit field specified
387 * by the @start and @length parameters, and return the modified
388 * @value. Bits of @value outside the bit field are not modified.
389 * Bits of @fieldval above the least significant @length bits are
390 * ignored. The bit field must lie entirely within the 32 bit word.
391 * It is valid to request that all 32 bits are modified (ie @length
394 * Returns: the modified @value.
396 static inline uint32_t deposit32(uint32_t value
, int start
, int length
,
400 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
401 mask
= (~0U >> (32 - length
)) << start
;
402 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
407 * @value: initial value to insert bit field into
408 * @start: the lowest bit in the bit field (numbered from 0)
409 * @length: the length of the bit field
410 * @fieldval: the value to insert into the bit field
412 * Deposit @fieldval into the 64 bit @value at the bit field specified
413 * by the @start and @length parameters, and return the modified
414 * @value. Bits of @value outside the bit field are not modified.
415 * Bits of @fieldval above the least significant @length bits are
416 * ignored. The bit field must lie entirely within the 64 bit word.
417 * It is valid to request that all 64 bits are modified (ie @length
420 * Returns: the modified @value.
422 static inline uint64_t deposit64(uint64_t value
, int start
, int length
,
426 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
427 mask
= (~0ULL >> (64 - length
)) << start
;
428 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
433 * @value: 32-bit value (of which only the bottom 16 bits are of interest)
435 * Given an input value:
436 * xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
437 * return the value where the bottom 16 bits are spread out into
438 * the odd bits in the word, and the even bits are zeroed:
439 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
441 * Any bits set in the top half of the input are ignored.
443 * Returns: the shuffled bits.
445 static inline uint32_t half_shuffle32(uint32_t x
)
447 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
448 * It ignores any bits set in the top half of the input.
450 x
= ((x
& 0xFF00) << 8) | (x
& 0x00FF);
451 x
= ((x
<< 4) | x
) & 0x0F0F0F0F;
452 x
= ((x
<< 2) | x
) & 0x33333333;
453 x
= ((x
<< 1) | x
) & 0x55555555;
459 * @value: 64-bit value (of which only the bottom 32 bits are of interest)
461 * Given an input value:
462 * xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
463 * return the value where the bottom 32 bits are spread out into
464 * the odd bits in the word, and the even bits are zeroed:
465 * 0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
467 * Any bits set in the top half of the input are ignored.
469 * Returns: the shuffled bits.
471 static inline uint64_t half_shuffle64(uint64_t x
)
473 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
474 * It ignores any bits set in the top half of the input.
476 x
= ((x
& 0xFFFF0000ULL
) << 16) | (x
& 0xFFFF);
477 x
= ((x
<< 8) | x
) & 0x00FF00FF00FF00FFULL
;
478 x
= ((x
<< 4) | x
) & 0x0F0F0F0F0F0F0F0FULL
;
479 x
= ((x
<< 2) | x
) & 0x3333333333333333ULL
;
480 x
= ((x
<< 1) | x
) & 0x5555555555555555ULL
;
486 * @value: 32-bit value (of which only the odd bits are of interest)
488 * Given an input value:
489 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
490 * return the value where all the odd bits are compressed down
491 * into the low half of the word, and the high half is zeroed:
492 * 0000 0000 0000 0000 ABCD EFGH IJKL MNOP
494 * Any even bits set in the input are ignored.
496 * Returns: the unshuffled bits.
498 static inline uint32_t half_unshuffle32(uint32_t x
)
500 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
501 * where it is called an inverse half shuffle.
504 x
= ((x
>> 1) | x
) & 0x33333333;
505 x
= ((x
>> 2) | x
) & 0x0F0F0F0F;
506 x
= ((x
>> 4) | x
) & 0x00FF00FF;
507 x
= ((x
>> 8) | x
) & 0x0000FFFF;
513 * @value: 64-bit value (of which only the odd bits are of interest)
515 * Given an input value:
516 * xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
517 * return the value where all the odd bits are compressed down
518 * into the low half of the word, and the high half is zeroed:
519 * 0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
521 * Any even bits set in the input are ignored.
523 * Returns: the unshuffled bits.
525 static inline uint64_t half_unshuffle64(uint64_t x
)
527 /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
528 * where it is called an inverse half shuffle.
530 x
&= 0x5555555555555555ULL
;
531 x
= ((x
>> 1) | x
) & 0x3333333333333333ULL
;
532 x
= ((x
>> 2) | x
) & 0x0F0F0F0F0F0F0F0FULL
;
533 x
= ((x
>> 4) | x
) & 0x00FF00FF00FF00FFULL
;
534 x
= ((x
>> 8) | x
) & 0x0000FFFF0000FFFFULL
;
535 x
= ((x
>> 16) | x
) & 0x00000000FFFFFFFFULL
;