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