Complex arithmetic in Fortran

Toon Moene toon@moene.org
Wed Nov 13 14:21:19 GMT 2024


On 11/13/24 15:12, Richard Biener wrote:

> On Wed, Nov 13, 2024 at 3:05 PM Thomas Koenig <tkoenig@netcologne.de> wrote:
>>
>> Hello world,
>>
>> J3, the US Fortran standards committee, has passed
>> https://j3-fortran.org/doc/year/24/24-179.txt
>> which states (with a bit of an overabundance of
>> clarity) that, in Fortran, it is possible special-case
>> complex multiplication when one of the numbers is known
>> to have a zero component, for example when promoting
>> a real to complex for complex multiplication.  For
>> example, multiplying a complex variable b with a real
>> variable a can be done with c%re = b%re * a, c%im = b%im * a,
>> without considering NaNs and infinities. Apparently, other
>> Fortran compilers do this.
>>
>> They also stated that ISO/IEC 60559:2020 (aka IEEE 754) does
>> not specify complex arithmetic (I wouldn't know, because it is a
>> paywalled standard).
>>
>> How do we want to deal with this? Do we want to implement this
>> (it's an obvious speed advantage)?  Should it be the default?
>> Do we want to include this in -fcx-fortran-rules?
> 
> The middle-end complex lowering pass does this already, irrespective
> of NaNs, same for some degenerate cases with division.

Are you sure ?

For this code:

$ cat complex.f90
complex function p(c, r)
complex, intent(in) :: c
real, intent(in)    :: r
p = c * r
end

I definitely see a difference between

$ gfortran -O2 -S complex.f90

and

$ gfortran -O2 -ffast-math -S complex.f90

-- 
Toon Moene - e-mail: toon@moene.org - phone: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands


More information about the Gcc mailing list