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 extern unsigned long _find_first_and_bit(const unsigned long *addr1,
26 const unsigned long *addr2, unsigned long size);
27 extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
28 extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
31 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size);
32 unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned
33 long size, unsigned long offset);
34 unsigned long _find_next_bit_le(const unsigned long *addr, unsigned
35 long size, unsigned long offset);
40 * find_next_bit - find the next set bit in a memory region
41 * @addr: The address to base the search on
42 * @size: The bitmap size in bits
43 * @offset: The bitnumber to start searching at
45 * Returns the bit number for the next set bit
46 * If no bits are set, returns @size.
49 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
52 if (small_const_nbits(size)) {
55 if (unlikely(offset >= size))
58 val = *addr & GENMASK(size - 1, offset);
59 return val ? __ffs(val) : size;
62 return _find_next_bit(addr, size, offset);
66 #ifndef find_next_and_bit
68 * find_next_and_bit - find the next set bit in both memory regions
69 * @addr1: The first address to base the search on
70 * @addr2: The second address to base the search on
71 * @size: The bitmap size in bits
72 * @offset: The bitnumber to start searching at
74 * Returns the bit number for the next set bit
75 * If no bits are set, returns @size.
78 unsigned long find_next_and_bit(const unsigned long *addr1,
79 const unsigned long *addr2, unsigned long size,
82 if (small_const_nbits(size)) {
85 if (unlikely(offset >= size))
88 val = *addr1 & *addr2 & GENMASK(size - 1, offset);
89 return val ? __ffs(val) : size;
92 return _find_next_and_bit(addr1, addr2, size, offset);
96 #ifndef find_next_andnot_bit
98 * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits
100 * @addr1: The first address to base the search on
101 * @addr2: The second address to base the search on
102 * @size: The bitmap size in bits
103 * @offset: The bitnumber to start searching at
105 * Returns the bit number for the next set bit
106 * If no bits are set, returns @size.
109 unsigned long find_next_andnot_bit(const unsigned long *addr1,
110 const unsigned long *addr2, unsigned long size,
111 unsigned long offset)
113 if (small_const_nbits(size)) {
116 if (unlikely(offset >= size))
119 val = *addr1 & ~*addr2 & GENMASK(size - 1, offset);
120 return val ? __ffs(val) : size;
123 return _find_next_andnot_bit(addr1, addr2, size, offset);
127 #ifndef find_next_zero_bit
129 * find_next_zero_bit - find the next cleared bit in a memory region
130 * @addr: The address to base the search on
131 * @size: The bitmap size in bits
132 * @offset: The bitnumber to start searching at
134 * Returns the bit number of the next zero bit
135 * If no bits are zero, returns @size.
138 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
139 unsigned long offset)
141 if (small_const_nbits(size)) {
144 if (unlikely(offset >= size))
147 val = *addr | ~GENMASK(size - 1, offset);
148 return val == ~0UL ? size : ffz(val);
151 return _find_next_zero_bit(addr, size, offset);
155 #ifndef find_first_bit
157 * find_first_bit - find the first set bit in a memory region
158 * @addr: The address to start the search at
159 * @size: The maximum number of bits to search
161 * Returns the bit number of the first set bit.
162 * If no bits are set, returns @size.
165 unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
167 if (small_const_nbits(size)) {
168 unsigned long val = *addr & GENMASK(size - 1, 0);
170 return val ? __ffs(val) : size;
173 return _find_first_bit(addr, size);
178 * find_nth_bit - find N'th set bit in a memory region
179 * @addr: The address to start the search at
180 * @size: The maximum number of bits to search
181 * @n: The number of set bit, which position is needed, counting from 0
183 * The following is semantically equivalent:
184 * idx = find_nth_bit(addr, size, 0);
185 * idx = find_first_bit(addr, size);
187 * Returns the bit number of the N'th set bit.
188 * If no such, returns @size.
191 unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n)
196 if (small_const_nbits(size)) {
197 unsigned long val = *addr & GENMASK(size - 1, 0);
199 return val ? fns(val, n) : size;
202 return __find_nth_bit(addr, size, n);
206 * find_nth_and_bit - find N'th set bit in 2 memory regions
207 * @addr1: The 1st address to start the search at
208 * @addr2: The 2nd address to start the search at
209 * @size: The maximum number of bits to search
210 * @n: The number of set bit, which position is needed, counting from 0
212 * Returns the bit number of the N'th set bit.
213 * If no such, returns @size.
216 unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
217 unsigned long size, unsigned long n)
222 if (small_const_nbits(size)) {
223 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
225 return val ? fns(val, n) : size;
228 return __find_nth_and_bit(addr1, addr2, size, n);
232 * find_nth_andnot_bit - find N'th set bit in 2 memory regions,
233 * flipping bits in 2nd region
234 * @addr1: The 1st address to start the search at
235 * @addr2: The 2nd address to start the search at
236 * @size: The maximum number of bits to search
237 * @n: The number of set bit, which position is needed, counting from 0
239 * Returns the bit number of the N'th set bit.
240 * If no such, returns @size.
243 unsigned long find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
244 unsigned long size, unsigned long n)
249 if (small_const_nbits(size)) {
250 unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0);
252 return val ? fns(val, n) : size;
255 return __find_nth_andnot_bit(addr1, addr2, size, n);
258 #ifndef find_first_and_bit
260 * find_first_and_bit - find the first set bit in both memory regions
261 * @addr1: The first address to base the search on
262 * @addr2: The second address to base the search on
263 * @size: The bitmap size in bits
265 * Returns the bit number for the next set bit
266 * If no bits are set, returns @size.
269 unsigned long find_first_and_bit(const unsigned long *addr1,
270 const unsigned long *addr2,
273 if (small_const_nbits(size)) {
274 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
276 return val ? __ffs(val) : size;
279 return _find_first_and_bit(addr1, addr2, size);
283 #ifndef find_first_zero_bit
285 * find_first_zero_bit - find the first cleared bit in a memory region
286 * @addr: The address to start the search at
287 * @size: The maximum number of bits to search
289 * Returns the bit number of the first cleared bit.
290 * If no bits are zero, returns @size.
293 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
295 if (small_const_nbits(size)) {
296 unsigned long val = *addr | ~GENMASK(size - 1, 0);
298 return val == ~0UL ? size : ffz(val);
301 return _find_first_zero_bit(addr, size);
305 #ifndef find_last_bit
307 * find_last_bit - find the last set bit in a memory region
308 * @addr: The address to start the search at
309 * @size: The number of bits to search
311 * Returns the bit number of the last set bit, or size.
314 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
316 if (small_const_nbits(size)) {
317 unsigned long val = *addr & GENMASK(size - 1, 0);
319 return val ? __fls(val) : size;
322 return _find_last_bit(addr, size);
327 * find_next_and_bit_wrap - find the next 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
331 * @offset: The bitnumber to start searching at
333 * Returns the bit number for the next set bit, or first set bit up to @offset
334 * If no bits are set, returns @size.
337 unsigned long find_next_and_bit_wrap(const unsigned long *addr1,
338 const unsigned long *addr2,
339 unsigned long size, unsigned long offset)
341 unsigned long bit = find_next_and_bit(addr1, addr2, size, offset);
346 bit = find_first_and_bit(addr1, addr2, offset);
347 return bit < offset ? bit : size;
351 * find_next_bit_wrap - find the next set bit in both memory regions
352 * @addr: The first address to base the search on
353 * @size: The bitmap size in bits
354 * @offset: The bitnumber to start searching at
356 * Returns the bit number for the next set bit, or first set bit up to @offset
357 * If no bits are set, returns @size.
360 unsigned long find_next_bit_wrap(const unsigned long *addr,
361 unsigned long size, unsigned long offset)
363 unsigned long bit = find_next_bit(addr, size, offset);
368 bit = find_first_bit(addr, offset);
369 return bit < offset ? bit : size;
373 * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing
374 * before using it alone.
377 unsigned long __for_each_wrap(const unsigned long *bitmap, unsigned long size,
378 unsigned long start, unsigned long n)
382 /* If not wrapped around */
384 /* and have a bit, just return it. */
385 bit = find_next_bit(bitmap, size, n);
389 /* Otherwise, wrap around and ... */
393 /* Search the other part. */
394 bit = find_next_bit(bitmap, start, n);
395 return bit < start ? bit : size;
399 * find_next_clump8 - find next 8-bit clump with set bits in a memory region
400 * @clump: location to store copy of found clump
401 * @addr: address to base the search on
402 * @size: bitmap size in number of bits
403 * @offset: bit offset at which to start searching
405 * Returns the bit offset for the next set clump; the found clump value is
406 * copied to the location pointed by @clump. If no bits are set, returns @size.
408 extern unsigned long find_next_clump8(unsigned long *clump,
409 const unsigned long *addr,
410 unsigned long size, unsigned long offset);
412 #define find_first_clump8(clump, bits, size) \
413 find_next_clump8((clump), (bits), (size), 0)
415 #if defined(__LITTLE_ENDIAN)
417 static inline unsigned long find_next_zero_bit_le(const void *addr,
418 unsigned long size, unsigned long offset)
420 return find_next_zero_bit(addr, size, offset);
423 static inline unsigned long find_next_bit_le(const void *addr,
424 unsigned long size, unsigned long offset)
426 return find_next_bit(addr, size, offset);
429 static inline unsigned long find_first_zero_bit_le(const void *addr,
432 return find_first_zero_bit(addr, size);
435 #elif defined(__BIG_ENDIAN)
437 #ifndef find_next_zero_bit_le
439 unsigned long find_next_zero_bit_le(const void *addr, unsigned
440 long size, unsigned long offset)
442 if (small_const_nbits(size)) {
443 unsigned long val = *(const unsigned long *)addr;
445 if (unlikely(offset >= size))
448 val = swab(val) | ~GENMASK(size - 1, offset);
449 return val == ~0UL ? size : ffz(val);
452 return _find_next_zero_bit_le(addr, size, offset);
456 #ifndef find_first_zero_bit_le
458 unsigned long find_first_zero_bit_le(const void *addr, unsigned long size)
460 if (small_const_nbits(size)) {
461 unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0);
463 return val == ~0UL ? size : ffz(val);
466 return _find_first_zero_bit_le(addr, size);
470 #ifndef find_next_bit_le
472 unsigned long find_next_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 ? __ffs(val) : size;
485 return _find_next_bit_le(addr, size, offset);
490 #error "Please fix <asm/byteorder.h>"
493 #define for_each_set_bit(bit, addr, size) \
494 for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
496 #define for_each_and_bit(bit, addr1, addr2, size) \
498 (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
501 #define for_each_andnot_bit(bit, addr1, addr2, size) \
503 (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
506 /* same as for_each_set_bit() but use bit as value to start with */
507 #define for_each_set_bit_from(bit, addr, size) \
508 for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
510 #define for_each_clear_bit(bit, addr, size) \
512 (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \
515 /* same as for_each_clear_bit() but use bit as value to start with */
516 #define for_each_clear_bit_from(bit, addr, size) \
517 for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
520 * for_each_set_bitrange - iterate over all set bit ranges [b; e)
521 * @b: bit offset of start of current bitrange (first set bit)
522 * @e: bit offset of end of current bitrange (first unset bit)
523 * @addr: bitmap address to base the search on
524 * @size: bitmap size in number of bits
526 #define for_each_set_bitrange(b, e, addr, size) \
528 (b) = find_next_bit((addr), (size), b), \
529 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
534 * for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
535 * @b: bit offset of start of current bitrange (first set bit); must be initialized
536 * @e: bit offset of end of current bitrange (first unset bit)
537 * @addr: bitmap address to base the search on
538 * @size: bitmap size in number of bits
540 #define for_each_set_bitrange_from(b, e, addr, size) \
542 (b) = find_next_bit((addr), (size), (b)), \
543 (e) = find_next_zero_bit((addr), (size), (b) + 1), \
548 * for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
549 * @b: bit offset of start of current bitrange (first unset bit)
550 * @e: bit offset of end of current bitrange (first set bit)
551 * @addr: bitmap address to base the search on
552 * @size: bitmap size in number of bits
554 #define for_each_clear_bitrange(b, e, addr, size) \
556 (b) = find_next_zero_bit((addr), (size), (b)), \
557 (e) = find_next_bit((addr), (size), (b) + 1), \
562 * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
563 * @b: bit offset of start of current bitrange (first set bit); must be initialized
564 * @e: bit offset of end of current bitrange (first unset bit)
565 * @addr: bitmap address to base the search on
566 * @size: bitmap size in number of bits
568 #define for_each_clear_bitrange_from(b, e, addr, size) \
570 (b) = find_next_zero_bit((addr), (size), (b)), \
571 (e) = find_next_bit((addr), (size), (b) + 1), \
576 * for_each_set_bit_wrap - iterate over all set bits starting from @start, and
577 * wrapping around the end of bitmap.
578 * @bit: offset for current iteration
579 * @addr: bitmap address to base the search on
580 * @size: bitmap size in number of bits
581 * @start: Starting bit for bitmap traversing, wrapping around the bitmap end
583 #define for_each_set_bit_wrap(bit, addr, size, start) \
584 for ((bit) = find_next_bit_wrap((addr), (size), (start)); \
586 (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1))
589 * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
590 * @start: bit offset to start search and to store the current iteration offset
591 * @clump: location to store copy of current 8-bit clump
592 * @bits: bitmap address to base the search on
593 * @size: bitmap size in number of bits
595 #define for_each_set_clump8(start, clump, bits, size) \
596 for ((start) = find_first_clump8(&(clump), (bits), (size)); \
598 (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
600 #endif /*__LINUX_FIND_H_ */