1*745e3ed8SKirill A. Shutemov // SPDX-License-Identifier: GPL-2.0-only
2*745e3ed8SKirill A. Shutemov #include <linux/bitmap.h>
3*745e3ed8SKirill A. Shutemov #include <linux/math.h>
4*745e3ed8SKirill A. Shutemov #include <linux/minmax.h>
5*745e3ed8SKirill A. Shutemov
6*745e3ed8SKirill A. Shutemov /*
7*745e3ed8SKirill A. Shutemov * Common helper for find_next_bit() function family
8*745e3ed8SKirill A. Shutemov * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
9*745e3ed8SKirill A. Shutemov * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
10*745e3ed8SKirill A. Shutemov * @size: The bitmap size in bits
11*745e3ed8SKirill A. Shutemov * @start: The bitnumber to start searching at
12*745e3ed8SKirill A. Shutemov */
13*745e3ed8SKirill A. Shutemov #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
14*745e3ed8SKirill A. Shutemov ({ \
15*745e3ed8SKirill A. Shutemov unsigned long mask, idx, tmp, sz = (size), __start = (start); \
16*745e3ed8SKirill A. Shutemov \
17*745e3ed8SKirill A. Shutemov if (unlikely(__start >= sz)) \
18*745e3ed8SKirill A. Shutemov goto out; \
19*745e3ed8SKirill A. Shutemov \
20*745e3ed8SKirill A. Shutemov mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
21*745e3ed8SKirill A. Shutemov idx = __start / BITS_PER_LONG; \
22*745e3ed8SKirill A. Shutemov \
23*745e3ed8SKirill A. Shutemov for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
24*745e3ed8SKirill A. Shutemov if ((idx + 1) * BITS_PER_LONG >= sz) \
25*745e3ed8SKirill A. Shutemov goto out; \
26*745e3ed8SKirill A. Shutemov idx++; \
27*745e3ed8SKirill A. Shutemov } \
28*745e3ed8SKirill A. Shutemov \
29*745e3ed8SKirill A. Shutemov sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
30*745e3ed8SKirill A. Shutemov out: \
31*745e3ed8SKirill A. Shutemov sz; \
32*745e3ed8SKirill A. Shutemov })
33*745e3ed8SKirill A. Shutemov
_find_next_bit(const unsigned long * addr,unsigned long nbits,unsigned long start)34*745e3ed8SKirill A. Shutemov unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
35*745e3ed8SKirill A. Shutemov {
36*745e3ed8SKirill A. Shutemov return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
37*745e3ed8SKirill A. Shutemov }
38*745e3ed8SKirill A. Shutemov
_find_next_zero_bit(const unsigned long * addr,unsigned long nbits,unsigned long start)39*745e3ed8SKirill A. Shutemov unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
40*745e3ed8SKirill A. Shutemov unsigned long start)
41*745e3ed8SKirill A. Shutemov {
42*745e3ed8SKirill A. Shutemov return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
43*745e3ed8SKirill A. Shutemov }
44