[Mesa-dev] [PATCH 21/56] glsl: Extract ir_triop_bitfield_extract implementation to a separate function

Ian Romanick idr at freedesktop.org
Tue Jul 19 19:24:40 UTC 2016


From: Ian Romanick <ian.d.romanick at intel.com>

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/compiler/glsl/ir_constant_expression.cpp | 66 ++++++++++++++++++----------
 1 file changed, 42 insertions(+), 24 deletions(-)

diff --git a/src/compiler/glsl/ir_constant_expression.cpp b/src/compiler/glsl/ir_constant_expression.cpp
index 0f90c4e..ebd2e07 100644
--- a/src/compiler/glsl/ir_constant_expression.cpp
+++ b/src/compiler/glsl/ir_constant_expression.cpp
@@ -537,6 +537,38 @@ ldexp_flush_subnormal(double x, int exp)
    return !isnormal(result) ? copysign(0.0, x) : result;
 }
 
+static uint32_t
+bitfield_extract_uint(uint32_t value, int offset, int bits)
+{
+   if (bits == 0)
+      return 0;
+   else if (offset < 0 || bits < 0)
+      return 0; /* Undefined, per spec. */
+   else if (offset + bits > 32)
+      return 0; /* Undefined, per spec. */
+   else {
+      value <<= 32 - bits - offset;
+      value >>= 32 - bits;
+      return value;
+   }
+}
+
+static int32_t
+bitfield_extract_int(int32_t value, int offset, int bits)
+{
+   if (bits == 0)
+      return 0;
+   else if (offset < 0 || bits < 0)
+      return 0; /* Undefined, per spec. */
+   else if (offset + bits > 32)
+      return 0; /* Undefined, per spec. */
+   else {
+      value <<= 32 - bits - offset;
+      value >>= 32 - bits;
+      return value;
+   }
+}
+
 ir_constant *
 ir_expression::constant_expression_value(struct hash_table *variable_context)
 {
@@ -1610,34 +1642,20 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
       data.u[1] = *((uint32_t *)&op[0]->value.d[0] + 1);
       break;
 
-   case ir_triop_bitfield_extract: {
+   case ir_triop_bitfield_extract:
       for (unsigned c = 0; c < components; c++) {
-         int offset = op[1]->value.i[c];
-         int bits = op[2]->value.i[c];
-
-         if (bits == 0)
-            data.u[c] = 0;
-         else if (offset < 0 || bits < 0)
-            data.u[c] = 0; /* Undefined, per spec. */
-         else if (offset + bits > 32)
-            data.u[c] = 0; /* Undefined, per spec. */
-         else {
-            if (op[0]->type->base_type == GLSL_TYPE_INT) {
-               /* int so that the right shift will sign-extend. */
-               int value = op[0]->value.i[c];
-               value <<= 32 - bits - offset;
-               value >>= 32 - bits;
-               data.i[c] = value;
-            } else {
-               unsigned value = op[0]->value.u[c];
-               value <<= 32 - bits - offset;
-               value >>= 32 - bits;
-               data.u[c] = value;
-            }
+         switch (this->type->base_type) {
+         case GLSL_TYPE_UINT:
+            data.u[c] = bitfield_extract_uint(op[0]->value.u[c], op[1]->value.i[c], op[2]->value.i[c]);
+            break;
+         case GLSL_TYPE_INT:
+            data.i[c] = bitfield_extract_int(op[0]->value.i[c], op[1]->value.i[c], op[2]->value.i[c]);
+            break;
+         default:
+            assert(0);
          }
       }
       break;
-   }
 
    case ir_binop_ldexp:
       for (unsigned c = 0; c < op[0]->type->components(); c++) {
-- 
2.5.5



More information about the mesa-dev mailing list