mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2024-12-29 09:12:07 +00:00
e79864f316
Over the past couple years, the function _find_next_bit() was extended with parameters that modify its behavior to implement and- zero- and le- flavors. The parameters are passed at compile time, but current design prevents a compiler from optimizing out the conditionals. As find_next_bit() API grows, I expect that more parameters will be added. Current design would require more conditional code in _find_next_bit(), which would bloat the helper even more and make it barely readable. This patch replaces _find_next_bit() with a macro FIND_NEXT_BIT, and adds a set of wrappers, so that the compile-time optimizations become possible. The common logic is moved to the new macro, and all flavors may be generated by providing a FETCH macro parameter, like in this example: #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) ... find_next_xornot_and_bit(addr1, addr2, addr3, size, start) { return FIND_NEXT_BIT(addr1[idx] ^ ~addr2[idx] & addr3[idx], /* nop */, size, start); } The FETCH may be of any complexity, as soon as it only refers the bitmap(s) and an iterator idx. MUNGE is here to support _le code generation for BE builds. May be empty. I ran find_bit_benchmark 16 times on top of 6.0-rc2 and 16 times on top of 6.0-rc2 + this series. The results for kvm/x86_64 are: v6.0-rc2 Optimized Difference Z-score Random dense bitmap ns ns ns % find_next_bit: 787735 670546 117189 14.9 3.97 find_next_zero_bit: 777492 664208 113284 14.6 10.51 find_last_bit: 830925 687573 143352 17.3 2.35 find_first_bit: 3874366 3306635 567731 14.7 1.84 find_first_and_bit: 40677125 37739887 2937238 7.2 1.36 find_next_and_bit: 347865 304456 43409 12.5 1.35 Random sparse bitmap find_next_bit: 19816 14021 5795 29.2 6.10 find_next_zero_bit: 1318901 1223794 95107 7.2 1.41 find_last_bit: 14573 13514 1059 7.3 6.92 find_first_bit: 1313321 1249024 64297 4.9 1.53 find_first_and_bit: 8921 8098 823 9.2 4.56 find_next_and_bit: 9796 7176 2620 26.7 5.39 Where the statistics is significant (z-score > 3), the improvement is ~15%. According to the bloat-o-meter, the Image size is 10-11K less: x86_64/defconfig: add/remove: 32/14 grow/shrink: 61/782 up/down: 6344/-16521 (-10177) arm64/defconfig: add/remove: 3/2 grow/shrink: 50/714 up/down: 608/-11556 (-10948) Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Yury Norov <yury.norov@gmail.com>
200 lines
5.1 KiB
C
200 lines
5.1 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* bit search implementation
|
|
*
|
|
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*
|
|
* Copyright (C) 2008 IBM Corporation
|
|
* 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
|
|
* (Inspired by David Howell's find_next_bit implementation)
|
|
*
|
|
* Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
|
|
* size and improve performance, 2015.
|
|
*/
|
|
|
|
#include <linux/bitops.h>
|
|
#include <linux/bitmap.h>
|
|
#include <linux/export.h>
|
|
#include <linux/math.h>
|
|
#include <linux/minmax.h>
|
|
#include <linux/swab.h>
|
|
|
|
/*
|
|
* 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
|
|
*/
|
|
#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; \
|
|
})
|
|
|
|
/*
|
|
* 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
|
|
/*
|
|
* Find the first set bit in a memory region.
|
|
*/
|
|
unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
|
|
{
|
|
return FIND_FIRST_BIT(addr[idx], /* nop */, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_bit);
|
|
#endif
|
|
|
|
#ifndef find_first_and_bit
|
|
/*
|
|
* Find the first set bit in two memory regions.
|
|
*/
|
|
unsigned long _find_first_and_bit(const unsigned long *addr1,
|
|
const unsigned long *addr2,
|
|
unsigned long size)
|
|
{
|
|
return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_and_bit);
|
|
#endif
|
|
|
|
#ifndef find_first_zero_bit
|
|
/*
|
|
* Find the first cleared bit in a memory region.
|
|
*/
|
|
unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
|
|
{
|
|
return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_zero_bit);
|
|
#endif
|
|
|
|
#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);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_bit);
|
|
#endif
|
|
|
|
#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);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_and_bit);
|
|
#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);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_zero_bit);
|
|
#endif
|
|
|
|
#ifndef find_last_bit
|
|
unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
|
|
{
|
|
if (size) {
|
|
unsigned long val = BITMAP_LAST_WORD_MASK(size);
|
|
unsigned long idx = (size-1) / BITS_PER_LONG;
|
|
|
|
do {
|
|
val &= addr[idx];
|
|
if (val)
|
|
return idx * BITS_PER_LONG + __fls(val);
|
|
|
|
val = ~0ul;
|
|
} while (idx--);
|
|
}
|
|
return size;
|
|
}
|
|
EXPORT_SYMBOL(_find_last_bit);
|
|
#endif
|
|
|
|
unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
|
|
unsigned long size, unsigned long offset)
|
|
{
|
|
offset = find_next_bit(addr, size, offset);
|
|
if (offset == size)
|
|
return size;
|
|
|
|
offset = round_down(offset, 8);
|
|
*clump = bitmap_get_value8(addr, offset);
|
|
|
|
return offset;
|
|
}
|
|
EXPORT_SYMBOL(find_next_clump8);
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
#ifndef find_first_zero_bit_le
|
|
/*
|
|
* Find the first cleared bit in an LE memory region.
|
|
*/
|
|
unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size)
|
|
{
|
|
return FIND_FIRST_BIT(~addr[idx], swab, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_zero_bit_le);
|
|
|
|
#endif
|
|
|
|
#ifndef find_next_zero_bit_le
|
|
unsigned long _find_next_zero_bit_le(const unsigned long *addr,
|
|
unsigned long size, unsigned long offset)
|
|
{
|
|
return FIND_NEXT_BIT(~addr[idx], swab, size, offset);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_zero_bit_le);
|
|
#endif
|
|
|
|
#ifndef find_next_bit_le
|
|
unsigned long _find_next_bit_le(const unsigned long *addr,
|
|
unsigned long size, unsigned long offset)
|
|
{
|
|
return FIND_NEXT_BIT(addr[idx], swab, size, offset);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_bit_le);
|
|
|
|
#endif
|
|
|
|
#endif /* __BIG_ENDIAN */
|