From: virtuoso@slind.org (Alexander Shishkin)
Subject: [ltt-dev] [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2
Date: Sat, 21 Aug 2010 22:53:37 +0300 [thread overview]
Message-ID: <20100821195337.GH3003@shisha.kicks-ass.net> (raw)
In-Reply-To: <20100821194428.GA10075@core2.telecom.by>
On Sun, Aug 21, 2010 at 10:44:28 +0300, Alexey Dobriyan wrote:
> On Sat, Aug 21, 2010 at 02:40:15PM -0400, Mathieu Desnoyers wrote:
> > * Alexey Dobriyan (adobriyan at gmail.com) wrote:
> > > On Sat, Aug 21, 2010 at 10:17:51AM -0400, Mathieu Desnoyers wrote:
> > > > +/* Force a compilation error if condition is constant and not a power of 2 */
> > > > +#define MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2(n) \
> > > > + MAYBE_BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
> > >
> > > Sorry, this is tasteless macro.
> >
> > Let's look at the surrounding where I added this macro in kernel.h:
> >
> >
> > /* Force a compilation error if condition is true */
> > #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
> >
> > /* Force a compilation error if condition is constant and true */
> > #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
> >
> > /* Force a compilation error if a constant expression is not a power of 2 */
> > #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
> > BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
> >
> > /* Force a compilation error if condition is true, but also produce a
> > result (of value 0 and type size_t), so the expression can be used
> > e.g. in a structure initializer (or where-ever else comma expressions
> > aren't permitted). */
> > #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
> > #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
> >
> >
> > So I am guessing that you plan to rewrite all of these ?
>
> Of course not.
>
> > Or perharps you have other suggestions ?
>
> Learn that n & (n - 1) is idiomatic way to check for power of two in C.
> Done that, encoding that information in identifier won't make sense.
I don't get it, do you suggest to call it BUILD_BUG_ON_SMART_PANTS() and have
people look it up every time to see what it does and do a git-blame to see
who's sense of taste interferes with coding or do you suggest to paste
the "BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))" every time so that
people learn how to write C?
Regards,
--
Alex
next prev parent reply other threads:[~2010-08-21 19:53 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-21 14:17 [ltt-dev] [PATCH for -tip 0/2] Generic Alignment API Mathieu Desnoyers
2010-08-21 14:17 ` [ltt-dev] [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2 Mathieu Desnoyers
2010-08-21 14:17 ` Mathieu Desnoyers
2010-08-21 14:17 ` Mathieu Desnoyers
2010-08-21 18:26 ` Alexey Dobriyan
2010-08-21 18:40 ` Mathieu Desnoyers
2010-08-21 19:44 ` Alexey Dobriyan
2010-08-21 19:53 ` Alexander Shishkin [this message]
2010-08-22 19:27 ` Andi Kleen
2010-08-22 19:38 ` Mathieu Desnoyers
2010-08-22 20:16 ` Anca Emanuel
2010-08-22 21:03 ` Mathieu Desnoyers
2010-08-21 14:17 ` [ltt-dev] [PATCH for -tip 2/2] Create generic alignment API (v9) Mathieu Desnoyers
2010-08-21 14:17 ` Mathieu Desnoyers
2010-08-21 14:17 ` Mathieu Desnoyers
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=20100821195337.GH3003@shisha.kicks-ass.net \
--to=virtuoso@slind.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