49 inline double compute (uint64_t significand, int64_t exponent)
noexcept
51 static constexpr double pow10[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
52 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
54 double value =
static_cast<double> (significand);
56 if (
DCONV_LIKELY ((exponent >= -22) && (exponent <= 22) && (value <= 9007199254740991.0)))
58 return (exponent < 0) ? (value / pow10[-exponent]) : (value * pow10[exponent]);
63 const double scaled = value * pow10[exponent - 22];
64 if (scaled <= 9007199254740991.0)
66 return scaled * pow10[22];
77 return std::numeric_limits<double>::infinity ();
80 const int lz = __builtin_clzll (significand);
86 if ((high & 0x1FF) == 0x1FF)
88 uint64_t secondHigh, secondLow;
91 high += (secondHigh > low);
94 const int upperbit =
static_cast<int> (high >> 63);
95 const int shift = upperbit + 9;
96 uint64_t mantissa = high >> shift;
97 int64_t power2 = (((152170 + 65536) * exponent) >> 16) + 63 + upperbit - lz + 1023;
101 if (-power2 + 1 >= 64)
105 mantissa >>= -power2 + 1;
106 mantissa += (mantissa & 1);
108 power2 = (mantissa < (uint64_t (1) << 52)) ? 0 : 1;
112 if ((low <= 1) && (exponent >= -4) && (exponent <= 23) && ((mantissa & 3) == 1) &&
113 ((mantissa << shift) == high))
115 mantissa &= ~uint64_t (1);
118 mantissa += (mantissa & 1);
123 mantissa = uint64_t (1) << 52;
129 return std::numeric_limits<double>::infinity ();
133 const uint64_t bits = (
static_cast<uint64_t
> (power2) << 52) | (mantissa & ((uint64_t (1) << 52) - 1));
134 memcpy (&value, &bits,
sizeof (
double));