From: Jani Nikula <jani.nikula@intel.com>
To: linux-kernel@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
jani.nikula@intel.com, Chris Wilson <chris@chris-wilson.co.uk>
Subject: [PATCH] log2: make is_power_of_2() integer constant expression when possible
Date: Fri, 1 Mar 2019 14:52:07 +0200 [thread overview]
Message-ID: <20190301125207.30973-1-jani.nikula@intel.com> (raw)
While is_power_of_2() is an inline function and likely gets optimized
for compile time constant arguments, it still doesn't produce an integer
constant expression that could be used in, say, static data
initialization or case labels.
Make is_power_of_2() an integer constant expression when possible,
otherwise using the inline function to avoid multiple evaluation of the
parameter.
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
The alternative would be to define both a function and a macro version
of is_power_of_2(), and let the callers decide what to use.
---
include/linux/log2.h | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/include/linux/log2.h b/include/linux/log2.h
index 2af7f77866d0..035932f52aeb 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -37,6 +37,14 @@ int __ilog2_u64(u64 n)
}
#endif
+#define __IS_POWER_OF_2(__n) ((__n) != 0 && (((__n) & ((__n) - 1)) == 0))
+
+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
@@ -45,11 +53,8 @@ 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 (n != 0 && ((n & (n - 1)) == 0));
-}
+#define is_power_of_2(n) \
+ __builtin_choose_expr(__builtin_constant_p(n), __IS_POWER_OF_2(n), __is_power_of_2(n))
/**
* __roundup_pow_of_two() - round up to nearest power of two
--
2.20.1
next reply other threads:[~2019-03-01 12:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-01 12:52 Jani Nikula [this message]
2019-03-01 13:07 ` [PATCH] log2: make is_power_of_2() integer constant expression when possible Chris Wilson
2019-03-01 20:26 ` Andrew Morton
2019-03-04 10:16 ` Jani Nikula
[not found] ` <201903041956.eLAFVhUK%fengguang.wu@intel.com>
2019-03-04 11:47 ` Jani Nikula
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190301125207.30973-1-jani.nikula@intel.com \
--to=jani.nikula@intel.com \
--cc=akpm@linux-foundation.org \
--cc=chris@chris-wilson.co.uk \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox