2010-08-22 19:27:15

by Andi Kleen

[permalink] [raw]
Subject: Re: [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2

> +/* 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))

Looks super-ugly. IMHO just writing MAYBE_BUILD_BUG_ON(!n || n & (n - 1)) directly
would be clear enough. If you really think that's unclear define a generic
is_power_of_two() macro.

-Andi


2010-08-22 19:38:25

by Mathieu Desnoyers

[permalink] [raw]
Subject: Re: [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2

* Andi Kleen ([email protected]) 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))
>
> Looks super-ugly. IMHO just writing MAYBE_BUILD_BUG_ON(!n || n & (n - 1)) directly
> would be clear enough. If you really think that's unclear define a generic
> is_power_of_two() macro.

There is already a is_power_of_two macro in log2.h, but I fear it might
incorrectly interact with "MAYBE_BUILD_BUG_ON" (for some reason passing the
constant result of a static inline is not treated as a constant by the macro,
and thus it always "passes" the test).

So if everyone object to this new macro, I'd be tempted to just go with your
suggestion. However the fact that we already have BUILD_BUG_ON_NOT_POWER_OF_2(n)
made me think that some people prefer to have it done as a macro.

Other opinions ?

Thanks,

Mathieu


--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

2010-08-22 20:16:47

by Anca Emanuel

[permalink] [raw]
Subject: Re: [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2

I think about some test at the last bit if it is on, the number is not
a power of 2

On Sun, Aug 22, 2010 at 10:38 PM, Mathieu Desnoyers
<[email protected]> wrote:
> * Andi Kleen ([email protected]) 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))
>>
>> Looks super-ugly. IMHO just writing MAYBE_BUILD_BUG_ON(!n || n & (n - 1)) directly
>> would be clear enough. If you really think that's unclear define a generic
>> is_power_of_two() macro.
>
> There is already a is_power_of_two macro in log2.h, but I fear it might
> incorrectly interact with "MAYBE_BUILD_BUG_ON" (for some reason passing the
> constant result of a static inline is not treated as a constant by the macro,
> and thus it always "passes" the test).
>
> So if everyone object to this new macro, I'd be tempted to just go with your
> suggestion. However the fact that we already have BUILD_BUG_ON_NOT_POWER_OF_2(n)
> made me think that some people prefer to have it done as a macro.
>
> Other opinions ?
>
> Thanks,
>
> Mathieu
>
>
> --
> Mathieu Desnoyers
> Operating System Efficiency R&D Consultant
> EfficiOS Inc.
> http://www.efficios.com
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at ?http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at ?http://www.tux.org/lkml/
>

2010-08-22 21:03:48

by Mathieu Desnoyers

[permalink] [raw]
Subject: Re: [PATCH for -tip 1/2] kernel.h: add MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2

* Anca Emanuel ([email protected]) wrote:
> I think about some test at the last bit if it is on, the number is not
> a power of 2

Can you give an example, along with the types you have in mind ?

Thanks,

Mathieu

>
> On Sun, Aug 22, 2010 at 10:38 PM, Mathieu Desnoyers
> <[email protected]> wrote:
> > * Andi Kleen ([email protected]) 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))
> >>
> >> Looks super-ugly. IMHO just writing MAYBE_BUILD_BUG_ON(!n || n & (n - 1)) directly
> >> would be clear enough. If you really think that's unclear define a generic
> >> is_power_of_two() macro.
> >
> > There is already a is_power_of_two macro in log2.h, but I fear it might
> > incorrectly interact with "MAYBE_BUILD_BUG_ON" (for some reason passing the
> > constant result of a static inline is not treated as a constant by the macro,
> > and thus it always "passes" the test).
> >
> > So if everyone object to this new macro, I'd be tempted to just go with your
> > suggestion. However the fact that we already have BUILD_BUG_ON_NOT_POWER_OF_2(n)
> > made me think that some people prefer to have it done as a macro.
> >
> > Other opinions ?
> >
> > Thanks,
> >
> > Mathieu
> >
> >
> > --
> > Mathieu Desnoyers
> > Operating System Efficiency R&D Consultant
> > EfficiOS Inc.
> > http://www.efficios.com
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to [email protected]
> > More majordomo info at ?http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at ?http://www.tux.org/lkml/
> >

--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com