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