| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef __LINUX_FIND_H_ |
| 3 | #define __LINUX_FIND_H_ |
| 4 | |
| 5 | #ifndef __LINUX_BITMAP_H |
| 6 | #error only <linux/bitmap.h> can be included directly |
| 7 | #endif |
| 8 | |
| 9 | #include <linux/bitops.h> |
| 10 | |
| 11 | unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits, |
| 12 | unsigned long start); |
| 13 | unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 14 | unsigned long nbits, unsigned long start); |
| 15 | unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 16 | unsigned long nbits, unsigned long start); |
| 17 | unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 18 | unsigned long nbits, unsigned long start); |
| 19 | unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, |
| 20 | unsigned long start); |
| 21 | extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); |
| 22 | unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n); |
| 23 | unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 24 | unsigned long size, unsigned long n); |
| 25 | unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 26 | unsigned long size, unsigned long n); |
| 27 | unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 28 | const unsigned long *addr3, unsigned long size, |
| 29 | unsigned long n); |
| 30 | extern unsigned long _find_first_and_bit(const unsigned long *addr1, |
| 31 | const unsigned long *addr2, unsigned long size); |
| 32 | unsigned long _find_first_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 33 | unsigned long size); |
| 34 | unsigned long _find_first_and_and_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 35 | const unsigned long *addr3, unsigned long size); |
| 36 | extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size); |
| 37 | extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size); |
| 38 | |
| 39 | #ifdef __BIG_ENDIAN |
| 40 | unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size); |
| 41 | unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned |
| 42 | long size, unsigned long offset); |
| 43 | unsigned long _find_next_bit_le(const unsigned long *addr, unsigned |
| 44 | long size, unsigned long offset); |
| 45 | #endif |
| 46 | |
| 47 | #ifndef find_next_bit |
| 48 | /** |
| 49 | * find_next_bit - find the next set bit in a memory region |
| 50 | * @addr: The address to base the search on |
| 51 | * @size: The bitmap size in bits |
| 52 | * @offset: The bitnumber to start searching at |
| 53 | * |
| 54 | * Returns the bit number for the next set bit |
| 55 | * If no bits are set, returns @size. |
| 56 | */ |
| 57 | static __always_inline |
| 58 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
| 59 | unsigned long offset) |
| 60 | { |
| 61 | if (small_const_nbits(size)) { |
| 62 | unsigned long val; |
| 63 | |
| 64 | if (unlikely(offset >= size)) |
| 65 | return size; |
| 66 | |
| 67 | val = *addr & GENMASK(size - 1, offset); |
| 68 | return val ? __ffs(val) : size; |
| 69 | } |
| 70 | |
| 71 | return _find_next_bit(addr1: addr, nbits: size, start: offset); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #ifndef find_next_and_bit |
| 76 | /** |
| 77 | * find_next_and_bit - find the next set bit in both memory regions |
| 78 | * @addr1: The first address to base the search on |
| 79 | * @addr2: The second address to base the search on |
| 80 | * @size: The bitmap size in bits |
| 81 | * @offset: The bitnumber to start searching at |
| 82 | * |
| 83 | * Returns the bit number for the next set bit |
| 84 | * If no bits are set, returns @size. |
| 85 | */ |
| 86 | static __always_inline |
| 87 | unsigned long find_next_and_bit(const unsigned long *addr1, |
| 88 | const unsigned long *addr2, unsigned long size, |
| 89 | unsigned long offset) |
| 90 | { |
| 91 | if (small_const_nbits(size)) { |
| 92 | unsigned long val; |
| 93 | |
| 94 | if (unlikely(offset >= size)) |
| 95 | return size; |
| 96 | |
| 97 | val = *addr1 & *addr2 & GENMASK(size - 1, offset); |
| 98 | return val ? __ffs(val) : size; |
| 99 | } |
| 100 | |
| 101 | return _find_next_and_bit(addr1, addr2, nbits: size, start: offset); |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | #ifndef find_next_andnot_bit |
| 106 | /** |
| 107 | * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits |
| 108 | * in *addr2 |
| 109 | * @addr1: The first address to base the search on |
| 110 | * @addr2: The second address to base the search on |
| 111 | * @size: The bitmap size in bits |
| 112 | * @offset: The bitnumber to start searching at |
| 113 | * |
| 114 | * Returns the bit number for the next set bit |
| 115 | * If no bits are set, returns @size. |
| 116 | */ |
| 117 | static __always_inline |
| 118 | unsigned long find_next_andnot_bit(const unsigned long *addr1, |
| 119 | const unsigned long *addr2, unsigned long size, |
| 120 | unsigned long offset) |
| 121 | { |
| 122 | if (small_const_nbits(size)) { |
| 123 | unsigned long val; |
| 124 | |
| 125 | if (unlikely(offset >= size)) |
| 126 | return size; |
| 127 | |
| 128 | val = *addr1 & ~*addr2 & GENMASK(size - 1, offset); |
| 129 | return val ? __ffs(val) : size; |
| 130 | } |
| 131 | |
| 132 | return _find_next_andnot_bit(addr1, addr2, nbits: size, start: offset); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #ifndef find_next_or_bit |
| 137 | /** |
| 138 | * find_next_or_bit - find the next set bit in either memory regions |
| 139 | * @addr1: The first address to base the search on |
| 140 | * @addr2: The second address to base the search on |
| 141 | * @size: The bitmap size in bits |
| 142 | * @offset: The bitnumber to start searching at |
| 143 | * |
| 144 | * Returns the bit number for the next set bit |
| 145 | * If no bits are set, returns @size. |
| 146 | */ |
| 147 | static __always_inline |
| 148 | unsigned long find_next_or_bit(const unsigned long *addr1, |
| 149 | const unsigned long *addr2, unsigned long size, |
| 150 | unsigned long offset) |
| 151 | { |
| 152 | if (small_const_nbits(size)) { |
| 153 | unsigned long val; |
| 154 | |
| 155 | if (unlikely(offset >= size)) |
| 156 | return size; |
| 157 | |
| 158 | val = (*addr1 | *addr2) & GENMASK(size - 1, offset); |
| 159 | return val ? __ffs(val) : size; |
| 160 | } |
| 161 | |
| 162 | return _find_next_or_bit(addr1, addr2, nbits: size, start: offset); |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | #ifndef find_next_zero_bit |
| 167 | /** |
| 168 | * find_next_zero_bit - find the next cleared bit in a memory region |
| 169 | * @addr: The address to base the search on |
| 170 | * @size: The bitmap size in bits |
| 171 | * @offset: The bitnumber to start searching at |
| 172 | * |
| 173 | * Returns the bit number of the next zero bit |
| 174 | * If no bits are zero, returns @size. |
| 175 | */ |
| 176 | static __always_inline |
| 177 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
| 178 | unsigned long offset) |
| 179 | { |
| 180 | if (small_const_nbits(size)) { |
| 181 | unsigned long val; |
| 182 | |
| 183 | if (unlikely(offset >= size)) |
| 184 | return size; |
| 185 | |
| 186 | val = *addr | ~GENMASK(size - 1, offset); |
| 187 | return val == ~0UL ? size : ffz(val); |
| 188 | } |
| 189 | |
| 190 | return _find_next_zero_bit(addr, nbits: size, start: offset); |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #ifndef find_first_bit |
| 195 | /** |
| 196 | * find_first_bit - find the first set bit in a memory region |
| 197 | * @addr: The address to start the search at |
| 198 | * @size: The maximum number of bits to search |
| 199 | * |
| 200 | * Returns the bit number of the first set bit. |
| 201 | * If no bits are set, returns @size. |
| 202 | */ |
| 203 | static __always_inline |
| 204 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
| 205 | { |
| 206 | if (small_const_nbits(size)) { |
| 207 | unsigned long val = *addr & GENMASK(size - 1, 0); |
| 208 | |
| 209 | return val ? __ffs(val) : size; |
| 210 | } |
| 211 | |
| 212 | return _find_first_bit(addr, size); |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | /** |
| 217 | * find_nth_bit - find N'th set bit in a memory region |
| 218 | * @addr: The address to start the search at |
| 219 | * @size: The maximum number of bits to search |
| 220 | * @n: The number of set bit, which position is needed, counting from 0 |
| 221 | * |
| 222 | * The following is semantically equivalent: |
| 223 | * idx = find_nth_bit(addr, size, 0); |
| 224 | * idx = find_first_bit(addr, size); |
| 225 | * |
| 226 | * Returns the bit number of the N'th set bit. |
| 227 | * If no such, returns >= @size. |
| 228 | */ |
| 229 | static __always_inline |
| 230 | unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n) |
| 231 | { |
| 232 | if (n >= size) |
| 233 | return size; |
| 234 | |
| 235 | if (small_const_nbits(size)) { |
| 236 | unsigned long val = *addr & GENMASK(size - 1, 0); |
| 237 | |
| 238 | return val ? fns(word: val, n) : size; |
| 239 | } |
| 240 | |
| 241 | return __find_nth_bit(addr, size, n); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * find_nth_and_bit - find N'th set bit in 2 memory regions |
| 246 | * @addr1: The 1st address to start the search at |
| 247 | * @addr2: The 2nd address to start the search at |
| 248 | * @size: The maximum number of bits to search |
| 249 | * @n: The number of set bit, which position is needed, counting from 0 |
| 250 | * |
| 251 | * Returns the bit number of the N'th set bit. |
| 252 | * If no such, returns @size. |
| 253 | */ |
| 254 | static __always_inline |
| 255 | unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 256 | unsigned long size, unsigned long n) |
| 257 | { |
| 258 | if (n >= size) |
| 259 | return size; |
| 260 | |
| 261 | if (small_const_nbits(size)) { |
| 262 | unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0); |
| 263 | |
| 264 | return val ? fns(word: val, n) : size; |
| 265 | } |
| 266 | |
| 267 | return __find_nth_and_bit(addr1, addr2, size, n); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * find_nth_andnot_bit - find N'th set bit in 2 memory regions, |
| 272 | * flipping bits in 2nd region |
| 273 | * @addr1: The 1st address to start the search at |
| 274 | * @addr2: The 2nd address to start the search at |
| 275 | * @size: The maximum number of bits to search |
| 276 | * @n: The number of set bit, which position is needed, counting from 0 |
| 277 | * |
| 278 | * Returns the bit number of the N'th set bit. |
| 279 | * If no such, returns @size. |
| 280 | */ |
| 281 | static __always_inline |
| 282 | unsigned long find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, |
| 283 | unsigned long size, unsigned long n) |
| 284 | { |
| 285 | if (n >= size) |
| 286 | return size; |
| 287 | |
| 288 | if (small_const_nbits(size)) { |
| 289 | unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0); |
| 290 | |
| 291 | return val ? fns(word: val, n) : size; |
| 292 | } |
| 293 | |
| 294 | return __find_nth_andnot_bit(addr1, addr2, size, n); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * find_nth_and_andnot_bit - find N'th set bit in 2 memory regions, |
| 299 | * excluding those set in 3rd region |
| 300 | * @addr1: The 1st address to start the search at |
| 301 | * @addr2: The 2nd address to start the search at |
| 302 | * @addr3: The 3rd address to start the search at |
| 303 | * @size: The maximum number of bits to search |
| 304 | * @n: The number of set bit, which position is needed, counting from 0 |
| 305 | * |
| 306 | * Returns the bit number of the N'th set bit. |
| 307 | * If no such, returns @size. |
| 308 | */ |
| 309 | static __always_inline |
| 310 | unsigned long find_nth_and_andnot_bit(const unsigned long *addr1, |
| 311 | const unsigned long *addr2, |
| 312 | const unsigned long *addr3, |
| 313 | unsigned long size, unsigned long n) |
| 314 | { |
| 315 | if (n >= size) |
| 316 | return size; |
| 317 | |
| 318 | if (small_const_nbits(size)) { |
| 319 | unsigned long val = *addr1 & *addr2 & (~*addr3) & GENMASK(size - 1, 0); |
| 320 | |
| 321 | return val ? fns(word: val, n) : size; |
| 322 | } |
| 323 | |
| 324 | return __find_nth_and_andnot_bit(addr1, addr2, addr3, size, n); |
| 325 | } |
| 326 | |
| 327 | #ifndef find_first_and_bit |
| 328 | /** |
| 329 | * find_first_and_bit - find the first set bit in both memory regions |
| 330 | * @addr1: The first address to base the search on |
| 331 | * @addr2: The second address to base the search on |
| 332 | * @size: The bitmap size in bits |
| 333 | * |
| 334 | * Returns the bit number for the next set bit |
| 335 | * If no bits are set, returns @size. |
| 336 | */ |
| 337 | static __always_inline |
| 338 | unsigned long find_first_and_bit(const unsigned long *addr1, |
| 339 | const unsigned long *addr2, |
| 340 | unsigned long size) |
| 341 | { |
| 342 | if (small_const_nbits(size)) { |
| 343 | unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0); |
| 344 | |
| 345 | return val ? __ffs(val) : size; |
| 346 | } |
| 347 | |
| 348 | return _find_first_and_bit(addr1, addr2, size); |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | /** |
| 353 | * find_first_andnot_bit - find the first bit set in 1st memory region and unset in 2nd |
| 354 | * @addr1: The first address to base the search on |
| 355 | * @addr2: The second address to base the search on |
| 356 | * @size: The bitmap size in bits |
| 357 | * |
| 358 | * Returns the bit number for the first set bit |
| 359 | * If no bits are set, returns >= @size. |
| 360 | */ |
| 361 | static __always_inline |
| 362 | unsigned long find_first_andnot_bit(const unsigned long *addr1, |
| 363 | const unsigned long *addr2, |
| 364 | unsigned long size) |
| 365 | { |
| 366 | if (small_const_nbits(size)) { |
| 367 | unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0); |
| 368 | |
| 369 | return val ? __ffs(val) : size; |
| 370 | } |
| 371 | |
| 372 | return _find_first_andnot_bit(addr1, addr2, size); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * find_first_and_and_bit - find the first set bit in 3 memory regions |
| 377 | * @addr1: The first address to base the search on |
| 378 | * @addr2: The second address to base the search on |
| 379 | * @addr3: The third address to base the search on |
| 380 | * @size: The bitmap size in bits |
| 381 | * |
| 382 | * Returns the bit number for the first set bit |
| 383 | * If no bits are set, returns @size. |
| 384 | */ |
| 385 | static __always_inline |
| 386 | unsigned long find_first_and_and_bit(const unsigned long *addr1, |
| 387 | const unsigned long *addr2, |
| 388 | const unsigned long *addr3, |
| 389 | unsigned long size) |
| 390 | { |
| 391 | if (small_const_nbits(size)) { |
| 392 | unsigned long val = *addr1 & *addr2 & *addr3 & GENMASK(size - 1, 0); |
| 393 | |
| 394 | return val ? __ffs(val) : size; |
| 395 | } |
| 396 | |
| 397 | return _find_first_and_and_bit(addr1, addr2, addr3, size); |
| 398 | } |
| 399 | |
| 400 | #ifndef find_first_zero_bit |
| 401 | /** |
| 402 | * find_first_zero_bit - find the first cleared bit in a memory region |
| 403 | * @addr: The address to start the search at |
| 404 | * @size: The maximum number of bits to search |
| 405 | * |
| 406 | * Returns the bit number of the first cleared bit. |
| 407 | * If no bits are zero, returns @size. |
| 408 | */ |
| 409 | static __always_inline |
| 410 | unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) |
| 411 | { |
| 412 | if (small_const_nbits(size)) { |
| 413 | unsigned long val = *addr | ~GENMASK(size - 1, 0); |
| 414 | |
| 415 | return val == ~0UL ? size : ffz(val); |
| 416 | } |
| 417 | |
| 418 | return _find_first_zero_bit(addr, size); |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | #ifndef find_last_bit |
| 423 | /** |
| 424 | * find_last_bit - find the last set bit in a memory region |
| 425 | * @addr: The address to start the search at |
| 426 | * @size: The number of bits to search |
| 427 | * |
| 428 | * Returns the bit number of the last set bit, or size. |
| 429 | */ |
| 430 | static __always_inline |
| 431 | unsigned long find_last_bit(const unsigned long *addr, unsigned long size) |
| 432 | { |
| 433 | if (small_const_nbits(size)) { |
| 434 | unsigned long val = *addr & GENMASK(size - 1, 0); |
| 435 | |
| 436 | return val ? __fls(word: val) : size; |
| 437 | } |
| 438 | |
| 439 | return _find_last_bit(addr, size); |
| 440 | } |
| 441 | #endif |
| 442 | |
| 443 | /** |
| 444 | * find_next_and_bit_wrap - find the next set bit in both memory regions |
| 445 | * @addr1: The first address to base the search on |
| 446 | * @addr2: The second address to base the search on |
| 447 | * @size: The bitmap size in bits |
| 448 | * @offset: The bitnumber to start searching at |
| 449 | * |
| 450 | * Returns the bit number for the next set bit, or first set bit up to @offset |
| 451 | * If no bits are set, returns @size. |
| 452 | */ |
| 453 | static __always_inline |
| 454 | unsigned long find_next_and_bit_wrap(const unsigned long *addr1, |
| 455 | const unsigned long *addr2, |
| 456 | unsigned long size, unsigned long offset) |
| 457 | { |
| 458 | unsigned long bit = find_next_and_bit(addr1, addr2, size, offset); |
| 459 | |
| 460 | if (bit < size || offset == 0) |
| 461 | return bit; |
| 462 | |
| 463 | bit = find_first_and_bit(addr1, addr2, size: offset); |
| 464 | return bit < offset ? bit : size; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * find_next_bit_wrap - find the next set bit in a memory region |
| 469 | * @addr: The address to base the search on |
| 470 | * @size: The bitmap size in bits |
| 471 | * @offset: The bitnumber to start searching at |
| 472 | * |
| 473 | * Returns the bit number for the next set bit, or first set bit up to @offset |
| 474 | * If no bits are set, returns @size. |
| 475 | */ |
| 476 | static __always_inline |
| 477 | unsigned long find_next_bit_wrap(const unsigned long *addr, |
| 478 | unsigned long size, unsigned long offset) |
| 479 | { |
| 480 | unsigned long bit = find_next_bit(addr, size, offset); |
| 481 | |
| 482 | if (bit < size || offset == 0) |
| 483 | return bit; |
| 484 | |
| 485 | bit = find_first_bit(addr, size: offset); |
| 486 | return bit < offset ? bit : size; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing |
| 491 | * before using it alone. |
| 492 | */ |
| 493 | static __always_inline |
| 494 | unsigned long __for_each_wrap(const unsigned long *bitmap, unsigned long size, |
| 495 | unsigned long start, unsigned long n) |
| 496 | { |
| 497 | unsigned long bit; |
| 498 | |
| 499 | /* If not wrapped around */ |
| 500 | if (n > start) { |
| 501 | /* and have a bit, just return it. */ |
| 502 | bit = find_next_bit(addr: bitmap, size, offset: n); |
| 503 | if (bit < size) |
| 504 | return bit; |
| 505 | |
| 506 | /* Otherwise, wrap around and ... */ |
| 507 | n = 0; |
| 508 | } |
| 509 | |
| 510 | /* Search the other part. */ |
| 511 | bit = find_next_bit(addr: bitmap, size: start, offset: n); |
| 512 | return bit < start ? bit : size; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * find_next_clump8 - find next 8-bit clump with set bits in a memory region |
| 517 | * @clump: location to store copy of found clump |
| 518 | * @addr: address to base the search on |
| 519 | * @size: bitmap size in number of bits |
| 520 | * @offset: bit offset at which to start searching |
| 521 | * |
| 522 | * Returns the bit offset for the next set clump; the found clump value is |
| 523 | * copied to the location pointed by @clump. If no bits are set, returns @size. |
| 524 | */ |
| 525 | extern unsigned long find_next_clump8(unsigned long *clump, |
| 526 | const unsigned long *addr, |
| 527 | unsigned long size, unsigned long offset); |
| 528 | |
| 529 | #define find_first_clump8(clump, bits, size) \ |
| 530 | find_next_clump8((clump), (bits), (size), 0) |
| 531 | |
| 532 | #if defined(__LITTLE_ENDIAN) |
| 533 | |
| 534 | static __always_inline |
| 535 | unsigned long find_next_zero_bit_le(const void *addr, unsigned long size, unsigned long offset) |
| 536 | { |
| 537 | return find_next_zero_bit(addr, size, offset); |
| 538 | } |
| 539 | |
| 540 | static __always_inline |
| 541 | unsigned long find_next_bit_le(const void *addr, unsigned long size, unsigned long offset) |
| 542 | { |
| 543 | return find_next_bit(addr, size, offset); |
| 544 | } |
| 545 | |
| 546 | static __always_inline |
| 547 | unsigned long find_first_zero_bit_le(const void *addr, unsigned long size) |
| 548 | { |
| 549 | return find_first_zero_bit(addr, size); |
| 550 | } |
| 551 | |
| 552 | #elif defined(__BIG_ENDIAN) |
| 553 | |
| 554 | #ifndef find_next_zero_bit_le |
| 555 | static __always_inline |
| 556 | unsigned long find_next_zero_bit_le(const void *addr, unsigned |
| 557 | long size, unsigned long offset) |
| 558 | { |
| 559 | if (small_const_nbits(size)) { |
| 560 | unsigned long val = *(const unsigned long *)addr; |
| 561 | |
| 562 | if (unlikely(offset >= size)) |
| 563 | return size; |
| 564 | |
| 565 | val = swab(val) | ~GENMASK(size - 1, offset); |
| 566 | return val == ~0UL ? size : ffz(val); |
| 567 | } |
| 568 | |
| 569 | return _find_next_zero_bit_le(addr, size, offset); |
| 570 | } |
| 571 | #endif |
| 572 | |
| 573 | #ifndef find_first_zero_bit_le |
| 574 | static __always_inline |
| 575 | unsigned long find_first_zero_bit_le(const void *addr, unsigned long size) |
| 576 | { |
| 577 | if (small_const_nbits(size)) { |
| 578 | unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0); |
| 579 | |
| 580 | return val == ~0UL ? size : ffz(val); |
| 581 | } |
| 582 | |
| 583 | return _find_first_zero_bit_le(addr, size); |
| 584 | } |
| 585 | #endif |
| 586 | |
| 587 | #ifndef find_next_bit_le |
| 588 | static __always_inline |
| 589 | unsigned long find_next_bit_le(const void *addr, unsigned |
| 590 | long size, unsigned long offset) |
| 591 | { |
| 592 | if (small_const_nbits(size)) { |
| 593 | unsigned long val = *(const unsigned long *)addr; |
| 594 | |
| 595 | if (unlikely(offset >= size)) |
| 596 | return size; |
| 597 | |
| 598 | val = swab(val) & GENMASK(size - 1, offset); |
| 599 | return val ? __ffs(val) : size; |
| 600 | } |
| 601 | |
| 602 | return _find_next_bit_le(addr, size, offset); |
| 603 | } |
| 604 | #endif |
| 605 | |
| 606 | #else |
| 607 | #error "Please fix <asm/byteorder.h>" |
| 608 | #endif |
| 609 | |
| 610 | #define for_each_set_bit(bit, addr, size) \ |
| 611 | for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++) |
| 612 | |
| 613 | #define for_each_and_bit(bit, addr1, addr2, size) \ |
| 614 | for ((bit) = 0; \ |
| 615 | (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ |
| 616 | (bit)++) |
| 617 | |
| 618 | #define for_each_andnot_bit(bit, addr1, addr2, size) \ |
| 619 | for ((bit) = 0; \ |
| 620 | (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ |
| 621 | (bit)++) |
| 622 | |
| 623 | #define for_each_or_bit(bit, addr1, addr2, size) \ |
| 624 | for ((bit) = 0; \ |
| 625 | (bit) = find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ |
| 626 | (bit)++) |
| 627 | |
| 628 | /* same as for_each_set_bit() but use bit as value to start with */ |
| 629 | #define for_each_set_bit_from(bit, addr, size) \ |
| 630 | for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++) |
| 631 | |
| 632 | #define for_each_clear_bit(bit, addr, size) \ |
| 633 | for ((bit) = 0; \ |
| 634 | (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \ |
| 635 | (bit)++) |
| 636 | |
| 637 | /* same as for_each_clear_bit() but use bit as value to start with */ |
| 638 | #define for_each_clear_bit_from(bit, addr, size) \ |
| 639 | for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++) |
| 640 | |
| 641 | /** |
| 642 | * for_each_set_bitrange - iterate over all set bit ranges [b; e) |
| 643 | * @b: bit offset of start of current bitrange (first set bit) |
| 644 | * @e: bit offset of end of current bitrange (first unset bit) |
| 645 | * @addr: bitmap address to base the search on |
| 646 | * @size: bitmap size in number of bits |
| 647 | */ |
| 648 | #define for_each_set_bitrange(b, e, addr, size) \ |
| 649 | for ((b) = 0; \ |
| 650 | (b) = find_next_bit((addr), (size), b), \ |
| 651 | (e) = find_next_zero_bit((addr), (size), (b) + 1), \ |
| 652 | (b) < (size); \ |
| 653 | (b) = (e) + 1) |
| 654 | |
| 655 | /** |
| 656 | * for_each_set_bitrange_from - iterate over all set bit ranges [b; e) |
| 657 | * @b: bit offset of start of current bitrange (first set bit); must be initialized |
| 658 | * @e: bit offset of end of current bitrange (first unset bit) |
| 659 | * @addr: bitmap address to base the search on |
| 660 | * @size: bitmap size in number of bits |
| 661 | */ |
| 662 | #define for_each_set_bitrange_from(b, e, addr, size) \ |
| 663 | for (; \ |
| 664 | (b) = find_next_bit((addr), (size), (b)), \ |
| 665 | (e) = find_next_zero_bit((addr), (size), (b) + 1), \ |
| 666 | (b) < (size); \ |
| 667 | (b) = (e) + 1) |
| 668 | |
| 669 | /** |
| 670 | * for_each_clear_bitrange - iterate over all unset bit ranges [b; e) |
| 671 | * @b: bit offset of start of current bitrange (first unset bit) |
| 672 | * @e: bit offset of end of current bitrange (first set bit) |
| 673 | * @addr: bitmap address to base the search on |
| 674 | * @size: bitmap size in number of bits |
| 675 | */ |
| 676 | #define for_each_clear_bitrange(b, e, addr, size) \ |
| 677 | for ((b) = 0; \ |
| 678 | (b) = find_next_zero_bit((addr), (size), (b)), \ |
| 679 | (e) = find_next_bit((addr), (size), (b) + 1), \ |
| 680 | (b) < (size); \ |
| 681 | (b) = (e) + 1) |
| 682 | |
| 683 | /** |
| 684 | * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e) |
| 685 | * @b: bit offset of start of current bitrange (first set bit); must be initialized |
| 686 | * @e: bit offset of end of current bitrange (first unset bit) |
| 687 | * @addr: bitmap address to base the search on |
| 688 | * @size: bitmap size in number of bits |
| 689 | */ |
| 690 | #define for_each_clear_bitrange_from(b, e, addr, size) \ |
| 691 | for (; \ |
| 692 | (b) = find_next_zero_bit((addr), (size), (b)), \ |
| 693 | (e) = find_next_bit((addr), (size), (b) + 1), \ |
| 694 | (b) < (size); \ |
| 695 | (b) = (e) + 1) |
| 696 | |
| 697 | /** |
| 698 | * for_each_set_bit_wrap - iterate over all set bits starting from @start, and |
| 699 | * wrapping around the end of bitmap. |
| 700 | * @bit: offset for current iteration |
| 701 | * @addr: bitmap address to base the search on |
| 702 | * @size: bitmap size in number of bits |
| 703 | * @start: Starting bit for bitmap traversing, wrapping around the bitmap end |
| 704 | */ |
| 705 | #define for_each_set_bit_wrap(bit, addr, size, start) \ |
| 706 | for ((bit) = find_next_bit_wrap((addr), (size), (start)); \ |
| 707 | (bit) < (size); \ |
| 708 | (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1)) |
| 709 | |
| 710 | /** |
| 711 | * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits |
| 712 | * @start: bit offset to start search and to store the current iteration offset |
| 713 | * @clump: location to store copy of current 8-bit clump |
| 714 | * @bits: bitmap address to base the search on |
| 715 | * @size: bitmap size in number of bits |
| 716 | */ |
| 717 | #define for_each_set_clump8(start, clump, bits, size) \ |
| 718 | for ((start) = find_first_clump8(&(clump), (bits), (size)); \ |
| 719 | (start) < (size); \ |
| 720 | (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8)) |
| 721 | |
| 722 | #endif /*__LINUX_FIND_H_ */ |
| 723 | |