1 ! Implementation of the IEEE_EXCEPTIONS standard intrinsic module
2 ! Copyright (C) 2013-2022 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_4, &
61 IEEE_SUPPORT_FLAG_8, &
62 #ifdef HAVE_GFC_REAL_10
63 IEEE_SUPPORT_FLAG_10, &
65 #ifdef HAVE_GFC_REAL_16
66 IEEE_SUPPORT_FLAG_16, &
68 IEEE_SUPPORT_FLAG_NOARG
69 end interface IEEE_SUPPORT_FLAG
71 public :: IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING
72 public :: IEEE_SET_HALTING_MODE, IEEE_GET_HALTING_MODE
73 public :: IEEE_SET_FLAG, IEEE_GET_FLAG
74 public :: IEEE_SET_STATUS, IEEE_GET_STATUS
78 ! Saving and restoring floating-point status
80 subroutine IEEE_GET_STATUS (STATUS_VALUE)
82 type(IEEE_STATUS_TYPE), intent(out) :: STATUS_VALUE
85 subroutine helper(ptr) &
86 bind(c, name="_gfortrani_get_fpu_state")
87 use, intrinsic :: iso_c_binding, only : c_char
88 character(kind=c_char) :: ptr(*)
92 call helper(STATUS_VALUE%hidden)
95 subroutine IEEE_SET_STATUS (STATUS_VALUE)
97 type(IEEE_STATUS_TYPE), intent(in) :: STATUS_VALUE
100 subroutine helper(ptr) &
101 bind(c, name="_gfortrani_set_fpu_state")
102 use, intrinsic :: iso_c_binding, only : c_char
103 character(kind=c_char) :: ptr(*)
107 call helper(STATUS_VALUE%hidden)
110 ! Getting and setting flags
112 elemental subroutine IEEE_GET_FLAG (FLAG, FLAG_VALUE)
114 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
115 logical, intent(out) :: FLAG_VALUE
118 pure integer function helper() &
119 bind(c, name="_gfortrani_get_fpu_except_flags")
123 FLAG_VALUE = (IAND(helper(), FLAG%hidden) /= 0)
126 elemental subroutine IEEE_SET_FLAG (FLAG, FLAG_VALUE)
128 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
129 logical, intent(in) :: FLAG_VALUE
132 pure subroutine helper(set, clear) &
133 bind(c, name="_gfortrani_set_fpu_except_flags")
134 integer, intent(in), value :: set, clear
139 call helper(FLAG%hidden, 0)
141 call helper(0, FLAG%hidden)
145 ! Querying and changing the halting mode
147 elemental subroutine IEEE_GET_HALTING_MODE (FLAG, HALTING)
149 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
150 logical, intent(out) :: HALTING
153 pure integer function helper() &
154 bind(c, name="_gfortrani_get_fpu_trap_exceptions")
158 HALTING = (IAND(helper(), FLAG%hidden) /= 0)
161 elemental subroutine IEEE_SET_HALTING_MODE (FLAG, HALTING)
163 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
164 logical, intent(in) :: HALTING
167 pure subroutine helper(trap, notrap) &
168 bind(c, name="_gfortrani_set_fpu_trap_exceptions")
169 integer, intent(in), value :: trap, notrap
174 call helper(FLAG%hidden, 0)
176 call helper(0, FLAG%hidden)
182 pure logical function IEEE_SUPPORT_HALTING (FLAG)
184 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
187 pure integer function helper(flag) &
188 bind(c, name="_gfortrani_support_fpu_trap")
189 integer, intent(in), value :: flag
193 IEEE_SUPPORT_HALTING = (helper(FLAG%hidden) /= 0)
196 pure logical function IEEE_SUPPORT_FLAG_NOARG (FLAG)
198 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
201 pure integer function helper(flag) &
202 bind(c, name="_gfortrani_support_fpu_flag")
203 integer, intent(in), value :: flag
207 IEEE_SUPPORT_FLAG_NOARG = (helper(FLAG%hidden) /= 0)
210 pure logical function IEEE_SUPPORT_FLAG_4 (FLAG, X) result(res)
212 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
213 real(kind=4), intent(in) :: X
214 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
217 pure logical function IEEE_SUPPORT_FLAG_8 (FLAG, X) result(res)
219 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
220 real(kind=8), intent(in) :: X
221 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
224 #ifdef HAVE_GFC_REAL_10
225 pure logical function IEEE_SUPPORT_FLAG_10 (FLAG, X) result(res)
227 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
228 real(kind=10), intent(in) :: X
229 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
233 #ifdef HAVE_GFC_REAL_16
234 pure logical function IEEE_SUPPORT_FLAG_16 (FLAG, X) result(res)
236 type(IEEE_FLAG_TYPE), intent(in) :: FLAG
237 real(kind=16), intent(in) :: X
238 res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
242 end module IEEE_EXCEPTIONS