| Issue |
239867
|
| Summary |
[flang] Runtime BESSEL_JN(n1, n2, 0.0) drops J(0, 0) when n1 is negative
|
| Labels |
flang:runtime,
flang
|
| Assignees |
|
| Reporter |
vzakhari
|
Flang accepts negative orders for transformational `BESSEL_JN(n1, n2, x)`. The runtime documents this as an extension; Fortran 2023 16.9.41 requires nonnegative `n1` and `n2`. On that extension, `J(0, 0) = 1` and `J(k, 0) = 0` for every other order.
When `n1 < 0 <= n2` and `x` is a runtime 0, the order-0 element is 0. The same call with constant arguments is correct, because it is folded and never enters the `x == 0` runtime.
```fortran
program bessel_jn_x0
implicit none
integer :: n1, n2
real(8) :: x, z(3)
n1 = -1
n2 = 1
x = 0.0d0
z = bessel_jn(n1, n2, x)
print '(A,3ES16.8)', 'runtime ', z
z = bessel_jn(-1, 1, 0.0d0)
print '(A,3ES16.8)', 'constant ', z
end program
```
```
runtime 0.00000000E+00 0.00000000E+00 0.00000000E+00
constant -0.00000000E+00 1.00000000E+00 0.00000000E+00
```
The three elements are orders -1, 0, and 1. Both lines should be `0, 1, 0` (the constant `-0` in the first element is fine). Observed with flang 24.0.0git (`b18fccbf0f1f727bdb1969455ad37d627be8a085`).
Lowering compares `x` to 0 with `arith.cmpf ueq` and calls `BesselJnX0` (`genBesselJn` in `flang/lib/Optimizer/Builder/IntrinsicCall.cpp`). `DoBesselJnX0` in `flang-rt/lib/runtime/transformational.cpp` stores 1 only when `n1 == 0`, then zeros every other element. For `n1 = -1`, `n2 = 1` the order-0 slot is the second element, so it stays 0.
After that zero fill, if `n1 <= 0 && n2 >= 0`, store 1 at subscript `-n1 + 1`. `testBesselJnX0` only calls the runtime with `n1 >= 0`, so it does not catch this. The arguments in a regression test have to be variables; constants are folded.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs