1!===-- module/ieee_arithmetic.f90 ------------------------------------------===! 2! 3! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4! See https://llvm.org/LICENSE.txt for license information. 5! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6! 7!===------------------------------------------------------------------------===! 8 9! See Fortran 2018, clause 17.2 10module ieee_arithmetic 11 12 use __Fortran_builtins, only: & 13 ieee_support_datatype => __builtin_ieee_support_datatype, & 14 ieee_support_denormal => __builtin_ieee_support_denormal, & 15 ieee_support_divide => __builtin_ieee_support_divide, & 16 ieee_support_inf => __builtin_ieee_support_inf, & 17 ieee_support_io => __builtin_ieee_support_io, & 18 ieee_support_nan => __builtin_ieee_support_nan, & 19 ieee_support_sqrt => __builtin_ieee_support_sqrt, & 20 ieee_support_standard => __builtin_ieee_support_standard, & 21 ieee_support_subnormal => __builtin_ieee_support_subnormal, & 22 ieee_support_underflow_control => __builtin_ieee_support_underflow_control 23 24 type :: ieee_class_type 25 private 26 integer(kind=1) :: which = 0 27 end type ieee_class_type 28 29 type(ieee_class_type), parameter :: & 30 ieee_signaling_nan = ieee_class_type(1), & 31 ieee_quiet_nan = ieee_class_type(2), & 32 ieee_negative_inf = ieee_class_type(3), & 33 ieee_negative_normal = ieee_class_type(4), & 34 ieee_negative_denormal = ieee_class_type(5), & 35 ieee_negative_zero = ieee_class_type(6), & 36 ieee_positive_zero = ieee_class_type(7), & 37 ieee_positive_subnormal = ieee_class_type(8), & 38 ieee_positive_normal = ieee_class_type(9), & 39 ieee_positive_inf = ieee_class_type(10), & 40 ieee_other_value = ieee_class_type(11) 41 42 type(ieee_class_type), parameter :: & 43 ieee_negative_subnormal = ieee_negative_denormal, & 44 ieee_positive_denormal = ieee_negative_subnormal 45 46 type :: ieee_round_type 47 private 48 integer(kind=1) :: mode = 0 49 end type ieee_round_type 50 51 type(ieee_round_type), parameter :: & 52 ieee_nearest = ieee_round_type(1), & 53 ieee_to_zero = ieee_round_type(2), & 54 ieee_up = ieee_round_type(3), & 55 ieee_down = ieee_round_type(4), & 56 ieee_away = ieee_round_type(5), & 57 ieee_other = ieee_round_type(6) 58 59 interface operator(==) 60 module procedure class_eq 61 module procedure round_eq 62 end interface operator(==) 63 interface operator(/=) 64 module procedure class_ne 65 module procedure round_ne 66 end interface operator(/=) 67 68 ! See Fortran 2018, 17.10 & 17.11 69 interface ieee_class 70 module procedure ieee_class_a2 71 module procedure ieee_class_a3 72 module procedure ieee_class_a4 73 module procedure ieee_class_a8 74 module procedure ieee_class_a10 75 module procedure ieee_class_a16 76 end interface ieee_class 77 78 interface ieee_copy_sign 79 module procedure ieee_copy_sign_a2 80 module procedure ieee_copy_sign_a3 81 module procedure ieee_copy_sign_a4 82 module procedure ieee_copy_sign_a8 83 module procedure ieee_copy_sign_a10 84 module procedure ieee_copy_sign_a16 85 end interface ieee_copy_sign 86 87 generic :: ieee_support_rounding => ieee_support_rounding_, & 88 ieee_support_rounding_2, ieee_support_rounding_3, & 89 ieee_support_rounding_4, ieee_support_rounding_8, & 90 ieee_support_rounding_10, ieee_support_rounding_16 91 private :: ieee_support_rounding_, & 92 ieee_support_rounding_2, ieee_support_rounding_3, & 93 ieee_support_rounding_4, ieee_support_rounding_8, & 94 ieee_support_rounding_10, ieee_support_rounding_16 95 96 ! TODO: more interfaces (_fma, &c.) 97 98 private :: classify 99 100 contains 101 102 elemental logical function class_eq(x,y) 103 type(ieee_class_type), intent(in) :: x, y 104 class_eq = x%which == y%which 105 end function class_eq 106 107 elemental logical function class_ne(x,y) 108 type(ieee_class_type), intent(in) :: x, y 109 class_ne = x%which /= y%which 110 end function class_ne 111 112 elemental logical function round_eq(x,y) 113 type(ieee_round_type), intent(in) :: x, y 114 round_eq = x%mode == y%mode 115 end function round_eq 116 117 elemental logical function round_ne(x,y) 118 type(ieee_round_type), intent(in) :: x, y 119 round_ne = x%mode /= y%mode 120 end function round_ne 121 122 elemental type(ieee_class_type) function classify( & 123 expo,maxExpo,negative,significandNZ,quietBit) 124 integer, intent(in) :: expo, maxExpo 125 logical, intent(in) :: negative, significandNZ, quietBit 126 if (expo == 0) then 127 if (significandNZ) then 128 if (negative) then 129 classify = ieee_negative_denormal 130 else 131 classify = ieee_positive_denormal 132 end if 133 else 134 if (negative) then 135 classify = ieee_negative_zero 136 else 137 classify = ieee_positive_zero 138 end if 139 end if 140 else if (expo == maxExpo) then 141 if (significandNZ) then 142 if (quietBit) then 143 classify = ieee_quiet_nan 144 else 145 classify = ieee_signaling_nan 146 end if 147 else 148 if (negative) then 149 classify = ieee_negative_inf 150 else 151 classify = ieee_positive_inf 152 end if 153 end if 154 else 155 if (negative) then 156 classify = ieee_negative_normal 157 else 158 classify = ieee_positive_normal 159 end if 160 end if 161 end function classify 162 163#define _CLASSIFY(RKIND,IKIND,TOTALBITS,PREC,IMPLICIT) \ 164 type(ieee_class_type) elemental function ieee_class_a##RKIND(x); \ 165 real(kind=RKIND), intent(in) :: x; \ 166 integer(kind=IKIND) :: raw; \ 167 integer, parameter :: significand = PREC - IMPLICIT; \ 168 integer, parameter :: exponentBits = TOTALBITS - 1 - significand; \ 169 integer, parameter :: maxExpo = shiftl(1, exponentBits) - 1; \ 170 integer :: exponent, sign; \ 171 logical :: negative, nzSignificand, quiet; \ 172 raw = transfer(x, raw); \ 173 exponent = ibits(raw, significand, exponentBits); \ 174 negative = btest(raw, TOTALBITS - 1); \ 175 nzSignificand = ibits(raw, 0, significand) /= 0; \ 176 quiet = btest(raw, significand - 1); \ 177 ieee_class_a##RKIND = classify(exponent, maxExpo, negative, nzSignificand, quiet); \ 178 end function ieee_class_a##RKIND 179 _CLASSIFY(2,2,16,11,1) 180 _CLASSIFY(3,2,16,8,1) 181 _CLASSIFY(4,4,32,24,1) 182 _CLASSIFY(8,8,64,53,1) 183 _CLASSIFY(10,16,80,64,0) 184 _CLASSIFY(16,16,128,112,1) 185#undef _CLASSIFY 186 187 ! TODO: This might need to be an actual Operation instead 188#define _COPYSIGN(RKIND,IKIND,BITS) \ 189 real(kind=RKIND) elemental function ieee_copy_sign_a##RKIND(x,y); \ 190 real(kind=RKIND), intent(in) :: x, y; \ 191 integer(kind=IKIND) :: xbits, ybits; \ 192 xbits = transfer(x, xbits); \ 193 ybits = transfer(y, ybits); \ 194 xbits = ior(ibclr(xbits, BITS-1), iand(ybits, shiftl(1_##IKIND, BITS-1))); \ 195 ieee_copy_sign_a##RKIND = transfer(xbits, x); \ 196 end function ieee_copy_sign_a##RKIND 197 _COPYSIGN(2,2,16) 198 _COPYSIGN(3,2,16) 199 _COPYSIGN(4,4,32) 200 _COPYSIGN(8,8,64) 201 _COPYSIGN(10,16,80) 202 _COPYSIGN(16,16,128) 203#undef _COPYSIGN 204 205 pure logical function ieee_support_rounding_(round_type) 206 type(ieee_round_type), intent(in) :: round_type 207 ieee_support_rounding_ = .true. 208 end function 209 pure logical function ieee_support_rounding_2(round_type,x) 210 type(ieee_round_type), intent(in) :: round_type 211 real(kind=2), intent(in) :: x 212 ieee_support_rounding_2 = .true. 213 end function 214 pure logical function ieee_support_rounding_3(round_type,x) 215 type(ieee_round_type), intent(in) :: round_type 216 real(kind=3), intent(in) :: x 217 ieee_support_rounding_3 = .true. 218 end function 219 pure logical function ieee_support_rounding_4(round_type,x) 220 type(ieee_round_type), intent(in) :: round_type 221 real(kind=4), intent(in) :: x 222 ieee_support_rounding_4 = .true. 223 end function 224 pure logical function ieee_support_rounding_8(round_type,x) 225 type(ieee_round_type), intent(in) :: round_type 226 real(kind=8), intent(in) :: x 227 ieee_support_rounding_8 = .true. 228 end function 229 pure logical function ieee_support_rounding_10(round_type,x) 230 type(ieee_round_type), intent(in) :: round_type 231 real(kind=10), intent(in) :: x 232 ieee_support_rounding_10 = .true. 233 end function 234 pure logical function ieee_support_rounding_16(round_type,x) 235 type(ieee_round_type), intent(in) :: round_type 236 real(kind=16), intent(in) :: x 237 ieee_support_rounding_16 = .true. 238 end function 239 240end module ieee_arithmetic 241