raytracing.github.io
raytracing.github.io copied to clipboard
Consider using std::min and std::max instead of fmin and fmax
fmin and fmax result in a function call on GCC and MSVC (or extra instructions on Clang).
Whereas, std::min and std::max map to single XMM instruction. I haven't bench-marked it, but it should definitely give performance boost.
Proof: https://godbolt.org/z/1jbPqbvGx
NB: On Windows, you will need to define NOMINMAX somewhere at the top (or in the CMake file).
I'll just note that this is one of those things that are affected by -ffast-math.
Specifically -ffinite-math-only seems to eliminate the call for GCC and clang at least.
We decided to go with fmin and fmax because these are explicit floating-point comparison functions that properly handle signed zeros, NaNs, and comparisons between floating-point and integer types. When readers write their own implementations in the code of their choice, this is a little hint that they may want to consider the floating-point semantics in their language, in case it matters.
Whether a particular language + compiler + platform optimizes or don't optimize this tiny function is not really a concern for exposition.