[PATCH 0/4] log2: make is_power_of_2() more generic

Andrew Morton akpm at linux-foundation.org
Thu Mar 30 19:50:41 UTC 2023


On Thu, 30 Mar 2023 13:42:39 +0300 Jani Nikula <jani.nikula at intel.com> wrote:

> is_power_of_2() only works for types <= sizeof(unsigned long) and it's
> also not a constant expression. There are a number of places in kernel
> where is_power_of_2() is called on u64, which fails on 32-bit
> builds. Try to remedy that. While at it, make it a constant expression
> when possible.

Yes, the current `is_power_of_2(unsigned long n)' isn't very general.

But wouldn't all these issues be addressed by simply doing

#define is_power_of_2(n) (n != 0 && ((n & (n - 1)) == 0))

?

(With suitable tweaks to avoid evaluating `n' more than once)




More information about the dri-devel mailing list