1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_FIND_H_
3 #define __LINUX_FIND_H_
5 #ifndef __LINUX_BITMAP_H
6 #error only <linux/bitmap.h> can be included directly
9 #include <linux/bitops.h>
11 unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,
13 unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
14 unsigned long nbits, unsigned long start);
15 unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
16 unsigned long nbits, unsigned long start);
17 unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
19 extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
20 unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n);
21 unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
22 unsigned long size, unsigned long n);
23 unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
24 unsigned long size, unsigned long n);
25 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
26 const unsigned long *addr3, unsigned long size,
28 extern unsigned long _find_first_and_bit(const unsigned long *addr1,
29 const unsigned long *addr2, unsigned long size);
30 extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
31 extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
34 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size);
35 unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned
36 long size, unsigned long offset);
37 unsigned long _find_next_bit_le(const unsigned long *addr, unsigned
38 long size, unsigned long offset);
43 * find_next_bit - find the next set bit in a memory region
44 * @addr: The address to base the search on
45 * @size: The bitmap size in bits
46 * @offset: The bitnumber to start searching at
48 * Returns the bit number for the next set bit
49 * If no bits are set, returns @size.
52 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
55 if (small_const_nbits(size)) {
58 if (unlikely(offset >= size))
61 val = *addr & GENMASK(size - 1, offset);
62 return val ? __ffs(val) : size;
65 return _find_next_bit(addr, size, offset);
69 #ifndef find_next_and_bit
71 * find_next_and_bit - find the next set bit in both memory regions
72 * @addr1: The first address to base the search on
73 * @addr2: The second address to base the search on
74 * @size: The bitmap size in bits
75 * @offset: The bitnumber to start searching at
77 * Returns the bit number for the next set bit
78 * If no bits are set, returns @size.
81 unsigned long find_next_and_bit(const unsigned long *addr1,
82 const unsigned long *addr2, unsigned long size,
85 if (small_const_nbits(size)) {
88 if (unlikely(offset >= size))
91 val = *addr1 & *addr2 & GENMASK(size - 1, offset);
92 return val ? __ffs(val) : size;
95 return _find_next_and_bit(addr1, addr2, size, offset);
99 #ifndef find_next_andnot_bit
101 * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits
103 * @addr1: The first address to base the search on
104 * @addr2: The second address to base the search on
105 * @size: The bitmap size in bits
106 * @offset: The bitnumber to start searching at
108 * Returns the bit number for the next set bit
109 * If no bits are set, returns @size.
112 unsigned long find_next_andnot_bit(const unsigned long *addr1,
113 const unsigned long *addr2, unsigned long size,
114 unsigned long offset)
116 if (small_const_nbits(size)) {
119 if (unlikely(offset >= size))
122 val = *addr1 & ~*addr2 & GENMASK(size - 1, offset);
123 return val ? __ffs(val) : size;
126 return _find_next_andnot_bit(addr1, addr2, size, offset);
130 #ifndef find_next_zero_bit
132 * find_next_zero_bit - find the next cleared bit in a memory region
133 * @addr: The address to base the search on
134 * @size: The bitmap size in bits
135 * @offset: The bitnumber to start searching at
137 * Returns the bit number of the next zero bit
138 * If no bits are zero, returns @size.
141 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
142 unsigned long offset)
144 if (small_const_nbits(size)) {
147 if (unlikely(offset >= size))
150 val = *addr | ~GENMASK(size - 1, offset);
151 return val == ~0UL ? size : ffz(val);
154 return _find_next_zero_bit(addr, size, offset);
158 #ifndef find_first_bit
160 * find_first_bit - find the first set bit in a memory region
161 * @addr: The address to start the search at
162 * @size: The maximum number of bits to search
164 * Returns the bit number of the first set bit.
165 * If no bits are set, returns @size.
168 unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
170 if (small_const_nbits(size)) {
171 unsigned long val = *addr & GENMASK(size - 1, 0);
173 return val ? __ffs(val) : size;
176 return _find_first_bit(addr, size);
181 * find_nth_bit - find N'th set bit in a memory region
182 * @addr: The address to start the search at
183 * @size: The maximum number of bits to search
184 * @n: The number of set bit, which position is needed, counting from 0
186 * The following is semantically equivalent:
187 * idx = find_nth_bit(addr, size, 0);
188 * idx = find_first_bit(addr, size);
190 * Returns the bit number of the N'th set bit.
191 * If no such, returns @size.
194 unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n)
199 if (small_const_nbits(size)) {
200 unsigned long val = *addr & GENMASK(size - 1, 0);
202 return val ? fns(val, n) : size;
205 return __find_nth_bit(addr, size, n);
209 * find_nth_and_bit - find N'th set bit in 2 memory regions
210 * @addr1: The 1st address to start the search at
211 * @addr2: The 2nd address to start the search at
212 * @size: The maximum number of bits to search
213 * @n: The number of set bit, which position is needed, counting from 0
215 * Returns the bit number of the N'th set bit.
216 * If no such, returns @size.
219 unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
220 unsigned long size, unsigned long n)
225 if (small_const_nbits(size)) {
226 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
228 return val ? fns(val, n) : size;
231 return __find_nth_and_bit(addr1, addr2, size, n);
235 * find_nth_andnot_bit - find N'th set bit in 2 memory regions,
236 * flipping bits in 2nd region
237 * @addr1: The 1st address to start the search at
238 * @addr2: The 2nd address to start the search at
239 * @size: The maximum number of bits to search
240 * @n: The number of set bit, which position is needed, counting from 0
242 * Returns the bit number of the N'th set bit.
243 * If no such, returns @size.
246 unsigned long find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
247 unsigned long size, unsigned long n)
252 if (small_const_nbits(size)) {
253 unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0);
255 return val ? fns(val, n) : size;
258 return __find_nth_andnot_bit(addr1, addr2, size, n);
262 * find_nth_and_andnot_bit - find N'th set bit in 2 memory regions,
263 * excluding those set in 3rd region
264 * @addr1: The 1st address to start the search at
265 * @addr2: The 2nd address to start the search at
266 * @addr3: The 3rd address to start the search at
267 * @size: The maximum number of bits to search
268 * @n: The number of set bit, which position is needed, counting from 0
270 * Returns the bit number of the N'th set bit.
271 * If no such, returns @size.
273 static __always_inline
274 unsigned long find_nth_and_andnot_bit(const unsigned long *addr1,
275 const unsigned long *addr2,
276 const unsigned long *addr3,
277 unsigned long size, unsigned long n)
282 if (small_const_nbits(size)) {
283 unsigned long val = *addr1 & *addr2 & (~*addr3) & GENMASK(size - 1, 0);
285 return val ? fns(val, n) : size;
288 return __find_nth_and_andnot_bit(addr1, addr2, addr3, size, n);
291 #ifndef find_first_and_bit
293 * find_first_and_bit - find the first set bit in both memory regions
294 * @addr1: The first address to base the search on
295 * @addr2: The second address to base the search on
296 * @size: The bitmap size in bits
298 * Returns the bit number for the next set bit
299 * If no bits are set, returns @size.
302 unsigned long find_first_and_bit(const unsigned long *addr1,
303 const unsigned long *addr2,
306 if (small_const_nbits(size)) {
307 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
309 return val ? __ffs(val) : size;
312 return _find_first_and_bit(addr1, addr2, size);
316 #ifndef find_first_zero_bit
318 * find_first_zero_bit - find the first cleared bit in a memory region
319 * @addr: The address to start the search at
320 * @size: The maximum number of bits to search
322 * Returns the bit number of the first cleared bit.
323 * If no bits are zero, returns @size.
326 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
328 if (small_const_nbits(size)) {
329 unsigned long val = *addr | ~GENMASK(size - 1, 0);
331 return val == ~0UL ? size : ffz(val);
334 return _find_first_zero_bit(addr, size);
338 #ifndef find_last_bit
340 * find_last_bit - find the last set bit in a memory region
341 * @addr: The address to start the search at
342 * @size: The number of bits to search
344 * Returns the bit number of the last set bit, or size.
347 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
349 if (small_const_nbits(size)) {
350 unsigned long val = *addr & GENMASK(size - 1, 0);
352 return val ? __fls(val) : size;
355 return _find_last_bit(addr, size);
360 * find_next_and_bit_wrap - find the next set bit in both memory regions
361 * @addr1: The first address to base the search on
362 * @addr2: The second address to base the search on
363 * @size: The bitmap size in bits
364 * @offset: The bitnumber to start searching at
366 * Returns the bit number for the next set bit, or first set bit up to @offset
367 * If no bits are set, returns @size.
370 unsigned long find_next_and_bit_wrap(const unsigned long *addr1,
371 const unsigned long *addr2,
372 unsigned long size, unsigned long offset)
374 unsigned long bit = find_next_and_bit(addr1, addr2, size, offset);
379 bit = find_first_and_bit(addr1, addr2, offset);
380 return bit < offset ? bit : size;
384 * find_next_bit_wrap - find the next set bit in both memory regions
385 * @addr: The first address to base the search on
386 * @size: The bitmap size in bits
387 * @offset: The bitnumber to start searching at
389 * Returns the bit number for the next set bit, or first set bit up to @offset
390 * If no bits are set, returns @size.
393 unsigned long find_next_bit_wrap(const unsigned long *addr,
394 unsigned long size, unsigned long offset)
396 unsigned long bit = find_next_bit(addr, size, offset);
401 bit = find_first_bit(addr, offset);
402 return bit < offset ? bit : size;
406 * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing
407 * before using it alone.
410 unsigned long __for_each_wrap(const unsigned long *bitmap, unsigned long size,
411 unsigned long start, unsigned long n)
415 /* If not wrapped around */
417 /* and have a bit, just return it. */
418 bit = find_next_bit(bitmap, size, n);
422 /* Otherwise, wrap around and ... */
426 /* Search the other part. */
427 bit = find_next_bit(bitmap, start, n);
428 return bit < start ? bit : size;
432 * find_next_clump8 - find next 8-bit clump with set bits in a memory region
433 * @clump: location to store copy of found clump
434 * @addr: address to base the search on
435 * @size: bitmap size in number of bits
436 * @offset: bit offset at which to start searching
438 * Returns the bit offset for the next set clump; the found clump value is
439 * copied to the location pointed by @clump. If no bits are set, returns @size.
441 extern unsigned long find_next_clump8(unsigned long *clump,
442 const unsigned long *addr,
443 unsigned long size, unsigned long offset);
445 #define find_first_clump8(clump, bits, size) \
446 find_next_clump8((clump), (bits), (size), 0)
448 #if defined(__LITTLE_ENDIAN)
450 static inline unsigned long find_next_zero_bit_le(const void *addr,
451 unsigned long size, unsigned long offset)
453 return find_next_zero_bit(addr, size, offset);
456 static inline unsigned long find_next_bit_le(const void *addr,
457 unsigned long size, unsigned long offset)
459 return find_next_bit(addr, size, offset);
462 static inline unsigned long find_first_zero_bit_le(const void *addr,
465 return find_first_zero_bit(addr, size);
468 #elif defined(__BIG_ENDIAN)
470 #ifndef find_next_zero_bit_le
472 unsigned long find_next_zero_bit_le(const void *addr, unsigned
473 long size, unsigned long offset)
475 if (small_const_nbits(size)) {
476 unsigned long val = *(const unsigned long *)addr;
478 if (unlikely(offset >= size))
481 val = swab(val) | ~GENMASK(size - 1, offset);
482 return val == ~0UL ? size : ffz(val);
485 return _find_next_zero_bit_le(addr, size, offset);
489 #ifndef find_first_zero_bit_le
491 unsigned long find_first_zero_bit_le(const void *addr, unsigned long size)
493 if (small_const_nbits(size)) {
494 unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0);
496 return val == ~0UL ? size : ffz(val);
499 return _find_first_zero_bit_le(addr, size);
503 #ifndef find_next_bit_le
505 unsigned long find_next_bit_le(const void *addr, unsigned
506 long size, unsigned long offset)
508 if (small_const_nbits(size)) {
509 unsigned long val = *(const unsigned long *)addr;
511 if (unlikely(offset >= size))
514 val = swab(val) & GENMASK(size - 1, offset);
515 return val ? __ffs(val) : size;
518 return _find_next_bit_le(addr, size, offset);
523 #error "Please fix <asm/byteorder.h>"
526 #define for_each_set_bit(bit, addr, size) \
527 for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
529 #define for_each_and_bit(bit, addr1, addr2, size) \
531 (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
534 #define for_each_andnot_bit(bit, addr1, addr2, size) \
536 (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
539 /* same as for_each_set_bit() but use bit as value to start with */
540 #define for_each_set_bit_from(bit, addr, size) \
541 for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
543 #define for_each_clear_bit(bit, addr, size) \
545 (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \
548 /* same as for_each_clear_bit() but use bit as value to start with */
549 #define for_each_clear_bit_from(bit, addr, size) \
550 for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
553 * for_each_set_bitrange - iterate over all set bit ranges [b; e)
554 * @b: bit offset of start of current bitrange (first set bit)
555 * @e: bit offset of end of current bitrange (first unset bit)
556 * @addr: bitmap address to base the search on
557 * @size: bitmap size in number of bits
559 #define for_each_set_bitrange(b, e, addr, size) \
561 (b) = find_next_bit((addr), (size), b), \
562 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
567 * for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
568 * @b: bit offset of start of current bitrange (first set bit); must be initialized
569 * @e: bit offset of end of current bitrange (first unset bit)
570 * @addr: bitmap address to base the search on
571 * @size: bitmap size in number of bits
573 #define for_each_set_bitrange_from(b, e, addr, size) \
575 (b) = find_next_bit((addr), (size), (b)), \
576 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
581 * for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
582 * @b: bit offset of start of current bitrange (first unset bit)
583 * @e: bit offset of end of current bitrange (first set bit)
584 * @addr: bitmap address to base the search on
585 * @size: bitmap size in number of bits
587 #define for_each_clear_bitrange(b, e, addr, size) \
589 (b) = find_next_zero_bit((addr), (size), (b)), \
590 (e) = find_next_bit((addr), (size), (b) + 1), \
595 * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
596 * @b: bit offset of start of current bitrange (first set bit); must be initialized
597 * @e: bit offset of end of current bitrange (first unset bit)
598 * @addr: bitmap address to base the search on
599 * @size: bitmap size in number of bits
601 #define for_each_clear_bitrange_from(b, e, addr, size) \
603 (b) = find_next_zero_bit((addr), (size), (b)), \
604 (e) = find_next_bit((addr), (size), (b) + 1), \
609 * for_each_set_bit_wrap - iterate over all set bits starting from @start, and
610 * wrapping around the end of bitmap.
611 * @bit: offset for current iteration
612 * @addr: bitmap address to base the search on
613 * @size: bitmap size in number of bits
614 * @start: Starting bit for bitmap traversing, wrapping around the bitmap end
616 #define for_each_set_bit_wrap(bit, addr, size, start) \
617 for ((bit) = find_next_bit_wrap((addr), (size), (start)); \
619 (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1))
622 * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
623 * @start: bit offset to start search and to store the current iteration offset
624 * @clump: location to store copy of current 8-bit clump
625 * @bits: bitmap address to base the search on
626 * @size: bitmap size in number of bits
628 #define for_each_set_clump8(start, clump, bits, size) \
629 for ((start) = find_first_clump8(&(clump), (bits), (size)); \
631 (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
633 #endif /*__LINUX_FIND_H_ */