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

From: Joe Perches
Date: Tue Aug 06 2019 - 17:16:00 EST


On Tue, 2019-08-06 at 21:27 +0200, Rikard Falkeborn wrote:
> On Wed, Aug 07, 2019 at 12:19:36AM +0900, Masahiro Yamada wrote:
> > How about this?
> > #define GENMASK_INPUT_CHECK(high, low) \
> > BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
> > __builtin_constant_p((low) > (high)), (low) > (high), 0))
> Thanks for the feedback, your version looks much cleaner than mine. I
> *think* I had a reason for using __is_constexpr() instead of
> __builtin_constant_p but I'll try a full rebuild to see if something
> comes up.

Perhaps a statement expression so high and low aren't possibly
evaluated multiple times?

#define GENMASK_INPUT_CHECK(high, low) \
({ \
typeof(high) _high = high; \
typeof(low) _low = low; \
BUILD_BUG_ON_ZERO(__builtin_constant_p(_low > _high, \
_low > _high, \
0)) \
})