Re: [PATCH 1/4] bitops: Add single_bit_set()

From: Andy Shevchenko
Date: Tue Nov 23 2021 - 05:47:41 EST


On Tue, Nov 23, 2021 at 10:42:45AM +0000, David Laight wrote:
> From: Vaittinen, Matti
> > Sent: 22 November 2021 13:19
> >
> > On 11/22/21 14:57, Andy Shevchenko wrote:
> > > On Mon, Nov 22, 2021 at 12:42:21PM +0000, Vaittinen, Matti wrote:
> > >> On 11/22/21 13:28, Andy Shevchenko wrote:
> > >>> On Mon, Nov 22, 2021 at 01:03:25PM +0200, Matti Vaittinen wrote:
> > >
> > > What do you mean by this?
> > >
> > > hweight() will return you the number of the non-zero elements in the set.
> >
> > Exactly. The function I added did only check if given set of bits had
> > only one bit set.
>
> Checking for exactly one bit can use the (x & (x - 1)) check on
> non-zero values - which may even be better on some cpus with a
> popcnt instruction.

In the discussed case the value pretty much can be 0, meaning you have
to add an additional test which I believe diminishes all efforts for
the is_power_of_2() call.

--
With Best Regards,
Andy Shevchenko