<p dir="ltr"><br>
On Jan 23, 2015 10:37 PM, "Connor Abbott" <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
><br>
> Other than the one comment fix below,<br>
><br>
> Reviewed-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
><br>
> On Fri, Jan 23, 2015 at 7:17 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> > Add a required field to the Opcode class, const_expr, that contains an<br>
> > expression or statement that computes the result of the opcode given known<br>
> > constant inputs. Then take those const_expr's and expand them into a function<br>
> > that takes an opcode and an array of constant inputs and spits out the constant<br>
> > result. This means that when adding opcodes, there's one less place to update,<br>
> > and almost all the opcodes are self-documenting since the information on how to<br>
> > compute the result is right next to the definition.<br>
> ><br>
> > The helper functions in nir_constant_expressions.c were taken from<br>
> > ir_constant_expressions.cpp.<br>
> ><br>
> > v3 Jason Ekstrand <<a href="mailto:jason.ekstrand@iastate.edu">jason.ekstrand@iastate.edu</a>><br>
><br>
> Might want to fix your email address here and a few lines below.</p>
<p dir="ltr">Oops.  I'll fix that.</p>
<p dir="ltr">><br>
> >  - Use mako to generate one function per opcode instead of doing piles of<br>
> >    string splicing<br>
> ><br>
> > v4 Jason Ekstrand <<a href="mailto:jason.ekstrand@iastate.edu">jason.ekstrand@iastate.edu</a>><br>
> >  - More comments and better indentation in the mako<br>
> >  - Add a description of the constant expression language in nir_opcodes.py<br>
> >  - Added nir_constant_expressions.py to EXTRA_DIST in Makefile.am<br>
> ><br>
> > Signed-off-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>><br>
> > ---<br>
> >  src/glsl/Makefile.am                     |   6 +<br>
> >  src/glsl/Makefile.sources                |   1 +<br>
> >  src/glsl/nir/.gitignore                  |   1 +<br>
> >  src/glsl/nir/nir_constant_expressions.h  |  31 ++<br>
> >  src/glsl/nir/nir_constant_expressions.py | 351 +++++++++++++++++++<br>
> >  src/glsl/nir/nir_opcodes.py              | 580 +++++++++++++++++++++----------<br>
> >  6 files changed, 786 insertions(+), 184 deletions(-)<br>
> >  create mode 100644 src/glsl/nir/nir_constant_expressions.h<br>
> >  create mode 100644 src/glsl/nir/nir_constant_expressions.py<br>
> ><br>
> > diff --git a/src/glsl/Makefile.am b/src/glsl/Makefile.am<br>
> > index bbaffbe..8c6c8b9 100644<br>
> > --- a/src/glsl/Makefile.am<br>
> > +++ b/src/glsl/Makefile.am<br>
> > @@ -37,6 +37,7 @@ EXTRA_DIST = tests glcpp/tests README TODO glcpp/README       \<br>
> >         glsl_parser.yy                                  \<br>
> >         glcpp/glcpp-lex.l                               \<br>
> >         glcpp/glcpp-parse.y                             \<br>
> > +       nir/nir_constant_expressions.py                 \<br>
> >         nir/nir_opcodes.py                              \<br>
> >         nir/nir_opcodes_c.py                            \<br>
> >         nir/nir_opcodes_h.py                            \<br>
> > @@ -220,6 +221,7 @@ BUILT_SOURCES =                                             \<br>
> >         glsl_lexer.cpp                                  \<br>
> >         glcpp/glcpp-parse.c                             \<br>
> >         glcpp/glcpp-lex.c                               \<br>
> > +       nir/nir_constant_expressions.c                  \<br>
> >         nir/nir_opcodes.c                               \<br>
> >         nir/nir_opcodes.h                               \<br>
> >         nir/nir_opt_algebraic.c<br>
> > @@ -235,6 +237,10 @@ dist-hook:<br>
> >         $(RM) glcpp/tests/*.out<br>
> >         $(RM) glcpp/tests/subtest*/*.out<br>
> ><br>
> > +nir/nir_constant_expressions.c: nir/nir_opcodes.py nir/nir_constant_expressions.py nir/nir_constant_expressions.h<br>
> > +       $(MKDIR_P) nir;                                                 \<br>
> > +       $(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_constant_expressions.py > $@<br>
> > +<br>
> >  nir/nir_opcodes.h: nir/nir_opcodes.py nir/nir_opcodes_h.py<br>
> >         $(MKDIR_P) nir;                                                 \<br>
> >         $(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_opcodes_h.py > $@<br>
> > diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
> > index dc1c55d..dd76c44 100644<br>
> > --- a/src/glsl/Makefile.sources<br>
> > +++ b/src/glsl/Makefile.sources<br>
> > @@ -14,6 +14,7 @@ LIBGLCPP_GENERATED_FILES = \<br>
> >         $(GLSL_BUILDDIR)/glcpp/glcpp-parse.c<br>
> ><br>
> >  NIR_GENERATED_FILES = \<br>
> > +       $(GLSL_BUILDDIR)/nir/nir_constant_expressions.c \<br>
> >         $(GLSL_BUILDDIR)/nir/nir_opcodes.c \<br>
> >         $(GLSL_BUILDDIR)/nir/nir_opcodes.h \<br>
> >         $(GLSL_BUILDDIR)/nir/nir_opt_algebraic.c<br>
> > diff --git a/src/glsl/nir/.gitignore b/src/glsl/nir/.gitignore<br>
> > index 4c28193..261f64f 100644<br>
> > --- a/src/glsl/nir/.gitignore<br>
> > +++ b/src/glsl/nir/.gitignore<br>
> > @@ -1,3 +1,4 @@<br>
> >  nir_opt_algebraic.c<br>
> >  nir_opcodes.c<br>
> >  nir_opcodes.h<br>
> > +nir_constant_expressions.c<br>
> > diff --git a/src/glsl/nir/nir_constant_expressions.h b/src/glsl/nir/nir_constant_expressions.h<br>
> > new file mode 100644<br>
> > index 0000000..97997f2<br>
> > --- /dev/null<br>
> > +++ b/src/glsl/nir/nir_constant_expressions.h<br>
> > @@ -0,0 +1,31 @@<br>
> > +/*<br>
> > + * Copyright © 2014 Connor Abbott<br>
> > + *<br>
> > + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> > + * copy of this software and associated documentation files (the "Software"),<br>
> > + * to deal in the Software without restriction, including without limitation<br>
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> > + * and/or sell copies of the Software, and to permit persons to whom the<br>
> > + * Software is furnished to do so, subject to the following conditions:<br>
> > + *<br>
> > + * The above copyright notice and this permission notice (including the next<br>
> > + * paragraph) shall be included in all copies or substantial portions of the<br>
> > + * Software.<br>
> > + *<br>
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> > + * IN THE SOFTWARE.<br>
> > + *<br>
> > + * Authors:<br>
> > + *    Connor Abbott (<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>)<br>
> > + *<br>
> > + */<br>
> > +<br>
> > +#include "nir.h"<br>
> > +<br>
> > +nir_const_value nir_eval_const_opcode(nir_op op, unsigned num_components,<br>
> > +                                      nir_const_value *src);<br>
> > diff --git a/src/glsl/nir/nir_constant_expressions.py b/src/glsl/nir/nir_constant_expressions.py<br>
> > new file mode 100644<br>
> > index 0000000..6860ad3<br>
> > --- /dev/null<br>
> > +++ b/src/glsl/nir/nir_constant_expressions.py<br>
> > @@ -0,0 +1,351 @@<br>
> > +#! /usr/bin/python2<br>
> > +template = """\<br>
> > +/*<br>
> > + * Copyright (C) 2014 Intel Corporation<br>
> > + *<br>
> > + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> > + * copy of this software and associated documentation files (the "Software"),<br>
> > + * to deal in the Software without restriction, including without limitation<br>
> > + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> > + * and/or sell copies of the Software, and to permit persons to whom the<br>
> > + * Software is furnished to do so, subject to the following conditions:<br>
> > + *<br>
> > + * The above copyright notice and this permission notice (including the next<br>
> > + * paragraph) shall be included in all copies or substantial portions of the<br>
> > + * Software.<br>
> > + *<br>
> > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> > + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> > + * IN THE SOFTWARE.<br>
> > + *<br>
> > + * Authors:<br>
> > + *    Jason Ekstrand (<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>)<br>
> > + */<br>
> > +<br>
> > +#include <math.h><br>
> > +#include "main/core.h"<br>
> > +#include "nir_constant_expressions.h"<br>
> > +<br>
> > +#if defined(_MSC_VER) && (_MSC_VER < 1800)<br>
> > +static int isnormal(double x)<br>
> > +{<br>
> > +   return _fpclass(x) == _FPCLASS_NN || _fpclass(x) == _FPCLASS_PN;<br>
> > +}<br>
> > +#elif defined(__SUNPRO_CC)<br>
> > +#include <ieeefp.h><br>
> > +static int isnormal(double x)<br>
> > +{<br>
> > +   return fpclass(x) == FP_NORMAL;<br>
> > +}<br>
> > +#endif<br>
> > +<br>
> > +#if defined(_MSC_VER)<br>
> > +static double copysign(double x, double y)<br>
> > +{<br>
> > +   return _copysign(x, y);<br>
> > +}<br>
> > +#endif<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of packSnorm4x8.<br>
> > + */<br>
> > +static uint8_t<br>
> > +pack_snorm_1x8(float x)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL 4.30 spec:<br>
> > +     *<br>
> > +     *    packSnorm4x8<br>
> > +     *    ------------<br>
> > +     *    The conversion for component c of v to fixed point is done as<br>
> > +     *    follows:<br>
> > +     *<br>
> > +     *      packSnorm4x8: round(clamp(c, -1, +1) * 127.0)<br>
> > +     *<br>
> > +     * We must first cast the float to an int, because casting a negative<br>
> > +     * float to a uint is undefined.<br>
> > +     */<br>
> > +   return (uint8_t) (int8_t)<br>
> > +          _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 127.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of packSnorm2x16.<br>
> > + */<br>
> > +static uint16_t<br>
> > +pack_snorm_1x16(float x)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL ES 3.00 spec:<br>
> > +     *<br>
> > +     *    packSnorm2x16<br>
> > +     *    -------------<br>
> > +     *    The conversion for component c of v to fixed point is done as<br>
> > +     *    follows:<br>
> > +     *<br>
> > +     *      packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)<br>
> > +     *<br>
> > +     * We must first cast the float to an int, because casting a negative<br>
> > +     * float to a uint is undefined.<br>
> > +     */<br>
> > +   return (uint16_t) (int16_t)<br>
> > +          _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 32767.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of unpackSnorm4x8.<br>
> > + */<br>
> > +static float<br>
> > +unpack_snorm_1x8(uint8_t u)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL 4.30 spec:<br>
> > +     *<br>
> > +     *    unpackSnorm4x8<br>
> > +     *    --------------<br>
> > +     *    The conversion for unpacked fixed-point value f to floating point is<br>
> > +     *    done as follows:<br>
> > +     *<br>
> > +     *       unpackSnorm4x8: clamp(f / 127.0, -1, +1)<br>
> > +     */<br>
> > +   return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of unpackSnorm2x16.<br>
> > + */<br>
> > +static float<br>
> > +unpack_snorm_1x16(uint16_t u)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL ES 3.00 spec:<br>
> > +     *<br>
> > +     *    unpackSnorm2x16<br>
> > +     *    ---------------<br>
> > +     *    The conversion for unpacked fixed-point value f to floating point is<br>
> > +     *    done as follows:<br>
> > +     *<br>
> > +     *       unpackSnorm2x16: clamp(f / 32767.0, -1, +1)<br>
> > +     */<br>
> > +   return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component packUnorm4x8.<br>
> > + */<br>
> > +static uint8_t<br>
> > +pack_unorm_1x8(float x)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL 4.30 spec:<br>
> > +     *<br>
> > +     *    packUnorm4x8<br>
> > +     *    ------------<br>
> > +     *    The conversion for component c of v to fixed point is done as<br>
> > +     *    follows:<br>
> > +     *<br>
> > +     *       packUnorm4x8: round(clamp(c, 0, +1) * 255.0)<br>
> > +     */<br>
> > +   return (uint8_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 255.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component packUnorm2x16.<br>
> > + */<br>
> > +static uint16_t<br>
> > +pack_unorm_1x16(float x)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL ES 3.00 spec:<br>
> > +     *<br>
> > +     *    packUnorm2x16<br>
> > +     *    -------------<br>
> > +     *    The conversion for component c of v to fixed point is done as<br>
> > +     *    follows:<br>
> > +     *<br>
> > +     *       packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)<br>
> > +     */<br>
> > +   return (uint16_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 65535.0f);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of unpackUnorm4x8.<br>
> > + */<br>
> > +static float<br>
> > +unpack_unorm_1x8(uint8_t u)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL 4.30 spec:<br>
> > +     *<br>
> > +     *    unpackUnorm4x8<br>
> > +     *    --------------<br>
> > +     *    The conversion for unpacked fixed-point value f to floating point is<br>
> > +     *    done as follows:<br>
> > +     *<br>
> > +     *       unpackUnorm4x8: f / 255.0<br>
> > +     */<br>
> > +   return (float) u / 255.0f;<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of unpackUnorm2x16.<br>
> > + */<br>
> > +static float<br>
> > +unpack_unorm_1x16(uint16_t u)<br>
> > +{<br>
> > +    /* From section 8.4 of the GLSL ES 3.00 spec:<br>
> > +     *<br>
> > +     *    unpackUnorm2x16<br>
> > +     *    ---------------<br>
> > +     *    The conversion for unpacked fixed-point value f to floating point is<br>
> > +     *    done as follows:<br>
> > +     *<br>
> > +     *       unpackUnorm2x16: f / 65535.0<br>
> > +     */<br>
> > +   return (float) u / 65535.0f;<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of packHalf2x16.<br>
> > + */<br>
> > +static uint16_t<br>
> > +pack_half_1x16(float x)<br>
> > +{<br>
> > +   return _mesa_float_to_half(x);<br>
> > +}<br>
> > +<br>
> > +/**<br>
> > + * Evaluate one component of unpackHalf2x16.<br>
> > + */<br>
> > +static float<br>
> > +unpack_half_1x16(uint16_t u)<br>
> > +{<br>
> > +   return _mesa_half_to_float(u);<br>
> > +}<br>
> > +<br>
> > +/* Some typed vector structures to make things like src0.y work */<br>
> > +% for type in ["float", "int", "unsigned", "bool"]:<br>
> > +struct ${type}_vec {<br>
> > +   ${type} x;<br>
> > +   ${type} y;<br>
> > +   ${type} z;<br>
> > +   ${type} w;<br>
> > +};<br>
> > +% endfor<br>
> > +<br>
> > +% for name, op in sorted(opcodes.iteritems()):<br>
> > +static nir_const_value<br>
> > +evaluate_${name}(unsigned num_components, nir_const_value *_src)<br>
> > +{<br>
> > +   nir_const_value _dst_val = { { {0, 0, 0, 0} } };<br>
> > +<br>
> > +   ## For each non-per-component input, create a variable srcN that<br>
> > +   ## contains x, y, z, and w elements which are filled in with the<br>
> > +   ## appropriately-typed values.<br>
> > +   % for j in range(op.num_inputs):<br>
> > +      % if op.input_sizes[j] == 0:<br>
> > +         <% continue %><br>
> > +      % elif "src" + str(j) not in op.const_expr:<br>
> > +         ## Avoid unused variable warnings<br>
> > +         <% continue %><br>
> > +      %endif<br>
> > +<br>
> > +      struct ${op.input_types[j]}_vec src${j} = {<br>
> > +      % for k in range(op.input_sizes[j]):<br>
> > +         % if op.input_types[j] == "bool":<br>
> > +            _src[${j}].u[${k}] != 0,<br>
> > +         % else:<br>
> > +            _src[${j}].${op.input_types[j][:1]}[${k}],<br>
> > +         % endif<br>
> > +      % endfor<br>
> > +      };<br>
> > +   % endfor<br>
> > +<br>
> > +   % if op.output_size == 0:<br>
> > +      ## For per-component instructions, we need to iterate over the<br>
> > +      ## components and apply the constant expression one component<br>
> > +      ## at a time.<br>
> > +      for (unsigned _i = 0; _i < num_components; _i++) {<br>
> > +         ## For each per-component input, create a variable srcN that<br>
> > +         ## contains the value of the current (_i'th) component.<br>
> > +         % for j in range(op.num_inputs):<br>
> > +            % if op.input_sizes[j] != 0:<br>
> > +               <% continue %><br>
> > +            % elif "src" + str(j) not in op.const_expr:<br>
> > +               ## Avoid unused variable warnings<br>
> > +               <% continue %><br>
> > +            % elif op.input_types[j] == "bool":<br>
> > +               bool src${j} = _src[${j}].u[_i] != 0;<br>
> > +            % else:<br>
> > +               ${op.input_types[j]} src${j} = _src[${j}].${op.input_types[j][:1]}[_i];<br>
> > +            % endif<br>
> > +         % endfor<br>
> > +<br>
> > +         ## Create an appropriately-typed variable dst and assign the<br>
> > +         ## result of the const_expr to it.  If const_expr already contains<br>
> > +         ## writes to dst, just include const_expr directly.<br>
> > +         % if "dst" in op.const_expr:<br>
> > +            ${op.output_type} dst;<br>
> > +            ${op.const_expr}<br>
> > +         % else:<br>
> > +            ${op.output_type} dst = ${op.const_expr};<br>
> > +         % endif<br>
> > +<br>
> > +         ## Store the current component of the actual destination to the<br>
> > +         ## value of dst.<br>
> > +         % if op.output_type == "bool":<br>
> > +            ## Sanitize the C value to a proper NIR bool<br>
> > +            _dst_val.u[_i] = dst ? NIR_TRUE : NIR_FALSE;<br>
> > +         % else:<br>
> > +            _dst_val.${op.output_type[:1]}[_i] = dst;<br>
> > +         % endif<br>
> > +      }<br>
> > +   % else:<br>
> > +      ## In the non-per-component case, create a struct dst with<br>
> > +      ## appropriately-typed elements x, y, z, and w and assign the result<br>
> > +      ## of the const_expr to all components of dst, or include the<br>
> > +      ## const_expr directly if it writes to dst already.<br>
> > +      struct ${op.output_type}_vec dst;<br>
> > +<br>
> > +      % if "dst" in op.const_expr:<br>
> > +         ${op.const_expr}<br>
> > +      % else:<br>
> > +         ## Splat the value to all components.  This way expressions such<br>
> > +         ## as fnoise and ddx which take on the constant value 0.0f<br>
> > +         ## irrespective of their inputs.<br>
><br>
> Seems like you're something there. I assume you meant to say "...will<br>
> only have to have '0.0f' as their const_expr" or something like that.<br>
> Also, I think fnoise is currently the only thing that depends on this<br>
> - ddx is per-component, so it doesn't hit this codepath.</p>
<p dir="ltr">Sure. I'll adjust the comment.  Thanks for the comments and review.<br>
--Jason</p>
<p dir="ltr">> > +         dst.x = dst.y = dst.z = dst.w = ${op.const_expr};<br>
> > +      % endif<br>
> > +<br>
> > +      ## For each component in the destination, copy the value of dst to<br>
> > +      ## the actual destination.<br>
> > +      % for k in range(op.output_size):<br>
> > +         % if op.output_type == "bool":<br>
> > +            ## Sanitize the C value to a proper NIR bool<br>
> > +            _dst_val.u[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;<br>
> > +         % else:<br>
> > +            _dst_val.${op.output_type[:1]}[${k}] = dst.${"xyzw"[k]};<br>
> > +         % endif<br>
> > +      % endfor<br>
> > +   % endif<br>
> > +<br>
> > +   return _dst_val;<br>
> > +}<br>
> > +% endfor<br>
> > +<br>
> > +nir_const_value<br>
> > +nir_eval_const_opcode(nir_op op, unsigned num_components,<br>
> > +                      nir_const_value *src)<br>
> > +{<br>
> > +   switch (op) {<br>
> > +% for name in sorted(opcodes.iterkeys()):<br>
> > +   case nir_op_${name}: {<br>
> > +      return evaluate_${name}(num_components, src);<br>
> > +      break;<br>
> > +   }<br>
> > +% endfor<br>
> > +   default:<br>
> > +      unreachable("shouldn't get here");<br>
> > +   }<br>
> > +}"""<br>
> > +<br>
> > +from nir_opcodes import opcodes<br>
> > +from mako.template import Template<br>
> > +<br>
> > +print Template(template).render(opcodes=opcodes)<br>
> > diff --git a/src/glsl/nir/nir_opcodes.py b/src/glsl/nir/nir_opcodes.py<br>
> > index 5bafbb0..5fe9572 100644<br>
> > --- a/src/glsl/nir/nir_opcodes.py<br>
> > +++ b/src/glsl/nir/nir_opcodes.py<br>
> > @@ -24,6 +24,7 @@<br>
> >  # Authors:<br>
> >  #    Connor Abbott (<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>)<br>
> ><br>
> > +<br>
> >  # Class that represents all the information we have about the opcode<br>
> >  # NOTE: this must be kept in sync with nir_op_info<br>
> ><br>
> > @@ -32,7 +33,7 @@ class Opcode(object):<br>
> >     NOTE: this must be kept in sync with nir_op_info<br>
> >     """<br>
> >     def __init__(self, name, output_size, output_type, input_sizes,<br>
> > -                input_types, algebraic_properties):<br>
> > +                input_types, algebraic_properties, const_expr):<br>
> >        """Parameters:<br>
> ><br>
> >        - name is the name of the opcode (prepend nir_op_ for the enum name)<br>
> > @@ -40,6 +41,27 @@ class Opcode(object):<br>
> >        - input_types is a list of types<br>
> >        - algebraic_properties is a space-seperated string, where nir_op_is_ is<br>
> >          prepended before each entry<br>
> > +      - const_expr is an expression or series of statements that computes the<br>
> > +        constant value of the opcode given the constant values of its inputs.<br>
> > +<br>
> > +      Constant expressions are formed from the variables src0, src1, ...,<br>
> > +      src(N-1), where N is the number of arguments.  The output of the<br>
> > +      expression should be stored in the dst variable.  Per-component input<br>
> > +      and output variables will be scalars and non-per-component input and<br>
> > +      output variables will be a struct with fields named x, y, z, and w<br>
> > +      all of the correct type.  Input and output variables can be assumed<br>
> > +      to already be of the correct type and need no conversion.  In<br>
> > +      particular, the conversion from the C bool type to/from  NIR_TRUE and<br>
> > +      NIR_FALSE happens automatically.<br>
> > +<br>
> > +      For per-component instructions, the entire expression will be<br>
> > +      executed once for each component.  For non-per-component<br>
> > +      instructions, the expression is expected to store the correct values<br>
> > +      in dst.x, dst.y, etc.  If "dst" does not exist anywhere in the<br>
> > +      constant expression, an assignment to dst will happen automatically<br>
> > +      and the result will be equivalent to "dst = <expression>" for<br>
> > +      per-component instructions and "dst.x = dst.y = ... = <expression>"<br>
> > +      for non-per-component instructions.<br>
> >        """<br>
> >        assert isinstance(name, str)<br>
> >        assert isinstance(output_size, int)<br>
> > @@ -49,6 +71,7 @@ class Opcode(object):<br>
> >        assert isinstance(input_types, list)<br>
> >        assert isinstance(input_types[0], str)<br>
> >        assert isinstance(algebraic_properties, str)<br>
> > +      assert isinstance(const_expr, str)<br>
> >        assert len(input_sizes) == len(input_types)<br>
> >        assert 0 <= output_size <= 4<br>
> >        for size in input_sizes:<br>
> > @@ -62,6 +85,7 @@ class Opcode(object):<br>
> >        self.input_sizes = input_sizes<br>
> >        self.input_types = input_types<br>
> >        self.algebraic_properties = algebraic_properties<br>
> > +      self.const_expr = const_expr<br>
> ><br>
> >  # helper variables for strings<br>
> >  tfloat = "float"<br>
> > @@ -76,178 +100,289 @@ associative = "associative "<br>
> >  opcodes = {}<br>
> ><br>
> >  def opcode(name, output_size, output_type, input_sizes, input_types,<br>
> > -           algebraic_properties):<br>
> > +           algebraic_properties, const_expr):<br>
> >     assert name not in opcodes<br>
> >     opcodes[name] = Opcode(name, output_size, output_type, input_sizes,<br>
> > -                          input_types, algebraic_properties)<br>
> > -<br>
> > -def unop_convert(name, in_type, out_type):<br>
> > -   opcode(name, 0, out_type, [0], [in_type], "")<br>
> > -<br>
> > -def unop(name, ty):<br>
> > -   opcode(name, 0, ty, [0], [ty], "")<br>
> > -<br>
> > -def unop_horiz(name, output_size, output_type, input_size, input_type):<br>
> > -   opcode(name, output_size, output_type, [input_size], [input_type], "")<br>
> > -<br>
> > -def unop_reduce(name, output_size, output_type, input_type):<br>
> > -   unop_horiz(name + "2", output_size, output_type, 2, input_type)<br>
> > -   unop_horiz(name + "3", output_size, output_type, 3, input_type)<br>
> > -   unop_horiz(name + "4", output_size, output_type, 4, input_type)<br>
> > +                          input_types, algebraic_properties, const_expr)<br>
> > +<br>
> > +def unop_convert(name, in_type, out_type, const_expr):<br>
> > +   opcode(name, 0, out_type, [0], [in_type], "", const_expr)<br>
> > +<br>
> > +def unop(name, ty, const_expr):<br>
> > +   opcode(name, 0, ty, [0], [ty], "", const_expr)<br>
> > +<br>
> > +def unop_horiz(name, output_size, output_type, input_size, input_type,<br>
> > +               const_expr):<br>
> > +   opcode(name, output_size, output_type, [input_size], [input_type], "",<br>
> > +          const_expr)<br>
> > +<br>
> > +def unop_reduce(name, output_size, output_type, input_type, prereduce_expr,<br>
> > +                reduce_expr, final_expr):<br>
> > +   def prereduce(src):<br>
> > +      return "(" + prereduce_expr.format(src=src) + ")"<br>
> > +   def final(src):<br>
> > +      return final_expr.format(src="(" + src + ")")<br>
> > +   def reduce_(src0, src1):<br>
> > +      return reduce_expr.format(src0=src0, src1=src1)<br>
> > +   src0 = prereduce("src0.x")<br>
> > +   src1 = prereduce("src0.y")<br>
> > +   src2 = prereduce("src0.z")<br>
> > +   src3 = prereduce("src0.w")<br>
> > +   unop_horiz(name + "2", output_size, output_type, 2, input_type,<br>
> > +              final(reduce_(src0, src1)))<br>
> > +   unop_horiz(name + "3", output_size, output_type, 3, input_type,<br>
> > +              final(reduce_(reduce_(src0, src1), src2)))<br>
> > +   unop_horiz(name + "4", output_size, output_type, 4, input_type,<br>
> > +              final(reduce_(reduce_(src0, src1), reduce_(src2, src3))))<br>
> ><br>
> ><br>
> >  # These two move instructions differ in what modifiers they support and what<br>
> >  # the negate modifier means. Otherwise, they are identical.<br>
> > -unop("fmov", tfloat)<br>
> > -unop("imov", tint)<br>
> > -<br>
> > -unop("ineg", tint)<br>
> > -unop("fneg", tfloat)<br>
> > -unop("inot", tint) # invert every bit of the integer<br>
> > -unop("fnot", tfloat) # (src == 0.0) ? 1.0 : 0.0<br>
> > -unop("fsign", tfloat)<br>
> > -unop("isign", tint)<br>
> > -unop("iabs", tint)<br>
> > -unop("fabs", tfloat)<br>
> > -unop("fsat", tfloat)<br>
> > -unop("frcp", tfloat)<br>
> > -unop("frsq", tfloat)<br>
> > -unop("fsqrt", tfloat)<br>
> > -unop("fexp", tfloat) # < e^x<br>
> > -unop("flog", tfloat) # log base e<br>
> > -unop("fexp2", tfloat)<br>
> > -unop("flog2", tfloat)<br>
> > -unop_convert("f2i", tfloat, tint) # Float-to-integer conversion.<br>
> > -unop_convert("f2u", tfloat, tunsigned) # Float-to-unsigned conversion<br>
> > -unop_convert("i2f", tint, tfloat) # Integer-to-float conversion.<br>
> > -unop_convert("f2b", tfloat, tbool) # Float-to-boolean conversion<br>
> > -unop_convert("b2f", tbool, tfloat) # Boolean-to-float conversion<br>
> > -unop_convert("i2b", tint, tbool) # int-to-boolean conversion<br>
> > -unop_convert("b2i", tbool, tint) # Boolean-to-int conversion<br>
> > -unop_convert("u2f", tunsigned, tfloat) #Unsigned-to-float conversion.<br>
> > -<br>
> > -unop_reduce("bany", 1, tbool, tbool) # returns ~0 if any component of src[0] != 0<br>
> > -unop_reduce("ball", 1, tbool, tbool) # returns ~0 if all components of src[0] != 0<br>
> > -unop_reduce("fany", 1, tfloat, tfloat) # returns 1.0 if any component of src[0] != 0<br>
> > -unop_reduce("fall", 1, tfloat, tfloat) # returns 1.0 if all components of src[0] != 0<br>
> > +unop("fmov", tfloat, "src0")<br>
> > +unop("imov", tint, "src0")<br>
> > +<br>
> > +unop("ineg", tint, "-src0")<br>
> > +unop("fneg", tfloat, "-src0")<br>
> > +unop("inot", tint, "~src0") # invert every bit of the integer<br>
> > +unop("fnot", tfloat, "(src0 == 0.0f) ? 1.0f : 0.0f")<br>
> > +unop("fsign", tfloat, "(src0 == 0.0f) ? 0.0f : ((src0 > 0.0f) ? 1.0f : -1.0f)")<br>
> > +unop("isign", tint, "(src0 == 0) ? 0 : ((src0 > 0) ? 1 : -1)")<br>
> > +unop("iabs", tint, "abs(src0)")<br>
> > +unop("fabs", tfloat, "fabsf(src0)")<br>
> > +unop("fsat", tfloat, "(src0 > 1.0f) ? 1.0f : ((src0 <= 0.0f) ? 0.0f : src0)")<br>
> > +unop("frcp", tfloat, "1.0f / src0")<br>
> > +unop("frsq", tfloat, "1.0f / sqrtf(src0)")<br>
> > +unop("fsqrt", tfloat, "sqrtf(src0)")<br>
> > +unop("fexp", tfloat, "expf(src0)") # < e^x<br>
> > +unop("flog", tfloat, "logf(src0)") # log base e<br>
> > +unop("fexp2", tfloat, "exp2f(src0)")<br>
> > +unop("flog2", tfloat, "log2f(src0)")<br>
> > +unop_convert("f2i", tfloat, tint, "src0") # Float-to-integer conversion.<br>
> > +unop_convert("f2u", tfloat, tunsigned, "src0") # Float-to-unsigned conversion<br>
> > +unop_convert("i2f", tint, tfloat, "src0") # Integer-to-float conversion.<br>
> > +# Float-to-boolean conversion<br>
> > +unop_convert("f2b", tfloat, tbool, "src0 == 0.0f")<br>
> > +# Boolean-to-float conversion<br>
> > +unop_convert("b2f", tbool, tfloat, "src0 ? 1.0f : 0.0f")<br>
> > +# Int-to-boolean conversion<br>
> > +unop_convert("i2b", tint, tbool, "src0 == 0")<br>
> > +unop_convert("b2i", tbool, tint, "src0 ? 0 : -1") # Boolean-to-int conversion<br>
> > +unop_convert("u2f", tunsigned, tfloat, "src0") #Unsigned-to-float conversion.<br>
> > +<br>
> > +unop_reduce("bany", 1, tbool, tbool, "{src}", "{src0} || {src1}", "{src}")<br>
> > +unop_reduce("ball", 1, tbool, tbool, "{src}", "{src0} && {src1}", "{src}")<br>
> > +unop_reduce("fany", 1, tfloat, tfloat, "{src} != 0.0f", "{src0} || {src1}",<br>
> > +            "{src} ? 1.0f : 0.0f")<br>
> > +unop_reduce("fall", 1, tfloat, tfloat, "{src} != 0.0f", "{src0} && {src1}",<br>
> > +            "{src} ? 1.0f : 0.0f")<br>
> ><br>
> >  # Unary floating-point rounding operations.<br>
> ><br>
> ><br>
> > -unop("ftrunc", tfloat)<br>
> > -unop("fceil", tfloat)<br>
> > -unop("ffloor", tfloat)<br>
> > -unop("ffract", tfloat)<br>
> > -unop("fround_even", tfloat)<br>
> > +unop("ftrunc", tfloat, "truncf(src0)")<br>
> > +unop("fceil", tfloat, "ceilf(src0)")<br>
> > +unop("ffloor", tfloat, "floorf(src0)")<br>
> > +unop("ffract", tfloat, "src0 - floorf(src0)")<br>
> > +unop("fround_even", tfloat, "_mesa_round_to_even(src0)")<br>
> ><br>
> ><br>
> >  # Trigonometric operations.<br>
> ><br>
> ><br>
> > -unop("fsin", tfloat)<br>
> > -unop("fcos", tfloat)<br>
> > -unop("fsin_reduced", tfloat)<br>
> > -unop("fcos_reduced", tfloat)<br>
> > +unop("fsin", tfloat, "sinf(src0)")<br>
> > +unop("fcos", tfloat, "cosf(src0)")<br>
> > +unop("fsin_reduced", tfloat, "sinf(src0)")<br>
> > +unop("fcos_reduced", tfloat, "cosf(src0)")<br>
> ><br>
> ><br>
> >  # Partial derivatives.<br>
> ><br>
> ><br>
> > -unop("fddx", tfloat)<br>
> > -unop("fddy", tfloat)<br>
> > -unop("fddx_fine", tfloat)<br>
> > -unop("fddy_fine", tfloat)<br>
> > -unop("fddx_coarse", tfloat)<br>
> > -unop("fddy_coarse", tfloat)<br>
> > +unop("fddx", tfloat, "0.0f") # the derivative of a constant is 0.<br>
> > +unop("fddy", tfloat, "0.0f")<br>
> > +unop("fddx_fine", tfloat, "0.0f")<br>
> > +unop("fddy_fine", tfloat, "0.0f")<br>
> > +unop("fddx_coarse", tfloat, "0.0f")<br>
> > +unop("fddy_coarse", tfloat, "0.0f")<br>
> ><br>
> ><br>
> >  # Floating point pack and unpack operations.<br>
> ><br>
> > -<br>
> > -unop_horiz("pack_snorm_2x16", 1, tunsigned, 2, tfloat)<br>
> > -unop_horiz("pack_snorm_4x8", 1, tunsigned, 4, tfloat)<br>
> > -unop_horiz("pack_unorm_2x16", 1, tunsigned, 2, tfloat)<br>
> > -unop_horiz("pack_unorm_4x8", 1, tunsigned, 4, tfloat)<br>
> > -unop_horiz("pack_half_2x16", 1, tunsigned, 2, tfloat)<br>
> > -unop_horiz("unpack_snorm_2x16", 2, tfloat, 1, tunsigned)<br>
> > -unop_horiz("unpack_snorm_4x8", 4, tfloat, 1, tunsigned)<br>
> > -unop_horiz("unpack_unorm_2x16", 2, tfloat, 1, tunsigned)<br>
> > -unop_horiz("unpack_unorm_4x8", 4, tfloat, 1, tunsigned)<br>
> > -unop_horiz("unpack_half_2x16", 2, tfloat, 1, tunsigned)<br>
> > +def pack_2x16(fmt):<br>
> > +   unop_horiz("pack_" + fmt + "_2x16", 1, tunsigned, 2, tfloat, """<br>
> > +dst.x = (uint32_t) pack_fmt_1x16(src0.x);<br>
> > +dst.x |= ((uint32_t) pack_fmt_1x16(src0.y)) << 16;<br>
> > +""".replace("fmt", fmt))<br>
> > +<br>
> > +def pack_4x8(fmt):<br>
> > +   unop_horiz("pack_" + fmt + "_4x8", 1, tunsigned, 4, tfloat, """<br>
> > +dst.x = (uint32_t) pack_fmt_1x8(src0.x);<br>
> > +dst.x |= ((uint32_t) pack_fmt_1x8(src0.y)) << 8;<br>
> > +dst.x |= ((uint32_t) pack_fmt_1x8(src0.z)) << 16;<br>
> > +dst.x |= ((uint32_t) pack_fmt_1x8(src0.w)) << 24;<br>
> > +""".replace("fmt", fmt))<br>
> > +<br>
> > +def unpack_2x16(fmt):<br>
> > +   unop_horiz("unpack_" + fmt + "_2x16", 2, tfloat, 1, tunsigned, """<br>
> > +dst.x = unpack_fmt_1x16((uint16_t)(src0.x & 0xffff));<br>
> > +dst.y = unpack_fmt_1x16((uint16_t)(src0.x << 16));<br>
> > +""".replace("fmt", fmt))<br>
> > +<br>
> > +def unpack_4x8(fmt):<br>
> > +   unop_horiz("unpack_" + fmt + "_4x8", 4, tfloat, 1, tunsigned, """<br>
> > +dst.x = unpack_fmt_1x8((uint8_t)(src0.x & 0xff));<br>
> > +dst.y = unpack_fmt_1x8((uint8_t)((src0.x >> 8) & 0xff));<br>
> > +dst.z = unpack_fmt_1x8((uint8_t)((src0.x >> 16) & 0xff));<br>
> > +dst.w = unpack_fmt_1x8((uint8_t)(src0.x >> 24));<br>
> > +""".replace("fmt", fmt))<br>
> > +<br>
> > +<br>
> > +pack_2x16("snorm")<br>
> > +pack_4x8("snorm")<br>
> > +pack_2x16("unorm")<br>
> > +pack_4x8("unorm")<br>
> > +pack_2x16("half")<br>
> > +unpack_2x16("snorm")<br>
> > +unpack_4x8("snorm")<br>
> > +unpack_2x16("unorm")<br>
> > +unpack_4x8("unorm")<br>
> > +unpack_2x16("half")<br>
> ><br>
> ><br>
> >  # Lowered floating point unpacking operations.<br>
> ><br>
> ><br>
> > -unop_horiz("unpack_half_2x16_split_x", 1, tfloat, 1, tunsigned)<br>
> > -unop_horiz("unpack_half_2x16_split_y", 1, tfloat, 1, tunsigned)<br>
> > +unop_horiz("unpack_half_2x16_split_x", 1, tfloat, 1, tunsigned, """<br>
> > +dst.x = unpack_half_1x16((uint16_t)(src0.x & 0xffff));<br>
> > +""")<br>
> > +unop_horiz("unpack_half_2x16_split_y", 1, tfloat, 1, tunsigned, """<br>
> > +dst.y = unpack_half_1x16((uint16_t)(src0.x >> 16));<br>
> > +""")<br>
> ><br>
> ><br>
> >  # Bit operations, part of ARB_gpu_shader5.<br>
> ><br>
> ><br>
> > -unop("bitfield_reverse", tunsigned)<br>
> > -unop("bit_count", tunsigned)<br>
> > -unop_convert("ufind_msb", tunsigned, tint)<br>
> > -unop("ifind_msb", tint)<br>
> > -unop("find_lsb", tint)<br>
> > +unop("bitfield_reverse", tunsigned, """<br>
> > +/* we're not winning any awards for speed here, but that's ok */<br>
> > +dst = 0;<br>
> > +for (unsigned bit = 0; bit < 32; bit++)<br>
> > +   dst |= ((src0 >> bit) & 1) << (31 - bit);<br>
> > +""")<br>
> > +unop("bit_count", tunsigned, """<br>
> > +dst = 0;<br>
> > +for (unsigned bit = 0; bit < 32; bit++) {<br>
> > +   if ((src0 >> bit) & 1)<br>
> > +      dst++;<br>
> > +}<br>
> > +""")<br>
> > +<br>
> > +unop_convert("ufind_msb", tunsigned, tint, """<br>
> > +dst = -1;<br>
> > +for (int bit = 31; bit > 0; bit--) {<br>
> > +   if ((src0 >> bit) & 1) {<br>
> > +      dst = bit;<br>
> > +      break;<br>
> > +   }<br>
> > +}<br>
> > +""")<br>
> > +<br>
> > +unop("ifind_msb", tint, """<br>
> > +dst = -1;<br>
> > +for (int bit = 31; bit >= 0; bit--) {<br>
> > +   /* If src0 < 0, we're looking for the first 0 bit.<br>
> > +    * if src0 >= 0, we're looking for the first 1 bit.<br>
> > +    */<br>
> > +   if ((((src0 >> bit) & 1) && (src0 >= 0)) ||<br>
> > +      (!((src0 >> bit) & 1) && (src0 < 0))) {<br>
> > +      dst = bit;<br>
> > +      break;<br>
> > +   }<br>
> > +}<br>
> > +""")<br>
> > +<br>
> > +unop("find_lsb", tint, """<br>
> > +dst = -1;<br>
> > +for (unsigned bit = 0; bit < 32; bit++) {<br>
> > +   if ((src0 >> bit) & 1) {<br>
> > +      dst = bit;<br>
> > +      break;<br>
> > +   }<br>
> > +}<br>
> > +""")<br>
> ><br>
> ><br>
> >  for i in xrange(1, 5):<br>
> >     for j in xrange(1, 5):<br>
> > -      unop_horiz("fnoise{0}_{1}".format(i, j), i, tfloat, j, tfloat)<br>
> > +      unop_horiz("fnoise{0}_{1}".format(i, j), i, tfloat, j, tfloat, "0.0f")<br>
> ><br>
> > -def binop_convert(name, out_type, in_type, alg_props):<br>
> > -   opcode(name, 0, out_type, [0, 0], [in_type, in_type], alg_props)<br>
> > +def binop_convert(name, out_type, in_type, alg_props, const_expr):<br>
> > +   opcode(name, 0, out_type, [0, 0], [in_type, in_type], alg_props, const_expr)<br>
> ><br>
> > -def binop(name, ty, alg_props):<br>
> > -   binop_convert(name, ty, ty, alg_props)<br>
> > +def binop(name, ty, alg_props, const_expr):<br>
> > +   binop_convert(name, ty, ty, alg_props, const_expr)<br>
> ><br>
> > -def binop_compare(name, ty, alg_props):<br>
> > -   binop_convert(name, ty, tbool, alg_props)<br>
> > +def binop_compare(name, ty, alg_props, const_expr):<br>
> > +   binop_convert(name, tbool, ty, alg_props, const_expr)<br>
> ><br>
> >  def binop_horiz(name, out_size, out_type, src1_size, src1_type, src2_size,<br>
> > -                src2_type):<br>
> > -   opcode(name, out_size, out_type, [src1_size, src2_size], [src1_type, src2_type], "")<br>
> > -<br>
> > -def binop_reduce(name, output_size, output_type, src_type):<br>
> > -   opcode(name + "2",output_size, output_type,<br>
> > -          [2, 2], [src_type, src_type], commutative)<br>
> > +                src2_type, const_expr):<br>
> > +   opcode(name, out_size, out_type, [src1_size, src2_size], [src1_type, src2_type],<br>
> > +          "", const_expr)<br>
> > +<br>
> > +def binop_reduce(name, output_size, output_type, src_type, prereduce_expr,<br>
> > +                 reduce_expr, final_expr):<br>
> > +   def final(src):<br>
> > +      return final_expr.format(src= "(" + src + ")")<br>
> > +   def reduce_(src0, src1):<br>
> > +      return reduce_expr.format(src0=src0, src1=src1)<br>
> > +   def prereduce(src0, src1):<br>
> > +      return "(" + prereduce_expr.format(src0=src0, src1=src1) + ")"<br>
> > +   src0 = prereduce("src0.x", "src1.x")<br>
> > +   src1 = prereduce("src0.y", "src1.y")<br>
> > +   src2 = prereduce("src0.z", "src1.z")<br>
> > +   src3 = prereduce("src0.w", "src1.w")<br>
> > +   opcode(name + "2", output_size, output_type,<br>
> > +          [2, 2], [src_type, src_type], commutative,<br>
> > +          final(reduce_(src0, src1)))<br>
> >     opcode(name + "3", output_size, output_type,<br>
> > -          [3, 3], [src_type, src_type], commutative)<br>
> > +          [3, 3], [src_type, src_type], commutative,<br>
> > +          final(reduce_(reduce_(src0, src1), src2)))<br>
> >     opcode(name + "4", output_size, output_type,<br>
> > -          [4, 4], [src_type, src_type], commutative)<br>
> > +          [4, 4], [src_type, src_type], commutative,<br>
> > +          final(reduce_(reduce_(src0, src1), reduce_(src2, src3))))<br>
> ><br>
> > -binop("fadd", tfloat, commutative + associative)<br>
> > -binop("iadd", tint, commutative + associative)<br>
> > -binop("fsub", tfloat, "")<br>
> > -binop("isub", tint, "")<br>
> > +binop("fadd", tfloat, commutative + associative, "src0 + src1")<br>
> > +binop("iadd", tint, commutative + associative, "src0 + src1")<br>
> > +binop("fsub", tfloat, "", "src0 - src1")<br>
> > +binop("isub", tint, "", "src0 - src1")<br>
> ><br>
> > -binop("fmul", tfloat, commutative + associative)<br>
> > +binop("fmul", tfloat, commutative + associative, "src0 * src1")<br>
> >  # low 32-bits of signed/unsigned integer multiply<br>
> > -binop("imul", tint, commutative + associative)<br>
> > +binop("imul", tint, commutative + associative, "src0 * src1")<br>
> >  # high 32-bits of signed integer multiply<br>
> > -binop("imul_high", tint, commutative)<br>
> > +binop("imul_high", tint, commutative,<br>
> > +      "(int32_t)(((int64_t) src0 * (int64_t) src1) >> 32)")<br>
> >  # high 32-bits of unsigned integer multiply<br>
> > -binop("umul_high", tunsigned, commutative)<br>
> > +binop("umul_high", tunsigned, commutative,<br>
> > +      "(uint32_t)(((uint64_t) src0 * (uint64_t) src1) >> 32)")<br>
> ><br>
> > -binop("fdiv", tfloat, "")<br>
> > -binop("idiv", tint, "")<br>
> > -binop("udiv", tunsigned, "")<br>
> > +binop("fdiv", tfloat, "", "src0 / src1")<br>
> > +binop("idiv", tint, "", "src0 / src1")<br>
> > +binop("udiv", tunsigned, "", "src0 / src1")<br>
> ><br>
> >  # returns a boolean representing the carry resulting from the addition of<br>
> >  # the two unsigned arguments.<br>
> ><br>
> > -binop_convert("uadd_carry", tbool, tunsigned,<br>
> > -              commutative)<br>
> > +binop_convert("uadd_carry", tbool, tunsigned, commutative, "src0 + src1 < src0")<br>
> ><br>
> >  # returns a boolean representing the borrow resulting from the subtraction<br>
> >  # of the two unsigned arguments.<br>
> ><br>
> > -binop_convert("usub_borrow", tbool, tunsigned, "")<br>
> > +binop_convert("usub_borrow", tbool, tunsigned, "", "src1 < src0")<br>
> ><br>
> > -binop("fmod", tfloat, "")<br>
> > -binop("umod", tunsigned, "")<br>
> > +binop("fmod", tfloat, "", "src0 - src1 * floorf(src0 / src1)")<br>
> > +binop("umod", tunsigned, "", "src1 == 0 ? 0 : src0 % src1")<br>
> ><br>
> >  #<br>
> >  # Comparisons<br>
> > @@ -256,41 +391,47 @@ binop("umod", tunsigned, "")<br>
> ><br>
> >  # these integer-aware comparisons return a boolean (0 or ~0)<br>
> ><br>
> > -binop_compare("flt", tfloat, "")<br>
> > -binop_compare("fge", tfloat, "")<br>
> > -binop_compare("feq", tfloat, commutative)<br>
> > -binop_compare("fne", tfloat, commutative)<br>
> > -binop_compare("ilt", tint, "")<br>
> > -binop_compare("ige", tint, "")<br>
> > -binop_compare("ieq", tint, commutative)<br>
> > -binop_compare("ine", tint, commutative)<br>
> > -binop_compare("ult", tunsigned, "")<br>
> > -binop_compare("uge", tunsigned, "")<br>
> > +binop_compare("flt", tfloat, "", "src0 < src1")<br>
> > +binop_compare("fge", tfloat, "", "src0 >= src1")<br>
> > +binop_compare("feq", tfloat, commutative, "src0 == src1")<br>
> > +binop_compare("fne", tfloat, commutative, "src0 != src1")<br>
> > +binop_compare("ilt", tint, "", "src0 < src1")<br>
> > +binop_compare("ige", tint, "", "src0 >= src1")<br>
> > +binop_compare("ieq", tint, commutative, "src0 == src1")<br>
> > +binop_compare("ine", tint, commutative, "src0 != src1")<br>
> > +binop_compare("ult", tunsigned, "", "src0 < src1")<br>
> > +binop_compare("uge", tunsigned, "", "src0 >= src1")<br>
> ><br>
> >  # integer-aware GLSL-style comparisons that compare floats and ints<br>
> ><br>
> > -binop_reduce("ball_fequal",  1, tbool, tfloat)<br>
> > -binop_reduce("bany_fnequal", 1, tbool, tfloat)<br>
> > -binop_reduce("ball_iequal",  1, tbool, tint)<br>
> > -binop_reduce("bany_inequal", 1, tbool, tint)<br>
> > +binop_reduce("ball_fequal",  1, tbool, tfloat, "{src0} == {src1}",<br>
> > +             "{src0} && {src1}", "{src}")<br>
> > +binop_reduce("bany_fnequal", 1, tbool, tfloat, "{src0} != {src1}",<br>
> > +             "{src0} || {src1}", "{src}")<br>
> > +binop_reduce("ball_iequal",  1, tbool, tint, "{src0} == {src1}",<br>
> > +             "{src0} && {src1}", "{src}")<br>
> > +binop_reduce("bany_inequal", 1, tbool, tint, "{src0} != {src1}",<br>
> > +             "{src0} || {src1}", "{src}")<br>
> ><br>
> >  # non-integer-aware GLSL-style comparisons that return 0.0 or 1.0<br>
> ><br>
> > -binop_reduce("fall_equal",  1, tfloat, tfloat)<br>
> > -binop_reduce("fany_nequal", 1, tfloat, tfloat)<br>
> > +binop_reduce("fall_equal",  1, tfloat, tfloat, "{src0} == {src1}",<br>
> > +             "{src0} && {src1}", "{src} ? 1.0f : 0.0f")<br>
> > +binop_reduce("fany_nequal", 1, tfloat, tfloat, "{src0} != {src1}",<br>
> > +             "{src0} || {src1}", "{src} ? 1.0f : 0.0f")<br>
> ><br>
> >  # These comparisons for integer-less hardware return 1.0 and 0.0 for true<br>
> >  # and false respectively<br>
> ><br>
> > -binop("slt", tfloat, "") # Set on Less Than<br>
> > -binop("sge", tfloat, "") # Set on Greater Than or Equal<br>
> > -binop("seq", tfloat, commutative) # Set on Equal<br>
> > -binop("sne", tfloat, commutative) # Set on Not Equal<br>
> > +binop("slt", tfloat, "", "(src0 < src1) ? 1.0f : 0.0f") # Set on Less Than<br>
> > +binop("sge", tfloat, "", "(src0 >= src1) ? 1.0f : 0.0f") # Set on Greater or Equal<br>
> > +binop("seq", tfloat, commutative, "(src0 == src1) ? 1.0f : 0.0f") # Set on Equal<br>
> > +binop("sne", tfloat, commutative, "(src0 != src1) ? 1.0f : 0.0f") # Set on Not Equal<br>
> ><br>
> ><br>
> > -binop("ishl", tint, "")<br>
> > -binop("ishr", tint, "")<br>
> > -binop("ushr", tunsigned, "")<br>
> > +binop("ishl", tint, "", "src0 << src1")<br>
> > +binop("ishr", tint, "", "src0 >> src1")<br>
> > +binop("ushr", tunsigned, "", "src0 >> src1")<br>
> ><br>
> >  # bitwise logic operators<br>
> >  #<br>
> > @@ -298,9 +439,9 @@ binop("ushr", tunsigned, "")<br>
> >  # integers.<br>
> ><br>
> ><br>
> > -binop("iand", tunsigned, commutative + associative)<br>
> > -binop("ior", tunsigned, commutative + associative)<br>
> > -binop("ixor", tunsigned, commutative + associative)<br>
> > +binop("iand", tunsigned, commutative + associative, "src0 & src1")<br>
> > +binop("ior", tunsigned, commutative + associative, "src0 | src1")<br>
> > +binop("ixor", tunsigned, commutative + associative, "src0 ^ src1")<br>
> ><br>
> ><br>
> >  # floating point logic operators<br>
> > @@ -308,42 +449,60 @@ binop("ixor", tunsigned, commutative + associative)<br>
> >  # These use (src != 0.0) for testing the truth of the input, and output 1.0<br>
> >  # for true and 0.0 for false<br>
> ><br>
> > -binop("fand", tfloat, commutative)<br>
> > -binop("for", tfloat, commutative)<br>
> > -binop("fxor", tfloat, commutative)<br>
> > -<br>
> > -binop_reduce("fdot", 1, tfloat, tfloat)<br>
> > -<br>
> > -binop("fmin", tfloat, "")<br>
> > -binop("imin", tint, commutative + associative)<br>
> > -binop("umin", tunsigned, commutative + associative)<br>
> > -binop("fmax", tfloat, "")<br>
> > -binop("imax", tint, commutative + associative)<br>
> > -binop("umax", tunsigned, commutative + associative)<br>
> > -<br>
> > -binop("fpow", tfloat, "")<br>
> > -<br>
> > -binop_horiz("pack_half_2x16_split", 1, tunsigned, 1, tfloat, 1, tfloat)<br>
> > -<br>
> > -binop("bfm", tunsigned, "")<br>
> > -<br>
> > -binop("ldexp", tunsigned, "")<br>
> > +binop("fand", tfloat, commutative,<br>
> > +      "((src0 != 0.0f) && (src1 != 0.0f)) ? 1.0f : 0.0f")<br>
> > +binop("for", tfloat, commutative,<br>
> > +      "((src0 != 0.0f) || (src1 != 0.0f)) ? 1.0f : 0.0f")<br>
> > +binop("fxor", tfloat, commutative,<br>
> > +      "(src0 != 0.0f && src1 == 0.0f) || (src0 == 0.0f && src1 != 0.0f) ? 1.0f : 0.0f")<br>
> > +<br>
> > +binop_reduce("fdot", 1, tfloat, tfloat, "{src0} * {src1}", "{src0} + {src1}",<br>
> > +             "{src}")<br>
> > +<br>
> > +binop("fmin", tfloat, "", "fminf(src0, src1)")<br>
> > +binop("imin", tint, commutative + associative, "src1 > src0 ? src0 : src1")<br>
> > +binop("umin", tunsigned, commutative + associative, "src1 > src0 ? src0 : src1")<br>
> > +binop("fmax", tfloat, "", "fmaxf(src0, src1)")<br>
> > +binop("imax", tint, commutative + associative, "src1 > src0 ? src1 : src0")<br>
> > +binop("umax", tunsigned, commutative + associative, "src1 > src0 ? src1 : src0")<br>
> > +<br>
> > +binop("fpow", tfloat, "", "powf(src0, src1)")<br>
> > +<br>
> > +binop_horiz("pack_half_2x16_split", 1, tunsigned, 1, tfloat, 1, tfloat,<br>
> > +            "pack_half_1x16(src0.x) | (pack_half_1x16(src1.x) << 16)")<br>
> > +<br>
> > +binop_convert("bfm", tunsigned, tint, "", """<br>
> > +int offset = src0, bits = src1;<br>
> > +if (offset < 0 || bits < 0 || offset + bits > 32)<br>
> > +   dst = 0; /* undefined per the spec */<br>
> > +else<br>
> > +   dst = ((1 << bits)- 1) << offset;<br>
> > +""")<br>
> > +<br>
> > +opcode("ldexp", 0, tunsigned, [0, 0], [tfloat, tint], "", """<br>
> > +dst = ldexp(src0, src1);<br>
> > +/* flush denormals to zero. */<br>
> > +if (!isnormal(dst))<br>
> > +   dst = copysign(0.0f, src0);<br>
> > +""")<br>
> ><br>
> >  # Combines the first component of each input to make a 2-component vector.<br>
> ><br>
> > -binop_horiz("vec2", 2, tunsigned, 1, tunsigned, 1, tunsigned)<br>
> > +binop_horiz("vec2", 2, tunsigned, 1, tunsigned, 1, tunsigned, """<br>
> > +dst.x = src0.x;<br>
> > +dst.y = src1.x;<br>
> > +""")<br>
> ><br>
> > -def triop(name, ty):<br>
> > -   opcode(name, 0, ty, [0, 0, 0], [ty, ty, ty], "")<br>
> > -def triop_horiz(name, output_size, src1_size, src2_size, src3_size):<br>
> > +def triop(name, ty, const_expr):<br>
> > +   opcode(name, 0, ty, [0, 0, 0], [ty, ty, ty], "", const_expr)<br>
> > +def triop_horiz(name, output_size, src1_size, src2_size, src3_size, const_expr):<br>
> >     opcode(name, output_size, tunsigned,<br>
> >     [src1_size, src2_size, src3_size],<br>
> > -   [tunsigned, tunsigned, tunsigned], "")<br>
> > +   [tunsigned, tunsigned, tunsigned], "", const_expr)<br>
> ><br>
> > -# fma(a, b, c) = (a# b) + c<br>
> > -triop("ffma", tfloat)<br>
> > +triop("ffma", tfloat, "src0 * src1 + src2")<br>
> ><br>
> > -triop("flrp", tfloat)<br>
> > +triop("flrp", tfloat, "src0 * (1 - src2) + src1 * src2")<br>
> ><br>
> >  # Conditional Select<br>
> >  #<br>
> > @@ -352,30 +511,83 @@ triop("flrp", tfloat)<br>
> >  # bools (0.0 vs 1.0) and one for integer bools (0 vs ~0).<br>
> ><br>
> ><br>
> > -triop("fcsel", tfloat)<br>
> > +triop("fcsel", tfloat, "(src0 != 0.0f) ? src1 : src2")<br>
> >  opcode("bcsel", 0, tunsigned, [0, 0, 0],<br>
> > -       [tbool, tunsigned, tunsigned], "")<br>
> > -<br>
> > -triop("bfi", tunsigned)<br>
> > -<br>
> > -triop("ubitfield_extract", tunsigned)<br>
> > -opcode("ibitfield_extract", 0, tint, [0, 0, 0],<br>
> > -       [tint, tunsigned, tunsigned], "")<br>
> > +      [tbool, tunsigned, tunsigned], "", "src0 ? src1 : src2")<br>
> > +<br>
> > +triop("bfi", tunsigned, """<br>
> > +unsigned mask = src0, insert = src1 & mask, base = src2;<br>
> > +if (mask == 0) {<br>
> > +   dst = base;<br>
> > +} else {<br>
> > +   unsigned tmp = mask;<br>
> > +   while (!(tmp & 1)) {<br>
> > +      tmp >>= 1;<br>
> > +      insert <<= 1;<br>
> > +   }<br>
> > +   dst = (base & ~mask) | insert;<br>
> > +}<br>
> > +""")<br>
> > +<br>
> > +opcode("ubitfield_extract", 0, tunsigned,<br>
> > +       [0, 1, 1], [tunsigned, tint, tint], "", """<br>
> > +unsigned base = src0;<br>
> > +int offset = src1.x, bits = src2.x;<br>
> > +if (bits == 0) {<br>
> > +   dst = 0;<br>
> > +} else if (bits < 0 || offset < 0 || offset + bits > 32) {<br>
> > +   dst = 0; /* undefined per the spec */<br>
> > +} else {<br>
> > +   dst = (base >> offset) & ((1 << bits) - 1);<br>
> > +}<br>
> > +""")<br>
> > +opcode("ibitfield_extract", 0, tint,<br>
> > +       [0, 1, 1], [tint, tint, tint], "", """<br>
> > +int base = src0;<br>
> > +int offset = src1.x, bits = src2.x;<br>
> > +if (bits == 0) {<br>
> > +   dst = 0;<br>
> > +} else if (offset < 0 || bits < 0 || offset + bits > 32) {<br>
> > +   dst = 0;<br>
> > +} else {<br>
> > +   dst = (base << (32 - offset - bits)) >> offset; /* use sign-extending shift */<br>
> > +}<br>
> > +""")<br>
> ><br>
> >  # Combines the first component of each input to make a 3-component vector.<br>
> ><br>
> > -triop_horiz("vec3", 3, 1, 1, 1)<br>
> > +triop_horiz("vec3", 3, 1, 1, 1, """<br>
> > +dst.x = src0.x;<br>
> > +dst.y = src1.x;<br>
> > +dst.z = src2.x;<br>
> > +""")<br>
> ><br>
> > -def quadop(name):<br>
> > -   opcode(name, 0, tunsigned, [0, 0, 0, 0],<br>
> > -          [tunsigned, tunsigned, tunsigned, tunsigned],<br>
> > -          "")<br>
> > -def quadop_horiz(name, output_size, src1_size, src2_size, src3_size, src4_size):<br>
> > +def quadop_horiz(name, output_size, src1_size, src2_size, src3_size,<br>
> > +                 src4_size, const_expr):<br>
> >     opcode(name, output_size, tunsigned,<br>
> >            [src1_size, src2_size, src3_size, src4_size],<br>
> >            [tunsigned, tunsigned, tunsigned, tunsigned],<br>
> > -          "")<br>
> > +          "", const_expr)<br>
> > +<br>
> > +opcode("bitfield_insert", 0, tunsigned, [0, 0, 1, 1],<br>
> > +       [tunsigned, tunsigned, tint, tint], "", """<br>
> > +unsigned base = src0, insert = src1;<br>
> > +int offset = src2.x, bits = src3.x;<br>
> > +if (bits == 0) {<br>
> > +   dst = 0;<br>
> > +} else if (offset < 0 || bits < 0 || bits + offset > 32) {<br>
> > +   dst = 0;<br>
> > +} else {<br>
> > +   unsigned mask = ((1 << bits) - 1) << offset;<br>
> > +   dst = (base & ~mask) | ((insert << bits) & mask);<br>
> > +}<br>
> > +""")<br>
> > +<br>
> > +quadop_horiz("vec4", 4, 1, 1, 1, 1, """<br>
> > +dst.x = src0.x;<br>
> > +dst.y = src1.x;<br>
> > +dst.z = src2.x;<br>
> > +dst.w = src3.x;<br>
> > +""")<br>
> ><br>
> > -quadop("bitfield_insert")<br>
> ><br>
> > -quadop_horiz("vec4", 4, 1, 1, 1, 1)<br>
> > --<br>
> > 2.2.1<br>
> ><br>
> > _______________________________________________<br>
> > mesa-dev mailing list<br>
> > <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> > <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</p>