Messages in this thread | | | From | David Laight <> | Subject | RE: [PATCH 08/22] bitops: introduce MANY_BITS() macro | Date | Tue, 10 May 2022 17:54:13 +0000 |
| |
From: Alexei Starovoitov > Sent: 10 May 2022 17:51 ... > +/* Return: nonzero if 2 or more bits are set */ > +#define MANY_BITS(n) ((n) & ((n) - 1))
You can't have a macro that expands its argument twice.
... > > static inline __attribute__((const)) > > bool is_power_of_2(unsigned long n) > > { > > - return (n != 0 && ((n & (n - 1)) == 0)); > > + return n != 0 && !MANY_BITS(n); > > } > > Please don't. Open coded version is much easier to read.
Especially if you remove all the spare parenthesis. return n && !(n & (n - 1));
I bet a lot of callers know the value is non-zero.
I suspect you'll find at least one caller that uses is_power_of_2() assuming it is !(n & (n - 1)) and so is wrong for zero.
David
- Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
| |