[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230330104243.2120761-3-jani.nikula@intel.com>
Date: Thu, 30 Mar 2023 13:42:41 +0300
From: Jani Nikula <jani.nikula@...el.com>
To: linux-kernel@...r.kernel.org
Cc: intel-gfx@...ts.freedesktop.org, dri-devel@...ts.freedesktop.org,
jani.nikula@...el.com, Andrew Morton <akpm@...ux-foundation.org>,
Christian König <christian.koenig@....com>,
David Gow <davidgow@...gle.com>
Subject: [PATCH 2/4] log2: have is_power_of_2() support bigger types than unsigned long
is_power_of_2() does not work properly for e.g. u64 in 32-bit
builds. Choose an unsigned long long version if the argument is bigger
than unsigned long.
Cc: Andrew Morton <akpm@...ux-foundation.org>
Cc: Christian König <christian.koenig@....com>
Cc: David Gow <davidgow@...gle.com>
Link: https://lore.kernel.org/r/20230329065532.2122295-2-davidgow@google.com
Signed-off-by: Jani Nikula <jani.nikula@...el.com>
---
include/linux/log2.h | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/include/linux/log2.h b/include/linux/log2.h
index 19e773116ae3..4027d1eecd7f 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -35,6 +35,18 @@ int __ilog2_u64(u64 n)
#define __IS_POWER_OF_2(n) ((n) != 0 && (((n) & ((n) - 1)) == 0))
+static inline __attribute__((const))
+bool __is_power_of_2_ull(unsigned long long n)
+{
+ return __IS_POWER_OF_2(n);
+}
+
+static inline __attribute__((const))
+bool __is_power_of_2(unsigned long n)
+{
+ return __IS_POWER_OF_2(n);
+}
+
/**
* is_power_of_2() - check if a value is a power of two
* @n: the value to check
@@ -43,11 +55,10 @@ int __ilog2_u64(u64 n)
* *not* considered a power of two.
* Return: true if @n is a power of 2, otherwise false.
*/
-static inline __attribute__((const))
-bool is_power_of_2(unsigned long n)
-{
- return __IS_POWER_OF_2(n);
-}
+#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))
/**
* __roundup_pow_of_two() - round up to nearest power of two
--
2.39.2
Powered by blists - more mailing lists