[Mesa-dev] [PATCH] util: don't use __builtin_clz unconditionally

Roland Scheidegger sroland at vmware.com
Wed Aug 1 03:18:14 UTC 2018


Am 01.08.2018 um 04:54 schrieb Marek Olšák:
> From: Marek Olšák <marek.olsak at amd.com>
> 
> This fixes the build if __builtin_clz is unsupported.
> ---
>  src/util/half_float.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/src/util/half_float.c b/src/util/half_float.c
> index 588f050d924..2eff2c84f51 100644
> --- a/src/util/half_float.c
> +++ b/src/util/half_float.c
> @@ -208,21 +208,31 @@ uint8_t _mesa_half_to_unorm8(uint16_t val)
>    * Takes a uint16_t, divides by 65536, converts the infinite-precision
>    * result to fp16 with round-to-zero. Used by the ASTC decoder.
>    */
>  uint16_t _mesa_uint16_div_64k_to_half(uint16_t v)
>  {
>     /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
>     if (v < 4)
>        return v << 8;
>  
>     /* Count the leading 0s in the uint16_t */
> -   int n = __builtin_clz(v) - (sizeof(unsigned int) - sizeof(uint16_t)) * 8;
> +#ifdef HAVE___BUILTIN_CLZ
> +   int n = __builtin_clz(v) - 16;
> +#else
> +   int n = 16;
> +   for (int i = 15; i >= 0; i--) {
> +      if (v & (1 << i)) {
> +         n = 15 - i;
> +         break;
> +      }
> +   }
> +#endif
Not sure why you're not just using util_last_bit (I think it's best to
keep such bit scan hackery in separate util files), but whatever fixes
the compile error, so
Reviewed-by: Roland Scheidegger <sroland at vmware.com>


>  
>     /* Shift the mantissa up so bit 16 is the hidden 1 bit,
>      * mask it off, then shift back down to 10 bits
>      */
>     int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
>  
>     /*  (0{n} 1 X{15-n}) * 2^-16
>      * = 1.X * 2^(15-n-16)
>      * = 1.X * 2^(14-n - 15)
>      * which is the FP16 form with e = 14 - n
> 



More information about the mesa-dev mailing list