Re: [PATCH v4 7/8] linux/log2.h: Fix 64bit calculations in roundup/down_pow_two()

From: Nicolas Saenz Julienne
Date: Thu Dec 12 2019 - 08:16:43 EST


On Thu, 2019-12-05 at 16:30 -0600, Bjorn Helgaas wrote:
> You got the "n" on "down" in the subject, but still missing "of" ;)

Yes, sorry about that, I tend to re-read what I meant to say instead of what
it's actually written.

> On Tue, Dec 03, 2019 at 12:47:40PM +0100, Nicolas Saenz Julienne wrote:
> > Some users need to make sure their rounding function accepts and returns
> > 64bit long variables regardless of the architecture. Sadly
> > roundup/rounddown_pow_two() takes and returns unsigned longs. It turns
> > out ilog2() already handles 32/64bit calculations properly, and being
> > the building block to the round functions we can rework them as a
> > wrapper around it.
>
> Missing "of" in the function names here.
> s/a wrapper/wrappers/

Noted

> IIUC the point of this is that roundup_pow_of_two() returned
> "unsigned long", which can be either 32 or 64 bits (worth pointing
> out, I think), and many callers need something that returns
> "unsigned long long" (always 64 bits).

I'll update the commit message to be a more explicit.

> It's a nice simplification to remove the "__" variants. Just as a
> casual reader of this commit message, I'd like to know why we had both
> the roundup and the __roundup versions in the first place, and why we
> no longer need both.

So, the commit that introduced it (312a0c170945b) meant to use the '__' variant
as a helper, but, due to the fact this is a header file, some found it and made
use of it. I went over some if the commits introducing '__' usages and none of
them seem to acknowledge its use as opposed to the macro version. I think it's
fair to say it's a case of cargo-culting.

> > -#define roundup_pow_of_two(n) \
> > -( \
> > - __builtin_constant_p(n) ? ( \
> > - (n == 1) ? 1 : \
> > - (1UL << (ilog2((n) - 1) + 1)) \
> > - ) : \
> > - __roundup_pow_of_two(n) \
> > - )
> > +#define roundup_pow_of_two(n) \
> > +( \
> > + (__builtin_constant_p(n) && ((n) == 1)) ? \
> > + 1 : (1ULL << (ilog2((n) - 1) + 1)) \
> > +)
>
> Should the resulting type of this expression always be a ULL, even
> when n==1, i.e., should it be this?
>
> 1ULL : (1ULL << (ilog2((n) - 1) + 1)) \
>
> Or maybe there's no case where that makes a difference?

It should be 1ULL on either case.

Regards,
Nicolas

Attachment: signature.asc
Description: This is a digitally signed message part