Re: [GIT PULL] bitmap changes for v6.2-rc1

From: Linus Torvalds
Date: Fri Dec 23 2022 - 14:19:56 EST


On Fri, Dec 23, 2022 at 10:44 AM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> Honestly, in this case, I think the logical thing to do is "check that
> the upper bits are the same". The way you do that is probably
> something like
>
> !((off) ^ ((nbits)-1) & ~(BITS_PER_LONG-1))

Note that while the above is probably correct (but you always need to
double-check my emailed "something like this" code - I literally write
it in the MUA, and I make mistakes too), I'd never want to see that as
part of one big complex macro.

In fact, I think I am missing a set of parentheses, because '&' has a
higher precedence than '^', so the above is actually buggy.

So I'd much rather see something like this

#define COMPILE_TIME_TRUE(x) (__builtin_constant_p(x) && (x))

#define bits_in_same_word(x,y) \
(!(((x)^(y))&~(BITS_PER_LONG-1)))

#define bitmap_off_in_last_word(nbits,off) \
bits_in_same_word((nbits)-1,off)

#define small_const_nbits_off(nbits, off) \
(__builtin_constant_p(nbits) && (nbits) > 0 && \
COMPILE_TIME_TRUE(bitmap_off_in_last_word(nbits,off)))

where each step does one thing and one thing only, and you don't have
one complicated thing that is hard to read.

And again, don't take my word blindly for the above. I *think* the
above may be correct, but there's a "think" and a "may" there.

Plus I'd still like to hear about where the above would actually
matter and make a code generation difference in real life (compared to
just the simple "optimize the single-word bitmap" case).

Linus