Re: [PATCH 4/6] Implement a general log2 facility in the kernel

David Howells
2006-09-13T16:51:16Z

Luck, Tony <tony.luck@intel.com> wrote:

> > This facility provides three entry points:
> >
> > log2() Log base 2 of u32
> > ll_log2() Log base 2 of u64
> > long_log2() Log base 2 of unsigned long
>
> The names are rather counter-intuitive. "ll" sounds like "long long", so
> why does it opearte on *unsigned* 64-bit? Ditto for "long_log2()".
> Perhaps they should be log2_u32(), log2_u64(), etc.

Well, given that you can't represent a log of a negative number, I'm not sure
it matters.

Note that long_log2() already exists and is of arch-dependent size.

> Even better if someone can come up with the right pre-processor magic
> using sizeof/typeof so that you could just use "log2(any type)"

I then end up with 32 of these per usage if I pass in a definite 32-bit value:

warning: comparison is always false due to limited range of data type

because of the:

n >= (1ULL << 63) ? 63 : \
n >= (1ULL << 62) ? 62 : \
n >= (1ULL << 61) ? 61 : \
...

in ll_log2().

David