1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* bit search implementation
4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * Copyright (C) 2008 IBM Corporation
8 * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
9 * (Inspired by David Howell's find_next_bit implementation)
11 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
12 * size and improve performance, 2015.
15 #include <linux/bitops.h>
16 #include <linux/bitmap.h>
17 #include <linux/export.h>
18 #include <linux/math.h>
19 #include <linux/minmax.h>
20 #include <linux/swab.h>
21 #include <linux/random.h>
24 * Common helper for find_bit() function family
25 * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
26 * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
27 * @size: The bitmap size in bits
29 #define FIND_FIRST_BIT(FETCH, MUNGE, size) \
31 unsigned long idx, val, sz = (size); \
33 for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \
36 sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \
45 * Common helper for find_next_bit() function family
46 * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
47 * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
48 * @size: The bitmap size in bits
49 * @start: The bitnumber to start searching at
51 #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
53 unsigned long mask, idx, tmp, sz = (size), __start = (start); \
55 if (unlikely(__start >= sz)) \
58 mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
59 idx = __start / BITS_PER_LONG; \
61 for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
62 if ((idx + 1) * BITS_PER_LONG >= sz) \
67 sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
72 #define FIND_NTH_BIT(FETCH, size, num) \
74 unsigned long sz = (size), nr = (num), idx, w, tmp; \
76 for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) { \
77 if (idx * BITS_PER_LONG + nr >= sz) \
81 w = hweight_long(tmp); \
88 if (sz % BITS_PER_LONG) \
89 tmp = (FETCH) & BITMAP_LAST_WORD_MASK(sz); \
91 sz = idx * BITS_PER_LONG + fns(tmp, nr); \
96 #ifndef find_first_bit
98 * Find the first set bit in a memory region.
100 unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
102 return FIND_FIRST_BIT(addr[idx], /* nop */, size);
104 EXPORT_SYMBOL(_find_first_bit);
107 #ifndef find_first_and_bit
109 * Find the first set bit in two memory regions.
111 unsigned long _find_first_and_bit(const unsigned long *addr1,
112 const unsigned long *addr2,
115 return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
117 EXPORT_SYMBOL(_find_first_and_bit);
121 * Find the first bit set in 1st memory region and unset in 2nd.
123 unsigned long _find_first_andnot_bit(const unsigned long *addr1,
124 const unsigned long *addr2,
127 return FIND_FIRST_BIT(addr1[idx] & ~addr2[idx], /* nop */, size);
129 EXPORT_SYMBOL(_find_first_andnot_bit);
132 * Find the first set bit in three memory regions.
134 unsigned long _find_first_and_and_bit(const unsigned long *addr1,
135 const unsigned long *addr2,
136 const unsigned long *addr3,
139 return FIND_FIRST_BIT(addr1[idx] & addr2[idx] & addr3[idx], /* nop */, size);
141 EXPORT_SYMBOL(_find_first_and_and_bit);
143 #ifndef find_first_zero_bit
145 * Find the first cleared bit in a memory region.
147 unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
149 return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
151 EXPORT_SYMBOL(_find_first_zero_bit);
154 #ifndef find_next_bit
155 unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
157 return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
159 EXPORT_SYMBOL(_find_next_bit);
162 unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n)
164 return FIND_NTH_BIT(addr[idx], size, n);
166 EXPORT_SYMBOL(__find_nth_bit);
168 unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
169 unsigned long size, unsigned long n)
171 return FIND_NTH_BIT(addr1[idx] & addr2[idx], size, n);
173 EXPORT_SYMBOL(__find_nth_and_bit);
175 unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
176 unsigned long size, unsigned long n)
178 return FIND_NTH_BIT(addr1[idx] & ~addr2[idx], size, n);
180 EXPORT_SYMBOL(__find_nth_andnot_bit);
182 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1,
183 const unsigned long *addr2,
184 const unsigned long *addr3,
185 unsigned long size, unsigned long n)
187 return FIND_NTH_BIT(addr1[idx] & addr2[idx] & ~addr3[idx], size, n);
189 EXPORT_SYMBOL(__find_nth_and_andnot_bit);
191 #ifndef find_next_and_bit
192 unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
193 unsigned long nbits, unsigned long start)
195 return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start);
197 EXPORT_SYMBOL(_find_next_and_bit);
200 #ifndef find_next_andnot_bit
201 unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
202 unsigned long nbits, unsigned long start)
204 return FIND_NEXT_BIT(addr1[idx] & ~addr2[idx], /* nop */, nbits, start);
206 EXPORT_SYMBOL(_find_next_andnot_bit);
209 #ifndef find_next_or_bit
210 unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2,
211 unsigned long nbits, unsigned long start)
213 return FIND_NEXT_BIT(addr1[idx] | addr2[idx], /* nop */, nbits, start);
215 EXPORT_SYMBOL(_find_next_or_bit);
218 #ifndef find_next_zero_bit
219 unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
222 return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
224 EXPORT_SYMBOL(_find_next_zero_bit);
227 #ifndef find_last_bit
228 unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
231 unsigned long val = BITMAP_LAST_WORD_MASK(size);
232 unsigned long idx = (size-1) / BITS_PER_LONG;
237 return idx * BITS_PER_LONG + __fls(val);
244 EXPORT_SYMBOL(_find_last_bit);
247 unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
248 unsigned long size, unsigned long offset)
250 offset = find_next_bit(addr, size, offset);
254 offset = round_down(offset, 8);
255 *clump = bitmap_get_value8(addr, offset);
259 EXPORT_SYMBOL(find_next_clump8);
263 #ifndef find_first_zero_bit_le
265 * Find the first cleared bit in an LE memory region.
267 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size)
269 return FIND_FIRST_BIT(~addr[idx], swab, size);
271 EXPORT_SYMBOL(_find_first_zero_bit_le);
275 #ifndef find_next_zero_bit_le
276 unsigned long _find_next_zero_bit_le(const unsigned long *addr,
277 unsigned long size, unsigned long offset)
279 return FIND_NEXT_BIT(~addr[idx], swab, size, offset);
281 EXPORT_SYMBOL(_find_next_zero_bit_le);
284 #ifndef find_next_bit_le
285 unsigned long _find_next_bit_le(const unsigned long *addr,
286 unsigned long size, unsigned long offset)
288 return FIND_NEXT_BIT(addr[idx], swab, size, offset);
290 EXPORT_SYMBOL(_find_next_bit_le);
294 #endif /* __BIG_ENDIAN */
297 * find_random_bit - find a set bit at random position
298 * @addr: The address to base the search on
299 * @size: The bitmap size in bits
301 * Returns: a position of a random set bit; >= @size otherwise
303 unsigned long find_random_bit(const unsigned long *addr, unsigned long size)
305 int w = bitmap_weight(addr, size);
311 /* Performance trick for single-bit bitmaps */
312 return find_first_bit(addr, size);
314 return find_nth_bit(addr, size, get_random_u32_below(w));
317 EXPORT_SYMBOL(find_random_bit);