OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000); | 47 static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000); |
48 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. | 48 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. |
49 static const int kSignificandSize = 53; | 49 static const int kSignificandSize = 53; |
50 | 50 |
51 Double() : d64_(0) {} | 51 Double() : d64_(0) {} |
52 explicit Double(double d) : d64_(double_to_uint64(d)) {} | 52 explicit Double(double d) : d64_(double_to_uint64(d)) {} |
53 explicit Double(uint64_t d64) : d64_(d64) {} | 53 explicit Double(uint64_t d64) : d64_(d64) {} |
54 explicit Double(DiyFp diy_fp) | 54 explicit Double(DiyFp diy_fp) |
55 : d64_(DiyFpToUint64(diy_fp)) {} | 55 : d64_(DiyFpToUint64(diy_fp)) {} |
56 | 56 |
| 57 // The value encoded by this Double must be greater or equal to +0.0. |
| 58 // It must not be special (infinity, or NaN). |
57 DiyFp AsDiyFp() const { | 59 DiyFp AsDiyFp() const { |
| 60 ASSERT(Sign() > 0); |
58 ASSERT(!IsSpecial()); | 61 ASSERT(!IsSpecial()); |
59 return DiyFp(Significand(), Exponent()); | 62 return DiyFp(Significand(), Exponent()); |
60 } | 63 } |
61 | 64 |
62 // this->Significand() must not be 0. | 65 // The value encoded by this Double must be strictly greater than 0. |
63 DiyFp AsNormalizedDiyFp() const { | 66 DiyFp AsNormalizedDiyFp() const { |
| 67 ASSERT(value() > 0.0); |
64 uint64_t f = Significand(); | 68 uint64_t f = Significand(); |
65 int e = Exponent(); | 69 int e = Exponent(); |
66 | 70 |
67 ASSERT(f != 0); | |
68 | |
69 // The current double could be a denormal. | 71 // The current double could be a denormal. |
70 while ((f & kHiddenBit) == 0) { | 72 while ((f & kHiddenBit) == 0) { |
71 f <<= 1; | 73 f <<= 1; |
72 e--; | 74 e--; |
73 } | 75 } |
74 // Do the final shifts in one go. | 76 // Do the final shifts in one go. |
75 f <<= DiyFp::kSignificandSize - kSignificandSize; | 77 f <<= DiyFp::kSignificandSize - kSignificandSize; |
76 e -= DiyFp::kSignificandSize - kSignificandSize; | 78 e -= DiyFp::kSignificandSize - kSignificandSize; |
77 return DiyFp(f, e); | 79 return DiyFp(f, e); |
78 } | 80 } |
79 | 81 |
80 // Returns the double's bit as uint64. | 82 // Returns the double's bit as uint64. |
81 uint64_t AsUint64() const { | 83 uint64_t AsUint64() const { |
82 return d64_; | 84 return d64_; |
83 } | 85 } |
84 | 86 |
| 87 // Returns the next greater double. Returns +infinity on input +infinity. |
85 double NextDouble() const { | 88 double NextDouble() const { |
86 if (d64_ == kInfinity) return Double(kInfinity).value(); | 89 if (d64_ == kInfinity) return Double(kInfinity).value(); |
87 if (Sign() < 0 && Significand() == 0) { | 90 if (Sign() < 0 && Significand() == 0) { |
88 // -0.0 | 91 // -0.0 |
89 return 0.0; | 92 return 0.0; |
90 } | 93 } |
91 if (Sign() < 0) { | 94 if (Sign() < 0) { |
92 return Double(d64_ - 1).value(); | 95 return Double(d64_ - 1).value(); |
93 } else { | 96 } else { |
94 return Double(d64_ + 1).value(); | 97 return Double(d64_ + 1).value(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 uint64_t d64 = AsUint64(); | 140 uint64_t d64 = AsUint64(); |
138 return ((d64 & kExponentMask) == kExponentMask) && | 141 return ((d64 & kExponentMask) == kExponentMask) && |
139 ((d64 & kSignificandMask) == 0); | 142 ((d64 & kSignificandMask) == 0); |
140 } | 143 } |
141 | 144 |
142 int Sign() const { | 145 int Sign() const { |
143 uint64_t d64 = AsUint64(); | 146 uint64_t d64 = AsUint64(); |
144 return (d64 & kSignMask) == 0? 1: -1; | 147 return (d64 & kSignMask) == 0? 1: -1; |
145 } | 148 } |
146 | 149 |
| 150 // Precondition: the value encoded by this Double must be greater or equal |
| 151 // than +0.0. |
147 DiyFp UpperBoundary() const { | 152 DiyFp UpperBoundary() const { |
| 153 ASSERT(Sign() > 0); |
148 return DiyFp(Significand() * 2 + 1, Exponent() - 1); | 154 return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
149 } | 155 } |
150 | 156 |
151 // Returns the two boundaries of this. | 157 // Returns the two boundaries of this. |
152 // The bigger boundary (m_plus) is normalized. The lower boundary has the same | 158 // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
153 // exponent as m_plus. | 159 // exponent as m_plus. |
| 160 // Precondition: the value encoded by this Double must be greater than 0. |
154 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { | 161 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
| 162 ASSERT(value() > 0.0); |
155 DiyFp v = this->AsDiyFp(); | 163 DiyFp v = this->AsDiyFp(); |
156 bool significand_is_zero = (v.f() == kHiddenBit); | 164 bool significand_is_zero = (v.f() == kHiddenBit); |
157 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); | 165 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
158 DiyFp m_minus; | 166 DiyFp m_minus; |
159 if (significand_is_zero && v.e() != kDenormalExponent) { | 167 if (significand_is_zero && v.e() != kDenormalExponent) { |
160 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. | 168 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. |
161 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but | 169 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
162 // at a distance of 1e8. | 170 // at a distance of 1e8. |
163 // The only exception is for the smallest normal: the largest denormal is | 171 // The only exception is for the smallest normal: the largest denormal is |
164 // at the same distance as its successor. | 172 // at the same distance as its successor. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); | 229 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); |
222 } | 230 } |
223 return (significand & kSignificandMask) | | 231 return (significand & kSignificandMask) | |
224 (biased_exponent << kPhysicalSignificandSize); | 232 (biased_exponent << kPhysicalSignificandSize); |
225 } | 233 } |
226 }; | 234 }; |
227 | 235 |
228 } } // namespace v8::internal | 236 } } // namespace v8::internal |
229 | 237 |
230 #endif // V8_DOUBLE_H_ | 238 #endif // V8_DOUBLE_H_ |
OLD | NEW |