1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/bitmap.h>
3 #include <linux/math.h>
4 #include <linux/minmax.h>
7 * Common helper for find_next_bit() function family
8 * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
9 * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
10 * @size: The bitmap size in bits
11 * @start: The bitnumber to start searching at
13 #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
15 unsigned long mask, idx, tmp, sz = (size), __start = (start); \
17 if (unlikely(__start >= sz)) \
20 mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
21 idx = __start / BITS_PER_LONG; \
23 for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
24 if ((idx + 1) * BITS_PER_LONG >= sz) \
29 sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
34 unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
36 return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
39 unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
42 return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);