nir/lower_double_ops: lower ceil()
authorIago Toral Quiroga <itoral@igalia.com>
Mon, 4 Jan 2016 15:10:11 +0000 (16:10 +0100)
committerSamuel Iglesias Gonsálvez <siglesias@igalia.com>
Thu, 28 Apr 2016 10:01:36 +0000 (12:01 +0200)
At least i965 hardware does not have native support for ceil on doubles.

v2 (Sam):
   - Improve the lowering pass to remove one bcsel (Jason).

Signed-off-by: Samuel Iglesias Gonsálvez <siglesias@igalia.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
src/compiler/nir/nir.h
src/compiler/nir/nir_lower_double_ops.c

index 809e83b7d128ada8b71e70ee237c5b7b3ec48b7a..252567ceae50d27f0b146fc6f1839d5dd5c8bec5 100644 (file)
@@ -2419,6 +2419,7 @@ typedef enum {
    nir_lower_drsq = (1 << 2),
    nir_lower_dtrunc = (1 << 3),
    nir_lower_dfloor = (1 << 4),
+   nir_lower_dceil = (1 << 5),
 } nir_lower_doubles_options;
 
 void nir_lower_doubles(nir_shader *shader, nir_lower_doubles_options options);
index 1370ddf9266fbba86d68dd45a61f85f450dd6748..2d94f78ec4ebb6702e924f4dd9e5db808101fc1a 100644 (file)
@@ -368,6 +368,21 @@ lower_floor(nir_builder *b, nir_ssa_def *src)
                     nir_fsub(b, tr, nir_imm_double(b, 1.0)));
 }
 
+static nir_ssa_def *
+lower_ceil(nir_builder *b, nir_ssa_def *src)
+{
+   /* if x < 0,                    ceil(x) = trunc(x)
+    * else if (x - trunc(x) == 0), ceil(x) = x
+    * else,                        ceil(x) = trunc(x) + 1
+    */
+   nir_ssa_def *tr = nir_ftrunc(b, src);
+   nir_ssa_def *negative = nir_flt(b, src, nir_imm_double(b, 0.0));
+   return nir_bcsel(b,
+                    nir_ior(b, negative, nir_feq(b, src, tr)),
+                    tr,
+                    nir_fadd(b, tr, nir_imm_double(b, 1.0)));
+}
+
 static void
 lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
 {
@@ -401,6 +416,11 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
          return;
       break;
 
+   case nir_op_fceil:
+      if (!(options & nir_lower_dceil))
+         return;
+      break;
+
    default:
       return;
    }
@@ -430,6 +450,9 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
    case nir_op_ffloor:
       result = lower_floor(&bld, src);
       break;
+   case nir_op_fceil:
+      result = lower_ceil(&bld, src);
+      break;
    default:
       unreachable("unhandled opcode");
    }