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,
139 unsigned long size, unsigned long offset);
142 * find_next_zero_bit - find the next cleared bit in a memory region
143 * @addr: The address to base the search on
144 * @offset: The bitnumber to start searching at
145 * @size: The bitmap size in bits
148 unsigned long find_next_zero_bit(const unsigned long *addr,
150 unsigned long offset);
153 * find_first_bit - find the first set bit in a memory region
154 * @addr: The address to start the search at
155 * @size: The maximum size to search
157 * Returns the bit number of the first set bit.
159 static inline unsigned long find_first_bit(const unsigned long *addr,
162 unsigned long result, tmp;
164 for (result = 0; result < size; result += BITS_PER_LONG) {
168 return result < size ? result : size;
176 * find_first_zero_bit - find the first cleared bit in a memory region
177 * @addr: The address to start the search at
178 * @size: The maximum size to search
180 * Returns the bit number of the first cleared bit.
182 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
185 return find_next_zero_bit(addr, size, 0);
188 static inline unsigned long hweight_long(unsigned long w)
192 for (count = 0; w; w >>= 1) {
199 * rol8 - rotate an 8-bit value left
200 * @word: value to rotate
201 * @shift: bits to roll
203 static inline uint8_t rol8(uint8_t word, unsigned int shift)
205 return (word << shift) | (word >> (8 - shift));
209 * ror8 - rotate an 8-bit value right
210 * @word: value to rotate
211 * @shift: bits to roll
213 static inline uint8_t ror8(uint8_t word, unsigned int shift)
215 return (word >> shift) | (word << (8 - shift));
219 * rol16 - rotate a 16-bit value left
220 * @word: value to rotate
221 * @shift: bits to roll
223 static inline uint16_t rol16(uint16_t word, unsigned int shift)
225 return (word << shift) | (word >> (16 - shift));
229 * ror16 - rotate a 16-bit value right
230 * @word: value to rotate
231 * @shift: bits to roll
233 static inline uint16_t ror16(uint16_t word, unsigned int shift)
235 return (word >> shift) | (word << (16 - shift));
239 * rol32 - rotate a 32-bit value left
240 * @word: value to rotate
241 * @shift: bits to roll
243 static inline uint32_t rol32(uint32_t word, unsigned int shift)
245 return (word << shift) | (word >> (32 - shift));
249 * ror32 - rotate a 32-bit value right
250 * @word: value to rotate
251 * @shift: bits to roll
253 static inline uint32_t ror32(uint32_t word, unsigned int shift)
255 return (word >> shift) | (word << (32 - shift));
259 * rol64 - rotate a 64-bit value left
260 * @word: value to rotate
261 * @shift: bits to roll
263 static inline uint64_t rol64(uint64_t word, unsigned int shift)
265 return (word << shift) | (word >> (64 - shift));
269 * ror64 - rotate a 64-bit value right
270 * @word: value to rotate
271 * @shift: bits to roll
273 static inline uint64_t ror64(uint64_t word, unsigned int shift)
275 return (word >> shift) | (word << (64 - shift));
280 * @value: the value to extract the bit field from
281 * @start: the lowest bit in the bit field (numbered from 0)
282 * @length: the length of the bit field
284 * Extract from the 32 bit input @value the bit field specified by the
285 * @start and @length parameters, and return it. The bit field must
286 * lie entirely within the 32 bit word. It is valid to request that
287 * all 32 bits are returned (ie @length 32 and @start 0).
289 * Returns: the value of the bit field extracted from the input value.
291 static inline uint32_t extract32(uint32_t value, int start, int length)
293 assert(start >= 0 && length > 0 && length <= 32 - start);
294 return (value >> start) & (~0U >> (32 - length));
299 * @value: the value to extract the bit field from
300 * @start: the lowest bit in the bit field (numbered from 0)
301 * @length: the length of the bit field
303 * Extract from the 64 bit input @value the bit field specified by the
304 * @start and @length parameters, and return it. The bit field must
305 * lie entirely within the 64 bit word. It is valid to request that
306 * all 64 bits are returned (ie @length 64 and @start 0).
308 * Returns: the value of the bit field extracted from the input value.
310 static inline uint64_t extract64(uint64_t value, int start, int length)
312 assert(start >= 0 && length > 0 && length <= 64 - start);
313 return (value >> start) & (~0ULL >> (64 - length));
318 * @value: the value to extract the bit field from
319 * @start: the lowest bit in the bit field (numbered from 0)
320 * @length: the length of the bit field
322 * Extract from the 32 bit input @value the bit field specified by the
323 * @start and @length parameters, and return it, sign extended to
324 * an int32_t (ie with the most significant bit of the field propagated
325 * to all the upper bits of the return value). The bit field must lie
326 * entirely within the 32 bit word. It is valid to request that
327 * all 32 bits are returned (ie @length 32 and @start 0).
329 * Returns: the sign extended value of the bit field extracted from the
332 static inline int32_t sextract32(uint32_t value, int start, int length)
334 assert(start >= 0 && length > 0 && length <= 32 - start);
335 /* Note that this implementation relies on right shift of signed
336 * integers being an arithmetic shift.
338 return ((int32_t)(value << (32 - length - start))) >> (32 - length);
343 * @value: the value to extract the bit field from
344 * @start: the lowest bit in the bit field (numbered from 0)
345 * @length: the length of the bit field
347 * Extract from the 64 bit input @value the bit field specified by the
348 * @start and @length parameters, and return it, sign extended to
349 * an int64_t (ie with the most significant bit of the field propagated
350 * to all the upper bits of the return value). The bit field must lie
351 * entirely within the 64 bit word. It is valid to request that
352 * all 64 bits are returned (ie @length 64 and @start 0).
354 * Returns: the sign extended value of the bit field extracted from the
357 static inline int64_t sextract64(uint64_t value, int start, int length)
359 assert(start >= 0 && length > 0 && length <= 64 - start);
360 /* Note that this implementation relies on right shift of signed
361 * integers being an arithmetic shift.
363 return ((int64_t)(value << (64 - length - start))) >> (64 - length);
368 * @value: initial value to insert bit field into
369 * @start: the lowest bit in the bit field (numbered from 0)
370 * @length: the length of the bit field
371 * @fieldval: the value to insert into the bit field
373 * Deposit @fieldval into the 32 bit @value at the bit field specified
374 * by the @start and @length parameters, and return the modified
375 * @value. Bits of @value outside the bit field are not modified.
376 * Bits of @fieldval above the least significant @length bits are
377 * ignored. The bit field must lie entirely within the 32 bit word.
378 * It is valid to request that all 32 bits are modified (ie @length
381 * Returns: the modified @value.
383 static inline uint32_t deposit32(uint32_t value, int start, int length,
387 assert(start >= 0 && length > 0 && length <= 32 - start);
388 mask = (~0U >> (32 - length)) << start;
389 return (value & ~mask) | ((fieldval << start) & mask);
394 * @value: initial value to insert bit field into
395 * @start: the lowest bit in the bit field (numbered from 0)
396 * @length: the length of the bit field
397 * @fieldval: the value to insert into the bit field
399 * Deposit @fieldval into the 64 bit @value at the bit field specified
400 * by the @start and @length parameters, and return the modified
401 * @value. Bits of @value outside the bit field are not modified.
402 * Bits of @fieldval above the least significant @length bits are
403 * ignored. The bit field must lie entirely within the 64 bit word.
404 * It is valid to request that all 64 bits are modified (ie @length
407 * Returns: the modified @value.
409 static inline uint64_t deposit64(uint64_t value, int start, int length,
413 assert(start >= 0 && length > 0 && length <= 64 - start);
414 mask = (~0ULL >> (64 - length)) << start;
415 return (value & ~mask) | ((fieldval << start) & mask);