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>
23 * Common helper for find_bit() function family
24 * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
25 * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
26 * @size: The bitmap size in bits
28 #define FIND_FIRST_BIT(FETCH, MUNGE, size) \
30 unsigned long idx, val, sz = (size); \
32 for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \
35 sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \
43 #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
44 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
45 !defined(find_next_and_bit)
47 * This is a common helper function for find_next_bit, find_next_zero_bit, and
48 * find_next_and_bit. The differences are:
49 * - The "invert" argument, which is XORed with each fetched word before
50 * searching it for one bits.
51 * - The optional "addr2", which is anded with "addr1" if present.
53 unsigned long _find_next_bit(const unsigned long *addr1,
54 const unsigned long *addr2, unsigned long nbits,
55 unsigned long start, unsigned long invert, unsigned long le)
57 unsigned long tmp, mask;
59 if (unlikely(start >= nbits))
62 tmp = addr1[start / BITS_PER_LONG];
64 tmp &= addr2[start / BITS_PER_LONG];
67 /* Handle 1st word. */
68 mask = BITMAP_FIRST_WORD_MASK(start);
74 start = round_down(start, BITS_PER_LONG);
77 start += BITS_PER_LONG;
81 tmp = addr1[start / BITS_PER_LONG];
83 tmp &= addr2[start / BITS_PER_LONG];
90 return min(start + __ffs(tmp), nbits);
92 EXPORT_SYMBOL(_find_next_bit);
95 #ifndef find_first_bit
97 * Find the first set bit in a memory region.
99 unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
101 return FIND_FIRST_BIT(addr[idx], /* nop */, size);
103 EXPORT_SYMBOL(_find_first_bit);
106 #ifndef find_first_and_bit
108 * Find the first set bit in two memory regions.
110 unsigned long _find_first_and_bit(const unsigned long *addr1,
111 const unsigned long *addr2,
114 return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
116 EXPORT_SYMBOL(_find_first_and_bit);
119 #ifndef find_first_zero_bit
121 * Find the first cleared bit in a memory region.
123 unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
125 return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
127 EXPORT_SYMBOL(_find_first_zero_bit);
130 #ifndef find_last_bit
131 unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
134 unsigned long val = BITMAP_LAST_WORD_MASK(size);
135 unsigned long idx = (size-1) / BITS_PER_LONG;
140 return idx * BITS_PER_LONG + __fls(val);
147 EXPORT_SYMBOL(_find_last_bit);
150 unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
151 unsigned long size, unsigned long offset)
153 offset = find_next_bit(addr, size, offset);
157 offset = round_down(offset, 8);
158 *clump = bitmap_get_value8(addr, offset);
162 EXPORT_SYMBOL(find_next_clump8);
166 #ifndef find_first_zero_bit_le
168 * Find the first cleared bit in an LE memory region.
170 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size)
172 return FIND_FIRST_BIT(~addr[idx], swab, size);
174 EXPORT_SYMBOL(_find_first_zero_bit_le);
178 #endif /* __BIG_ENDIAN */