15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Copyright 2010 the V8 project authors. All rights reserved.
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Redistribution and use in source and binary forms, with or without
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// modification, are permitted provided that the following conditions are
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// met:
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Redistributions of source code must retain the above copyright
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       notice, this list of conditions and the following disclaimer.
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Redistributions in binary form must reproduce the above
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       copyright notice, this list of conditions and the following
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       disclaimer in the documentation and/or other materials provided
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       contributors may be used to endorse or promote products derived
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       from this software without specific prior written permission.
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef DOUBLE_CONVERSION_DOUBLE_H_
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define DOUBLE_CONVERSION_DOUBLE_H_
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "diy-fp.h"
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WTF {
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace double_conversion {
3602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // We assume that doubles and uint64_t have the same endianness.
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
4002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // Helper functions for doubles.
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    class Double {
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    public:
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const int kPhysicalSignificandSize = 52;  // Excludes the hidden bit.
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const int kSignificandSize = 53;
5002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        Double() : d64_(0) {}
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        explicit Double(double d) : d64_(double_to_uint64(d)) {}
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        explicit Double(uint64_t d64) : d64_(d64) {}
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        explicit Double(DiyFp diy_fp)
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        : d64_(DiyFpToUint64(diy_fp)) {}
5602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // The value encoded by this Double must be greater or equal to +0.0.
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // It must not be special (infinity, or NaN).
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        DiyFp AsDiyFp() const {
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ASSERT(Sign() > 0);
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ASSERT(!IsSpecial());
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return DiyFp(Significand(), Exponent());
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
6402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // The value encoded by this Double must be strictly greater than 0.
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        DiyFp AsNormalizedDiyFp() const {
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ASSERT(value() > 0.0);
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t f = Significand();
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            int e = Exponent();
7002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            // The current double could be a denormal.
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            while ((f & kHiddenBit) == 0) {
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                f <<= 1;
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                e--;
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            // Do the final shifts in one go.
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            f <<= DiyFp::kSignificandSize - kSignificandSize;
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            e -= DiyFp::kSignificandSize - kSignificandSize;
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return DiyFp(f, e);
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
8102772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Returns the double's bit as uint64.
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        uint64_t AsUint64() const {
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return d64_;
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
8602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Returns the next greater double. Returns +infinity on input +infinity.
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        double NextDouble() const {
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (d64_ == kInfinity) return Double(kInfinity).value();
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (Sign() < 0 && Significand() == 0) {
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // -0.0
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return 0.0;
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (Sign() < 0) {
955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return Double(d64_ - 1).value();
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            } else {
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return Double(d64_ + 1).value();
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
10002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        int Exponent() const {
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (IsDenormal()) return kDenormalExponent;
10302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            int biased_e =
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return biased_e - kExponentBias;
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
10902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        uint64_t Significand() const {
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t significand = d64 & kSignificandMask;
1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (!IsDenormal()) {
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return significand + kHiddenBit;
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            } else {
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return significand;
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
11902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Returns true if the double is a denormal.
1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        bool IsDenormal() const {
1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return (d64 & kExponentMask) == 0;
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
12502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // We consider denormals not to be special.
1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Hence only Infinity and NaN are special.
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        bool IsSpecial() const {
1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return (d64 & kExponentMask) == kExponentMask;
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
13202772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        bool IsNan() const {
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return ((d64 & kExponentMask) == kExponentMask) &&
1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ((d64 & kSignificandMask) != 0);
1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
13802772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        bool IsInfinite() const {
1405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return ((d64 & kExponentMask) == kExponentMask) &&
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ((d64 & kSignificandMask) == 0);
1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
14402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        int Sign() const {
1465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t d64 = AsUint64();
1475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return (d64 & kSignMask) == 0? 1: -1;
1485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
14902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Precondition: the value encoded by this Double must be greater or equal
1515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // than +0.0.
1525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        DiyFp UpperBoundary() const {
1535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ASSERT(Sign() > 0);
1545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return DiyFp(Significand() * 2 + 1, Exponent() - 1);
1555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
15602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Computes the two boundaries of this.
1585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // The bigger boundary (m_plus) is normalized. The lower boundary has the same
1595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // exponent as m_plus.
1605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Precondition: the value encoded by this Double must be greater than 0.
1615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
1625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            ASSERT(value() > 0.0);
1635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            DiyFp v = this->AsDiyFp();
1645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            bool significand_is_zero = (v.f() == kHiddenBit);
1655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
1665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            DiyFp m_minus;
1675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (significand_is_zero && v.e() != kDenormalExponent) {
1685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
1695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
1705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // at a distance of 1e8.
1715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // The only exception is for the smallest normal: the largest denormal is
1725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // at the same distance as its successor.
1735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                // Note: denormals have the same exponent as the smallest normals.
1745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
1755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            } else {
1765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
1775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
1785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
1795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            m_minus.set_e(m_plus.e());
1805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            *out_m_plus = m_plus;
1815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            *out_m_minus = m_minus;
1825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
18302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        double value() const { return uint64_to_double(d64_); }
18502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
1865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Returns the significand size for a given order of magnitude.
1875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
1885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // This function returns the number of significant binary digits v will have
1895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // once it's encoded into a double. In almost all cases this is equal to
1905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // kSignificandSize. The only exceptions are denormals. They start with
1915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // leading zeroes and their effective significand-size is hence smaller.
1925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static int SignificandSizeForOrderOfMagnitude(int order) {
1935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (order >= (kDenormalExponent + kSignificandSize)) {
1945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return kSignificandSize;
1955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
1965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (order <= kDenormalExponent) return 0;
1975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return order - kDenormalExponent;
1985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
19902772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static double Infinity() {
2015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return Double(kInfinity).value();
2025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
20302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static double NaN() {
2055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return Double(kNaN).value();
2065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
20702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    private:
2095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
2105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const int kDenormalExponent = -kExponentBias + 1;
2115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const int kMaxExponent = 0x7FF - kExponentBias;
2125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
2135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
21402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        const uint64_t d64_;
21602772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        static uint64_t DiyFpToUint64(DiyFp diy_fp) {
2185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t significand = diy_fp.f();
2195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            int exponent = diy_fp.e();
2205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            while (significand > kHiddenBit + kSignificandMask) {
2215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                significand >>= 1;
2225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                exponent++;
2235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
2245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (exponent >= kMaxExponent) {
2255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return kInfinity;
2265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
2275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (exponent < kDenormalExponent) {
2285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                return 0;
2295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
2305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
2315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                significand <<= 1;
2325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                exponent--;
2335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
2345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            uint64_t biased_exponent;
2355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
2365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                biased_exponent = 0;
2375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            } else {
2385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
2395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            }
2405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            return (significand & kSignificandMask) |
2415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            (biased_exponent << kPhysicalSignificandSize);
2425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
2435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    };
24402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
2455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}  // namespace double_conversion
2465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace WTF
2485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif  // DOUBLE_CONVERSION_DOUBLE_H_
250