MCPcopy Create free account
hub / github.com/ElementsProject/elements / EncodeDouble

Function EncodeDouble

src/util/serfloat.cpp:37–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35}
36
37uint64_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}

Callers 4

SerMethod · 0.85
TestDoubleFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
FUZZ_TARGETFunction · 0.85

Calls

no outgoing calls

Tested by 3

TestDoubleFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68
FUZZ_TARGETFunction · 0.68