public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kargl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/107753] gfortran returns NaN in complex divisions (x+x*I)/(x+x*I) and (x+x*I)/(x-x*I)
Date: Sat, 19 Nov 2022 19:11:41 +0000	[thread overview]
Message-ID: <bug-107753-4-aiTb8H4N0e@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107753-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107753

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P4

--- Comment #10 from kargl at gcc dot gnu.org ---
(In reply to Steve Kargl from comment #9)
> On Fri, Nov 18, 2022 at 11:24:29PM +0000, sgk at troutmask dot
> apl.washington.edu wrote:
> > 
> > Does anyone know what is meant by "Fortran rules"?  F66 does not
> > have any particular algorithm specified.  I'll look at F77 shortly.
> > 
> 
> Well, I hunted down the origins of -fcx-fortran-rules.
> 
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=29549
> 
> So, it appears to be an optimization, where Smith's algorithm
> will fail for extreme values of the real and imaginary parts
> of the complex number.

So, I wrong a dirty little program to time complex division.


program foo

   use timerm, only : rdtsc

   implicit none

   integer, parameter :: n = 1024*1024, dp = kind(1.d0)
   real(dp) re(n), im(n)
   complex(dp) x(n)
   integer i
   integer(8) t0, t

   t = 0
   do i = 1, 10
      call random_number(re)
      call random_number(im)
      x = cmplx(4 * re, 10 * im,8)
      t0 = rdtsc()
      x = x / x
      t = t + (rdtsc() - t0)
   end do
   print '(G0,1X,G0)', x(1)
   print *, real(t,10) / 10 / n
end program foo


Compiled with gfortran with its current method of doing division
(i.e., -fcx-fortran-rules), I see roughly 44.5 clock ticks per
division.  If run with a patched gfortran that uses the method that
the C compiler uses, I get about 62 ticks per division.  So, using
the stricter method impacts performance.

I'll note that gfortran unilaterally enforces -fcx-fortran-rules,
i.e., -fno-cx-fortran-rules has no effect.  Perhaps, gfortran 
could be given a new -fcx-division=XXX option, where XXX is one of

   naive --> what -ffast-math does   (flags_complex_method = 0)
   smith --> what -fcx-fortran-rules (flags_complex_method = 1)
   strict -> default C behavior      (flags_complex_method = 2)

  parent reply	other threads:[~2022-11-19 19:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-18 19:32 [Bug fortran/107753] New: " weslley.pereira at ucdenver dot edu
2022-11-18 19:50 ` [Bug fortran/107753] " anlauf at gcc dot gnu.org
2022-11-18 20:50 ` kargl at gcc dot gnu.org
2022-11-18 21:26 ` anlauf at gcc dot gnu.org
2022-11-18 22:05 ` kargl at gcc dot gnu.org
2022-11-18 23:24 ` sgk at troutmask dot apl.washington.edu
2022-11-18 23:32 ` sgk at troutmask dot apl.washington.edu
2022-11-18 23:45 ` weslley.pereira at ucdenver dot edu
2022-11-18 23:47 ` weslley.pereira at ucdenver dot edu
2022-11-19  0:25 ` sgk at troutmask dot apl.washington.edu
2022-11-19 19:11 ` kargl at gcc dot gnu.org [this message]
2022-11-19 20:14 ` anlauf at gcc dot gnu.org
2022-11-20  0:54 ` sgk at troutmask dot apl.washington.edu
2022-12-07 21:16 ` anlauf at gcc dot gnu.org
2022-12-07 21:50 ` kargl at gcc dot gnu.org
2022-12-07 22:31 ` anlauf at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107753-4-aiTb8H4N0e@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).