1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/double-conversion/ieee.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,398 @@ 1.4 +// Copyright 2012 the V8 project authors. All rights reserved. 1.5 +// Redistribution and use in source and binary forms, with or without 1.6 +// modification, are permitted provided that the following conditions are 1.7 +// met: 1.8 +// 1.9 +// * Redistributions of source code must retain the above copyright 1.10 +// notice, this list of conditions and the following disclaimer. 1.11 +// * Redistributions in binary form must reproduce the above 1.12 +// copyright notice, this list of conditions and the following 1.13 +// disclaimer in the documentation and/or other materials provided 1.14 +// with the distribution. 1.15 +// * Neither the name of Google Inc. nor the names of its 1.16 +// contributors may be used to endorse or promote products derived 1.17 +// from this software without specific prior written permission. 1.18 +// 1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + 1.31 +#ifndef DOUBLE_CONVERSION_DOUBLE_H_ 1.32 +#define DOUBLE_CONVERSION_DOUBLE_H_ 1.33 + 1.34 +#include "diy-fp.h" 1.35 + 1.36 +namespace double_conversion { 1.37 + 1.38 +// We assume that doubles and uint64_t have the same endianness. 1.39 +static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } 1.40 +static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); } 1.41 +static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); } 1.42 +static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); } 1.43 + 1.44 +// Helper functions for doubles. 1.45 +class Double { 1.46 + public: 1.47 + static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000); 1.48 + static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000); 1.49 + static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF); 1.50 + static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000); 1.51 + static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. 1.52 + static const int kSignificandSize = 53; 1.53 + 1.54 + Double() : d64_(0) {} 1.55 + explicit Double(double d) : d64_(double_to_uint64(d)) {} 1.56 + explicit Double(uint64_t d64) : d64_(d64) {} 1.57 + explicit Double(DiyFp diy_fp) 1.58 + : d64_(DiyFpToUint64(diy_fp)) {} 1.59 + 1.60 + // The value encoded by this Double must be greater or equal to +0.0. 1.61 + // It must not be special (infinity, or NaN). 1.62 + DiyFp AsDiyFp() const { 1.63 + ASSERT(Sign() > 0); 1.64 + ASSERT(!IsSpecial()); 1.65 + return DiyFp(Significand(), Exponent()); 1.66 + } 1.67 + 1.68 + // The value encoded by this Double must be strictly greater than 0. 1.69 + DiyFp AsNormalizedDiyFp() const { 1.70 + ASSERT(value() > 0.0); 1.71 + uint64_t f = Significand(); 1.72 + int e = Exponent(); 1.73 + 1.74 + // The current double could be a denormal. 1.75 + while ((f & kHiddenBit) == 0) { 1.76 + f <<= 1; 1.77 + e--; 1.78 + } 1.79 + // Do the final shifts in one go. 1.80 + f <<= DiyFp::kSignificandSize - kSignificandSize; 1.81 + e -= DiyFp::kSignificandSize - kSignificandSize; 1.82 + return DiyFp(f, e); 1.83 + } 1.84 + 1.85 + // Returns the double's bit as uint64. 1.86 + uint64_t AsUint64() const { 1.87 + return d64_; 1.88 + } 1.89 + 1.90 + // Returns the next greater double. Returns +infinity on input +infinity. 1.91 + double NextDouble() const { 1.92 + if (d64_ == kInfinity) return Double(kInfinity).value(); 1.93 + if (Sign() < 0 && Significand() == 0) { 1.94 + // -0.0 1.95 + return 0.0; 1.96 + } 1.97 + if (Sign() < 0) { 1.98 + return Double(d64_ - 1).value(); 1.99 + } else { 1.100 + return Double(d64_ + 1).value(); 1.101 + } 1.102 + } 1.103 + 1.104 + double PreviousDouble() const { 1.105 + if (d64_ == (kInfinity | kSignMask)) return -Double::Infinity(); 1.106 + if (Sign() < 0) { 1.107 + return Double(d64_ + 1).value(); 1.108 + } else { 1.109 + if (Significand() == 0) return -0.0; 1.110 + return Double(d64_ - 1).value(); 1.111 + } 1.112 + } 1.113 + 1.114 + int Exponent() const { 1.115 + if (IsDenormal()) return kDenormalExponent; 1.116 + 1.117 + uint64_t d64 = AsUint64(); 1.118 + int biased_e = 1.119 + static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); 1.120 + return biased_e - kExponentBias; 1.121 + } 1.122 + 1.123 + uint64_t Significand() const { 1.124 + uint64_t d64 = AsUint64(); 1.125 + uint64_t significand = d64 & kSignificandMask; 1.126 + if (!IsDenormal()) { 1.127 + return significand + kHiddenBit; 1.128 + } else { 1.129 + return significand; 1.130 + } 1.131 + } 1.132 + 1.133 + // Returns true if the double is a denormal. 1.134 + bool IsDenormal() const { 1.135 + uint64_t d64 = AsUint64(); 1.136 + return (d64 & kExponentMask) == 0; 1.137 + } 1.138 + 1.139 + // We consider denormals not to be special. 1.140 + // Hence only Infinity and NaN are special. 1.141 + bool IsSpecial() const { 1.142 + uint64_t d64 = AsUint64(); 1.143 + return (d64 & kExponentMask) == kExponentMask; 1.144 + } 1.145 + 1.146 + bool IsNan() const { 1.147 + uint64_t d64 = AsUint64(); 1.148 + return ((d64 & kExponentMask) == kExponentMask) && 1.149 + ((d64 & kSignificandMask) != 0); 1.150 + } 1.151 + 1.152 + bool IsInfinite() const { 1.153 + uint64_t d64 = AsUint64(); 1.154 + return ((d64 & kExponentMask) == kExponentMask) && 1.155 + ((d64 & kSignificandMask) == 0); 1.156 + } 1.157 + 1.158 + int Sign() const { 1.159 + uint64_t d64 = AsUint64(); 1.160 + return (d64 & kSignMask) == 0? 1: -1; 1.161 + } 1.162 + 1.163 + // Precondition: the value encoded by this Double must be greater or equal 1.164 + // than +0.0. 1.165 + DiyFp UpperBoundary() const { 1.166 + ASSERT(Sign() > 0); 1.167 + return DiyFp(Significand() * 2 + 1, Exponent() - 1); 1.168 + } 1.169 + 1.170 + // Computes the two boundaries of this. 1.171 + // The bigger boundary (m_plus) is normalized. The lower boundary has the same 1.172 + // exponent as m_plus. 1.173 + // Precondition: the value encoded by this Double must be greater than 0. 1.174 + void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { 1.175 + ASSERT(value() > 0.0); 1.176 + DiyFp v = this->AsDiyFp(); 1.177 + DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); 1.178 + DiyFp m_minus; 1.179 + if (LowerBoundaryIsCloser()) { 1.180 + m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); 1.181 + } else { 1.182 + m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); 1.183 + } 1.184 + m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); 1.185 + m_minus.set_e(m_plus.e()); 1.186 + *out_m_plus = m_plus; 1.187 + *out_m_minus = m_minus; 1.188 + } 1.189 + 1.190 + bool LowerBoundaryIsCloser() const { 1.191 + // The boundary is closer if the significand is of the form f == 2^p-1 then 1.192 + // the lower boundary is closer. 1.193 + // Think of v = 1000e10 and v- = 9999e9. 1.194 + // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but 1.195 + // at a distance of 1e8. 1.196 + // The only exception is for the smallest normal: the largest denormal is 1.197 + // at the same distance as its successor. 1.198 + // Note: denormals have the same exponent as the smallest normals. 1.199 + bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0); 1.200 + return physical_significand_is_zero && (Exponent() != kDenormalExponent); 1.201 + } 1.202 + 1.203 + double value() const { return uint64_to_double(d64_); } 1.204 + 1.205 + // Returns the significand size for a given order of magnitude. 1.206 + // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. 1.207 + // This function returns the number of significant binary digits v will have 1.208 + // once it's encoded into a double. In almost all cases this is equal to 1.209 + // kSignificandSize. The only exceptions are denormals. They start with 1.210 + // leading zeroes and their effective significand-size is hence smaller. 1.211 + static int SignificandSizeForOrderOfMagnitude(int order) { 1.212 + if (order >= (kDenormalExponent + kSignificandSize)) { 1.213 + return kSignificandSize; 1.214 + } 1.215 + if (order <= kDenormalExponent) return 0; 1.216 + return order - kDenormalExponent; 1.217 + } 1.218 + 1.219 + static double Infinity() { 1.220 + return Double(kInfinity).value(); 1.221 + } 1.222 + 1.223 + static double NaN() { 1.224 + return Double(kNaN).value(); 1.225 + } 1.226 + 1.227 + private: 1.228 + static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; 1.229 + static const int kDenormalExponent = -kExponentBias + 1; 1.230 + static const int kMaxExponent = 0x7FF - kExponentBias; 1.231 + static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000); 1.232 + static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000); 1.233 + 1.234 + const uint64_t d64_; 1.235 + 1.236 + static uint64_t DiyFpToUint64(DiyFp diy_fp) { 1.237 + uint64_t significand = diy_fp.f(); 1.238 + int exponent = diy_fp.e(); 1.239 + while (significand > kHiddenBit + kSignificandMask) { 1.240 + significand >>= 1; 1.241 + exponent++; 1.242 + } 1.243 + if (exponent >= kMaxExponent) { 1.244 + return kInfinity; 1.245 + } 1.246 + if (exponent < kDenormalExponent) { 1.247 + return 0; 1.248 + } 1.249 + while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { 1.250 + significand <<= 1; 1.251 + exponent--; 1.252 + } 1.253 + uint64_t biased_exponent; 1.254 + if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { 1.255 + biased_exponent = 0; 1.256 + } else { 1.257 + biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); 1.258 + } 1.259 + return (significand & kSignificandMask) | 1.260 + (biased_exponent << kPhysicalSignificandSize); 1.261 + } 1.262 +}; 1.263 + 1.264 +class Single { 1.265 + public: 1.266 + static const uint32_t kSignMask = 0x80000000; 1.267 + static const uint32_t kExponentMask = 0x7F800000; 1.268 + static const uint32_t kSignificandMask = 0x007FFFFF; 1.269 + static const uint32_t kHiddenBit = 0x00800000; 1.270 + static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit. 1.271 + static const int kSignificandSize = 24; 1.272 + 1.273 + Single() : d32_(0) {} 1.274 + explicit Single(float f) : d32_(float_to_uint32(f)) {} 1.275 + explicit Single(uint32_t d32) : d32_(d32) {} 1.276 + 1.277 + // The value encoded by this Single must be greater or equal to +0.0. 1.278 + // It must not be special (infinity, or NaN). 1.279 + DiyFp AsDiyFp() const { 1.280 + ASSERT(Sign() > 0); 1.281 + ASSERT(!IsSpecial()); 1.282 + return DiyFp(Significand(), Exponent()); 1.283 + } 1.284 + 1.285 + // Returns the single's bit as uint64. 1.286 + uint32_t AsUint32() const { 1.287 + return d32_; 1.288 + } 1.289 + 1.290 + int Exponent() const { 1.291 + if (IsDenormal()) return kDenormalExponent; 1.292 + 1.293 + uint32_t d32 = AsUint32(); 1.294 + int biased_e = 1.295 + static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize); 1.296 + return biased_e - kExponentBias; 1.297 + } 1.298 + 1.299 + uint32_t Significand() const { 1.300 + uint32_t d32 = AsUint32(); 1.301 + uint32_t significand = d32 & kSignificandMask; 1.302 + if (!IsDenormal()) { 1.303 + return significand + kHiddenBit; 1.304 + } else { 1.305 + return significand; 1.306 + } 1.307 + } 1.308 + 1.309 + // Returns true if the single is a denormal. 1.310 + bool IsDenormal() const { 1.311 + uint32_t d32 = AsUint32(); 1.312 + return (d32 & kExponentMask) == 0; 1.313 + } 1.314 + 1.315 + // We consider denormals not to be special. 1.316 + // Hence only Infinity and NaN are special. 1.317 + bool IsSpecial() const { 1.318 + uint32_t d32 = AsUint32(); 1.319 + return (d32 & kExponentMask) == kExponentMask; 1.320 + } 1.321 + 1.322 + bool IsNan() const { 1.323 + uint32_t d32 = AsUint32(); 1.324 + return ((d32 & kExponentMask) == kExponentMask) && 1.325 + ((d32 & kSignificandMask) != 0); 1.326 + } 1.327 + 1.328 + bool IsInfinite() const { 1.329 + uint32_t d32 = AsUint32(); 1.330 + return ((d32 & kExponentMask) == kExponentMask) && 1.331 + ((d32 & kSignificandMask) == 0); 1.332 + } 1.333 + 1.334 + int Sign() const { 1.335 + uint32_t d32 = AsUint32(); 1.336 + return (d32 & kSignMask) == 0? 1: -1; 1.337 + } 1.338 + 1.339 + // Computes the two boundaries of this. 1.340 + // The bigger boundary (m_plus) is normalized. The lower boundary has the same 1.341 + // exponent as m_plus. 1.342 + // Precondition: the value encoded by this Single must be greater than 0. 1.343 + void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { 1.344 + ASSERT(value() > 0.0); 1.345 + DiyFp v = this->AsDiyFp(); 1.346 + DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); 1.347 + DiyFp m_minus; 1.348 + if (LowerBoundaryIsCloser()) { 1.349 + m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); 1.350 + } else { 1.351 + m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); 1.352 + } 1.353 + m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); 1.354 + m_minus.set_e(m_plus.e()); 1.355 + *out_m_plus = m_plus; 1.356 + *out_m_minus = m_minus; 1.357 + } 1.358 + 1.359 + // Precondition: the value encoded by this Single must be greater or equal 1.360 + // than +0.0. 1.361 + DiyFp UpperBoundary() const { 1.362 + ASSERT(Sign() > 0); 1.363 + return DiyFp(Significand() * 2 + 1, Exponent() - 1); 1.364 + } 1.365 + 1.366 + bool LowerBoundaryIsCloser() const { 1.367 + // The boundary is closer if the significand is of the form f == 2^p-1 then 1.368 + // the lower boundary is closer. 1.369 + // Think of v = 1000e10 and v- = 9999e9. 1.370 + // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but 1.371 + // at a distance of 1e8. 1.372 + // The only exception is for the smallest normal: the largest denormal is 1.373 + // at the same distance as its successor. 1.374 + // Note: denormals have the same exponent as the smallest normals. 1.375 + bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0); 1.376 + return physical_significand_is_zero && (Exponent() != kDenormalExponent); 1.377 + } 1.378 + 1.379 + float value() const { return uint32_to_float(d32_); } 1.380 + 1.381 + static float Infinity() { 1.382 + return Single(kInfinity).value(); 1.383 + } 1.384 + 1.385 + static float NaN() { 1.386 + return Single(kNaN).value(); 1.387 + } 1.388 + 1.389 + private: 1.390 + static const int kExponentBias = 0x7F + kPhysicalSignificandSize; 1.391 + static const int kDenormalExponent = -kExponentBias + 1; 1.392 + static const int kMaxExponent = 0xFF - kExponentBias; 1.393 + static const uint32_t kInfinity = 0x7F800000; 1.394 + static const uint32_t kNaN = 0x7FC00000; 1.395 + 1.396 + const uint32_t d32_; 1.397 +}; 1.398 + 1.399 +} // namespace double_conversion 1.400 + 1.401 +#endif // DOUBLE_CONVERSION_DOUBLE_H_