zlacker

[return to "Std: Clamp generates less efficient assembly than std:min(max,std:max(min,v))"]
1. camblo+3q[view] [source] 2024-01-16 14:17:19
>>x1f604+(OP)
I did a double take on this because I wrote a blog post about this topic a few months ago and came to a very different conclusion, that the results are effectively identical on clang and gcc is just weird.

Then I realized that I was writing about compiling for ARM and this post is about x86. Which is extra weird! Why is the compiler better tuned for ARM than x86 in this case?

Never did figure out what gcc's problem was.

https://godbolt.org/z/Y75qnTGdr

◧◩
2. frozen+NA[view] [source] 2024-01-16 15:13:44
>>camblo+3q
Try switching to -Ofast it produces different ASM
◧◩◪
3. klodol+eD[view] [source] 2024-01-16 15:25:38
>>frozen+NA
-Ofast is one of those dangerous flags that you should probably be careful with. It is “contagious” and it can mess up code elsewhere in the program, because it changes processor flags.

I would try a more specific flag like -ffinite-math-only.

◧◩◪◨
4. Sharli+4I[view] [source] 2024-01-16 15:43:57
>>klodol+eD
finite-math-only is a footgun as well as it allows the compiler assume that NaNs do not exist. Which means all `isnan()` calls are just reduced to `false` so it’s difficult to program defensively. And if a NaN in fact occurs it’s naturally a one-way ticket to UB land.
[go to top]