File: is_quiet_nan.h

package info (click to toggle)
golang-github-google-flatbuffers 24.12.23-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,704 kB
  • sloc: cpp: 53,217; python: 6,900; cs: 5,566; java: 4,370; php: 1,460; javascript: 1,061; xml: 1,016; sh: 886; makefile: 13
file content (41 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef TESTS_IS_QUIET_NAN_H
#define TESTS_IS_QUIET_NAN_H

namespace flatbuffers {
namespace tests {

#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
// The IEEE-754 quiet_NaN is not simple binary constant.
// All binary NaN bit strings have all the bits of the biased exponent field E
// set to 1. A quiet NaN bit string should be encoded with the first bit d[1]
// of the trailing significand field T being 1 (d[0] is implicit bit).
// It is assumed that endianness of floating-point is same as integer.
template<typename T, typename U, U qnan_base> bool is_quiet_nan_impl(T v) {
  static_assert(sizeof(T) == sizeof(U), "unexpected");
  U b = 0;
  std::memcpy(&b, &v, sizeof(T));
  return ((b & qnan_base) == qnan_base);
}
#  if defined(__mips__) || defined(__hppa__)
inline bool is_quiet_nan(float v) {
  return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v) ||
         is_quiet_nan_impl<float, uint32_t, 0x7FBFFFFFu>(v);
}
inline bool is_quiet_nan(double v) {
  return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v) ||
         is_quiet_nan_impl<double, uint64_t, 0x7FF7FFFFFFFFFFFFu>(v);
}
#  else
inline bool is_quiet_nan(float v) {
  return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v);
}
inline bool is_quiet_nan(double v) {
  return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v);
}
#  endif
#endif

}  // namespace tests
}  // namespace flatbuffers

#endif  // TESTS_IS_QUIET_NAN_H