Search code examples
openglglslshaderexponent

GLSL: pow vs multiplication for integer exponent


Which is faster in GLSL:

pow(x, 3.0f);

or

x*x*x;

?

Does exponentiation performance depend on hardware vendor or exponent value?


Solution

  • While this can definitely be hardware/vendor/compiler dependent, advanced mathematical functions like pow() tend to be considerably more expensive than basic operations.

    The best approach is of course to try both, and benchmark. But if there is a simple replacement for an advanced mathematical functions, I don't think you can go very wrong by using it.

    If you write pow(x, 3.0), the best you can probably hope for is that the compiler will recognize the special case, and expand it. But why take the risk, if the replacement is just as short and easy to read? C/C++ compilers don't always replace pow(x, 2.0) by a simple multiplication, so I wouldn't necessarily count on all GLSL compilers to do that.