Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* find_next_bit.c: fallback find next bit implementation |
| 2 | * |
| 3 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 6 | * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease |
| 7 | * size and improve performance, 2015. |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/bitops.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 16 | #include <linux/export.h> |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 19 | #if !defined(find_next_bit) || !defined(find_next_zero_bit) |
| 20 | |
| 21 | /* |
| 22 | * This is a common helper function for find_next_bit and |
| 23 | * find_next_zero_bit. The difference is the "invert" argument, which |
| 24 | * is XORed with each fetched word before searching it for one bits. |
| 25 | */ |
| 26 | static unsigned long _find_next_bit(const unsigned long *addr, |
| 27 | unsigned long nbits, unsigned long start, unsigned long invert) |
| 28 | { |
| 29 | unsigned long tmp; |
| 30 | |
| 31 | if (!nbits || start >= nbits) |
| 32 | return nbits; |
| 33 | |
| 34 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
| 35 | |
| 36 | /* Handle 1st word. */ |
| 37 | tmp &= BITMAP_FIRST_WORD_MASK(start); |
| 38 | start = round_down(start, BITS_PER_LONG); |
| 39 | |
| 40 | while (!tmp) { |
| 41 | start += BITS_PER_LONG; |
| 42 | if (start >= nbits) |
| 43 | return nbits; |
| 44 | |
| 45 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
| 46 | } |
| 47 | |
| 48 | return min(start + __ffs(tmp), nbits); |
| 49 | } |
| 50 | #endif |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 51 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 52 | #ifndef find_next_bit |
Alexander van Heukelum | 64970b6 | 2008-03-11 16:17:19 +0100 | [diff] [blame] | 53 | /* |
| 54 | * Find the next set bit in a memory region. |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 55 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 56 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
| 57 | unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 59 | return _find_next_bit(addr, size, offset, 0UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 61 | EXPORT_SYMBOL(find_next_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 62 | #endif |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 63 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 64 | #ifndef find_next_zero_bit |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 65 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
| 66 | unsigned long offset) |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 67 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 68 | return _find_next_bit(addr, size, offset, ~0UL); |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 69 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 70 | EXPORT_SYMBOL(find_next_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 71 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 72 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 73 | #ifndef find_first_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 74 | /* |
| 75 | * Find the first set bit in a memory region. |
| 76 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 77 | 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] | 78 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 79 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 80 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 81 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 82 | if (addr[idx]) |
| 83 | return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 84 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 85 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 86 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 87 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 88 | EXPORT_SYMBOL(find_first_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 89 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 90 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 91 | #ifndef find_first_zero_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 92 | /* |
| 93 | * Find the first cleared bit in a memory region. |
| 94 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 95 | 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] | 96 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 97 | unsigned long idx; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 98 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 99 | for (idx = 0; idx * BITS_PER_LONG < size; idx++) { |
| 100 | if (addr[idx] != ~0UL) |
| 101 | return min(idx * BITS_PER_LONG + ffz(addr[idx]), size); |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 102 | } |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 103 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 104 | return size; |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 105 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 106 | EXPORT_SYMBOL(find_first_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 107 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 108 | |
| 109 | #ifdef __BIG_ENDIAN |
| 110 | |
| 111 | /* include/linux/byteorder does not support "unsigned long" type */ |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 112 | static inline unsigned long ext2_swab(const unsigned long y) |
| 113 | { |
| 114 | #if BITS_PER_LONG == 64 |
| 115 | return (unsigned long) __swab64((u64) y); |
| 116 | #elif BITS_PER_LONG == 32 |
| 117 | return (unsigned long) __swab32((u32) y); |
| 118 | #else |
| 119 | #error BITS_PER_LONG not defined |
| 120 | #endif |
| 121 | } |
| 122 | |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 123 | #if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) |
| 124 | static unsigned long _find_next_bit_le(const unsigned long *addr, |
| 125 | unsigned long nbits, unsigned long start, unsigned long invert) |
| 126 | { |
| 127 | unsigned long tmp; |
| 128 | |
| 129 | if (!nbits || start >= nbits) |
| 130 | return nbits; |
| 131 | |
| 132 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
| 133 | |
| 134 | /* Handle 1st word. */ |
| 135 | tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start)); |
| 136 | start = round_down(start, BITS_PER_LONG); |
| 137 | |
| 138 | while (!tmp) { |
| 139 | start += BITS_PER_LONG; |
| 140 | if (start >= nbits) |
| 141 | return nbits; |
| 142 | |
| 143 | tmp = addr[start / BITS_PER_LONG] ^ invert; |
| 144 | } |
| 145 | |
| 146 | return min(start + __ffs(ext2_swab(tmp)), nbits); |
| 147 | } |
| 148 | #endif |
| 149 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 150 | #ifndef find_next_zero_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 151 | unsigned long find_next_zero_bit_le(const void *addr, unsigned |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 152 | long size, unsigned long offset) |
| 153 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 154 | return _find_next_bit_le(addr, size, offset, ~0UL); |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 155 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL(find_next_zero_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 157 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 158 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 159 | #ifndef find_next_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 160 | unsigned long find_next_bit_le(const void *addr, unsigned |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 161 | long size, unsigned long offset) |
| 162 | { |
Yury Norov | 2c57a0e | 2015-04-16 12:43:13 -0700 | [diff] [blame^] | 163 | return _find_next_bit_le(addr, size, offset, 0UL); |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 164 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 165 | EXPORT_SYMBOL(find_next_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame] | 166 | #endif |
Akinobu Mita | 0664996 | 2011-03-23 16:41:59 -0700 | [diff] [blame] | 167 | |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 168 | #endif /* __BIG_ENDIAN */ |