michael@0: // Copyright 2012 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #ifndef DOUBLE_CONVERSION_DOUBLE_H_ michael@0: #define DOUBLE_CONVERSION_DOUBLE_H_ michael@0: michael@0: #include "diy-fp.h" michael@0: michael@0: namespace double_conversion { michael@0: michael@0: // We assume that doubles and uint64_t have the same endianness. michael@0: static uint64_t double_to_uint64(double d) { return BitCast(d); } michael@0: static double uint64_to_double(uint64_t d64) { return BitCast(d64); } michael@0: static uint32_t float_to_uint32(float f) { return BitCast(f); } michael@0: static float uint32_to_float(uint32_t d32) { return BitCast(d32); } michael@0: michael@0: // Helper functions for doubles. michael@0: class Double { michael@0: public: michael@0: static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000); michael@0: static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000); michael@0: static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF); michael@0: static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000); michael@0: static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. michael@0: static const int kSignificandSize = 53; michael@0: michael@0: Double() : d64_(0) {} michael@0: explicit Double(double d) : d64_(double_to_uint64(d)) {} michael@0: explicit Double(uint64_t d64) : d64_(d64) {} michael@0: explicit Double(DiyFp diy_fp) michael@0: : d64_(DiyFpToUint64(diy_fp)) {} michael@0: michael@0: // The value encoded by this Double must be greater or equal to +0.0. michael@0: // It must not be special (infinity, or NaN). michael@0: DiyFp AsDiyFp() const { michael@0: ASSERT(Sign() > 0); michael@0: ASSERT(!IsSpecial()); michael@0: return DiyFp(Significand(), Exponent()); michael@0: } michael@0: michael@0: // The value encoded by this Double must be strictly greater than 0. michael@0: DiyFp AsNormalizedDiyFp() const { michael@0: ASSERT(value() > 0.0); michael@0: uint64_t f = Significand(); michael@0: int e = Exponent(); michael@0: michael@0: // The current double could be a denormal. michael@0: while ((f & kHiddenBit) == 0) { michael@0: f <<= 1; michael@0: e--; michael@0: } michael@0: // Do the final shifts in one go. michael@0: f <<= DiyFp::kSignificandSize - kSignificandSize; michael@0: e -= DiyFp::kSignificandSize - kSignificandSize; michael@0: return DiyFp(f, e); michael@0: } michael@0: michael@0: // Returns the double's bit as uint64. michael@0: uint64_t AsUint64() const { michael@0: return d64_; michael@0: } michael@0: michael@0: // Returns the next greater double. Returns +infinity on input +infinity. michael@0: double NextDouble() const { michael@0: if (d64_ == kInfinity) return Double(kInfinity).value(); michael@0: if (Sign() < 0 && Significand() == 0) { michael@0: // -0.0 michael@0: return 0.0; michael@0: } michael@0: if (Sign() < 0) { michael@0: return Double(d64_ - 1).value(); michael@0: } else { michael@0: return Double(d64_ + 1).value(); michael@0: } michael@0: } michael@0: michael@0: double PreviousDouble() const { michael@0: if (d64_ == (kInfinity | kSignMask)) return -Double::Infinity(); michael@0: if (Sign() < 0) { michael@0: return Double(d64_ + 1).value(); michael@0: } else { michael@0: if (Significand() == 0) return -0.0; michael@0: return Double(d64_ - 1).value(); michael@0: } michael@0: } michael@0: michael@0: int Exponent() const { michael@0: if (IsDenormal()) return kDenormalExponent; michael@0: michael@0: uint64_t d64 = AsUint64(); michael@0: int biased_e = michael@0: static_cast((d64 & kExponentMask) >> kPhysicalSignificandSize); michael@0: return biased_e - kExponentBias; michael@0: } michael@0: michael@0: uint64_t Significand() const { michael@0: uint64_t d64 = AsUint64(); michael@0: uint64_t significand = d64 & kSignificandMask; michael@0: if (!IsDenormal()) { michael@0: return significand + kHiddenBit; michael@0: } else { michael@0: return significand; michael@0: } michael@0: } michael@0: michael@0: // Returns true if the double is a denormal. michael@0: bool IsDenormal() const { michael@0: uint64_t d64 = AsUint64(); michael@0: return (d64 & kExponentMask) == 0; michael@0: } michael@0: michael@0: // We consider denormals not to be special. michael@0: // Hence only Infinity and NaN are special. michael@0: bool IsSpecial() const { michael@0: uint64_t d64 = AsUint64(); michael@0: return (d64 & kExponentMask) == kExponentMask; michael@0: } michael@0: michael@0: bool IsNan() const { michael@0: uint64_t d64 = AsUint64(); michael@0: return ((d64 & kExponentMask) == kExponentMask) && michael@0: ((d64 & kSignificandMask) != 0); michael@0: } michael@0: michael@0: bool IsInfinite() const { michael@0: uint64_t d64 = AsUint64(); michael@0: return ((d64 & kExponentMask) == kExponentMask) && michael@0: ((d64 & kSignificandMask) == 0); michael@0: } michael@0: michael@0: int Sign() const { michael@0: uint64_t d64 = AsUint64(); michael@0: return (d64 & kSignMask) == 0? 1: -1; michael@0: } michael@0: michael@0: // Precondition: the value encoded by this Double must be greater or equal michael@0: // than +0.0. michael@0: DiyFp UpperBoundary() const { michael@0: ASSERT(Sign() > 0); michael@0: return DiyFp(Significand() * 2 + 1, Exponent() - 1); michael@0: } michael@0: michael@0: // Computes the two boundaries of this. michael@0: // The bigger boundary (m_plus) is normalized. The lower boundary has the same michael@0: // exponent as m_plus. michael@0: // Precondition: the value encoded by this Double must be greater than 0. michael@0: void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { michael@0: ASSERT(value() > 0.0); michael@0: DiyFp v = this->AsDiyFp(); michael@0: DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); michael@0: DiyFp m_minus; michael@0: if (LowerBoundaryIsCloser()) { michael@0: m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); michael@0: } else { michael@0: m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); michael@0: } michael@0: m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); michael@0: m_minus.set_e(m_plus.e()); michael@0: *out_m_plus = m_plus; michael@0: *out_m_minus = m_minus; michael@0: } michael@0: michael@0: bool LowerBoundaryIsCloser() const { michael@0: // The boundary is closer if the significand is of the form f == 2^p-1 then michael@0: // the lower boundary is closer. michael@0: // Think of v = 1000e10 and v- = 9999e9. michael@0: // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but michael@0: // at a distance of 1e8. michael@0: // The only exception is for the smallest normal: the largest denormal is michael@0: // at the same distance as its successor. michael@0: // Note: denormals have the same exponent as the smallest normals. michael@0: bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0); michael@0: return physical_significand_is_zero && (Exponent() != kDenormalExponent); michael@0: } michael@0: michael@0: double value() const { return uint64_to_double(d64_); } michael@0: michael@0: // Returns the significand size for a given order of magnitude. michael@0: // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. michael@0: // This function returns the number of significant binary digits v will have michael@0: // once it's encoded into a double. In almost all cases this is equal to michael@0: // kSignificandSize. The only exceptions are denormals. They start with michael@0: // leading zeroes and their effective significand-size is hence smaller. michael@0: static int SignificandSizeForOrderOfMagnitude(int order) { michael@0: if (order >= (kDenormalExponent + kSignificandSize)) { michael@0: return kSignificandSize; michael@0: } michael@0: if (order <= kDenormalExponent) return 0; michael@0: return order - kDenormalExponent; michael@0: } michael@0: michael@0: static double Infinity() { michael@0: return Double(kInfinity).value(); michael@0: } michael@0: michael@0: static double NaN() { michael@0: return Double(kNaN).value(); michael@0: } michael@0: michael@0: private: michael@0: static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; michael@0: static const int kDenormalExponent = -kExponentBias + 1; michael@0: static const int kMaxExponent = 0x7FF - kExponentBias; michael@0: static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000); michael@0: static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000); michael@0: michael@0: const uint64_t d64_; michael@0: michael@0: static uint64_t DiyFpToUint64(DiyFp diy_fp) { michael@0: uint64_t significand = diy_fp.f(); michael@0: int exponent = diy_fp.e(); michael@0: while (significand > kHiddenBit + kSignificandMask) { michael@0: significand >>= 1; michael@0: exponent++; michael@0: } michael@0: if (exponent >= kMaxExponent) { michael@0: return kInfinity; michael@0: } michael@0: if (exponent < kDenormalExponent) { michael@0: return 0; michael@0: } michael@0: while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { michael@0: significand <<= 1; michael@0: exponent--; michael@0: } michael@0: uint64_t biased_exponent; michael@0: if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { michael@0: biased_exponent = 0; michael@0: } else { michael@0: biased_exponent = static_cast(exponent + kExponentBias); michael@0: } michael@0: return (significand & kSignificandMask) | michael@0: (biased_exponent << kPhysicalSignificandSize); michael@0: } michael@0: }; michael@0: michael@0: class Single { michael@0: public: michael@0: static const uint32_t kSignMask = 0x80000000; michael@0: static const uint32_t kExponentMask = 0x7F800000; michael@0: static const uint32_t kSignificandMask = 0x007FFFFF; michael@0: static const uint32_t kHiddenBit = 0x00800000; michael@0: static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit. michael@0: static const int kSignificandSize = 24; michael@0: michael@0: Single() : d32_(0) {} michael@0: explicit Single(float f) : d32_(float_to_uint32(f)) {} michael@0: explicit Single(uint32_t d32) : d32_(d32) {} michael@0: michael@0: // The value encoded by this Single must be greater or equal to +0.0. michael@0: // It must not be special (infinity, or NaN). michael@0: DiyFp AsDiyFp() const { michael@0: ASSERT(Sign() > 0); michael@0: ASSERT(!IsSpecial()); michael@0: return DiyFp(Significand(), Exponent()); michael@0: } michael@0: michael@0: // Returns the single's bit as uint64. michael@0: uint32_t AsUint32() const { michael@0: return d32_; michael@0: } michael@0: michael@0: int Exponent() const { michael@0: if (IsDenormal()) return kDenormalExponent; michael@0: michael@0: uint32_t d32 = AsUint32(); michael@0: int biased_e = michael@0: static_cast((d32 & kExponentMask) >> kPhysicalSignificandSize); michael@0: return biased_e - kExponentBias; michael@0: } michael@0: michael@0: uint32_t Significand() const { michael@0: uint32_t d32 = AsUint32(); michael@0: uint32_t significand = d32 & kSignificandMask; michael@0: if (!IsDenormal()) { michael@0: return significand + kHiddenBit; michael@0: } else { michael@0: return significand; michael@0: } michael@0: } michael@0: michael@0: // Returns true if the single is a denormal. michael@0: bool IsDenormal() const { michael@0: uint32_t d32 = AsUint32(); michael@0: return (d32 & kExponentMask) == 0; michael@0: } michael@0: michael@0: // We consider denormals not to be special. michael@0: // Hence only Infinity and NaN are special. michael@0: bool IsSpecial() const { michael@0: uint32_t d32 = AsUint32(); michael@0: return (d32 & kExponentMask) == kExponentMask; michael@0: } michael@0: michael@0: bool IsNan() const { michael@0: uint32_t d32 = AsUint32(); michael@0: return ((d32 & kExponentMask) == kExponentMask) && michael@0: ((d32 & kSignificandMask) != 0); michael@0: } michael@0: michael@0: bool IsInfinite() const { michael@0: uint32_t d32 = AsUint32(); michael@0: return ((d32 & kExponentMask) == kExponentMask) && michael@0: ((d32 & kSignificandMask) == 0); michael@0: } michael@0: michael@0: int Sign() const { michael@0: uint32_t d32 = AsUint32(); michael@0: return (d32 & kSignMask) == 0? 1: -1; michael@0: } michael@0: michael@0: // Computes the two boundaries of this. michael@0: // The bigger boundary (m_plus) is normalized. The lower boundary has the same michael@0: // exponent as m_plus. michael@0: // Precondition: the value encoded by this Single must be greater than 0. michael@0: void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { michael@0: ASSERT(value() > 0.0); michael@0: DiyFp v = this->AsDiyFp(); michael@0: DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); michael@0: DiyFp m_minus; michael@0: if (LowerBoundaryIsCloser()) { michael@0: m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); michael@0: } else { michael@0: m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); michael@0: } michael@0: m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); michael@0: m_minus.set_e(m_plus.e()); michael@0: *out_m_plus = m_plus; michael@0: *out_m_minus = m_minus; michael@0: } michael@0: michael@0: // Precondition: the value encoded by this Single must be greater or equal michael@0: // than +0.0. michael@0: DiyFp UpperBoundary() const { michael@0: ASSERT(Sign() > 0); michael@0: return DiyFp(Significand() * 2 + 1, Exponent() - 1); michael@0: } michael@0: michael@0: bool LowerBoundaryIsCloser() const { michael@0: // The boundary is closer if the significand is of the form f == 2^p-1 then michael@0: // the lower boundary is closer. michael@0: // Think of v = 1000e10 and v- = 9999e9. michael@0: // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but michael@0: // at a distance of 1e8. michael@0: // The only exception is for the smallest normal: the largest denormal is michael@0: // at the same distance as its successor. michael@0: // Note: denormals have the same exponent as the smallest normals. michael@0: bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0); michael@0: return physical_significand_is_zero && (Exponent() != kDenormalExponent); michael@0: } michael@0: michael@0: float value() const { return uint32_to_float(d32_); } michael@0: michael@0: static float Infinity() { michael@0: return Single(kInfinity).value(); michael@0: } michael@0: michael@0: static float NaN() { michael@0: return Single(kNaN).value(); michael@0: } michael@0: michael@0: private: michael@0: static const int kExponentBias = 0x7F + kPhysicalSignificandSize; michael@0: static const int kDenormalExponent = -kExponentBias + 1; michael@0: static const int kMaxExponent = 0xFF - kExponentBias; michael@0: static const uint32_t kInfinity = 0x7F800000; michael@0: static const uint32_t kNaN = 0x7FC00000; michael@0: michael@0: const uint32_t d32_; michael@0: }; michael@0: michael@0: } // namespace double_conversion michael@0: michael@0: #endif // DOUBLE_CONVERSION_DOUBLE_H_