| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/temporal/Int128.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/Casting.h" |
| 11 |
#include "mozilla/FloatingPoint.h" |
| 12 |
#include "mozilla/MathAlgorithms.h" |
| 13 |
|
| 14 |
#include <stdint.h> |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
using namespace js::temporal; |
| 18 |
|
| 19 |
double Uint128::toDouble(const Uint128& x, bool negative) { |
| 20 |
// Simplified version of |BigInt::numberValue()| for DigitBits=64. See the |
| 21 |
// comments in |BigInt::numberValue()| for how this code works. |
| 22 |
|
| 23 |
using Double = mozilla::FloatingPoint<double>; |
| 24 |
constexpr uint8_t ExponentShift = Double::kExponentShift; |
| 25 |
constexpr uint8_t SignificandWidth = Double::kSignificandWidth; |
| 26 |
constexpr unsigned ExponentBias = Double::kExponentBias; |
| 27 |
constexpr uint8_t SignShift = Double::kExponentWidth + SignificandWidth; |
| 28 |
|
| 29 |
constexpr uint64_t MaxIntegralPrecisionDouble = uint64_t(1) |
| 30 |
<< (SignificandWidth + 1); |
| 31 |
|
| 32 |
// We compute the final mantissa of the result, shifted upward to the top of |
| 33 |
// the `uint64_t` space -- plus an extra bit to detect potential rounding. |
| 34 |
constexpr uint8_t BitsNeededForShiftedMantissa = SignificandWidth + 1; |
| 35 |
|
| 36 |
uint64_t shiftedMantissa = 0; |
| 37 |
uint64_t exponent = 0; |
| 38 |
|
| 39 |
// If the extra bit is set, correctly rounding the result may require |
| 40 |
// examining all lower-order bits. Also compute 1) the index of the Digit |
| 41 |
// storing the extra bit, and 2) whether bits beneath the extra bit in that |
| 42 |
// Digit are nonzero so we can round if needed. |
| 43 |
uint64_t bitsBeneathExtraBitInDigitContainingExtraBit = 0; |
| 44 |
|
| 45 |
if (x.high == 0) { |
| 46 |
uint64_t msd = x.low; |
| 47 |
|
| 48 |
// Fast path for the likely-common case of up to a uint64_t of magnitude not |
| 49 |
// exceeding integral precision in IEEE-754. |
| 50 |
if (msd <= MaxIntegralPrecisionDouble) { |
| 51 |
return negative ? -double(msd) : +double(msd); |
| 52 |
} |
| 53 |
|
| 54 |
const uint8_t msdLeadingZeroes = mozilla::CountLeadingZeroes64(msd); |
| 55 |
MOZ_ASSERT(msdLeadingZeroes <= 10, |
| 56 |
"leading zeroes is at most 10 when the fast path isn't taken"); |
| 57 |
|
| 58 |
exponent = 64 - msdLeadingZeroes - 1; |
| 59 |
|
| 60 |
// Omit the most significant bit: the IEEE-754 format includes this bit |
| 61 |
// implicitly for all double-precision integers. |
| 62 |
const uint8_t msdIgnoredBits = msdLeadingZeroes + 1; |
| 63 |
MOZ_ASSERT(1 <= msdIgnoredBits && msdIgnoredBits <= 11); |
| 64 |
|
| 65 |
const uint8_t msdIncludedBits = 64 - msdIgnoredBits; |
| 66 |
MOZ_ASSERT(53 <= msdIncludedBits && msdIncludedBits <= 63); |
| 67 |
MOZ_ASSERT(msdIncludedBits >= BitsNeededForShiftedMantissa); |
| 68 |
|
| 69 |
// Munge the most significant bits of the number into proper |
| 70 |
// position in an IEEE-754 double and go to town. |
| 71 |
|
| 72 |
// Shift `msd`'s contributed bits upward to remove high-order zeroes and the |
| 73 |
// highest set bit (which is implicit in IEEE-754 integral values so must be |
| 74 |
// removed) and to add low-order zeroes. (Lower-order garbage bits are |
| 75 |
// discarded when `shiftedMantissa` is converted to a real mantissa.) |
| 76 |
shiftedMantissa = msd << (64 - msdIncludedBits); |
| 77 |
|
| 78 |
// Add shifted bits to `shiftedMantissa` until we have a complete mantissa |
| 79 |
// and an extra bit. |
| 80 |
const uint8_t countOfBitsInDigitBelowExtraBit = |
| 81 |
64 - BitsNeededForShiftedMantissa - msdIgnoredBits; |
| 82 |
bitsBeneathExtraBitInDigitContainingExtraBit = |
| 83 |
msd & ((uint64_t(1) << countOfBitsInDigitBelowExtraBit) - 1); |
| 84 |
} else { |
| 85 |
uint64_t msd = x.high; |
| 86 |
uint64_t second = x.low; |
| 87 |
|
| 88 |
uint8_t msdLeadingZeroes = mozilla::CountLeadingZeroes64(msd); |
| 89 |
|
| 90 |
exponent = 2 * 64 - msdLeadingZeroes - 1; |
| 91 |
|
| 92 |
// Munge the most significant bits of the number into proper |
| 93 |
// position in an IEEE-754 double and go to town. |
| 94 |
|
| 95 |
// Omit the most significant bit: the IEEE-754 format includes this bit |
| 96 |
// implicitly for all double-precision integers. |
| 97 |
const uint8_t msdIgnoredBits = msdLeadingZeroes + 1; |
| 98 |
const uint8_t msdIncludedBits = 64 - msdIgnoredBits; |
| 99 |
|
| 100 |
// Shift `msd`'s contributed bits upward to remove high-order zeroes and the |
| 101 |
// highest set bit (which is implicit in IEEE-754 integral values so must be |
| 102 |
// removed) and to add low-order zeroes. (Lower-order garbage bits are |
| 103 |
// discarded when `shiftedMantissa` is converted to a real mantissa.) |
| 104 |
shiftedMantissa = msdIncludedBits == 0 ? 0 : msd << (64 - msdIncludedBits); |
| 105 |
|
| 106 |
// Add shifted bits to `shiftedMantissa` until we have a complete mantissa |
| 107 |
// and an extra bit. |
| 108 |
if (msdIncludedBits >= BitsNeededForShiftedMantissa) { |
| 109 |
const uint8_t countOfBitsInDigitBelowExtraBit = |
| 110 |
64 - BitsNeededForShiftedMantissa - msdIgnoredBits; |
| 111 |
bitsBeneathExtraBitInDigitContainingExtraBit = |
| 112 |
msd & ((uint64_t(1) << countOfBitsInDigitBelowExtraBit) - 1); |
| 113 |
|
| 114 |
if (bitsBeneathExtraBitInDigitContainingExtraBit == 0) { |
| 115 |
bitsBeneathExtraBitInDigitContainingExtraBit = second; |
| 116 |
} |
| 117 |
} else { |
| 118 |
shiftedMantissa |= second >> msdIncludedBits; |
| 119 |
|
| 120 |
const uint8_t countOfBitsInSecondDigitBelowExtraBit = |
| 121 |
(msdIncludedBits + 64) - BitsNeededForShiftedMantissa; |
| 122 |
bitsBeneathExtraBitInDigitContainingExtraBit = |
| 123 |
second << (64 - countOfBitsInSecondDigitBelowExtraBit); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
constexpr uint64_t LeastSignificantBit = uint64_t(1) |
| 128 |
<< (64 - SignificandWidth); |
| 129 |
constexpr uint64_t ExtraBit = LeastSignificantBit >> 1; |
| 130 |
|
| 131 |
// The extra bit must be set for rounding to change the mantissa. |
| 132 |
if ((shiftedMantissa & ExtraBit) != 0) { |
| 133 |
bool shouldRoundUp; |
| 134 |
if (shiftedMantissa & LeastSignificantBit) { |
| 135 |
// If the lowest mantissa bit is set, it doesn't matter what lower bits |
| 136 |
// are: nearest-even rounds up regardless. |
| 137 |
shouldRoundUp = true; |
| 138 |
} else { |
| 139 |
// If the lowest mantissa bit is unset, *all* lower bits are relevant. |
| 140 |
// All-zero bits below the extra bit situates `x` halfway between two |
| 141 |
// values, and the nearest *even* value lies downward. But if any bit |
| 142 |
// below the extra bit is set, `x` is closer to the rounded-up value. |
| 143 |
shouldRoundUp = bitsBeneathExtraBitInDigitContainingExtraBit != 0; |
| 144 |
} |
| 145 |
|
| 146 |
if (shouldRoundUp) { |
| 147 |
// Add one to the significand bits. If they overflow, the exponent must |
| 148 |
// also be increased. If *that* overflows, return the correct infinity. |
| 149 |
uint64_t before = shiftedMantissa; |
| 150 |
shiftedMantissa += ExtraBit; |
| 151 |
if (shiftedMantissa < before) { |
| 152 |
exponent++; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
uint64_t significandBits = shiftedMantissa >> (64 - SignificandWidth); |
| 158 |
uint64_t signBit = uint64_t(negative ? 1 : 0) << SignShift; |
| 159 |
uint64_t exponentBits = (exponent + ExponentBias) << ExponentShift; |
| 160 |
return mozilla::BitwiseCast<double>(signBit | exponentBits | significandBits); |
| 161 |
} |