Re: [PATCH] Introduce a boolean "single_bit_set" function.

Robert P. J. Day
2009-04-24T10:41:49Z

On Thu, 23 Apr 2009, Andrew Morton wrote:

> On Thu, 23 Apr 2009 12:57:11 -0700
> David Daney <ddaney@caviumnetworks.com> wrote:
>
> > > +static inline __attribute__((const))
> > > +bool single_bit_set(unsigned long n)
> > > +{
> > > + return (n != 0 && ((n & (n - 1)) == 0));
> > > +}
> > > +
> > > +
> >
> >
> > It would be nice to be able to override this per architecture.
> >
> > For example a more efficient implementation on CPUs that have a
> > population count instruction (__builtin_popcountl()) might be:
> >
> > static inline __attribute__((const))
> > bool singe_bit_set(unsigned long n)
> > {
> > return __builtin_popcountl(n) == 1;
> > }
>
> Already done, via hweight_long().

so it would be a simple matter to define the bit set boolean in
terms of hweight_long(), yes? so what about, in bitops.h:

static inline bool
exactly_one_bit_set(unsigned long w)
{
return hweight_long(w) == 1;
}

static inline bool
more_than_one_bit_set(unsigned long w)
{
return hweight_long(w) > 1;
}

or something to that effect, *if* people think it's worth it.
obviously, none of the above is strictly necessary, but it would make
a lot of code semantically cleaner.

rday

p.s. i notice that, even in a single header file like bitops.h, there
is a mixture of both "inline" and "__inline__". what's the
recommended choice these days?

--