ieee_exceptions.F90 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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>
  4. !
  5. ! This file is part of the GNU Fortran runtime library (libgfortran).
  6. !
  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.
  11. !
  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.
  16. !
  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.
  20. !
  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/>. */
  25. #include "config.h"
  26. #include "kinds.inc"
  27. #include "c99_protos.inc"
  28. #include "fpu-target.inc"
  29. module IEEE_EXCEPTIONS
  30. implicit none
  31. private
  32. ! Derived types and named constants
  33. type, public :: IEEE_FLAG_TYPE
  34. private
  35. integer :: hidden
  36. end type
  37. type(IEEE_FLAG_TYPE), parameter, public :: &
  38. IEEE_INVALID = IEEE_FLAG_TYPE(GFC_FPE_INVALID), &
  39. IEEE_OVERFLOW = IEEE_FLAG_TYPE(GFC_FPE_OVERFLOW), &
  40. IEEE_DIVIDE_BY_ZERO = IEEE_FLAG_TYPE(GFC_FPE_ZERO), &
  41. IEEE_UNDERFLOW = IEEE_FLAG_TYPE(GFC_FPE_UNDERFLOW), &
  42. IEEE_INEXACT = IEEE_FLAG_TYPE(GFC_FPE_INEXACT)
  43. type(IEEE_FLAG_TYPE), parameter, public :: &
  44. IEEE_USUAL(3) = [ IEEE_OVERFLOW, IEEE_DIVIDE_BY_ZERO, IEEE_INVALID ], &
  45. IEEE_ALL(5) = [ IEEE_USUAL, IEEE_UNDERFLOW, IEEE_INEXACT ]
  46. type, public :: IEEE_STATUS_TYPE
  47. private
  48. character(len=GFC_FPE_STATE_BUFFER_SIZE) :: hidden
  49. end type
  50. interface IEEE_SUPPORT_FLAG
  51. module procedure IEEE_SUPPORT_FLAG_4, &
  52. IEEE_SUPPORT_FLAG_8, &
  53. #ifdef HAVE_GFC_REAL_10
  54. IEEE_SUPPORT_FLAG_10, &
  55. #endif
  56. #ifdef HAVE_GFC_REAL_16
  57. IEEE_SUPPORT_FLAG_16, &
  58. #endif
  59. IEEE_SUPPORT_FLAG_NOARG
  60. end interface IEEE_SUPPORT_FLAG
  61. public :: IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING
  62. public :: IEEE_SET_HALTING_MODE, IEEE_GET_HALTING_MODE
  63. public :: IEEE_SET_FLAG, IEEE_GET_FLAG
  64. public :: IEEE_SET_STATUS, IEEE_GET_STATUS
  65. contains
  66. ! Saving and restoring floating-point status
  67. subroutine IEEE_GET_STATUS (STATUS_VALUE)
  68. implicit none
  69. type(IEEE_STATUS_TYPE), intent(out) :: STATUS_VALUE
  70. interface
  71. subroutine helper(ptr) &
  72. bind(c, name="_gfortrani_get_fpu_state")
  73. use, intrinsic :: iso_c_binding, only : c_char
  74. character(kind=c_char) :: ptr(*)
  75. end subroutine
  76. end interface
  77. call helper(STATUS_VALUE%hidden)
  78. end subroutine
  79. subroutine IEEE_SET_STATUS (STATUS_VALUE)
  80. implicit none
  81. type(IEEE_STATUS_TYPE), intent(in) :: STATUS_VALUE
  82. interface
  83. subroutine helper(ptr) &
  84. bind(c, name="_gfortrani_set_fpu_state")
  85. use, intrinsic :: iso_c_binding, only : c_char
  86. character(kind=c_char) :: ptr(*)
  87. end subroutine
  88. end interface
  89. call helper(STATUS_VALUE%hidden)
  90. end subroutine
  91. ! Getting and setting flags
  92. elemental subroutine IEEE_GET_FLAG (FLAG, FLAG_VALUE)
  93. implicit none
  94. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  95. logical, intent(out) :: FLAG_VALUE
  96. interface
  97. pure integer function helper() &
  98. bind(c, name="_gfortrani_get_fpu_except_flags")
  99. end function
  100. end interface
  101. FLAG_VALUE = (IAND(helper(), FLAG%hidden) /= 0)
  102. end subroutine
  103. elemental subroutine IEEE_SET_FLAG (FLAG, FLAG_VALUE)
  104. implicit none
  105. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  106. logical, intent(in) :: FLAG_VALUE
  107. interface
  108. pure subroutine helper(set, clear) &
  109. bind(c, name="_gfortrani_set_fpu_except_flags")
  110. integer, intent(in), value :: set, clear
  111. end subroutine
  112. end interface
  113. if (FLAG_VALUE) then
  114. call helper(FLAG%hidden, 0)
  115. else
  116. call helper(0, FLAG%hidden)
  117. end if
  118. end subroutine
  119. ! Querying and changing the halting mode
  120. elemental subroutine IEEE_GET_HALTING_MODE (FLAG, HALTING)
  121. implicit none
  122. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  123. logical, intent(out) :: HALTING
  124. interface
  125. pure integer function helper() &
  126. bind(c, name="_gfortrani_get_fpu_trap_exceptions")
  127. end function
  128. end interface
  129. HALTING = (IAND(helper(), FLAG%hidden) /= 0)
  130. end subroutine
  131. elemental subroutine IEEE_SET_HALTING_MODE (FLAG, HALTING)
  132. implicit none
  133. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  134. logical, intent(in) :: HALTING
  135. interface
  136. pure subroutine helper(trap, notrap) &
  137. bind(c, name="_gfortrani_set_fpu_trap_exceptions")
  138. integer, intent(in), value :: trap, notrap
  139. end subroutine
  140. end interface
  141. if (HALTING) then
  142. call helper(FLAG%hidden, 0)
  143. else
  144. call helper(0, FLAG%hidden)
  145. end if
  146. end subroutine
  147. ! Querying support
  148. pure logical function IEEE_SUPPORT_HALTING (FLAG)
  149. implicit none
  150. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  151. interface
  152. pure integer function helper(flag) &
  153. bind(c, name="_gfortrani_support_fpu_trap")
  154. integer, intent(in), value :: flag
  155. end function
  156. end interface
  157. IEEE_SUPPORT_HALTING = (helper(FLAG%hidden) /= 0)
  158. end function
  159. pure logical function IEEE_SUPPORT_FLAG_NOARG (FLAG)
  160. implicit none
  161. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  162. interface
  163. pure integer function helper(flag) &
  164. bind(c, name="_gfortrani_support_fpu_flag")
  165. integer, intent(in), value :: flag
  166. end function
  167. end interface
  168. IEEE_SUPPORT_FLAG_NOARG = (helper(FLAG%hidden) /= 0)
  169. end function
  170. pure logical function IEEE_SUPPORT_FLAG_4 (FLAG, X) result(res)
  171. implicit none
  172. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  173. real(kind=4), intent(in) :: X
  174. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  175. end function
  176. pure logical function IEEE_SUPPORT_FLAG_8 (FLAG, X) result(res)
  177. implicit none
  178. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  179. real(kind=8), intent(in) :: X
  180. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  181. end function
  182. #ifdef HAVE_GFC_REAL_10
  183. pure logical function IEEE_SUPPORT_FLAG_10 (FLAG, X) result(res)
  184. implicit none
  185. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  186. real(kind=10), intent(in) :: X
  187. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  188. end function
  189. #endif
  190. #ifdef HAVE_GFC_REAL_16
  191. pure logical function IEEE_SUPPORT_FLAG_16 (FLAG, X) result(res)
  192. implicit none
  193. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  194. real(kind=16), intent(in) :: X
  195. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  196. end function
  197. #endif
  198. end module IEEE_EXCEPTIONS