Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Last active December 11, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/0c340629e451ff31ef66 to your computer and use it in GitHub Desktop.
Save bnoordhuis/0c340629e451ff31ef66 to your computer and use it in GitHub Desktop.
#include <limits>
#include <cstdio>
#include <cstring>
int main()
{
union {
unsigned char b[sizeof(double)];
double d;
float f;
} u;
memset(&u, 0, sizeof(u));
u.f = std::numeric_limits<float>::signaling_NaN(); //__builtin_nansf("");
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
u.f = std::numeric_limits<float>::quiet_NaN(); //__builtin_nanf("");
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
u.d = std::numeric_limits<double>::signaling_NaN(); //__builtin_nans("");
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
u.d = std::numeric_limits<double>::quiet_NaN(); //__builtin_nan("");
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
}
$ for CXX in clang++ g++; do for M in 32 64; do $CXX -m$M -o tmp/nan-$CXX$M tmp/nan.cc; echo "*** $CXX $M bits"; tmp/nan-$CXX$M; done; done
*** clang++ 32 bits
00 00 c0 ff 00 00 00 00
00 00 c0 ff 00 00 00 00
00 00 00 00 00 00 f8 ff
00 00 00 00 00 00 f8 ff
*** clang++ 64 bits
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
*** g++ 32 bits
00 00 e0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 fc 7f
00 00 00 00 00 00 f8 7f
*** g++ 64 bits
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
#include <limits>
#include <cstdio>
#include <cstring>
int main()
{
union {
unsigned char b[sizeof(double)];
double d;
float f;
} u;
memset(&u, 0, sizeof(u));
#if defined(USE_BUILTINS)
u.f = __builtin_nansf("");
#else
u.f = std::numeric_limits<float>::signaling_NaN(); //__builtin_nansf("");
#endif
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
#if defined(USE_BUILTINS)
u.f = __builtin_nanf("");
#else
u.f = std::numeric_limits<float>::quiet_NaN(); //__builtin_nanf("");
#endif
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
#if defined(USE_BUILTINS)
u.d = __builtin_nans("");
#else
u.d = std::numeric_limits<double>::signaling_NaN(); //__builtin_nans("");
#endif
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
#if defined(USE_BUILTINS)
u.d = __builtin_nan("");
#else
u.d = std::numeric_limits<double>::quiet_NaN(); //__builtin_nan("");
#endif
printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
u.b[0], u.b[1], u.b[2], u.b[3],
u.b[4], u.b[5], u.b[6], u.b[7]);
}
$ for CXX in clang++ g++; do for M in 32 64; do for D in '' '-DUSE_BUILTINS'; do $CXX -m$M $D -o tmp/snan-$CXX$M tmp/snan.cc; echo "*** $CXX $M bits $D"; tmp/snan-$CXX$M; done; done; done
*** clang++ 32 bits
00 00 c0 ff 00 00 00 00
00 00 c0 ff 00 00 00 00
00 00 00 00 00 00 f8 ff
00 00 00 00 00 00 f8 ff
*** clang++ 32 bits -DUSE_BUILTINS
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
*** clang++ 64 bits
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
*** clang++ 64 bits -DUSE_BUILTINS
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
*** g++ 32 bits
00 00 e0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 fc 7f
00 00 00 00 00 00 f8 7f
*** g++ 32 bits -DUSE_BUILTINS
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 fc 7f
00 00 00 00 00 00 f8 7f
*** g++ 64 bits
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
*** g++ 64 bits -DUSE_BUILTINS
00 00 a0 7f 00 00 00 00
00 00 c0 7f 00 00 00 00
00 00 00 00 00 00 f4 7f
00 00 00 00 00 00 f8 7f
@bnoordhuis
Copy link
Author

Updated with builtins.

/cc @trevnorris

@bnoordhuis
Copy link
Author

That's with clang's current HEAD and gcc 4.6.3, by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment