glsl: Add support for ldexp.
authorMatt Turner <mattst88@gmail.com>
Thu, 22 Aug 2013 20:31:18 +0000 (13:31 -0700)
committerMatt Turner <mattst88@gmail.com>
Tue, 17 Sep 2013 23:59:05 +0000 (16:59 -0700)
v2: Drop frexp. Rebase on builtins rewrite.
Reviewed-by: Paul Berry <stereotype441@gmail.com>
src/glsl/builtin_functions.cpp
src/glsl/ir.cpp
src/glsl/ir.h
src/glsl/ir_constant_expression.cpp
src/glsl/ir_validate.cpp
src/mesa/drivers/dri/i965/brw_fs_channel_expressions.cpp
src/mesa/drivers/dri/i965/brw_fs_visitor.cpp
src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
src/mesa/program/ir_to_mesa.cpp
src/mesa/state_tracker/st_glsl_to_tgsi.cpp

index 528af0dc65d8f444cf0d8075272be160045d411d..ffabf8a8710c9b5104d51dc51592255eef8b33ad 100644 (file)
@@ -510,6 +510,7 @@ private:
    B1(findLSB)
    B1(findMSB)
    B1(fma)
+   B2(ldexp)
 #undef B0
 #undef B1
 #undef B2
@@ -1822,6 +1823,13 @@ builtin_builder::create_builtins()
    IU(findLSB)
    IU(findMSB)
    F(fma)
+
+   add_function("ldexp",
+                _ldexp(glsl_type::float_type, glsl_type::int_type),
+                _ldexp(glsl_type::vec2_type,  glsl_type::ivec2_type),
+                _ldexp(glsl_type::vec3_type,  glsl_type::ivec3_type),
+                _ldexp(glsl_type::vec4_type,  glsl_type::ivec4_type),
+                NULL);
 #undef F
 #undef FI
 #undef FIU
@@ -3514,6 +3522,12 @@ builtin_builder::_fma(const glsl_type *type)
 
    return sig;
 }
+
+ir_function_signature *
+builtin_builder::_ldexp(const glsl_type *x_type, const glsl_type *exp_type)
+{
+   return binop(ir_binop_ldexp, gpu_shader5, x_type, x_type, exp_type);
+}
 /** @} */
 
 /******************************************************************************/
index 1b1799958eab641d7f248462d0167f75794090a5..a2dca457a9550e9651a78e612f46ae9752705dbe 100644 (file)
@@ -401,6 +401,7 @@ ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
    case ir_binop_lshift:
    case ir_binop_rshift:
    case ir_binop_bfm:
+   case ir_binop_ldexp:
       this->type = op0->type;
       break;
 
@@ -551,6 +552,7 @@ static const char *const operator_strs[] = {
    "packHalf2x16_split",
    "bfm",
    "ubo_load",
+   "ldexp",
    "vector_extract",
    "fma",
    "lrp",
index 2637b40e28ba39badf2a1a1ada0c1f8314747206..b0e77327711173283403f99edbefd096a1d25ce8 100644 (file)
@@ -1174,6 +1174,13 @@ enum ir_expression_operation {
     */
    ir_binop_ubo_load,
 
+   /**
+    * \name Multiplies a number by two to a power, part of ARB_gpu_shader5.
+    */
+   /*@{*/
+   ir_binop_ldexp,
+   /*@}*/
+
    /**
     * Extract a scalar from a vector
     *
index a67470bf366e28e068695ff1b0c229223a42b2ef..4579ef209dae87891d5f4a6b452856418a8ec9d0 100644 (file)
@@ -394,6 +394,7 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
       switch (this->operation) {
       case ir_binop_lshift:
       case ir_binop_rshift:
+      case ir_binop_ldexp:
       case ir_binop_vector_extract:
       case ir_triop_csel:
       case ir_triop_bitfield_extract:
@@ -1376,6 +1377,15 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
       break;
    }
 
+   case ir_binop_ldexp:
+      for (unsigned c = 0; c < components; c++) {
+         data.f[c] = ldexp(op[0]->value.f[c], op[1]->value.i[c]);
+         /* Flush subnormal values to zero. */
+         if (!isnormal(data.f[c]))
+            data.f[c] = copysign(0.0, op[0]->value.f[c]);
+      }
+      break;
+
    case ir_triop_fma:
       assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
       assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
index ae3f09daf4338f341dfcbdfaa625f8c12644552a..66a9800ce75156afe88e1ed7f311e85e01783560 100644 (file)
@@ -516,6 +516,14 @@ ir_validate::visit_leave(ir_expression *ir)
       assert(ir->operands[1]->type == glsl_type::uint_type);
       break;
 
+   case ir_binop_ldexp:
+      assert(ir->operands[0]->type == ir->type);
+      assert(ir->operands[0]->type->is_float());
+      assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
+      assert(ir->operands[0]->type->components() ==
+             ir->operands[1]->type->components());
+      break;
+
    case ir_binop_vector_extract:
       assert(ir->operands[0]->type->is_vector());
       assert(ir->operands[1]->type->is_scalar()
index b4c33e636a9f4c1a131655e8ceb6f4790d300451..fb932d8c2178bee17c77b2bfc33224cfb925d726 100644 (file)
@@ -404,6 +404,7 @@ ir_channel_expressions_visitor::visit_leave(ir_assignment *ir)
    case ir_unop_unpack_unorm_2x16:
    case ir_unop_unpack_unorm_4x8:
    case ir_unop_unpack_half_2x16:
+   case ir_binop_ldexp:
    case ir_binop_vector_extract:
    case ir_triop_vector_insert:
    case ir_quadop_bitfield_insert:
index 8b505a07cad8fcaa9f1c4ac27c7a9f5a834f66ce..3d25fe3eca556083850d3738fa372a0af5a4820b 100644 (file)
@@ -501,6 +501,10 @@ fs_visitor::visit(ir_expression *ir)
       assert(!"not reached: should be handled by lower_vector_insert()");
       break;
 
+   case ir_binop_ldexp:
+      assert(!"not reached: should be handled by ldexp_to_arith()");
+      break;
+
    case ir_unop_sqrt:
       emit_math(SHADER_OPCODE_SQRT, this->result, op[0]);
       break;
index 874e6e3f3852db050e9675f1376a21b83115b1d8..3ff6a610359d6c067f18bf33697b3d785f454fe7 100644 (file)
@@ -1651,6 +1651,9 @@ vec4_visitor::visit(ir_expression *ir)
    case ir_binop_pack_half_2x16_split:
       assert(!"not reached: should not occur in vertex shader");
       break;
+   case ir_binop_ldexp:
+      assert(!"not reached: should be handled by ldexp_to_arith()");
+      break;
    }
 }
 
index 510235caeed7dbb5c1e46b3e8443f6bba1115b4f..cef32dc07125a3109a731b714ad6c3281fdb8976 100644 (file)
@@ -1497,6 +1497,7 @@ ir_to_mesa_visitor::visit(ir_expression *ir)
    case ir_triop_bitfield_extract:
    case ir_triop_vector_insert:
    case ir_quadop_bitfield_insert:
+   case ir_binop_ldexp:
    case ir_triop_csel:
       assert(!"not supported");
       break;
index 1c9174c91d01088a015b4d07acbb86cfc5bd54b2..0a1837f69b34cba5db57bd66a696b3a6bdd2d9d7 100644 (file)
@@ -1979,6 +1979,7 @@ glsl_to_tgsi_visitor::visit(ir_expression *ir)
    case ir_quadop_vector:
    case ir_binop_vector_extract:
    case ir_triop_vector_insert:
+   case ir_binop_ldexp:
    case ir_triop_csel:
       /* This operation is not supported, or should have already been handled.
        */