1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // We assume that doubles and uint64_t have the same endianness.
37 inline uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
38 inline double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
40 // Helper functions for doubles.
43 static const uint64_t kSignMask = V8_2PART_UINT64_C(0x80000000, 00000000);
44 static const uint64_t kExponentMask = V8_2PART_UINT64_C(0x7FF00000, 00000000);
45 static const uint64_t kSignificandMask =
46 V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
47 static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000);
48 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
49 static const int kSignificandSize = 53;
52 explicit Double(double d) : d64_(double_to_uint64(d)) {}
53 explicit Double(uint64_t d64) : d64_(d64) {}
54 explicit Double(DiyFp diy_fp)
55 : d64_(DiyFpToUint64(diy_fp)) {}
57 // The value encoded by this Double must be greater or equal to +0.0.
58 // It must not be special (infinity, or NaN).
59 DiyFp AsDiyFp() const {
62 return DiyFp(Significand(), Exponent());
65 // The value encoded by this Double must be strictly greater than 0.
66 DiyFp AsNormalizedDiyFp() const {
67 ASSERT(value() > 0.0);
68 uint64_t f = Significand();
71 // The current double could be a denormal.
72 while ((f & kHiddenBit) == 0) {
76 // Do the final shifts in one go.
77 f <<= DiyFp::kSignificandSize - kSignificandSize;
78 e -= DiyFp::kSignificandSize - kSignificandSize;
82 // Returns the double's bit as uint64.
83 uint64_t AsUint64() const {
87 // Returns the next greater double. Returns +infinity on input +infinity.
88 double NextDouble() const {
89 if (d64_ == kInfinity) return Double(kInfinity).value();
90 if (Sign() < 0 && Significand() == 0) {
95 return Double(d64_ - 1).value();
97 return Double(d64_ + 1).value();
101 int Exponent() const {
102 if (IsDenormal()) return kDenormalExponent;
104 uint64_t d64 = AsUint64();
106 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
107 return biased_e - kExponentBias;
110 uint64_t Significand() const {
111 uint64_t d64 = AsUint64();
112 uint64_t significand = d64 & kSignificandMask;
114 return significand + kHiddenBit;
120 // Returns true if the double is a denormal.
121 bool IsDenormal() const {
122 uint64_t d64 = AsUint64();
123 return (d64 & kExponentMask) == 0;
126 // We consider denormals not to be special.
127 // Hence only Infinity and NaN are special.
128 bool IsSpecial() const {
129 uint64_t d64 = AsUint64();
130 return (d64 & kExponentMask) == kExponentMask;
133 bool IsInfinite() const {
134 uint64_t d64 = AsUint64();
135 return ((d64 & kExponentMask) == kExponentMask) &&
136 ((d64 & kSignificandMask) == 0);
140 uint64_t d64 = AsUint64();
141 return (d64 & kSignMask) == 0? 1: -1;
144 // Precondition: the value encoded by this Double must be greater or equal
146 DiyFp UpperBoundary() const {
148 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
151 // Returns the two boundaries of this.
152 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
153 // exponent as m_plus.
154 // Precondition: the value encoded by this Double must be greater than 0.
155 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
156 ASSERT(value() > 0.0);
157 DiyFp v = this->AsDiyFp();
158 bool significand_is_zero = (v.f() == kHiddenBit);
159 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
161 if (significand_is_zero && v.e() != kDenormalExponent) {
162 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
163 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
164 // at a distance of 1e8.
165 // The only exception is for the smallest normal: the largest denormal is
166 // at the same distance as its successor.
167 // Note: denormals have the same exponent as the smallest normals.
168 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
170 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
172 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
173 m_minus.set_e(m_plus.e());
174 *out_m_plus = m_plus;
175 *out_m_minus = m_minus;
178 double value() const { return uint64_to_double(d64_); }
180 // Returns the significand size for a given order of magnitude.
181 // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
182 // This function returns the number of significant binary digits v will have
183 // once its encoded into a double. In almost all cases this is equal to
184 // kSignificandSize. The only exception are denormals. They start with leading
185 // zeroes and their effective significand-size is hence smaller.
186 static int SignificandSizeForOrderOfMagnitude(int order) {
187 if (order >= (kDenormalExponent + kSignificandSize)) {
188 return kSignificandSize;
190 if (order <= kDenormalExponent) return 0;
191 return order - kDenormalExponent;
195 static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
196 static const int kDenormalExponent = -kExponentBias + 1;
197 static const int kMaxExponent = 0x7FF - kExponentBias;
198 static const uint64_t kInfinity = V8_2PART_UINT64_C(0x7FF00000, 00000000);
202 static uint64_t DiyFpToUint64(DiyFp diy_fp) {
203 uint64_t significand = diy_fp.f();
204 int exponent = diy_fp.e();
205 while (significand > kHiddenBit + kSignificandMask) {
209 if (exponent >= kMaxExponent) {
212 if (exponent < kDenormalExponent) {
215 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
219 uint64_t biased_exponent;
220 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
223 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
225 return (significand & kSignificandMask) |
226 (biased_exponent << kPhysicalSignificandSize);
230 } } // namespace v8::internal
232 #endif // V8_DOUBLE_H_