<div dir="ltr"><br><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 truncating doubles.<br>
---<br>
src/compiler/nir/nir.h | 1 +<br>
src/compiler/nir/nir_lower_double_ops.c | 83 +++++++++++++++++++++++++++++++++<br>
2 files changed, 84 insertions(+)<br>
<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index 434d92b..f83b2e0 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2286,6 +2286,7 @@ typedef enum {<br>
nir_lower_drcp = (1 << 0),<br>
nir_lower_dsqrt = (1 << 1),<br>
nir_lower_drsq = (1 << 2),<br>
+ nir_lower_dtrunc = (1 << 3),<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 4cd153c..9eec858 100644<br>
--- a/src/compiler/nir/nir_lower_double_ops.c<br>
+++ b/src/compiler/nir/nir_lower_double_ops.c<br>
@@ -302,6 +302,81 @@ lower_sqrt_rsq(nir_builder *b, nir_ssa_def *src, bool sqrt)<br>
return res;<br>
}<br>
<br>
+static nir_ssa_def *<br>
+lower_trunc(nir_builder *b, nir_ssa_def *src)<br>
+{<br>
+ nir_ssa_def *unbiased_exp = nir_isub(b, get_exponent(b, src),<br>
+ nir_imm_int(b, 1023));<br>
+<br>
+ nir_ssa_def *frac_bits = nir_isub(b, nir_imm_int(b, 52), unbiased_exp);<br>
+<br>
+ /*<br>
+ * Depending on the exponent, we compute a mask with the bits we need to<br>
+ * remove in order to trunc the double. The mask is computed like this:<br>
+ *<br>
+ * if (unbiased_exp < 0)<br>
+ * mask = 0x0<br>
+ * else if (unbiased_exp > 52)<br>
+ * mask = 0x7fffffffffffffff<br>
+ * else<br>
+ * mask = (1LL < frac_bits) - 1<br></blockquote><div><br></div><div>I'm having a bit of trouble convincing myself that this is correct. Let me walk through it one case at a time:<br><br></div><div>unbiased_exp < 0:<br></div><div>In this case, 2^exp <= 2 so src < 1 and the result should be zero. In that case we want to stomp all the bits to zero, not keep them all.<br><br></div><div>unbiased_exp > 52:<br></div><div>In this case 2^exp is large enough that all of the bits matter. We want to keep them all not zero them out.<br><br></div><div>else:<br></div><div>In this case, 2^exp >= 1 but not big enough to make all the mantissa bits matter. We need to mask off the bottom 52-exp many bits.<br><br></div><div>If I'm getting this backwards, please let me know. If it's doing what I think it's doing, there are several cases this should be getting wrong. Are we testing all of those cases?<br><br></div><div>One other aside: I think it's more efficient to generate the masks with either (~0u >> (32 - bits)) or (0x80000000 >> (bits - 1)) if you want the top bits. NIR should be able to easily get rid of the integer adds and subtracts. Getting rid of the -1 on (1 << frac_bits) - 1 is much harder.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ *<br>
+ * Notice that the else branch is a 64-bit integer operation that we need<br>
+ * to implement in terms of 32-bit integer arithmetics (at least until we<br>
+ * support 64-bit integer arithmetics).<br>
+ */<br>
+<br>
+ /* Compute "mask = (1LL << frac_bits) - 1" in terms of hi/lo 32-bit chunks<br>
+ * for the else branch<br>
+ */<br>
+ nir_ssa_def *mask_lo =<br>
+ nir_bcsel(b,<br>
+ nir_ige(b, frac_bits, nir_imm_int(b, 32)),<br>
+ nir_imm_int(b, 0xffffffff),<br>
+ nir_isub(b,<br>
+ nir_ishl(b,<br>
+ nir_imm_int(b, 1),<br>
+ frac_bits),<br>
+ nir_imm_int(b, 1)));<br>
+<br>
+ nir_ssa_def *mask_hi =<br>
+ nir_bcsel(b,<br>
+ nir_ilt(b, frac_bits, nir_imm_int(b, 33)),<br>
+ nir_imm_int(b, 0),<br>
+ nir_isub(b,<br>
+ nir_ishl(b,<br>
+ nir_imm_int(b, 1),<br>
+ nir_isub(b,<br>
+ frac_bits,<br>
+ nir_imm_int(b, 32))),<br>
+ nir_imm_int(b, 1)));<br>
+<br>
+ /* Compute the correct mask to use based on unbiased_exp */<br>
+ nir_ssa_def *mask =<br>
+ nir_bcsel(b,<br>
+ nir_ilt(b, unbiased_exp, nir_imm_int(b, 0)),<br>
+ nir_pack_double_2x32_split(b,<br>
+ nir_imm_int(b, 0xffffffff),<br>
+ nir_imm_int(b, 0x7fffffff)),<br>
+ nir_bcsel(b, nir_ige(b, unbiased_exp, nir_imm_int(b, 53)),<br>
+ nir_imm_double(b, 0.0),<br>
+ nir_pack_double_2x32_split(b, mask_lo, mask_hi)));<br>
+<br>
+ /* Mask off relevant mantissa bits (0..31 in the low 32-bits<br>
+ * and 0..19 in the high 32 bits)<br>
+ */<br>
+ mask_lo = nir_unpack_double_2x32_split_x(b, mask);<br>
+ mask_hi = nir_unpack_double_2x32_split_y(b, mask);<br>
+<br>
+ nir_ssa_def *src_lo = nir_unpack_double_2x32_split_x(b, src);<br>
+ nir_ssa_def *src_hi = nir_unpack_double_2x32_split_y(b, src);<br>
+<br>
+ nir_ssa_def *zero = nir_imm_int(b, 0);<br>
+ nir_ssa_def *new_src_lo = nir_bfi(b, mask_lo, zero, src_lo);<br>
+ nir_ssa_def *new_src_hi = nir_bfi(b, mask_hi, zero, src_hi);<br>
+ return nir_pack_double_2x32_split(b, new_src_lo, new_src_hi);<br>
+}<br>
+<br>
static void<br>
lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
{<br>
@@ -325,6 +400,11 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
return;<br>
break;<br>
<br>
+ case nir_op_ftrunc:<br>
+ if (!(options & nir_lower_dtrunc))<br>
+ return;<br>
+ break;<br>
+<br>
default:<br>
return;<br>
}<br>
@@ -348,6 +428,9 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)<br>
case nir_op_frsq:<br>
result = lower_sqrt_rsq(&bld, src, false);<br>
break;<br>
+ case nir_op_ftrunc:<br>
+ result = lower_trunc(&bld, src);<br>
+ break;<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></div>