Hi Paul,

How do the other brands handle the examples?

Complicated, there are very many option variants :-(
I had written part of this mail before, but it can't find
it anywhere. So, here we go again. If anybody wants to
play, see https://godbolt.org/z/dvn4791Pn .  Source code is

real(8) function radd_v1 (a,b,c,d) result(res)
  real(8), value :: a, b, c, d
  res = a + b + c + d
end function radd_v1

real(8) function radd_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 radd_v2

real(8) function radd_v3 (a,b,c,d) result(res)
  real(8), value :: a, b, c, d
  res = ((a + b) + c) + d
end function radd_v3

real(8) function radd_v4 (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 radd_v4

real(8) function radd_v5 (a,b,c,d) result(res)
  real(8), value :: a, b, c, d
  real(8) :: tmp
  res = (a + b) + (c + d)
end function radd_v5

integer(8) function iadd_v1 (a,b,c,d) result(res)
  integer(8), value :: a, b, c, d
  res = a + b + c + d
end function iadd_v1

integer(8) function iadd_v2 (a,b,c,d) result(res)
  integer(8), value :: a, b, c, d
  res = (a + b) + (c + d)
end function iadd_v2

First, reals.

gfortran -O3  -fassociative-math -fno-signed-zeros -fno-trapping-math
reassociates a+b+d+d, but also tmp = a + b; tmp = tmp + c; res = tmp + d .

nvfortran -O3 reassociates a+b+c+d and does not reassociate when
the assignments to variables are explicit.

flang -O3 behaves the same as nvfortran -O3.

With -O3, ifort does not reassociate a+b+c+d and actually pessimizes
(a + b) + (c + d) into ((a + b) + c) + d. You need to tell it
-assume protect_parens. Same behavior with ifx.

Integers:

gfortran never reassociates.

nvfortran reassociates integer additions, as does flang.

ifort evaluates both a+b+c+d and (a+b)+(c+d) as if it were the latter even
with -O3 -assume protect_parens .

ifx pessimizes (a+b)+(c+d) into ((a + b) + c) + d.

So, from the compilers I have access to at godbolt, nvfortran and flang
behave as I would expect.

Best regards

        Thomas

Reply via email to