Why does changing 0.1f to 0 slow down performance by 10x?
stackoverflow.com
stackoverflow.com
As a folk-theorem of numerics goes though: such problems are often a symptom that you're not doing quite the right thing. Should your numbers really be that small? Often recasting the problem slightly avoids having to turn on low level flags or other hacking.
The NaNs were being used in a sensible way to indicate "don't process this part of the image, it is over ocean not land and the values are nonsense".
Something like:
y[i] = y[i] + 0;
"Should" not take more time than: y[i] = y[i] + 0.1;
because "any decent optimizing compiler obviously would NOP the entire statement out".I seldom see it is the case though where compilers optimize things that coders assume it is obvious the compiler will optimize.
I got the impression that the "+ 0.1" was enough to raise the value of the floating point number being operated on, so that it was not denormalized.
So, the "+ 0.1" version was faster because the numbers it generated were not denormalized. It had nothing to do with the special properties of 0 versus 0.1f (a float).
Neither of the two explanations on the SO post made any assumption about the "theoretical optimizing compiler" that you mention, right?
Now that I look at the assembly, not even + 0.0f gets optimized out. If I had to guess, it could be that + 0.0f would have side-effects if y[i] happened to be a signalling NaN or something.
x += 0.0;
If x = -0.0 (yes, zero has a sign) then adding 0.0 gives 0.0, so it can have an effect and optimizing it away is wrong and generally only allowed with -ffast-math.TLDR: there are some classes of problems for which the sign bit of zero preserves enough important information to get an accurate solution to a problem that would not be possible if you only had an unsigned zero. These happen to turn up in certain types of conformal mappings that are useful for solving certain PDEs.
1/-0 will give you -∞, of course.
(Strictly speaking, 1/0 in C is kaboom! (undefined behaviour))