8 ms·
Why is this compiler optimization beneficial with -ffinite-math-only and -fno-signed-integers? From if (x > y) { do_something(); } else {
by tcpekin 5y ago
Why is this compiler optimization beneficial with -ffinite-math-only and -fno-signed-integers?
From
if (x > y) {
do_something();
} else {
do_something_else();
}
to the form
if (x <= y) {
do_something_else();
} else {
do_something();
}
What happens when x or y are NaN?
- zro 5y ago> NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x is false if the value of x is NaN [0] My read of this is that comparisons involving NaN on either side always evaluate to false. In the first one if X or Y is NaN then you'll get do_something_else, and in the second one you'll get do_something. As far as why one order would be more optimal than the other, I'm not sure. Maybe something to do with branch prediction? [0] https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html https://www.gnu.org/software/libc/manual/html_node/Infinity-...
- progbits 5y agoIt is always false. In the first block do_something_else() gets executed when either is NaN, in the second it is do_something().