On Mon, Sep 18, 2023 at 12:10 PM Florian Weimer <fwei...@redhat.com> wrote:
>
> * Richard Biener:
>
> >> > > GCC contraction behavior is rather inconsistent.  It does not contract 
> >> > > x
> >> > > + x - x without -ffast-math, for example, although I believe it would 
> >> > > be
> >> > > permissible under the rules that enable FMA contraction.  This whole
> >
> > Is that really just x + x - x?  We currently gate simplifying x - x to zero
> > on no-signed-zeros and round-to-nearest and have no special
> > handling for x + x - x.
>
> It's just an example I made up, and checked that GCC wouldn't optimize
> it by default (without -ffast-math et al.).  I believe the rule that GCC
> invokes to introduce FMA under -ffp-contract=standard (and
> -ffp-contract=fast) would apply to these contractions as well.
>
> Or put differently, -ffp-contract= seems to be exclusively about FMA,
> while the option name and description is more generic.
>
> x - x is different because replacing it with 0 doesn't seem to be a
> valid contraction because it's incorrect for NaNs.  x + x - x seems to
> be different in this regard, but in our implementation, there might be a
> quirk about sNaNs and qNaNs.
>
> >> The "correctness trumps performance" principle still applies, and
> >> -ffp-contract=fast (the current default outside of -std=cNN) is
> >> known to cause correctness issues and violates the C language standard.
> >> And -ffast[-and-loose]-math for is not going away.
> >
> > I think that changing the default to =standard without -ffast-math is
> > reasonable.  IIRC the standard allows such default if it's indicated,
> > so it doesn't require =off anywhere.
>
> How much numerical code is compatible with that?  For example, David
> Goldberg's What Every Computer Scientist Should Know About
> Floating-Point Arithmetic (revised) contains this sentence:
>
> | A language definition that does not require parentheses to be honored
> | is useless for floating-point calculations.
>
> <https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html>

I suppose that applies to re-association not honoring parens.  With C
you either have no re-association (fine) or re-association but globally,
at the expense of violating the standard.  FP contraction is _not_
about honoring parens, the 'error' introduced by is subtle.

Fortran for example allows re-association by default, but honors
parens.  GCC can do this just fine.

> Few people have access to the IEEE 754 standard, and Goldberg's article
> is therefore widely quoted as gospel.  If I understand this correctly,
> according to Goldberg, contractions make C “useless”.  But I'm not a
> floating point person, and I nowadays regret that I looked down upon the
> numerical methods people at university.

No, I don't think it says that.  Fortran also allows contraction (but not
across parens).

Richard.

> Thanks,
> Florian
>

Reply via email to