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

From: Bjorn Helgaas
Date: Thu Dec 05 2019 - 17:30:51 EST


You got the "n" on "down" in the subject, but still missing "of" ;)

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/

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).

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.

> -#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?

Bjorn