lkml.org 
[lkml]   [2022]   [May]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: [PATCH 08/22] bitops: introduce MANY_BITS() macro
Date
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)
\
 
 \ /
  Last update: 2022-05-10 19:55    [W:0.111 / U:1.436 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site