<div dir="ltr">Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 29, 2017 at 6:07 PM, Jose Maria Casanova Crespo <span dir="ltr"><<a href="mailto:jmcasanova@igalia.com" target="_blank">jmcasanova@igalia.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">nir_type_conversion enables new operations to handle rounding modes to<br>
convert to fp16 values. Two new opcodes are enabled nir_op_f2f16_rtne<br>
and nir_op_f2f16_rtz.<br>
<br>
The undefined behaviour doesn't has any effect and uses the original<br>
nir_op_f2f16 operation.<br>
<br>
v2: Indentation fixed (Jason Ekstrand)<br>
<br>
v3: Use explicit case for undefined rounding and assert if<br>
    rounding mode is used for non 16-bit float conversions<br>
    (Jason Ekstrand)<br>
---<br>
 src/compiler/glsl/glsl_to_nir.<wbr>cpp |  3 ++-<br>
 src/compiler/nir/nir.h            |  3 ++-<br>
 src/compiler/nir/nir_opcodes.<wbr>py   | 11 +++++++++--<br>
 src/compiler/nir/nir_opcodes_<wbr>c.py | 15 ++++++++++++++-<br>
 src/compiler/spirv/vtn_alu.c      |  2 +-<br>
 5 files changed, 28 insertions(+), 6 deletions(-)<br>
<br>
diff --git a/src/compiler/glsl/glsl_to_<wbr>nir.cpp b/src/compiler/glsl/glsl_to_<wbr>nir.cpp<br>
index 33ebc13edb..1e636225c1 100644<br>
--- a/src/compiler/glsl/glsl_to_<wbr>nir.cpp<br>
+++ b/src/compiler/glsl/glsl_to_<wbr>nir.cpp<br>
@@ -1575,7 +1575,8 @@ nir_visitor::visit(ir_<wbr>expression *ir)<br>
    case ir_unop_u642i64: {<br>
       nir_alu_type src_type = nir_get_nir_type_for_glsl_<wbr>base_type(types[0]);<br>
       nir_alu_type dst_type = nir_get_nir_type_for_glsl_<wbr>base_type(out_type);<br>
-      result = nir_build_alu(&b, nir_type_conversion_op(src_<wbr>type, dst_type),<br>
+      result = nir_build_alu(&b, nir_type_conversion_op(src_<wbr>type, dst_type,<br>
+                                 nir_rounding_mode_undef),<br>
                                  srcs[0], NULL, NULL, NULL);<br>
       /* b2i and b2f don't have fixed bit-size versions so the builder will<br>
        * just assume 32 and we have to fix it up here.<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 883f371d1f..c70c0e0220 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -761,7 +761,8 @@ nir_get_nir_type_for_glsl_<wbr>type(const struct glsl_type *type)<br>
    return nir_get_nir_type_for_glsl_<wbr>base_type(glsl_get_base_type(<wbr>type));<br>
 }<br>
<br>
-nir_op nir_type_conversion_op(nir_<wbr>alu_type src, nir_alu_type dst);<br>
+nir_op nir_type_conversion_op(nir_<wbr>alu_type src, nir_alu_type dst,<br>
+                              nir_rounding_mode rnd);<br>
<br>
 typedef enum {<br>
    NIR_OP_IS_COMMUTATIVE = (1 << 0),<br>
diff --git a/src/compiler/nir/nir_<wbr>opcodes.py b/src/compiler/nir/nir_<wbr>opcodes.py<br>
index 28a0467228..ac7333fe78 100644<br>
--- a/src/compiler/nir/nir_<wbr>opcodes.py<br>
+++ b/src/compiler/nir/nir_<wbr>opcodes.py<br>
@@ -179,8 +179,15 @@ for src_t in [tint, tuint, tfloat]:<br>
       else:<br>
          bit_sizes = [8, 16, 32, 64]<br>
       for bit_size in bit_sizes:<br>
-         unop_convert("{0}2{1}{2}".<wbr>format(src_t[0], dst_t[0], bit_size),<br>
-                      dst_t + str(bit_size), src_t, "src0")<br>
+          if bit_size == 16 and dst_t == tfloat and src_t == tfloat:<br>
+              rnd_modes = ['rtne', 'rtz', 'undef']<br>
+              for rnd_mode in rnd_modes:<br>
+                  unop_convert("{0}2{1}{2}_{3}".<wbr>format(src_t[0], dst_t[0],<br>
+                                                       bit_size, rnd_mode),<br>
+                               dst_t + str(bit_size), src_t, "src0")<br>
+          else:<br>
+              unop_convert("{0}2{1}{2}".<wbr>format(src_t[0], dst_t[0], bit_size),<br>
+                           dst_t + str(bit_size), src_t, "src0")<br>
<br>
 # We'll hand-code the to/from bool conversion opcodes.  Because bool doesn't<br>
 # have multiple bit-sizes, we can always infer the size from the other type.<br>
diff --git a/src/compiler/nir/nir_<wbr>opcodes_c.py b/src/compiler/nir/nir_<wbr>opcodes_c.py<br>
index 02bb4738ed..c19185534a 100644<br>
--- a/src/compiler/nir/nir_<wbr>opcodes_c.py<br>
+++ b/src/compiler/nir/nir_<wbr>opcodes_c.py<br>
@@ -30,7 +30,7 @@ template = Template("""<br>
 #include "nir.h"<br>
<br>
 nir_op<br>
-nir_type_conversion_op(nir_<wbr>alu_type src, nir_alu_type dst)<br>
+nir_type_conversion_op(nir_<wbr>alu_type src, nir_alu_type dst, nir_rounding_mode rnd)<br>
 {<br>
    nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(<wbr>src);<br>
    nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(<wbr>dst);<br>
@@ -64,7 +64,20 @@ nir_type_conversion_op(nir_<wbr>alu_type src, nir_alu_type dst)<br>
                switch (dst_bit_size) {<br>
 %                 for dst_bits in [16, 32, 64]:<br>
                   case ${dst_bits}:<br>
+%                    if src_t == 'float' and dst_t == 'float' and dst_bits == 16:<br>
+                     switch(rnd) {<br>
+%                       for rnd_t in ['rtne', 'rtz', 'undef']:<br>
+                        case nir_rounding_mode_${rnd_t}:<br>
+                           return ${'nir_op_{0}2{1}{2}_{3}'.<wbr>format(src_t[0], dst_t[0],<br>
+                                                                   dst_bits, rnd_t)};<br>
+%                       endfor<br>
+                        default:<br>
+                           unreachable("Invalid 16-bit nir rounding mode");<br>
+                     }<br>
+%                    else:<br>
+                     assert(rnd == nir_rounding_mode_undef);<br>
                      return ${'nir_op_{0}2{1}{2}'.format(<wbr>src_t[0], dst_t[0], dst_bits)};<br>
+%                    endif<br>
 %                 endfor<br>
                   default:<br>
                      unreachable("Invalid nir alu bit size");<br>
diff --git a/src/compiler/spirv/vtn_alu.c b/src/compiler/spirv/vtn_alu.c<br>
index ecf9cbc34d..7ec30b8a63 100644<br>
--- a/src/compiler/spirv/vtn_alu.c<br>
+++ b/src/compiler/spirv/vtn_alu.c<br>
@@ -355,7 +355,7 @@ vtn_nir_alu_op_for_spirv_<wbr>opcode(SpvOp opcode, bool *swap,<br>
    case SpvOpConvertUToF:<br>
    case SpvOpSConvert:<br>
    case SpvOpFConvert:<br>
-      return nir_type_conversion_op(src, dst);<br>
+      return nir_type_conversion_op(src, dst, nir_rounding_mode_undef);<br>
<br>
    /* Derivatives: */<br>
    case SpvOpDPdx:         return nir_op_fddx;<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.14.3<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div>