Hello,

If you look at the thread
https://mailman.j3-fortran.org/pipermail/j3/2026-January/015528.html, you
will see that the canonical way of expressing (for REAL)

xx=(a+b)+(c+d)

in more than one statement is

tmp1=ieee_real(a+b, kind=<something>)
tmp2=ieee_real(c+d, kind=<something>)
xx=ieee_real(tmp1+tmp2,kind=<something>)

The language has all the facilities needed to express either a "loose"
addition of many things (e.g. xx=SUM([a,b,c,d])), or a specific computation
tree. It just so happens that the maximally unadorned version is quite
loose, and that is by design.

Themos Tsikas

On Fri, Nov 27, 2026 at 10:18 AM Thomas Koenig <[email protected]>
wrote:

> Am 26.11.26 um 22:40 schrieb Steve Kargl:
> > On 11/26/26 11:51, Thomas Koenig wrote:
> >> Am 26.11.26 um 19:16 schrieb Steve Kargl:
> >>> (Yes,
> >>> I know a "Note" in the standard is not normative text.)  Once
> >>> the compiler has the "combined" expression, it can apply the
> >>> transformations from Note 2, page 163, and one gets
> >>> add4 = (a + b) + (c + d).  Your argument suggests that the
> >>> individual statements imply the existence of parentheses,
> >>
> >> Splitting a hair really fine, parentheses are not needed, but
> >> the end result is the same (and gfortran could implement this
> >> handing off a PAREN_EXPR to the middle).
> >>
> >> which
> >>> may inhibit a compiler's ability to perform optimizations.
> >>
> >> Yes, it does (which is the point).
> >
> > The "as-if" in 5.3.5  and left-to-right evaluation means one cannot
> > distinguish if a programmer wrote
> >
> >    t1 = a + b
> >    t2 = t1 + c
> >    xx = t2 + d
> >
> > or
> >
> >    xx = a + b + c + d
>
> That correct, but....
>
> > Mathematically, these are the same.  Numerically, these are
> > the same (as long as the compiler is inhibited from doing any
> > optimization such as xx = (a + b) + (c + d)).
>
> The Fortran standard allows that reassociation. So
>
>      t1 = a + b
>      t2 = t1 + c
>      xx = t2 + d
>
> is equivalent to
>
> xx = ((a + b) + c) + d
>
>
> > As Fortran is a language for numerical computation, it would
> > seem strange for J3 to purposely inhibit a compiler from doing
> > an optimization here.
>
> Numerical code is not only about efficiency, it is also about correctness
> of results... IIRC, it is possible to reduce Kahan summation to a simple
> sum with -Ofast, which is probably not what the people who wrote the
> code intended.
>
>
> > Likewise, consider (in double precision) the over-simplified,
> >
> >    t1 = 1 / 3.0_8
> >    ...
> >    ! A bunch of FP operations that do not involve a, b, and t1
> >    ...
> >    t2 = 1 / 3.0_8 - t1
> >    if (t2 /= 0) then
> >       ...
> >    end if
> >
> > Mathematically, a compiler should be able to set t2 = 0 without
> > doing any computation.
>
> On a system with reproducible arithmetic, such as IEEE, that is
> indeed true.
>
> > On a i387 FPU, t1 is likely flushed
> > from an 80-bit register with 64 bits of precision to memory
> > with only 53 bits precision.  If the processor reloads 1, 3.0_8,
> > and t1 into 80-bit FPU registers, then a - b may not equal t1.
> > (And, yes, I know about -ffloat-store).  I doubt anyone is going
> > to get upset is the compiler optimizes to t2 = 0.
>
> I think what you write is true, but if there is a difference, the
> IEEE rules at least are broken.
>
>
> But I think we have reached a an impasse.  We probably need to
> resolve this via an interp request; my mail to the J3 mailing list
> didn't seem to interest people.
>
> What do you think?
>

Reply via email to