[Mesa-dev] [PATCH v2? 1-2/9] i965/fs: Add fs_reg::is_zero() and is_one(); use for opt_algebraic().

Kenneth Graunke kenneth at whitecape.org
Sat Nov 17 15:10:53 PST 2012


These helper macros save you from writing nasty expressions like:

   if ((inst->src[1].type == BRW_REGISTER_TYPE_F &&
         inst->src[1].imm.f == 1.0) ||
        ((inst->src[1].type == BRW_REGISTER_TYPE_D ||
          inst->src[1].type == BRW_REGISTER_TYPE_UD) &&
         inst->src[1].imm.u == 1)) {

Instead, you simply get to write inst->src[1].is_one().  Simple.
Also, this makes the FS backend match the VS backend (which has these).

This patch also converts opt_algebraic to use the new helper functions.
As a consequence, it will now also optimize integer-typed expressions.
---
 src/mesa/drivers/dri/i965/brw_fs.cpp | 29 ++++++++++++++++++++++-------
 src/mesa/drivers/dri/i965/brw_fs.h   |  2 ++
 2 files changed, 24 insertions(+), 7 deletions(-)

This proposed patch would replace Eric's first two.  I feel it's a bit
simpler/cleaner, matches the VS backend, and additionally adds support
for optimizing a * 0 -> 0.

diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 2cd3ffe..a9100c5 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -266,6 +266,24 @@ fs_reg::equals(const fs_reg &r) const
            imm.u == r.imm.u);
 }
 
+bool
+fs_reg::is_zero() const
+{
+   if (file != IMM)
+      return false;
+
+   return type == BRW_REGISTER_TYPE_F ? imm.f == 0.0 : imm.i == 0;
+}
+
+bool
+fs_reg::is_one() const
+{
+   if (file != IMM)
+      return false;
+
+   return type == BRW_REGISTER_TYPE_F ? imm.f == 1.0 : imm.i == 1;
+}
+
 int
 fs_visitor::type_size(const struct glsl_type *type)
 {
@@ -1347,8 +1365,7 @@ fs_visitor::opt_algebraic()
 	    continue;
 
 	 /* a * 1.0 = a */
-	 if (inst->src[1].type == BRW_REGISTER_TYPE_F &&
-	     inst->src[1].imm.f == 1.0) {
+	 if (inst->src[1].is_one()) {
 	    inst->opcode = BRW_OPCODE_MOV;
 	    inst->src[1] = reg_undef;
 	    progress = true;
@@ -1356,10 +1373,9 @@ fs_visitor::opt_algebraic()
 	 }
 
          /* a * 0.0 = 0.0 */
-         if (inst->src[1].type == BRW_REGISTER_TYPE_F &&
-             inst->src[1].imm.f == 0.0) {
+         if (inst->src[1].is_zero()) {
             inst->opcode = BRW_OPCODE_MOV;
-            inst->src[0] = fs_reg(0.0f);
+            inst->src[0] = inst->src[1];
             inst->src[1] = reg_undef;
             progress = true;
             break;
@@ -1371,8 +1387,7 @@ fs_visitor::opt_algebraic()
             continue;
 
          /* a + 0.0 = a */
-         if (inst->src[1].type == BRW_REGISTER_TYPE_F &&
-             inst->src[1].imm.f == 0.0) {
+         if (inst->src[1].is_zero()) {
             inst->opcode = BRW_OPCODE_MOV;
             inst->src[1] = reg_undef;
             progress = true;
diff --git a/src/mesa/drivers/dri/i965/brw_fs.h b/src/mesa/drivers/dri/i965/brw_fs.h
index 9fc05ea..4bd5581 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.h
+++ b/src/mesa/drivers/dri/i965/brw_fs.h
@@ -91,6 +91,8 @@ public:
    fs_reg(class fs_visitor *v, const struct glsl_type *type);
 
    bool equals(const fs_reg &r) const;
+   bool is_zero() const;
+   bool is_one() const;
 
    /** Register file: ARF, GRF, MRF, IMM. */
    enum register_file file;
-- 
1.8.0



More information about the mesa-dev mailing list