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> |
Andy Shevchenko | aa6159a | 2020-12-15 20:42:48 -0800 | [diff] [blame] | 18 | #include <linux/math.h> |
Andy Shevchenko | b296a6d | 2020-10-15 20:10:21 -0700 | [diff] [blame] | 19 | #include <linux/minmax.h> |
Andy Shevchenko | aa6159a | 2020-12-15 20:42:48 -0800 | [diff] [blame] | 20 | #include <linux/swab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 22 | #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 Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 25 | /* |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 26 | * 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 Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 31 | */ |
Yury Norov | 5c88af5 | 2021-05-06 18:03:03 -0700 | [diff] [blame] | 32 | unsigned long _find_next_bit(const unsigned long *addr1, |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 33 | const unsigned long *addr2, unsigned long nbits, |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 34 | unsigned long start, unsigned long invert, unsigned long le) |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 35 | { |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 36 | unsigned long tmp, mask; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 37 | |
Matthew Wilcox | e4afd2e | 2017-02-24 15:00:58 -0800 | [diff] [blame] | 38 | if (unlikely(start >= nbits)) |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 39 | return nbits; |
| 40 | |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 41 | tmp = addr1[start / BITS_PER_LONG]; |
| 42 | if (addr2) |
| 43 | tmp &= addr2[start / BITS_PER_LONG]; |
| 44 | tmp ^= invert; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 45 | |
| 46 | /* Handle 1st word. */ |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 47 | mask = BITMAP_FIRST_WORD_MASK(start); |
| 48 | if (le) |
| 49 | mask = swab(mask); |
| 50 | |
| 51 | tmp &= mask; |
| 52 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 53 | 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 Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 60 | tmp = addr1[start / BITS_PER_LONG]; |
| 61 | if (addr2) |
| 62 | tmp &= addr2[start / BITS_PER_LONG]; |
| 63 | tmp ^= invert; |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yury Norov | b78c571 | 2020-01-30 22:16:43 -0800 | [diff] [blame] | 66 | if (le) |
| 67 | tmp = swab(tmp); |
| 68 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 69 | return min(start + __ffs(tmp), nbits); |
| 70 | } |
Yury Norov | 5c88af5 | 2021-05-06 18:03:03 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(_find_next_bit); |
Clement Courbet | 0ade34c | 2018-02-06 15:38:34 -0800 | [diff] [blame] | 72 | #endif |
| 73 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 74 | #ifndef find_first_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Find the first set bit in a memory region. |
| 77 | */ |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 78 | 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] | 79 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 80 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 81 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 82 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 83 | if (addr[idx]) |
| 84 | return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 85 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 86 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 87 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 88 | } |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 89 | EXPORT_SYMBOL(_find_first_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 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 92 | #ifndef find_first_zero_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Find the first cleared bit in a memory region. |
| 95 | */ |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 96 | 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] | 97 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 98 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 99 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 100 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 101 | if (addr[idx] != ~0UL) |
| 102 | return min(idx * BITS_PER_LONG + ffz(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 103 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 104 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame] | 105 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 106 | } |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 107 | EXPORT_SYMBOL(_find_first_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 108 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 109 | |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 110 | #ifndef find_last_bit |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 111 | unsigned long _find_last_bit(const unsigned long *addr, unsigned long size) |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 112 | { |
| 113 | if (size) { |
| 114 | unsigned long val = BITMAP_LAST_WORD_MASK(size); |
| 115 | unsigned long idx = (size-1) / BITS_PER_LONG; |
| 116 | |
| 117 | do { |
| 118 | val &= addr[idx]; |
| 119 | if (val) |
| 120 | return idx * BITS_PER_LONG + __fls(val); |
| 121 | |
| 122 | val = ~0ul; |
| 123 | } while (idx--); |
| 124 | } |
| 125 | return size; |
| 126 | } |
Yury Norov | 2cc7b6a | 2021-05-06 18:03:14 -0700 | [diff] [blame] | 127 | EXPORT_SYMBOL(_find_last_bit); |
Yury Norov | 8f6f19d | 2015-04-16 12:43:16 -0700 | [diff] [blame] | 128 | #endif |
| 129 | |
William Breathitt Gray | 169c474 | 2019-12-04 16:50:57 -0800 | [diff] [blame] | 130 | unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr, |
| 131 | unsigned long size, unsigned long offset) |
| 132 | { |
| 133 | offset = find_next_bit(addr, size, offset); |
| 134 | if (offset == size) |
| 135 | return size; |
| 136 | |
| 137 | offset = round_down(offset, 8); |
| 138 | *clump = bitmap_get_value8(addr, offset); |
| 139 | |
| 140 | return offset; |
| 141 | } |
| 142 | EXPORT_SYMBOL(find_next_clump8); |