From 492954263e39346287a5a2a32bcc5312466a0ee1 Mon Sep 17 00:00:00 2001 From: Francois-Xavier Coudert Date: Sun, 2 Jan 2022 11:36:23 +0100 Subject: Fortran: Allow IEEE_CLASS to identify signaling NaNs We use the issignaling macro, present in some libc's (notably glibc), when it is available. Compile all IEEE-related files in the library (both C and Fortran sources) with -fsignaling-nans to ensure maximum compatibility. libgfortran/ChangeLog: PR fortran/82207 * Makefile.am: Pass -fsignaling-nans for IEEE files. * Makefile.in: Regenerate. * ieee/ieee_helper.c: Use issignaling macro to recognized signaling NaNs. gcc/testsuite/ChangeLog: PR fortran/82207 * gfortran.dg/ieee/signaling_1.f90: New test. * gfortran.dg/ieee/signaling_1_c.c: New file. --- libgfortran/Makefile.am | 8 +++++++- libgfortran/Makefile.in | 6 +++++- libgfortran/ieee/ieee_helper.c | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) (limited to 'libgfortran') diff --git a/libgfortran/Makefile.am b/libgfortran/Makefile.am index 008f2e7549c..b7ef912a440 100644 --- a/libgfortran/Makefile.am +++ b/libgfortran/Makefile.am @@ -185,6 +185,8 @@ endif if IEEE_SUPPORT +gfor_ieee_helper_src=ieee/ieee_helper.c + gfor_helper_src+=ieee/ieee_helper.c gfor_ieee_src= \ @@ -991,9 +993,13 @@ selected_real_kind.lo selected_int_kind.lo: AM_FCFLAGS += -fallow-leading-unders if IEEE_SUPPORT # Add flags for IEEE modules -$(patsubst %.F90,%.lo,$(notdir $(gfor_ieee_src))): AM_FCFLAGS += -Wno-unused-dummy-argument -Wno-c-binding-type -ffree-line-length-0 -fallow-leading-underscore +$(patsubst %.F90,%.lo,$(notdir $(gfor_ieee_src))): AM_FCFLAGS += -Wno-unused-dummy-argument -Wno-c-binding-type -ffree-line-length-0 -fallow-leading-underscore -fsignaling-nans + +# Add flags for IEEE helper code +$(patsubst %.c,%.lo,$(notdir $(gfor_ieee_helper_src))): AM_CFLAGS += -fsignaling-nans endif + # Dependencies between IEEE_ARITHMETIC and IEEE_EXCEPTIONS ieee_arithmetic.lo: ieee/ieee_arithmetic.F90 ieee_exceptions.lo $(LTPPFCCOMPILE) -c -o $@ $< diff --git a/libgfortran/Makefile.in b/libgfortran/Makefile.in index 5dac04e171e..3684b2aaa75 100644 --- a/libgfortran/Makefile.in +++ b/libgfortran/Makefile.in @@ -779,6 +779,7 @@ gfor_helper_src = intrinsics/associated.c intrinsics/abort.c \ intrinsics/selected_real_kind.f90 intrinsics/trigd.c \ intrinsics/unpack_generic.c runtime/in_pack_generic.c \ runtime/in_unpack_generic.c $(am__append_3) $(am__append_4) +@IEEE_SUPPORT_TRUE@gfor_ieee_helper_src = ieee/ieee_helper.c @IEEE_SUPPORT_FALSE@gfor_ieee_src = @IEEE_SUPPORT_TRUE@gfor_ieee_src = \ @IEEE_SUPPORT_TRUE@ieee/ieee_arithmetic.F90 \ @@ -6999,7 +7000,10 @@ $(patsubst %.F90,%.lo,$(patsubst %.f90,%.lo,$(notdir $(gfor_specific_src)))): AM selected_real_kind.lo selected_int_kind.lo: AM_FCFLAGS += -fallow-leading-underscore # Add flags for IEEE modules -@IEEE_SUPPORT_TRUE@$(patsubst %.F90,%.lo,$(notdir $(gfor_ieee_src))): AM_FCFLAGS += -Wno-unused-dummy-argument -Wno-c-binding-type -ffree-line-length-0 -fallow-leading-underscore +@IEEE_SUPPORT_TRUE@$(patsubst %.F90,%.lo,$(notdir $(gfor_ieee_src))): AM_FCFLAGS += -Wno-unused-dummy-argument -Wno-c-binding-type -ffree-line-length-0 -fallow-leading-underscore -fsignaling-nans + +# Add flags for IEEE helper code +@IEEE_SUPPORT_TRUE@$(patsubst %.c,%.lo,$(notdir $(gfor_ieee_helper_src))): AM_CFLAGS += -fsignaling-nans # Dependencies between IEEE_ARITHMETIC and IEEE_EXCEPTIONS ieee_arithmetic.lo: ieee/ieee_arithmetic.F90 ieee_exceptions.lo diff --git a/libgfortran/ieee/ieee_helper.c b/libgfortran/ieee/ieee_helper.c index d70728c5b79..7a103df58f0 100644 --- a/libgfortran/ieee/ieee_helper.c +++ b/libgfortran/ieee/ieee_helper.c @@ -25,6 +25,15 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include "libgfortran.h" + +/* Check support for issignaling macro. + TODO: In the future, provide fallback implementations for IEEE types, + because many libc's do not have issignaling yet. */ +#ifndef issignaling +# define issignaling(X) 0 +#endif + + /* Prototypes. */ extern int ieee_class_helper_4 (GFC_REAL_4 *); @@ -86,8 +95,10 @@ enum { \ if (res == IEEE_QUIET_NAN) \ { \ - /* TODO: Handle signaling NaNs */ \ - return res; \ + if (issignaling (*value)) \ + return IEEE_SIGNALING_NAN; \ + else \ + return IEEE_QUIET_NAN; \ } \ \ return res; \ -- cgit v1.2.3