tools: sync find_bit() implementation
authorYury Norov <yury.norov@gmail.com>
Thu, 15 Sep 2022 02:07:30 +0000 (19:07 -0700)
committerYury Norov <yury.norov@gmail.com>
Wed, 21 Sep 2022 19:21:44 +0000 (12:21 -0700)
Sync find_first_bit() and find_next_bit() implementation with the
mother kernel.

Also, drop unused find_last_bit() and find_next_clump8().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
tools/include/linux/find.h
tools/lib/find_bit.c

index 47e2bd6c51745d4942f4121dc58004f4d94c0aa5..38c0a542b0e2f20a6852305f4cb2d2fff307f7c6 100644 (file)
@@ -8,21 +8,23 @@
 
 #include <linux/bitops.h>
 
-extern unsigned long _find_next_bit(const unsigned long *addr1,
-               const unsigned long *addr2, unsigned long nbits,
-               unsigned long start, unsigned long invert, unsigned long le);
+unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,
+                               unsigned long start);
+unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
+                                       unsigned long nbits, unsigned long start);
+unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
+                                        unsigned long start);
 extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
 extern unsigned long _find_first_and_bit(const unsigned long *addr1,
                                         const unsigned long *addr2, unsigned long size);
 extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
-extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
 
 #ifndef find_next_bit
 /**
  * find_next_bit - find the next set bit in a memory region
  * @addr: The address to base the search on
- * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ * @offset: The bitnumber to start searching at
  *
  * Returns the bit number for the next set bit
  * If no bits are set, returns @size.
@@ -41,7 +43,7 @@ unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
                return val ? __ffs(val) : size;
        }
 
-       return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
+       return _find_next_bit(addr, size, offset);
 }
 #endif
 
@@ -50,8 +52,8 @@ unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  * find_next_and_bit - find the next set bit in both memory regions
  * @addr1: The first address to base the search on
  * @addr2: The second address to base the search on
- * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ * @offset: The bitnumber to start searching at
  *
  * Returns the bit number for the next set bit
  * If no bits are set, returns @size.
@@ -71,7 +73,7 @@ unsigned long find_next_and_bit(const unsigned long *addr1,
                return val ? __ffs(val) : size;
        }
 
-       return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
+       return _find_next_and_bit(addr1, addr2, size, offset);
 }
 #endif
 
@@ -79,8 +81,8 @@ unsigned long find_next_and_bit(const unsigned long *addr1,
 /**
  * find_next_zero_bit - find the next cleared bit in a memory region
  * @addr: The address to base the search on
- * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ * @offset: The bitnumber to start searching at
  *
  * Returns the bit number of the next zero bit
  * If no bits are zero, returns @size.
@@ -99,7 +101,7 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
                return val == ~0UL ? size : ffz(val);
        }
 
-       return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
+       return _find_next_zero_bit(addr, size, offset);
 }
 #endif
 
@@ -172,43 +174,4 @@ unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
 }
 #endif
 
-#ifndef find_last_bit
-/**
- * find_last_bit - find the last set bit in a memory region
- * @addr: The address to start the search at
- * @size: The number of bits to search
- *
- * Returns the bit number of the last set bit, or size.
- */
-static inline
-unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
-{
-       if (small_const_nbits(size)) {
-               unsigned long val = *addr & GENMASK(size - 1, 0);
-
-               return val ? __fls(val) : size;
-       }
-
-       return _find_last_bit(addr, size);
-}
-#endif
-
-/**
- * find_next_clump8 - find next 8-bit clump with set bits in a memory region
- * @clump: location to store copy of found clump
- * @addr: address to base the search on
- * @size: bitmap size in number of bits
- * @offset: bit offset at which to start searching
- *
- * Returns the bit offset for the next set clump; the found clump value is
- * copied to the location pointed by @clump. If no bits are set, returns @size.
- */
-extern unsigned long find_next_clump8(unsigned long *clump,
-                                     const unsigned long *addr,
-                                     unsigned long size, unsigned long offset);
-
-#define find_first_clump8(clump, bits, size) \
-       find_next_clump8((clump), (bits), (size), 0)
-
-
 #endif /*__LINUX_FIND_H_ */
index ba4b8d94e004823219594a09347be0d416823db0..6a3dc167d30ed16210a7b8dc0396c911393d04e4 100644 (file)
 #include <linux/bitmap.h>
 #include <linux/kernel.h>
 
-#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
-               !defined(find_next_and_bit)
-
 /*
- * This is a common helper function for find_next_bit, find_next_zero_bit, and
- * find_next_and_bit. The differences are:
- *  - The "invert" argument, which is XORed with each fetched word before
- *    searching it for one bits.
- *  - The optional "addr2", which is anded with "addr1" if present.
+ * Common helper for find_bit() function family
+ * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
+ * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
+ * @size: The bitmap size in bits
  */
-unsigned long _find_next_bit(const unsigned long *addr1,
-               const unsigned long *addr2, unsigned long nbits,
-               unsigned long start, unsigned long invert, unsigned long le)
-{
-       unsigned long tmp, mask;
-       (void) le;
-
-       if (unlikely(start >= nbits))
-               return nbits;
-
-       tmp = addr1[start / BITS_PER_LONG];
-       if (addr2)
-               tmp &= addr2[start / BITS_PER_LONG];
-       tmp ^= invert;
-
-       /* Handle 1st word. */
-       mask = BITMAP_FIRST_WORD_MASK(start);
-
-       /*
-        * Due to the lack of swab() in tools, and the fact that it doesn't
-        * need little-endian support, just comment it out
-        */
-#if (0)
-       if (le)
-               mask = swab(mask);
-#endif
-
-       tmp &= mask;
+#define FIND_FIRST_BIT(FETCH, MUNGE, size)                                     \
+({                                                                             \
+       unsigned long idx, val, sz = (size);                                    \
+                                                                               \
+       for (idx = 0; idx * BITS_PER_LONG < sz; idx++) {                        \
+               val = (FETCH);                                                  \
+               if (val) {                                                      \
+                       sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz);  \
+                       break;                                                  \
+               }                                                               \
+       }                                                                       \
+                                                                               \
+       sz;                                                                     \
+})
 
-       start = round_down(start, BITS_PER_LONG);
-
-       while (!tmp) {
-               start += BITS_PER_LONG;
-               if (start >= nbits)
-                       return nbits;
-
-               tmp = addr1[start / BITS_PER_LONG];
-               if (addr2)
-                       tmp &= addr2[start / BITS_PER_LONG];
-               tmp ^= invert;
-       }
-
-#if (0)
-       if (le)
-               tmp = swab(tmp);
-#endif
-
-       return min(start + __ffs(tmp), nbits);
-}
-#endif
+/*
+ * Common helper for find_next_bit() function family
+ * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
+ * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
+ * @size: The bitmap size in bits
+ * @start: The bitnumber to start searching at
+ */
+#define FIND_NEXT_BIT(FETCH, MUNGE, size, start)                               \
+({                                                                             \
+       unsigned long mask, idx, tmp, sz = (size), __start = (start);           \
+                                                                               \
+       if (unlikely(__start >= sz))                                            \
+               goto out;                                                       \
+                                                                               \
+       mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start));                          \
+       idx = __start / BITS_PER_LONG;                                          \
+                                                                               \
+       for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) {                       \
+               if ((idx + 1) * BITS_PER_LONG >= sz)                            \
+                       goto out;                                               \
+               idx++;                                                          \
+       }                                                                       \
+                                                                               \
+       sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz);                  \
+out:                                                                           \
+       sz;                                                                     \
+})
 
 #ifndef find_first_bit
 /*
@@ -85,14 +73,7 @@ unsigned long _find_next_bit(const unsigned long *addr1,
  */
 unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
 {
-       unsigned long idx;
-
-       for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
-               if (addr[idx])
-                       return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
-       }
-
-       return size;
+       return FIND_FIRST_BIT(addr[idx], /* nop */, size);
 }
 #endif
 
@@ -104,15 +85,7 @@ unsigned long _find_first_and_bit(const unsigned long *addr1,
                                  const unsigned long *addr2,
                                  unsigned long size)
 {
-       unsigned long idx, val;
-
-       for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
-               val = addr1[idx] & addr2[idx];
-               if (val)
-                       return min(idx * BITS_PER_LONG + __ffs(val), size);
-       }
-
-       return size;
+       return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
 }
 #endif
 
@@ -122,13 +95,29 @@ unsigned long _find_first_and_bit(const unsigned long *addr1,
  */
 unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
 {
-       unsigned long idx;
+       return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
+}
+#endif
 
-       for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
-               if (addr[idx] != ~0UL)
-                       return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
-       }
+#ifndef find_next_bit
+unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
+{
+       return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
+}
+#endif
 
-       return size;
+#ifndef find_next_and_bit
+unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
+                                       unsigned long nbits, unsigned long start)
+{
+       return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start);
+}
+#endif
+
+#ifndef find_next_zero_bit
+unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
+                                        unsigned long start)
+{
+       return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
 }
 #endif