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.
18 #include "host-utils.h"
20 #define BITS_PER_BYTE CHAR_BIT
21 #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
23 #define BIT(nr) (1UL << (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))
29 * set_bit - Set a bit in memory
31 * @addr: the address to start counting from
33 static inline void set_bit(long nr
, unsigned long *addr
)
35 unsigned long mask
= BIT_MASK(nr
);
36 unsigned long *p
= addr
+ BIT_WORD(nr
);
42 * clear_bit - Clears a bit in memory
44 * @addr: Address to start counting from
46 static inline void clear_bit(long nr
, unsigned long *addr
)
48 unsigned long mask
= BIT_MASK(nr
);
49 unsigned long *p
= addr
+ BIT_WORD(nr
);
55 * change_bit - Toggle a bit in memory
57 * @addr: Address to start counting from
59 static inline void change_bit(long nr
, unsigned long *addr
)
61 unsigned long mask
= BIT_MASK(nr
);
62 unsigned long *p
= addr
+ BIT_WORD(nr
);
68 * test_and_set_bit - Set a bit and return its old value
70 * @addr: Address to count from
72 static inline int test_and_set_bit(long nr
, unsigned long *addr
)
74 unsigned long mask
= BIT_MASK(nr
);
75 unsigned long *p
= addr
+ BIT_WORD(nr
);
76 unsigned long old
= *p
;
79 return (old
& mask
) != 0;
83 * test_and_clear_bit - Clear a bit and return its old value
85 * @addr: Address to count from
87 static inline int test_and_clear_bit(long nr
, unsigned long *addr
)
89 unsigned long mask
= BIT_MASK(nr
);
90 unsigned long *p
= addr
+ BIT_WORD(nr
);
91 unsigned long old
= *p
;
94 return (old
& mask
) != 0;
98 * test_and_change_bit - Change a bit and return its old value
100 * @addr: Address to count from
102 static inline int test_and_change_bit(long nr
, unsigned long *addr
)
104 unsigned long mask
= BIT_MASK(nr
);
105 unsigned long *p
= addr
+ BIT_WORD(nr
);
106 unsigned long old
= *p
;
109 return (old
& mask
) != 0;
113 * test_bit - Determine whether a bit is set
114 * @nr: bit number to test
115 * @addr: Address to start counting from
117 static inline int test_bit(long nr
, const unsigned long *addr
)
119 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
123 * find_last_bit - find the last set bit in a memory region
124 * @addr: The address to start the search at
125 * @size: The maximum size to search
127 * Returns the bit number of the first set bit, or size.
129 unsigned long find_last_bit(const unsigned long *addr
,
133 * find_next_bit - find the next set bit in a memory region
134 * @addr: The address to base the search on
135 * @offset: The bitnumber to start searching at
136 * @size: The bitmap size in bits
138 unsigned long find_next_bit(const unsigned long *addr
,
140 unsigned long offset
);
143 * find_next_zero_bit - find the next cleared bit in a memory region
144 * @addr: The address to base the search on
145 * @offset: The bitnumber to start searching at
146 * @size: The bitmap size in bits
149 unsigned long find_next_zero_bit(const unsigned long *addr
,
151 unsigned long offset
);
154 * find_first_bit - find the first set bit in a memory region
155 * @addr: The address to start the search at
156 * @size: The maximum size to search
158 * Returns the bit number of the first set bit.
160 static inline unsigned long find_first_bit(const unsigned long *addr
,
163 unsigned long result
, tmp
;
165 for (result
= 0; result
< size
; result
+= BITS_PER_LONG
) {
169 return result
< size
? result
: size
;
177 * find_first_zero_bit - find the first cleared 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 cleared bit.
183 static inline unsigned long find_first_zero_bit(const unsigned long *addr
,
186 return find_next_zero_bit(addr
, size
, 0);
189 static inline unsigned long hweight_long(unsigned long w
)
193 for (count
= 0; w
; w
>>= 1) {
200 * rol8 - rotate an 8-bit value left
201 * @word: value to rotate
202 * @shift: bits to roll
204 static inline uint8_t rol8(uint8_t word
, unsigned int shift
)
206 return (word
<< shift
) | (word
>> (8 - shift
));
210 * ror8 - rotate an 8-bit value right
211 * @word: value to rotate
212 * @shift: bits to roll
214 static inline uint8_t ror8(uint8_t word
, unsigned int shift
)
216 return (word
>> shift
) | (word
<< (8 - shift
));
220 * rol16 - rotate a 16-bit value left
221 * @word: value to rotate
222 * @shift: bits to roll
224 static inline uint16_t rol16(uint16_t word
, unsigned int shift
)
226 return (word
<< shift
) | (word
>> (16 - shift
));
230 * ror16 - rotate a 16-bit value right
231 * @word: value to rotate
232 * @shift: bits to roll
234 static inline uint16_t ror16(uint16_t word
, unsigned int shift
)
236 return (word
>> shift
) | (word
<< (16 - shift
));
240 * rol32 - rotate a 32-bit value left
241 * @word: value to rotate
242 * @shift: bits to roll
244 static inline uint32_t rol32(uint32_t word
, unsigned int shift
)
246 return (word
<< shift
) | (word
>> (32 - shift
));
250 * ror32 - rotate a 32-bit value right
251 * @word: value to rotate
252 * @shift: bits to roll
254 static inline uint32_t ror32(uint32_t word
, unsigned int shift
)
256 return (word
>> shift
) | (word
<< (32 - shift
));
260 * rol64 - rotate a 64-bit value left
261 * @word: value to rotate
262 * @shift: bits to roll
264 static inline uint64_t rol64(uint64_t word
, unsigned int shift
)
266 return (word
<< shift
) | (word
>> (64 - shift
));
270 * ror64 - rotate a 64-bit value right
271 * @word: value to rotate
272 * @shift: bits to roll
274 static inline uint64_t ror64(uint64_t word
, unsigned int shift
)
276 return (word
>> shift
) | (word
<< (64 - shift
));
281 * @value: the value to extract the bit field from
282 * @start: the lowest bit in the bit field (numbered from 0)
283 * @length: the length of the bit field
285 * Extract from the 32 bit input @value the bit field specified by the
286 * @start and @length parameters, and return it. The bit field must
287 * lie entirely within the 32 bit word. It is valid to request that
288 * all 32 bits are returned (ie @length 32 and @start 0).
290 * Returns: the value of the bit field extracted from the input value.
292 static inline uint32_t extract32(uint32_t value
, int start
, int length
)
294 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
295 return (value
>> start
) & (~0U >> (32 - length
));
300 * @value: the value to extract the bit field from
301 * @start: the lowest bit in the bit field (numbered from 0)
302 * @length: the length of the bit field
304 * Extract from the 64 bit input @value the bit field specified by the
305 * @start and @length parameters, and return it. The bit field must
306 * lie entirely within the 64 bit word. It is valid to request that
307 * all 64 bits are returned (ie @length 64 and @start 0).
309 * Returns: the value of the bit field extracted from the input value.
311 static inline uint64_t extract64(uint64_t value
, int start
, int length
)
313 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
314 return (value
>> start
) & (~0ULL >> (64 - length
));
319 * @value: the value to extract the bit field from
320 * @start: the lowest bit in the bit field (numbered from 0)
321 * @length: the length of the bit field
323 * Extract from the 32 bit input @value the bit field specified by the
324 * @start and @length parameters, and return it, sign extended to
325 * an int32_t (ie with the most significant bit of the field propagated
326 * to all the upper bits of the return value). The bit field must lie
327 * entirely within the 32 bit word. It is valid to request that
328 * all 32 bits are returned (ie @length 32 and @start 0).
330 * Returns: the sign extended value of the bit field extracted from the
333 static inline int32_t sextract32(uint32_t value
, int start
, int length
)
335 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
336 /* Note that this implementation relies on right shift of signed
337 * integers being an arithmetic shift.
339 return ((int32_t)(value
<< (32 - length
- start
))) >> (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 64 bit input @value the bit field specified by the
349 * @start and @length parameters, and return it, sign extended to
350 * an int64_t (ie with the most significant bit of the field propagated
351 * to all the upper bits of the return value). The bit field must lie
352 * entirely within the 64 bit word. It is valid to request that
353 * all 64 bits are returned (ie @length 64 and @start 0).
355 * Returns: the sign extended value of the bit field extracted from the
358 static inline int64_t sextract64(uint64_t value
, int start
, int length
)
360 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
361 /* Note that this implementation relies on right shift of signed
362 * integers being an arithmetic shift.
364 return ((int64_t)(value
<< (64 - length
- start
))) >> (64 - length
);
369 * @value: initial value to insert bit field into
370 * @start: the lowest bit in the bit field (numbered from 0)
371 * @length: the length of the bit field
372 * @fieldval: the value to insert into the bit field
374 * Deposit @fieldval into the 32 bit @value at the bit field specified
375 * by the @start and @length parameters, and return the modified
376 * @value. Bits of @value outside the bit field are not modified.
377 * Bits of @fieldval above the least significant @length bits are
378 * ignored. The bit field must lie entirely within the 32 bit word.
379 * It is valid to request that all 32 bits are modified (ie @length
382 * Returns: the modified @value.
384 static inline uint32_t deposit32(uint32_t value
, int start
, int length
,
388 assert(start
>= 0 && length
> 0 && length
<= 32 - start
);
389 mask
= (~0U >> (32 - length
)) << start
;
390 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);
395 * @value: initial value to insert bit field into
396 * @start: the lowest bit in the bit field (numbered from 0)
397 * @length: the length of the bit field
398 * @fieldval: the value to insert into the bit field
400 * Deposit @fieldval into the 64 bit @value at the bit field specified
401 * by the @start and @length parameters, and return the modified
402 * @value. Bits of @value outside the bit field are not modified.
403 * Bits of @fieldval above the least significant @length bits are
404 * ignored. The bit field must lie entirely within the 64 bit word.
405 * It is valid to request that all 64 bits are modified (ie @length
408 * Returns: the modified @value.
410 static inline uint64_t deposit64(uint64_t value
, int start
, int length
,
414 assert(start
>= 0 && length
> 0 && length
<= 64 - start
);
415 mask
= (~0ULL >> (64 - length
)) << start
;
416 return (value
& ~mask
) | ((fieldval
<< start
) & mask
);