Re: [PATCH] linux/bits.h: Add compile time sanity check of GENMASK inputs

From: Rikard Falkeborn
Date: Thu Aug 01 2019 - 19:04:02 EST


On Wed, Jul 31, 2019 at 07:57:48PM -0700, Joe Perches wrote:
> On Thu, 2019-08-01 at 11:50 +0900, Masahiro Yamada wrote:
> > On Thu, Aug 1, 2019 at 4:04 AM Rikard Falkeborn
> > <rikard.falkeborn@xxxxxxxxx> wrote:
> > > GENMASK() and GENMASK_ULL() are supposed to be called with the high bit
> > > as the first argument and the low bit as the second argument. Mixing
> > > them will return a mask with zero bits set.
> >
> > This is getting cluttered with so many parentheses.
> >
> > One way of clean-up is to rename the current macros as follows:
> >
> > GENMASK() -> __GENMASK()
> > GENMASK_UL() -> __GENMASK_ULL()
> >
> > Then,
> >
> > #define GENMASK(h, l) (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
> > #define GENMASK_ULL(h, l) (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
>
> Much nicer. It may be better still to use avoid
> multiple dereferences of each argument.

Much nicer indeed, I changed it accordingly. There are no multiple
dererences of the arguments. GENMASK_INPUT_CHECK() and __is_constexpr()
both use sizeof() on the input arguments, which does not evaluate the
argument (unless the argument is a VLA, which is not allowed).

> Also it'd be useful to rename h and l to something like
> high_bit and low_bit or high and low.

Agreed.