GLSL: pow vs multiplication for integer exponent

前端 未结 1 1042
小鲜肉
小鲜肉 2021-01-21 18:52

Which is faster in GLSL:

pow(x, 3.0f);

or

x*x*x;

?

Does exponentiation performance depend on hardware

1条回答
  •  深忆病人
    2021-01-21 19:04

    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.

    0 讨论(0)
提交回复
热议问题