[Mesa-dev] [PATCH v2 4/5] util: Add u_bit_count64 and u_next_power_of_two

Marek Olšák maraeo at gmail.com
Wed Jul 18 17:24:47 UTC 2018


On Wed, Jul 18, 2018 at 1:05 PM, Rhys Perry <pendingchaos02 at gmail.com> wrote:
> Signed-off-by: Rhys Perry <pendingchaos02 at gmail.com>
> ---
>  src/util/bitscan.h | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
>
> diff --git a/src/util/bitscan.h b/src/util/bitscan.h
> index dc89ac93f2..cae61d3f71 100644
> --- a/src/util/bitscan.h
> +++ b/src/util/bitscan.h
> @@ -286,6 +286,34 @@ u_bit_consecutive64(unsigned start, unsigned count)
>     return (((uint64_t)1 << count) - 1) << start;
>  }
>
> +/* Returns the number of bits set.
> + *
> + * based on
> + * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
> + */
> +static inline unsigned
> +u_bit_count64(uint64_t val)
> +{
> +#ifdef __POPCNT__
> +   return _mm_popcnt_u64(v);
> +#else
> +   unsigned result;
> +   for (result = 0; val; result++)
> +      val &= val - 1; /* clear the least significant bit set */
> +   return result;
> +#endif
> +}

There is also util_bitcount64.

> +
> +/* Round the input to the next power of two.
> + * Zero is rounded to one.
> + */
> +static inline uint64_t
> +u_next_power_of_two(unsigned val)
> +{
> +   bool power_of_two_nonzero = util_is_power_of_two_or_zero64(val) && val;
> +   return power_of_two_nonzero ? val : ((uint64_t)1 << util_last_bit64(val));
> +}
> +

val is unsigned (32 bits), you are treating it as 64-bit.

There are also util_next_power_of_two and util_next_power_of_two64.

Marek

>
>  #ifdef __cplusplus
>  }
> --
> 2.14.4
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list