nir: Add src/dest num_components helpers
[mesa.git] / src / compiler / nir / nir_constant_expressions.py
index 972d2819af96561bdf77821ac3c92a22272b2808..ee92be51dbeb55d84e22f3fb58f65285bee520f0 100644 (file)
@@ -1,42 +1,52 @@
-#! /usr/bin/python2
+
+import re
+
+type_split_re = re.compile(r'(?P<type>[a-z]+)(?P<bits>\d+)')
 
 def type_has_size(type_):
     return type_[-1:].isdigit()
 
+def type_size(type_):
+    assert type_has_size(type_)
+    return int(type_split_re.match(type_).group('bits'))
+
 def type_sizes(type_):
-    if type_.endswith("8"):
-        return [8]
-    elif type_.endswith("16"):
-        return [16]
-    elif type_.endswith("32"):
-        return [32]
-    elif type_.endswith("64"):
-        return [64]
+    if type_has_size(type_):
+        return [type_size(type_)]
+    elif type_ == 'float':
+        return [16, 32, 64]
     else:
-        return [32, 64]
+        return [8, 16, 32, 64]
 
 def type_add_size(type_, size):
     if type_has_size(type_):
         return type_
     return type_ + str(size)
 
+def op_bit_sizes(op):
+    sizes = None
+    if not type_has_size(op.output_type):
+        sizes = set(type_sizes(op.output_type))
+
+    for input_type in op.input_types:
+        if not type_has_size(input_type):
+            if sizes is None:
+                sizes = set(type_sizes(input_type))
+            else:
+                sizes = sizes.intersection(set(type_sizes(input_type)))
+
+    return sorted(list(sizes)) if sizes is not None else None
+
 def get_const_field(type_):
-    if type_ == "int32":
-        return "i"
-    if type_ == "uint32":
-        return "u"
-    if type_ == "int64":
-        return "l"
-    if type_ == "uint64":
-        return "ul"
     if type_ == "bool32":
-        return "b"
-    if type_ == "float32":
-        return "f"
-    if type_ == "float64":
-        return "d"
-    raise Exception(str(type_))
-    assert(0)
+        return "u32"
+    elif type_ == "float16":
+        return "u16"
+    else:
+        m = type_split_re.match(type_)
+        if not m:
+            raise Exception(str(type_))
+        return m.group('type')[0] + m.group('bits')
 
 template = """\
 /*
@@ -244,11 +254,12 @@ unpack_half_1x16(uint16_t u)
 }
 
 /* Some typed vector structures to make things like src0.y work */
+typedef float float16_t;
 typedef float float32_t;
 typedef double float64_t;
 typedef bool bool32_t;
 % for type in ["float", "int", "uint"]:
-% for width in [32, 64]:
+% for width in type_sizes(type):
 struct ${type}${width}_vec {
    ${type}${width}_t x;
    ${type}${width}_t y;
@@ -265,119 +276,140 @@ struct bool32_vec {
     bool w;
 };
 
-% for name, op in sorted(opcodes.iteritems()):
-static nir_const_value
-evaluate_${name}(unsigned num_components, unsigned bit_size,
-                 nir_const_value *_src)
-{
-   nir_const_value _dst_val = { { {0, 0, 0, 0} } };
-
-   switch (bit_size) {
-   % for bit_size in [32, 64]:
-   case ${bit_size}: {
-      <%
-      output_type = type_add_size(op.output_type, bit_size)
-      input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
-      %>
-
-      ## For each non-per-component input, create a variable srcN that
-      ## contains x, y, z, and w elements which are filled in with the
-      ## appropriately-typed values.
-      % for j in range(op.num_inputs):
-         % if op.input_sizes[j] == 0:
-            <% continue %>
-         % elif "src" + str(j) not in op.const_expr:
-            ## Avoid unused variable warnings
-            <% continue %>
-         %endif
-
-         struct ${input_types[j]}_vec src${j} = {
-         % for k in range(op.input_sizes[j]):
-            % if input_types[j] == "bool32":
-               _src[${j}].u[${k}] != 0,
-            % else:
-               _src[${j}].${get_const_field(input_types[j])}[${k}],
-            % endif
-         % endfor
-         };
+<%def name="evaluate_op(op, bit_size)">
+   <%
+   output_type = type_add_size(op.output_type, bit_size)
+   input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
+   %>
+
+   ## For each non-per-component input, create a variable srcN that
+   ## contains x, y, z, and w elements which are filled in with the
+   ## appropriately-typed values.
+   % for j in range(op.num_inputs):
+      % if op.input_sizes[j] == 0:
+         <% continue %>
+      % elif "src" + str(j) not in op.const_expr:
+         ## Avoid unused variable warnings
+         <% continue %>
+      %endif
+
+      const struct ${input_types[j]}_vec src${j} = {
+      % for k in range(op.input_sizes[j]):
+         % if input_types[j] == "bool32":
+            _src[${j}].u32[${k}] != 0,
+         % elif input_types[j] == "float16":
+            _mesa_half_to_float(_src[${j}].u16[${k}]),
+         % else:
+            _src[${j}].${get_const_field(input_types[j])}[${k}],
+         % endif
       % endfor
+      % for k in range(op.input_sizes[j], 4):
+         0,
+      % endfor
+      };
+   % endfor
 
-      % if op.output_size == 0:
-         ## For per-component instructions, we need to iterate over the
-         ## components and apply the constant expression one component
-         ## at a time.
-         for (unsigned _i = 0; _i < num_components; _i++) {
-            ## For each per-component input, create a variable srcN that
-            ## contains the value of the current (_i'th) component.
-            % for j in range(op.num_inputs):
-               % if op.input_sizes[j] != 0:
-                  <% continue %>
-               % elif "src" + str(j) not in op.const_expr:
-                  ## Avoid unused variable warnings
-                  <% continue %>
-               % elif input_types[j] == "bool32":
-                  bool src${j} = _src[${j}].u[_i] != 0;
-               % else:
-                  ${input_types[j]}_t src${j} =
-                     _src[${j}].${get_const_field(input_types[j])}[_i];
-               % endif
-            % endfor
-
-            ## Create an appropriately-typed variable dst and assign the
-            ## result of the const_expr to it.  If const_expr already contains
-            ## writes to dst, just include const_expr directly.
-            % if "dst" in op.const_expr:
-               ${output_type}_t dst;
-               ${op.const_expr}
-            % else:
-               ${output_type}_t dst = ${op.const_expr};
-            % endif
-
-            ## Store the current component of the actual destination to the
-            ## value of dst.
-            % if output_type == "bool32":
-               ## Sanitize the C value to a proper NIR bool
-               _dst_val.u[_i] = dst ? NIR_TRUE : NIR_FALSE;
+   % if op.output_size == 0:
+      ## For per-component instructions, we need to iterate over the
+      ## components and apply the constant expression one component
+      ## at a time.
+      for (unsigned _i = 0; _i < num_components; _i++) {
+         ## For each per-component input, create a variable srcN that
+         ## contains the value of the current (_i'th) component.
+         % for j in range(op.num_inputs):
+            % if op.input_sizes[j] != 0:
+               <% continue %>
+            % elif "src" + str(j) not in op.const_expr:
+               ## Avoid unused variable warnings
+               <% continue %>
+            % elif input_types[j] == "bool32":
+               const bool src${j} = _src[${j}].u32[_i] != 0;
+            % elif input_types[j] == "float16":
+               const float src${j} =
+                  _mesa_half_to_float(_src[${j}].u16[_i]);
             % else:
-               _dst_val.${get_const_field(output_type)}[_i] = dst;
+               const ${input_types[j]}_t src${j} =
+                  _src[${j}].${get_const_field(input_types[j])}[_i];
             % endif
-         }
-      % else:
-         ## In the non-per-component case, create a struct dst with
-         ## appropriately-typed elements x, y, z, and w and assign the result
-         ## of the const_expr to all components of dst, or include the
-         ## const_expr directly if it writes to dst already.
-         struct ${output_type}_vec dst;
+         % endfor
 
+         ## Create an appropriately-typed variable dst and assign the
+         ## result of the const_expr to it.  If const_expr already contains
+         ## writes to dst, just include const_expr directly.
          % if "dst" in op.const_expr:
+            ${output_type}_t dst;
+
             ${op.const_expr}
          % else:
-            ## Splat the value to all components.  This way expressions which
-            ## write the same value to all components don't need to explicitly
-            ## write to dest.  One such example is fnoise which has a
-            ## const_expr of 0.0f.
-            dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
+            ${output_type}_t dst = ${op.const_expr};
          % endif
 
-         ## For each component in the destination, copy the value of dst to
-         ## the actual destination.
-         % for k in range(op.output_size):
-            % if output_type == "bool32":
-               ## Sanitize the C value to a proper NIR bool
-               _dst_val.u[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
-            % else:
-               _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
-            % endif
-         % endfor
+         ## Store the current component of the actual destination to the
+         ## value of dst.
+         % if output_type == "bool32":
+            ## Sanitize the C value to a proper NIR bool
+            _dst_val.u32[_i] = dst ? NIR_TRUE : NIR_FALSE;
+         % elif output_type == "float16":
+            _dst_val.u16[_i] = _mesa_float_to_half(dst);
+         % else:
+            _dst_val.${get_const_field(output_type)}[_i] = dst;
+         % endif
+      }
+   % else:
+      ## In the non-per-component case, create a struct dst with
+      ## appropriately-typed elements x, y, z, and w and assign the result
+      ## of the const_expr to all components of dst, or include the
+      ## const_expr directly if it writes to dst already.
+      struct ${output_type}_vec dst;
+
+      % if "dst" in op.const_expr:
+         ${op.const_expr}
+      % else:
+         ## Splat the value to all components.  This way expressions which
+         ## write the same value to all components don't need to explicitly
+         ## write to dest.  One such example is fnoise which has a
+         ## const_expr of 0.0f.
+         dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
       % endif
 
-      break;
-   }
-   % endfor
+      ## For each component in the destination, copy the value of dst to
+      ## the actual destination.
+      % for k in range(op.output_size):
+         % if output_type == "bool32":
+            ## Sanitize the C value to a proper NIR bool
+            _dst_val.u32[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
+         % elif output_type == "float16":
+            _dst_val.u16[${k}] = _mesa_float_to_half(dst.${"xyzw"[k]});
+         % else:
+            _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
+         % endif
+      % endfor
+   % endif
+</%def>
 
-   default:
-      unreachable("unknown bit width");
-   }
+% for name, op in sorted(opcodes.iteritems()):
+static nir_const_value
+evaluate_${name}(MAYBE_UNUSED unsigned num_components,
+                 ${"UNUSED" if op_bit_sizes(op) is None else ""} unsigned bit_size,
+                 MAYBE_UNUSED nir_const_value *_src)
+{
+   nir_const_value _dst_val = { {0, } };
+
+   % if op_bit_sizes(op) is not None:
+      switch (bit_size) {
+      % for bit_size in op_bit_sizes(op):
+      case ${bit_size}: {
+         ${evaluate_op(op, bit_size)}
+         break;
+      }
+      % endfor
+
+      default:
+         unreachable("unknown bit width");
+      }
+   % else:
+      ${evaluate_op(op, 0)}
+   % endif
 
    return _dst_val;
 }
@@ -389,10 +421,8 @@ nir_eval_const_opcode(nir_op op, unsigned num_components,
 {
    switch (op) {
 % for name in sorted(opcodes.iterkeys()):
-   case nir_op_${name}: {
+   case nir_op_${name}:
       return evaluate_${name}(num_components, bit_width, src);
-      break;
-   }
 % endfor
    default:
       unreachable("shouldn't get here");
@@ -405,4 +435,5 @@ from mako.template import Template
 print Template(template).render(opcodes=opcodes, type_sizes=type_sizes,
                                 type_has_size=type_has_size,
                                 type_add_size=type_add_size,
+                                op_bit_sizes=op_bit_sizes,
                                 get_const_field=get_const_field)