[Mesa-dev] [RFC 03/10] nir/lower_double_ops: lower sign()

Elie Tournier tournier.elie at gmail.com
Wed Apr 12 22:43:12 UTC 2017


Signed-off-by: Elie Tournier <elie.tournier at collabora.com>
---
 src/compiler/nir/nir.h                  |  3 ++-
 src/compiler/nir/nir_lower_double_ops.c | 33 +++++++++++++++++++++++++++++++++
 src/intel/compiler/brw_nir.c            |  3 ++-
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index e891d21499..10dd6b1056 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2570,7 +2570,8 @@ typedef enum {
    nir_lower_dround_even = (1 << 7),
    nir_lower_dmod = (1 << 8),
    nir_lower_dabs = (1 << 9),
-   nir_lower_dneg = (1 << 10)
+   nir_lower_dneg = (1 << 10),
+   nir_lower_dsign = (1 << 11)
 } nir_lower_doubles_options;
 
 bool 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 eb16e513ae..5c0d62b554 100644
--- a/src/compiler/nir/nir_lower_double_ops.c
+++ b/src/compiler/nir/nir_lower_double_ops.c
@@ -502,6 +502,30 @@ lower_fneg64(nir_builder *b, nir_ssa_def *src)
                        nir_pack_64_2x32_split(b, src_lo, src_hi));
 }
 
+static nir_ssa_def *
+lower_fsign64(nir_builder *b, nir_ssa_def *src)
+{
+   nir_ssa_def *src_lo = nir_unpack_64_2x32_split_x(b, src);
+   nir_ssa_def *src_hi = nir_unpack_64_2x32_split_y(b, src);
+
+   nir_ssa_def *is_zero = nir_ieq(b,
+                                  nir_ior(b,
+                                          nir_ishl(b, src_hi,
+                                                      nir_imm_int(b, 1)),
+                                          src_lo),
+                                  nir_imm_int(b, 0));
+
+   nir_ssa_def *sign = nir_iand(b, src_hi, nir_imm_int(b, 0x80000000));
+   nir_ssa_def *one = nir_imm_int(b, 0x3FF00000);
+
+   return nir_bcsel(b,
+                    is_zero,
+                    nir_imm_double(b, 0),
+                    nir_pack_64_2x32_split(b,
+                                           nir_imm_int(b, 0),
+                                           nir_ior(b, sign, one)));
+}
+
 static bool
 lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
 {
@@ -565,6 +589,11 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
          return false;
       break;
 
+   case nir_op_fsign:
+      if (!(options & nir_lower_dsign))
+         return false;
+      break;
+
    default:
       return false;
    }
@@ -619,6 +648,10 @@ lower_doubles_instr(nir_alu_instr *instr, nir_lower_doubles_options options)
       result = lower_fneg64(&bld, src);
       break;
 
+   case nir_op_fsign:
+      result = lower_fsign64(&bld, src);
+      break;
+
    default:
       unreachable("unhandled opcode");
    }
diff --git a/src/intel/compiler/brw_nir.c b/src/intel/compiler/brw_nir.c
index bf6935b1c2..405d756e6c 100644
--- a/src/intel/compiler/brw_nir.c
+++ b/src/intel/compiler/brw_nir.c
@@ -511,7 +511,8 @@ nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
                              nir_lower_dround_even |
                              nir_lower_dmod |
                              nir_lower_dabs |
-                             nir_lower_dneg);
+                             nir_lower_dneg |
+                             nir_lower_dsign);
       OPT(nir_lower_64bit_pack);
    } while (progress);
 
-- 
2.11.0



More information about the mesa-dev mailing list