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

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #2 from kargl at gcc dot gnu.org ---
(In reply to Richard Biener from comment #1)
> The invalid exception is raised here:
> 
> Program received signal SIGFPE, Arithmetic exception.
> 0x0000000000400cde in demo_nan () at t.f90:17
> 17         if ( (r32<=0.0_real32) .or. (r32>=0.0_real32) )then
> 
>    0x0000000000400cd2 <+416>:   movss  -0x134(%rbp),%xmm1
>    0x0000000000400cda <+424>:   pxor   %xmm0,%xmm0
> => 0x0000000000400cde <+428>:   comiss %xmm1,%xmm0
> 
> where %xmm1 is NaN.
> 
> (gdb) p $xmm1
> $2 = ( v4_float = (nan(0x400000), 0, 0, 0),
> 
> so not sure what you are expecting?  Is Fortran supposed to use the
> C equivalent of isgreaterequal (aka comparisons that do not raise
> exceptions?)

The Fortran standard says nothing about the option -ffpe-trap=invalid.
If one uses an option requesting a trap, then one might anticipate
that it cause a SIGFPE when an NaN is used in an expression.

This reduced testcase fails with 8-branch and trunk when compiled
with -ffpe-trap=invalid.

program demo_nan
   use,intrinsic :: iso_fortran_env, only: real32
   implicit none
   character(len=3),save :: STRING='NaN'
   real(kind=real32)  :: r32
   character(len=256) :: message
   integer            :: ios
   read(STRING,*)r32
   ! an option to terminate a program when a NaN is encountered
   ! (if X is NaN the comparison with 0. is always false.)
   if ( (r32<=0.0_real32) .or. (r32>=0.0_real32) )then
      write(*,*)'did not produce a nan_real32'
   else
      write(*,*)'found a nan_real32. Handle it.'
   endif
   ! list directed format
   write(*,*,iomsg=message,iostat=ios)r32
   if(ios.ne.0)write(*,*)trim(message)
   ! hexadecimal format to show different kinds
   write(*,'(z0)',iomsg=message,iostat=ios)r32
end program demo_nan

The above compiles with both 6-branch and an old 7-branch
(need to update 7 to test).

There are two ways in which I can avoid the SIGFPE and
still use an NaN.  First, one can use the intrinsic
function isnan() to change the conditional to

   if (.not.isnan(r32))then
      write(*,*)'did not produce a nan_real32'
   else
      write(*,*)'found a nan_real32. Handle it.'
   endif

The second, and likely preferred method, is to use the IEEE_ARITHMETIC
module that is provided by the Fortran Standard.

Reply via email to