blob: 5c51eb45178a9da5d6073bc711b12b6d0c9392f5 [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>
Yury Norov2c57a0e2015-04-16 12:43:13 -070018#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Clement Courbet0ade34c2018-02-06 15:38:34 -080020#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
21 !defined(find_next_and_bit)
Yury Norov2c57a0e2015-04-16 12:43:13 -070022
23/*
Clement Courbet0ade34c2018-02-06 15:38:34 -080024 * This is a common helper function for find_next_bit, find_next_zero_bit, and
25 * find_next_and_bit. The differences are:
26 * - The "invert" argument, which is XORed with each fetched word before
27 * searching it for one bits.
28 * - The optional "addr2", which is anded with "addr1" if present.
Yury Norov2c57a0e2015-04-16 12:43:13 -070029 */
Clement Courbet0ade34c2018-02-06 15:38:34 -080030static inline unsigned long _find_next_bit(const unsigned long *addr1,
31 const unsigned long *addr2, unsigned long nbits,
32 unsigned long start, unsigned long invert)
Yury Norov2c57a0e2015-04-16 12:43:13 -070033{
34 unsigned long tmp;
35
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080036 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -070037 return nbits;
38
Clement Courbet0ade34c2018-02-06 15:38:34 -080039 tmp = addr1[start / BITS_PER_LONG];
40 if (addr2)
41 tmp &= addr2[start / BITS_PER_LONG];
42 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070043
44 /* Handle 1st word. */
45 tmp &= BITMAP_FIRST_WORD_MASK(start);
46 start = round_down(start, BITS_PER_LONG);
47
48 while (!tmp) {
49 start += BITS_PER_LONG;
50 if (start >= nbits)
51 return nbits;
52
Clement Courbet0ade34c2018-02-06 15:38:34 -080053 tmp = addr1[start / BITS_PER_LONG];
54 if (addr2)
55 tmp &= addr2[start / BITS_PER_LONG];
56 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070057 }
58
59 return min(start + __ffs(tmp), nbits);
60}
61#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080062
Akinobu Mita19de85e2011-05-26 16:26:09 -070063#ifndef find_next_bit
Alexander van Heukelum64970b62008-03-11 16:17:19 +010064/*
65 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080066 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020067unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
68 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Clement Courbet0ade34c2018-02-06 15:38:34 -080070 return _find_next_bit(addr, NULL, size, offset, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020072EXPORT_SYMBOL(find_next_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070073#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080074
Akinobu Mita19de85e2011-05-26 16:26:09 -070075#ifndef find_next_zero_bit
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020076unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
77 unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080078{
Clement Courbet0ade34c2018-02-06 15:38:34 -080079 return _find_next_bit(addr, NULL, size, offset, ~0UL);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080080}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020081EXPORT_SYMBOL(find_next_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070082#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020083
Clement Courbet0ade34c2018-02-06 15:38:34 -080084#if !defined(find_next_and_bit)
85unsigned long find_next_and_bit(const unsigned long *addr1,
86 const unsigned long *addr2, unsigned long size,
87 unsigned long offset)
88{
89 return _find_next_bit(addr1, addr2, size, offset, 0UL);
90}
91EXPORT_SYMBOL(find_next_and_bit);
92#endif
93
Akinobu Mita19de85e2011-05-26 16:26:09 -070094#ifndef find_first_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020095/*
96 * Find the first set bit in a memory region.
97 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020098unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020099{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700100 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200101
Yury Norov2c57a0e2015-04-16 12:43:13 -0700102 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
103 if (addr[idx])
104 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200105 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200106
Yury Norov2c57a0e2015-04-16 12:43:13 -0700107 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200108}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200109EXPORT_SYMBOL(find_first_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700110#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200111
Akinobu Mita19de85e2011-05-26 16:26:09 -0700112#ifndef find_first_zero_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200113/*
114 * Find the first cleared bit in a memory region.
115 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200116unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200117{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700118 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200119
Yury Norov2c57a0e2015-04-16 12:43:13 -0700120 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
121 if (addr[idx] != ~0UL)
122 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200123 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200124
Yury Norov2c57a0e2015-04-16 12:43:13 -0700125 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200126}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200127EXPORT_SYMBOL(find_first_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700128#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800129
Yury Norov8f6f19d2015-04-16 12:43:16 -0700130#ifndef find_last_bit
131unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
132{
133 if (size) {
134 unsigned long val = BITMAP_LAST_WORD_MASK(size);
135 unsigned long idx = (size-1) / BITS_PER_LONG;
136
137 do {
138 val &= addr[idx];
139 if (val)
140 return idx * BITS_PER_LONG + __fls(val);
141
142 val = ~0ul;
143 } while (idx--);
144 }
145 return size;
146}
147EXPORT_SYMBOL(find_last_bit);
148#endif
149
Akinobu Mita930ae742006-03-26 01:39:15 -0800150#ifdef __BIG_ENDIAN
151
152/* include/linux/byteorder does not support "unsigned long" type */
Akinobu Mita930ae742006-03-26 01:39:15 -0800153static inline unsigned long ext2_swab(const unsigned long y)
154{
155#if BITS_PER_LONG == 64
156 return (unsigned long) __swab64((u64) y);
157#elif BITS_PER_LONG == 32
158 return (unsigned long) __swab32((u32) y);
159#else
160#error BITS_PER_LONG not defined
161#endif
162}
163
Yury Norov2c57a0e2015-04-16 12:43:13 -0700164#if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le)
Clement Courbet0ade34c2018-02-06 15:38:34 -0800165static inline unsigned long _find_next_bit_le(const unsigned long *addr1,
166 const unsigned long *addr2, unsigned long nbits,
167 unsigned long start, unsigned long invert)
Yury Norov2c57a0e2015-04-16 12:43:13 -0700168{
169 unsigned long tmp;
170
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -0800171 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -0700172 return nbits;
173
Clement Courbet0ade34c2018-02-06 15:38:34 -0800174 tmp = addr1[start / BITS_PER_LONG];
175 if (addr2)
176 tmp &= addr2[start / BITS_PER_LONG];
177 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -0700178
179 /* Handle 1st word. */
180 tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start));
181 start = round_down(start, BITS_PER_LONG);
182
183 while (!tmp) {
184 start += BITS_PER_LONG;
185 if (start >= nbits)
186 return nbits;
187
Clement Courbet0ade34c2018-02-06 15:38:34 -0800188 tmp = addr1[start / BITS_PER_LONG];
189 if (addr2)
190 tmp &= addr2[start / BITS_PER_LONG];
191 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -0700192 }
193
194 return min(start + __ffs(ext2_swab(tmp)), nbits);
195}
196#endif
197
Akinobu Mita19de85e2011-05-26 16:26:09 -0700198#ifndef find_next_zero_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700199unsigned long find_next_zero_bit_le(const void *addr, unsigned
Akinobu Mita930ae742006-03-26 01:39:15 -0800200 long size, unsigned long offset)
201{
Clement Courbet0ade34c2018-02-06 15:38:34 -0800202 return _find_next_bit_le(addr, NULL, size, offset, ~0UL);
Akinobu Mita930ae742006-03-26 01:39:15 -0800203}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700204EXPORT_SYMBOL(find_next_zero_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700205#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800206
Akinobu Mita19de85e2011-05-26 16:26:09 -0700207#ifndef find_next_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700208unsigned long find_next_bit_le(const void *addr, unsigned
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500209 long size, unsigned long offset)
210{
Clement Courbet0ade34c2018-02-06 15:38:34 -0800211 return _find_next_bit_le(addr, NULL, size, offset, 0UL);
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500212}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700213EXPORT_SYMBOL(find_next_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700214#endif
Akinobu Mita06649962011-03-23 16:41:59 -0700215
Akinobu Mita930ae742006-03-26 01:39:15 -0800216#endif /* __BIG_ENDIAN */