linux/bitmap.h: Improve BITMAP_{LAST,FIRST}_WORD_MASK
Rasmus Villemoes
reviewerPATCH 1/1 linux/bitmap.h: Improve BITMAP_{LAST,FIRST}_WORD_MASK
The macro BITMAP_LAST_WORD_MASK can be implemented without a conditional, which will generally lead to slightly better generated code (221 bytes saved for allmodconfig-GCOV_KERNEL, ~2k with GCOV_KERNEL). As a small bonus, this also ensures that the nbits parameter is expanded exactly once.
In BITMAP_FIRST_WORD_MASK, if start is signed gcc is technically allowed to assume it is positive (or divisible by BITS_PER_LONG), and hence just do the simple mask. It doesn't seem to use this, and even on an architecture like x86 where the shift only depends on the lower 5 or 6 bits, and these bits are not affected by the signedness of the expression, gcc still generates code to compute the C99 mandated value of start % BITS_PER_LONG. So just use a mask explicitly, also for consistency with BITMAP_LAST_WORD_MASK.
--- include/linux/bitmap.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
include/linux/bitmap.h +2 -6
@@ -172,12 +172,8 @@ extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned intextern int bitmap_print_to_pagebuf (bool list,char *buf,const unsigned long *maskp,int nmaskbits); -#define BITMAP_FIRST_WORD_MASK (start) (~0UL << ((start) % BITS_PER_LONG)) -#define BITMAP_LAST_WORD_MASK (nbits) \ -( \ - ((nbits) % BITS_PER_LONG) ? \ - (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ -) +#define BITMAP_FIRST_WORD_MASK (start) (~0UL << ((start) & (BITS_PER_LONG - 1))) +#define BITMAP_LAST_WORD_MASK (nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))#define small_const_nbits (nbits) \ (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
Signature · 1 line
2.1.3
Looks good.