whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 |
| 4 | // met: |
| 5 | // |
| 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. |
| 15 | // |
| 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. |
| 27 | |
| 28 | #ifndef V8_DOUBLE_H_ |
| 29 | #define V8_DOUBLE_H_ |
| 30 | |
| 31 | #include "diy-fp.h" |
| 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | // We assume that doubles and uint64_t have the same endianness. |
| 37 | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } |
| 38 | static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); } |
| 39 | |
| 40 | // Helper functions for doubles. |
| 41 | class Double { |
| 42 | public: |
| 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); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 48 | static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. |
| 49 | static const int kSignificandSize = 53; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 50 | |
| 51 | Double() : d64_(0) {} |
| 52 | explicit Double(double d) : d64_(double_to_uint64(d)) {} |
| 53 | explicit Double(uint64_t d64) : d64_(d64) {} |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 54 | explicit Double(DiyFp diy_fp) |
| 55 | : d64_(DiyFpToUint64(diy_fp)) {} |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 56 | |
| 57 | DiyFp AsDiyFp() const { |
| 58 | ASSERT(!IsSpecial()); |
| 59 | return DiyFp(Significand(), Exponent()); |
| 60 | } |
| 61 | |
| 62 | // this->Significand() must not be 0. |
| 63 | DiyFp AsNormalizedDiyFp() const { |
| 64 | uint64_t f = Significand(); |
| 65 | int e = Exponent(); |
| 66 | |
| 67 | ASSERT(f != 0); |
| 68 | |
| 69 | // The current double could be a denormal. |
| 70 | while ((f & kHiddenBit) == 0) { |
| 71 | f <<= 1; |
| 72 | e--; |
| 73 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 74 | // Do the final shifts in one go. |
| 75 | f <<= DiyFp::kSignificandSize - kSignificandSize; |
| 76 | e -= DiyFp::kSignificandSize - kSignificandSize; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 77 | return DiyFp(f, e); |
| 78 | } |
| 79 | |
| 80 | // Returns the double's bit as uint64. |
| 81 | uint64_t AsUint64() const { |
| 82 | return d64_; |
| 83 | } |
| 84 | |
| 85 | int Exponent() const { |
| 86 | if (IsDenormal()) return kDenormalExponent; |
| 87 | |
| 88 | uint64_t d64 = AsUint64(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 89 | int biased_e = |
| 90 | static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 91 | return biased_e - kExponentBias; |
| 92 | } |
| 93 | |
| 94 | uint64_t Significand() const { |
| 95 | uint64_t d64 = AsUint64(); |
| 96 | uint64_t significand = d64 & kSignificandMask; |
| 97 | if (!IsDenormal()) { |
| 98 | return significand + kHiddenBit; |
| 99 | } else { |
| 100 | return significand; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Returns true if the double is a denormal. |
| 105 | bool IsDenormal() const { |
| 106 | uint64_t d64 = AsUint64(); |
| 107 | return (d64 & kExponentMask) == 0; |
| 108 | } |
| 109 | |
| 110 | // We consider denormals not to be special. |
| 111 | // Hence only Infinity and NaN are special. |
| 112 | bool IsSpecial() const { |
| 113 | uint64_t d64 = AsUint64(); |
| 114 | return (d64 & kExponentMask) == kExponentMask; |
| 115 | } |
| 116 | |
| 117 | bool IsNan() const { |
| 118 | uint64_t d64 = AsUint64(); |
| 119 | return ((d64 & kExponentMask) == kExponentMask) && |
| 120 | ((d64 & kSignificandMask) != 0); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | bool IsInfinite() const { |
| 125 | uint64_t d64 = AsUint64(); |
| 126 | return ((d64 & kExponentMask) == kExponentMask) && |
| 127 | ((d64 & kSignificandMask) == 0); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | int Sign() const { |
| 132 | uint64_t d64 = AsUint64(); |
| 133 | return (d64 & kSignMask) == 0? 1: -1; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | // Returns the two boundaries of this. |
| 138 | // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
| 139 | // exponent as m_plus. |
| 140 | void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
| 141 | DiyFp v = this->AsDiyFp(); |
| 142 | bool significand_is_zero = (v.f() == kHiddenBit); |
| 143 | DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
| 144 | DiyFp m_minus; |
| 145 | if (significand_is_zero && v.e() != kDenormalExponent) { |
| 146 | // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. |
| 147 | // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
| 148 | // at a distance of 1e8. |
| 149 | // The only exception is for the smallest normal: the largest denormal is |
| 150 | // at the same distance as its successor. |
| 151 | // Note: denormals have the same exponent as the smallest normals. |
| 152 | m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
| 153 | } else { |
| 154 | m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
| 155 | } |
| 156 | m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); |
| 157 | m_minus.set_e(m_plus.e()); |
| 158 | *out_m_plus = m_plus; |
| 159 | *out_m_minus = m_minus; |
| 160 | } |
| 161 | |
| 162 | double value() const { return uint64_to_double(d64_); } |
| 163 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 164 | // Returns the significand size for a given order of magnitude. |
| 165 | // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. |
| 166 | // This function returns the number of significant binary digits v will have |
| 167 | // once its encoded into a double. In almost all cases this is equal to |
| 168 | // kSignificandSize. The only exception are denormals. They start with leading |
| 169 | // zeroes and their effective significand-size is hence smaller. |
| 170 | static int SignificandSizeForOrderOfMagnitude(int order) { |
| 171 | if (order >= (kDenormalExponent + kSignificandSize)) { |
| 172 | return kSignificandSize; |
| 173 | } |
| 174 | if (order <= kDenormalExponent) return 0; |
| 175 | return order - kDenormalExponent; |
| 176 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 177 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 178 | private: |
| 179 | static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; |
| 180 | static const int kDenormalExponent = -kExponentBias + 1; |
| 181 | static const int kMaxExponent = 0x7FF - kExponentBias; |
| 182 | static const uint64_t kInfinity = V8_2PART_UINT64_C(0x7FF00000, 00000000); |
| 183 | |
| 184 | const uint64_t d64_; |
| 185 | |
| 186 | static uint64_t DiyFpToUint64(DiyFp diy_fp) { |
| 187 | uint64_t significand = diy_fp.f(); |
| 188 | int exponent = diy_fp.e(); |
| 189 | while (significand > kHiddenBit + kSignificandMask) { |
| 190 | significand >>= 1; |
| 191 | exponent++; |
| 192 | } |
| 193 | if (exponent >= kMaxExponent) { |
| 194 | return kInfinity; |
| 195 | } |
| 196 | if (exponent < kDenormalExponent) { |
| 197 | return 0; |
| 198 | } |
| 199 | while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { |
| 200 | significand <<= 1; |
| 201 | exponent--; |
| 202 | } |
| 203 | uint64_t biased_exponent; |
| 204 | if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { |
| 205 | biased_exponent = 0; |
| 206 | } else { |
| 207 | biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); |
| 208 | } |
| 209 | return (significand & kSignificandMask) | |
| 210 | (biased_exponent << kPhysicalSignificandSize); |
| 211 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } } // namespace v8::internal |
| 215 | |
| 216 | #endif // V8_DOUBLE_H_ |