Line data Source code
1 : // Copyright 2012 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 48 : static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
37 16 : static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
38 0 : static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
39 0 : static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
40 :
41 : // Helper functions for doubles.
42 : class Double {
43 : public:
44 : static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
45 : static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
46 : static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
47 : static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
48 : static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
49 : static const int kSignificandSize = 53;
50 :
51 : Double() : d64_(0) {}
52 48 : explicit Double(double d) : d64_(double_to_uint64(d)) {}
53 0 : explicit Double(uint64_t d64) : d64_(d64) {}
54 0 : explicit Double(DiyFp diy_fp)
55 0 : : d64_(DiyFpToUint64(diy_fp)) {}
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).
59 8 : DiyFp AsDiyFp() const {
60 8 : ASSERT(Sign() > 0);
61 8 : ASSERT(!IsSpecial());
62 8 : return DiyFp(Significand(), Exponent());
63 : }
64 :
65 : // The value encoded by this Double must be strictly greater than 0.
66 8 : DiyFp AsNormalizedDiyFp() const {
67 8 : ASSERT(value() > 0.0);
68 8 : uint64_t f = Significand();
69 8 : int e = Exponent();
70 :
71 : // The current double could be a denormal.
72 8 : while ((f & kHiddenBit) == 0) {
73 0 : f <<= 1;
74 0 : e--;
75 : }
76 : // Do the final shifts in one go.
77 8 : f <<= DiyFp::kSignificandSize - kSignificandSize;
78 8 : e -= DiyFp::kSignificandSize - kSignificandSize;
79 8 : return DiyFp(f, e);
80 : }
81 :
82 : // Returns the double's bit as uint64.
83 120 : uint64_t AsUint64() const {
84 120 : return d64_;
85 : }
86 :
87 : // Returns the next greater double. Returns +infinity on input +infinity.
88 0 : double NextDouble() const {
89 0 : if (d64_ == kInfinity) return Double(kInfinity).value();
90 0 : if (Sign() < 0 && Significand() == 0) {
91 : // -0.0
92 0 : return 0.0;
93 : }
94 0 : if (Sign() < 0) {
95 0 : return Double(d64_ - 1).value();
96 : } else {
97 0 : return Double(d64_ + 1).value();
98 : }
99 : }
100 :
101 0 : double PreviousDouble() const {
102 0 : if (d64_ == (kInfinity | kSignMask)) return -Infinity();
103 0 : if (Sign() < 0) {
104 0 : return Double(d64_ + 1).value();
105 : } else {
106 0 : if (Significand() == 0) return -0.0;
107 0 : return Double(d64_ - 1).value();
108 : }
109 : }
110 :
111 16 : int Exponent() const {
112 16 : if (IsDenormal()) return kDenormalExponent;
113 :
114 16 : uint64_t d64 = AsUint64();
115 : int biased_e =
116 16 : static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
117 16 : return biased_e - kExponentBias;
118 : }
119 :
120 16 : uint64_t Significand() const {
121 16 : uint64_t d64 = AsUint64();
122 16 : uint64_t significand = d64 & kSignificandMask;
123 16 : if (!IsDenormal()) {
124 16 : return significand + kHiddenBit;
125 : } else {
126 0 : return significand;
127 : }
128 : }
129 :
130 : // Returns true if the double is a denormal.
131 32 : bool IsDenormal() const {
132 32 : uint64_t d64 = AsUint64();
133 32 : return (d64 & kExponentMask) == 0;
134 : }
135 :
136 : // We consider denormals not to be special.
137 : // Hence only Infinity and NaN are special.
138 32 : bool IsSpecial() const {
139 32 : uint64_t d64 = AsUint64();
140 32 : return (d64 & kExponentMask) == kExponentMask;
141 : }
142 :
143 0 : bool IsNan() const {
144 0 : uint64_t d64 = AsUint64();
145 0 : return ((d64 & kExponentMask) == kExponentMask) &&
146 0 : ((d64 & kSignificandMask) != 0);
147 : }
148 :
149 0 : bool IsInfinite() const {
150 0 : uint64_t d64 = AsUint64();
151 0 : return ((d64 & kExponentMask) == kExponentMask) &&
152 0 : ((d64 & kSignificandMask) == 0);
153 : }
154 :
155 16 : int Sign() const {
156 16 : uint64_t d64 = AsUint64();
157 16 : return (d64 & kSignMask) == 0? 1: -1;
158 : }
159 :
160 : // Precondition: the value encoded by this Double must be greater or equal
161 : // than +0.0.
162 0 : DiyFp UpperBoundary() const {
163 0 : ASSERT(Sign() > 0);
164 0 : return DiyFp(Significand() * 2 + 1, Exponent() - 1);
165 : }
166 :
167 : // Computes the two boundaries of this.
168 : // The bigger boundary (m_plus) is normalized. The lower boundary has the same
169 : // exponent as m_plus.
170 : // Precondition: the value encoded by this Double must be greater than 0.
171 8 : void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
172 8 : ASSERT(value() > 0.0);
173 8 : DiyFp v = this->AsDiyFp();
174 8 : DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
175 8 : DiyFp m_minus;
176 8 : if (LowerBoundaryIsCloser()) {
177 0 : m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
178 : } else {
179 8 : m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
180 : }
181 8 : m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
182 8 : m_minus.set_e(m_plus.e());
183 8 : *out_m_plus = m_plus;
184 8 : *out_m_minus = m_minus;
185 8 : }
186 :
187 8 : bool LowerBoundaryIsCloser() const {
188 : // The boundary is closer if the significand is of the form f == 2^p-1 then
189 : // the lower boundary is closer.
190 : // Think of v = 1000e10 and v- = 9999e9.
191 : // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
192 : // at a distance of 1e8.
193 : // The only exception is for the smallest normal: the largest denormal is
194 : // at the same distance as its successor.
195 : // Note: denormals have the same exponent as the smallest normals.
196 8 : bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
197 8 : return physical_significand_is_zero && (Exponent() != kDenormalExponent);
198 : }
199 :
200 16 : double value() const { return uint64_to_double(d64_); }
201 :
202 : // Returns the significand size for a given order of magnitude.
203 : // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
204 : // This function returns the number of significant binary digits v will have
205 : // once it's encoded into a double. In almost all cases this is equal to
206 : // kSignificandSize. The only exceptions are denormals. They start with
207 : // leading zeroes and their effective significand-size is hence smaller.
208 0 : static int SignificandSizeForOrderOfMagnitude(int order) {
209 0 : if (order >= (kDenormalExponent + kSignificandSize)) {
210 0 : return kSignificandSize;
211 : }
212 0 : if (order <= kDenormalExponent) return 0;
213 0 : return order - kDenormalExponent;
214 : }
215 :
216 0 : static double Infinity() {
217 0 : return Double(kInfinity).value();
218 : }
219 :
220 0 : static double NaN() {
221 0 : return Double(kNaN).value();
222 : }
223 :
224 : private:
225 : static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
226 : static const int kDenormalExponent = -kExponentBias + 1;
227 : static const int kMaxExponent = 0x7FF - kExponentBias;
228 : static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
229 : static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
230 :
231 : const uint64_t d64_;
232 :
233 0 : static uint64_t DiyFpToUint64(DiyFp diy_fp) {
234 0 : uint64_t significand = diy_fp.f();
235 0 : int exponent = diy_fp.e();
236 0 : while (significand > kHiddenBit + kSignificandMask) {
237 0 : significand >>= 1;
238 0 : exponent++;
239 : }
240 0 : if (exponent >= kMaxExponent) {
241 0 : return kInfinity;
242 : }
243 0 : if (exponent < kDenormalExponent) {
244 0 : return 0;
245 : }
246 0 : while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
247 0 : significand <<= 1;
248 0 : exponent--;
249 : }
250 : uint64_t biased_exponent;
251 0 : if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
252 0 : biased_exponent = 0;
253 : } else {
254 0 : biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
255 : }
256 0 : return (significand & kSignificandMask) |
257 0 : (biased_exponent << kPhysicalSignificandSize);
258 : }
259 :
260 : DISALLOW_COPY_AND_ASSIGN(Double);
261 : };
262 :
263 : class Single {
264 : public:
265 : static const uint32_t kSignMask = 0x80000000;
266 : static const uint32_t kExponentMask = 0x7F800000;
267 : static const uint32_t kSignificandMask = 0x007FFFFF;
268 : static const uint32_t kHiddenBit = 0x00800000;
269 : static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
270 : static const int kSignificandSize = 24;
271 :
272 : Single() : d32_(0) {}
273 0 : explicit Single(float f) : d32_(float_to_uint32(f)) {}
274 : explicit Single(uint32_t d32) : d32_(d32) {}
275 :
276 : // The value encoded by this Single must be greater or equal to +0.0.
277 : // It must not be special (infinity, or NaN).
278 0 : DiyFp AsDiyFp() const {
279 0 : ASSERT(Sign() > 0);
280 0 : ASSERT(!IsSpecial());
281 0 : return DiyFp(Significand(), Exponent());
282 : }
283 :
284 : // Returns the single's bit as uint64.
285 0 : uint32_t AsUint32() const {
286 0 : return d32_;
287 : }
288 :
289 0 : int Exponent() const {
290 0 : if (IsDenormal()) return kDenormalExponent;
291 :
292 0 : uint32_t d32 = AsUint32();
293 : int biased_e =
294 0 : static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
295 0 : return biased_e - kExponentBias;
296 : }
297 :
298 0 : uint32_t Significand() const {
299 0 : uint32_t d32 = AsUint32();
300 0 : uint32_t significand = d32 & kSignificandMask;
301 0 : if (!IsDenormal()) {
302 0 : return significand + kHiddenBit;
303 : } else {
304 0 : return significand;
305 : }
306 : }
307 :
308 : // Returns true if the single is a denormal.
309 0 : bool IsDenormal() const {
310 0 : uint32_t d32 = AsUint32();
311 0 : return (d32 & kExponentMask) == 0;
312 : }
313 :
314 : // We consider denormals not to be special.
315 : // Hence only Infinity and NaN are special.
316 0 : bool IsSpecial() const {
317 0 : uint32_t d32 = AsUint32();
318 0 : return (d32 & kExponentMask) == kExponentMask;
319 : }
320 :
321 : bool IsNan() const {
322 : uint32_t d32 = AsUint32();
323 : return ((d32 & kExponentMask) == kExponentMask) &&
324 : ((d32 & kSignificandMask) != 0);
325 : }
326 :
327 : bool IsInfinite() const {
328 : uint32_t d32 = AsUint32();
329 : return ((d32 & kExponentMask) == kExponentMask) &&
330 : ((d32 & kSignificandMask) == 0);
331 : }
332 :
333 0 : int Sign() const {
334 0 : uint32_t d32 = AsUint32();
335 0 : return (d32 & kSignMask) == 0? 1: -1;
336 : }
337 :
338 : // Computes the two boundaries of this.
339 : // The bigger boundary (m_plus) is normalized. The lower boundary has the same
340 : // exponent as m_plus.
341 : // Precondition: the value encoded by this Single must be greater than 0.
342 0 : void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
343 0 : ASSERT(value() > 0.0);
344 0 : DiyFp v = this->AsDiyFp();
345 0 : DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
346 0 : DiyFp m_minus;
347 0 : if (LowerBoundaryIsCloser()) {
348 0 : m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
349 : } else {
350 0 : m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
351 : }
352 0 : m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
353 0 : m_minus.set_e(m_plus.e());
354 0 : *out_m_plus = m_plus;
355 0 : *out_m_minus = m_minus;
356 0 : }
357 :
358 : // Precondition: the value encoded by this Single must be greater or equal
359 : // than +0.0.
360 0 : DiyFp UpperBoundary() const {
361 0 : ASSERT(Sign() > 0);
362 0 : return DiyFp(Significand() * 2 + 1, Exponent() - 1);
363 : }
364 :
365 0 : bool LowerBoundaryIsCloser() const {
366 : // The boundary is closer if the significand is of the form f == 2^p-1 then
367 : // the lower boundary is closer.
368 : // Think of v = 1000e10 and v- = 9999e9.
369 : // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
370 : // at a distance of 1e8.
371 : // The only exception is for the smallest normal: the largest denormal is
372 : // at the same distance as its successor.
373 : // Note: denormals have the same exponent as the smallest normals.
374 0 : bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
375 0 : return physical_significand_is_zero && (Exponent() != kDenormalExponent);
376 : }
377 :
378 0 : float value() const { return uint32_to_float(d32_); }
379 :
380 : static float Infinity() {
381 : return Single(kInfinity).value();
382 : }
383 :
384 : static float NaN() {
385 : return Single(kNaN).value();
386 : }
387 :
388 : private:
389 : static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
390 : static const int kDenormalExponent = -kExponentBias + 1;
391 : static const int kMaxExponent = 0xFF - kExponentBias;
392 : static const uint32_t kInfinity = 0x7F800000;
393 : static const uint32_t kNaN = 0x7FC00000;
394 :
395 : const uint32_t d32_;
396 :
397 : DISALLOW_COPY_AND_ASSIGN(Single);
398 : };
399 :
400 : } // namespace double_conversion
401 :
402 : #endif // DOUBLE_CONVERSION_DOUBLE_H_
|