From: Andrew Morton <akpm@linux-foundation.org>
To: Roland Dreier <rdreier@cisco.com>
Cc: linux-kernel@vger.kernel.org,
Bart Van Assche <bart.vanassche@gmail.com>,
David Dillow <dave@thedillows.org>
Subject: Re: [PATCH] log2.h: Macro-ize is_power_of_2() for use in BUILD_BUG_ON
Date: Wed, 6 Jan 2010 12:33:06 -0800 [thread overview]
Message-ID: <20100106123306.ac85e557.akpm@linux-foundation.org> (raw)
In-Reply-To: <ada3a2jugqu.fsf@roland-alpha.cisco.com>
On Wed, 06 Jan 2010 12:21:13 -0800
Roland Dreier <rdreier@cisco.com> wrote:
> When code relies on a constant being a power of 2:
>
> #define FOO 512 /* must be a power of 2 */
>
> it would be nice to be able to do:
>
> BUILD_BUG_ON(!is_power_of_2(FOO));
>
> However applying an inline function does not result in a compile-time
> constant that can be used with BUILD_BUG_ON(), so trying that gives
> results in:
>
> error: bit-field '<anonymous>' width not an integer constant
>
> We can fix this by changing is_power_of_2() to a macro; we leave the
> inline function for the non-constant case, to avoid evaluating the
> parameter more than once. (gcc does not accept a multi-statement
> expression like "({ unsigned long __n = n; ... })" as a compile-time
> constant so that solution doesn't work)
>
> Signed-off-by: Roland Dreier <rolandd@cisco.com>
> ---
> This is somewhat of an RFC -- I'm a bit undecided whether it's really
> worth making this change. It's prompted by
> http://www.mail-archive.com/linux-rdma@vger.kernel.org/msg01941.html and
> http://www.mail-archive.com/linux-rdma@vger.kernel.org/msg01950.html
> which do ugly things to work around is_power_of_2 not being usable in
> BUILD_BUG_ON().
>
> On the other hand maybe just
>
> /* FOO must be a power of 2 */
> #define FOO_SHIFT 9
> #define FOO (1 << FOO_SHIFT)
>
> is good enough.
>
> include/linux/log2.h | 9 ++++++++-
> 1 files changed, 8 insertions(+), 1 deletions(-)
>
> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index 25b8086..248f69c 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
> @@ -49,11 +49,18 @@ int __ilog2_u64(u64 n)
> */
>
> static inline __attribute__((const))
> -bool is_power_of_2(unsigned long n)
> +bool __is_power_of_2(unsigned long n)
> {
> return (n != 0 && ((n & (n - 1)) == 0));
> }
>
> +#define is_power_of_2(n) \
> +( \
> + __builtin_constant_p(n) ? \
> + (((n) != 0) && (((n) & ((n) - 1)) == 0)) : \
> + __is_power_of_2(n) \
> +)
> +
We've had recurring struggles with various versions of gcc screwing up
constructs of this form and trying to emit the non-constant code when
the arg was clearly a compile-time constant. One episode which comes
to mind was when we made changes to kmalloc().
Of course, that might not bite us in this case - it would need a lot of
coverage testing to find out.
Perhaps we can avoid worrying about that via
#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
BUILD_BUG_ON((n != 0 && ((n & (n - 1)) == 0)))
?
next prev parent reply other threads:[~2010-01-06 20:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-06 20:21 [PATCH] log2.h: Macro-ize is_power_of_2() for use in BUILD_BUG_ON Roland Dreier
2010-01-06 20:33 ` Andrew Morton [this message]
2010-01-06 20:44 ` Roland Dreier
2010-01-06 21:15 ` David Dillow
2010-01-06 21:42 ` Andrew Morton
2010-01-06 23:02 ` [PATCH] Add BUILD_BUG_ON_NOT_POWER_OF_2() Roland Dreier
2010-01-06 23:09 ` Andrew Morton
2010-01-07 7:33 ` Bart Van Assche
2010-01-07 7:51 ` Roland Dreier
2010-01-07 8:36 ` Robert P. J. Day
2010-01-07 16:45 ` Stefan Richter
2010-01-06 21:23 ` [PATCH] log2.h: Macro-ize is_power_of_2() for use in BUILD_BUG_ON Roland Dreier
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=20100106123306.ac85e557.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=bart.vanassche@gmail.com \
--cc=dave@thedillows.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rdreier@cisco.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.