| Issue |
234904
|
| Summary |
[flang] COMPLEX(8) ** negative INTEGER: NaN when folded, zero at run time, when the result is subnormal
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
`z**n` with a COMPLEX(8) `z` and a negative INTEGER `n` gives a wrong result when `z**(-n)` overflows but `z**n` is a representable subnormal. Constant folding produces `(NaN,NaN)` and warns about overflow and an invalid argument. The runtime (`_FortranAzpowi`) returns zero. COMPLEX(10) and COMPLEX(16) behave the same way.
This is the COMPLEX counterpart of #192639, which #205262 fixed for REAL. The REAL and COMPLEX(4) lines below are controls. COMPLEX(4) is correct only because #230257 now evaluates it in COMPLEX(8).
Tested with `flang version 24.0.0git (https://github.com/llvm/llvm-project 1efb690992f0054079a9d8e474a551af066aee0c)`, which includes #205262, on x86_64 Linux.
### Constant folding
```fortran
program fold
implicit none
real(8), parameter :: r8 = 2.0_8**(-1070)
complex(4), parameter :: c4 = (2.0_4, 0.0_4)**(-133)
complex(8), parameter :: c8 = (2.0_8, 0.0_8)**(-1070)
complex(8), parameter :: c8i = (1.0_8, 1.0_8)**(-2140)
print *, 'real(8) 2**-1070 ', r8
print *, 'complex(4) (2,0)**-133 ', c4
print *, 'complex(8) (2,0)**-1070', c8
print *, 'complex(8) (1,1)**-2140', c8i
end program
```
```console
$ flang fold.f90 -o fold
fold.f90:5:33: warning: overflow on power with INTEGER exponent [-Wfolding-exception]
complex(8), parameter :: c8 = (2.0_8, 0.0_8)**(-1070)
^^^^^^^^^^^^^^^^^^^^^^^
fold.f90:5:33: warning: invalid argument on power with INTEGER exponent [-Wfolding-exception]
complex(8), parameter :: c8 = (2.0_8, 0.0_8)**(-1070)
^^^^^^^^^^^^^^^^^^^^^^^
fold.f90:6:34: warning: overflow on power with INTEGER exponent [-Wfolding-exception]
complex(8), parameter :: c8i = (1.0_8, 1.0_8)**(-2140)
^^^^^^^^^^^^^^^^^^^^^^^
fold.f90:6:34: warning: invalid argument on power with INTEGER exponent [-Wfolding-exception]
complex(8), parameter :: c8i = (1.0_8, 1.0_8)**(-2140)
^^^^^^^^^^^^^^^^^^^^^^^
$ ./fold
real(8) 2**-1070 8.E-323
complex(4) (2,0)**-133 (9.1835E-41,0.)
complex(8) (2,0)**-1070 (NaN,NaN)
complex(8) (1,1)**-2140 (NaN,NaN)
```
Expected: `(2,0)**(-1070)` is `(2**-1070, 0)`, and `(1,1)**(-2140) = (2i)**(-1070)` is `(-2**-1070, 0)`. Both are exact subnormals. gfortran 13.3 prints `(7.90505033345994471E-323,-0.0000000000000000)` and `(-7.90505033345994471E-323,0.0000000000000000)`.
### Run time
```fortran
program runtime
implicit none
complex(4) :: z4
complex(8) :: z8, w8
integer :: n4, n8, m8
call get(z4, n4, z8, n8, w8, m8)
print *, 'complex(4) (2,0)**-133 ', z4**n4
print *, 'complex(8) (2,0)**-1070', z8**n8
print *, 'complex(8) (1,1)**-2140', w8**m8
contains
subroutine get(z4, n4, z8, n8, w8, m8)
complex(4), intent(out) :: z4
complex(8), intent(out) :: z8, w8
integer, intent(out) :: n4, n8, m8
z4 = (2.0_4, 0.0_4); n4 = -133
z8 = (2.0_8, 0.0_8); n8 = -1070
w8 = (1.0_8, 1.0_8); m8 = -2140
end subroutine
end program
```
```console
$ flang runtime.f90 -o runtime && ./runtime
complex(4) (2,0)**-133 (9.1835E-41,0.)
complex(8) (2,0)**-1070 (0.,0.)
complex(8) (1,1)**-2140 (-0.,0.)
```
`-O2` prints the same. Both levels call `_FortranAcpowi` and `_FortranAzpowi`. gfortran 13.3 prints `(7.90505033345994471E-323,0.0000000000000000)` and `(-7.90505033345994471E-323,-0.0000000000000000)`.
COMPLEX(10) and COMPLEX(16) with `(2,0)**(-16440)` fold to `(NaN,NaN)` with the same two warnings, and evaluate to `(0.,0.)` at run time. On x86_64 both kinds call `_FortranAcqpowi`.
### Where it goes wrong
- Folding: the complex case uses the generic `TimesIntPowerOf` in `flang/lib/Evaluate/int-power.h`. It divides the running result by successive squares of the base, and the last square overflows (for base `(2,0)` it is `(Inf,0)`). `Complex::Divide` in `flang/lib/Evaluate/complex.cpp` picks its scaled algorithm only when computing `c*c + d*d` raises the overflow flag. An operand that is already infinite raises no flag, so the unscaled formula runs and computes `0*Inf` and `Inf/Inf`. Folding `(1.0_8,0.0_8) / cmplx(huge(1.0_8)*2, 0.0_8, 8)` on its own gives `(NaN,NaN)` with "invalid argument on division". The REAL overload added by #205262, which retries from `1/base` on overflow, does not apply to `value::Complex`.
- Runtime: `tgpowiInline` in `flang-rt/lib/runtime/complex-powi.cpp` computes `z**|n|` and inverts it once at the end. `z**|n|` overflows, so `1/z**|n|` is zero. Only `cpowi` and `cpowk` avoid this, by widening to `double _Complex`. `zpowi`, `zpowk`, `cqpowi` and `cqpowk` have no wider type to fall back on.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs