[Mesa-dev] [PATCH 1/3] glsl: Add support for constant expression evaluation on round(), roundEven().

Eric Anholt eric at anholt.net
Mon Oct 3 15:41:03 PDT 2011


They're both implemented the same in GLSL IR (since round() has
undefined behavior for n + 0.5).  The specification of the C function
in use here is a bit unclear: rint(3) says that nearbyint() is like
rint() in that it uses the current rounding direction from
fesetround(3).  fesetround(3) says behavior is according to
FLT_ROUNDS, which on this system is 1 ("Rounding is toward nearest
number"), but doesn't specify behavior for n + 0.5 in that case.
However, round(3) says that rint(3) has roundEven()'s behavior for n +
0.5.

The testcases we have that do test roundEven()'s behavior for n + 0.5
pass with this change.

Fixes glsl-1.30/compiler/built-in-functions/round*
---
 src/glsl/ir_constant_expression.cpp |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp
index b3fe6cf..0d7fae7 100644
--- a/src/glsl/ir_constant_expression.cpp
+++ b/src/glsl/ir_constant_expression.cpp
@@ -196,6 +196,13 @@ ir_expression::constant_expression_value()
       }
       break;
 
+   case ir_unop_round_even:
+      assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+	 data.f[c] = nearbyint(op[0]->value.f[c]);
+      }
+      break;
+
    case ir_unop_ceil:
       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
       for (unsigned c = 0; c < op[0]->type->components(); c++) {
@@ -1324,6 +1331,9 @@ ir_call::constant_expression_value()
 			    * op[1]->value.f[c];
 	 }
       }
+   } else if (strcmp(callee, "round") == 0 ||
+	      strcmp(callee, "roundEven") == 0) {
+      expr = new(mem_ctx) ir_expression(ir_unop_round_even, op[0]);
    } else if (strcmp(callee, "sign") == 0) {
       expr = new(mem_ctx) ir_expression(ir_unop_sign, type, op[0], NULL);
    } else if (strcmp(callee, "sin") == 0) {
-- 
1.7.6.3



More information about the mesa-dev mailing list