Re:Optimizations Enabled by -ffast-Math
by nly on Wednesday October 20, 2021 @04:26AM
(#28928165)
He means simple stuff like ensuring a function like
is only called where count > 0
[ Parent
| on HN ]
Re:Optimizations Enabled by -ffast-Math
by colejohnson66 on Wednesday October 20, 2021 @05:05AM
(#28928449)
But couldn’t the compiler optimize out the check because you’re guaranteeing to never divide by 0, so logically count would never be 0. Akin to undefined behavior almost.
Re:Optimizations Enabled by -ffast-Math
by throwawaylinux on Wednesday October 20, 2021 @05:33AM
(#28928645)
This test can not be optimized to always true:
if (count > 0)
total += unit_price(volume, count);
Are you confusing it with the opposite scenario: total += unit_price(volume, count);
if (count == 0)
printf("oops\n");
That test might be optimized out because divide by zero is undefined behavior.
Re:Optimizations Enabled by -ffast-Math
by colejohnson66 on Wednesday October 20, 2021 @06:56AM
(#28929216)
I probably am confusing them. I don’t program in C or C++, so I’m not aware of all the “gotchas”, and can easily confuse them (as shown here). Not to mention that compilers have been getting more aggressive in optimizing undefined behavior.
Re:Optimizations Enabled by -ffast-Math
by adrian_b on Wednesday October 20, 2021 @09:03AM
(#28930140)
What the compiler assumes depends on the compilation options.
Unfortunately most compilers have too permissive default options.
C and C++ programs should always be compiled, at least during development, with strict options, e.g.:
-fsanitize=undefined -fsanitize-undefined-trap-on-error
or equivalent options, which eliminate all the horror stories with unexpected behavior caused by integer divide-by-zero or overflow, pointer overflow, out-of-bounds access and many others.
Because unlike integer operations the FP operations are standardized, there is no need for compiler options, the behavior for exceptions can be controlled from the program with the functions defined in <fenv.h> in C or <cfenv> in C++.