Am 26.11.26 um 19:16 schrieb Steve Kargl:
On 11/26/26 06:12, Thomas Koenig wrote:
Hello world,

my query to the J3 list was met with resounding silence, for whatever
reason.  So, let me go again through the argument, this time with
some more standardese.

First, we have 5.3.5, which states

With the following exceptions, the effect of execution is as if the
executable constructs are executed in the order in which they appear
in the main program or subprogram until a STOP, ERROR STOP, RETURN,
or END statement is executed.

(where the exceptions are do, if, select ... end= etc, not of
interest in this case).

Plus, we have, as part of 10.1.5.2.4,

Once the interpretation of a numeric intrinsic operation is established, the
processor may evaluate any mathematically equivalent expression, provided that
the integrity of parentheses is not violated.

So consider

real function add4 (a,b,c,d)
   real, intent(in) :: a, b, c, d
   real :: t1, t2
   t1 = a + b
   t2 = t1 + c
   add4 = t2 + d
end function add4

So the compiler has to behave as if it evaluated the assignment
to t1 before anything else.  This is not consistent with reassociating
the expressions to

   add4 = a + b + c + d

and the grouping them as if the user had written

   t1 = a + b
   t2 = c + d
   add4 = t1 + t2

because that would violate 5.3.5 in case where the result
is different (e.g. an infinity where otherwise none would occur).

Well, since I started the deep dive into standardese, I might
as well add to the discussion.  F202y, Note 2, page 159,

    In interpreting a level-2-expr containing two or more
    binary operators + or -, each operand (add-operand) is
    combined from left to right.

That is correct, with mathematical reassociation allowed as long
as parentheses are not violated.


To me (and I could be misguided) the "as-if" in 5.3.5 and the
left-to-right evaluation of a level-2-expr allows a compiler
to transform your 3 statements to add4 = a + b + c + d.

This would directly contravene 5.3.5, which I quoted above.
It can do so, but it *must* obtain at the same result as if it had
not done so.


(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).

On the other hand, implemeting this in gfortran would allow to
make -freassoc-math the default without violating the standard.

Best regards

        Thomas

Reply via email to