Mesa (7.9): glsl: Refactor get_num_operands.

Ian Romanick idr at kemper.freedesktop.org
Fri Dec 17 02:21:27 UTC 2010


Module: Mesa
Branch: 7.9
Commit: 212769254218ced67c54e345accd69741a7900a1
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=212769254218ced67c54e345accd69741a7900a1

Author: Kenneth Graunke <kenneth at whitecape.org>
Date:   Thu Dec 16 15:26:55 2010 -0800

glsl: Refactor get_num_operands.

This adds sentinel values to the ir_expression_operation enum type:
ir_last_unop, ir_last_binop, and ir_last_opcode.  They are set to the
previous one so they don't trigger "unhandled case in switch statement"
warnings, but should never be handled directly.

This allows us to remove the huge array of 1s and 2s in
ir_expression::get_num_operands().
(cherry picked from commit 007f4881503b69055d65cfb20bd237673779786b)

---

 src/glsl/ir.cpp |   79 +++++--------------------------------------------------
 src/glsl/ir.h   |   17 +++++++++++-
 2 files changed, 23 insertions(+), 73 deletions(-)

diff --git a/src/glsl/ir.cpp b/src/glsl/ir.cpp
index 5e2109e..0199472 100644
--- a/src/glsl/ir.cpp
+++ b/src/glsl/ir.cpp
@@ -189,78 +189,13 @@ ir_expression::ir_expression(int op, const struct glsl_type *type,
 unsigned int
 ir_expression::get_num_operands(ir_expression_operation op)
 {
-/* Update ir_print_visitor.cpp when updating this list. */
-   const int num_operands[] = {
-      1, /* ir_unop_bit_not */
-      1, /* ir_unop_logic_not */
-      1, /* ir_unop_neg */
-      1, /* ir_unop_abs */
-      1, /* ir_unop_sign */
-      1, /* ir_unop_rcp */
-      1, /* ir_unop_rsq */
-      1, /* ir_unop_sqrt */
-      1, /* ir_unop_exp */
-      1, /* ir_unop_log */
-      1, /* ir_unop_exp2 */
-      1, /* ir_unop_log2 */
-      1, /* ir_unop_f2i */
-      1, /* ir_unop_i2f */
-      1, /* ir_unop_f2b */
-      1, /* ir_unop_b2f */
-      1, /* ir_unop_i2b */
-      1, /* ir_unop_b2i */
-      1, /* ir_unop_u2f */
-      1, /* ir_unop_any */
-
-      1, /* ir_unop_trunc */
-      1, /* ir_unop_ceil */
-      1, /* ir_unop_floor */
-      1, /* ir_unop_fract */
-
-      1, /* ir_unop_sin */
-      1, /* ir_unop_cos */
-
-      1, /* ir_unop_dFdx */
-      1, /* ir_unop_dFdy */
-
-      1, /* ir_unop_noise */
-
-      2, /* ir_binop_add */
-      2, /* ir_binop_sub */
-      2, /* ir_binop_mul */
-      2, /* ir_binop_div */
-      2, /* ir_binop_mod */
-
-      2, /* ir_binop_less */
-      2, /* ir_binop_greater */
-      2, /* ir_binop_lequal */
-      2, /* ir_binop_gequal */
-      2, /* ir_binop_equal */
-      2, /* ir_binop_nequal */
-      2, /* ir_binop_all_equal */
-      2, /* ir_binop_any_nequal */
-
-      2, /* ir_binop_lshift */
-      2, /* ir_binop_rshift */
-      2, /* ir_binop_bit_and */
-      2, /* ir_binop_bit_xor */
-      2, /* ir_binop_bit_or */
-
-      2, /* ir_binop_logic_and */
-      2, /* ir_binop_logic_xor */
-      2, /* ir_binop_logic_or */
-
-      2, /* ir_binop_dot */
-      2, /* ir_binop_cross */
-      2, /* ir_binop_min */
-      2, /* ir_binop_max */
-
-      2, /* ir_binop_pow */
-   };
+   assert(op <= ir_last_opcode);
 
-   assert(sizeof(num_operands) / sizeof(num_operands[0]) == ir_binop_pow + 1);
+   if (op <= ir_last_unop)
+      return 1;
 
-   return num_operands[op];
+   if (op <= ir_last_binop)
+      return 2;
 }
 
 static const char *const operator_strs[] = {
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index e0347fb..d45e254 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -712,6 +712,11 @@ enum ir_expression_operation {
 
    ir_unop_noise,
 
+   /**
+    * A sentinel marking the last of the unary operations.
+    */
+   ir_last_unop = ir_unop_noise,
+
    ir_binop_add,
    ir_binop_sub,
    ir_binop_mul,
@@ -770,7 +775,17 @@ enum ir_expression_operation {
    ir_binop_min,
    ir_binop_max,
 
-   ir_binop_pow
+   ir_binop_pow,
+
+   /**
+    * A sentinel marking the last of the binary operations.
+    */
+   ir_last_binop = ir_binop_pow,
+
+   /**
+    * A sentinel marking the last of all operations.
+    */
+   ir_last_opcode = ir_last_binop
 };
 
 class ir_expression : public ir_rvalue {




More information about the mesa-commit mailing list