| 29 | using namespace tvm::ffi; |
| 30 | |
| 31 | inline bool FastMathSafeIsNaN(double x) { |
| 32 | #ifdef __FAST_MATH__ |
| 33 | // Bit-level NaN detection (IEEE 754 double) |
| 34 | // IEEE 754 standard: https://en.wikipedia.org/wiki/IEEE_754 |
| 35 | // NaN is encoded as all 1s in the exponent and non-zero in the mantissa |
| 36 | static_assert(sizeof(double) == sizeof(uint64_t), "Unexpected double size"); |
| 37 | uint64_t bits = *reinterpret_cast<const uint64_t*>(&x); |
| 38 | uint64_t exponent = (bits >> 52) & 0x7FF; |
| 39 | uint64_t mantissa = bits & 0xFFFFFFFFFFFFFull; |
| 40 | return (exponent == 0x7FF) && (mantissa != 0); |
| 41 | #else |
| 42 | // Safe to use std::isnan when fast-math is off |
| 43 | return std::isnan(x); |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | inline bool FastMathSafeIsInf(double x) { |
| 48 | #ifdef __FAST_MATH__ |
no outgoing calls
no test coverage detected