Hi Paul,

As a matter of curiosity, what do the other brands do with your example?
I ran

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

through a few compilers (only those on godbolt).

Result: ifort could not be persuaded to issue anything but

radd_v5_:
..B5.1:                         # Preds ..B5.0
        movsd     (%rdi), %xmm0                                 #30.3
        addsd     (%rsi), %xmm0                                 #30.12
        addsd     (%rdx), %xmm0                                 #30.3
        addsd     (%rcx), %xmm0                                 #30.22
        ret                                                     #31.1

for the floating point code (-O3, -Ofast, ...)  They reassociated and violated
the standard with the v5 version.  Integer code was always

iadd_v1_:
..B6.1:                         # Preds ..B6.0
        movq      (%rdi), %rax                                  #35.3
        movq      (%rdx), %r8                                   #35.11
        addq      (%rsi), %rax                                  #35.11
        addq      (%rcx), %r8                                   #35.3
        addq      %r8, %rax                                     #35.15
        ret                                                     #36.1

so they reassociated. ifx was the same.

flang with -O3 yielded

radd_v2_:
        addsd   %xmm1, %xmm0
        addsd   %xmm2, %xmm0
        addsd   %xmm3, %xmm0
        retq

(no reassociation and showed

radd_v5_:
        addsd   %xmm1, %xmm0
        addsd   %xmm3, %xmm2
        addsd   %xmm2, %xmm0
        retq

(following the parentheses). With -Ofast, it did the optmized version
for everything.  gfortran did something weird with -O3:

radd_v5_:
        addsd   %xmm3, %xmm2
        addsd   %xmm1, %xmm0
        unpcklpd        %xmm2, %xmm0
        unpcklpd        %xmm2, %xmm2
        addpd   %xmm0, %xmm2
        movapd  %xmm2, %xmm0
        ret

(now PR128946) and rearranges the floating point, but not the integer, with
-Ofast.


Hope this is of some help.

Best regards

        Thomas

Reply via email to