MCPcopy Create free account
hub / github.com/Open-GD/OpenGD / compute_float

Function compute_float

Source/external/fast_float.h:1722–1814  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1720// in such cases.
1721template <typename binary>
1722fastfloat_really_inline FASTFLOAT_CONSTEXPR20
1723adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept {
1724 adjusted_mantissa answer;
1725 if ((w == 0) || (q < binary::smallest_power_of_ten())) {
1726 answer.power2 = 0;
1727 answer.mantissa = 0;
1728 // result should be zero
1729 return answer;
1730 }
1731 if (q > binary::largest_power_of_ten()) {
1732 // we want to get infinity:
1733 answer.power2 = binary::infinite_power();
1734 answer.mantissa = 0;
1735 return answer;
1736 }
1737 // At this point in time q is in [powers::smallest_power_of_five, powers::largest_power_of_five].
1738
1739 // We want the most significant bit of i to be 1. Shift if needed.
1740 int lz = leading_zeroes(w);
1741 w <<= lz;
1742
1743 // The required precision is binary::mantissa_explicit_bits() + 3 because
1744 // 1. We need the implicit bit
1745 // 2. We need an extra bit for rounding purposes
1746 // 3. We might lose a bit due to the "upperbit" routine (result too small, requiring a shift)
1747
1748 value128 product = compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
1749 // The computed 'product' is always sufficient.
1750 // Mathematical proof:
1751 // Noble Mushtak and Daniel Lemire, Fast Number Parsing Without Fallback (to appear)
1752 // See script/mushtak_lemire.py
1753
1754 // The "compute_product_approximation" function can be slightly slower than a branchless approach:
1755 // value128 product = compute_product(q, w);
1756 // but in practice, we can win big with the compute_product_approximation if its additional branch
1757 // is easily predicted. Which is best is data specific.
1758 int upperbit = int(product.high >> 63);
1759
1760 answer.mantissa = product.high >> (upperbit + 64 - binary::mantissa_explicit_bits() - 3);
1761
1762 answer.power2 = int32_t(detail::power(int32_t(q)) + upperbit - lz - binary::minimum_exponent());
1763 if (answer.power2 <= 0) { // we have a subnormal?
1764 // Here have that answer.power2 <= 0 so -answer.power2 >= 0
1765 if(-answer.power2 + 1 >= 64) { // if we have more than 64 bits below the minimum exponent, you have a zero for sure.
1766 answer.power2 = 0;
1767 answer.mantissa = 0;
1768 // result should be zero
1769 return answer;
1770 }
1771 // next line is safe because -answer.power2 + 1 < 64
1772 answer.mantissa >>= -answer.power2 + 1;
1773 // Thankfully, we can't have both "round-to-even" and subnormals because
1774 // "round-to-even" only occurs for powers close to 0.
1775 answer.mantissa += (answer.mantissa & 1); // round up
1776 answer.mantissa >>= 1;
1777 // There is a weird scenario where we don't have a subnormal but just.
1778 // Suppose we start with 2.2250738585072013e-308, we end up
1779 // with 0x3fffffffffffff x 2^-1023-53 which is technically subnormal

Callers

nothing calls this directly

Calls 2

leading_zeroesFunction · 0.85
powerFunction · 0.85

Tested by

no test coverage detected