• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef TESTS_IS_QUIET_NAN_H
2 #define TESTS_IS_QUIET_NAN_H
3 
4 namespace flatbuffers {
5 namespace tests {
6 
7 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
8 // The IEEE-754 quiet_NaN is not simple binary constant.
9 // All binary NaN bit strings have all the bits of the biased exponent field E
10 // set to 1. A quiet NaN bit string should be encoded with the first bit d[1]
11 // of the trailing significand field T being 1 (d[0] is implicit bit).
12 // It is assumed that endianness of floating-point is same as integer.
is_quiet_nan_impl(T v)13 template<typename T, typename U, U qnan_base> bool is_quiet_nan_impl(T v) {
14   static_assert(sizeof(T) == sizeof(U), "unexpected");
15   U b = 0;
16   std::memcpy(&b, &v, sizeof(T));
17   return ((b & qnan_base) == qnan_base);
18 }
19 #  if defined(__mips__) || defined(__hppa__)
is_quiet_nan(float v)20 inline bool is_quiet_nan(float v) {
21   return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v) ||
22          is_quiet_nan_impl<float, uint32_t, 0x7FBFFFFFu>(v);
23 }
is_quiet_nan(double v)24 inline bool is_quiet_nan(double v) {
25   return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v) ||
26          is_quiet_nan_impl<double, uint64_t, 0x7FF7FFFFFFFFFFFFu>(v);
27 }
28 #  else
is_quiet_nan(float v)29 inline bool is_quiet_nan(float v) {
30   return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v);
31 }
is_quiet_nan(double v)32 inline bool is_quiet_nan(double v) {
33   return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v);
34 }
35 #  endif
36 #endif
37 
38 }  // namespace tests
39 }  // namespace flatbuffers
40 
41 #endif  // TESTS_IS_QUIET_NAN_H
42