llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Complex division operator overflows needlessly.

Open chandankds opened this issue 2 years ago • 2 comments

Complex division produces Infinity in results that can be computed without overflow.

The Intel compiler does not overflow this case, GNU does.

Program test_complex_division_overflow
  Real(Kind(1.d0)) :: x, y
  x = Huge(0.d0)
  y = 2.d0
  Call ops(x,y)
  Contains
    Subroutine ops(n,d)
      Real(Kind(1.d0)) :: n,d
      Complex(Kind(1.d0)) :: a,b
      a = Cmplx(n,-n,Kind(1.d0))
      b = Cmplx(d,d,Kind(1.d0))
      Print *,a/b ! avoids infinity?
    End Subroutine ops
End Program test_complex_division_overflow

chandankds avatar Dec 24 '23 09:12 chandankds

The robust algorithm described in Baudin and Smith (2012) avoids infinity in this case.

amd-thtsikas avatar Jan 05 '24 14:01 amd-thtsikas

@llvm/issue-subscribers-flang-runtime

Author: chandan singh (chandankds)

Complex division produces Infinity in results that can be computed without overflow.

The Intel compiler does not overflow this case, GNU does.

Program test_complex_division_overflow
  Real(Kind(1.d0)) :: x, y
  x = Huge(0.d0)
  y = 2.d0
  Call ops(x,y)
  Contains
    Subroutine ops(n,d)
      Real(Kind(1.d0)) :: n,d
      Complex(Kind(1.d0)) :: a,b
      a = Cmplx(n,-n,Kind(1.d0))
      b = Cmplx(d,d,Kind(1.d0))
      Print *,a/b ! avoids infinity?
    End Subroutine ops
End Program test_complex_division_overflow

llvmbot avatar Jan 05 '24 14:01 llvmbot