public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Robert P. J. Day" <rpjday@crashcourse.ca>
To: Roland Dreier <rdreier@cisco.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org,
	Bart Van Assche <bart.vanassche@gmail.com>,
	David Dillow <dave@thedillows.org>
Subject: Re: [PATCH] Add BUILD_BUG_ON_NOT_POWER_OF_2()
Date: Thu, 7 Jan 2010 03:36:09 -0500 (EST)	[thread overview]
Message-ID: <alpine.LFD.2.00.1001070332060.16874@localhost> (raw)
In-Reply-To: <adaeim2supb.fsf_-_@roland-alpha.cisco.com>

On Wed, 6 Jan 2010, Roland Dreier 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
>
> As suggested by akpm, rather than monkeying around with
> is_power_of_2() and risking gcc warts about constant expressions,
> just create a macro BUILD_BUG_ON_NOT_POWER_OF_2() to encapsulate
> this common requirement.

  my normal pedantry coming to the surface, but can we at least assume
that people will use this test to *legitimately* test whether or not
something is a power of two, and not that there is a single bit set
(in the case of mask bits where all settings must be mutually
exclusive)?

  there's a lot of this sort of thing throughout the kernel:

    (n) != 0 && (((n) & ((n) - 1))

but it's sometimes unclear whether someone is testing for a) power of
two, or b) single bit set.  if you're going to introduce that kind of
BUILD BUG (which is a good idea), let's try to not immediately abuse
it semantically. :-)

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================

  parent reply	other threads:[~2010-01-07  8:36 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
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 [this message]
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=alpine.LFD.2.00.1001070332060.16874@localhost \
    --to=rpjday@crashcourse.ca \
    --cc=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox