[J3] function references and optimization

Bill Long longb at cray.com
Tue Oct 9 16:15:53 EDT 2018

> On Oct 9, 2018, at 2:44 PM, Robert Corbett via J3 <j3 at mailman.j3-fortran.org> wrote:
> Van's latest e-mail led me to think
> of a possible problem.  Suppose
> a program contains the statement
> function
>      f(x) = y
> and the assignment statement
>      x = y - f(1.0)
> Does the mathematical equivalence
> rule allow the expression to be
> replaced with 0.0?  What if f is an
> internal procedure that returns the
> value of y?
> Oracle Fortran probably does the
> first optimization at high
> optimization levels.  Statement
> function references are inlined,
> so the optimization phase would
> see the expression as y - y.
> Oracle Fortran probably does
> not do the optimization if f is an
> internal procedure.  The
> optimization could matter if the
> value of y is infinite.

Or a NaN.  

Like similar examples, I would expect the default behavior to enable optimizations that improve performance at the expense of strict IEEE rules. But there are usually compiler options to shut down optimizations, such as inlining or expression simplification.   If you have “well behaved” data, the optimized version is usually preferred. Real world simulations normally don’t involve infinities or NaNs. But if you want to double check, you can turn off the floating point optimizations and see if that affects the answer. 


> Robert Corbett

Bill Long                                                                       longb at cray.com
Principal Engineer, Fortran Technical Support &   voice:  651-605-9024
Bioinformatics Software Development                      fax:  651-605-9143
Cray Inc./ 2131 Lindau Lane/  Suite 1000/  Bloomington, MN  55425

More information about the J3 mailing list