1 ! Implementation of the IEEE_EXCEPTIONS standard intrinsic module
2 ! Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 ! Contributed by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
5 ! This file is part of the GNU Fortran runtime library (libgfortran).
7 ! Libgfortran is free software; you can redistribute it and/or
8 ! modify it under the terms of the GNU General Public
9 ! License as published by the Free Software Foundation; either
10 ! version 3 of the License, or (at your option) any later version.
12 ! Libgfortran is distributed in the hope that it will be useful,
13 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ! GNU General Public License for more details.
17 ! Under Section 7 of GPL version 3, you are granted additional
18 ! permissions described in the GCC Runtime Library Exception, version
19 ! 3.1, as published by the Free Software Foundation.
21 ! You should have received a copy of the GNU General Public License and
22 ! a copy of the GCC Runtime Library Exception along with this program;
23 ! see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 ! <http://www.gnu.org/licenses/>. */
28 #include "c99_protos.inc"
29 #include "fpu-target.inc"
31 module IEEE_EXCEPTIONS
36 ! Derived types and named constants
38 type, public :: IEEE_FLAG_TYPE
43 type(IEEE_FLAG_TYPE), parameter, public :: &
44 IEEE_INVALID = IEEE_FLAG_TYPE(GFC_FPE_INVALID), &
45 IEEE_OVERFLOW = IEEE_FLAG_TYPE(GFC_FPE_OVERFLOW), &
46 IEEE_DIVIDE_BY_ZERO = IEEE_FLAG_TYPE(GFC_FPE_ZERO), &
47 IEEE_UNDERFLOW = IEEE_FLAG_TYPE(GFC_FPE_UNDERFLOW), &
48 IEEE_INEXACT = IEEE_FLAG_TYPE(GFC_FPE_INEXACT)
50 type(IEEE_FLAG_TYPE), parameter, public :: &
51 IEEE_USUAL(3) = [ IEEE_OVERFLOW, IEEE_DIVIDE_BY_ZERO, IEEE_INVALID ], &
52 IEEE_ALL(5) = [ IEEE_USUAL, IEEE_UNDERFLOW, IEEE_INEXACT ]
54 type, public :: IEEE_STATUS_TYPE
56 character(len=GFC_FPE_STATE_BUFFER_SIZE) :: hidden
59 interface IEEE_SUPPORT_FLAG
60 module procedure IEEE_SUPPORT_FLAG_NOARG, &
61 IEEE_SUPPORT_FLAG_4, &
63 end interface IEEE_SUPPORT_FLAG
65 public :: IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING
66 public :: IEEE_SET_HALTING_MODE, IEEE_GET_HALTING_MODE
67 public :: IEEE_SET_FLAG, IEEE_GET_FLAG
68 public :: IEEE_SET_STATUS, IEEE_GET_STATUS
72 ! Saving and restoring floating-point status
74 subroutine IEEE_GET_STATUS (STATUS_VALUE)
76 type(IEEE_STATUS_TYPE), intent(out) :: STATUS_VALUE
79 subroutine helper(ptr) &
80 bind(c, name="_gfortrani_get_fpu_state")
81 use, intrinsic :: iso_c_binding, only : c_char
82 character(kind=c_char) :: ptr(*)
86 call helper(STATUS_VALUE%hidden)
89 subroutine IEEE_SET_STATUS (STATUS_VALUE)
91 type(IEEE_STATUS_TYPE), intent(in) :: STATUS_VALUE
94 subroutine helper(ptr) &
95 bind(c, name="_gfortrani_set_fpu_state")
96 use, intrinsic :: iso_c_binding, only : c_char
97 character(kind=c_char) :: ptr(*)
101 call helper(STATUS_VALUE%hidden)
104 ! Getting and setting flags
106 elemental subroutine IEEE_GET_FLAG (FLAG, FLAG_VALUE)
108 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
109 logical, intent(out) :: FLAG_VALUE
112 pure integer function helper() &
113 bind(c, name="_gfortrani_get_fpu_except_flags")
117 FLAG_VALUE = (IAND(helper(), FLAG%hidden) /= 0)
120 elemental subroutine IEEE_SET_FLAG (FLAG, FLAG_VALUE)
122 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
123 logical, intent(in) :: FLAG_VALUE
126 pure subroutine helper(set, clear) &
127 bind(c, name="_gfortrani_set_fpu_except_flags")
128 integer, intent(in), value :: set, clear
133 call helper(FLAG%hidden, 0)
135 call helper(0, FLAG%hidden)
139 ! Querying and changing the halting mode
141 elemental subroutine IEEE_GET_HALTING_MODE (FLAG, HALTING)
143 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
144 logical, intent(out) :: HALTING
147 pure integer function helper() &
148 bind(c, name="_gfortrani_get_fpu_trap_exceptions")
152 HALTING = (IAND(helper(), FLAG%hidden) /= 0)
155 elemental subroutine IEEE_SET_HALTING_MODE (FLAG, HALTING)
157 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
158 logical, intent(in) :: HALTING
161 pure subroutine helper(trap, notrap) &
162 bind(c, name="_gfortrani_set_fpu_trap_exceptions")
163 integer, intent(in), value :: trap, notrap
168 call helper(FLAG%hidden, 0)
170 call helper(0, FLAG%hidden)
176 pure logical function IEEE_SUPPORT_HALTING (FLAG)
178 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
181 pure integer function helper(flag) &
182 bind(c, name="_gfortrani_support_fpu_trap")
183 integer, intent(in), value :: flag
187 IEEE_SUPPORT_HALTING = (helper(FLAG%hidden) /= 0)
190 pure logical function IEEE_SUPPORT_FLAG_NOARG (FLAG)
192 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
195 pure integer function helper(flag) &
196 bind(c, name="_gfortrani_support_fpu_flag")
197 integer, intent(in), value :: flag
201 IEEE_SUPPORT_FLAG_NOARG = (helper(FLAG%hidden) /= 0)
204 pure logical function IEEE_SUPPORT_FLAG_4 (FLAG, X) result(res)
206 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
207 real(kind=4), intent(in) :: X
208 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
211 pure logical function IEEE_SUPPORT_FLAG_8 (FLAG, X) result(res)
213 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
214 real(kind=8), intent(in) :: X
215 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
218 end module IEEE_EXCEPTIONS