News for hackers. Stuff that matters.
Re:Optimizations Enabled by -ffast-Math by okl on Wednesday October 20, 2021 @04:05AM (#28928035)
> A promise that you will not produce Inf, NaN, or -0.0. Whilst especially Inf and NaN can be hard to exclude.

Cumbersome but not that difficult: Range-check all input values.

Re:Optimizations Enabled by -ffast-Math by orangepanda on Wednesday October 20, 2021 @04:22AM (#28928136)
As the compiler assumes you wont produce such values, wouldnt it also optimise those range checks away?
Re:Optimizations Enabled by -ffast-Math by jcelerier on Wednesday October 20, 2021 @04:26AM (#28928166)
At least GCC is I believe fairly buggy in that regard since isnan is optimised out, thus you cannot check if say an incoming value read from the network is a NaN. There's some lengthy discussion about it on the big tracker iirc..
Re:Optimizations Enabled by -ffast-Math by jcelerier on Wednesday October 20, 2021 @07:07AM (#28929271)
I was misremembering, was thinking about this recent convo on LLVM mailinglist: https://lists.llvm.org/pipermail/llvm-dev/2021-September/152...
Re:Optimizations Enabled by -ffast-Math by usefulcat on Wednesday October 20, 2021 @12:39PM (#28932807)
I don't think you are misremembering; -ffast-math causes __builtin_isnan and __builtin_isinf to unconditionally return zero, for both gcc and clang:

https://godbolt.org/z/1qjxvW3KK

I found this out the hard way several years ago and still have my own implementations of these functions laying about in case I want to use -ffast-math.

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

    float
    unit_price (float volume, int count) {
       return volume / count;
    }
is only called where count > 0
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++.

Re:Optimizations Enabled by -ffast-Math by benttoothpaste on Wednesday October 20, 2021 @07:44AM (#28929528)
The checks could be expensive though, more expensive than the gains from this optimization. Often it is better to let the calculation proceed and use isnan to check the final result.
Re:Optimizations Enabled by -ffast-Math by fho on Wednesday October 20, 2021 @05:37AM (#28928678)
That's where more advanced type systems shine. You get to encode things like "this number is not null" and a safe division function will only take "not nulls" as the input.

The workflow would then be:

    1. prove that the input is not null (if input == 0) yielding a "not null number"
    2. running your calculation (no check needed for multiplication or division, if you add something you have to reprove that the number is not null)
Re:Optimizations Enabled by -ffast-Math by contravariant on Wednesday October 20, 2021 @04:58AM (#28928391)
And don't do any division.

Easy.

Re:Optimizations Enabled by -ffast-Math by okl on Wednesday October 20, 2021 @05:35AM (#28928661)
There's nothing wrong with divisions if you exclude problematic combinations of input values.
Re:Optimizations Enabled by -ffast-Math by contravariant on Wednesday October 20, 2021 @07:31AM (#28929437)
You're going to have to do some intermediate checks if you want stuff like x/sin(x) to behave.
Re:Optimizations Enabled by -ffast-Math by scheme271 on Wednesday October 20, 2021 @05:12AM (#28928490)
Or multiplications or additions to avoid overflows or denormals