Path: utzoo!attcan!uunet!yale!cmcl2!lanl!jlg
From: jlg@lanl.gov (Jim Giles)
Newsgroups: comp.lang.fortran
Subject: Side effects in functions - the special case
Message-ID: <3979@lanl.gov>
Date: 21 Sep 88 22:52:11 GMT
Organization: Los Alamos National Laboratory
Lines: 29

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