public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran 2018 rounding modes changes
@ 2022-08-31 18:29 FX
  2022-08-31 20:42 ` Bernhard Reutner-Fischer
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: FX @ 2022-08-31 18:29 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 654 bytes --]

This adds new F2018 features, that are not really enabled (because their runtime support is optional).

1. Add the new IEEE_AWAY rounding mode. It is unsupported on all known targets, but could be supported by glibc and AIX as part of the C2x proposal. Testing for now is minimal, but once a target supports that rounding mode, the tests will fail and we can fix them by expanding them.

2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.


Regression-tested on x86_64-pc-linux-gnu, both 32- and 64-bit.
OK to commit?



[-- Attachment #2: 0001-fortran-Fortran-2018-rounding-modes-changes.patch --]
[-- Type: application/octet-stream, Size: 8346 bytes --]

From 06c1c3f53941c347205dd12bfe3c1601f829accb Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Date: Wed, 31 Aug 2022 19:15:20 +0200
Subject: [PATCH] fortran: Fortran 2018 rounding modes changes

Add the new IEEE_AWAY rounding mode. It is unsupported on all known
targets, but could be supported by glibc and AIX as part of the C2x
proposal. Testing for now is minimal.

Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and
IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not
support radices other than 2.

2022-08-31  Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>

gcc/fortran/
	* libgfortran.h: Declare GFC_FPE_AWAY.

gcc/testsuite/
	* gfortran.dg/ieee/rounding_2.f90: New test.

libgfortran/
	* ieee/ieee_arithmetic.F90: Add RADIX argument to
	IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE.
	* config/fpu-387.h: Add IEEE_AWAY mode.
	* config/fpu-aarch64.h: Add IEEE_AWAY mode.
	* config/fpu-aix.h: Add IEEE_AWAY mode.
	* config/fpu-generic.h: Add IEEE_AWAY mode.
	* config/fpu-glibc.h: Add IEEE_AWAY mode.
	* config/fpu-sysv.h: Add IEEE_AWAY mode.
---
 gcc/fortran/libgfortran.h                     |  1 +
 gcc/testsuite/gfortran.dg/ieee/rounding_2.f90 | 20 +++++++++++++++++
 libgfortran/config/fpu-387.h                  |  7 ++++--
 libgfortran/config/fpu-aarch64.h              |  7 ++++--
 libgfortran/config/fpu-aix.h                  | 22 +++++++++++++++++--
 libgfortran/config/fpu-generic.h              | 11 ++++++++--
 libgfortran/config/fpu-glibc.h                | 18 +++++++++++++++
 libgfortran/config/fpu-sysv.h                 |  7 ++++--
 libgfortran/ieee/ieee_arithmetic.F90          |  7 ++++--
 9 files changed, 88 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/ieee/rounding_2.f90

diff --git a/gcc/fortran/libgfortran.h b/gcc/fortran/libgfortran.h
index ef06194eeb1..79a8c2ff450 100644
--- a/gcc/fortran/libgfortran.h
+++ b/gcc/fortran/libgfortran.h
@@ -60,6 +60,7 @@ along with GCC; see the file COPYING3.  If not see
 #define GFC_FPE_TONEAREST  2
 #define GFC_FPE_TOWARDZERO 3
 #define GFC_FPE_UPWARD     4
+#define GFC_FPE_AWAY       5
 
 /* Size of the buffer required to store FPU state for any target.
    In particular, this has to be larger than fenv_t on all glibc targets.
diff --git a/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90 b/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90
new file mode 100644
index 00000000000..8af6c9182f4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+
+  use, intrinsic :: ieee_arithmetic
+  implicit none
+
+  real :: sx1, sx2, sx3
+  double precision :: dx1, dx2, dx3
+
+  ! IEEE_AWAY was added in Fortran 2018 and not supported by any target
+  ! at the moment. Just check we can query for its support.
+
+  ! We should support at least C float and C double types
+  if (ieee_support_rounding(ieee_away) &
+      .or. ieee_support_rounding(ieee_away, 0.) &
+      .or. ieee_support_rounding(ieee_away, 0.d0)) then
+    print *, "If a target / libc now supports this, we need to add a proper check!"
+    stop 1
+  end if
+
+end
diff --git a/libgfortran/config/fpu-387.h b/libgfortran/config/fpu-387.h
index fd00dab829a..e2f4a7d3fbf 100644
--- a/libgfortran/config/fpu-387.h
+++ b/libgfortran/config/fpu-387.h
@@ -418,9 +418,12 @@ get_fpu_rounding_mode (void)
 }
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 void
diff --git a/libgfortran/config/fpu-aarch64.h b/libgfortran/config/fpu-aarch64.h
index 3a2e4bab8eb..47893908f60 100644
--- a/libgfortran/config/fpu-aarch64.h
+++ b/libgfortran/config/fpu-aarch64.h
@@ -293,9 +293,12 @@ set_fpu_rounding_mode (int round)
 
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 
diff --git a/libgfortran/config/fpu-aix.h b/libgfortran/config/fpu-aix.h
index c643874af03..fb1ac809f03 100644
--- a/libgfortran/config/fpu-aix.h
+++ b/libgfortran/config/fpu-aix.h
@@ -320,6 +320,11 @@ get_fpu_rounding_mode (void)
 	return GFC_FPE_TOWARDZERO;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case FE_TONEARESTFROMZERO:
+	return GFC_FPE_AWAY;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
@@ -357,8 +362,14 @@ set_fpu_rounding_mode (int mode)
 	break;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case GFC_FPE_AWAY:
+	rnd_mode = FE_TONEARESTFROMZERO;
+	break;
+#endif
+
       default:
-	return; /* Should be unreachable.  */
+	return;
     }
 
   fesetround (rnd_mode);
@@ -398,8 +409,15 @@ support_fpu_rounding_mode (int mode)
 	return 0;
 #endif
 
+      case GFC_FPE_AWAY:
+#ifdef FE_TONEARESTFROMZERO
+	return 1;
+#else
+	return 0;
+#endif
+
       default:
-	return 0; /* Should be unreachable.  */
+	return 0;
     }
 }
 
diff --git a/libgfortran/config/fpu-generic.h b/libgfortran/config/fpu-generic.h
index 3b62228c1a1..9e976a8ded8 100644
--- a/libgfortran/config/fpu-generic.h
+++ b/libgfortran/config/fpu-generic.h
@@ -66,9 +66,16 @@ get_fpu_except_flags (void)
 
 int
 get_fpu_rounding_mode (void)
-{   
+{
+  return 0;
+}
+
+
+int
+support_fpu_rounding_mode (int mode __attribute__((unused)))
+{
   return 0;
-}               
+}
 
 
 void
diff --git a/libgfortran/config/fpu-glibc.h b/libgfortran/config/fpu-glibc.h
index 265ef693803..b56899fee0e 100644
--- a/libgfortran/config/fpu-glibc.h
+++ b/libgfortran/config/fpu-glibc.h
@@ -342,6 +342,11 @@ get_fpu_rounding_mode (void)
 	return GFC_FPE_TOWARDZERO;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case FE_TONEARESTFROMZERO:
+	return GFC_FPE_AWAY;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
@@ -379,6 +384,12 @@ set_fpu_rounding_mode (int mode)
 	break;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case GFC_FPE_GFC_FPE_AWAY:
+	rnd_mode = FE_TONEARESTFROMZERO;
+	break;
+#endif
+
       default:
 	return; /* Should be unreachable.  */
     }
@@ -420,6 +431,13 @@ support_fpu_rounding_mode (int mode)
 	return 0;
 #endif
 
+      case GFC_FPE_AWAY:
+#ifdef FE_TONEARESTFROMZERO
+	return 1;
+#else
+	return 0;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
diff --git a/libgfortran/config/fpu-sysv.h b/libgfortran/config/fpu-sysv.h
index 4de3852cea8..4681322ae9b 100644
--- a/libgfortran/config/fpu-sysv.h
+++ b/libgfortran/config/fpu-sysv.h
@@ -374,9 +374,12 @@ set_fpu_rounding_mode (int mode)
 
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 
diff --git a/libgfortran/ieee/ieee_arithmetic.F90 b/libgfortran/ieee/ieee_arithmetic.F90
index 4e01aa5504c..7dce37a5099 100644
--- a/libgfortran/ieee/ieee_arithmetic.F90
+++ b/libgfortran/ieee/ieee_arithmetic.F90
@@ -73,6 +73,7 @@ module IEEE_ARITHMETIC
     IEEE_TO_ZERO           = IEEE_ROUND_TYPE(GFC_FPE_TOWARDZERO), &
     IEEE_UP                = IEEE_ROUND_TYPE(GFC_FPE_UPWARD), &
     IEEE_DOWN              = IEEE_ROUND_TYPE(GFC_FPE_DOWNWARD), &
+    IEEE_AWAY              = IEEE_ROUND_TYPE(GFC_FPE_AWAY), &
     IEEE_OTHER             = IEEE_ROUND_TYPE(0)
 
 
@@ -1044,9 +1045,10 @@ contains
 
   ! IEEE_GET_ROUNDING_MODE
 
-  subroutine IEEE_GET_ROUNDING_MODE (ROUND_VALUE)
+  subroutine IEEE_GET_ROUNDING_MODE (ROUND_VALUE, RADIX)
     implicit none
     type(IEEE_ROUND_TYPE), intent(out) :: ROUND_VALUE
+    integer, intent(in), optional :: RADIX
 
     interface
       integer function helper() &
@@ -1060,9 +1062,10 @@ contains
 
   ! IEEE_SET_ROUNDING_MODE
 
-  subroutine IEEE_SET_ROUNDING_MODE (ROUND_VALUE)
+  subroutine IEEE_SET_ROUNDING_MODE (ROUND_VALUE, RADIX)
     implicit none
     type(IEEE_ROUND_TYPE), intent(in) :: ROUND_VALUE
+    integer, intent(in), optional :: RADIX
 
     interface
       subroutine helper(val) &
-- 
2.25.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-08-31 18:29 [PATCH] Fortran 2018 rounding modes changes FX
@ 2022-08-31 20:42 ` Bernhard Reutner-Fischer
  2022-08-31 21:23   ` FX
  2022-09-10 10:21 ` FX
  2022-09-19 15:35 ` Mikael Morin
  2 siblings, 1 reply; 10+ messages in thread
From: Bernhard Reutner-Fischer @ 2022-08-31 20:42 UTC (permalink / raw)
  To: FX, FX via Fortran, fortran; +Cc: gcc-patches

On 31 August 2022 20:29:12 CEST, FX via Fortran <fortran@gcc.gnu.org> wrote:


+      case GFC_FPE_GFC_FPE_AWAY:

typo?

thanks,

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-08-31 20:42 ` Bernhard Reutner-Fischer
@ 2022-08-31 21:23   ` FX
  0 siblings, 0 replies; 10+ messages in thread
From: FX @ 2022-08-31 21:23 UTC (permalink / raw)
  To: Bernhard Reutner-Fischer; +Cc: FX via Fortran, gcc-patches

> +      case GFC_FPE_GFC_FPE_AWAY:
> 
> typo?

Absolutely. Didn’t break the build because glibc currently doesn’t define FE_TONEARESTFROMZERO, but it should in the future (when C2x is included).

FX

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-08-31 18:29 [PATCH] Fortran 2018 rounding modes changes FX
  2022-08-31 20:42 ` Bernhard Reutner-Fischer
@ 2022-09-10 10:21 ` FX
  2022-09-19 12:26   ` FX
  2022-09-19 15:35 ` Mikael Morin
  2 siblings, 1 reply; 10+ messages in thread
From: FX @ 2022-09-10 10:21 UTC (permalink / raw)
  To: GNU Fortran; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 47 bytes --]

ping
(with fix for the typo Bernhard noticed)


[-- Attachment #2: 0001-Fortran-F2018-rounding-modes-changes.patch --]
[-- Type: application/octet-stream, Size: 8331 bytes --]

From c3d074950b72b91aee1f4bc2719abaf2c877d871 Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Date: Wed, 31 Aug 2022 19:15:20 +0200
Subject: [PATCH] Fortran: F2018 rounding modes changes

Add the new IEEE_AWAY rounding mode. It is unsupported on all known
targets, but could be supported by glibc and AIX as part of the C2x
proposal. Testing for now is minimal.

Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and
IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not
support radices other than 2.

2022-08-31  Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>

gcc/fortran/
	* libgfortran.h: Declare GFC_FPE_AWAY.

gcc/testsuite/
	* gfortran.dg/ieee/rounding_2.f90: New test.

libgfortran/
	* ieee/ieee_arithmetic.F90: Add RADIX argument to
	IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE.
	* config/fpu-387.h: Add IEEE_AWAY mode.
	* config/fpu-aarch64.h: Add IEEE_AWAY mode.
	* config/fpu-aix.h: Add IEEE_AWAY mode.
	* config/fpu-generic.h: Add IEEE_AWAY mode.
	* config/fpu-glibc.h: Add IEEE_AWAY mode.
	* config/fpu-sysv.h: Add IEEE_AWAY mode.
---
 gcc/fortran/libgfortran.h                     |  1 +
 gcc/testsuite/gfortran.dg/ieee/rounding_2.f90 | 20 +++++++++++++++++
 libgfortran/config/fpu-387.h                  |  7 ++++--
 libgfortran/config/fpu-aarch64.h              |  7 ++++--
 libgfortran/config/fpu-aix.h                  | 22 +++++++++++++++++--
 libgfortran/config/fpu-generic.h              | 11 ++++++++--
 libgfortran/config/fpu-glibc.h                | 18 +++++++++++++++
 libgfortran/config/fpu-sysv.h                 |  7 ++++--
 libgfortran/ieee/ieee_arithmetic.F90          |  7 ++++--
 9 files changed, 88 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/ieee/rounding_2.f90

diff --git a/gcc/fortran/libgfortran.h b/gcc/fortran/libgfortran.h
index ef06194eeb1..79a8c2ff450 100644
--- a/gcc/fortran/libgfortran.h
+++ b/gcc/fortran/libgfortran.h
@@ -60,6 +60,7 @@ along with GCC; see the file COPYING3.  If not see
 #define GFC_FPE_TONEAREST  2
 #define GFC_FPE_TOWARDZERO 3
 #define GFC_FPE_UPWARD     4
+#define GFC_FPE_AWAY       5
 
 /* Size of the buffer required to store FPU state for any target.
    In particular, this has to be larger than fenv_t on all glibc targets.
diff --git a/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90 b/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90
new file mode 100644
index 00000000000..8af6c9182f4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ieee/rounding_2.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+
+  use, intrinsic :: ieee_arithmetic
+  implicit none
+
+  real :: sx1, sx2, sx3
+  double precision :: dx1, dx2, dx3
+
+  ! IEEE_AWAY was added in Fortran 2018 and not supported by any target
+  ! at the moment. Just check we can query for its support.
+
+  ! We should support at least C float and C double types
+  if (ieee_support_rounding(ieee_away) &
+      .or. ieee_support_rounding(ieee_away, 0.) &
+      .or. ieee_support_rounding(ieee_away, 0.d0)) then
+    print *, "If a target / libc now supports this, we need to add a proper check!"
+    stop 1
+  end if
+
+end
diff --git a/libgfortran/config/fpu-387.h b/libgfortran/config/fpu-387.h
index fd00dab829a..e2f4a7d3fbf 100644
--- a/libgfortran/config/fpu-387.h
+++ b/libgfortran/config/fpu-387.h
@@ -418,9 +418,12 @@ get_fpu_rounding_mode (void)
 }
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 void
diff --git a/libgfortran/config/fpu-aarch64.h b/libgfortran/config/fpu-aarch64.h
index 3a2e4bab8eb..47893908f60 100644
--- a/libgfortran/config/fpu-aarch64.h
+++ b/libgfortran/config/fpu-aarch64.h
@@ -293,9 +293,12 @@ set_fpu_rounding_mode (int round)
 
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 
diff --git a/libgfortran/config/fpu-aix.h b/libgfortran/config/fpu-aix.h
index c643874af03..fb1ac809f03 100644
--- a/libgfortran/config/fpu-aix.h
+++ b/libgfortran/config/fpu-aix.h
@@ -320,6 +320,11 @@ get_fpu_rounding_mode (void)
 	return GFC_FPE_TOWARDZERO;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case FE_TONEARESTFROMZERO:
+	return GFC_FPE_AWAY;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
@@ -357,8 +362,14 @@ set_fpu_rounding_mode (int mode)
 	break;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case GFC_FPE_AWAY:
+	rnd_mode = FE_TONEARESTFROMZERO;
+	break;
+#endif
+
       default:
-	return; /* Should be unreachable.  */
+	return;
     }
 
   fesetround (rnd_mode);
@@ -398,8 +409,15 @@ support_fpu_rounding_mode (int mode)
 	return 0;
 #endif
 
+      case GFC_FPE_AWAY:
+#ifdef FE_TONEARESTFROMZERO
+	return 1;
+#else
+	return 0;
+#endif
+
       default:
-	return 0; /* Should be unreachable.  */
+	return 0;
     }
 }
 
diff --git a/libgfortran/config/fpu-generic.h b/libgfortran/config/fpu-generic.h
index 3b62228c1a1..9e976a8ded8 100644
--- a/libgfortran/config/fpu-generic.h
+++ b/libgfortran/config/fpu-generic.h
@@ -66,9 +66,16 @@ get_fpu_except_flags (void)
 
 int
 get_fpu_rounding_mode (void)
-{   
+{
+  return 0;
+}
+
+
+int
+support_fpu_rounding_mode (int mode __attribute__((unused)))
+{
   return 0;
-}               
+}
 
 
 void
diff --git a/libgfortran/config/fpu-glibc.h b/libgfortran/config/fpu-glibc.h
index 265ef693803..f34b696a5f7 100644
--- a/libgfortran/config/fpu-glibc.h
+++ b/libgfortran/config/fpu-glibc.h
@@ -342,6 +342,11 @@ get_fpu_rounding_mode (void)
 	return GFC_FPE_TOWARDZERO;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case FE_TONEARESTFROMZERO:
+	return GFC_FPE_AWAY;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
@@ -379,6 +384,12 @@ set_fpu_rounding_mode (int mode)
 	break;
 #endif
 
+#ifdef FE_TONEARESTFROMZERO
+      case GFC_FPE_AWAY:
+	rnd_mode = FE_TONEARESTFROMZERO;
+	break;
+#endif
+
       default:
 	return; /* Should be unreachable.  */
     }
@@ -420,6 +431,13 @@ support_fpu_rounding_mode (int mode)
 	return 0;
 #endif
 
+      case GFC_FPE_AWAY:
+#ifdef FE_TONEARESTFROMZERO
+	return 1;
+#else
+	return 0;
+#endif
+
       default:
 	return 0; /* Should be unreachable.  */
     }
diff --git a/libgfortran/config/fpu-sysv.h b/libgfortran/config/fpu-sysv.h
index 4de3852cea8..4681322ae9b 100644
--- a/libgfortran/config/fpu-sysv.h
+++ b/libgfortran/config/fpu-sysv.h
@@ -374,9 +374,12 @@ set_fpu_rounding_mode (int mode)
 
 
 int
-support_fpu_rounding_mode (int mode __attribute__((unused)))
+support_fpu_rounding_mode (int mode)
 {
-  return 1;
+  if (mode == GFC_FPE_AWAY)
+    return 0;
+  else
+    return 1;
 }
 
 
diff --git a/libgfortran/ieee/ieee_arithmetic.F90 b/libgfortran/ieee/ieee_arithmetic.F90
index 4e01aa5504c..7dce37a5099 100644
--- a/libgfortran/ieee/ieee_arithmetic.F90
+++ b/libgfortran/ieee/ieee_arithmetic.F90
@@ -73,6 +73,7 @@ module IEEE_ARITHMETIC
     IEEE_TO_ZERO           = IEEE_ROUND_TYPE(GFC_FPE_TOWARDZERO), &
     IEEE_UP                = IEEE_ROUND_TYPE(GFC_FPE_UPWARD), &
     IEEE_DOWN              = IEEE_ROUND_TYPE(GFC_FPE_DOWNWARD), &
+    IEEE_AWAY              = IEEE_ROUND_TYPE(GFC_FPE_AWAY), &
     IEEE_OTHER             = IEEE_ROUND_TYPE(0)
 
 
@@ -1044,9 +1045,10 @@ contains
 
   ! IEEE_GET_ROUNDING_MODE
 
-  subroutine IEEE_GET_ROUNDING_MODE (ROUND_VALUE)
+  subroutine IEEE_GET_ROUNDING_MODE (ROUND_VALUE, RADIX)
     implicit none
     type(IEEE_ROUND_TYPE), intent(out) :: ROUND_VALUE
+    integer, intent(in), optional :: RADIX
 
     interface
       integer function helper() &
@@ -1060,9 +1062,10 @@ contains
 
   ! IEEE_SET_ROUNDING_MODE
 
-  subroutine IEEE_SET_ROUNDING_MODE (ROUND_VALUE)
+  subroutine IEEE_SET_ROUNDING_MODE (ROUND_VALUE, RADIX)
     implicit none
     type(IEEE_ROUND_TYPE), intent(in) :: ROUND_VALUE
+    integer, intent(in), optional :: RADIX
 
     interface
       subroutine helper(val) &
-- 
2.25.1


[-- Attachment #3: Type: text/plain, Size: 812 bytes --]



> Le 31 août 2022 à 20:29, FX <fxcoudert@gmail.com> a écrit :
> 
> This adds new F2018 features, that are not really enabled (because their runtime support is optional).
> 
> 1. Add the new IEEE_AWAY rounding mode. It is unsupported on all known targets, but could be supported by glibc and AIX as part of the C2x proposal. Testing for now is minimal, but once a target supports that rounding mode, the tests will fail and we can fix them by expanding them.
> 
> 2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.
> 
> 
> Regression-tested on x86_64-pc-linux-gnu, both 32- and 64-bit.
> OK to commit?
> 
> 
> <0001-fortran-Fortran-2018-rounding-modes-changes.patch>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-09-10 10:21 ` FX
@ 2022-09-19 12:26   ` FX
  0 siblings, 0 replies; 10+ messages in thread
From: FX @ 2022-09-19 12:26 UTC (permalink / raw)
  Cc: GNU Fortran, gcc-patches

Committed as https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=4637a1d293c978816ad622ba33e3a32a78640edd

FX


> Le 10 sept. 2022 à 12:21, FX <fxcoudert@gmail.com> a écrit :
> 
> ping
> (with fix for the typo Bernhard noticed)
> 
> <0001-Fortran-F2018-rounding-modes-changes.patch>
> 
>> Le 31 août 2022 à 20:29, FX <fxcoudert@gmail.com> a écrit :
>> 
>> This adds new F2018 features, that are not really enabled (because their runtime support is optional).
>> 
>> 1. Add the new IEEE_AWAY rounding mode. It is unsupported on all known targets, but could be supported by glibc and AIX as part of the C2x proposal. Testing for now is minimal, but once a target supports that rounding mode, the tests will fail and we can fix them by expanding them.
>> 
>> 2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.
>> 
>> 
>> Regression-tested on x86_64-pc-linux-gnu, both 32- and 64-bit.
>> OK to commit?
>> 
>> 
>> <0001-fortran-Fortran-2018-rounding-modes-changes.patch>
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-08-31 18:29 [PATCH] Fortran 2018 rounding modes changes FX
  2022-08-31 20:42 ` Bernhard Reutner-Fischer
  2022-09-10 10:21 ` FX
@ 2022-09-19 15:35 ` Mikael Morin
  2022-09-19 16:17   ` FX
  2 siblings, 1 reply; 10+ messages in thread
From: Mikael Morin @ 2022-09-19 15:35 UTC (permalink / raw)
  To: FX, fortran; +Cc: gcc-patches

Hello,

I'm coming (late) to this.

Le 31/08/2022 à 20:29, FX via Fortran a écrit :
> This adds new F2018 features, that are not really enabled (because their runtime support is optional).
> 
(...)

> 2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.
> 
If we accept the argument, we have to support it somehow.
So for IEEE_GET_ROUNDING_MODE (*, 10), we should return IEEE_OTHER, 
shouldn't we?
There is no problem for IEEE_SET_ROUNDING_MODE (*, 10) as there is no 
way this to be a valid call if radix 10 is not supported, but changing a 
compile time error to a runtime unexpected behavior seems like a step 
backwards.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-09-19 15:35 ` Mikael Morin
@ 2022-09-19 16:17   ` FX
  2022-09-19 17:09     ` Mikael Morin
  0 siblings, 1 reply; 10+ messages in thread
From: FX @ 2022-09-19 16:17 UTC (permalink / raw)
  To: Mikael Morin; +Cc: Fortran, gcc-patches

Hi,

>> 2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.
> If we accept the argument, we have to support it somehow.
> So for IEEE_GET_ROUNDING_MODE (*, 10), we should return IEEE_OTHER, shouldn't we?

I think I disagree. We do not support any real kind with radix 10, so calling IEEE_GET_ROUNDING_MODE with RADIX=10 is invalid. Or, in another interpretation, the rounding mode is whatever-you-want-to-call it, since you cannot perform arithmetic in that radix anyway.


> There is no problem for IEEE_SET_ROUNDING_MODE (*, 10) as there is no way this to be a valid call if radix 10 is not supported, but changing a compile time error to a runtime unexpected behavior seems like a step backwards.

What do you mean by that? The behavior is not unexpected, the value returned by IEEE_GET_ROUNDING_MODE for RADIX=10 is irrelevant and cannot be used for anything useful.

FX

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-09-19 16:17   ` FX
@ 2022-09-19 17:09     ` Mikael Morin
  2022-09-19 17:29       ` FX
  0 siblings, 1 reply; 10+ messages in thread
From: Mikael Morin @ 2022-09-19 17:09 UTC (permalink / raw)
  To: FX; +Cc: Fortran, gcc-patches

Le 19/09/2022 à 18:17, FX a écrit :
> Hi,
> 
>>> 2. Add the optional RADIX argument to IEEE_SET_ROUNDING_MODE and IEEE_GET_ROUNDING_MODE. It is unused for now, because we do not support floating-point radices other than 2.
>> If we accept the argument, we have to support it somehow.
>> So for IEEE_GET_ROUNDING_MODE (*, 10), we should return IEEE_OTHER, shouldn't we?
> 
> I think I disagree. We do not support any real kind with radix 10, so calling IEEE_GET_ROUNDING_MODE with RADIX=10 is invalid. Or, in another interpretation, the rounding mode is whatever-you-want-to-call it, since you cannot perform arithmetic in that radix anyway.
> 
Hmm, not really convinced, but that's a possible interpretation, I guess.
> 
>> There is no problem for IEEE_SET_ROUNDING_MODE (*, 10) as there is no way this to be a valid call if radix 10 is not supported, but changing a compile time error to a runtime unexpected behavior seems like a step backwards.
> 
> What do you mean by that? The behavior is not unexpected, the value returned by IEEE_GET_ROUNDING_MODE for RADIX=10 is irrelevant and cannot be used for anything useful.
> 

My sentence was about IEEE_*S*ET_ROUNDING_MODE actually.
My point that we previously reported an error (at compile time) to a 
user that would try to pass a radix 10 while we now silently do... 
something else that was requested (i.e. set the radix 2 rounding mode).

I think it's worth at least documenting that only radix 2 is supported.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-09-19 17:09     ` Mikael Morin
@ 2022-09-19 17:29       ` FX
  2022-09-21  9:15         ` FX
  0 siblings, 1 reply; 10+ messages in thread
From: FX @ 2022-09-19 17:29 UTC (permalink / raw)
  To: Mikael Morin; +Cc: Fortran, gcc-patches

Hi,

> Hmm, not really convinced, but that's a possible interpretation, I guess.

It seems to me to be in line with the handling of all other IEEE intrinsics: the user is responsible for querying support before calling any relevant routine. I admit that there is no explicit language in the case of the rounding mode, I will try to find time to ask for an official interpretation.


> My sentence was about IEEE_*S*ET_ROUNDING_MODE actually.
> My point that we previously reported an error (at compile time) to a user that would try to pass a radix 10 while we now silently do... something else that was requested (i.e. set the radix 2 rounding mode).

Oh sorry, I see what you mean and you’re totally right. This is an easy improvement, and while I believe it is undefined behaviour in any case, we can easily improve that.


> I think it's worth at least documenting that only radix 2 is supported.

That also makes sense. I’ll propose a patch.

Thanks,
FX

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Fortran 2018 rounding modes changes
  2022-09-19 17:29       ` FX
@ 2022-09-21  9:15         ` FX
  0 siblings, 0 replies; 10+ messages in thread
From: FX @ 2022-09-21  9:15 UTC (permalink / raw)
  To: Fortran; +Cc: Mikael Morin, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 63 bytes --]

Follow-up patch, including a test, committed as attached.

FX


[-- Attachment #2: 0001-Fortran-handle-RADIX-kind-in-IEEE_SET_ROUNDING_MODE.patch --]
[-- Type: application/octet-stream, Size: 2978 bytes --]

From 1aea1cb714f5a676901e702fc733595e0c9ad413 Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Date: Wed, 21 Sep 2022 11:06:19 +0200
Subject: [PATCH] Fortran: handle RADIX kind in IEEE_SET_ROUNDING_MODE

Make sure that calling IEEE_SET_ROUNDING_MODE with RADIX=10 does not
affect the binary rounding mode.

2022-09-21  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

libgfortran/

	* ieee/ieee_arithmetic.F90 (IEEE_SET_ROUNDING_MODE): Handle
	RADIX argument better.

gcc/testsuite/

	* gfortran.dg/ieee/rounding_3.f90: New test.
---
 gcc/testsuite/gfortran.dg/ieee/rounding_3.f90 | 27 +++++++++++++++++++
 libgfortran/ieee/ieee_arithmetic.F90          | 12 ++++++---
 2 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/ieee/rounding_3.f90

diff --git a/gcc/testsuite/gfortran.dg/ieee/rounding_3.f90 b/gcc/testsuite/gfortran.dg/ieee/rounding_3.f90
new file mode 100644
index 00000000000..ff4e834a042
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ieee/rounding_3.f90
@@ -0,0 +1,27 @@
+! { dg-do run }
+
+  ! Test IEEE_GET_ROUNDING_MODE and IEEE_SET_ROUNDING_MODE
+  ! with a RADIX argument
+  use, intrinsic :: ieee_arithmetic
+  implicit none
+
+  real :: sx1
+  type(ieee_round_type) :: r
+
+  if (ieee_support_rounding(ieee_up, sx1) .and. &
+      ieee_support_rounding(ieee_down, sx1)) then
+
+    call ieee_set_rounding_mode(ieee_up)
+    call ieee_get_rounding_mode(r)
+    if (r /= ieee_up) stop 1
+
+    call ieee_set_rounding_mode(ieee_down, radix=2)
+    call ieee_get_rounding_mode(r, radix=2)
+    if (r /= ieee_down) stop 2
+
+    call ieee_set_rounding_mode(ieee_up, radix=10)
+    call ieee_get_rounding_mode(r, radix=2)
+    if (r /= ieee_down) stop 3
+  end if
+
+end
diff --git a/libgfortran/ieee/ieee_arithmetic.F90 b/libgfortran/ieee/ieee_arithmetic.F90
index ce30e4afca3..4c8e3bb5e64 100644
--- a/libgfortran/ieee/ieee_arithmetic.F90
+++ b/libgfortran/ieee/ieee_arithmetic.F90
@@ -816,7 +816,7 @@ REM_MACRO(4,4,4)
                      IEEE_SUPPORT_ROUNDING_NOARG
   end interface
   public :: IEEE_SUPPORT_ROUNDING
-  
+
   ! Interface to the FPU-specific function
   interface
     pure integer function support_rounding_helper(flag) &
@@ -839,7 +839,7 @@ REM_MACRO(4,4,4)
                      IEEE_SUPPORT_UNDERFLOW_CONTROL_NOARG
   end interface
   public :: IEEE_SUPPORT_UNDERFLOW_CONTROL
-  
+
   ! Interface to the FPU-specific function
   interface
     pure integer function support_underflow_control_helper(kind) &
@@ -1074,7 +1074,13 @@ contains
         integer, value :: val
       end subroutine
     end interface
-    
+
+    ! We do not support RADIX = 10, and such calls should not
+    ! modify the binary rounding mode.
+    if (present(RADIX)) then
+      if (RADIX == 10) return
+    end if
+
     call helper(ROUND_VALUE%hidden)
   end subroutine
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-09-21  9:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-31 18:29 [PATCH] Fortran 2018 rounding modes changes FX
2022-08-31 20:42 ` Bernhard Reutner-Fischer
2022-08-31 21:23   ` FX
2022-09-10 10:21 ` FX
2022-09-19 12:26   ` FX
2022-09-19 15:35 ` Mikael Morin
2022-09-19 16:17   ` FX
2022-09-19 17:09     ` Mikael Morin
2022-09-19 17:29       ` FX
2022-09-21  9:15         ` FX

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).