Skip to content

[flang][mlir] complex arithmetic dialect conversion for absolute value is incorrect in face of underflow #62011

Closed
@k-arrows

Description

@k-arrows

Test program:

$ cat check.f90
program complex_arithmetic
  implicit none

  integer, parameter :: dp = selected_real_kind(15, 307)
  complex(dp) :: z1, z2, z3
  real(dp) :: abs_z1, abs_z2

  z1 = (1.0_dp, 1.0_dp)
  z2 = (1.0e-300_dp, 1.0e-300_dp)

  ! Divide z1 by z2
  z3 = z1 / z2
  write(*,*) "Result of z1 / z2: ", z3

  ! Calculate absolute values of z1 and z2
  abs_z1 = abs(z1)
  abs_z2 = abs(z2)
  write(*,*) "Absolute value of z1: ", abs_z1
  write(*,*) "Absolute value of z2: ", abs_z2

  ! Check for overflow or underflow in absolute value calculation
  if (abs_z1 == huge(abs_z1)) then
    write(*,*) "Overflow occurred in absolute value calculation of z1"
  endif

  if (abs_z2 == 0.0_dp) then
    write(*,*) "Underflow occurred in absolute value calculation of z2"
  endif

end program complex_arithmetic

flang-new vs gfortran (see the result of "Absolute value of z2")

$ flang-new -flang-experimental-exec check.f90
$ ./a.out
 Result of z1 / z2:  (9.999999999999999E+299,0.)
 Absolute value of z1:  1.4142135623730951
 Absolute value of z2:  0.
 Underflow occurred in absolute value calculation of z2
$ gfortran check.f90
$ ./a.out
 Result of z1 / z2:          (9.99999999999999904E+299,0.0000000000000000)
 Absolute value of z1:    1.4142135623730951
 Absolute value of z2:    1.4142135623730950E-300

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions