[Mesa-dev] [PATCH 2/2] nir: Lower ldexp to arithmetic.

Kenneth Graunke kenneth at whitecape.org
Wed Jan 20 12:19:59 PST 2016


On Tuesday, January 19, 2016 10:14:18 PM PST Kenneth Graunke wrote:
> This is a port of Matt's GLSL IR lowering pass to NIR.  It's required
> because we translate SPIR-V directly to NIR, bypassing GLSL IR.
> 
> I haven't introduced a lower_ldexp flag, as I believe all current NIR
> consumers would set the flag.  i965 wants this, vc4 doesn't implement
> this feature, and st_glsl_to_tgsi currently lowers ldexp
> unconditionally anyway.
> 
> Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
> ---
>  src/glsl/nir/nir_opt_algebraic.py | 63 ++++++++++++++++++++++++++++++++++++
+++
>  1 file changed, 63 insertions(+)
> 
> diff --git a/src/glsl/nir/nir_opt_algebraic.py b/src/glsl/nir/
nir_opt_algebraic.py
> index 7745b76..0976ed6 100644
> --- a/src/glsl/nir/nir_opt_algebraic.py
> +++ b/src/glsl/nir/nir_opt_algebraic.py
> @@ -1,4 +1,5 @@
>  #! /usr/bin/env python
> +# -*- encoding: utf-8 -*-
>  #
>  # Copyright (C) 2014 Intel Corporation
>  #
> @@ -265,6 +266,68 @@ for op in ['flt', 'fge', 'feq', 'fne',
>         ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
>     ]
>  
> +def ldexp_to_arith(x, exp):
> +   """
> +   Translates
> +      ldexp x exp
> +   into
> +
> +      extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
> +      resulting_biased_exp = extracted_biased_exp + exp;
> +
> +      if (resulting_biased_exp < 1) {
> +         return copysign(0.0, x);
> +      }
> +
> +      return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
> +                         lshift(i2u(resulting_biased_exp), exp_shift));
> +
> +   which we can't actually implement as such, since NIR doesn't have
> +   vectorized if-statements. We actually implement it without branches
> +   using conditional-select:
> +
> +      extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
> +      resulting_biased_exp = extracted_biased_exp + exp;
> +
> +      is_not_zero_or_underflow = gequal(resulting_biased_exp, 1);
> +      x = csel(is_not_zero_or_underflow, x, copysign(0.0f, x));
> +      resulting_biased_exp = csel(is_not_zero_or_underflow,
> +                                  resulting_biased_exp, 0);
> +
> +      return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
> +                         lshift(i2u(resulting_biased_exp), exp_shift));
> +   """
> +
> +   sign_mask = 0x80000000
> +   exp_shift = 23
> +   exp_width = 8
> +
> +   # Extract the biased exponent from <x>.
> +   extracted_biased_exp = ('ushr', ('iabs', x), exp_shift)

This should be fabs, not iabs:

+   extracted_biased_exp = ('ushr', ('fabs', x), exp_shift)

Then it works fine.  With that change, un-NAK!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20160120/a603b323/attachment.sig>


More information about the mesa-dev mailing list