raytracing.github.io icon indicating copy to clipboard operation
raytracing.github.io copied to clipboard

Consider using std::min and std::max instead of fmin and fmax

Open dimitry-ishenko opened this issue 1 year ago • 2 comments

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

dimitry-ishenko avatar Apr 01 '24 01:04 dimitry-ishenko

NB: On Windows, you will need to define NOMINMAX somewhere at the top (or in the CMake file).

dimitry-ishenko avatar Apr 01 '24 02:04 dimitry-ishenko

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.

eloj avatar Apr 08 '24 17:04 eloj

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.

hollasch avatar Aug 22 '24 18:08 hollasch