problem with loops and optimization

problem with loops and optimization

I am trying to compile the following piece of code 

me2cc(:,:,:) = (zero,zero)

do i = -1,1,2; do j = -1,1,2;do i2 = -1,1,2;do i3 = -1,1,2

me2cc(i,j,1) = me2cc(i,j,1) + ap1(i2,i3,i)*conjg(ap1(i2,i3,j)) !-- part1

me2cc(i,j,2) = me2cc(i,j,2) + ap2(i3,i2,i)*conjg(ap2(i3,i2,j)) !-- part2

me2cc(i,j,3) = me2cc(i,j,3) + ap1(i2,i3,i)*conjg(ap2(i3,i2,j))  + ap2(i3,i2,i)*conjg(ap1(i2,i3,j)) !--part3

me2cc(i,j,1) = me2cc(i,j,1) + ap1(-i2,-i3,-j)*conjg(ap1(-i2,-i3,-i)) !-- part4

me2cc(i,j,2) = me2cc(i,j,2) + ap2(-i3,-i2,-j)*conjg(ap2(-i3,-i2,-i)) !-- part5

me2cc(i,j,3) = me2cc(i,j,3) + ap1(-i2,-i3,-j)*conjg(ap2(-i3,-i2,-i))  + ap2(-i3,-i2,-j)*conjg(ap1(-i2,-i3,-i)) !--part6

enddo; enddo; enddo; enddo

where everything is defined as a quadruple precision complex vector. If i compile with -O0 optimization, I get the right result. If I compile with -O2, I get a wrong result. To get the right result with optimization, I have to split the code in two distinct nested do-loop structure, one containing the !--part 1,2,3 lines and another one containing the !--part 4,5,6 part.

Why is this happening? What am I doing wrong?

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.