logical function is_nan8(x) result(ret)
  implicit none
  real(8), intent(in) :: x
  integer(8) :: n
  integer :: i

  n = transfer(x, 0_8)

  ! Exponent part
  ret = .true.
  do i = 52, 62
    if( .not. btest(n,i) )then
      ret = .false.
      return
    endif
  enddo

  ! Fraction part
  ret = .false.
  do i = 0, 51
    if( btest(n,i) )then
      ret = .true.
      return
    endif
  enddo
end function is_nan8