Re: [PATCH] Add BUILD_BUG_ON_NOT_POWER_OF_2()

From: Stefan Richter
Date: Thu Jan 07 2010 - 12:14:55 EST


Robert P. J. Day wrote:
> 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. :-)

It's merely about math, not about semantics. Plus, its application is
restricted to build-time checks (of defined constants) anyway.

Hence I would argue that "check at build time whether a defined
bitmask's Hamming weight is 1; abort build if it isn't" can be
legitimately and sufficiently readably implemented by means of the new
BUILD_BUG_ON_NOT_POWER_OF_2().
--
Stefan Richter
-=====-==-=- ---= --===
http://arcgraph.de/sr/
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/