[Mesa-dev] [RFC 4/7] mesa: Helper functions for counting set bits in a mask

Toni Lönnberg toni.lonnberg at intel.com
Tue Oct 16 08:04:37 UTC 2018


I tried to find a helper function for bit count already available but apparently I missed it :) No need for a new one if one exists already indeed.

-Toni

On Mon, Oct 15, 2018 at 07:59:58PM +0000, Roland Scheidegger wrote:
> Am 15.10.18 um 15:19 schrieb Toni Lönnberg:
> > ---
> >  src/util/bitscan.h | 25 +++++++++++++++++++++++++
> >  1 file changed, 25 insertions(+)
> > 
> > diff --git a/src/util/bitscan.h b/src/util/bitscan.h
> > index dc89ac9..cdfecaf 100644
> > --- a/src/util/bitscan.h
> > +++ b/src/util/bitscan.h
> > @@ -112,6 +112,31 @@ u_bit_scan64(uint64_t *mask)
> >     return i;
> >  }
> >  
> > +/* Count bits set in mask */
> > +static inline int
> > +u_count_bits(unsigned *mask)
> I don't think you'd want to pass a pointer.
> 
> Besides, I don't think we need another set of functions for this.
> src/util/u_math.h already has util_bitcount64 and util_bitcount which do
> the same thing.
> (Although I don't know which one is better, util_bitcount looks like it
> would be potentially faster with just very few bits set, but with
> "random" uint/uint64 it certainly would seem the new one is better. But
> in any case, can't beat the cpu popcount instruction...)
> 
> Roland
> 
> 
> > +{
> > +   unsigned v = *mask;
> > +   int c;
> > +   v = v - ((v >> 1) & 0x55555555);
> > +   v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
> > +   v = (v + (v >> 4)) & 0xF0F0F0F;
> > +   c = (int)((v * 0x1010101) >> 24);
> > +   return c;
> > +}
> > +
> > +static inline int
> > +u_count_bits64(uint64_t *mask)
> > +{
> > +   uint64_t v = *mask;
> > +   int c;
> > +   v = v - ((v >> 1) & 0x5555555555555555ull);
> > +   v = (v & 0x3333333333333333ull) + ((v >> 2) & 0x3333333333333333ull);
> > +   v = (v + (v >> 4)) & 0xF0F0F0F0F0F0F0Full;
> > +   c = (int)((v * 0x101010101010101ull) >> 56);
> > +   return c;
> > +}
> > +
> >  /* Determine if an unsigned value is a power of two.
> >   *
> >   * \note
> > 
> 


More information about the mesa-dev mailing list