Strange behavior of `ifort`

Hello,
This is a somewhat simplified version of the code, where I just used 1/real(i) to generate array elements and made the array S symmetric afterward. Then, if I divide S by tmp, the S becomes not symmetric. If I attach the -fp-model stric or -check all option (in addition to -O3), the S becomes symmetric again. The result also becomes symmetric if I multiply by tmp (rather than division).

program test                                                                            
implicit none                                                                                                                                                                                                        
integer :: i                                                                                                                              
real, parameter :: A(25) = [( 1 / real(i), i=1,25 )]
real :: S(5, 5), tmp                                                                                                           
                                                                                                                                     
S = reshape(A, [5, 5])                                                                                                               
S = ( S + transpose(S) ) / 2

print *, "S(before) = "
print *, S
print *, "symmetric? ", all(abs(S - transpose(S)) <= 0)                                                                                         

tmp = 10                                                                                                                                     
S = S / tmp   !! not symmetric (-O3), symmetric (-O3 -fp-model strict or -chek all)
!! S = S * tmp     !! symmetric (-O3)

print *
print *, "S(after) = "
print *, S                                                                                                                                    
print *, "symmetric? ", all(abs(S - transpose(S)) <= 0)      

print *
print *, "error in symmetry"
print *, abs(S - transpose(S))
                                                                                                                                     
end program

I am not familiar with recent ifort options, but apart from the precision stuff, is it possibly related to some optimization that breaks the symmetry of S…?

(I tried it with Compiler explorer here. code + result )

2 Likes