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