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 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/bitops.h> |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 14 | #include <asm/types.h> |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 15 | #include <asm/byteorder.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 17 | #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) |
| 18 | |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_GENERIC_FIND_NEXT_BIT |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 20 | #ifndef find_next_bit |
Alexander van Heukelum | 64970b6 | 2008-03-11 16:17:19 +0100 | [diff] [blame] | 21 | /* |
| 22 | * Find the next set bit in a memory region. |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 23 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 24 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
| 25 | unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 27 | const unsigned long *p = addr + BITOP_WORD(offset); |
| 28 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | unsigned long tmp; |
| 30 | |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 31 | if (offset >= size) |
| 32 | return size; |
| 33 | size -= result; |
| 34 | offset %= BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | if (offset) { |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 36 | tmp = *(p++); |
| 37 | tmp &= (~0UL << offset); |
| 38 | if (size < BITS_PER_LONG) |
| 39 | goto found_first; |
| 40 | if (tmp) |
| 41 | goto found_middle; |
| 42 | size -= BITS_PER_LONG; |
| 43 | result += BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 45 | while (size & ~(BITS_PER_LONG-1)) { |
| 46 | if ((tmp = *(p++))) |
| 47 | goto found_middle; |
| 48 | result += BITS_PER_LONG; |
| 49 | size -= BITS_PER_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 51 | if (!size) |
| 52 | return result; |
| 53 | tmp = *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 55 | found_first: |
| 56 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
| 57 | if (tmp == 0UL) /* Are any bits set? */ |
| 58 | return result + size; /* Nope. */ |
| 59 | found_middle: |
| 60 | return result + __ffs(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 62 | EXPORT_SYMBOL(find_next_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 63 | #endif |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 64 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 65 | #ifndef find_next_zero_bit |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 66 | /* |
| 67 | * This implementation of find_{first,next}_zero_bit was stolen from |
| 68 | * Linus' asm-alpha/bitops.h. |
| 69 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 70 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
| 71 | unsigned long offset) |
Akinobu Mita | c7f612c | 2006-03-26 01:39:11 -0800 | [diff] [blame] | 72 | { |
| 73 | const unsigned long *p = addr + BITOP_WORD(offset); |
| 74 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
| 75 | unsigned long tmp; |
| 76 | |
| 77 | if (offset >= size) |
| 78 | return size; |
| 79 | size -= result; |
| 80 | offset %= BITS_PER_LONG; |
| 81 | if (offset) { |
| 82 | tmp = *(p++); |
| 83 | tmp |= ~0UL >> (BITS_PER_LONG - offset); |
| 84 | if (size < BITS_PER_LONG) |
| 85 | goto found_first; |
| 86 | if (~tmp) |
| 87 | goto found_middle; |
| 88 | size -= BITS_PER_LONG; |
| 89 | result += BITS_PER_LONG; |
| 90 | } |
| 91 | while (size & ~(BITS_PER_LONG-1)) { |
| 92 | if (~(tmp = *(p++))) |
| 93 | goto found_middle; |
| 94 | result += BITS_PER_LONG; |
| 95 | size -= BITS_PER_LONG; |
| 96 | } |
| 97 | if (!size) |
| 98 | return result; |
| 99 | tmp = *p; |
| 100 | |
| 101 | found_first: |
| 102 | tmp |= ~0UL << size; |
| 103 | if (tmp == ~0UL) /* Are any bits zero? */ |
| 104 | return result + size; /* Nope. */ |
| 105 | found_middle: |
| 106 | return result + ffz(tmp); |
| 107 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 108 | EXPORT_SYMBOL(find_next_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 109 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 110 | #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */ |
| 111 | |
| 112 | #ifdef CONFIG_GENERIC_FIND_FIRST_BIT |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 113 | #ifndef find_first_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Find the first set bit in a memory region. |
| 116 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | const unsigned long *p = addr; |
| 120 | unsigned long result = 0; |
| 121 | unsigned long tmp; |
| 122 | |
| 123 | while (size & ~(BITS_PER_LONG-1)) { |
| 124 | if ((tmp = *(p++))) |
| 125 | goto found; |
| 126 | result += BITS_PER_LONG; |
| 127 | size -= BITS_PER_LONG; |
| 128 | } |
| 129 | if (!size) |
| 130 | return result; |
| 131 | |
| 132 | tmp = (*p) & (~0UL >> (BITS_PER_LONG - size)); |
| 133 | if (tmp == 0UL) /* Are any bits set? */ |
| 134 | return result + size; /* Nope. */ |
| 135 | found: |
| 136 | return result + __ffs(tmp); |
| 137 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 138 | EXPORT_SYMBOL(find_first_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 139 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 140 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 141 | #ifndef find_first_zero_bit |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Find the first cleared bit in a memory region. |
| 144 | */ |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | const unsigned long *p = addr; |
| 148 | unsigned long result = 0; |
| 149 | unsigned long tmp; |
| 150 | |
| 151 | while (size & ~(BITS_PER_LONG-1)) { |
| 152 | if (~(tmp = *(p++))) |
| 153 | goto found; |
| 154 | result += BITS_PER_LONG; |
| 155 | size -= BITS_PER_LONG; |
| 156 | } |
| 157 | if (!size) |
| 158 | return result; |
| 159 | |
| 160 | tmp = (*p) | (~0UL << size); |
| 161 | if (tmp == ~0UL) /* Are any bits zero? */ |
| 162 | return result + size; /* Nope. */ |
| 163 | found: |
| 164 | return result + ffz(tmp); |
| 165 | } |
Thomas Gleixner | fee4b19 | 2008-04-29 12:01:02 +0200 | [diff] [blame] | 166 | EXPORT_SYMBOL(find_first_zero_bit); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 167 | #endif |
Alexander van Heukelum | 77b9bd9 | 2008-04-01 11:46:19 +0200 | [diff] [blame] | 168 | #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 169 | |
| 170 | #ifdef __BIG_ENDIAN |
Akinobu Mita | 0664996 | 2011-03-23 16:41:59 -0700 | [diff] [blame] | 171 | #ifdef CONFIG_GENERIC_FIND_BIT_LE |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 172 | |
| 173 | /* include/linux/byteorder does not support "unsigned long" type */ |
| 174 | static inline unsigned long ext2_swabp(const unsigned long * x) |
| 175 | { |
| 176 | #if BITS_PER_LONG == 64 |
| 177 | return (unsigned long) __swab64p((u64 *) x); |
| 178 | #elif BITS_PER_LONG == 32 |
| 179 | return (unsigned long) __swab32p((u32 *) x); |
| 180 | #else |
| 181 | #error BITS_PER_LONG not defined |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | /* include/linux/byteorder doesn't support "unsigned long" type */ |
| 186 | static inline unsigned long ext2_swab(const unsigned long y) |
| 187 | { |
| 188 | #if BITS_PER_LONG == 64 |
| 189 | return (unsigned long) __swab64((u64) y); |
| 190 | #elif BITS_PER_LONG == 32 |
| 191 | return (unsigned long) __swab32((u32) y); |
| 192 | #else |
| 193 | #error BITS_PER_LONG not defined |
| 194 | #endif |
| 195 | } |
| 196 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 197 | #ifndef find_next_zero_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 198 | unsigned long find_next_zero_bit_le(const void *addr, unsigned |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 199 | long size, unsigned long offset) |
| 200 | { |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 201 | const unsigned long *p = addr; |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 202 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
| 203 | unsigned long tmp; |
| 204 | |
| 205 | if (offset >= size) |
| 206 | return size; |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 207 | p += BITOP_WORD(offset); |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 208 | size -= result; |
| 209 | offset &= (BITS_PER_LONG - 1UL); |
| 210 | if (offset) { |
| 211 | tmp = ext2_swabp(p++); |
| 212 | tmp |= (~0UL >> (BITS_PER_LONG - offset)); |
| 213 | if (size < BITS_PER_LONG) |
| 214 | goto found_first; |
| 215 | if (~tmp) |
| 216 | goto found_middle; |
| 217 | size -= BITS_PER_LONG; |
| 218 | result += BITS_PER_LONG; |
| 219 | } |
| 220 | |
| 221 | while (size & ~(BITS_PER_LONG - 1)) { |
| 222 | if (~(tmp = *(p++))) |
| 223 | goto found_middle_swap; |
| 224 | result += BITS_PER_LONG; |
| 225 | size -= BITS_PER_LONG; |
| 226 | } |
| 227 | if (!size) |
| 228 | return result; |
| 229 | tmp = ext2_swabp(p); |
| 230 | found_first: |
| 231 | tmp |= ~0UL << size; |
| 232 | if (tmp == ~0UL) /* Are any bits zero? */ |
| 233 | return result + size; /* Nope. Skip ffz */ |
| 234 | found_middle: |
| 235 | return result + ffz(tmp); |
| 236 | |
| 237 | found_middle_swap: |
| 238 | return result + ffz(ext2_swab(tmp)); |
| 239 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 240 | EXPORT_SYMBOL(find_next_zero_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 241 | #endif |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 242 | |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 243 | #ifndef find_next_bit_le |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 244 | unsigned long find_next_bit_le(const void *addr, unsigned |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 245 | long size, unsigned long offset) |
| 246 | { |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 247 | const unsigned long *p = addr; |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 248 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
| 249 | unsigned long tmp; |
| 250 | |
| 251 | if (offset >= size) |
| 252 | return size; |
Akinobu Mita | a56560b | 2011-03-23 16:41:50 -0700 | [diff] [blame] | 253 | p += BITOP_WORD(offset); |
Aneesh Kumar K.V | aa02ad6 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 254 | size -= result; |
| 255 | offset &= (BITS_PER_LONG - 1UL); |
| 256 | if (offset) { |
| 257 | tmp = ext2_swabp(p++); |
| 258 | tmp &= (~0UL << offset); |
| 259 | if (size < BITS_PER_LONG) |
| 260 | goto found_first; |
| 261 | if (tmp) |
| 262 | goto found_middle; |
| 263 | size -= BITS_PER_LONG; |
| 264 | result += BITS_PER_LONG; |
| 265 | } |
| 266 | |
| 267 | while (size & ~(BITS_PER_LONG - 1)) { |
| 268 | tmp = *(p++); |
| 269 | if (tmp) |
| 270 | goto found_middle_swap; |
| 271 | result += BITS_PER_LONG; |
| 272 | size -= BITS_PER_LONG; |
| 273 | } |
| 274 | if (!size) |
| 275 | return result; |
| 276 | tmp = ext2_swabp(p); |
| 277 | found_first: |
| 278 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
| 279 | if (tmp == 0UL) /* Are any bits set? */ |
| 280 | return result + size; /* Nope. */ |
| 281 | found_middle: |
| 282 | return result + __ffs(tmp); |
| 283 | |
| 284 | found_middle_swap: |
| 285 | return result + __ffs(ext2_swab(tmp)); |
| 286 | } |
Akinobu Mita | c4945b9 | 2011-03-23 16:41:47 -0700 | [diff] [blame] | 287 | EXPORT_SYMBOL(find_next_bit_le); |
Akinobu Mita | 19de85e | 2011-05-26 16:26:09 -0700 | [diff] [blame^] | 288 | #endif |
Akinobu Mita | 0664996 | 2011-03-23 16:41:59 -0700 | [diff] [blame] | 289 | |
| 290 | #endif /* CONFIG_GENERIC_FIND_BIT_LE */ |
Akinobu Mita | 930ae74 | 2006-03-26 01:39:15 -0800 | [diff] [blame] | 291 | #endif /* __BIG_ENDIAN */ |