[Mesa-dev] [RFC 4/7] mesa: Helper functions for counting set bits in a mask
Roland Scheidegger
sroland at vmware.com
Mon Oct 15 19:59:58 UTC 2018
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