ieee_exceptions.F90 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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>
  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_NOARG, &
  52. IEEE_SUPPORT_FLAG_4, &
  53. IEEE_SUPPORT_FLAG_8
  54. end interface IEEE_SUPPORT_FLAG
  55. public :: IEEE_SUPPORT_FLAG, IEEE_SUPPORT_HALTING
  56. public :: IEEE_SET_HALTING_MODE, IEEE_GET_HALTING_MODE
  57. public :: IEEE_SET_FLAG, IEEE_GET_FLAG
  58. public :: IEEE_SET_STATUS, IEEE_GET_STATUS
  59. contains
  60. ! Saving and restoring floating-point status
  61. subroutine IEEE_GET_STATUS (STATUS_VALUE)
  62. implicit none
  63. type(IEEE_STATUS_TYPE), intent(out) :: STATUS_VALUE
  64. interface
  65. subroutine helper(ptr) &
  66. bind(c, name="_gfortrani_get_fpu_state")
  67. use, intrinsic :: iso_c_binding, only : c_char
  68. character(kind=c_char) :: ptr(*)
  69. end subroutine
  70. end interface
  71. call helper(STATUS_VALUE%hidden)
  72. end subroutine
  73. subroutine IEEE_SET_STATUS (STATUS_VALUE)
  74. implicit none
  75. type(IEEE_STATUS_TYPE), intent(in) :: STATUS_VALUE
  76. interface
  77. subroutine helper(ptr) &
  78. bind(c, name="_gfortrani_set_fpu_state")
  79. use, intrinsic :: iso_c_binding, only : c_char
  80. character(kind=c_char) :: ptr(*)
  81. end subroutine
  82. end interface
  83. call helper(STATUS_VALUE%hidden)
  84. end subroutine
  85. ! Getting and setting flags
  86. elemental subroutine IEEE_GET_FLAG (FLAG, FLAG_VALUE)
  87. implicit none
  88. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  89. logical, intent(out) :: FLAG_VALUE
  90. interface
  91. pure integer function helper() &
  92. bind(c, name="_gfortrani_get_fpu_except_flags")
  93. end function
  94. end interface
  95. FLAG_VALUE = (IAND(helper(), FLAG%hidden) /= 0)
  96. end subroutine
  97. elemental subroutine IEEE_SET_FLAG (FLAG, FLAG_VALUE)
  98. implicit none
  99. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  100. logical, intent(in) :: FLAG_VALUE
  101. interface
  102. pure subroutine helper(set, clear) &
  103. bind(c, name="_gfortrani_set_fpu_except_flags")
  104. integer, intent(in), value :: set, clear
  105. end subroutine
  106. end interface
  107. if (FLAG_VALUE) then
  108. call helper(FLAG%hidden, 0)
  109. else
  110. call helper(0, FLAG%hidden)
  111. end if
  112. end subroutine
  113. ! Querying and changing the halting mode
  114. elemental subroutine IEEE_GET_HALTING_MODE (FLAG, HALTING)
  115. implicit none
  116. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  117. logical, intent(out) :: HALTING
  118. interface
  119. pure integer function helper() &
  120. bind(c, name="_gfortrani_get_fpu_trap_exceptions")
  121. end function
  122. end interface
  123. HALTING = (IAND(helper(), FLAG%hidden) /= 0)
  124. end subroutine
  125. elemental subroutine IEEE_SET_HALTING_MODE (FLAG, HALTING)
  126. implicit none
  127. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  128. logical, intent(in) :: HALTING
  129. interface
  130. pure subroutine helper(trap, notrap) &
  131. bind(c, name="_gfortrani_set_fpu_trap_exceptions")
  132. integer, intent(in), value :: trap, notrap
  133. end subroutine
  134. end interface
  135. if (HALTING) then
  136. call helper(FLAG%hidden, 0)
  137. else
  138. call helper(0, FLAG%hidden)
  139. end if
  140. end subroutine
  141. ! Querying support
  142. pure logical function IEEE_SUPPORT_HALTING (FLAG)
  143. implicit none
  144. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  145. interface
  146. pure integer function helper(flag) &
  147. bind(c, name="_gfortrani_support_fpu_trap")
  148. integer, intent(in), value :: flag
  149. end function
  150. end interface
  151. IEEE_SUPPORT_HALTING = (helper(FLAG%hidden) /= 0)
  152. end function
  153. pure logical function IEEE_SUPPORT_FLAG_NOARG (FLAG)
  154. implicit none
  155. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  156. interface
  157. pure integer function helper(flag) &
  158. bind(c, name="_gfortrani_support_fpu_flag")
  159. integer, intent(in), value :: flag
  160. end function
  161. end interface
  162. IEEE_SUPPORT_FLAG_NOARG = (helper(FLAG%hidden) /= 0)
  163. end function
  164. pure logical function IEEE_SUPPORT_FLAG_4 (FLAG, X) result(res)
  165. implicit none
  166. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  167. real(kind=4), intent(in) :: X
  168. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  169. end function
  170. pure logical function IEEE_SUPPORT_FLAG_8 (FLAG, X) result(res)
  171. implicit none
  172. type(IEEE_FLAG_TYPE), intent(in) :: FLAG
  173. real(kind=8), intent(in) :: X
  174. res = IEEE_SUPPORT_FLAG_NOARG(FLAG)
  175. end function
  176. end module IEEE_EXCEPTIONS