On Tue, Aug 06, 2019 at 02:15:54PM -0700, Joe Perches wrote: > 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)) \ > }) > >
That doesn't work I think (even after adding __builtin_choose_expr). Even so, high and low are not evaluated multiple times (they're not evaluated at all in GENMASK_INPUT_CHECK, if they were, the arguments would be evaluated twice since they're evaluated in __GENMASK as well. __builtin_constant_p does not seem to evaluate it's expression (even though I didn't manage to find that spelled out in the docs, but since __builtin_constant_p is evaluated at compile time it makes sense that it doesn't), and __builtin_choose_expr does not evaluate the operand that is not chosen (this is actually in the docs). Even if it was, BUIlD_BUG_ON_ZERO uses sizeof its argument, which is evaluated at compile time (unless the argument is a VLA). So this should be safe.