blob: f67f86fd2f620ed92b2e36de7b5f28d9851cbed7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yury Norov8f6f19d2015-04-16 12:43:16 -07002/* bit search implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
Yury Norov8f6f19d2015-04-16 12:43:16 -07007 * 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)
10 *
Yury Norov2c57a0e2015-04-16 12:43:13 -070011 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
12 * size and improve performance, 2015.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/bitops.h>
Yury Norov8f6f19d2015-04-16 12:43:16 -070016#include <linux/bitmap.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050017#include <linux/export.h>
Andy Shevchenkoaa6159a2020-12-15 20:42:48 -080018#include <linux/math.h>
Andy Shevchenkob296a6d2020-10-15 20:10:21 -070019#include <linux/minmax.h>
Andy Shevchenkoaa6159a2020-12-15 20:42:48 -080020#include <linux/swab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Yury Norovb78c5712020-01-30 22:16:43 -080022#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
23 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
24 !defined(find_next_and_bit)
Yury Norov2c57a0e2015-04-16 12:43:13 -070025/*
Clement Courbet0ade34c2018-02-06 15:38:34 -080026 * This is a common helper function for find_next_bit, find_next_zero_bit, and
27 * find_next_and_bit. The differences are:
28 * - The "invert" argument, which is XORed with each fetched word before
29 * searching it for one bits.
30 * - The optional "addr2", which is anded with "addr1" if present.
Yury Norov2c57a0e2015-04-16 12:43:13 -070031 */
Yury Norov7dfaa98f2020-01-30 22:16:47 -080032static unsigned long _find_next_bit(const unsigned long *addr1,
Clement Courbet0ade34c2018-02-06 15:38:34 -080033 const unsigned long *addr2, unsigned long nbits,
Yury Norovb78c5712020-01-30 22:16:43 -080034 unsigned long start, unsigned long invert, unsigned long le)
Yury Norov2c57a0e2015-04-16 12:43:13 -070035{
Yury Norovb78c5712020-01-30 22:16:43 -080036 unsigned long tmp, mask;
Yury Norov2c57a0e2015-04-16 12:43:13 -070037
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080038 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -070039 return nbits;
40
Clement Courbet0ade34c2018-02-06 15:38:34 -080041 tmp = addr1[start / BITS_PER_LONG];
42 if (addr2)
43 tmp &= addr2[start / BITS_PER_LONG];
44 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070045
46 /* Handle 1st word. */
Yury Norovb78c5712020-01-30 22:16:43 -080047 mask = BITMAP_FIRST_WORD_MASK(start);
48 if (le)
49 mask = swab(mask);
50
51 tmp &= mask;
52
Yury Norov2c57a0e2015-04-16 12:43:13 -070053 start = round_down(start, BITS_PER_LONG);
54
55 while (!tmp) {
56 start += BITS_PER_LONG;
57 if (start >= nbits)
58 return nbits;
59
Clement Courbet0ade34c2018-02-06 15:38:34 -080060 tmp = addr1[start / BITS_PER_LONG];
61 if (addr2)
62 tmp &= addr2[start / BITS_PER_LONG];
63 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070064 }
65
Yury Norovb78c5712020-01-30 22:16:43 -080066 if (le)
67 tmp = swab(tmp);
68
Yury Norov2c57a0e2015-04-16 12:43:13 -070069 return min(start + __ffs(tmp), nbits);
70}
71#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080072
Akinobu Mita19de85e2011-05-26 16:26:09 -070073#ifndef find_next_bit
Alexander van Heukelum64970b62008-03-11 16:17:19 +010074/*
75 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080076 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020077unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
78 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Yury Norovb78c5712020-01-30 22:16:43 -080080 return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020082EXPORT_SYMBOL(find_next_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070083#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080084
Akinobu Mita19de85e2011-05-26 16:26:09 -070085#ifndef find_next_zero_bit
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020086unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
87 unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080088{
Yury Norovb78c5712020-01-30 22:16:43 -080089 return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080090}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020091EXPORT_SYMBOL(find_next_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070092#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020093
Clement Courbet0ade34c2018-02-06 15:38:34 -080094#if !defined(find_next_and_bit)
95unsigned long find_next_and_bit(const unsigned long *addr1,
96 const unsigned long *addr2, unsigned long size,
97 unsigned long offset)
98{
Yury Norovb78c5712020-01-30 22:16:43 -080099 return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
Clement Courbet0ade34c2018-02-06 15:38:34 -0800100}
101EXPORT_SYMBOL(find_next_and_bit);
102#endif
103
Akinobu Mita19de85e2011-05-26 16:26:09 -0700104#ifndef find_first_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200105/*
106 * Find the first set bit in a memory region.
107 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200108unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200109{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700110 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200111
Yury Norov2c57a0e2015-04-16 12:43:13 -0700112 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
113 if (addr[idx])
114 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200115 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200116
Yury Norov2c57a0e2015-04-16 12:43:13 -0700117 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200118}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200119EXPORT_SYMBOL(find_first_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700120#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200121
Akinobu Mita19de85e2011-05-26 16:26:09 -0700122#ifndef find_first_zero_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200123/*
124 * Find the first cleared bit in a memory region.
125 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200126unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200127{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700128 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200129
Yury Norov2c57a0e2015-04-16 12:43:13 -0700130 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
131 if (addr[idx] != ~0UL)
132 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200133 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200134
Yury Norov2c57a0e2015-04-16 12:43:13 -0700135 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200136}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200137EXPORT_SYMBOL(find_first_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700138#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800139
Yury Norov8f6f19d2015-04-16 12:43:16 -0700140#ifndef find_last_bit
141unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
142{
143 if (size) {
144 unsigned long val = BITMAP_LAST_WORD_MASK(size);
145 unsigned long idx = (size-1) / BITS_PER_LONG;
146
147 do {
148 val &= addr[idx];
149 if (val)
150 return idx * BITS_PER_LONG + __fls(val);
151
152 val = ~0ul;
153 } while (idx--);
154 }
155 return size;
156}
157EXPORT_SYMBOL(find_last_bit);
158#endif
159
Akinobu Mita930ae742006-03-26 01:39:15 -0800160#ifdef __BIG_ENDIAN
161
Akinobu Mita19de85e2011-05-26 16:26:09 -0700162#ifndef find_next_zero_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700163unsigned long find_next_zero_bit_le(const void *addr, unsigned
Akinobu Mita930ae742006-03-26 01:39:15 -0800164 long size, unsigned long offset)
165{
Yury Norovb78c5712020-01-30 22:16:43 -0800166 return _find_next_bit(addr, NULL, size, offset, ~0UL, 1);
Akinobu Mita930ae742006-03-26 01:39:15 -0800167}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700168EXPORT_SYMBOL(find_next_zero_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700169#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800170
Akinobu Mita19de85e2011-05-26 16:26:09 -0700171#ifndef find_next_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700172unsigned long find_next_bit_le(const void *addr, unsigned
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500173 long size, unsigned long offset)
174{
Yury Norovb78c5712020-01-30 22:16:43 -0800175 return _find_next_bit(addr, NULL, size, offset, 0UL, 1);
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500176}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700177EXPORT_SYMBOL(find_next_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700178#endif
Akinobu Mita06649962011-03-23 16:41:59 -0700179
Akinobu Mita930ae742006-03-26 01:39:15 -0800180#endif /* __BIG_ENDIAN */
William Breathitt Gray169c4742019-12-04 16:50:57 -0800181
182unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
183 unsigned long size, unsigned long offset)
184{
185 offset = find_next_bit(addr, size, offset);
186 if (offset == size)
187 return size;
188
189 offset = round_down(offset, 8);
190 *clump = bitmap_get_value8(addr, offset);
191
192 return offset;
193}
194EXPORT_SYMBOL(find_next_clump8);