[Mesa-dev] [PATCH 06/13] nir/lower_double_ops: lower floor()

Samuel Iglesias Gonsálvez siglesias at igalia.com
Tue Apr 12 08:05:15 UTC 2016


From: Iago Toral Quiroga <itoral at igalia.com>

At least i965 hardware does not have native support for floor on doubles.
---
 src/compiler/nir/nir.h                  |  1 +
 src/compiler/nir/nir_lower_double_ops.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index f83b2e0..b7231a7 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2287,6 +2287,7 @@ typedef enum {
    nir_lower_dsqrt = (1 << 1),
    nir_lower_drsq = (1 << 2),
    nir_lower_dtrunc = (1 << 3),
+   nir_lower_dfloor = (1 << 4),
 } nir_lower_doubles_options;
 
 void nir_lower_doubles(nir_shader *shader, nir_lower_doubles_options options);
diff --git a/src/compiler/nir/nir_lower_double_ops.c b/src/compiler/nir/nir_lower_double_ops.c
index 9eec858..e1ec6da 100644
--- a/src/compiler/nir/nir_lower_double_ops.c
+++ b/src/compiler/nir/nir_lower_double_ops.c
@@ -377,6 +377,27 @@ lower_trunc(nir_builder *b, nir_ssa_def *src)
    return nir_pack_double_2x32_split(b, new_src_lo, new_src_hi);
 }
 
+static nir_ssa_def *
+lower_floor(nir_builder *b, nir_ssa_def *src)
+{
+   /*
+    * For x >= 0, floor(x) = trunc(x)
+    * For x < 0,
+    *    - if x is integer, floor(x) = x
+    *    - otherwise, floor(x) = trunc(x) - 1
+    */
+   nir_ssa_def *tr = nir_ftrunc(b, src);
+   return nir_bcsel(b,
+                    nir_fge(b, src, nir_imm_double(b, 0.0)),
+                    tr,
+                    nir_bcsel(b,
+                              nir_fne(b,
+                                      nir_fsub(b, src, tr),
+                                      nir_imm_double(b, 0.0)),
+                              nir_fsub(b, tr, nir_imm_double(b, 1.0)),
+                              src));
+}
+
 static void
 lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
 {
@@ -405,6 +426,11 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
          return;
       break;
 
+   case nir_op_ffloor:
+      if (!(options & nir_lower_dfloor))
+         return;
+      break;
+
    default:
       return;
    }
@@ -431,6 +457,9 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
    case nir_op_ftrunc:
       result = lower_trunc(&bld, src);
       break;
+   case nir_op_ffloor:
+      result = lower_floor(&bld, src);
+      break;
    default:
       unreachable("unhandled opcode");
    }
-- 
2.5.0



More information about the mesa-dev mailing list