GLSL: pow vs multiplication for integer exponent

孤者浪人 提交于 2019-12-02 04:23:36

问题


Which is faster in GLSL:

pow(x, 3.0f);

or

x*x*x;

?

Does exponentiation performance depend on hardware vendor or exponent value?


回答1:


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.



来源:https://stackoverflow.com/questions/39446306/glsl-pow-vs-multiplication-for-integer-exponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!