Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 2 | /* bit search implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | * |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 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) |
| 10 | * |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 11 | * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease |
| 12 | * size and improve performance, 2015. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/bitops.h> |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 16 | #include <linux/bitmap.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 17 | #include <linux/export.h> |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Andy Shevchenko | b296a6d | 2020-10-15 20:10:21 -0700 | [diff] [blame] | 19 | #include <linux/minmax.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 21 | #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ |
| 22 | !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \ |
| 23 | !defined(find_next_and_bit) |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 24 | /* |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 25 | * This is a common helper function for find_next_bit, find_next_zero_bit, and |
| 26 | * find_next_and_bit. The differences are: |
| 27 | * - The "invert" argument, which is XORed with each fetched word before |
| 28 | * searching it for one bits. |
| 29 | * - The optional "addr2", which is anded with "addr1" if present. |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 30 | */ |
Yury Norov | 7dfaa98f | 2020-01-30 22:16:47 -0800 | [diff] [blame] | 31 | static unsigned long _find_next_bit(const unsigned long *addr1, |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 32 | const unsigned long *addr2, unsigned long nbits, |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 33 | unsigned long start, unsigned long invert, unsigned long le) |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 34 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 35 | unsigned long tmp, mask; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 36 | |
Matthew Wilcox | e4afd2e | 2017-02-24 15:00:58 -0800 | [diff] [blame] | 37 | if (unlikely(start >= nbits)) |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 38 | return nbits; |
| 39 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 40 | tmp = addr1[start / BITS_PER_LONG]; |
| 41 | if (addr2) |
| 42 | tmp &= addr2[start / BITS_PER_LONG]; |
| 43 | tmp ^= invert; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 44 | |
| 45 | /* Handle 1st word. */ |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 46 | mask = BITMAP_FIRST_WORD_MASK(start); |
| 47 | if (le) |
| 48 | mask = swab(mask); |
| 49 | |
| 50 | tmp &= mask; |
| 51 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 52 | start = round_down(start, BITS_PER_LONG); |
| 53 | |
| 54 | while (!tmp) { |
| 55 | start += BITS_PER_LONG; |
| 56 | if (start >= nbits) |
| 57 | return nbits; |
| 58 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 59 | tmp = addr1[start / BITS_PER_LONG]; |
| 60 | if (addr2) |
| 61 | tmp &= addr2[start / BITS_PER_LONG]; |
| 62 | tmp ^= invert; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 65 | if (le) |
| 66 | tmp = swab(tmp); |
| 67 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 68 | return min(start + __ffs(tmp), nbits); |
| 69 | } |
| 70 | #endif |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 71 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 72 | #ifndef find_next_bit |
Alexander van Heukelum | 64970b6 | 2008-03-11 16:17:19 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Find the next set bit in a memory region. |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 75 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 76 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
| 77 | unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 79 | return _find_next_bit(addr, NULL, size, offset, 0UL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 81 | EXPORT_SYMBOL(find_next_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 82 | #endif |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 83 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 84 | #ifndef find_next_zero_bit |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 85 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
| 86 | unsigned long offset) |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 87 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 88 | return _find_next_bit(addr, NULL, size, offset, ~0UL, 0); |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 89 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 90 | EXPORT_SYMBOL(find_next_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 91 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 92 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 93 | #if !defined(find_next_and_bit) |
| 94 | unsigned long find_next_and_bit(const unsigned long *addr1, |
| 95 | const unsigned long *addr2, unsigned long size, |
| 96 | unsigned long offset) |
| 97 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 98 | return _find_next_bit(addr1, addr2, size, offset, 0UL, 0); |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL(find_next_and_bit); |
| 101 | #endif |
| 102 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 103 | #ifndef find_first_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 104 | /* |
| 105 | * Find the first set bit in a memory region. |
| 106 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 107 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 108 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 109 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 110 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 111 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 112 | if (addr[idx]) |
| 113 | return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 114 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 115 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 116 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 117 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 118 | EXPORT_SYMBOL(find_first_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 119 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 120 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 121 | #ifndef find_first_zero_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Find the first cleared bit in a memory region. |
| 124 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 125 | unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 126 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 127 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 128 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 129 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 130 | if (addr[idx] != ~0UL) |
| 131 | return min(idx * BITS_PER_LONG + ffz(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 132 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 133 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 134 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 135 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 136 | EXPORT_SYMBOL(find_first_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 137 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 138 | |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 139 | #ifndef find_last_bit |
| 140 | unsigned long find_last_bit(const unsigned long *addr, unsigned long size) |
| 141 | { |
| 142 | if (size) { |
| 143 | unsigned long val = BITMAP_LAST_WORD_MASK(size); |
| 144 | unsigned long idx = (size-1) / BITS_PER_LONG; |
| 145 | |
| 146 | do { |
| 147 | val &= addr[idx]; |
| 148 | if (val) |
| 149 | return idx * BITS_PER_LONG + __fls(val); |
| 150 | |
| 151 | val = ~0ul; |
| 152 | } while (idx--); |
| 153 | } |
| 154 | return size; |
| 155 | } |
| 156 | EXPORT_SYMBOL(find_last_bit); |
| 157 | #endif |
| 158 | |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 159 | #ifdef __BIG_ENDIAN |
| 160 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 161 | #ifndef find_next_zero_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 162 | unsigned long find_next_zero_bit_le(const void *addr, unsigned |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 163 | long size, unsigned long offset) |
| 164 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 165 | return _find_next_bit(addr, NULL, size, offset, ~0UL, 1); |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 166 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 167 | EXPORT_SYMBOL(find_next_zero_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 168 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 169 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 170 | #ifndef find_next_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 171 | unsigned long find_next_bit_le(const void *addr, unsigned |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 172 | long size, unsigned long offset) |
| 173 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 174 | return _find_next_bit(addr, NULL, size, offset, 0UL, 1); |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 175 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 176 | EXPORT_SYMBOL(find_next_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 177 | #endif |
Akinobu Mita | 0664996 | 2011-03-23 16:41:59 -0700 | [diff] [blame] | 178 | |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 179 | #endif /* __BIG_ENDIAN */ |
William Breathitt Gray | 169c474 | 2019-12-04 16:50:57 -0800 | [diff] [blame] | 180 | |
| 181 | unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr, |
| 182 | unsigned long size, unsigned long offset) |
| 183 | { |
| 184 | offset = find_next_bit(addr, size, offset); |
| 185 | if (offset == size) |
| 186 | return size; |
| 187 | |
| 188 | offset = round_down(offset, 8); |
| 189 | *clump = bitmap_get_value8(addr, offset); |
| 190 | |
| 191 | return offset; |
| 192 | } |
| 193 | EXPORT_SYMBOL(find_next_clump8); |