00001 #include <shr_isnan.h>
00002
00003 module shr_infnan_mod
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 use shr_kind_mod, only : r8 => SHR_KIND_R8, r4 => SHR_KIND_R4
00037 implicit none
00038
00039 private
00040
00041 public :: shr_infnan_isnan
00042 public :: shr_infnan_isinf
00043 public :: shr_infnan_isposinf
00044 public :: shr_infnan_isneginf
00045
00046
00047 integer, parameter :: Single = selected_int_kind(precision(1.0_r4))
00048 integer, parameter :: Double = selected_int_kind(precision(1.0_r8))
00049
00050
00051 integer(Single), parameter :: sNaN = Z"7FC00000"
00052 integer(Single), parameter :: sPosInf = Z"7F800000"
00053 integer(Single), parameter :: sNegInf = Z"FF800000"
00054
00055
00056 integer(Double), parameter :: dNaN = Z"7FF8000000000000"
00057 integer(Double), parameter :: dPosInf = Z"7FF0000000000000"
00058 integer(Double), parameter :: dNegInf = Z"FFF0000000000000"
00059
00060
00061
00062 integer, parameter :: SPSB = bit_size(sNaN) - 1
00063 integer, parameter :: DPSB = bit_size(dNaN) - 1
00064
00065 interface shr_infnan_isnan
00066 #ifdef NOFTN_INTRINSIC
00067 module procedure c_sisnan_scalar
00068 module procedure c_sisnan_1D
00069 module procedure c_sisnan_2D
00070 module procedure c_sisnan_3D
00071 module procedure c_sisnan_4D
00072 module procedure c_sisnan_5D
00073 module procedure c_sisnan_6D
00074 module procedure c_sisnan_7D
00075 module procedure c_disnan_scalar
00076 module procedure c_disnan_1D
00077 module procedure c_disnan_2D
00078 module procedure c_disnan_3D
00079 module procedure c_disnan_4D
00080 module procedure c_disnan_5D
00081 module procedure c_disnan_6D
00082 module procedure c_disnan_7D
00083 #else
00084 module procedure sisnan
00085 module procedure disnan
00086 #endif
00087 end interface
00088
00089 interface shr_infnan_isinf
00090 module procedure sisinf
00091 module procedure disinf
00092 end interface
00093
00094 interface shr_infnan_isposinf
00095 module procedure sisposinf
00096 module procedure disposinf
00097 end interface
00098
00099 interface shr_infnan_isneginf
00100 module procedure sisneginf
00101 module procedure disneginf
00102 end interface
00103
00104
00105 integer :: shr_sisnan
00106 external :: shr_sisnan
00107 integer :: shr_disnan
00108 external :: shr_disnan
00109
00110 contains
00111
00112
00113
00114
00115 #ifndef NOFTN_INTRINSIC
00116
00117
00118 elemental function sisnan(x) result(res)
00119 #ifdef SunOS
00120 use IEEE_ARITHMETIC, only : IEEE_IS_NAN
00121 #endif
00122 implicit none
00123 real(r4), intent(in) :: x
00124 logical :: res
00125 #ifdef AIX
00126 intrinsic :: IEEE_IS_NAN
00127 #elif defined(ISNAN_INTRINSIC)
00128 intrinsic :: isnan
00129 #endif
00130
00131 #if defined(AIX) || defined(SunOS)
00132 res = IEEE_IS_NAN(x)
00133 #elif defined(ISNAN_INTRINSIC)
00134 res = isnan(x)
00135 #endif
00136
00137 end function
00138
00139
00140 elemental function disnan(d) result(res)
00141 #ifdef SunOS
00142 use IEEE_ARITHMETIC, only : IEEE_IS_NAN
00143 #endif
00144 implicit none
00145 real(r8), intent(in) :: d
00146 logical :: res
00147 #ifdef AIX
00148 intrinsic :: IEEE_IS_NAN
00149 #elif defined(ISNAN_INTRINSIC)
00150 intrinsic :: isnan
00151 #endif
00152
00153 #if defined(AIX) || defined(SunOS)
00154 res = IEEE_IS_NAN(d)
00155 #elif defined(ISNAN_INTRINSIC)
00156 res = isnan(d)
00157 #endif
00158
00159 end function
00160
00161
00162
00163
00164
00165
00166 #else
00167
00168 function c_sisnan_scalar(x) result(res)
00169 real(r4), intent(in) :: x
00170 logical :: res
00171
00172 res = (shr_sisnan(x) /= 0)
00173 end function c_sisnan_scalar
00174
00175 function c_sisnan_1D(x) result(res)
00176 real(r4), intent(in) :: x(:)
00177 logical :: res(size(x))
00178
00179 integer :: i
00180
00181 do i = 1, size(x)
00182 res(i) = (shr_sisnan(x(i)) /= 0)
00183 end do
00184 end function c_sisnan_1D
00185
00186 function c_sisnan_2D(x) result(res)
00187 real(r4), intent(in) :: x(:,:)
00188 logical :: res(size(x,1),size(x,2))
00189
00190 integer :: i, j
00191
00192 do j = 1, size(x,2)
00193 do i = 1, size(x,1)
00194 res(i,j) = (shr_sisnan(x(i,j)) /= 0)
00195 end do
00196 end do
00197 end function c_sisnan_2D
00198
00199 function c_sisnan_3D(x) result(res)
00200 real(r4), intent(in) :: x(:,:,:)
00201 logical :: res(size(x,1),size(x,2),size(x,3))
00202
00203 integer :: i, j, k
00204
00205 do k = 1, size(x,3)
00206 do j = 1, size(x,2)
00207 do i = 1, size(x,1)
00208 res(i,j,k) = (shr_sisnan(x(i,j,k)) /= 0)
00209 end do
00210 end do
00211 end do
00212 end function c_sisnan_3D
00213
00214 function c_sisnan_4D(x) result(res)
00215 real(r4), intent(in) :: x(:,:,:,:)
00216 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4))
00217
00218 integer :: i, j, k, m
00219
00220 do m = 1, size(x,4)
00221 do k = 1, size(x,3)
00222 do j = 1, size(x,2)
00223 do i = 1, size(x,1)
00224 res(i,j,k,m) = (shr_sisnan(x(i,j,k,m)) /= 0)
00225 end do
00226 end do
00227 end do
00228 end do
00229 end function c_sisnan_4D
00230
00231 function c_sisnan_5D(x) result(res)
00232 real(r4), intent(in) :: x(:,:,:,:,:)
00233 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5))
00234
00235 integer :: i, j, k, m, n
00236
00237 do n = 1, size(x,5)
00238 do m = 1, size(x,4)
00239 do k = 1, size(x,3)
00240 do j = 1, size(x,2)
00241 do i = 1, size(x,1)
00242 res(i,j,k,m,n) = (shr_sisnan(x(i,j,k,m,n)) /= 0)
00243 end do
00244 end do
00245 end do
00246 end do
00247 end do
00248 end function c_sisnan_5D
00249
00250 function c_sisnan_6D(x) result(res)
00251 real(r4), intent(in) :: x(:,:,:,:,:,:)
00252 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5),size(x,6))
00253
00254 integer :: i, j, k, m, n, o
00255
00256 do o = 1, size(x,6)
00257 do n = 1, size(x,5)
00258 do m = 1, size(x,4)
00259 do k = 1, size(x,3)
00260 do j = 1, size(x,2)
00261 do i = 1, size(x,1)
00262 res(i,j,k,m,n,o) = (shr_sisnan(x(i,j,k,m,n,o)) /= 0)
00263 end do
00264 end do
00265 end do
00266 end do
00267 end do
00268 end do
00269 end function c_sisnan_6D
00270
00271 function c_sisnan_7D(x) result(res)
00272 real(r4), intent(in) :: x(:,:,:,:,:,:,:)
00273 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5),size(x,6),size(x,7))
00274
00275 integer :: i, j, k, m, n, o, p
00276
00277 do p = 1, size(x,7)
00278 do o = 1, size(x,6)
00279 do n = 1, size(x,5)
00280 do m = 1, size(x,4)
00281 do k = 1, size(x,3)
00282 do j = 1, size(x,2)
00283 do i = 1, size(x,1)
00284 res(i,j,k,m,n,o,p) = (shr_sisnan(x(i,j,k,m,n,o,p)) /= 0)
00285 end do
00286 end do
00287 end do
00288 end do
00289 end do
00290 end do
00291 end do
00292 end function c_sisnan_7D
00293
00294 function c_disnan_scalar(x) result(res)
00295 real(r8), intent(in) :: x
00296 logical :: res
00297
00298 res = (shr_disnan(x) /= 0)
00299 end function c_disnan_scalar
00300
00301 function c_disnan_1D(x) result(res)
00302 real(r8), intent(in) :: x(:)
00303 logical :: res(size(x))
00304
00305 integer :: i
00306
00307 do i = 1, size(x)
00308 res(i) = (shr_disnan(x(i)) /= 0)
00309 end do
00310 end function c_disnan_1D
00311
00312 function c_disnan_2D(x) result(res)
00313 real(r8), intent(in) :: x(:,:)
00314 logical :: res(size(x,1),size(x,2))
00315
00316 integer :: i, j
00317
00318 do j = 1, size(x,2)
00319 do i = 1, size(x,1)
00320 res(i,j) = (shr_disnan(x(i,j)) /= 0)
00321 end do
00322 end do
00323 end function c_disnan_2D
00324
00325 function c_disnan_3D(x) result(res)
00326 real(r8), intent(in) :: x(:,:,:)
00327 logical :: res(size(x,1),size(x,2),size(x,3))
00328
00329 integer :: i, j, k
00330
00331 do k = 1, size(x,3)
00332 do j = 1, size(x,2)
00333 do i = 1, size(x,1)
00334 res(i,j,k) = (shr_disnan(x(i,j,k)) /= 0)
00335 end do
00336 end do
00337 end do
00338 end function c_disnan_3D
00339
00340 function c_disnan_4D(x) result(res)
00341 real(r8), intent(in) :: x(:,:,:,:)
00342 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4))
00343
00344 integer :: i, j, k, m
00345
00346 do m = 1, size(x,4)
00347 do k = 1, size(x,3)
00348 do j = 1, size(x,2)
00349 do i = 1, size(x,1)
00350 res(i,j,k,m) = (shr_disnan(x(i,j,k,m)) /= 0)
00351 end do
00352 end do
00353 end do
00354 end do
00355 end function c_disnan_4D
00356
00357 function c_disnan_5D(x) result(res)
00358 real(r8), intent(in) :: x(:,:,:,:,:)
00359 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5))
00360
00361 integer :: i, j, k, m, n
00362
00363 do n = 1, size(x,5)
00364 do m = 1, size(x,4)
00365 do k = 1, size(x,3)
00366 do j = 1, size(x,2)
00367 do i = 1, size(x,1)
00368 res(i,j,k,m,n) = (shr_disnan(x(i,j,k,m,n)) /= 0)
00369 end do
00370 end do
00371 end do
00372 end do
00373 end do
00374 end function c_disnan_5D
00375
00376 function c_disnan_6D(x) result(res)
00377 real(r8), intent(in) :: x(:,:,:,:,:,:)
00378 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5),size(x,6))
00379
00380 integer :: i, j, k, m, n, o
00381
00382 do o = 1, size(x,6)
00383 do n = 1, size(x,5)
00384 do m = 1, size(x,4)
00385 do k = 1, size(x,3)
00386 do j = 1, size(x,2)
00387 do i = 1, size(x,1)
00388 res(i,j,k,m,n,o) = (shr_disnan(x(i,j,k,m,n,o)) /= 0)
00389 end do
00390 end do
00391 end do
00392 end do
00393 end do
00394 end do
00395 end function c_disnan_6D
00396
00397 function c_disnan_7D(x) result(res)
00398 real(r8), intent(in) :: x(:,:,:,:,:,:,:)
00399 logical :: res(size(x,1),size(x,2),size(x,3),size(x,4),size(x,5),size(x,6),size(x,7))
00400
00401 integer :: i, j, k, m, n, o, p
00402
00403 do p = 1, size(x,7)
00404 do o = 1, size(x,6)
00405 do n = 1, size(x,5)
00406 do m = 1, size(x,4)
00407 do k = 1, size(x,3)
00408 do j = 1, size(x,2)
00409 do i = 1, size(x,1)
00410 res(i,j,k,m,n,o,p) = (shr_disnan(x(i,j,k,m,n,o,p)) /= 0)
00411 end do
00412 end do
00413 end do
00414 end do
00415 end do
00416 end do
00417 end do
00418 end function c_disnan_7D
00419
00420 #endif
00421
00422
00423 elemental function sisinf(x) result(res)
00424 real(r4), intent(in) :: x
00425 logical :: res
00426 res = ieor(ibclr(transfer(x,sPosInf),SPSB), sPosInf) == 0
00427 end function
00428
00429
00430 elemental function disinf(d) result(res)
00431 real(r8), intent(in) :: d
00432 logical :: res
00433 res = ieor(ibclr(transfer(d,dPosInf),DPSB), dPosInf) == 0
00434 end function
00435
00436
00437 elemental function sisposinf(x) result(res)
00438 real(r4), intent(in) :: x
00439 logical :: res
00440 res = ieor(transfer(x,sPosInf), sPosInf) == 0
00441 end function
00442
00443
00444 elemental function disposinf(d) result(res)
00445 real(r8), intent(in) :: d
00446 logical :: res
00447 res = ieor(transfer(d,dPosInf), dPosInf) == 0
00448 end function
00449
00450
00451 elemental function sisneginf(x) result(res)
00452 real(r4), intent(in) :: x
00453 logical :: res
00454 res = ieor(transfer(x,sNegInf), sNegInf) == 0
00455 end function
00456
00457
00458 elemental function disneginf(d) result(res)
00459 real(r8), intent(in) :: d
00460 logical :: res
00461 res = ieor(transfer(d,dNegInf), dNegInf) == 0
00462 end function
00463
00464 end module shr_infnan_mod
00465
00466