1 // SPDX-License-Identifier: GPL-2.0+
2 #ifndef __LINUX_BITMAP_H
3 #define __LINUX_BITMAP_H
6 #include <linux/types.h>
7 #include <linux/bitops.h>
9 #define small_const_nbits(nbits) \
10 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
12 static inline void bitmap_zero(unsigned long *dst, int nbits)
14 if (small_const_nbits(nbits)) {
17 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
23 static inline unsigned long
24 find_next_bit(const unsigned long *addr, unsigned long size,
27 const unsigned long *p = addr + BIT_WORD(offset);
28 unsigned long result = offset & ~(BITS_PER_LONG - 1);
34 offset %= BITS_PER_LONG;
37 tmp &= (~0UL << offset);
38 if (size < BITS_PER_LONG)
42 size -= BITS_PER_LONG;
43 result += BITS_PER_LONG;
45 while (size & ~(BITS_PER_LONG - 1)) {
49 result += BITS_PER_LONG;
50 size -= BITS_PER_LONG;
57 tmp &= (~0UL >> (BITS_PER_LONG - size));
58 if (tmp == 0UL) /* Are any bits set? */
59 return result + size; /* Nope. */
61 return result + __ffs(tmp);
65 * Find the first set bit in a memory region.
67 static inline unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
71 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
73 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
79 #define for_each_set_bit(bit, addr, size) \
80 for ((bit) = find_first_bit((addr), (size)); \
82 (bit) = find_next_bit((addr), (size), (bit) + 1))
84 #endif /* __LINUX_BITMAP_H */