OLD | NEW |
(Empty) | |
| 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 bit_cast<uint64_t>(d); } |
| 38 static double uint64_to_double(uint64_t d64) { return bit_cast<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); |
| 48 |
| 49 Double() : d64_(0.0) {} |
| 50 explicit Double(double d) : d64_(double_to_uint64(d)) {} |
| 51 explicit Double(uint64_t d64) : d64_(d64) {} |
| 52 |
| 53 DiyFp AsDiyFp() const { |
| 54 ASSERT(!IsSpecial()); |
| 55 return DiyFp(Significand(), Exponent()); |
| 56 } |
| 57 |
| 58 DiyFp AsNormalizedDiyFp() const { |
| 59 uint64_t f = Significand(); |
| 60 int e = Exponent(); |
| 61 |
| 62 // The current double could be a denormal. |
| 63 while ((f & kHiddenBit) == 0) { |
| 64 f <<= 1; |
| 65 e--; |
| 66 } |
| 67 // Do the final shifts in one go. Don't forget the hidden bit (the '-1'). |
| 68 f <<= DiyFp::kSignificandSize - kSignificandSize - 1; |
| 69 e -= DiyFp::kSignificandSize - kSignificandSize - 1; |
| 70 return DiyFp(f, e); |
| 71 } |
| 72 |
| 73 // Returns the double's bit as uint64. |
| 74 uint64_t AsUint64() const { |
| 75 return d64_; |
| 76 } |
| 77 |
| 78 int Exponent() const { |
| 79 if (IsDenormal()) return kDenormalExponent; |
| 80 |
| 81 uint64_t d64 = AsUint64(); |
| 82 int biased_e = (d64 & kExponentMask) >> kSignificandSize; |
| 83 return biased_e - kExponentBias; |
| 84 } |
| 85 |
| 86 uint64_t Significand() const { |
| 87 uint64_t d64 = AsUint64(); |
| 88 uint64_t significand = d64 & kSignificandMask; |
| 89 if (!IsDenormal()) { |
| 90 return significand + kHiddenBit; |
| 91 } else { |
| 92 return significand; |
| 93 } |
| 94 } |
| 95 |
| 96 // Returns true if the double is a denormal. |
| 97 bool IsDenormal() const { |
| 98 uint64_t d64 = AsUint64(); |
| 99 return (d64 & kExponentMask) == 0; |
| 100 } |
| 101 |
| 102 // We consider denormals not to be special. |
| 103 // Hence only Infinity and NaN are special. |
| 104 bool IsSpecial() const { |
| 105 uint64_t d64 = AsUint64(); |
| 106 return (d64 & kExponentMask) == kExponentMask; |
| 107 } |
| 108 |
| 109 bool IsNan() const { |
| 110 uint64_t d64 = AsUint64(); |
| 111 return ((d64 & kExponentMask) == kExponentMask) && |
| 112 ((d64 & kSignificandMask) != 0); |
| 113 } |
| 114 |
| 115 |
| 116 bool IsInfinite() const { |
| 117 uint64_t d64 = AsUint64(); |
| 118 return ((d64 & kExponentMask) == kExponentMask) && |
| 119 ((d64 & kSignificandMask) == 0); |
| 120 } |
| 121 |
| 122 |
| 123 int Sign() const { |
| 124 uint64_t d64 = AsUint64(); |
| 125 return (d64 & kSignMask) == 0? 1: -1; |
| 126 } |
| 127 |
| 128 |
| 129 // Returns the two boundaries of this. |
| 130 // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
| 131 // exponent as m_plus. |
| 132 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
| 133 DiyFp v = this->AsDiyFp(); |
| 134 bool significand_is_zero = (v.f() == kHiddenBit); |
| 135 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
| 136 DiyFp m_minus; |
| 137 if (significand_is_zero && v.e() != kDenormalExponent) { |
| 138 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. |
| 139 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
| 140 // at a distance of 1e8. |
| 141 // The only exception is for the smallest normal: the largest denormal is |
| 142 // at the same distance as its successor. |
| 143 // Note: denormals have the same exponent as the smallest normals. |
| 144 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
| 145 } else { |
| 146 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
| 147 } |
| 148 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); |
| 149 m_minus.set_e(m_plus.e()); |
| 150 *out_m_plus = m_plus; |
| 151 *out_m_minus = m_minus; |
| 152 } |
| 153 |
| 154 double value() const { return uint64_to_double(d64_); } |
| 155 |
| 156 private: |
| 157 static const int kSignificandSize = 52; // Excludes the hidden bit. |
| 158 static const int kExponentBias = 0x3FF + kSignificandSize; |
| 159 static const int kDenormalExponent = -kExponentBias + 1; |
| 160 |
| 161 uint64_t d64_; |
| 162 }; |
| 163 |
| 164 } } // namespace v8::internal |
| 165 |
| 166 #endif // V8_DOUBLE_H_ |
OLD | NEW |