Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_DOUBLE_H_ |
| 6 | #define V8_DOUBLE_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/diy-fp.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
| 13 | // We assume that doubles and uint64_t have the same endianness. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | inline uint64_t double_to_uint64(double d) { return bit_cast<uint64_t>(d); } |
| 15 | inline double uint64_to_double(uint64_t d64) { return bit_cast<double>(d64); } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 16 | |
| 17 | // Helper functions for doubles. |
| 18 | class Double { |
| 19 | public: |
| 20 | static const uint64_t kSignMask = V8_2PART_UINT64_C(0x80000000, 00000000); |
| 21 | static const uint64_t kExponentMask = V8_2PART_UINT64_C(0x7FF00000, 00000000); |
| 22 | static const uint64_t kSignificandMask = |
| 23 | V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); |
| 24 | static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 25 | static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. |
| 26 | static const int kSignificandSize = 53; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 27 | |
| 28 | Double() : d64_(0) {} |
| 29 | explicit Double(double d) : d64_(double_to_uint64(d)) {} |
| 30 | explicit Double(uint64_t d64) : d64_(d64) {} |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 31 | explicit Double(DiyFp diy_fp) |
| 32 | : d64_(DiyFpToUint64(diy_fp)) {} |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 34 | // The value encoded by this Double must be greater or equal to +0.0. |
| 35 | // It must not be special (infinity, or NaN). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | DiyFp AsDiyFp() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | DCHECK(Sign() > 0); |
| 38 | DCHECK(!IsSpecial()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 39 | return DiyFp(Significand(), Exponent()); |
| 40 | } |
| 41 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 42 | // The value encoded by this Double must be strictly greater than 0. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 43 | DiyFp AsNormalizedDiyFp() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | DCHECK(value() > 0.0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 45 | uint64_t f = Significand(); |
| 46 | int e = Exponent(); |
| 47 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 48 | // The current double could be a denormal. |
| 49 | while ((f & kHiddenBit) == 0) { |
| 50 | f <<= 1; |
| 51 | e--; |
| 52 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 53 | // Do the final shifts in one go. |
| 54 | f <<= DiyFp::kSignificandSize - kSignificandSize; |
| 55 | e -= DiyFp::kSignificandSize - kSignificandSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | return DiyFp(f, e); |
| 57 | } |
| 58 | |
| 59 | // Returns the double's bit as uint64. |
| 60 | uint64_t AsUint64() const { |
| 61 | return d64_; |
| 62 | } |
| 63 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 64 | // Returns the next greater double. Returns +infinity on input +infinity. |
| 65 | double NextDouble() const { |
| 66 | if (d64_ == kInfinity) return Double(kInfinity).value(); |
| 67 | if (Sign() < 0 && Significand() == 0) { |
| 68 | // -0.0 |
| 69 | return 0.0; |
| 70 | } |
| 71 | if (Sign() < 0) { |
| 72 | return Double(d64_ - 1).value(); |
| 73 | } else { |
| 74 | return Double(d64_ + 1).value(); |
| 75 | } |
| 76 | } |
| 77 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 78 | int Exponent() const { |
| 79 | if (IsDenormal()) return kDenormalExponent; |
| 80 | |
| 81 | uint64_t d64 = AsUint64(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 82 | int biased_e = |
| 83 | static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 84 | return biased_e - kExponentBias; |
| 85 | } |
| 86 | |
| 87 | uint64_t Significand() const { |
| 88 | uint64_t d64 = AsUint64(); |
| 89 | uint64_t significand = d64 & kSignificandMask; |
| 90 | if (!IsDenormal()) { |
| 91 | return significand + kHiddenBit; |
| 92 | } else { |
| 93 | return significand; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Returns true if the double is a denormal. |
| 98 | bool IsDenormal() const { |
| 99 | uint64_t d64 = AsUint64(); |
| 100 | return (d64 & kExponentMask) == 0; |
| 101 | } |
| 102 | |
| 103 | // We consider denormals not to be special. |
| 104 | // Hence only Infinity and NaN are special. |
| 105 | bool IsSpecial() const { |
| 106 | uint64_t d64 = AsUint64(); |
| 107 | return (d64 & kExponentMask) == kExponentMask; |
| 108 | } |
| 109 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 110 | bool IsInfinite() const { |
| 111 | uint64_t d64 = AsUint64(); |
| 112 | return ((d64 & kExponentMask) == kExponentMask) && |
| 113 | ((d64 & kSignificandMask) == 0); |
| 114 | } |
| 115 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 116 | int Sign() const { |
| 117 | uint64_t d64 = AsUint64(); |
| 118 | return (d64 & kSignMask) == 0? 1: -1; |
| 119 | } |
| 120 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 121 | // Precondition: the value encoded by this Double must be greater or equal |
| 122 | // than +0.0. |
| 123 | DiyFp UpperBoundary() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | DCHECK(Sign() > 0); |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 125 | return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
| 126 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 127 | |
| 128 | // Returns the two boundaries of this. |
| 129 | // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
| 130 | // exponent as m_plus. |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 131 | // Precondition: the value encoded by this Double must be greater than 0. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 132 | void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | DCHECK(value() > 0.0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 134 | DiyFp v = this->AsDiyFp(); |
| 135 | bool significand_is_zero = (v.f() == kHiddenBit); |
| 136 | DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
| 137 | DiyFp m_minus; |
| 138 | if (significand_is_zero && v.e() != kDenormalExponent) { |
| 139 | // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. |
| 140 | // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
| 141 | // at a distance of 1e8. |
| 142 | // The only exception is for the smallest normal: the largest denormal is |
| 143 | // at the same distance as its successor. |
| 144 | // Note: denormals have the same exponent as the smallest normals. |
| 145 | m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
| 146 | } else { |
| 147 | m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
| 148 | } |
| 149 | m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); |
| 150 | m_minus.set_e(m_plus.e()); |
| 151 | *out_m_plus = m_plus; |
| 152 | *out_m_minus = m_minus; |
| 153 | } |
| 154 | |
| 155 | double value() const { return uint64_to_double(d64_); } |
| 156 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 157 | // Returns the significand size for a given order of magnitude. |
| 158 | // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. |
| 159 | // This function returns the number of significant binary digits v will have |
| 160 | // once its encoded into a double. In almost all cases this is equal to |
| 161 | // kSignificandSize. The only exception are denormals. They start with leading |
| 162 | // zeroes and their effective significand-size is hence smaller. |
| 163 | static int SignificandSizeForOrderOfMagnitude(int order) { |
| 164 | if (order >= (kDenormalExponent + kSignificandSize)) { |
| 165 | return kSignificandSize; |
| 166 | } |
| 167 | if (order <= kDenormalExponent) return 0; |
| 168 | return order - kDenormalExponent; |
| 169 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 170 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 171 | private: |
| 172 | static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; |
| 173 | static const int kDenormalExponent = -kExponentBias + 1; |
| 174 | static const int kMaxExponent = 0x7FF - kExponentBias; |
| 175 | static const uint64_t kInfinity = V8_2PART_UINT64_C(0x7FF00000, 00000000); |
| 176 | |
| 177 | const uint64_t d64_; |
| 178 | |
| 179 | static uint64_t DiyFpToUint64(DiyFp diy_fp) { |
| 180 | uint64_t significand = diy_fp.f(); |
| 181 | int exponent = diy_fp.e(); |
| 182 | while (significand > kHiddenBit + kSignificandMask) { |
| 183 | significand >>= 1; |
| 184 | exponent++; |
| 185 | } |
| 186 | if (exponent >= kMaxExponent) { |
| 187 | return kInfinity; |
| 188 | } |
| 189 | if (exponent < kDenormalExponent) { |
| 190 | return 0; |
| 191 | } |
| 192 | while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { |
| 193 | significand <<= 1; |
| 194 | exponent--; |
| 195 | } |
| 196 | uint64_t biased_exponent; |
| 197 | if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { |
| 198 | biased_exponent = 0; |
| 199 | } else { |
| 200 | biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); |
| 201 | } |
| 202 | return (significand & kSignificandMask) | |
| 203 | (biased_exponent << kPhysicalSignificandSize); |
| 204 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 207 | } // namespace internal |
| 208 | } // namespace v8 |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 209 | |
| 210 | #endif // V8_DOUBLE_H_ |