^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * MSB0 numbered special bitops handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * The bits are numbered:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * |0..............63|64............127|128...........191|192...........255|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * The reason for this bit numbering is the fact that the hardware sets bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * in a bitmap starting at bit 0 (MSB) and we don't want to scan the bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * from the 'wrong end'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const unsigned long *p = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned long result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) while (size & ~(BITS_PER_LONG - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if ((tmp = *(p++)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) result += BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) size -= BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) tmp = (*p) & (~0UL << (BITS_PER_LONG - size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!tmp) /* Are any bits set? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return result + size; /* Nope. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return result + (__fls(tmp) ^ (BITS_PER_LONG - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) EXPORT_SYMBOL(find_first_bit_inv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const unsigned long *p = addr + (offset / BITS_PER_LONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned long result = offset & ~(BITS_PER_LONG - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (offset >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size -= result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) offset %= BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) tmp = *(p++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) tmp &= (~0UL >> offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (size < BITS_PER_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto found_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto found_middle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) size -= BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) result += BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) while (size & ~(BITS_PER_LONG-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if ((tmp = *(p++)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) goto found_middle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) result += BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) size -= BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) tmp = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) found_first:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) tmp &= (~0UL << (BITS_PER_LONG - size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!tmp) /* Are any bits set? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return result + size; /* Nope. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) found_middle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return result + (__fls(tmp) ^ (BITS_PER_LONG - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) EXPORT_SYMBOL(find_next_bit_inv);