Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib: add fast path for find_first_*_bit() and find_last_bit()

Similarly to bitmap functions, users would benefit if we'll handle a case
of small-size bitmaps that fit into a single word.

While here, move the find_last_bit() declaration to bitops/find.h where
other find_*_bit() functions sit.

Link: https://lkml.kernel.org/r/20210401003153.97325-11-yury.norov@gmail.com
Signed-off-by: Yury Norov <yury.norov@gmail.com>
Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Alexey Klimov <aklimov@redhat.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: David Sterba <dsterba@suse.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Jianpeng Ma <jianpeng.ma@intel.com>
Cc: Joe Perches <joe@perches.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Rich Felker <dalias@libc.org>
Cc: Stefano Brivio <sbrivio@redhat.com>
Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>
Cc: Yoshinori Sato <ysato@users.osdn.me>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Yury Norov and committed by
Linus Torvalds
2cc7b6a4 277a20a4

+52 -22
+46 -4
include/asm-generic/bitops/find.h
··· 5 5 extern unsigned long _find_next_bit(const unsigned long *addr1, 6 6 const unsigned long *addr2, unsigned long nbits, 7 7 unsigned long start, unsigned long invert, unsigned long le); 8 + extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); 9 + extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size); 10 + extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size); 8 11 9 12 #ifndef find_next_bit 10 13 /** ··· 105 102 * Returns the bit number of the first set bit. 106 103 * If no bits are set, returns @size. 107 104 */ 108 - extern unsigned long find_first_bit(const unsigned long *addr, 109 - unsigned long size); 105 + static inline 106 + unsigned long find_first_bit(const unsigned long *addr, unsigned long size) 107 + { 108 + if (small_const_nbits(size)) { 109 + unsigned long val = *addr & GENMASK(size - 1, 0); 110 + 111 + return val ? __ffs(val) : size; 112 + } 113 + 114 + return _find_first_bit(addr, size); 115 + } 110 116 111 117 /** 112 118 * find_first_zero_bit - find the first cleared bit in a memory region ··· 125 113 * Returns the bit number of the first cleared bit. 126 114 * If no bits are zero, returns @size. 127 115 */ 128 - extern unsigned long find_first_zero_bit(const unsigned long *addr, 129 - unsigned long size); 116 + static inline 117 + unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) 118 + { 119 + if (small_const_nbits(size)) { 120 + unsigned long val = *addr | ~GENMASK(size - 1, 0); 121 + 122 + return val == ~0UL ? size : ffz(val); 123 + } 124 + 125 + return _find_first_zero_bit(addr, size); 126 + } 130 127 #else /* CONFIG_GENERIC_FIND_FIRST_BIT */ 131 128 132 129 #ifndef find_first_bit ··· 146 125 #endif 147 126 148 127 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ 128 + 129 + #ifndef find_last_bit 130 + /** 131 + * find_last_bit - find the last set bit in a memory region 132 + * @addr: The address to start the search at 133 + * @size: The number of bits to search 134 + * 135 + * Returns the bit number of the last set bit, or size. 136 + */ 137 + static inline 138 + unsigned long find_last_bit(const unsigned long *addr, unsigned long size) 139 + { 140 + if (small_const_nbits(size)) { 141 + unsigned long val = *addr & GENMASK(size - 1, 0); 142 + 143 + return val ? __fls(val) : size; 144 + } 145 + 146 + return _find_last_bit(addr, size); 147 + } 148 + #endif 149 149 150 150 /** 151 151 * find_next_clump8 - find next 8-bit clump with set bits in a memory region
-12
include/linux/bitops.h
··· 286 286 }) 287 287 #endif 288 288 289 - #ifndef find_last_bit 290 - /** 291 - * find_last_bit - find the last set bit in a memory region 292 - * @addr: The address to start the search at 293 - * @size: The number of bits to search 294 - * 295 - * Returns the bit number of the last set bit, or size. 296 - */ 297 - extern unsigned long find_last_bit(const unsigned long *addr, 298 - unsigned long size); 299 - #endif 300 - 301 289 #endif /* __KERNEL__ */ 302 290 #endif
+6 -6
lib/find_bit.c
··· 75 75 /* 76 76 * Find the first set bit in a memory region. 77 77 */ 78 - unsigned long find_first_bit(const unsigned long *addr, unsigned long size) 78 + unsigned long _find_first_bit(const unsigned long *addr, unsigned long size) 79 79 { 80 80 unsigned long idx; 81 81 ··· 86 86 87 87 return size; 88 88 } 89 - EXPORT_SYMBOL(find_first_bit); 89 + EXPORT_SYMBOL(_find_first_bit); 90 90 #endif 91 91 92 92 #ifndef find_first_zero_bit 93 93 /* 94 94 * Find the first cleared bit in a memory region. 95 95 */ 96 - unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) 96 + unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size) 97 97 { 98 98 unsigned long idx; 99 99 ··· 104 104 105 105 return size; 106 106 } 107 - EXPORT_SYMBOL(find_first_zero_bit); 107 + EXPORT_SYMBOL(_find_first_zero_bit); 108 108 #endif 109 109 110 110 #ifndef find_last_bit 111 - unsigned long find_last_bit(const unsigned long *addr, unsigned long size) 111 + unsigned long _find_last_bit(const unsigned long *addr, unsigned long size) 112 112 { 113 113 if (size) { 114 114 unsigned long val = BITMAP_LAST_WORD_MASK(size); ··· 124 124 } 125 125 return size; 126 126 } 127 - EXPORT_SYMBOL(find_last_bit); 127 + EXPORT_SYMBOL(_find_last_bit); 128 128 #endif 129 129 130 130 unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,