Hi Thomas, As a matter of curiosity, what do the other brands do with your example?
Cheers Paul On Sun, 22 Nov 2026 at 07:31, Thomas Koenig <[email protected]> wrote: > > Am 21.11.26 um 18:02 schrieb Steve Kargl: > > On 11/21/26 02:22, Thomas Koenig wrote: > >> Hello world, > >> > >> I have looked at reassociation for a bit. Fortran allows expressions to > >> be reassociated, but parentheses must be honored. This we do (at least as > >> long as -fprotect-parens is in force). > > > > The above is specifically stated in the standard. > > > > F2023: 10.1.5.2.4(2) > > > > 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. > > > > Note 2 on page 159, states a left-to-right evaluation order. So, > > x = a + b + c + d from l-to-r is x = ((a + b) + c) + d. But, > > Note 2 on page 163 shows allowed alternate forms. The RHS can > > be evaluated as x = (a + b) + (c + d) or x = a + (b + c) + d. > > > > There is also F2023:10.1.7. > > > > It is not necessary for a processor to evaluate all of the > > operands of an expression, or to evaluate entirely each > > operand, if the value of the expression can be determined > > otherwise. > > OK. > > >> But Fortran forbids reassociation > >> across different expressions or statements, > > > > Can you point me to where the standard makes such a statement. > > > Coming back to my example: > > real(8) function add_v2 (a,b,c,d) result(res) > real(8), value :: a, b, c, d > real(8) :: tmp > tmp = a + b > tmp = tmp + c > res = tmp + d > end function add_v2 > > is currently, with -fassociative-math, translated as if it were > written > > real(8) function add_v2 (a,b,c,d) result(res) > real(8), value :: a, b, c, d > real(8) :: tmp1, tmp2 > tmp1 = (a+b) > tmp2 = (c+d) > res = tmp1 + tmp2 > end function add_v2 > > which is not permitted by the standard, as far as I have been able to > read clause 10. > > > This suggests the standard forbids common subexpression elimination. > > Common subexpression is fine, as long as parentheses are not permitted. > > t1 = a + b + c > t2 = a + c > > can be reassociated to > > t2 = a + c > t1 = t1 + b > > without violating the standard. > > Best regards > > Thomas >
