[Mesa-dev] [PATCH 5/6] Bitwise conversion operator support in ir_constant_expression.

Olivier Galibert galibert at pobox.com
Mon Apr 30 04:19:05 PDT 2012


This patch expects to be applied after the "Constants through builtins
evaluation rewrite" for two reasons:
- a line of that patch appears in the context
- the ir_function side changes that the old method would need are not
  present (and they would conflict)

At that point a "test_out = floatBitsToUint(-1.0);" fired through the
glsl compiler gives a correct "(assign (x) (var_ref test_out)
(constant uint (3212836864)))"

Signed-off-by: Olivier Galibert <galibert at pobox.com>
---
 src/glsl/ir_constant_expression.cpp |   47 +++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp
index a7aafaa..5e6c164 100644
--- a/src/glsl/ir_constant_expression.cpp
+++ b/src/glsl/ir_constant_expression.cpp
@@ -71,6 +71,29 @@ dot(ir_constant *op0, ir_constant *op1)
    return result;
 }
 
+/* This method is the only one supported by gcc.  Unions in particular
+ * are iffy, and read-through-converted-pointer is killed by strict
+ * aliasing.  OTOH, the compiler sees through the memcpy, so the
+ * resulting asm is reasonable.
+ */
+static float
+bu2f(unsigned int u)
+{
+   assert(sizeof(float) == sizeof(unsigned int));
+   float f;
+   memcpy(&f, &u, sizeof(f));
+   return f;
+}
+
+static unsigned int
+bf2u(float f)
+{
+   assert(sizeof(float) == sizeof(unsigned int));
+   unsigned int u;
+   memcpy(&u, &f, sizeof(f));
+   return u;
+}
+
 ir_constant *
 ir_rvalue::constant_expression_value(struct hash_table *variable_context)
 {
@@ -207,6 +230,30 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
 	 data.u[c] = op[0]->value.i[c];
       }
       break;
+   case ir_unop_bi2f:
+      assert(op[0]->type->base_type == GLSL_TYPE_INT);
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+	 data.f[c] = bu2f(op[0]->value.i[c]);
+      }
+      break;
+   case ir_unop_bf2i:
+      assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+	 data.i[c] = bf2u(op[0]->value.f[c]);
+      }
+      break;
+   case ir_unop_bu2f:
+      assert(op[0]->type->base_type == GLSL_TYPE_UINT);
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+	 data.f[c] = bu2f(op[0]->value.u[c]);
+      }
+      break;
+   case ir_unop_bf2u:
+      assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+	 data.u[c] = bf2u(op[0]->value.f[c]);
+      }
+      break;
    case ir_unop_any:
       assert(op[0]->type->is_boolean());
       data.b[0] = false;
-- 
1.7.10.rc3.1.gb306



More information about the mesa-dev mailing list