public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
@ 2022-09-04 16:30 FX
  2022-09-10 16:54 ` FX
  2022-09-17 12:14 ` Mikael Morin
  0 siblings, 2 replies; 5+ messages in thread
From: FX @ 2022-09-04 16:30 UTC (permalink / raw)
  To: Fortran; +Cc: gcc-patches

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

Hi,

The IEEE_MODES_TYPE type and the two functions that get and set it were added in Fortran 2018.  They can be implemented using the already existing target-specific functions.  A future optimization could, on some targets, set/get all modes through one or two instructions only, but that would need a new set of functions in all config/fpu-* files.

This was regtested on aarch64-darwin, which does not support underflow modes, so I will further test on x86_64-linux when I finish travelling in a couple of days.
OK to commit?

FX


[-- Attachment #2: 0001-Fortran-add-IEEE_MODES_TYPE-IEEE_GET_MODES-and-IEEE_.patch --]
[-- Type: application/octet-stream, Size: 7608 bytes --]

From 874d0e9c15323836610b6181ba50c234157fbac1 Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <fxcoudert@gmail.com>
Date: Sun, 4 Sep 2022 18:24:23 +0200
Subject: [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and
 IEEE_SET_MODES

The IEEE_MODES_TYPE type and the two functions that get and set it
were added in Fortran 2018.  They can be implemented using the already
existing target-specific functions.  A future optimization could, on
some targets, set/get all modes through one or two instructions only,
but that would need a new set of functions in all config/fpu-* files.

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

libgfortran/

	* ieee/ieee_exceptions.F90: Add IEEE_MODES_TYPE, IEEE_GET_MODES
	and IEEE_SET_MODES.
	* ieee/ieee_arithmetic.F90: Make them public in IEEE_ARITHMETIC
	as well.

gcc/testsuite/

	* gfortran.dg/ieee/modes_1.f90: New test.
---
 gcc/testsuite/gfortran.dg/ieee/modes_1.f90 | 95 ++++++++++++++++++++++
 libgfortran/ieee/ieee_arithmetic.F90       |  3 +-
 libgfortran/ieee/ieee_exceptions.F90       | 63 ++++++++++++++
 3 files changed, 160 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/ieee/modes_1.f90

diff --git a/gcc/testsuite/gfortran.dg/ieee/modes_1.f90 b/gcc/testsuite/gfortran.dg/ieee/modes_1.f90
new file mode 100644
index 00000000000..b6ab28847f7
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ieee/modes_1.f90
@@ -0,0 +1,95 @@
+! { dg-do run }
+!
+! Test IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
+
+
+! The symbols should be accessible from both IEEE_EXCEPTIONS
+! and IEEE_ARITHMETIC.
+
+subroutine test_1
+  use ieee_exceptions, only : IEEE_GET_MODES, IEEE_SET_MODES
+end subroutine
+
+subroutine test_2
+  use ieee_arithmetic, only : IEEE_GET_MODES, IEEE_SET_MODES
+end subroutine
+
+subroutine test_3
+  use ieee_exceptions, only : IEEE_MODES_TYPE
+end subroutine
+
+subroutine test_4
+  use ieee_arithmetic, only : IEEE_MODES_TYPE
+end subroutine
+
+
+! Check that the functions actually do the job
+
+program foo
+  use ieee_arithmetic
+  implicit none
+
+  type(ieee_modes_type) :: modes1, modes2
+  type(ieee_round_type) :: rmode
+  logical :: f
+
+  ! Set some modes
+  if (ieee_support_underflow_control()) then
+    call ieee_set_underflow_mode(gradual=.false.)
+  endif
+  if (ieee_support_rounding(ieee_up)) then
+    call ieee_set_rounding_mode(ieee_up)
+  endif
+  if (ieee_support_halting(ieee_overflow)) then
+    call ieee_set_halting_mode(ieee_overflow, .true.)
+  endif
+
+  call ieee_get_modes(modes1)
+
+  ! Change modes
+  if (ieee_support_underflow_control()) then
+    call ieee_set_underflow_mode(gradual=.true.)
+  endif
+  if (ieee_support_rounding(ieee_down)) then
+    call ieee_set_rounding_mode(ieee_down)
+  endif
+  if (ieee_support_halting(ieee_overflow)) then
+    call ieee_set_halting_mode(ieee_overflow, .false.)
+  endif
+
+  ! Save and restore the previous modes
+  call ieee_get_modes(modes2)
+  call ieee_set_modes(modes1)
+
+  ! Check them
+  if (ieee_support_underflow_control()) then
+    call ieee_get_underflow_mode(f)
+    if (f) stop 1
+  endif
+  if (ieee_support_rounding(ieee_down)) then
+    call ieee_get_rounding_mode(rmode)
+    if (rmode /= ieee_up) stop 2
+  endif
+  if (ieee_support_halting(ieee_overflow)) then
+    call ieee_get_halting_mode(ieee_overflow, f)
+    if (.not. f) stop 3
+  endif
+
+  ! Restore the second set of modes
+  call ieee_set_modes(modes2)
+
+  ! Check again
+  if (ieee_support_underflow_control()) then
+    call ieee_get_underflow_mode(f)
+    if (.not. f) stop 3
+  endif
+  if (ieee_support_rounding(ieee_down)) then
+    call ieee_get_rounding_mode(rmode)
+    if (rmode /= ieee_down) stop 4
+  endif
+  if (ieee_support_halting(ieee_overflow)) then
+    call ieee_get_halting_mode(ieee_overflow, f)
+    if (f) stop 5
+  endif
+
+end program foo
diff --git a/libgfortran/ieee/ieee_arithmetic.F90 b/libgfortran/ieee/ieee_arithmetic.F90
index c8ef3e2faeb..2f285f464f1 100644
--- a/libgfortran/ieee/ieee_arithmetic.F90
+++ b/libgfortran/ieee/ieee_arithmetic.F90
@@ -39,7 +39,8 @@ module IEEE_ARITHMETIC
     IEEE_DIVIDE_BY_ZERO, IEEE_UNDERFLOW, IEEE_INEXACT, IEEE_USUAL, &
     IEEE_ALL, IEEE_STATUS_TYPE, IEEE_GET_FLAG, IEEE_GET_HALTING_MODE, &
     IEEE_GET_STATUS, IEEE_SET_FLAG, IEEE_SET_HALTING_MODE, &
-    IEEE_SET_STATUS, IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING
+    IEEE_SET_STATUS, IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING, &
+    IEEE_MODES_TYPE, IEEE_GET_MODES, IEEE_SET_MODES
 
   ! Derived types and named constants
 
diff --git a/libgfortran/ieee/ieee_exceptions.F90 b/libgfortran/ieee/ieee_exceptions.F90
index 77363cfe342..3ed2f6e3e2c 100644
--- a/libgfortran/ieee/ieee_exceptions.F90
+++ b/libgfortran/ieee/ieee_exceptions.F90
@@ -56,6 +56,13 @@ module IEEE_EXCEPTIONS
     character(len=GFC_FPE_STATE_BUFFER_SIZE) :: hidden
   end type
 
+  type, public :: IEEE_MODES_TYPE
+    private
+    integer :: rounding
+    integer :: underflow
+    integer :: halting
+  end type
+
   interface IEEE_SUPPORT_FLAG
     module procedure IEEE_SUPPORT_FLAG_4, &
                      IEEE_SUPPORT_FLAG_8, &
@@ -72,9 +79,65 @@ module IEEE_EXCEPTIONS
   public :: IEEE_SET_HALTING_MODE, IEEE_GET_HALTING_MODE
   public :: IEEE_SET_FLAG, IEEE_GET_FLAG
   public :: IEEE_SET_STATUS, IEEE_GET_STATUS
+  public :: IEEE_SET_MODES, IEEE_GET_MODES
 
 contains
 
+! Fortran 2018: Saving and restoring floating-point modes
+! (rounding modes, underflow mode, and halting mode)
+! 
+! For now, we only have one rounding mode for all kinds.
+! Some targets could optimize getting/setting all modes at once, but for now
+! we make three calls.  This code must be kept in sync with:
+!   - IEEE_{GET,SET}_ROUNDING_MODE
+!   - IEEE_{GET,SET}_UNDERFLOW_MODE
+!   - IEEE_{GET,SET}_HALTING_MODE
+
+  subroutine IEEE_GET_MODES (MODES)
+    implicit none
+    type(IEEE_MODES_TYPE), intent(out) :: MODES
+
+    interface
+      integer function helper_rounding() &
+        bind(c, name="_gfortrani_get_fpu_rounding_mode")
+      end function
+      integer function helper_underflow() &
+        bind(c, name="_gfortrani_get_fpu_underflow_mode")
+      end function
+      pure integer function helper_halting() &
+          bind(c, name="_gfortrani_get_fpu_trap_exceptions")
+      end function
+    end interface
+
+    MODES%rounding = helper_rounding()
+    MODES%underflow = helper_underflow()
+    MODES%halting = helper_halting()
+  end subroutine
+
+  subroutine IEEE_SET_MODES (MODES)
+    implicit none
+    type(IEEE_MODES_TYPE), intent(in) :: MODES
+
+    interface
+      subroutine helper_rounding(val) &
+          bind(c, name="_gfortrani_set_fpu_rounding_mode")
+        integer, value :: val
+      end subroutine
+      subroutine helper_underflow(val) &
+          bind(c, name="_gfortrani_set_fpu_underflow_mode")
+        integer, value :: val
+      end subroutine
+      pure subroutine helper_halting(trap, notrap) &
+          bind(c, name="_gfortrani_set_fpu_trap_exceptions")
+        integer, intent(in), value :: trap, notrap
+      end subroutine
+    end interface
+
+    call helper_rounding(MODES%rounding)
+    call helper_underflow(MODES%underflow)
+    call helper_halting(MODES%halting, NOT(MODES%halting))
+  end subroutine
+
 ! Saving and restoring floating-point status
 
   subroutine IEEE_GET_STATUS (STATUS_VALUE)
-- 
2.32.1 (Apple Git-133)


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

* Re: [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
  2022-09-04 16:30 [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES FX
@ 2022-09-10 16:54 ` FX
  2022-09-17 12:14 ` Mikael Morin
  1 sibling, 0 replies; 5+ messages in thread
From: FX @ 2022-09-10 16:54 UTC (permalink / raw)
  To: Fortran; +Cc: gcc-patches

ping


> Hi,
> 
> The IEEE_MODES_TYPE type and the two functions that get and set it were added in Fortran 2018.  They can be implemented using the already existing target-specific functions.  A future optimization could, on some targets, set/get all modes through one or two instructions only, but that would need a new set of functions in all config/fpu-* files.
> 
> This was regtested on aarch64-darwin, which does not support underflow modes, so I will further test on x86_64-linux when I finish travelling in a couple of days.
> OK to commit?
> 
> FX
> 
> <0001-Fortran-add-IEEE_MODES_TYPE-IEEE_GET_MODES-and-IEEE_.patch>


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

* Re: [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
  2022-09-04 16:30 [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES FX
  2022-09-10 16:54 ` FX
@ 2022-09-17 12:14 ` Mikael Morin
  2022-09-19 12:25   ` FX
  1 sibling, 1 reply; 5+ messages in thread
From: Mikael Morin @ 2022-09-17 12:14 UTC (permalink / raw)
  To: FX, Fortran; +Cc: gcc-patches

Le 04/09/2022 à 18:30, FX via Fortran a écrit :
> Hi,
> 
> The IEEE_MODES_TYPE type and the two functions that get and set it were added in Fortran 2018.  They can be implemented using the already existing target-specific functions.  A future optimization could, on some targets, set/get all modes through one or two instructions only, but that would need a new set of functions in all config/fpu-* files.
> 
> This was regtested on aarch64-darwin, which does not support underflow modes, so I will further test on x86_64-linux when I finish travelling in a couple of days.
> OK to commit?
> 

Looks good, thanks.

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

* Re: [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
  2022-09-17 12:14 ` Mikael Morin
@ 2022-09-19 12:25   ` FX
  2022-09-21  8:07     ` FX
  0 siblings, 1 reply; 5+ messages in thread
From: FX @ 2022-09-19 12:25 UTC (permalink / raw)
  To: Mikael Morin; +Cc: Fortran, gcc-patches

Hi Mikael,

> Looks good, thanks.

Thank you for your reviews. This patch is committed to trunk: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=4637a1d293c978816ad622ba33e3a32a78640edd

FX

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

* Re: [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES
  2022-09-19 12:25   ` FX
@ 2022-09-21  8:07     ` FX
  0 siblings, 0 replies; 5+ messages in thread
From: FX @ 2022-09-21  8:07 UTC (permalink / raw)
  To: Fortran; +Cc: Mikael Morin, gcc-patches

I forgot to include the gfortran.map part of the patch, and so the test failed on platforms that have symbol versioning.
Fix below committed to master.

FX



commit ce8aed75a38b468490ecab4c318e3eb08d468608 (HEAD -> master)
Author: Francois-Xavier Coudert <fxcoudert@gmail.com>
Date:   2022-09-21 10:04:22 +0200

    Fortran: add symbols in version map for IEEE_GET_MODES and IEEE_SET_MODES
    
    The symbols were forgotten in the patch that added IEEE_GET_MODES
    and IEEE_SET_MODES.
    
    2022-09-21  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
    
    libgfortran/
    
            * gfortran.map: Add symbols for IEEE_GET_MODES
            and IEEE_SET_MODES.

diff --git a/libgfortran/gfortran.map b/libgfortran/gfortran.map
index e0e795c3d48..db9b86cb183 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -1759,3 +1759,9 @@ GFORTRAN_12 {
   _gfortran_transfer_real128_write;
 #endif
 } GFORTRAN_10.2;
+
+GFORTRAN_13 {
+  global:
+    __ieee_exceptions_MOD_ieee_get_modes;
+    __ieee_exceptions_MOD_ieee_set_modes;
+} GFORTRAN_12;


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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-04 16:30 [PATCH] Fortran: add IEEE_MODES_TYPE, IEEE_GET_MODES and IEEE_SET_MODES FX
2022-09-10 16:54 ` FX
2022-09-17 12:14 ` Mikael Morin
2022-09-19 12:25   ` FX
2022-09-21  8:07     ` 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).