[PATCH 3/4] log2: allow use of is_power_of_2() in constant expressions

From: Jani Nikula
Date: Thu Mar 30 2023 - 06:43:47 EST


Even though the static inline functions are __attribute__((const)) you
can't use them in constant expressions. Make is_power_of_2() a constant
expression if possible.

Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Christian König <christian.koenig@xxxxxxx>
Cc: David Gow <davidgow@xxxxxxxxxx>
Signed-off-by: Jani Nikula <jani.nikula@xxxxxxxxx>
---
include/linux/log2.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/linux/log2.h b/include/linux/log2.h
index 4027d1eecd7f..447a85102de0 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -56,9 +56,11 @@ bool __is_power_of_2(unsigned long n)
* Return: true if @n is a power of 2, otherwise false.
*/
#define is_power_of_2(n) \
- __builtin_choose_expr(sizeof(n) > sizeof(unsigned long), \
- __is_power_of_2_ull(n), \
- __is_power_of_2(n))
+ __builtin_choose_expr(__is_constexpr(n), \
+ __IS_POWER_OF_2(n), \
+ __builtin_choose_expr(sizeof(n) > sizeof(unsigned long), \
+ __is_power_of_2_ull(n), \
+ __is_power_of_2(n)))

/**
* __roundup_pow_of_two() - round up to nearest power of two
--
2.39.2