<div dir="ltr"><div><div>Matt,<br></div>Care to take this one? I'm not that familiar with roundeven.<br></div>--Jason<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 12, 2016 at 1:05 AM, Samuel Iglesias Gonsálvez <span dir="ltr"><<a href="mailto:siglesias@igalia.com" target="_blank">siglesias@igalia.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Iago Toral Quiroga <<a href="mailto:itoral@igalia.com">itoral@igalia.com</a>><br>
<br>
At least i965 hardware does not have native support for round_even() on doubles.<br>
---<br>
src/compiler/nir/nir.h | 3 +-<br>
src/compiler/nir/nir_lower_double_ops.c | 58 +++++++++++++++++++++++++++++++++<br>
2 files changed, 60 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 4a57e74..0d17ce0 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2289,7 +2289,8 @@ typedef enum {<br>
nir_lower_dtrunc = (1 << 3),<br>
nir_lower_dfloor = (1 << 4),<br>
nir_lower_dceil = (1 << 5),<br>
- nir_lower_dfract = (1 << 6)<br>
+ nir_lower_dfract = (1 << 6),<br>
+ nir_lower_dround_even = (1 << 7)<br>
} nir_lower_doubles_options;<br>
<br>
void nir_lower_doubles(nir_shader *shader, nir_lower_doubles_options options);<br>
diff --git a/src/compiler/nir/nir_lower_double_ops.c b/src/compiler/nir/nir_lower_double_ops.c<br>
index 0f42c21..3d98075 100644<br>
--- a/src/compiler/nir/nir_lower_double_ops.c<br>
+++ b/src/compiler/nir/nir_lower_double_ops.c<br>
@@ -423,6 +423,55 @@ lower_fract(nir_builder *b, nir_ssa_def *src)<br>
return nir_fsub(b, src, nir_ffloor(b, src));<br>
}<br>
<br>
+static nir_ssa_def *<br>
+lower_round_even(nir_builder *b, nir_ssa_def *src)<br>
+{<br>
+ /* If fract(src) == 0.5, then we will have to decide the rounding direction.<br>
+ * We will do this by computing the mod(abs(src), 2) and testing if it<br>
+ * is < 1 or not.<br>
+ *<br>
+ * We compute mod(abs(src), 2) as:<br>
+ * abs(src) - 2.0 * floor(abs(src) / 2.0)<br>
+ */<br>
+ nir_ssa_def *two = nir_imm_double(b, 2.0);<br>
+ nir_ssa_def *abs_src = nir_fabs(b, src);<br>
+ nir_ssa_def *mod =<br>
+ nir_fsub(b,<br>
+ abs_src,<br>
+ nir_fmul(b,<br>
+ two,<br>
+ nir_ffloor(b,<br>
+ nir_fmul(b,<br>
+ abs_src,<br>
+ nir_imm_double(b, 0.5)))));<br>
+<br>
+ /*<br>
+ * If fract(src) != 0.5, then we round as floor(src + 0.5)<br>
+ *<br>
+ * If fract(src) == 0.5, then we have to check the modulo:<br>
+ *<br>
+ * if it is < 1 we need a trunc operation so we get:<br>
+ * 0.5 -> 0, -0.5 -> -0<br>
+ * 2.5 -> 2, -2.5 -> -2<br>
+ *<br>
+ * otherwise we need to check if src >= 0, in which case we need to round<br>
+ * upwards, or not, in which case we need to round downwards so we get:<br>
+ * 1.5 -> 2, -1.5 -> -2<br>
+ * 3.5 -> 4, -3.5 -> -4<br>
+ */<br>
+ nir_ssa_def *fract = nir_ffract(b, src);<br>
+ return nir_bcsel(b,<br>
+ nir_fne(b, fract, nir_imm_double(b, 0.5)),<br>
+ nir_ffloor(b, nir_fadd(b, src, nir_imm_double(b, 0.5))),<br>
+ nir_bcsel(b,<br>
+ nir_flt(b, mod, nir_imm_double(b, 1.0)),<br>
+ nir_ftrunc(b, src),<br>
+ nir_bcsel(b,<br>
+ nir_fge(b, src, nir_imm_double(b, 0.0)),<br>
+ nir_fadd(b, src, nir_imm_double(b, 0.5)),<br>
+ nir_fsub(b, src, nir_imm_double(b, 0.5)))));<br>
+}<br>
+<br>
static void<br>
lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
{<br>
@@ -466,6 +515,11 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
return;<br>
break;<br>
<br>
+ case nir_op_fround_even:<br>
+ if (!(options & nir_lower_dround_even))<br>
+ return;<br>
+ break;<br>
+<br>
default:<br>
return;<br>
}<br>
@@ -501,6 +555,10 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
case nir_op_ffract:<br>
result = lower_fract(&bld, src);<br>
break;<br>
+ case nir_op_fround_even:<br>
+ result = lower_round_even(&bld, src);<br>
+ break;<br>
+<br>
default:<br>
unreachable("unhandled opcode");<br>
}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.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="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div>