Mesa (master): glsl: Fix constant-folding for reciprocal expressions

Chad Versace chadversary at kemper.freedesktop.org
Wed Feb 2 18:01:34 UTC 2011


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

Author: Chad Versace <chad.versace at intel.com>
Date:   Tue Feb  1 09:09:33 2011 -0800

glsl: Fix constant-folding for reciprocal expressions

Do not constant-fold a reciprocal if any component of the reciprocated
expression is 0. For example, do not constant-fold `1 / vec4(0, 1, 2, 3)`.

Incorrect, previous behavior
----------------------------
Reciprocals were constant-folded even when some component of the
reciprocated expression was 0. The incorrectly applied arithmetic was:
   1 / 0 := 0
For example,
   1 / vec4(0, 1, 2, 3) = vec4(0, 1, 1/2, 1/3)

NOTE: This is a candidate for the 7.9 and 7.10 branches.

---

 src/glsl/ir_constant_expression.cpp |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp
index cf958aa..321374f 100644
--- a/src/glsl/ir_constant_expression.cpp
+++ b/src/glsl/ir_constant_expression.cpp
@@ -288,20 +288,24 @@ ir_expression::constant_expression_value()
       break;
 
    case ir_unop_rcp:
+      /* FINISHME: Emit warning when division-by-zero is detected. */
       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
       for (unsigned c = 0; c < op[0]->type->components(); c++) {
 	 switch (this->type->base_type) {
 	 case GLSL_TYPE_UINT:
-	    if (op[0]->value.u[c] != 0.0)
-	       data.u[c] = 1 / op[0]->value.u[c];
+	    if (op[0]->value.u[c] == 0.0)
+	       return NULL;
+	    data.u[c] = 1 / op[0]->value.u[c];
 	    break;
 	 case GLSL_TYPE_INT:
-	    if (op[0]->value.i[c] != 0.0)
-	       data.i[c] = 1 / op[0]->value.i[c];
+	    if (op[0]->value.i[c] == 0.0)
+	       return NULL;
+	    data.i[c] = 1 / op[0]->value.i[c];
 	    break;
 	 case GLSL_TYPE_FLOAT:
-	    if (op[0]->value.f[c] != 0.0)
-	       data.f[c] = 1.0F / op[0]->value.f[c];
+	    if (op[0]->value.f[c] == 0.0)
+	       return NULL;
+	    data.f[c] = 1.0F / op[0]->value.f[c];
 	    break;
 	 default:
 	    assert(0);




More information about the mesa-commit mailing list