OSSIA
Open Scenario System for Interactive Application
safe_math.hpp
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <cmath>
5 
6 namespace ossia
7 {
8 
9 OSSIA_INLINE bool safe_isnan(double val) noexcept
10 {
11 #if __FINITE_MATH_ONLY__
12 #if defined(_MSC_VER)
13  return std::isnan(val);
14 #elif defined(__APPLE__)
15  return __isnand(val);
16 #elif defined(__EMSCRIPTEN__)
17  return __fpclassifyl(val) == FP_NAN;
18 #else
19  // On gcc / clang, with -ffast-math, std::isnan always returns 0
20  // There's __isnan but it's not always available.
21  union
22  {
23  double fp;
24  uint64_t bits;
25  } num{.fp = val};
26 
27  return ((unsigned)(num.bits >> 32) & 0x7fffffff) + ((unsigned)num.bits != 0)
28  > 0x7ff00000;
29 #endif
30 #else
31  return std::isnan(val);
32 #endif
33 }
34 
35 OSSIA_INLINE bool safe_isinf(double val) noexcept
36 {
37 #if __FINITE_MATH_ONLY__
38 #if defined(_MSC_VER)
39  return std::isinf(val);
40 #elif defined(__APPLE__)
41  return __isinfd(val);
42 #elif defined(__EMSCRIPTEN__)
43  return __fpclassifyl(val) == FP_INFINITE;
44 #else
45  // On gcc / clang, with -ffast-math, std::isinf always returns 0
46  // There's __isinf but it's not always available.
47  union
48  {
49  double fp;
50  uint64_t bits;
51  } num{.fp = val};
52 
53  return ((unsigned)(num.bits >> 32) & 0x7fffffff) == 0x7ff00000
54  && (unsigned)num.bits == 0;
55 #endif
56 #else
57  return std::isinf(val);
58 #endif
59 }
60 
61 }
Definition: git_info.h:7