Re: [PATCH v3] bitops: Fix shift overflow in GENMASK macros

From: Maxime Coquelin
Date: Wed Nov 05 2014 - 10:48:15 EST



On 11/05/2014 12:10 PM, Rasmus Villemoes wrote:
On Tue, Nov 04 2014, Maxime COQUELIN <maxime.coquelin@xxxxxx> wrote:

-#define GENMASK(h, l) (((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
-#define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
+#define GENMASK(h, l) \
+ ((~0UL >> (BITS_PER_LONG - ((h) - (l) + 1))) << (l))
+
+#define GENMASK_ULL(h, l) \
+ ((~0ULL >> (BITS_PER_LONG_LONG - ((h) - (l) + 1))) << (l))
Slightly bikeshedding here, but may I suggest spelling it

(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))

? This will ensure the arguments are expanded once each, and will, IMHO,
DTRT if/when l > h (namely, yield 0).
That's a good point.

Peter, what is your view?

Kind regards,
Maxime

Rasmus

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/