double-conversion-ieee.h (15734B)
1 // © 2018 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 // 4 // From the double-conversion library. Original license: 5 // 6 // Copyright 2012 the V8 project authors. All rights reserved. 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions are 9 // met: 10 // 11 // * Redistributions of source code must retain the above copyright 12 // notice, this list of conditions and the following disclaimer. 13 // * Redistributions in binary form must reproduce the above 14 // copyright notice, this list of conditions and the following 15 // disclaimer in the documentation and/or other materials provided 16 // with the distribution. 17 // * Neither the name of Google Inc. nor the names of its 18 // contributors may be used to endorse or promote products derived 19 // from this software without specific prior written permission. 20 // 21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING 34 #include "unicode/utypes.h" 35 #if !UCONFIG_NO_FORMATTING 36 37 #ifndef DOUBLE_CONVERSION_DOUBLE_H_ 38 #define DOUBLE_CONVERSION_DOUBLE_H_ 39 40 // ICU PATCH: Customize header file paths for ICU. 41 42 #include "double-conversion-diy-fp.h" 43 44 // ICU PATCH: Wrap in ICU namespace 45 U_NAMESPACE_BEGIN 46 47 namespace double_conversion { 48 49 // We assume that doubles and uint64_t have the same endianness. 50 static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } 51 static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); } 52 static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); } 53 static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); } 54 55 // Helper functions for doubles. 56 class Double { 57 public: 58 static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000); 59 static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000); 60 static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF); 61 static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000); 62 static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000); 63 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. 64 static const int kSignificandSize = 53; 65 static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; 66 static const int kMaxExponent = 0x7FF - kExponentBias; 67 68 Double() : d64_(0) {} 69 explicit Double(double d) : d64_(double_to_uint64(d)) {} 70 explicit Double(uint64_t d64) : d64_(d64) {} 71 explicit Double(DiyFp diy_fp) 72 : d64_(DiyFpToUint64(diy_fp)) {} 73 74 // The value encoded by this Double must be greater or equal to +0.0. 75 // It must not be special (infinity, or NaN). 76 DiyFp AsDiyFp() const { 77 DOUBLE_CONVERSION_ASSERT(Sign() > 0); 78 DOUBLE_CONVERSION_ASSERT(!IsSpecial()); 79 return DiyFp(Significand(), Exponent()); 80 } 81 82 // The value encoded by this Double must be strictly greater than 0. 83 DiyFp AsNormalizedDiyFp() const { 84 DOUBLE_CONVERSION_ASSERT(value() > 0.0); 85 uint64_t f = Significand(); 86 int e = Exponent(); 87 88 // The current double could be a denormal. 89 while ((f & kHiddenBit) == 0) { 90 f <<= 1; 91 e--; 92 } 93 // Do the final shifts in one go. 94 f <<= DiyFp::kSignificandSize - kSignificandSize; 95 e -= DiyFp::kSignificandSize - kSignificandSize; 96 return DiyFp(f, e); 97 } 98 99 // Returns the double's bit as uint64. 100 uint64_t AsUint64() const { 101 return d64_; 102 } 103 104 // Returns the next greater double. Returns +infinity on input +infinity. 105 double NextDouble() const { 106 if (d64_ == kInfinity) return Double(kInfinity).value(); 107 if (Sign() < 0 && Significand() == 0) { 108 // -0.0 109 return 0.0; 110 } 111 if (Sign() < 0) { 112 return Double(d64_ - 1).value(); 113 } else { 114 return Double(d64_ + 1).value(); 115 } 116 } 117 118 double PreviousDouble() const { 119 if (d64_ == (kInfinity | kSignMask)) return -Infinity(); 120 if (Sign() < 0) { 121 return Double(d64_ + 1).value(); 122 } else { 123 if (Significand() == 0) return -0.0; 124 return Double(d64_ - 1).value(); 125 } 126 } 127 128 int Exponent() const { 129 if (IsDenormal()) return kDenormalExponent; 130 131 uint64_t d64 = AsUint64(); 132 int biased_e = 133 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); 134 return biased_e - kExponentBias; 135 } 136 137 uint64_t Significand() const { 138 uint64_t d64 = AsUint64(); 139 uint64_t significand = d64 & kSignificandMask; 140 if (!IsDenormal()) { 141 return significand + kHiddenBit; 142 } else { 143 return significand; 144 } 145 } 146 147 // Returns true if the double is a denormal. 148 bool IsDenormal() const { 149 uint64_t d64 = AsUint64(); 150 return (d64 & kExponentMask) == 0; 151 } 152 153 // We consider denormals not to be special. 154 // Hence only Infinity and NaN are special. 155 bool IsSpecial() const { 156 uint64_t d64 = AsUint64(); 157 return (d64 & kExponentMask) == kExponentMask; 158 } 159 160 bool IsNan() const { 161 uint64_t d64 = AsUint64(); 162 return ((d64 & kExponentMask) == kExponentMask) && 163 ((d64 & kSignificandMask) != 0); 164 } 165 166 bool IsQuietNan() const { 167 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 168 return IsNan() && ((AsUint64() & kQuietNanBit) == 0); 169 #else 170 return IsNan() && ((AsUint64() & kQuietNanBit) != 0); 171 #endif 172 } 173 174 bool IsSignalingNan() const { 175 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 176 return IsNan() && ((AsUint64() & kQuietNanBit) != 0); 177 #else 178 return IsNan() && ((AsUint64() & kQuietNanBit) == 0); 179 #endif 180 } 181 182 183 bool IsInfinite() const { 184 uint64_t d64 = AsUint64(); 185 return ((d64 & kExponentMask) == kExponentMask) && 186 ((d64 & kSignificandMask) == 0); 187 } 188 189 int Sign() const { 190 uint64_t d64 = AsUint64(); 191 return (d64 & kSignMask) == 0? 1: -1; 192 } 193 194 // Precondition: the value encoded by this Double must be greater or equal 195 // than +0.0. 196 DiyFp UpperBoundary() const { 197 DOUBLE_CONVERSION_ASSERT(Sign() > 0); 198 return DiyFp(Significand() * 2 + 1, Exponent() - 1); 199 } 200 201 // Computes the two boundaries of this. 202 // The bigger boundary (m_plus) is normalized. The lower boundary has the same 203 // exponent as m_plus. 204 // Precondition: the value encoded by this Double must be greater than 0. 205 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { 206 DOUBLE_CONVERSION_ASSERT(value() > 0.0); 207 DiyFp v = this->AsDiyFp(); 208 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); 209 DiyFp m_minus; 210 if (LowerBoundaryIsCloser()) { 211 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); 212 } else { 213 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); 214 } 215 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); 216 m_minus.set_e(m_plus.e()); 217 *out_m_plus = m_plus; 218 *out_m_minus = m_minus; 219 } 220 221 bool LowerBoundaryIsCloser() const { 222 // The boundary is closer if the significand is of the form f == 2^p-1 then 223 // the lower boundary is closer. 224 // Think of v = 1000e10 and v- = 9999e9. 225 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but 226 // at a distance of 1e8. 227 // The only exception is for the smallest normal: the largest denormal is 228 // at the same distance as its successor. 229 // Note: denormals have the same exponent as the smallest normals. 230 bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0); 231 return physical_significand_is_zero && (Exponent() != kDenormalExponent); 232 } 233 234 double value() const { return uint64_to_double(d64_); } 235 236 // Returns the significand size for a given order of magnitude. 237 // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. 238 // This function returns the number of significant binary digits v will have 239 // once it's encoded into a double. In almost all cases this is equal to 240 // kSignificandSize. The only exceptions are denormals. They start with 241 // leading zeroes and their effective significand-size is hence smaller. 242 static int SignificandSizeForOrderOfMagnitude(int order) { 243 if (order >= (kDenormalExponent + kSignificandSize)) { 244 return kSignificandSize; 245 } 246 if (order <= kDenormalExponent) return 0; 247 return order - kDenormalExponent; 248 } 249 250 static double Infinity() { 251 return Double(kInfinity).value(); 252 } 253 254 static double NaN() { 255 return Double(kNaN).value(); 256 } 257 258 private: 259 static const int kDenormalExponent = -kExponentBias + 1; 260 static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000); 261 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 262 static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF); 263 #else 264 static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000); 265 #endif 266 267 268 const uint64_t d64_; 269 270 static uint64_t DiyFpToUint64(DiyFp diy_fp) { 271 uint64_t significand = diy_fp.f(); 272 int exponent = diy_fp.e(); 273 while (significand > kHiddenBit + kSignificandMask) { 274 significand >>= 1; 275 exponent++; 276 } 277 if (exponent >= kMaxExponent) { 278 return kInfinity; 279 } 280 if (exponent < kDenormalExponent) { 281 return 0; 282 } 283 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { 284 significand <<= 1; 285 exponent--; 286 } 287 uint64_t biased_exponent; 288 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { 289 biased_exponent = 0; 290 } else { 291 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); 292 } 293 return (significand & kSignificandMask) | 294 (biased_exponent << kPhysicalSignificandSize); 295 } 296 297 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double); 298 }; 299 300 class Single { 301 public: 302 static const uint32_t kSignMask = 0x80000000; 303 static const uint32_t kExponentMask = 0x7F800000; 304 static const uint32_t kSignificandMask = 0x007FFFFF; 305 static const uint32_t kHiddenBit = 0x00800000; 306 static const uint32_t kQuietNanBit = 0x00400000; 307 static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit. 308 static const int kSignificandSize = 24; 309 310 Single() : d32_(0) {} 311 explicit Single(float f) : d32_(float_to_uint32(f)) {} 312 explicit Single(uint32_t d32) : d32_(d32) {} 313 314 // The value encoded by this Single must be greater or equal to +0.0. 315 // It must not be special (infinity, or NaN). 316 DiyFp AsDiyFp() const { 317 DOUBLE_CONVERSION_ASSERT(Sign() > 0); 318 DOUBLE_CONVERSION_ASSERT(!IsSpecial()); 319 return DiyFp(Significand(), Exponent()); 320 } 321 322 // Returns the single's bit as uint64. 323 uint32_t AsUint32() const { 324 return d32_; 325 } 326 327 int Exponent() const { 328 if (IsDenormal()) return kDenormalExponent; 329 330 uint32_t d32 = AsUint32(); 331 int biased_e = 332 static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize); 333 return biased_e - kExponentBias; 334 } 335 336 uint32_t Significand() const { 337 uint32_t d32 = AsUint32(); 338 uint32_t significand = d32 & kSignificandMask; 339 if (!IsDenormal()) { 340 return significand + kHiddenBit; 341 } else { 342 return significand; 343 } 344 } 345 346 // Returns true if the single is a denormal. 347 bool IsDenormal() const { 348 uint32_t d32 = AsUint32(); 349 return (d32 & kExponentMask) == 0; 350 } 351 352 // We consider denormals not to be special. 353 // Hence only Infinity and NaN are special. 354 bool IsSpecial() const { 355 uint32_t d32 = AsUint32(); 356 return (d32 & kExponentMask) == kExponentMask; 357 } 358 359 bool IsNan() const { 360 uint32_t d32 = AsUint32(); 361 return ((d32 & kExponentMask) == kExponentMask) && 362 ((d32 & kSignificandMask) != 0); 363 } 364 365 bool IsQuietNan() const { 366 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 367 return IsNan() && ((AsUint32() & kQuietNanBit) == 0); 368 #else 369 return IsNan() && ((AsUint32() & kQuietNanBit) != 0); 370 #endif 371 } 372 373 bool IsSignalingNan() const { 374 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 375 return IsNan() && ((AsUint32() & kQuietNanBit) != 0); 376 #else 377 return IsNan() && ((AsUint32() & kQuietNanBit) == 0); 378 #endif 379 } 380 381 382 bool IsInfinite() const { 383 uint32_t d32 = AsUint32(); 384 return ((d32 & kExponentMask) == kExponentMask) && 385 ((d32 & kSignificandMask) == 0); 386 } 387 388 int Sign() const { 389 uint32_t d32 = AsUint32(); 390 return (d32 & kSignMask) == 0? 1: -1; 391 } 392 393 // Computes the two boundaries of this. 394 // The bigger boundary (m_plus) is normalized. The lower boundary has the same 395 // exponent as m_plus. 396 // Precondition: the value encoded by this Single must be greater than 0. 397 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { 398 DOUBLE_CONVERSION_ASSERT(value() > 0.0); 399 DiyFp v = this->AsDiyFp(); 400 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); 401 DiyFp m_minus; 402 if (LowerBoundaryIsCloser()) { 403 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); 404 } else { 405 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); 406 } 407 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); 408 m_minus.set_e(m_plus.e()); 409 *out_m_plus = m_plus; 410 *out_m_minus = m_minus; 411 } 412 413 // Precondition: the value encoded by this Single must be greater or equal 414 // than +0.0. 415 DiyFp UpperBoundary() const { 416 DOUBLE_CONVERSION_ASSERT(Sign() > 0); 417 return DiyFp(Significand() * 2 + 1, Exponent() - 1); 418 } 419 420 bool LowerBoundaryIsCloser() const { 421 // The boundary is closer if the significand is of the form f == 2^p-1 then 422 // the lower boundary is closer. 423 // Think of v = 1000e10 and v- = 9999e9. 424 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but 425 // at a distance of 1e8. 426 // The only exception is for the smallest normal: the largest denormal is 427 // at the same distance as its successor. 428 // Note: denormals have the same exponent as the smallest normals. 429 bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0); 430 return physical_significand_is_zero && (Exponent() != kDenormalExponent); 431 } 432 433 float value() const { return uint32_to_float(d32_); } 434 435 static float Infinity() { 436 return Single(kInfinity).value(); 437 } 438 439 static float NaN() { 440 return Single(kNaN).value(); 441 } 442 443 private: 444 static const int kExponentBias = 0x7F + kPhysicalSignificandSize; 445 static const int kDenormalExponent = -kExponentBias + 1; 446 static const int kMaxExponent = 0xFF - kExponentBias; 447 static const uint32_t kInfinity = 0x7F800000; 448 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) 449 static const uint32_t kNaN = 0x7FBFFFFF; 450 #else 451 static const uint32_t kNaN = 0x7FC00000; 452 #endif 453 454 const uint32_t d32_; 455 456 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single); 457 }; 458 459 } // namespace double_conversion 460 461 // ICU PATCH: Close ICU namespace 462 U_NAMESPACE_END 463 464 #endif // DOUBLE_CONVERSION_DOUBLE_H_ 465 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING