| 35 | } |
| 36 | |
| 37 | uint64_t EncodeDouble(double f) noexcept { |
| 38 | int cls = std::fpclassify(f); |
| 39 | uint64_t sign = 0; |
| 40 | if (copysign(1.0, f) == -1.0) { |
| 41 | f = -f; |
| 42 | sign = 0x8000000000000000; |
| 43 | } |
| 44 | // Zero |
| 45 | if (cls == FP_ZERO) return sign; |
| 46 | // Infinity |
| 47 | if (cls == FP_INFINITE) return sign | 0x7ff0000000000000; |
| 48 | // NaN |
| 49 | if (cls == FP_NAN) return 0x7ff8000000000000; |
| 50 | // Other numbers |
| 51 | int exp; |
| 52 | uint64_t man = std::round(std::frexp(f, &exp) * 9007199254740992.0); |
| 53 | if (exp < -1021) { |
| 54 | // Too small to represent, encode 0 |
| 55 | if (exp < -1084) return sign; |
| 56 | // Subnormal numbers |
| 57 | return sign | (man >> (-1021 - exp)); |
| 58 | } else { |
| 59 | // Too big to represent, encode infinity |
| 60 | if (exp > 1024) return sign | 0x7ff0000000000000; |
| 61 | // Normal numbers |
| 62 | return sign | (((uint64_t)(1022 + exp)) << 52) | (man & 0xFFFFFFFFFFFFF); |
| 63 | } |
| 64 | } |
no outgoing calls