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_or_bit(const unsigned long *addr1
, const unsigned long *addr2
,
18 unsigned long nbits
, unsigned long start
);
19 unsigned long _find_next_zero_bit(const unsigned long *addr
, unsigned long nbits
,
21 extern unsigned long _find_first_bit(const unsigned long *addr
, unsigned long size
);
22 unsigned long __find_nth_bit(const unsigned long *addr
, unsigned long size
, unsigned long n
);
23 unsigned long __find_nth_and_bit(const unsigned long *addr1
, const unsigned long *addr2
,
24 unsigned long size
, unsigned long n
);
25 unsigned long __find_nth_andnot_bit(const unsigned long *addr1
, const unsigned long *addr2
,
26 unsigned long size
, unsigned long n
);
27 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1
, const unsigned long *addr2
,
28 const unsigned long *addr3
, unsigned long size
,
30 extern unsigned long _find_first_and_bit(const unsigned long *addr1
,
31 const unsigned long *addr2
, unsigned long size
);
32 unsigned long _find_first_and_and_bit(const unsigned long *addr1
, const unsigned long *addr2
,
33 const unsigned long *addr3
, unsigned long size
);
34 extern unsigned long _find_first_zero_bit(const unsigned long *addr
, unsigned long size
);
35 extern unsigned long _find_last_bit(const unsigned long *addr
, unsigned long size
);
38 unsigned long _find_first_zero_bit_le(const unsigned long *addr
, unsigned long size
);
39 unsigned long _find_next_zero_bit_le(const unsigned long *addr
, unsigned
40 long size
, unsigned long offset
);
41 unsigned long _find_next_bit_le(const unsigned long *addr
, unsigned
42 long size
, unsigned long offset
);
47 * find_next_bit - find the next set bit in a memory region
48 * @addr: The address to base the search on
49 * @size: The bitmap size in bits
50 * @offset: The bitnumber to start searching at
52 * Returns the bit number for the next set bit
53 * If no bits are set, returns @size.
55 static __always_inline
56 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
59 if (small_const_nbits(size
)) {
62 if (unlikely(offset
>= size
))
65 val
= *addr
& GENMASK(size
- 1, offset
);
66 return val
? __ffs(val
) : size
;
69 return _find_next_bit(addr
, size
, offset
);
73 #ifndef find_next_and_bit
75 * find_next_and_bit - find the next set bit in both memory regions
76 * @addr1: The first address to base the search on
77 * @addr2: The second address to base the search on
78 * @size: The bitmap size in bits
79 * @offset: The bitnumber to start searching at
81 * Returns the bit number for the next set bit
82 * If no bits are set, returns @size.
84 static __always_inline
85 unsigned long find_next_and_bit(const unsigned long *addr1
,
86 const unsigned long *addr2
, unsigned long size
,
89 if (small_const_nbits(size
)) {
92 if (unlikely(offset
>= size
))
95 val
= *addr1
& *addr2
& GENMASK(size
- 1, offset
);
96 return val
? __ffs(val
) : size
;
99 return _find_next_and_bit(addr1
, addr2
, size
, offset
);
103 #ifndef find_next_andnot_bit
105 * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits
107 * @addr1: The first address to base the search on
108 * @addr2: The second address to base the search on
109 * @size: The bitmap size in bits
110 * @offset: The bitnumber to start searching at
112 * Returns the bit number for the next set bit
113 * If no bits are set, returns @size.
115 static __always_inline
116 unsigned long find_next_andnot_bit(const unsigned long *addr1
,
117 const unsigned long *addr2
, unsigned long size
,
118 unsigned long offset
)
120 if (small_const_nbits(size
)) {
123 if (unlikely(offset
>= size
))
126 val
= *addr1
& ~*addr2
& GENMASK(size
- 1, offset
);
127 return val
? __ffs(val
) : size
;
130 return _find_next_andnot_bit(addr1
, addr2
, size
, offset
);
134 #ifndef find_next_or_bit
136 * find_next_or_bit - find the next set bit in either memory regions
137 * @addr1: The first address to base the search on
138 * @addr2: The second address to base the search on
139 * @size: The bitmap size in bits
140 * @offset: The bitnumber to start searching at
142 * Returns the bit number for the next set bit
143 * If no bits are set, returns @size.
145 static __always_inline
146 unsigned long find_next_or_bit(const unsigned long *addr1
,
147 const unsigned long *addr2
, unsigned long size
,
148 unsigned long offset
)
150 if (small_const_nbits(size
)) {
153 if (unlikely(offset
>= size
))
156 val
= (*addr1
| *addr2
) & GENMASK(size
- 1, offset
);
157 return val
? __ffs(val
) : size
;
160 return _find_next_or_bit(addr1
, addr2
, size
, offset
);
164 #ifndef find_next_zero_bit
166 * find_next_zero_bit - find the next cleared bit in a memory region
167 * @addr: The address to base the search on
168 * @size: The bitmap size in bits
169 * @offset: The bitnumber to start searching at
171 * Returns the bit number of the next zero bit
172 * If no bits are zero, returns @size.
174 static __always_inline
175 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
176 unsigned long offset
)
178 if (small_const_nbits(size
)) {
181 if (unlikely(offset
>= size
))
184 val
= *addr
| ~GENMASK(size
- 1, offset
);
185 return val
== ~0UL ? size
: ffz(val
);
188 return _find_next_zero_bit(addr
, size
, offset
);
192 #ifndef find_first_bit
194 * find_first_bit - find the first set bit in a memory region
195 * @addr: The address to start the search at
196 * @size: The maximum number of bits to search
198 * Returns the bit number of the first set bit.
199 * If no bits are set, returns @size.
201 static __always_inline
202 unsigned long find_first_bit(const unsigned long *addr
, unsigned long size
)
204 if (small_const_nbits(size
)) {
205 unsigned long val
= *addr
& GENMASK(size
- 1, 0);
207 return val
? __ffs(val
) : size
;
210 return _find_first_bit(addr
, size
);
215 * find_nth_bit - find N'th set bit in a memory region
216 * @addr: The address to start the search at
217 * @size: The maximum number of bits to search
218 * @n: The number of set bit, which position is needed, counting from 0
220 * The following is semantically equivalent:
221 * idx = find_nth_bit(addr, size, 0);
222 * idx = find_first_bit(addr, size);
224 * Returns the bit number of the N'th set bit.
225 * If no such, returns >= @size.
227 static __always_inline
228 unsigned long find_nth_bit(const unsigned long *addr
, unsigned long size
, unsigned long n
)
233 if (small_const_nbits(size
)) {
234 unsigned long val
= *addr
& GENMASK(size
- 1, 0);
236 return val
? fns(val
, n
) : size
;
239 return __find_nth_bit(addr
, size
, n
);
243 * find_nth_and_bit - find N'th set bit in 2 memory regions
244 * @addr1: The 1st address to start the search at
245 * @addr2: The 2nd address to start the search at
246 * @size: The maximum number of bits to search
247 * @n: The number of set bit, which position is needed, counting from 0
249 * Returns the bit number of the N'th set bit.
250 * If no such, returns @size.
252 static __always_inline
253 unsigned long find_nth_and_bit(const unsigned long *addr1
, const unsigned long *addr2
,
254 unsigned long size
, unsigned long n
)
259 if (small_const_nbits(size
)) {
260 unsigned long val
= *addr1
& *addr2
& GENMASK(size
- 1, 0);
262 return val
? fns(val
, n
) : size
;
265 return __find_nth_and_bit(addr1
, addr2
, size
, n
);
269 * find_nth_andnot_bit - find N'th set bit in 2 memory regions,
270 * flipping bits in 2nd region
271 * @addr1: The 1st address to start the search at
272 * @addr2: The 2nd address to start the search at
273 * @size: The maximum number of bits to search
274 * @n: The number of set bit, which position is needed, counting from 0
276 * Returns the bit number of the N'th set bit.
277 * If no such, returns @size.
279 static __always_inline
280 unsigned long find_nth_andnot_bit(const unsigned long *addr1
, const unsigned long *addr2
,
281 unsigned long size
, unsigned long n
)
286 if (small_const_nbits(size
)) {
287 unsigned long val
= *addr1
& (~*addr2
) & GENMASK(size
- 1, 0);
289 return val
? fns(val
, n
) : size
;
292 return __find_nth_andnot_bit(addr1
, addr2
, size
, n
);
296 * find_nth_and_andnot_bit - find N'th set bit in 2 memory regions,
297 * excluding those set in 3rd region
298 * @addr1: The 1st address to start the search at
299 * @addr2: The 2nd address to start the search at
300 * @addr3: The 3rd address to start the search at
301 * @size: The maximum number of bits to search
302 * @n: The number of set bit, which position is needed, counting from 0
304 * Returns the bit number of the N'th set bit.
305 * If no such, returns @size.
307 static __always_inline
308 unsigned long find_nth_and_andnot_bit(const unsigned long *addr1
,
309 const unsigned long *addr2
,
310 const unsigned long *addr3
,
311 unsigned long size
, unsigned long n
)
316 if (small_const_nbits(size
)) {
317 unsigned long val
= *addr1
& *addr2
& (~*addr3
) & GENMASK(size
- 1, 0);
319 return val
? fns(val
, n
) : size
;
322 return __find_nth_and_andnot_bit(addr1
, addr2
, addr3
, size
, n
);
325 #ifndef find_first_and_bit
327 * find_first_and_bit - find the first set bit in both memory regions
328 * @addr1: The first address to base the search on
329 * @addr2: The second address to base the search on
330 * @size: The bitmap size in bits
332 * Returns the bit number for the next set bit
333 * If no bits are set, returns @size.
335 static __always_inline
336 unsigned long find_first_and_bit(const unsigned long *addr1
,
337 const unsigned long *addr2
,
340 if (small_const_nbits(size
)) {
341 unsigned long val
= *addr1
& *addr2
& GENMASK(size
- 1, 0);
343 return val
? __ffs(val
) : size
;
346 return _find_first_and_bit(addr1
, addr2
, size
);
351 * find_first_and_and_bit - find the first set bit in 3 memory regions
352 * @addr1: The first address to base the search on
353 * @addr2: The second address to base the search on
354 * @addr3: The third address to base the search on
355 * @size: The bitmap size in bits
357 * Returns the bit number for the first set bit
358 * If no bits are set, returns @size.
360 static __always_inline
361 unsigned long find_first_and_and_bit(const unsigned long *addr1
,
362 const unsigned long *addr2
,
363 const unsigned long *addr3
,
366 if (small_const_nbits(size
)) {
367 unsigned long val
= *addr1
& *addr2
& *addr3
& GENMASK(size
- 1, 0);
369 return val
? __ffs(val
) : size
;
372 return _find_first_and_and_bit(addr1
, addr2
, addr3
, size
);
375 #ifndef find_first_zero_bit
377 * find_first_zero_bit - find the first cleared bit in a memory region
378 * @addr: The address to start the search at
379 * @size: The maximum number of bits to search
381 * Returns the bit number of the first cleared bit.
382 * If no bits are zero, returns @size.
384 static __always_inline
385 unsigned long find_first_zero_bit(const unsigned long *addr
, unsigned long size
)
387 if (small_const_nbits(size
)) {
388 unsigned long val
= *addr
| ~GENMASK(size
- 1, 0);
390 return val
== ~0UL ? size
: ffz(val
);
393 return _find_first_zero_bit(addr
, size
);
397 #ifndef find_last_bit
399 * find_last_bit - find the last set bit in a memory region
400 * @addr: The address to start the search at
401 * @size: The number of bits to search
403 * Returns the bit number of the last set bit, or size.
405 static __always_inline
406 unsigned long find_last_bit(const unsigned long *addr
, unsigned long size
)
408 if (small_const_nbits(size
)) {
409 unsigned long val
= *addr
& GENMASK(size
- 1, 0);
411 return val
? __fls(val
) : size
;
414 return _find_last_bit(addr
, size
);
419 * find_next_and_bit_wrap - find the next set bit in both memory regions
420 * @addr1: The first address to base the search on
421 * @addr2: The second address to base the search on
422 * @size: The bitmap size in bits
423 * @offset: The bitnumber to start searching at
425 * Returns the bit number for the next set bit, or first set bit up to @offset
426 * If no bits are set, returns @size.
428 static __always_inline
429 unsigned long find_next_and_bit_wrap(const unsigned long *addr1
,
430 const unsigned long *addr2
,
431 unsigned long size
, unsigned long offset
)
433 unsigned long bit
= find_next_and_bit(addr1
, addr2
, size
, offset
);
435 if (bit
< size
|| offset
== 0)
438 bit
= find_first_and_bit(addr1
, addr2
, offset
);
439 return bit
< offset
? bit
: size
;
443 * find_next_bit_wrap - find the next set bit in a memory region
444 * @addr: The address to base the search on
445 * @size: The bitmap size in bits
446 * @offset: The bitnumber to start searching at
448 * Returns the bit number for the next set bit, or first set bit up to @offset
449 * If no bits are set, returns @size.
451 static __always_inline
452 unsigned long find_next_bit_wrap(const unsigned long *addr
,
453 unsigned long size
, unsigned long offset
)
455 unsigned long bit
= find_next_bit(addr
, size
, offset
);
457 if (bit
< size
|| offset
== 0)
460 bit
= find_first_bit(addr
, offset
);
461 return bit
< offset
? bit
: size
;
465 * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing
466 * before using it alone.
468 static __always_inline
469 unsigned long __for_each_wrap(const unsigned long *bitmap
, unsigned long size
,
470 unsigned long start
, unsigned long n
)
474 /* If not wrapped around */
476 /* and have a bit, just return it. */
477 bit
= find_next_bit(bitmap
, size
, n
);
481 /* Otherwise, wrap around and ... */
485 /* Search the other part. */
486 bit
= find_next_bit(bitmap
, start
, n
);
487 return bit
< start
? bit
: size
;
491 * find_next_clump8 - find next 8-bit clump with set bits in a memory region
492 * @clump: location to store copy of found clump
493 * @addr: address to base the search on
494 * @size: bitmap size in number of bits
495 * @offset: bit offset at which to start searching
497 * Returns the bit offset for the next set clump; the found clump value is
498 * copied to the location pointed by @clump. If no bits are set, returns @size.
500 extern unsigned long find_next_clump8(unsigned long *clump
,
501 const unsigned long *addr
,
502 unsigned long size
, unsigned long offset
);
504 #define find_first_clump8(clump, bits, size) \
505 find_next_clump8((clump), (bits), (size), 0)
507 #if defined(__LITTLE_ENDIAN)
509 static __always_inline
510 unsigned long find_next_zero_bit_le(const void *addr
, unsigned long size
, unsigned long offset
)
512 return find_next_zero_bit(addr
, size
, offset
);
515 static __always_inline
516 unsigned long find_next_bit_le(const void *addr
, unsigned long size
, unsigned long offset
)
518 return find_next_bit(addr
, size
, offset
);
521 static __always_inline
522 unsigned long find_first_zero_bit_le(const void *addr
, unsigned long size
)
524 return find_first_zero_bit(addr
, size
);
527 #elif defined(__BIG_ENDIAN)
529 #ifndef find_next_zero_bit_le
530 static __always_inline
531 unsigned long find_next_zero_bit_le(const void *addr
, unsigned
532 long size
, unsigned long offset
)
534 if (small_const_nbits(size
)) {
535 unsigned long val
= *(const unsigned long *)addr
;
537 if (unlikely(offset
>= size
))
540 val
= swab(val
) | ~GENMASK(size
- 1, offset
);
541 return val
== ~0UL ? size
: ffz(val
);
544 return _find_next_zero_bit_le(addr
, size
, offset
);
548 #ifndef find_first_zero_bit_le
549 static __always_inline
550 unsigned long find_first_zero_bit_le(const void *addr
, unsigned long size
)
552 if (small_const_nbits(size
)) {
553 unsigned long val
= swab(*(const unsigned long *)addr
) | ~GENMASK(size
- 1, 0);
555 return val
== ~0UL ? size
: ffz(val
);
558 return _find_first_zero_bit_le(addr
, size
);
562 #ifndef find_next_bit_le
563 static __always_inline
564 unsigned long find_next_bit_le(const void *addr
, unsigned
565 long size
, unsigned long offset
)
567 if (small_const_nbits(size
)) {
568 unsigned long val
= *(const unsigned long *)addr
;
570 if (unlikely(offset
>= size
))
573 val
= swab(val
) & GENMASK(size
- 1, offset
);
574 return val
? __ffs(val
) : size
;
577 return _find_next_bit_le(addr
, size
, offset
);
582 #error "Please fix <asm/byteorder.h>"
585 #define for_each_set_bit(bit, addr, size) \
586 for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
588 #define for_each_and_bit(bit, addr1, addr2, size) \
590 (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
593 #define for_each_andnot_bit(bit, addr1, addr2, size) \
595 (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
598 #define for_each_or_bit(bit, addr1, addr2, size) \
600 (bit) = find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
603 /* same as for_each_set_bit() but use bit as value to start with */
604 #define for_each_set_bit_from(bit, addr, size) \
605 for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
607 #define for_each_clear_bit(bit, addr, size) \
609 (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \
612 /* same as for_each_clear_bit() but use bit as value to start with */
613 #define for_each_clear_bit_from(bit, addr, size) \
614 for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
617 * for_each_set_bitrange - iterate over all set bit ranges [b; e)
618 * @b: bit offset of start of current bitrange (first set bit)
619 * @e: bit offset of end of current bitrange (first unset bit)
620 * @addr: bitmap address to base the search on
621 * @size: bitmap size in number of bits
623 #define for_each_set_bitrange(b, e, addr, size) \
625 (b) = find_next_bit((addr), (size), b), \
626 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
631 * for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
632 * @b: bit offset of start of current bitrange (first set bit); must be initialized
633 * @e: bit offset of end of current bitrange (first unset bit)
634 * @addr: bitmap address to base the search on
635 * @size: bitmap size in number of bits
637 #define for_each_set_bitrange_from(b, e, addr, size) \
639 (b) = find_next_bit((addr), (size), (b)), \
640 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
645 * for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
646 * @b: bit offset of start of current bitrange (first unset bit)
647 * @e: bit offset of end of current bitrange (first set bit)
648 * @addr: bitmap address to base the search on
649 * @size: bitmap size in number of bits
651 #define for_each_clear_bitrange(b, e, addr, size) \
653 (b) = find_next_zero_bit((addr), (size), (b)), \
654 (e) = find_next_bit((addr), (size), (b) + 1), \
659 * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
660 * @b: bit offset of start of current bitrange (first set bit); must be initialized
661 * @e: bit offset of end of current bitrange (first unset bit)
662 * @addr: bitmap address to base the search on
663 * @size: bitmap size in number of bits
665 #define for_each_clear_bitrange_from(b, e, addr, size) \
667 (b) = find_next_zero_bit((addr), (size), (b)), \
668 (e) = find_next_bit((addr), (size), (b) + 1), \
673 * for_each_set_bit_wrap - iterate over all set bits starting from @start, and
674 * wrapping around the end of bitmap.
675 * @bit: offset for current iteration
676 * @addr: bitmap address to base the search on
677 * @size: bitmap size in number of bits
678 * @start: Starting bit for bitmap traversing, wrapping around the bitmap end
680 #define for_each_set_bit_wrap(bit, addr, size, start) \
681 for ((bit) = find_next_bit_wrap((addr), (size), (start)); \
683 (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1))
686 * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
687 * @start: bit offset to start search and to store the current iteration offset
688 * @clump: location to store copy of current 8-bit clump
689 * @bits: bitmap address to base the search on
690 * @size: bitmap size in number of bits
692 #define for_each_set_clump8(start, clump, bits, size) \
693 for ((start) = find_first_clump8(&(clump), (bits), (size)); \
695 (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
697 #endif /*__LINUX_FIND_H_ */