zlacker

[return to "Std: Clamp generates less efficient assembly than std:min(max,std:max(min,v))"]
1. celega+im[view] [source] 2024-01-16 13:50:05
>>x1f604+(OP)
On gcc 13, the difference in assembly between the min(max()) version and std::clamp is eliminated when I add the -ffast-math flag. I suspect that the two implementations handle one of the arguments being NaN a bit differently.

https://gcc.godbolt.org/z/fGaP6roe9

I see the same behavior on clang 17 as well

https://gcc.godbolt.org/z/6jvnoxWhb

◧◩
2. gumby+1n[view] [source] 2024-01-16 13:54:31
>>celega+im
You (celegans25) probably know this but here is a PSA that -ffast-math is really -finaccurate-math. The knowledgeable developer will know when to use it (almost never) while the naive user will have bugs.
◧◩◪
3. mort96+kA[view] [source] 2024-01-16 15:11:52
>>gumby+1n
What you really should enable is the fun and safe math optimizations, with -funsafe-math-optimizations.
◧◩◪◨
4. aqfamn+gN[view] [source] 2024-01-16 16:06:01
>>mort96+kA
I know almost nothing about compiler flags but I got a laugh out of this even though I still don't know if you're joking or not. Edit: Just read it again and now I understand the joke. Haha
◧◩◪◨⬒
5. kevinc+RY[view] [source] 2024-01-16 17:02:06
>>aqfamn+gN
To others `-f` is a common prefix for GCC flags. You can think of this as "enable feature". So -funsafe-math-operations should be read as (-f) (unsafe-math-operations). Not (-)(funsafe-math-operations).
◧◩◪◨⬒⬓
6. arctic+cw1[view] [source] 2024-01-16 19:20:48
>>kevinc+RY
I kind of like the idea the flag is sarcastically calling them very fun and very safe.
[go to top]