Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(837)

Side by Side Diff: runtime/third_party/double-conversion/src/double.h

Issue 8632010: double-conversion drop. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated to latest double-conversion version. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 DOUBLE_CONVERSION_DOUBLE_H_
29 #define DOUBLE_CONVERSION_DOUBLE_H_
30
31 #include "diy-fp.h"
32
33 namespace double_conversion {
34
35 // We assume that doubles and uint64_t have the same endianness.
36 static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
37 static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
38
39 // Helper functions for doubles.
40 class Double {
41 public:
42 static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
43 static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
44 static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
45 static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
46 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
47 static const int kSignificandSize = 53;
48
49 Double() : d64_(0) {}
50 explicit Double(double d) : d64_(double_to_uint64(d)) {}
51 explicit Double(uint64_t d64) : d64_(d64) {}
52 explicit Double(DiyFp diy_fp)
53 : d64_(DiyFpToUint64(diy_fp)) {}
54
55 // The value encoded by this Double must be greater or equal to +0.0.
56 // It must not be special (infinity, or NaN).
57 DiyFp AsDiyFp() const {
58 ASSERT(Sign() > 0);
59 ASSERT(!IsSpecial());
60 return DiyFp(Significand(), Exponent());
61 }
62
63 // The value encoded by this Double must be strictly greater than 0.
64 DiyFp AsNormalizedDiyFp() const {
65 ASSERT(value() > 0.0);
66 uint64_t f = Significand();
67 int e = Exponent();
68
69 // The current double could be a denormal.
70 while ((f & kHiddenBit) == 0) {
71 f <<= 1;
72 e--;
73 }
74 // Do the final shifts in one go.
75 f <<= DiyFp::kSignificandSize - kSignificandSize;
76 e -= DiyFp::kSignificandSize - kSignificandSize;
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 // Returns the next greater double. Returns +infinity on input +infinity.
86 double NextDouble() const {
87 if (d64_ == kInfinity) return Double(kInfinity).value();
88 if (Sign() < 0 && Significand() == 0) {
89 // -0.0
90 return 0.0;
91 }
92 if (Sign() < 0) {
93 return Double(d64_ - 1).value();
94 } else {
95 return Double(d64_ + 1).value();
96 }
97 }
98
99 int Exponent() const {
100 if (IsDenormal()) return kDenormalExponent;
101
102 uint64_t d64 = AsUint64();
103 int biased_e =
104 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
105 return biased_e - kExponentBias;
106 }
107
108 uint64_t Significand() const {
109 uint64_t d64 = AsUint64();
110 uint64_t significand = d64 & kSignificandMask;
111 if (!IsDenormal()) {
112 return significand + kHiddenBit;
113 } else {
114 return significand;
115 }
116 }
117
118 // Returns true if the double is a denormal.
119 bool IsDenormal() const {
120 uint64_t d64 = AsUint64();
121 return (d64 & kExponentMask) == 0;
122 }
123
124 // We consider denormals not to be special.
125 // Hence only Infinity and NaN are special.
126 bool IsSpecial() const {
127 uint64_t d64 = AsUint64();
128 return (d64 & kExponentMask) == kExponentMask;
129 }
130
131 bool IsNan() const {
132 uint64_t d64 = AsUint64();
133 return ((d64 & kExponentMask) == kExponentMask) &&
134 ((d64 & kSignificandMask) != 0);
135 }
136
137 bool IsInfinite() const {
138 uint64_t d64 = AsUint64();
139 return ((d64 & kExponentMask) == kExponentMask) &&
140 ((d64 & kSignificandMask) == 0);
141 }
142
143 int Sign() const {
144 uint64_t d64 = AsUint64();
145 return (d64 & kSignMask) == 0? 1: -1;
146 }
147
148 // Precondition: the value encoded by this Double must be greater or equal
149 // than +0.0.
150 DiyFp UpperBoundary() const {
151 ASSERT(Sign() > 0);
152 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
153 }
154
155 // Computes the two boundaries of this.
156 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
157 // exponent as m_plus.
158 // Precondition: the value encoded by this Double must be greater than 0.
159 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
160 ASSERT(value() > 0.0);
161 DiyFp v = this->AsDiyFp();
162 bool significand_is_zero = (v.f() == kHiddenBit);
163 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
164 DiyFp m_minus;
165 if (significand_is_zero && v.e() != kDenormalExponent) {
166 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
167 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
168 // at a distance of 1e8.
169 // The only exception is for the smallest normal: the largest denormal is
170 // at the same distance as its successor.
171 // Note: denormals have the same exponent as the smallest normals.
172 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
173 } else {
174 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
175 }
176 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
177 m_minus.set_e(m_plus.e());
178 *out_m_plus = m_plus;
179 *out_m_minus = m_minus;
180 }
181
182 double value() const { return uint64_to_double(d64_); }
183
184 // Returns the significand size for a given order of magnitude.
185 // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
186 // This function returns the number of significant binary digits v will have
187 // once it's encoded into a double. In almost all cases this is equal to
188 // kSignificandSize. The only exceptions are denormals. They start with
189 // leading zeroes and their effective significand-size is hence smaller.
190 static int SignificandSizeForOrderOfMagnitude(int order) {
191 if (order >= (kDenormalExponent + kSignificandSize)) {
192 return kSignificandSize;
193 }
194 if (order <= kDenormalExponent) return 0;
195 return order - kDenormalExponent;
196 }
197
198 static double Infinity() {
199 return Double(kInfinity).value();
200 }
201
202 static double NaN() {
203 return Double(kNaN).value();
204 }
205
206 private:
207 static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
208 static const int kDenormalExponent = -kExponentBias + 1;
209 static const int kMaxExponent = 0x7FF - kExponentBias;
210 static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
211 static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
212
213 const uint64_t d64_;
214
215 static uint64_t DiyFpToUint64(DiyFp diy_fp) {
216 uint64_t significand = diy_fp.f();
217 int exponent = diy_fp.e();
218 while (significand > kHiddenBit + kSignificandMask) {
219 significand >>= 1;
220 exponent++;
221 }
222 if (exponent >= kMaxExponent) {
223 return kInfinity;
224 }
225 if (exponent < kDenormalExponent) {
226 return 0;
227 }
228 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
229 significand <<= 1;
230 exponent--;
231 }
232 uint64_t biased_exponent;
233 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
234 biased_exponent = 0;
235 } else {
236 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
237 }
238 return (significand & kSignificandMask) |
239 (biased_exponent << kPhysicalSignificandSize);
240 }
241 };
242
243 } // namespace double_conversion
244
245 #endif // DOUBLE_CONVERSION_DOUBLE_H_
OLDNEW
« no previous file with comments | « runtime/third_party/double-conversion/src/diy-fp.cc ('k') | runtime/third_party/double-conversion/src/double-conversion.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698