12

Is there any device functions in CUDA to obtain IEEE 754 special values like inf, NaN? I mean the stable way, not by some maths ops that could be optimized-out by compilers.

I only manage to find a device function called nan() that must take some unknown string argument.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user0002128
  • 2,785
  • 2
  • 23
  • 40

2 Answers2

18

How about CUDART_NAN (double) and CUDART_NAN_F (float) defined in /usr/local/cuda/include/math_constants.h :

#define CUDART_NAN_F            __int_as_float(0x7fffffff)
#define CUDART_NAN              __longlong_as_double(0xfff8000000000000ULL)

and:

#define CUDART_INF_F            __int_as_float(0x7f800000)
#define CUDART_INF              __longlong_as_double(0x7ff0000000000000ULL)
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • 3
    The nan() function you found in CUDA is the nan() function as specified by the ISO C99 standard. I believe the same function is now part of C++11. Note that the double-precision version of this standard math function will be a lot slower than using the solution Robert pointed out, because it needs to parse a string argument to construct the numerical value of the NaN. The single-precision version, nanf(), is very fast since NVIDIA GPUs only support one canonical single-precision NaN, so that is what what the function returns. – njuffa Mar 20 '13 at 07:41
1

CUDA now has the nanf instruction in the maths library.

device float nanf( const char *tagp )

Returns "NaN"
Mikael
  • 11
  • 1