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.
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. (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, which
may inhibit a compiler's ability to perform optimizations.
After looking into F202y:10.1 Expressions I tend to agree with Steve.
"An expression is formed from operands, operators, and parentheses. ..."
and the already quoted Note 3 in 10.1.5.2.4
"In addition to the parentheses required to establish the desired
interpretation, parentheses can be included to
restrict the alternative forms that can be used by the processor in the
actual evaluation of the expression. This is
useful for controlling the magnitude and accuracy of intermediate values
developed during the evaluation of an
expression. ..."
I haven't checked if the standard requires the explicit computation
of a temporary used by the programmer which is used in a subsequent
expression, but if this were required, it would be a serious performance
hit.
How about introducing a new option that treats each assignment as if
the r.h.s. were parenthesized, which is on at -O0 and off at any
higher level? Might be useful for programmers to detect if their
code is affected. To make it really bad, inserting parentheses
automatically so that a+b+c+d always evaluates as ((a+b)+c)+d if
this option is on.
For integer, as long as no conversion is done, re-association should
always by allowed default, if possible. Or is there a reason not to?
Harald