<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 19, 2015 at 5:01 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Mon, Jan 19, 2015 at 4:04 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> I've got some specific comments below, but I want to make some more general<br>
> comments here. I like this in principle: having all the opcodes<br>
> self-documenting is wonderful. However, I'm not terribly happy with the way<br>
> it worked out. A lot of the codegen stuff is very confusing and its not at<br>
> all obvious what's going on. I'll give it some thought and see if I can<br>
> come up with a good way to clean it up.<br>
><br>
> On Jan 16, 2015 3:46 PM, "Connor Abbott" <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>> wrote:<br>
>><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<br>
>> function<br>
>> that takes an opcode and an array of constant inputs and spits out the<br>
>> constant<br>
>> result. This means that when adding opcodes, there's one less place to<br>
>> update,<br>
>> and almost all the opcodes are self-documenting since the information on<br>
>> 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>
>> v2: use Python formatting and get rid of regex's<br>
>> Signed-off-by: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
>> ---<br>
>> src/glsl/Makefile.am | 10 +-<br>
>> src/glsl/Makefile.sources | 3 +-<br>
>> src/glsl/nir/.gitignore | 1 +<br>
>> src/glsl/nir/nir_constant_expressions.h | 32 ++<br>
>> src/glsl/nir/nir_constant_expressions.py | 320 ++++++++++++++++++<br>
>> src/glsl/nir/nir_opcodes.py | 562<br>
>> +++++++++++++++++++++----------<br>
>> 6 files changed, 740 insertions(+), 188 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 b2fe16a..51036b7 100644<br>
>> --- a/src/glsl/Makefile.am<br>
>> +++ b/src/glsl/Makefile.am<br>
>> @@ -210,7 +210,8 @@ BUILT_SOURCES =<br>
>> \<br>
>> glcpp/glcpp-lex.c \<br>
>> nir/nir_opt_algebraic.c \<br>
>> nir/nir_opcodes.h \<br>
>> - nir/nir_opcodes.c<br>
>> + nir/nir_opcodes.c \<br>
>> + nir/nir_constant_expressions.c<br>
>> CLEANFILES = \<br>
>> glcpp/glcpp-parse.h \<br>
>> glsl_parser.h \<br>
>> @@ -236,3 +237,10 @@ nir/nir_opcodes.c: nir/nir_opcodes.py<br>
>> nir/nir_opcodes_c.py<br>
>> $(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/nir/nir_opcodes_c.py > $@<br>
>><br>
>> nir/nir.h: nir/nir_opcodes.h<br>
>> +<br>
>> +nir/nir_constant_expressions.c: nir/nir_opcodes.py<br>
>> nir/nir_constant_expressions.py nir/nir_constant_expressions.h<br>
>> + $(AM_V_GEN)set -e;<br>
>> \<br>
>> + $(MKDIR_P) `dirname $@`;<br>
>> \<br>
>> + $(PYTHON2) $(PYTHON_FLAGS)<br>
>> $(srcdir)/nir/nir_constant_expressions.py > $@.tmp; \<br>
>> + mv $@.tmp $@;<br>
>> +<br>
>> diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
>> index 03b4f2e..9dd1a56 100644<br>
>> --- a/src/glsl/Makefile.sources<br>
>> +++ b/src/glsl/Makefile.sources<br>
>> @@ -16,7 +16,8 @@ LIBGLCPP_GENERATED_FILES = \<br>
>> NIR_GENERATED_FILES = \<br>
>> $(GLSL_BUILDDIR)/nir/nir_opt_algebraic.c \<br>
>> $(GLSL_BUILDDIR)/nir/nir_opcodes.h \<br>
>> - $(GLSL_BUILDDIR)/nir/nir_opcodes.c<br>
>> + $(GLSL_BUILDDIR)/nir/nir_opcodes.c \<br>
>> + $(GLSL_BUILDDIR)/nir/nir_constant_expressions.c<br>
>><br>
>> NIR_FILES = \<br>
>> $(GLSL_SRCDIR)/nir/nir.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<br>
>> b/src/glsl/nir/nir_constant_expressions.h<br>
>> new file mode 100644<br>
>> index 0000000..4ca09be<br>
>> --- /dev/null<br>
>> +++ b/src/glsl/nir/nir_constant_expressions.h<br>
>> @@ -0,0 +1,32 @@<br>
>> +/*<br>
>> + * Copyright © 2014 Connor Abbott<br>
>> + *<br>
>> + * Permission is hereby granted, free of charge, to any person obtaining<br>
>> a<br>
>> + * copy of this software and associated documentation files (the<br>
>> "Software"),<br>
>> + * to deal in the Software without restriction, including without<br>
>> limitation<br>
>> + * the rights to use, copy, modify, merge, publish, distribute,<br>
>> 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<br>
>> next<br>
>> + * paragraph) shall be included in all copies or substantial portions of<br>
>> the<br>
>> + * Software.<br>
>> + *<br>
>> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,<br>
>> EXPRESS OR<br>
>> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF<br>
>> MERCHANTABILITY,<br>
>> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT<br>
>> SHALL<br>
>> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
>> OTHER<br>
>> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
>> ARISING<br>
>> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER<br>
>> 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>
>> +<br>
>> diff --git a/src/glsl/nir/nir_constant_expressions.py<br>
>> b/src/glsl/nir/nir_constant_expressions.py<br>
>> new file mode 100644<br>
>> index 0000000..8498fc3<br>
>> --- /dev/null<br>
>> +++ b/src/glsl/nir/nir_constant_expressions.py<br>
>> @@ -0,0 +1,320 @@<br>
>> +#! /usr/bin/env python<br>
>> +#<br>
>> +# Copyright (C) 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<br>
>> "Software"),<br>
>> +# to deal in the Software without restriction, including without<br>
>> limitation<br>
>> +# the rights to use, copy, modify, merge, publish, distribute,<br>
>> 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<br>
>> next<br>
>> +# paragraph) shall be included in all copies or substantial portions of<br>
>> the<br>
>> +# Software.<br>
>> +#<br>
>> +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS<br>
>> OR<br>
>> +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF<br>
>> MERCHANTABILITY,<br>
>> +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT<br>
>> SHALL<br>
>> +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
>> 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<br>
>> DEALINGS<br>
>> +# IN THE SOFTWARE.<br>
>> +#<br>
>> +# Authors:<br>
>> +# Connor Abbott (<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>)<br>
>> +<br>
>> +from nir_opcodes import opcodes<br>
>> +from mako.template import Template<br>
>> +<br>
>> +# the const_expr string for each opcode has a few shortcuts - most only<br>
>> have<br>
>> +# an expression, and the "dst = (expr);" is implied. In addition,<br>
>> +# per-component inputs and outputs are referenced without any subscripts,<br>
>> so<br>
>> +# we need to create the implicit for-loop for per-component opcodes. In<br>
>> +# addition, we need to expand out the src0, src1, etc. with actual<br>
>> sources<br>
>> +# with the appropriate type using the union.<br>
>> +<br>
>> +def wr(string, wrap_bool):<br>
>> + if wrap_bool:<br>
>> + return "((" + string + ") ? NIR_TRUE : NIR_FALSE)"<br>
>> + return string<br>
>> +<br>
>> +class Operand(object):<br>
>> + def __init__(self, name, type_, is_src):<br>
>> + if type_ == "bool" or type_ == "unsigned":<br>
>> + prefix = "u"<br>
>> + elif type_ == "int":<br>
>> + prefix = "i"<br>
>> + else:<br>
>> + prefix = "f"<br>
>> +<br>
>> + wrap_bool = is_src and type_ == "bool"<br>
>> +<br>
>> + def wr(string, wrap=wrap_bool):<br>
>> + if wrap:<br>
>> + return "(" + string + " != NIR_FALSE)"<br>
>> + return string<br>
><br>
> Wow, this is confusing... You redefine a global function locally to do<br>
> something similar but very different. Then give it two parameters one of<br>
> which has a default value that comes from the argument of the parent<br>
> function. On top of that, its never used with its argument so the argument<br>
> isn't needed at all. I think we can do better than this...<br>
<br>
</div></div>Whoops, I meant to delete the global version of wr() above :/ also, I<br>
can rename it to wrap() if you want. I added the argument because I<br>
thought it was necessary, but it turns out Python does support a weak<br>
form of closures (it won't keep them alive once you escape the context<br>
of the outer function), so this isn't actually necessary. I'll fix<br>
this, plus a similar thing in unop_reduce() and binop_reduce().<br>
<span class=""><br>
><br>
>> +<br>
>> + <a href="http://self.name" target="_blank">self.name</a> = wr(name + "." + prefix + "[_i]")<br>
><br>
> Calling that "name" seems kind of odd.<br>
<br>
</span>Yeah, I called it that because I was thinking of it as the "name" of<br>
the operand since calling str() on the object would return it, but it<br>
seems like "per_component" would probably be a better name.<br>
<div><div class="h5"><br>
><br>
>> + self.x = wr(name + "." + prefix + "[0]")<br>
>> + self.y = wr(name + "." + prefix + "[1]")<br>
>> + self.z = wr(name + "." + prefix + "[2]")<br>
>> + self.w = wr(name + "." + prefix + "[3]")<br>
>> +<br>
>> + def __str__(self):<br>
>> + return <a href="http://self.name" target="_blank">self.name</a><br>
>> +<br>
>> +def expand_constexpr(opcode):<br>
>> + const_expr = opcode.const_expr<br>
>> +<br>
>> + if "dst" not in const_expr:<br>
>> + if opcode.output_type == "bool":<br>
>> + # For convenience, insert the conversion to unsigned.<br>
>> + # Note that we don't do this for things that aren't expressions.<br>
>> + const_expr = "(" + const_expr + ") ? NIR_TRUE : NIR_FALSE"<br>
>> +<br>
>> + if opcode.output_size == 0:<br>
>> + const_expr = "{dst} = " + const_expr + ";"<br>
>> + else:<br>
>> + # for non-per-component opcodes, assume we broadcast to all<br>
>> components<br>
>> + const_expr = "\n".join(<br>
>> + "{dst." + "xyzw"[i] + "} = " + const_expr + ";"<br>
>> + for i in range(opcode.output_size))<br>
>> +<br>
>> + replacement_dict = {<br>
>> + "src" + str(i) : Operand("src[" + str(i) + "]",<br>
>> opcode.input_types[i], True)<br>
>> + for i in range(opcode.num_inputs)<br>
>> + }<br>
>> +<br>
>> + replacement_dict["dst"] = Operand("dst", opcode.output_type, False)<br>
>> +<br>
>> + const_expr = const_expr.format(**replacement_dict)<br>
>> +<br>
>> + if opcode.output_size == 0:<br>
>> + const_expr = "for (unsigned _i = 0; _i < num_components; _i++) {" +<br>
>> const_expr + "}"<br>
>> +<br>
>> + return const_expr<br>
><br>
> I can't help but think that the above is far more confusing than needed.<br>
> Also, I'd rather do as much of this in the mako as possible. That's why we<br>
> are using a templating language after all.<br>
<br>
</div></div>Is there anything in Mako that would really help us here? The things<br>
that need to be done are:<br>
<br>
- replacing a pure expression with "dst = ..." or "dst = (...) ?<br>
NIR_TRUE : NIR_FALSE" for bools<br>
- adding the for loop for per-component things<br>
- replacing e.g. ${src0.x} with src[0].f[0] for floats or (src[0].u[0]<br>
!= NIR_FALSE) for bools<br>
<br>
The problem with Mako is that the only thing it offers is the ability<br>
to do fancier formatting in the source string, when we don't really<br>
need that: we want the source string to be as simple as possible. It's<br>
the transformations on the source string that are more complicated, so<br>
what we're doing is closer to text substitution than formatting. That<br>
being said, if you can come up with something better than this or the<br>
pile of regex's that we had before, I'm all ears.<br></blockquote><div><br></div><div>I think this can be cleaned up later. It'll be easier to do that as a patch on top of this. Let's deal with it later.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
><br>
>> +<br>
>> +<br>
>> +const_exprs = {name : expand_constexpr(opcode)<br>
>> + for name, opcode in opcodes.iteritems()}<br>
>> +<br>
>> +template = Template("""<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<br>
>> 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<br>
>> 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) *<br>
>> 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<br>
>> 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<br>
>> 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>
>> +nir_const_value<br>
>> +nir_eval_const_opcode(nir_op op, unsigned num_components,<br>
>> + nir_const_value *src)<br>
>> +{<br>
>> + nir_const_value dst = {<br>
>> + .u = {0, 0, 0, 0}<br>
>> + };<br>
>> +<br>
>> + switch (op) {<br>
>> +% for name, const_expr in sorted(const_exprs.iteritems()):<br>
>> + case nir_op_${name}: {<br>
>> + ${const_expr}<br>
>> + break;<br>
>> + }<br>
>> +% endfor<br>
>> + case nir_num_opcodes: unreachable("shouldn't get here");<br>
>> + }<br>
>> +<br>
>> + return dst;<br>
>> +}<br>
>> +""")<br>
>> +<br>
>> +print template.render(const_exprs=const_exprs)<br>
>> +<br>
>> diff --git a/src/glsl/nir/nir_opcodes.py b/src/glsl/nir/nir_opcodes.py<br>
>> index fa2f563..6f3c5ba 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>
>> @@ -34,7 +35,7 @@ class Opcode:<br>
>> # algebraic_properties is a space-seperated string,<br>
>> # where nir_op_is_ is prepended before each entry<br>
>> def __init__(self, name, output_size, output_type, input_sizes,<br>
>> - input_types, algebraic_properties):<br>
>> + input_types, algebraic_properties, const_expr):<br>
>> assert(isinstance(name, str))<br>
>> assert(isinstance(output_size, int))<br>
>> assert(isinstance(output_type, str))<br>
>> @@ -43,6 +44,7 @@ class Opcode:<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>
>> @@ -56,6 +58,7 @@ class Opcode:<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>
>> @@ -70,178 +73,290 @@ 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,<br>
>> prereduce_expr,<br>
>> + reduce_expr, final_expr):<br>
>> + def prereduce(src, expr=prereduce_expr):<br>
>> + return "(" + expr.format(src=src) + ")"<br>
>> + def final(src, expr=final_expr):<br>
>> + return expr.format(src="(" + src + ")")<br>
>> + def reduce_(src0, src1, expr=reduce_expr):<br>
>> + return 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>
> I really don't like the way this worked out. That said, I can't come up<br>
> with anything better at the moment, so I won't complain too much.<br>
<br>
</div></div>Meh, I think it's ok... the only thing is the extra arguments to<br>
prereduce(), final(), and reduce_() that we can get rid of.<br></blockquote><div><br></div><div>Like I said. I can't come up with anything better. Assuming the two changes above are made (wr and per_component), this is<br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com">jason.ekstrand@intel.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
><br>
>><br>
>> # These two move instructions differ in what modifiers they support and<br>
>> 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<br>
>> src[0] != 0<br>
>> -unop_reduce("ball", 1, tbool, tbool) # returns ~0 if all components of<br>
>> src[0] != 0<br>
>> -unop_reduce("fany", 1, tfloat, tfloat) # returns 1.0 if any component of<br>
>> src[0] != 0<br>
>> -unop_reduce("fall", 1, tfloat, tfloat) # returns 1.0 if all components of<br>
>> 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<br>
>> : -1.0f)")<br>
>> +unop("isign", tint, "({src0} == 0) ? 0 : ((src > 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 :<br>
>> {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<br>
>> conversion.<br>
>> +unop_convert("f2u", tfloat, tunsigned, "{src0}") # Float-to-unsigned<br>
>> conversion<br>
>> +unop_convert("i2f", tint, tfloat, "{src0}") # Integer-to-float<br>
>> 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<br>
>> conversion<br>
>> +unop_convert("u2f", tunsigned, tfloat, "{src0}") #Unsigned-to-float<br>
>> conversion.<br>
>> +<br>
>> +unop_reduce("bany", 1, tbool, tbool, "{src}", "{src0} || {src1}",<br>
>> "{src}")<br>
>> +unop_reduce("ball", 1, tbool, tbool, "{src}", "{src0} && {src1}",<br>
>> "{src}")<br>
>> +unop_reduce("fany", 1, tfloat, tfloat, "{src} != 0.0f", "{src0} ||<br>
>> {src1}",<br>
>> + "{src} ? 1.0f : 0.0f")<br>
>> +unop_reduce("fall", 1, tfloat, tfloat, "{src} != 0.0f", "{src0} &&<br>
>> {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>
> We do have helpers for most of the above. I was moving them onto util but<br>
> got sidetracked. I should rework those patches.<br>
<br>
</div></div>Sure, and while we're at it we can remove the hand-written versions in<br>
the GLSL IR constant folding code as well.<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
>><br>
>><br>
>> for i in range(1, 5):<br>
>> for j in range(1, 5):<br>
>> - unop_horiz("fnoise" + str(i) + "_" + str(j), i, tfloat, j, tfloat)<br>
>> + unop_horiz("fnoise" + str(i) + "_" + str(j), i, tfloat, j, tfloat,<br>
>> + "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,<br>
>> 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,<br>
>> src2_size,<br>
>> - src2_type):<br>
>> - opcode(name, out_size, out_type, [src1_size, src2_size], [src1_type,<br>
>> 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,<br>
>> src2_type],<br>
>> + "", const_expr)<br>
>> +<br>
>> +def binop_reduce(name, output_size, output_type, src_type,<br>
>> prereduce_expr,<br>
>> + reduce_expr, final_expr):<br>
>> + def final(src, expr=final_expr):<br>
>> + return expr.format(src= "(" + src + ")")<br>
>> + def reduce_(src0, src1, expr=reduce_expr):<br>
>> + return expr.format(src0=src0, src1=src1)<br>
>> + def prereduce(src0, src1, expr=prereduce_expr):<br>
>> + return "(" + 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} +<br>
>> {src1} < {src0}")<br>
>><br>
>> # returns a boolean representing the borrow resulting from the<br>
>> 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>
>> @@ -250,41 +365,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} == {src0}",<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<br>
>> Than<br>
>> +binop("sge", tfloat, "", "({src0} >= {src1}) ? 1.0f : 0.0f") # Set on<br>
>> Greater or Equal<br>
>> +binop("seq", tfloat, commutative, "({src0} == {src1}) ? 1.0f : 0.0f") #<br>
>> Set on Equal<br>
>> +binop("sne", tfloat, commutative, "({src0} != {src1}) ? 1.0f : 0.0f") #<br>
>> 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>
>> @@ -292,9 +413,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>
>> @@ -302,42 +423,60 @@ binop("ixor", tunsigned, commutative + associative)<br>
>> # These use (src != 0.0) for testing the truth of the input, and output<br>
>> 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, commutative + associative)<br>
>> -binop("imin", tint, commutative + associative)<br>
>> -binop("umin", tunsigned, commutative + associative)<br>
>> -binop("fmax", tfloat, commutative + associative)<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} !=<br>
>> 0.0f) ? 1.0f : 0.0f")<br>
>> +<br>
>> +binop_reduce("fdot", 1, tfloat, tfloat, "{src0} * {src1}", "{src0} +<br>
>> {src1}",<br>
>> + "{src}")<br>
>> +<br>
>> +binop("fmin", tfloat, commutative + associative, "fminf({src0}, {src1})")<br>
>> +binop("imin", tint, commutative + associative, "{src1} > {src0} ? {src0}<br>
>> : {src1}")<br>
>> +binop("umin", tunsigned, commutative + associative, "{src1} > {src0} ?<br>
>> {src0} : {src1}")<br>
>> +binop("fmax", tfloat, commutative + associative, "fmaxf({src0}, {src1})")<br>
>> +binop("imax", tint, commutative + associative, "{src1} > {src0} ? {src1}<br>
>> : {src0}")<br>
>> +binop("umax", tunsigned, commutative + associative, "{src1} > {src0} ?<br>
>> {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}) <<<br>
>> 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<br>
>> 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,<br>
>> 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>
>> @@ -346,32 +485,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<br>
>> sign-extending shift */<br>
>> +}}<br>
>> +""")<br>
>><br>
>> # Combines the first component of each input to make a 3-component<br>
>> 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,<br>
>> 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>
>> -<br>
>> -quadop("bitfield_insert")<br>
>> -<br>
>> -quadop_horiz("vec4", 4, 1, 1, 1, 1)<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>
>><br>
>> --<br>
>> 2.1.0<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" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</div></div></blockquote></div><br></div></div>