Mesa (master): glsl: Optimize pow(2, x) --> exp2(x).

Kenneth Graunke kwg at kemper.freedesktop.org
Tue Jan 7 20:52:54 UTC 2014


Module: Mesa
Branch: master
Commit: 847bc36a38d42967ad6bf0492fe90a4892d9d799
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=847bc36a38d42967ad6bf0492fe90a4892d9d799

Author: Kenneth Graunke <kenneth at whitecape.org>
Date:   Sun Jan  5 22:57:01 2014 -0800

glsl: Optimize pow(2, x) --> exp2(x).

On Haswell, POW takes 24 cycles, while EXP2 only takes 14.  Plus, using
POW requires putting 2.0 in a register, while EXP2 doesn't.

I believe that EXP2 will be faster than POW on basically all GPUs, so
it makes sense to optimize it.

Looking at the savage2 subset of shader-db:
total instructions in shared programs: 113225 -> 113179 (-0.04%)
instructions in affected programs:     2139 -> 2093 (-2.15%)
instances of 'math pow':               795 -> 749 (-6.14%)
instances of 'math exp':               389 -> 435 (11.8%)

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
Reviewed-by: Matt Turner <mattst88 at gmail.com>

---

 src/glsl/opt_algebraic.cpp |   11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 5e885f7..332f0b7 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -88,6 +88,12 @@ is_vec_one(ir_constant *ir)
 }
 
 static inline bool
+is_vec_two(ir_constant *ir)
+{
+   return (ir == NULL) ? false : ir->is_value(2.0, 2);
+}
+
+static inline bool
 is_vec_negative_one(ir_constant *ir)
 {
    return (ir == NULL) ? false : ir->is_negative_one();
@@ -420,6 +426,11 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       /* 1^x == 1 */
       if (is_vec_one(op_const[0]))
          return op_const[0];
+
+      /* pow(2,x) == exp2(x) */
+      if (is_vec_two(op_const[0]))
+         return expr(ir_unop_exp2, ir->operands[1]);
+
       break;
 
    case ir_unop_rcp:




More information about the mesa-commit mailing list