Skip to content

Commit 1c2aa56

Browse files
visitorckwYuryNorov
authored andcommitted
bitops: Optimize fns() for improved performance
The current fns() repeatedly uses __ffs() to find the index of the least significant bit and then clears the corresponding bit using __clear_bit(). The method for clearing the least significant bit can be optimized by using word &= word - 1 instead. Typically, the execution time of one __ffs() plus one __clear_bit() is longer than that of a bitwise AND operation and a subtraction. To improve performance, the loop for clearing the least significant bit has been replaced with word &= word - 1, followed by a single __ffs() operation to obtain the answer. This change reduces the number of __ffs() iterations from n to just one, enhancing overall performance. This modification significantly accelerates the fns() function in the test_bitops benchmark, improving its speed by approximately 7.6 times. Additionally, it enhances the performance of find_nth_bit() in the find_bit benchmark by approximately 26%. Before: test_bitops: fns: 58033164 ns find_nth_bit: 4254313 ns, 16525 iterations After: test_bitops: fns: 7637268 ns find_nth_bit: 3362863 ns, 16501 iterations CC: Andrew Morton <akpm@linux-foundation.org> CC: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Signed-off-by: Yury Norov <yury.norov@gmail.com>
1 parent 0a2c666 commit 1c2aa56

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

include/linux/bitops.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,10 @@ static inline unsigned long __ffs64(u64 word)
254254
*/
255255
static inline unsigned long fns(unsigned long word, unsigned int n)
256256
{
257-
unsigned int bit;
257+
while (word && n--)
258+
word &= word - 1;
258259

259-
while (word) {
260-
bit = __ffs(word);
261-
if (n-- == 0)
262-
return bit;
263-
__clear_bit(bit, &word);
264-
}
265-
266-
return BITS_PER_LONG;
260+
return word ? __ffs(word) : BITS_PER_LONG;
267261
}
268262

269263
/**

0 commit comments

Comments
 (0)