1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_BITOPS_FIND_H_
3 #define _ASM_GENERIC_BITOPS_FIND_H_
5 extern unsigned long _find_next_bit(const unsigned long *addr1,
6 const unsigned long *addr2, unsigned long nbits,
7 unsigned long start, unsigned long invert, unsigned long le);
11 * find_next_bit - find the next set bit in a memory region
12 * @addr: The address to base the search on
13 * @offset: The bitnumber to start searching at
14 * @size: The bitmap size in bits
16 * Returns the bit number for the next set bit
17 * If no bits are set, returns @size.
20 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
23 if (small_const_nbits(size)) {
26 if (unlikely(offset >= size))
29 val = *addr & GENMASK(size - 1, offset);
30 return val ? __ffs(val) : size;
33 return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
37 #ifndef find_next_and_bit
39 * find_next_and_bit - find the next set bit in both memory regions
40 * @addr1: The first address to base the search on
41 * @addr2: The second address to base the search on
42 * @offset: The bitnumber to start searching at
43 * @size: The bitmap size in bits
45 * Returns the bit number for the next set bit
46 * If no bits are set, returns @size.
49 unsigned long find_next_and_bit(const unsigned long *addr1,
50 const unsigned long *addr2, unsigned long size,
53 if (small_const_nbits(size)) {
56 if (unlikely(offset >= size))
59 val = *addr1 & *addr2 & GENMASK(size - 1, offset);
60 return val ? __ffs(val) : size;
63 return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
67 #ifndef find_next_zero_bit
69 * find_next_zero_bit - find the next cleared bit in a memory region
70 * @addr: The address to base the search on
71 * @offset: The bitnumber to start searching at
72 * @size: The bitmap size in bits
74 * Returns the bit number of the next zero bit
75 * If no bits are zero, returns @size.
78 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
81 if (small_const_nbits(size)) {
84 if (unlikely(offset >= size))
87 val = *addr | ~GENMASK(size - 1, offset);
88 return val == ~0UL ? size : ffz(val);
91 return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
95 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
98 * find_first_bit - find the first set bit in a memory region
99 * @addr: The address to start the search at
100 * @size: The maximum number of bits to search
102 * Returns the bit number of the first set bit.
103 * If no bits are set, returns @size.
105 extern unsigned long find_first_bit(const unsigned long *addr,
109 * find_first_zero_bit - find the first cleared bit in a memory region
110 * @addr: The address to start the search at
111 * @size: The maximum number of bits to search
113 * Returns the bit number of the first cleared bit.
114 * If no bits are zero, returns @size.
116 extern unsigned long find_first_zero_bit(const unsigned long *addr,
118 #else /* CONFIG_GENERIC_FIND_FIRST_BIT */
120 #ifndef find_first_bit
121 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
123 #ifndef find_first_zero_bit
124 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
127 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
130 * find_next_clump8 - find next 8-bit clump with set bits in a memory region
131 * @clump: location to store copy of found clump
132 * @addr: address to base the search on
133 * @size: bitmap size in number of bits
134 * @offset: bit offset at which to start searching
136 * Returns the bit offset for the next set clump; the found clump value is
137 * copied to the location pointed by @clump. If no bits are set, returns @size.
139 extern unsigned long find_next_clump8(unsigned long *clump,
140 const unsigned long *addr,
141 unsigned long size, unsigned long offset);
143 #define find_first_clump8(clump, bits, size) \
144 find_next_clump8((clump), (bits), (size), 0)
146 #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */