Mesa (master): glsl: Refactor is_vec_{zero, one} to be methods of ir_constant

Ian Romanick idr at kemper.freedesktop.org
Tue Nov 16 20:35:45 UTC 2010


Module: Mesa
Branch: master
Commit: 38e55153af031e48125b1cd0a5d939bb92379ddc
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=38e55153af031e48125b1cd0a5d939bb92379ddc

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Fri Nov 12 10:19:08 2010 -0800

glsl: Refactor is_vec_{zero,one} to be methods of ir_constant

These predicates will be used in other places soon.

---

 src/glsl/ir.cpp            |   73 ++++++++++++++++++++++++++++++++++++++++++++
 src/glsl/ir.h              |   24 ++++++++++++++
 src/glsl/opt_algebraic.cpp |   72 ++-----------------------------------------
 3 files changed, 101 insertions(+), 68 deletions(-)

diff --git a/src/glsl/ir.cpp b/src/glsl/ir.cpp
index 87e78ee..4b88601 100644
--- a/src/glsl/ir.cpp
+++ b/src/glsl/ir.cpp
@@ -742,6 +742,79 @@ ir_constant::has_value(const ir_constant *c) const
    return true;
 }
 
+bool
+ir_constant::is_zero() const
+{
+   if (!this->type->is_scalar() && !this->type->is_vector())
+      return false;
+
+   for (unsigned c = 0; c < this->type->vector_elements; c++) {
+      switch (this->type->base_type) {
+      case GLSL_TYPE_FLOAT:
+	 if (this->value.f[c] != 0.0)
+	    return false;
+	 break;
+      case GLSL_TYPE_INT:
+	 if (this->value.i[c] != 0)
+	    return false;
+	 break;
+      case GLSL_TYPE_UINT:
+	 if (this->value.u[c] != 0)
+	    return false;
+	 break;
+      case GLSL_TYPE_BOOL:
+	 if (this->value.b[c] != false)
+	    return false;
+	 break;
+      default:
+	 /* The only other base types are structures, arrays, and samplers.
+	  * Samplers cannot be constants, and the others should have been
+	  * filtered out above.
+	  */
+	 assert(!"Should not get here.");
+	 return false;
+      }
+   }
+
+   return true;
+}
+
+bool
+ir_constant::is_one() const
+{
+   if (!this->type->is_scalar() && !this->type->is_vector())
+      return false;
+
+   for (unsigned c = 0; c < this->type->vector_elements; c++) {
+      switch (this->type->base_type) {
+      case GLSL_TYPE_FLOAT:
+	 if (this->value.f[c] != 1.0)
+	    return false;
+	 break;
+      case GLSL_TYPE_INT:
+	 if (this->value.i[c] != 1)
+	    return false;
+	 break;
+      case GLSL_TYPE_UINT:
+	 if (this->value.u[c] != 1)
+	    return false;
+	 break;
+      case GLSL_TYPE_BOOL:
+	 if (this->value.b[c] != true)
+	    return false;
+	 break;
+      default:
+	 /* The only other base types are structures, arrays, and samplers.
+	  * Samplers cannot be constants, and the others should have been
+	  * filtered out above.
+	  */
+	 assert(!"Should not get here.");
+	 return false;
+      }
+   }
+
+   return true;
+}
 
 ir_loop::ir_loop()
 {
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index 80e0f67..6a70ded 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -1441,10 +1441,34 @@ public:
 
    /**
     * Determine whether a constant has the same value as another constant
+    *
+    * \sa ir_constant::is_zero, ir_constant::is_one
     */
    bool has_value(const ir_constant *) const;
 
    /**
+    * Determine if a constant has the value zero
+    *
+    * \note
+    * This function always returns \c false for constants that are not
+    * scalars or vectors.
+    *
+    * \sa ir_constant::has_value, ir_constant::is_one
+    */
+   bool is_zero() const;
+
+   /**
+    * Determine if a constant has the value one
+    *
+    * \note
+    * This function always returns \c false for constants that are not
+    * scalars or vectors.
+    *
+    * \sa ir_constant::has_value, ir_constant::is_zero
+    */
+   bool is_one() const;
+
+   /**
     * Value of the constant.
     *
     * The field used to back the values supplied by the constant is determined
diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 2ed66db..c7f5c3b 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -68,80 +68,16 @@ public:
    bool progress;
 };
 
-static bool
+static inline bool
 is_vec_zero(ir_constant *ir)
 {
-   int c;
-
-   if (!ir)
-      return false;
-   if (!ir->type->is_scalar() &&
-       !ir->type->is_vector())
-      return false;
-
-   for (c = 0; c < ir->type->vector_elements; c++) {
-      switch (ir->type->base_type) {
-      case GLSL_TYPE_FLOAT:
-	 if (ir->value.f[c] != 0.0)
-	    return false;
-	 break;
-      case GLSL_TYPE_INT:
-	 if (ir->value.i[c] != 0)
-	    return false;
-	 break;
-      case GLSL_TYPE_UINT:
-	 if (ir->value.u[c] != 0)
-	    return false;
-	 break;
-      case GLSL_TYPE_BOOL:
-	 if (ir->value.b[c] != false)
-	    return false;
-	 break;
-      default:
-	 assert(!"bad base type");
-	 return false;
-      }
-   }
-
-   return true;
+   return (ir == NULL) ? false : ir->is_zero();
 }
 
-static bool
+static inline bool
 is_vec_one(ir_constant *ir)
 {
-   int c;
-
-   if (!ir)
-      return false;
-   if (!ir->type->is_scalar() &&
-       !ir->type->is_vector())
-      return false;
-
-   for (c = 0; c < ir->type->vector_elements; c++) {
-      switch (ir->type->base_type) {
-      case GLSL_TYPE_FLOAT:
-	 if (ir->value.f[c] != 1.0)
-	    return false;
-	 break;
-      case GLSL_TYPE_INT:
-	 if (ir->value.i[c] != 1)
-	    return false;
-	 break;
-      case GLSL_TYPE_UINT:
-	 if (ir->value.u[c] != 1)
-	    return false;
-	 break;
-      case GLSL_TYPE_BOOL:
-	 if (ir->value.b[c] != true)
-	    return false;
-	 break;
-      default:
-	 assert(!"bad base type");
-	 return false;
-      }
-   }
-
-   return true;
+   return (ir == NULL) ? false : ir->is_one();
 }
 
 static void




More information about the mesa-commit mailing list