[comp.lang.fortran] Side effects in functions - the special case

jlg@lanl.gov (Jim Giles) (09/22/88)

By the way, I've found the passage in Fortran that allows the following
expression optimization:

      A = F(I) + F(I)

is the same as:

      tmp = F(I)
      A = 2*tmp

Section 6.6.4 of the ANSI Fortran standard:

      6.6.4 Evaluation of Arithmetic Expressions.  The rules
      given in 6.1.2 specify the interpretation of an arithmetic
      expression. ...

Section 6.1.2 give the normal left-to-right evaluation order with precedence.

      ...         Once the interpretation has been established in
      accordance with those rules, the processor may evaluate any
      mathematically equivalent expression, provided that the 
      integrity of parenthesis is not violated.

Provided that the function F obeys all the prohibitions on side effects
given secion 6.6, the result of my optimization is mathematically (and
computationally, in this case) the same.  So it's legal.

J. Giles
Los alamos

ags@h.cc.purdue.edu (Dave Seaman) (09/22/88)

In article <3979@lanl.gov> jlg@lanl.gov (Jim Giles) writes:
>By the way, I've found the passage in Fortran that allows the following
>expression optimization:

>      A = F(I) + F(I)

>is the same as:

>      tmp = F(I)
>      A = 2*tmp

[Quotes rule on expression evaluation].

That is true, but in Fortran (unlike C), assignment is a statement, not an
expression.  Therefore the rule you quoted does not apply to

A(INVERT(I)) = A(INVERT(I)) + 1

-- 
Dave Seaman	  					
ags@j.cc.purdue.edu

jlg@lanl.gov (Jim Giles) (09/23/88)

From article <4002@h.cc.purdue.edu>, by ags@h.cc.purdue.edu (Dave Seaman):
> That is true, but in Fortran (unlike C), assignment is a statement, not an
> expression.  Therefore the rule you quoted does not apply to
> 
> A(INVERT(I)) = A(INVERT(I)) + 1

True.  This case is ambiguous (as I've said before).  But in C (unlike
Fortran) this construct is deliberately ambiguous.  I suspect that the
Fortran committee would prefer not to have left this hole in the
standard.  The C committee is aparently going to leave a lot of
ambiguous stuff in the ANSI C standard.  Myself, I would prefer that
the ambiguity be removed.  In the meantime, I will continue to regard
that construct as dangerous as if it were illegal.

J. Giles
Los Alamos