···8899#include <linux/bitops.h>10101111-extern unsigned long _find_next_bit(const unsigned long *addr1,1212- const unsigned long *addr2, unsigned long nbits,1313- unsigned long start, unsigned long invert, unsigned long le);1111+unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,1212+ unsigned long start);1313+unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,1414+ unsigned long nbits, unsigned long start);1515+unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,1616+ unsigned long start);1417extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);1518extern unsigned long _find_first_and_bit(const unsigned long *addr1,1619 const unsigned long *addr2, unsigned long size);1720extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);1818-extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);19212022#ifndef find_next_bit2123/**2224 * find_next_bit - find the next set bit in a memory region2325 * @addr: The address to base the search on2424- * @offset: The bitnumber to start searching at2526 * @size: The bitmap size in bits2727+ * @offset: The bitnumber to start searching at2628 *2729 * Returns the bit number for the next set bit2830 * If no bits are set, returns @size.···4341 return val ? __ffs(val) : size;4442 }45434646- return _find_next_bit(addr, NULL, size, offset, 0UL, 0);4444+ return _find_next_bit(addr, size, offset);4745}4846#endif4947···5250 * find_next_and_bit - find the next set bit in both memory regions5351 * @addr1: The first address to base the search on5452 * @addr2: The second address to base the search on5555- * @offset: The bitnumber to start searching at5653 * @size: The bitmap size in bits5454+ * @offset: The bitnumber to start searching at5755 *5856 * Returns the bit number for the next set bit5957 * If no bits are set, returns @size.···7371 return val ? __ffs(val) : size;7472 }75737676- return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);7474+ return _find_next_and_bit(addr1, addr2, size, offset);7775}7876#endif7977···8179/**8280 * find_next_zero_bit - find the next cleared bit in a memory region8381 * @addr: The address to base the search on8484- * @offset: The bitnumber to start searching at8582 * @size: The bitmap size in bits8383+ * @offset: The bitnumber to start searching at8684 *8785 * Returns the bit number of the next zero bit8886 * If no bits are zero, returns @size.···10199 return val == ~0UL ? size : ffz(val);102100 }103101104104- return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);102102+ return _find_next_zero_bit(addr, size, offset);105103}106104#endif107105···173171 return _find_first_zero_bit(addr, size);174172}175173#endif176176-177177-#ifndef find_last_bit178178-/**179179- * find_last_bit - find the last set bit in a memory region180180- * @addr: The address to start the search at181181- * @size: The number of bits to search182182- *183183- * Returns the bit number of the last set bit, or size.184184- */185185-static inline186186-unsigned long find_last_bit(const unsigned long *addr, unsigned long size)187187-{188188- if (small_const_nbits(size)) {189189- unsigned long val = *addr & GENMASK(size - 1, 0);190190-191191- return val ? __fls(val) : size;192192- }193193-194194- return _find_last_bit(addr, size);195195-}196196-#endif197197-198198-/**199199- * find_next_clump8 - find next 8-bit clump with set bits in a memory region200200- * @clump: location to store copy of found clump201201- * @addr: address to base the search on202202- * @size: bitmap size in number of bits203203- * @offset: bit offset at which to start searching204204- *205205- * Returns the bit offset for the next set clump; the found clump value is206206- * copied to the location pointed by @clump. If no bits are set, returns @size.207207- */208208-extern unsigned long find_next_clump8(unsigned long *clump,209209- const unsigned long *addr,210210- unsigned long size, unsigned long offset);211211-212212-#define find_first_clump8(clump, bits, size) \213213- find_next_clump8((clump), (bits), (size), 0)214214-215174216175#endif /*__LINUX_FIND_H_ */
+69-80
tools/lib/find_bit.c
···1818#include <linux/bitmap.h>1919#include <linux/kernel.h>20202121-#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \2222- !defined(find_next_and_bit)2121+/*2222+ * Common helper for find_bit() function family2323+ * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)2424+ * @MUNGE: The expression that post-processes a word containing found bit (may be empty)2525+ * @size: The bitmap size in bits2626+ */2727+#define FIND_FIRST_BIT(FETCH, MUNGE, size) \2828+({ \2929+ unsigned long idx, val, sz = (size); \3030+ \3131+ for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \3232+ val = (FETCH); \3333+ if (val) { \3434+ sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \3535+ break; \3636+ } \3737+ } \3838+ \3939+ sz; \4040+})23412442/*2525- * This is a common helper function for find_next_bit, find_next_zero_bit, and2626- * find_next_and_bit. The differences are:2727- * - The "invert" argument, which is XORed with each fetched word before2828- * searching it for one bits.2929- * - The optional "addr2", which is anded with "addr1" if present.4343+ * Common helper for find_next_bit() function family4444+ * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)4545+ * @MUNGE: The expression that post-processes a word containing found bit (may be empty)4646+ * @size: The bitmap size in bits4747+ * @start: The bitnumber to start searching at3048 */3131-unsigned long _find_next_bit(const unsigned long *addr1,3232- const unsigned long *addr2, unsigned long nbits,3333- unsigned long start, unsigned long invert, unsigned long le)3434-{3535- unsigned long tmp, mask;3636- (void) le;3737-3838- if (unlikely(start >= nbits))3939- return nbits;4040-4141- tmp = addr1[start / BITS_PER_LONG];4242- if (addr2)4343- tmp &= addr2[start / BITS_PER_LONG];4444- tmp ^= invert;4545-4646- /* Handle 1st word. */4747- mask = BITMAP_FIRST_WORD_MASK(start);4848-4949- /*5050- * Due to the lack of swab() in tools, and the fact that it doesn't5151- * need little-endian support, just comment it out5252- */5353-#if (0)5454- if (le)5555- mask = swab(mask);5656-#endif5757-5858- tmp &= mask;5959-6060- start = round_down(start, BITS_PER_LONG);6161-6262- while (!tmp) {6363- start += BITS_PER_LONG;6464- if (start >= nbits)6565- return nbits;6666-6767- tmp = addr1[start / BITS_PER_LONG];6868- if (addr2)6969- tmp &= addr2[start / BITS_PER_LONG];7070- tmp ^= invert;7171- }7272-7373-#if (0)7474- if (le)7575- tmp = swab(tmp);7676-#endif7777-7878- return min(start + __ffs(tmp), nbits);7979-}8080-#endif4949+#define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \5050+({ \5151+ unsigned long mask, idx, tmp, sz = (size), __start = (start); \5252+ \5353+ if (unlikely(__start >= sz)) \5454+ goto out; \5555+ \5656+ mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \5757+ idx = __start / BITS_PER_LONG; \5858+ \5959+ for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \6060+ if ((idx + 1) * BITS_PER_LONG >= sz) \6161+ goto out; \6262+ idx++; \6363+ } \6464+ \6565+ sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \6666+out: \6767+ sz; \6868+})81698270#ifndef find_first_bit8371/*···7385 */7486unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)7587{7676- unsigned long idx;7777-7878- for (idx = 0; idx * BITS_PER_LONG < size; idx++) {7979- if (addr[idx])8080- return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);8181- }8282-8383- return size;8888+ return FIND_FIRST_BIT(addr[idx], /* nop */, size);8489}8590#endif8691···85104 const unsigned long *addr2,86105 unsigned long size)87106{8888- unsigned long idx, val;8989-9090- for (idx = 0; idx * BITS_PER_LONG < size; idx++) {9191- val = addr1[idx] & addr2[idx];9292- if (val)9393- return min(idx * BITS_PER_LONG + __ffs(val), size);9494- }9595-9696- return size;107107+ return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);97108}98109#endif99110···95122 */96123unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)97124{9898- unsigned long idx;125125+ return FIND_FIRST_BIT(~addr[idx], /* nop */, size);126126+}127127+#endif99128100100- for (idx = 0; idx * BITS_PER_LONG < size; idx++) {101101- if (addr[idx] != ~0UL)102102- return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);103103- }129129+#ifndef find_next_bit130130+unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)131131+{132132+ return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);133133+}134134+#endif104135105105- return size;136136+#ifndef find_next_and_bit137137+unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,138138+ unsigned long nbits, unsigned long start)139139+{140140+ return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start);141141+}142142+#endif143143+144144+#ifndef find_next_zero_bit145145+unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,146146+ unsigned long start)147147+{148148+ return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);106149}107150#endif