1 //===-- APFloat.cpp - Implement APFloat class -----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements a class to represent arbitrary precision floating 10 // point values and provide a variety of arithmetic operations on them. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/APFloat.h" 15 #include "llvm/ADT/APSInt.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/FoldingSet.h" 18 #include "llvm/ADT/Hashing.h" 19 #include "llvm/ADT/StringExtras.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/Config/llvm-config.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/MathExtras.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include <cstring> 27 #include <limits.h> 28 29 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ 30 do { \ 31 if (usesLayout<IEEEFloat>(getSemantics())) \ 32 return U.IEEE.METHOD_CALL; \ 33 if (usesLayout<DoubleAPFloat>(getSemantics())) \ 34 return U.Double.METHOD_CALL; \ 35 llvm_unreachable("Unexpected semantics"); \ 36 } while (false) 37 38 using namespace llvm; 39 40 /// A macro used to combine two fcCategory enums into one key which can be used 41 /// in a switch statement to classify how the interaction of two APFloat's 42 /// categories affects an operation. 43 /// 44 /// TODO: If clang source code is ever allowed to use constexpr in its own 45 /// codebase, change this into a static inline function. 46 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) 47 48 /* Assumed in hexadecimal significand parsing, and conversion to 49 hexadecimal strings. */ 50 static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part width must be divisible by 4!"); 51 52 namespace llvm { 53 /* Represents floating point arithmetic semantics. */ 54 struct fltSemantics { 55 /* The largest E such that 2^E is representable; this matches the 56 definition of IEEE 754. */ 57 APFloatBase::ExponentType maxExponent; 58 59 /* The smallest E such that 2^E is a normalized number; this 60 matches the definition of IEEE 754. */ 61 APFloatBase::ExponentType minExponent; 62 63 /* Number of bits in the significand. This includes the integer 64 bit. */ 65 unsigned int precision; 66 67 /* Number of bits actually used in the semantics. */ 68 unsigned int sizeInBits; 69 }; 70 71 static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; 72 static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; 73 static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; 74 static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; 75 static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; 76 static const fltSemantics semBogus = {0, 0, 0, 0}; 77 78 /* The IBM double-double semantics. Such a number consists of a pair of IEEE 79 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, 80 (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. 81 Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent 82 to each other, and two 11-bit exponents. 83 84 Note: we need to make the value different from semBogus as otherwise 85 an unsafe optimization may collapse both values to a single address, 86 and we heavily rely on them having distinct addresses. */ 87 static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0}; 88 89 /* These are legacy semantics for the fallback, inaccrurate implementation of 90 IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the 91 operation. It's equivalent to having an IEEE number with consecutive 106 92 bits of mantissa and 11 bits of exponent. 93 94 It's not equivalent to IBM double-double. For example, a legit IBM 95 double-double, 1 + epsilon: 96 97 1 + epsilon = 1 + (1 >> 1076) 98 99 is not representable by a consecutive 106 bits of mantissa. 100 101 Currently, these semantics are used in the following way: 102 103 semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> 104 (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> 105 semPPCDoubleDoubleLegacy -> IEEE operations 106 107 We use bitcastToAPInt() to get the bit representation (in APInt) of the 108 underlying IEEEdouble, then use the APInt constructor to construct the 109 legacy IEEE float. 110 111 TODO: Implement all operations in semPPCDoubleDouble, and delete these 112 semantics. */ 113 static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, 114 53 + 53, 128}; 115 116 const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { 117 switch (S) { 118 case S_IEEEhalf: 119 return IEEEhalf(); 120 case S_IEEEsingle: 121 return IEEEsingle(); 122 case S_IEEEdouble: 123 return IEEEdouble(); 124 case S_x87DoubleExtended: 125 return x87DoubleExtended(); 126 case S_IEEEquad: 127 return IEEEquad(); 128 case S_PPCDoubleDouble: 129 return PPCDoubleDouble(); 130 } 131 llvm_unreachable("Unrecognised floating semantics"); 132 } 133 134 APFloatBase::Semantics 135 APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { 136 if (&Sem == &llvm::APFloat::IEEEhalf()) 137 return S_IEEEhalf; 138 else if (&Sem == &llvm::APFloat::IEEEsingle()) 139 return S_IEEEsingle; 140 else if (&Sem == &llvm::APFloat::IEEEdouble()) 141 return S_IEEEdouble; 142 else if (&Sem == &llvm::APFloat::x87DoubleExtended()) 143 return S_x87DoubleExtended; 144 else if (&Sem == &llvm::APFloat::IEEEquad()) 145 return S_IEEEquad; 146 else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) 147 return S_PPCDoubleDouble; 148 else 149 llvm_unreachable("Unknown floating semantics"); 150 } 151 152 const fltSemantics &APFloatBase::IEEEhalf() { 153 return semIEEEhalf; 154 } 155 const fltSemantics &APFloatBase::IEEEsingle() { 156 return semIEEEsingle; 157 } 158 const fltSemantics &APFloatBase::IEEEdouble() { 159 return semIEEEdouble; 160 } 161 const fltSemantics &APFloatBase::IEEEquad() { 162 return semIEEEquad; 163 } 164 const fltSemantics &APFloatBase::x87DoubleExtended() { 165 return semX87DoubleExtended; 166 } 167 const fltSemantics &APFloatBase::Bogus() { 168 return semBogus; 169 } 170 const fltSemantics &APFloatBase::PPCDoubleDouble() { 171 return semPPCDoubleDouble; 172 } 173 174 /* A tight upper bound on number of parts required to hold the value 175 pow(5, power) is 176 177 power * 815 / (351 * integerPartWidth) + 1 178 179 However, whilst the result may require only this many parts, 180 because we are multiplying two values to get it, the 181 multiplication may require an extra part with the excess part 182 being zero (consider the trivial case of 1 * 1, tcFullMultiply 183 requires two parts to hold the single-part result). So we add an 184 extra one to guarantee enough space whilst multiplying. */ 185 const unsigned int maxExponent = 16383; 186 const unsigned int maxPrecision = 113; 187 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; 188 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); 189 190 unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { 191 return semantics.precision; 192 } 193 APFloatBase::ExponentType 194 APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { 195 return semantics.maxExponent; 196 } 197 APFloatBase::ExponentType 198 APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { 199 return semantics.minExponent; 200 } 201 unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { 202 return semantics.sizeInBits; 203 } 204 205 unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { 206 return Sem.sizeInBits; 207 } 208 209 /* A bunch of private, handy routines. */ 210 211 static inline unsigned int 212 partCountForBits(unsigned int bits) 213 { 214 return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; 215 } 216 217 /* Returns 0U-9U. Return values >= 10U are not digits. */ 218 static inline unsigned int 219 decDigitValue(unsigned int c) 220 { 221 return c - '0'; 222 } 223 224 /* Return the value of a decimal exponent of the form 225 [+-]ddddddd. 226 227 If the exponent overflows, returns a large exponent with the 228 appropriate sign. */ 229 static int 230 readExponent(StringRef::iterator begin, StringRef::iterator end) 231 { 232 bool isNegative; 233 unsigned int absExponent; 234 const unsigned int overlargeExponent = 24000; /* FIXME. */ 235 StringRef::iterator p = begin; 236 237 // Treat no exponent as 0 to match binutils 238 if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { 239 return 0; 240 } 241 242 isNegative = (*p == '-'); 243 if (*p == '-' || *p == '+') { 244 p++; 245 assert(p != end && "Exponent has no digits"); 246 } 247 248 absExponent = decDigitValue(*p++); 249 assert(absExponent < 10U && "Invalid character in exponent"); 250 251 for (; p != end; ++p) { 252 unsigned int value; 253 254 value = decDigitValue(*p); 255 assert(value < 10U && "Invalid character in exponent"); 256 257 value += absExponent * 10; 258 if (absExponent >= overlargeExponent) { 259 absExponent = overlargeExponent; 260 p = end; /* outwit assert below */ 261 break; 262 } 263 absExponent = value; 264 } 265 266 assert(p == end && "Invalid exponent in exponent"); 267 268 if (isNegative) 269 return -(int) absExponent; 270 else 271 return (int) absExponent; 272 } 273 274 /* This is ugly and needs cleaning up, but I don't immediately see 275 how whilst remaining safe. */ 276 static int 277 totalExponent(StringRef::iterator p, StringRef::iterator end, 278 int exponentAdjustment) 279 { 280 int unsignedExponent; 281 bool negative, overflow; 282 int exponent = 0; 283 284 assert(p != end && "Exponent has no digits"); 285 286 negative = *p == '-'; 287 if (*p == '-' || *p == '+') { 288 p++; 289 assert(p != end && "Exponent has no digits"); 290 } 291 292 unsignedExponent = 0; 293 overflow = false; 294 for (; p != end; ++p) { 295 unsigned int value; 296 297 value = decDigitValue(*p); 298 assert(value < 10U && "Invalid character in exponent"); 299 300 unsignedExponent = unsignedExponent * 10 + value; 301 if (unsignedExponent > 32767) { 302 overflow = true; 303 break; 304 } 305 } 306 307 if (exponentAdjustment > 32767 || exponentAdjustment < -32768) 308 overflow = true; 309 310 if (!overflow) { 311 exponent = unsignedExponent; 312 if (negative) 313 exponent = -exponent; 314 exponent += exponentAdjustment; 315 if (exponent > 32767 || exponent < -32768) 316 overflow = true; 317 } 318 319 if (overflow) 320 exponent = negative ? -32768: 32767; 321 322 return exponent; 323 } 324 325 static StringRef::iterator 326 skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, 327 StringRef::iterator *dot) 328 { 329 StringRef::iterator p = begin; 330 *dot = end; 331 while (p != end && *p == '0') 332 p++; 333 334 if (p != end && *p == '.') { 335 *dot = p++; 336 337 assert(end - begin != 1 && "Significand has no digits"); 338 339 while (p != end && *p == '0') 340 p++; 341 } 342 343 return p; 344 } 345 346 /* Given a normal decimal floating point number of the form 347 348 dddd.dddd[eE][+-]ddd 349 350 where the decimal point and exponent are optional, fill out the 351 structure D. Exponent is appropriate if the significand is 352 treated as an integer, and normalizedExponent if the significand 353 is taken to have the decimal point after a single leading 354 non-zero digit. 355 356 If the value is zero, V->firstSigDigit points to a non-digit, and 357 the return exponent is zero. 358 */ 359 struct decimalInfo { 360 const char *firstSigDigit; 361 const char *lastSigDigit; 362 int exponent; 363 int normalizedExponent; 364 }; 365 366 static void 367 interpretDecimal(StringRef::iterator begin, StringRef::iterator end, 368 decimalInfo *D) 369 { 370 StringRef::iterator dot = end; 371 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot); 372 373 D->firstSigDigit = p; 374 D->exponent = 0; 375 D->normalizedExponent = 0; 376 377 for (; p != end; ++p) { 378 if (*p == '.') { 379 assert(dot == end && "String contains multiple dots"); 380 dot = p++; 381 if (p == end) 382 break; 383 } 384 if (decDigitValue(*p) >= 10U) 385 break; 386 } 387 388 if (p != end) { 389 assert((*p == 'e' || *p == 'E') && "Invalid character in significand"); 390 assert(p != begin && "Significand has no digits"); 391 assert((dot == end || p - begin != 1) && "Significand has no digits"); 392 393 /* p points to the first non-digit in the string */ 394 D->exponent = readExponent(p + 1, end); 395 396 /* Implied decimal point? */ 397 if (dot == end) 398 dot = p; 399 } 400 401 /* If number is all zeroes accept any exponent. */ 402 if (p != D->firstSigDigit) { 403 /* Drop insignificant trailing zeroes. */ 404 if (p != begin) { 405 do 406 do 407 p--; 408 while (p != begin && *p == '0'); 409 while (p != begin && *p == '.'); 410 } 411 412 /* Adjust the exponents for any decimal point. */ 413 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); 414 D->normalizedExponent = (D->exponent + 415 static_cast<APFloat::ExponentType>((p - D->firstSigDigit) 416 - (dot > D->firstSigDigit && dot < p))); 417 } 418 419 D->lastSigDigit = p; 420 } 421 422 /* Return the trailing fraction of a hexadecimal number. 423 DIGITVALUE is the first hex digit of the fraction, P points to 424 the next digit. */ 425 static lostFraction 426 trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, 427 unsigned int digitValue) 428 { 429 unsigned int hexDigit; 430 431 /* If the first trailing digit isn't 0 or 8 we can work out the 432 fraction immediately. */ 433 if (digitValue > 8) 434 return lfMoreThanHalf; 435 else if (digitValue < 8 && digitValue > 0) 436 return lfLessThanHalf; 437 438 // Otherwise we need to find the first non-zero digit. 439 while (p != end && (*p == '0' || *p == '.')) 440 p++; 441 442 assert(p != end && "Invalid trailing hexadecimal fraction!"); 443 444 hexDigit = hexDigitValue(*p); 445 446 /* If we ran off the end it is exactly zero or one-half, otherwise 447 a little more. */ 448 if (hexDigit == -1U) 449 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; 450 else 451 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; 452 } 453 454 /* Return the fraction lost were a bignum truncated losing the least 455 significant BITS bits. */ 456 static lostFraction 457 lostFractionThroughTruncation(const APFloatBase::integerPart *parts, 458 unsigned int partCount, 459 unsigned int bits) 460 { 461 unsigned int lsb; 462 463 lsb = APInt::tcLSB(parts, partCount); 464 465 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */ 466 if (bits <= lsb) 467 return lfExactlyZero; 468 if (bits == lsb + 1) 469 return lfExactlyHalf; 470 if (bits <= partCount * APFloatBase::integerPartWidth && 471 APInt::tcExtractBit(parts, bits - 1)) 472 return lfMoreThanHalf; 473 474 return lfLessThanHalf; 475 } 476 477 /* Shift DST right BITS bits noting lost fraction. */ 478 static lostFraction 479 shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits) 480 { 481 lostFraction lost_fraction; 482 483 lost_fraction = lostFractionThroughTruncation(dst, parts, bits); 484 485 APInt::tcShiftRight(dst, parts, bits); 486 487 return lost_fraction; 488 } 489 490 /* Combine the effect of two lost fractions. */ 491 static lostFraction 492 combineLostFractions(lostFraction moreSignificant, 493 lostFraction lessSignificant) 494 { 495 if (lessSignificant != lfExactlyZero) { 496 if (moreSignificant == lfExactlyZero) 497 moreSignificant = lfLessThanHalf; 498 else if (moreSignificant == lfExactlyHalf) 499 moreSignificant = lfMoreThanHalf; 500 } 501 502 return moreSignificant; 503 } 504 505 /* The error from the true value, in half-ulps, on multiplying two 506 floating point numbers, which differ from the value they 507 approximate by at most HUE1 and HUE2 half-ulps, is strictly less 508 than the returned value. 509 510 See "How to Read Floating Point Numbers Accurately" by William D 511 Clinger. */ 512 static unsigned int 513 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) 514 { 515 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); 516 517 if (HUerr1 + HUerr2 == 0) 518 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ 519 else 520 return inexactMultiply + 2 * (HUerr1 + HUerr2); 521 } 522 523 /* The number of ulps from the boundary (zero, or half if ISNEAREST) 524 when the least significant BITS are truncated. BITS cannot be 525 zero. */ 526 static APFloatBase::integerPart 527 ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, 528 bool isNearest) { 529 unsigned int count, partBits; 530 APFloatBase::integerPart part, boundary; 531 532 assert(bits != 0); 533 534 bits--; 535 count = bits / APFloatBase::integerPartWidth; 536 partBits = bits % APFloatBase::integerPartWidth + 1; 537 538 part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits)); 539 540 if (isNearest) 541 boundary = (APFloatBase::integerPart) 1 << (partBits - 1); 542 else 543 boundary = 0; 544 545 if (count == 0) { 546 if (part - boundary <= boundary - part) 547 return part - boundary; 548 else 549 return boundary - part; 550 } 551 552 if (part == boundary) { 553 while (--count) 554 if (parts[count]) 555 return ~(APFloatBase::integerPart) 0; /* A lot. */ 556 557 return parts[0]; 558 } else if (part == boundary - 1) { 559 while (--count) 560 if (~parts[count]) 561 return ~(APFloatBase::integerPart) 0; /* A lot. */ 562 563 return -parts[0]; 564 } 565 566 return ~(APFloatBase::integerPart) 0; /* A lot. */ 567 } 568 569 /* Place pow(5, power) in DST, and return the number of parts used. 570 DST must be at least one part larger than size of the answer. */ 571 static unsigned int 572 powerOf5(APFloatBase::integerPart *dst, unsigned int power) { 573 static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 }; 574 APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; 575 pow5s[0] = 78125 * 5; 576 577 unsigned int partsCount[16] = { 1 }; 578 APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; 579 unsigned int result; 580 assert(power <= maxExponent); 581 582 p1 = dst; 583 p2 = scratch; 584 585 *p1 = firstEightPowers[power & 7]; 586 power >>= 3; 587 588 result = 1; 589 pow5 = pow5s; 590 591 for (unsigned int n = 0; power; power >>= 1, n++) { 592 unsigned int pc; 593 594 pc = partsCount[n]; 595 596 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ 597 if (pc == 0) { 598 pc = partsCount[n - 1]; 599 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc); 600 pc *= 2; 601 if (pow5[pc - 1] == 0) 602 pc--; 603 partsCount[n] = pc; 604 } 605 606 if (power & 1) { 607 APFloatBase::integerPart *tmp; 608 609 APInt::tcFullMultiply(p2, p1, pow5, result, pc); 610 result += pc; 611 if (p2[result - 1] == 0) 612 result--; 613 614 /* Now result is in p1 with partsCount parts and p2 is scratch 615 space. */ 616 tmp = p1; 617 p1 = p2; 618 p2 = tmp; 619 } 620 621 pow5 += pc; 622 } 623 624 if (p1 != dst) 625 APInt::tcAssign(dst, p1, result); 626 627 return result; 628 } 629 630 /* Zero at the end to avoid modular arithmetic when adding one; used 631 when rounding up during hexadecimal output. */ 632 static const char hexDigitsLower[] = "0123456789abcdef0"; 633 static const char hexDigitsUpper[] = "0123456789ABCDEF0"; 634 static const char infinityL[] = "infinity"; 635 static const char infinityU[] = "INFINITY"; 636 static const char NaNL[] = "nan"; 637 static const char NaNU[] = "NAN"; 638 639 /* Write out an integerPart in hexadecimal, starting with the most 640 significant nibble. Write out exactly COUNT hexdigits, return 641 COUNT. */ 642 static unsigned int 643 partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count, 644 const char *hexDigitChars) 645 { 646 unsigned int result = count; 647 648 assert(count != 0 && count <= APFloatBase::integerPartWidth / 4); 649 650 part >>= (APFloatBase::integerPartWidth - 4 * count); 651 while (count--) { 652 dst[count] = hexDigitChars[part & 0xf]; 653 part >>= 4; 654 } 655 656 return result; 657 } 658 659 /* Write out an unsigned decimal integer. */ 660 static char * 661 writeUnsignedDecimal (char *dst, unsigned int n) 662 { 663 char buff[40], *p; 664 665 p = buff; 666 do 667 *p++ = '0' + n % 10; 668 while (n /= 10); 669 670 do 671 *dst++ = *--p; 672 while (p != buff); 673 674 return dst; 675 } 676 677 /* Write out a signed decimal integer. */ 678 static char * 679 writeSignedDecimal (char *dst, int value) 680 { 681 if (value < 0) { 682 *dst++ = '-'; 683 dst = writeUnsignedDecimal(dst, -(unsigned) value); 684 } else 685 dst = writeUnsignedDecimal(dst, value); 686 687 return dst; 688 } 689 690 namespace detail { 691 /* Constructors. */ 692 void IEEEFloat::initialize(const fltSemantics *ourSemantics) { 693 unsigned int count; 694 695 semantics = ourSemantics; 696 count = partCount(); 697 if (count > 1) 698 significand.parts = new integerPart[count]; 699 } 700 701 void IEEEFloat::freeSignificand() { 702 if (needsCleanup()) 703 delete [] significand.parts; 704 } 705 706 void IEEEFloat::assign(const IEEEFloat &rhs) { 707 assert(semantics == rhs.semantics); 708 709 sign = rhs.sign; 710 category = rhs.category; 711 exponent = rhs.exponent; 712 if (isFiniteNonZero() || category == fcNaN) 713 copySignificand(rhs); 714 } 715 716 void IEEEFloat::copySignificand(const IEEEFloat &rhs) { 717 assert(isFiniteNonZero() || category == fcNaN); 718 assert(rhs.partCount() >= partCount()); 719 720 APInt::tcAssign(significandParts(), rhs.significandParts(), 721 partCount()); 722 } 723 724 /* Make this number a NaN, with an arbitrary but deterministic value 725 for the significand. If double or longer, this is a signalling NaN, 726 which may not be ideal. If float, this is QNaN(0). */ 727 void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) { 728 category = fcNaN; 729 sign = Negative; 730 731 integerPart *significand = significandParts(); 732 unsigned numParts = partCount(); 733 734 // Set the significand bits to the fill. 735 if (!fill || fill->getNumWords() < numParts) 736 APInt::tcSet(significand, 0, numParts); 737 if (fill) { 738 APInt::tcAssign(significand, fill->getRawData(), 739 std::min(fill->getNumWords(), numParts)); 740 741 // Zero out the excess bits of the significand. 742 unsigned bitsToPreserve = semantics->precision - 1; 743 unsigned part = bitsToPreserve / 64; 744 bitsToPreserve %= 64; 745 significand[part] &= ((1ULL << bitsToPreserve) - 1); 746 for (part++; part != numParts; ++part) 747 significand[part] = 0; 748 } 749 750 unsigned QNaNBit = semantics->precision - 2; 751 752 if (SNaN) { 753 // We always have to clear the QNaN bit to make it an SNaN. 754 APInt::tcClearBit(significand, QNaNBit); 755 756 // If there are no bits set in the payload, we have to set 757 // *something* to make it a NaN instead of an infinity; 758 // conventionally, this is the next bit down from the QNaN bit. 759 if (APInt::tcIsZero(significand, numParts)) 760 APInt::tcSetBit(significand, QNaNBit - 1); 761 } else { 762 // We always have to set the QNaN bit to make it a QNaN. 763 APInt::tcSetBit(significand, QNaNBit); 764 } 765 766 // For x87 extended precision, we want to make a NaN, not a 767 // pseudo-NaN. Maybe we should expose the ability to make 768 // pseudo-NaNs? 769 if (semantics == &semX87DoubleExtended) 770 APInt::tcSetBit(significand, QNaNBit + 1); 771 } 772 773 IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) { 774 if (this != &rhs) { 775 if (semantics != rhs.semantics) { 776 freeSignificand(); 777 initialize(rhs.semantics); 778 } 779 assign(rhs); 780 } 781 782 return *this; 783 } 784 785 IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) { 786 freeSignificand(); 787 788 semantics = rhs.semantics; 789 significand = rhs.significand; 790 exponent = rhs.exponent; 791 category = rhs.category; 792 sign = rhs.sign; 793 794 rhs.semantics = &semBogus; 795 return *this; 796 } 797 798 bool IEEEFloat::isDenormal() const { 799 return isFiniteNonZero() && (exponent == semantics->minExponent) && 800 (APInt::tcExtractBit(significandParts(), 801 semantics->precision - 1) == 0); 802 } 803 804 bool IEEEFloat::isSmallest() const { 805 // The smallest number by magnitude in our format will be the smallest 806 // denormal, i.e. the floating point number with exponent being minimum 807 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). 808 return isFiniteNonZero() && exponent == semantics->minExponent && 809 significandMSB() == 0; 810 } 811 812 bool IEEEFloat::isSignificandAllOnes() const { 813 // Test if the significand excluding the integral bit is all ones. This allows 814 // us to test for binade boundaries. 815 const integerPart *Parts = significandParts(); 816 const unsigned PartCount = partCount(); 817 for (unsigned i = 0; i < PartCount - 1; i++) 818 if (~Parts[i]) 819 return false; 820 821 // Set the unused high bits to all ones when we compare. 822 const unsigned NumHighBits = 823 PartCount*integerPartWidth - semantics->precision + 1; 824 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " 825 "fill than integerPartWidth"); 826 const integerPart HighBitFill = 827 ~integerPart(0) << (integerPartWidth - NumHighBits); 828 if (~(Parts[PartCount - 1] | HighBitFill)) 829 return false; 830 831 return true; 832 } 833 834 bool IEEEFloat::isSignificandAllZeros() const { 835 // Test if the significand excluding the integral bit is all zeros. This 836 // allows us to test for binade boundaries. 837 const integerPart *Parts = significandParts(); 838 const unsigned PartCount = partCount(); 839 840 for (unsigned i = 0; i < PartCount - 1; i++) 841 if (Parts[i]) 842 return false; 843 844 const unsigned NumHighBits = 845 PartCount*integerPartWidth - semantics->precision + 1; 846 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " 847 "clear than integerPartWidth"); 848 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; 849 850 if (Parts[PartCount - 1] & HighBitMask) 851 return false; 852 853 return true; 854 } 855 856 bool IEEEFloat::isLargest() const { 857 // The largest number by magnitude in our format will be the floating point 858 // number with maximum exponent and with significand that is all ones. 859 return isFiniteNonZero() && exponent == semantics->maxExponent 860 && isSignificandAllOnes(); 861 } 862 863 bool IEEEFloat::isInteger() const { 864 // This could be made more efficient; I'm going for obviously correct. 865 if (!isFinite()) return false; 866 IEEEFloat truncated = *this; 867 truncated.roundToIntegral(rmTowardZero); 868 return compare(truncated) == cmpEqual; 869 } 870 871 bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { 872 if (this == &rhs) 873 return true; 874 if (semantics != rhs.semantics || 875 category != rhs.category || 876 sign != rhs.sign) 877 return false; 878 if (category==fcZero || category==fcInfinity) 879 return true; 880 881 if (isFiniteNonZero() && exponent != rhs.exponent) 882 return false; 883 884 return std::equal(significandParts(), significandParts() + partCount(), 885 rhs.significandParts()); 886 } 887 888 IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) { 889 initialize(&ourSemantics); 890 sign = 0; 891 category = fcNormal; 892 zeroSignificand(); 893 exponent = ourSemantics.precision - 1; 894 significandParts()[0] = value; 895 normalize(rmNearestTiesToEven, lfExactlyZero); 896 } 897 898 IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) { 899 initialize(&ourSemantics); 900 category = fcZero; 901 sign = false; 902 } 903 904 // Delegate to the previous constructor, because later copy constructor may 905 // actually inspects category, which can't be garbage. 906 IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) 907 : IEEEFloat(ourSemantics) {} 908 909 IEEEFloat::IEEEFloat(const IEEEFloat &rhs) { 910 initialize(rhs.semantics); 911 assign(rhs); 912 } 913 914 IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) { 915 *this = std::move(rhs); 916 } 917 918 IEEEFloat::~IEEEFloat() { freeSignificand(); } 919 920 unsigned int IEEEFloat::partCount() const { 921 return partCountForBits(semantics->precision + 1); 922 } 923 924 const IEEEFloat::integerPart *IEEEFloat::significandParts() const { 925 return const_cast<IEEEFloat *>(this)->significandParts(); 926 } 927 928 IEEEFloat::integerPart *IEEEFloat::significandParts() { 929 if (partCount() > 1) 930 return significand.parts; 931 else 932 return &significand.part; 933 } 934 935 void IEEEFloat::zeroSignificand() { 936 APInt::tcSet(significandParts(), 0, partCount()); 937 } 938 939 /* Increment an fcNormal floating point number's significand. */ 940 void IEEEFloat::incrementSignificand() { 941 integerPart carry; 942 943 carry = APInt::tcIncrement(significandParts(), partCount()); 944 945 /* Our callers should never cause us to overflow. */ 946 assert(carry == 0); 947 (void)carry; 948 } 949 950 /* Add the significand of the RHS. Returns the carry flag. */ 951 IEEEFloat::integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) { 952 integerPart *parts; 953 954 parts = significandParts(); 955 956 assert(semantics == rhs.semantics); 957 assert(exponent == rhs.exponent); 958 959 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount()); 960 } 961 962 /* Subtract the significand of the RHS with a borrow flag. Returns 963 the borrow flag. */ 964 IEEEFloat::integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs, 965 integerPart borrow) { 966 integerPart *parts; 967 968 parts = significandParts(); 969 970 assert(semantics == rhs.semantics); 971 assert(exponent == rhs.exponent); 972 973 return APInt::tcSubtract(parts, rhs.significandParts(), borrow, 974 partCount()); 975 } 976 977 /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it 978 on to the full-precision result of the multiplication. Returns the 979 lost fraction. */ 980 lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, 981 const IEEEFloat *addend) { 982 unsigned int omsb; // One, not zero, based MSB. 983 unsigned int partsCount, newPartsCount, precision; 984 integerPart *lhsSignificand; 985 integerPart scratch[4]; 986 integerPart *fullSignificand; 987 lostFraction lost_fraction; 988 bool ignored; 989 990 assert(semantics == rhs.semantics); 991 992 precision = semantics->precision; 993 994 // Allocate space for twice as many bits as the original significand, plus one 995 // extra bit for the addition to overflow into. 996 newPartsCount = partCountForBits(precision * 2 + 1); 997 998 if (newPartsCount > 4) 999 fullSignificand = new integerPart[newPartsCount]; 1000 else 1001 fullSignificand = scratch; 1002 1003 lhsSignificand = significandParts(); 1004 partsCount = partCount(); 1005 1006 APInt::tcFullMultiply(fullSignificand, lhsSignificand, 1007 rhs.significandParts(), partsCount, partsCount); 1008 1009 lost_fraction = lfExactlyZero; 1010 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1011 exponent += rhs.exponent; 1012 1013 // Assume the operands involved in the multiplication are single-precision 1014 // FP, and the two multiplicants are: 1015 // *this = a23 . a22 ... a0 * 2^e1 1016 // rhs = b23 . b22 ... b0 * 2^e2 1017 // the result of multiplication is: 1018 // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2) 1019 // Note that there are three significant bits at the left-hand side of the 1020 // radix point: two for the multiplication, and an overflow bit for the 1021 // addition (that will always be zero at this point). Move the radix point 1022 // toward left by two bits, and adjust exponent accordingly. 1023 exponent += 2; 1024 1025 if (addend && addend->isNonZero()) { 1026 // The intermediate result of the multiplication has "2 * precision" 1027 // signicant bit; adjust the addend to be consistent with mul result. 1028 // 1029 Significand savedSignificand = significand; 1030 const fltSemantics *savedSemantics = semantics; 1031 fltSemantics extendedSemantics; 1032 opStatus status; 1033 unsigned int extendedPrecision; 1034 1035 // Normalize our MSB to one below the top bit to allow for overflow. 1036 extendedPrecision = 2 * precision + 1; 1037 if (omsb != extendedPrecision - 1) { 1038 assert(extendedPrecision > omsb); 1039 APInt::tcShiftLeft(fullSignificand, newPartsCount, 1040 (extendedPrecision - 1) - omsb); 1041 exponent -= (extendedPrecision - 1) - omsb; 1042 } 1043 1044 /* Create new semantics. */ 1045 extendedSemantics = *semantics; 1046 extendedSemantics.precision = extendedPrecision; 1047 1048 if (newPartsCount == 1) 1049 significand.part = fullSignificand[0]; 1050 else 1051 significand.parts = fullSignificand; 1052 semantics = &extendedSemantics; 1053 1054 IEEEFloat extendedAddend(*addend); 1055 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored); 1056 assert(status == opOK); 1057 (void)status; 1058 1059 // Shift the significand of the addend right by one bit. This guarantees 1060 // that the high bit of the significand is zero (same as fullSignificand), 1061 // so the addition will overflow (if it does overflow at all) into the top bit. 1062 lost_fraction = extendedAddend.shiftSignificandRight(1); 1063 assert(lost_fraction == lfExactlyZero && 1064 "Lost precision while shifting addend for fused-multiply-add."); 1065 1066 lost_fraction = addOrSubtractSignificand(extendedAddend, false); 1067 1068 /* Restore our state. */ 1069 if (newPartsCount == 1) 1070 fullSignificand[0] = significand.part; 1071 significand = savedSignificand; 1072 semantics = savedSemantics; 1073 1074 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1075 } 1076 1077 // Convert the result having "2 * precision" significant-bits back to the one 1078 // having "precision" significant-bits. First, move the radix point from 1079 // poision "2*precision - 1" to "precision - 1". The exponent need to be 1080 // adjusted by "2*precision - 1" - "precision - 1" = "precision". 1081 exponent -= precision + 1; 1082 1083 // In case MSB resides at the left-hand side of radix point, shift the 1084 // mantissa right by some amount to make sure the MSB reside right before 1085 // the radix point (i.e. "MSB . rest-significant-bits"). 1086 // 1087 // Note that the result is not normalized when "omsb < precision". So, the 1088 // caller needs to call IEEEFloat::normalize() if normalized value is 1089 // expected. 1090 if (omsb > precision) { 1091 unsigned int bits, significantParts; 1092 lostFraction lf; 1093 1094 bits = omsb - precision; 1095 significantParts = partCountForBits(omsb); 1096 lf = shiftRight(fullSignificand, significantParts, bits); 1097 lost_fraction = combineLostFractions(lf, lost_fraction); 1098 exponent += bits; 1099 } 1100 1101 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); 1102 1103 if (newPartsCount > 4) 1104 delete [] fullSignificand; 1105 1106 return lost_fraction; 1107 } 1108 1109 /* Multiply the significands of LHS and RHS to DST. */ 1110 lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) { 1111 unsigned int bit, i, partsCount; 1112 const integerPart *rhsSignificand; 1113 integerPart *lhsSignificand, *dividend, *divisor; 1114 integerPart scratch[4]; 1115 lostFraction lost_fraction; 1116 1117 assert(semantics == rhs.semantics); 1118 1119 lhsSignificand = significandParts(); 1120 rhsSignificand = rhs.significandParts(); 1121 partsCount = partCount(); 1122 1123 if (partsCount > 2) 1124 dividend = new integerPart[partsCount * 2]; 1125 else 1126 dividend = scratch; 1127 1128 divisor = dividend + partsCount; 1129 1130 /* Copy the dividend and divisor as they will be modified in-place. */ 1131 for (i = 0; i < partsCount; i++) { 1132 dividend[i] = lhsSignificand[i]; 1133 divisor[i] = rhsSignificand[i]; 1134 lhsSignificand[i] = 0; 1135 } 1136 1137 exponent -= rhs.exponent; 1138 1139 unsigned int precision = semantics->precision; 1140 1141 /* Normalize the divisor. */ 1142 bit = precision - APInt::tcMSB(divisor, partsCount) - 1; 1143 if (bit) { 1144 exponent += bit; 1145 APInt::tcShiftLeft(divisor, partsCount, bit); 1146 } 1147 1148 /* Normalize the dividend. */ 1149 bit = precision - APInt::tcMSB(dividend, partsCount) - 1; 1150 if (bit) { 1151 exponent -= bit; 1152 APInt::tcShiftLeft(dividend, partsCount, bit); 1153 } 1154 1155 /* Ensure the dividend >= divisor initially for the loop below. 1156 Incidentally, this means that the division loop below is 1157 guaranteed to set the integer bit to one. */ 1158 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { 1159 exponent--; 1160 APInt::tcShiftLeft(dividend, partsCount, 1); 1161 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); 1162 } 1163 1164 /* Long division. */ 1165 for (bit = precision; bit; bit -= 1) { 1166 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { 1167 APInt::tcSubtract(dividend, divisor, 0, partsCount); 1168 APInt::tcSetBit(lhsSignificand, bit - 1); 1169 } 1170 1171 APInt::tcShiftLeft(dividend, partsCount, 1); 1172 } 1173 1174 /* Figure out the lost fraction. */ 1175 int cmp = APInt::tcCompare(dividend, divisor, partsCount); 1176 1177 if (cmp > 0) 1178 lost_fraction = lfMoreThanHalf; 1179 else if (cmp == 0) 1180 lost_fraction = lfExactlyHalf; 1181 else if (APInt::tcIsZero(dividend, partsCount)) 1182 lost_fraction = lfExactlyZero; 1183 else 1184 lost_fraction = lfLessThanHalf; 1185 1186 if (partsCount > 2) 1187 delete [] dividend; 1188 1189 return lost_fraction; 1190 } 1191 1192 unsigned int IEEEFloat::significandMSB() const { 1193 return APInt::tcMSB(significandParts(), partCount()); 1194 } 1195 1196 unsigned int IEEEFloat::significandLSB() const { 1197 return APInt::tcLSB(significandParts(), partCount()); 1198 } 1199 1200 /* Note that a zero result is NOT normalized to fcZero. */ 1201 lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { 1202 /* Our exponent should not overflow. */ 1203 assert((ExponentType) (exponent + bits) >= exponent); 1204 1205 exponent += bits; 1206 1207 return shiftRight(significandParts(), partCount(), bits); 1208 } 1209 1210 /* Shift the significand left BITS bits, subtract BITS from its exponent. */ 1211 void IEEEFloat::shiftSignificandLeft(unsigned int bits) { 1212 assert(bits < semantics->precision); 1213 1214 if (bits) { 1215 unsigned int partsCount = partCount(); 1216 1217 APInt::tcShiftLeft(significandParts(), partsCount, bits); 1218 exponent -= bits; 1219 1220 assert(!APInt::tcIsZero(significandParts(), partsCount)); 1221 } 1222 } 1223 1224 IEEEFloat::cmpResult 1225 IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const { 1226 int compare; 1227 1228 assert(semantics == rhs.semantics); 1229 assert(isFiniteNonZero()); 1230 assert(rhs.isFiniteNonZero()); 1231 1232 compare = exponent - rhs.exponent; 1233 1234 /* If exponents are equal, do an unsigned bignum comparison of the 1235 significands. */ 1236 if (compare == 0) 1237 compare = APInt::tcCompare(significandParts(), rhs.significandParts(), 1238 partCount()); 1239 1240 if (compare > 0) 1241 return cmpGreaterThan; 1242 else if (compare < 0) 1243 return cmpLessThan; 1244 else 1245 return cmpEqual; 1246 } 1247 1248 /* Handle overflow. Sign is preserved. We either become infinity or 1249 the largest finite number. */ 1250 IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { 1251 /* Infinity? */ 1252 if (rounding_mode == rmNearestTiesToEven || 1253 rounding_mode == rmNearestTiesToAway || 1254 (rounding_mode == rmTowardPositive && !sign) || 1255 (rounding_mode == rmTowardNegative && sign)) { 1256 category = fcInfinity; 1257 return (opStatus) (opOverflow | opInexact); 1258 } 1259 1260 /* Otherwise we become the largest finite number. */ 1261 category = fcNormal; 1262 exponent = semantics->maxExponent; 1263 APInt::tcSetLeastSignificantBits(significandParts(), partCount(), 1264 semantics->precision); 1265 1266 return opInexact; 1267 } 1268 1269 /* Returns TRUE if, when truncating the current number, with BIT the 1270 new LSB, with the given lost fraction and rounding mode, the result 1271 would need to be rounded away from zero (i.e., by increasing the 1272 signficand). This routine must work for fcZero of both signs, and 1273 fcNormal numbers. */ 1274 bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode, 1275 lostFraction lost_fraction, 1276 unsigned int bit) const { 1277 /* NaNs and infinities should not have lost fractions. */ 1278 assert(isFiniteNonZero() || category == fcZero); 1279 1280 /* Current callers never pass this so we don't handle it. */ 1281 assert(lost_fraction != lfExactlyZero); 1282 1283 switch (rounding_mode) { 1284 case rmNearestTiesToAway: 1285 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; 1286 1287 case rmNearestTiesToEven: 1288 if (lost_fraction == lfMoreThanHalf) 1289 return true; 1290 1291 /* Our zeroes don't have a significand to test. */ 1292 if (lost_fraction == lfExactlyHalf && category != fcZero) 1293 return APInt::tcExtractBit(significandParts(), bit); 1294 1295 return false; 1296 1297 case rmTowardZero: 1298 return false; 1299 1300 case rmTowardPositive: 1301 return !sign; 1302 1303 case rmTowardNegative: 1304 return sign; 1305 } 1306 llvm_unreachable("Invalid rounding mode found"); 1307 } 1308 1309 IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, 1310 lostFraction lost_fraction) { 1311 unsigned int omsb; /* One, not zero, based MSB. */ 1312 int exponentChange; 1313 1314 if (!isFiniteNonZero()) 1315 return opOK; 1316 1317 /* Before rounding normalize the exponent of fcNormal numbers. */ 1318 omsb = significandMSB() + 1; 1319 1320 if (omsb) { 1321 /* OMSB is numbered from 1. We want to place it in the integer 1322 bit numbered PRECISION if possible, with a compensating change in 1323 the exponent. */ 1324 exponentChange = omsb - semantics->precision; 1325 1326 /* If the resulting exponent is too high, overflow according to 1327 the rounding mode. */ 1328 if (exponent + exponentChange > semantics->maxExponent) 1329 return handleOverflow(rounding_mode); 1330 1331 /* Subnormal numbers have exponent minExponent, and their MSB 1332 is forced based on that. */ 1333 if (exponent + exponentChange < semantics->minExponent) 1334 exponentChange = semantics->minExponent - exponent; 1335 1336 /* Shifting left is easy as we don't lose precision. */ 1337 if (exponentChange < 0) { 1338 assert(lost_fraction == lfExactlyZero); 1339 1340 shiftSignificandLeft(-exponentChange); 1341 1342 return opOK; 1343 } 1344 1345 if (exponentChange > 0) { 1346 lostFraction lf; 1347 1348 /* Shift right and capture any new lost fraction. */ 1349 lf = shiftSignificandRight(exponentChange); 1350 1351 lost_fraction = combineLostFractions(lf, lost_fraction); 1352 1353 /* Keep OMSB up-to-date. */ 1354 if (omsb > (unsigned) exponentChange) 1355 omsb -= exponentChange; 1356 else 1357 omsb = 0; 1358 } 1359 } 1360 1361 /* Now round the number according to rounding_mode given the lost 1362 fraction. */ 1363 1364 /* As specified in IEEE 754, since we do not trap we do not report 1365 underflow for exact results. */ 1366 if (lost_fraction == lfExactlyZero) { 1367 /* Canonicalize zeroes. */ 1368 if (omsb == 0) 1369 category = fcZero; 1370 1371 return opOK; 1372 } 1373 1374 /* Increment the significand if we're rounding away from zero. */ 1375 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) { 1376 if (omsb == 0) 1377 exponent = semantics->minExponent; 1378 1379 incrementSignificand(); 1380 omsb = significandMSB() + 1; 1381 1382 /* Did the significand increment overflow? */ 1383 if (omsb == (unsigned) semantics->precision + 1) { 1384 /* Renormalize by incrementing the exponent and shifting our 1385 significand right one. However if we already have the 1386 maximum exponent we overflow to infinity. */ 1387 if (exponent == semantics->maxExponent) { 1388 category = fcInfinity; 1389 1390 return (opStatus) (opOverflow | opInexact); 1391 } 1392 1393 shiftSignificandRight(1); 1394 1395 return opInexact; 1396 } 1397 } 1398 1399 /* The normal case - we were and are not denormal, and any 1400 significand increment above didn't overflow. */ 1401 if (omsb == semantics->precision) 1402 return opInexact; 1403 1404 /* We have a non-zero denormal. */ 1405 assert(omsb < semantics->precision); 1406 1407 /* Canonicalize zeroes. */ 1408 if (omsb == 0) 1409 category = fcZero; 1410 1411 /* The fcZero case is a denormal that underflowed to zero. */ 1412 return (opStatus) (opUnderflow | opInexact); 1413 } 1414 1415 IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, 1416 bool subtract) { 1417 switch (PackCategoriesIntoKey(category, rhs.category)) { 1418 default: 1419 llvm_unreachable(nullptr); 1420 1421 case PackCategoriesIntoKey(fcNaN, fcZero): 1422 case PackCategoriesIntoKey(fcNaN, fcNormal): 1423 case PackCategoriesIntoKey(fcNaN, fcInfinity): 1424 case PackCategoriesIntoKey(fcNaN, fcNaN): 1425 case PackCategoriesIntoKey(fcNormal, fcZero): 1426 case PackCategoriesIntoKey(fcInfinity, fcNormal): 1427 case PackCategoriesIntoKey(fcInfinity, fcZero): 1428 return opOK; 1429 1430 case PackCategoriesIntoKey(fcZero, fcNaN): 1431 case PackCategoriesIntoKey(fcNormal, fcNaN): 1432 case PackCategoriesIntoKey(fcInfinity, fcNaN): 1433 // We need to be sure to flip the sign here for subtraction because we 1434 // don't have a separate negate operation so -NaN becomes 0 - NaN here. 1435 sign = rhs.sign ^ subtract; 1436 category = fcNaN; 1437 copySignificand(rhs); 1438 return opOK; 1439 1440 case PackCategoriesIntoKey(fcNormal, fcInfinity): 1441 case PackCategoriesIntoKey(fcZero, fcInfinity): 1442 category = fcInfinity; 1443 sign = rhs.sign ^ subtract; 1444 return opOK; 1445 1446 case PackCategoriesIntoKey(fcZero, fcNormal): 1447 assign(rhs); 1448 sign = rhs.sign ^ subtract; 1449 return opOK; 1450 1451 case PackCategoriesIntoKey(fcZero, fcZero): 1452 /* Sign depends on rounding mode; handled by caller. */ 1453 return opOK; 1454 1455 case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1456 /* Differently signed infinities can only be validly 1457 subtracted. */ 1458 if (((sign ^ rhs.sign)!=0) != subtract) { 1459 makeNaN(); 1460 return opInvalidOp; 1461 } 1462 1463 return opOK; 1464 1465 case PackCategoriesIntoKey(fcNormal, fcNormal): 1466 return opDivByZero; 1467 } 1468 } 1469 1470 /* Add or subtract two normal numbers. */ 1471 lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs, 1472 bool subtract) { 1473 integerPart carry; 1474 lostFraction lost_fraction; 1475 int bits; 1476 1477 /* Determine if the operation on the absolute values is effectively 1478 an addition or subtraction. */ 1479 subtract ^= static_cast<bool>(sign ^ rhs.sign); 1480 1481 /* Are we bigger exponent-wise than the RHS? */ 1482 bits = exponent - rhs.exponent; 1483 1484 /* Subtraction is more subtle than one might naively expect. */ 1485 if (subtract) { 1486 IEEEFloat temp_rhs(rhs); 1487 1488 if (bits == 0) 1489 lost_fraction = lfExactlyZero; 1490 else if (bits > 0) { 1491 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1); 1492 shiftSignificandLeft(1); 1493 } else { 1494 lost_fraction = shiftSignificandRight(-bits - 1); 1495 temp_rhs.shiftSignificandLeft(1); 1496 } 1497 1498 // Should we reverse the subtraction. 1499 if (compareAbsoluteValue(temp_rhs) == cmpLessThan) { 1500 carry = temp_rhs.subtractSignificand 1501 (*this, lost_fraction != lfExactlyZero); 1502 copySignificand(temp_rhs); 1503 sign = !sign; 1504 } else { 1505 carry = subtractSignificand 1506 (temp_rhs, lost_fraction != lfExactlyZero); 1507 } 1508 1509 /* Invert the lost fraction - it was on the RHS and 1510 subtracted. */ 1511 if (lost_fraction == lfLessThanHalf) 1512 lost_fraction = lfMoreThanHalf; 1513 else if (lost_fraction == lfMoreThanHalf) 1514 lost_fraction = lfLessThanHalf; 1515 1516 /* The code above is intended to ensure that no borrow is 1517 necessary. */ 1518 assert(!carry); 1519 (void)carry; 1520 } else { 1521 if (bits > 0) { 1522 IEEEFloat temp_rhs(rhs); 1523 1524 lost_fraction = temp_rhs.shiftSignificandRight(bits); 1525 carry = addSignificand(temp_rhs); 1526 } else { 1527 lost_fraction = shiftSignificandRight(-bits); 1528 carry = addSignificand(rhs); 1529 } 1530 1531 /* We have a guard bit; generating a carry cannot happen. */ 1532 assert(!carry); 1533 (void)carry; 1534 } 1535 1536 return lost_fraction; 1537 } 1538 1539 IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) { 1540 switch (PackCategoriesIntoKey(category, rhs.category)) { 1541 default: 1542 llvm_unreachable(nullptr); 1543 1544 case PackCategoriesIntoKey(fcNaN, fcZero): 1545 case PackCategoriesIntoKey(fcNaN, fcNormal): 1546 case PackCategoriesIntoKey(fcNaN, fcInfinity): 1547 case PackCategoriesIntoKey(fcNaN, fcNaN): 1548 sign = false; 1549 return opOK; 1550 1551 case PackCategoriesIntoKey(fcZero, fcNaN): 1552 case PackCategoriesIntoKey(fcNormal, fcNaN): 1553 case PackCategoriesIntoKey(fcInfinity, fcNaN): 1554 sign = false; 1555 category = fcNaN; 1556 copySignificand(rhs); 1557 return opOK; 1558 1559 case PackCategoriesIntoKey(fcNormal, fcInfinity): 1560 case PackCategoriesIntoKey(fcInfinity, fcNormal): 1561 case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1562 category = fcInfinity; 1563 return opOK; 1564 1565 case PackCategoriesIntoKey(fcZero, fcNormal): 1566 case PackCategoriesIntoKey(fcNormal, fcZero): 1567 case PackCategoriesIntoKey(fcZero, fcZero): 1568 category = fcZero; 1569 return opOK; 1570 1571 case PackCategoriesIntoKey(fcZero, fcInfinity): 1572 case PackCategoriesIntoKey(fcInfinity, fcZero): 1573 makeNaN(); 1574 return opInvalidOp; 1575 1576 case PackCategoriesIntoKey(fcNormal, fcNormal): 1577 return opOK; 1578 } 1579 } 1580 1581 IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) { 1582 switch (PackCategoriesIntoKey(category, rhs.category)) { 1583 default: 1584 llvm_unreachable(nullptr); 1585 1586 case PackCategoriesIntoKey(fcZero, fcNaN): 1587 case PackCategoriesIntoKey(fcNormal, fcNaN): 1588 case PackCategoriesIntoKey(fcInfinity, fcNaN): 1589 category = fcNaN; 1590 copySignificand(rhs); 1591 LLVM_FALLTHROUGH; 1592 case PackCategoriesIntoKey(fcNaN, fcZero): 1593 case PackCategoriesIntoKey(fcNaN, fcNormal): 1594 case PackCategoriesIntoKey(fcNaN, fcInfinity): 1595 case PackCategoriesIntoKey(fcNaN, fcNaN): 1596 sign = false; 1597 LLVM_FALLTHROUGH; 1598 case PackCategoriesIntoKey(fcInfinity, fcZero): 1599 case PackCategoriesIntoKey(fcInfinity, fcNormal): 1600 case PackCategoriesIntoKey(fcZero, fcInfinity): 1601 case PackCategoriesIntoKey(fcZero, fcNormal): 1602 return opOK; 1603 1604 case PackCategoriesIntoKey(fcNormal, fcInfinity): 1605 category = fcZero; 1606 return opOK; 1607 1608 case PackCategoriesIntoKey(fcNormal, fcZero): 1609 category = fcInfinity; 1610 return opDivByZero; 1611 1612 case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1613 case PackCategoriesIntoKey(fcZero, fcZero): 1614 makeNaN(); 1615 return opInvalidOp; 1616 1617 case PackCategoriesIntoKey(fcNormal, fcNormal): 1618 return opOK; 1619 } 1620 } 1621 1622 IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) { 1623 switch (PackCategoriesIntoKey(category, rhs.category)) { 1624 default: 1625 llvm_unreachable(nullptr); 1626 1627 case PackCategoriesIntoKey(fcNaN, fcZero): 1628 case PackCategoriesIntoKey(fcNaN, fcNormal): 1629 case PackCategoriesIntoKey(fcNaN, fcInfinity): 1630 case PackCategoriesIntoKey(fcNaN, fcNaN): 1631 case PackCategoriesIntoKey(fcZero, fcInfinity): 1632 case PackCategoriesIntoKey(fcZero, fcNormal): 1633 case PackCategoriesIntoKey(fcNormal, fcInfinity): 1634 return opOK; 1635 1636 case PackCategoriesIntoKey(fcZero, fcNaN): 1637 case PackCategoriesIntoKey(fcNormal, fcNaN): 1638 case PackCategoriesIntoKey(fcInfinity, fcNaN): 1639 sign = false; 1640 category = fcNaN; 1641 copySignificand(rhs); 1642 return opOK; 1643 1644 case PackCategoriesIntoKey(fcNormal, fcZero): 1645 case PackCategoriesIntoKey(fcInfinity, fcZero): 1646 case PackCategoriesIntoKey(fcInfinity, fcNormal): 1647 case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1648 case PackCategoriesIntoKey(fcZero, fcZero): 1649 makeNaN(); 1650 return opInvalidOp; 1651 1652 case PackCategoriesIntoKey(fcNormal, fcNormal): 1653 return opOK; 1654 } 1655 } 1656 1657 /* Change sign. */ 1658 void IEEEFloat::changeSign() { 1659 /* Look mummy, this one's easy. */ 1660 sign = !sign; 1661 } 1662 1663 /* Normalized addition or subtraction. */ 1664 IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs, 1665 roundingMode rounding_mode, 1666 bool subtract) { 1667 opStatus fs; 1668 1669 fs = addOrSubtractSpecials(rhs, subtract); 1670 1671 /* This return code means it was not a simple case. */ 1672 if (fs == opDivByZero) { 1673 lostFraction lost_fraction; 1674 1675 lost_fraction = addOrSubtractSignificand(rhs, subtract); 1676 fs = normalize(rounding_mode, lost_fraction); 1677 1678 /* Can only be zero if we lost no fraction. */ 1679 assert(category != fcZero || lost_fraction == lfExactlyZero); 1680 } 1681 1682 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1683 positive zero unless rounding to minus infinity, except that 1684 adding two like-signed zeroes gives that zero. */ 1685 if (category == fcZero) { 1686 if (rhs.category != fcZero || (sign == rhs.sign) == subtract) 1687 sign = (rounding_mode == rmTowardNegative); 1688 } 1689 1690 return fs; 1691 } 1692 1693 /* Normalized addition. */ 1694 IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs, 1695 roundingMode rounding_mode) { 1696 return addOrSubtract(rhs, rounding_mode, false); 1697 } 1698 1699 /* Normalized subtraction. */ 1700 IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs, 1701 roundingMode rounding_mode) { 1702 return addOrSubtract(rhs, rounding_mode, true); 1703 } 1704 1705 /* Normalized multiply. */ 1706 IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs, 1707 roundingMode rounding_mode) { 1708 opStatus fs; 1709 1710 sign ^= rhs.sign; 1711 fs = multiplySpecials(rhs); 1712 1713 if (isFiniteNonZero()) { 1714 lostFraction lost_fraction = multiplySignificand(rhs, nullptr); 1715 fs = normalize(rounding_mode, lost_fraction); 1716 if (lost_fraction != lfExactlyZero) 1717 fs = (opStatus) (fs | opInexact); 1718 } 1719 1720 return fs; 1721 } 1722 1723 /* Normalized divide. */ 1724 IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs, 1725 roundingMode rounding_mode) { 1726 opStatus fs; 1727 1728 sign ^= rhs.sign; 1729 fs = divideSpecials(rhs); 1730 1731 if (isFiniteNonZero()) { 1732 lostFraction lost_fraction = divideSignificand(rhs); 1733 fs = normalize(rounding_mode, lost_fraction); 1734 if (lost_fraction != lfExactlyZero) 1735 fs = (opStatus) (fs | opInexact); 1736 } 1737 1738 return fs; 1739 } 1740 1741 /* Normalized remainder. This is not currently correct in all cases. */ 1742 IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { 1743 opStatus fs; 1744 IEEEFloat V = *this; 1745 unsigned int origSign = sign; 1746 1747 fs = V.divide(rhs, rmNearestTiesToEven); 1748 if (fs == opDivByZero) 1749 return fs; 1750 1751 int parts = partCount(); 1752 integerPart *x = new integerPart[parts]; 1753 bool ignored; 1754 fs = V.convertToInteger(makeMutableArrayRef(x, parts), 1755 parts * integerPartWidth, true, rmNearestTiesToEven, 1756 &ignored); 1757 if (fs == opInvalidOp) { 1758 delete[] x; 1759 return fs; 1760 } 1761 1762 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true, 1763 rmNearestTiesToEven); 1764 assert(fs==opOK); // should always work 1765 1766 fs = V.multiply(rhs, rmNearestTiesToEven); 1767 assert(fs==opOK || fs==opInexact); // should not overflow or underflow 1768 1769 fs = subtract(V, rmNearestTiesToEven); 1770 assert(fs==opOK || fs==opInexact); // likewise 1771 1772 if (isZero()) 1773 sign = origSign; // IEEE754 requires this 1774 delete[] x; 1775 return fs; 1776 } 1777 1778 /* Normalized llvm frem (C fmod). */ 1779 IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { 1780 opStatus fs; 1781 fs = modSpecials(rhs); 1782 unsigned int origSign = sign; 1783 1784 while (isFiniteNonZero() && rhs.isFiniteNonZero() && 1785 compareAbsoluteValue(rhs) != cmpLessThan) { 1786 IEEEFloat V = scalbn(rhs, ilogb(*this) - ilogb(rhs), rmNearestTiesToEven); 1787 if (compareAbsoluteValue(V) == cmpLessThan) 1788 V = scalbn(V, -1, rmNearestTiesToEven); 1789 V.sign = sign; 1790 1791 fs = subtract(V, rmNearestTiesToEven); 1792 assert(fs==opOK); 1793 } 1794 if (isZero()) 1795 sign = origSign; // fmod requires this 1796 return fs; 1797 } 1798 1799 /* Normalized fused-multiply-add. */ 1800 IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand, 1801 const IEEEFloat &addend, 1802 roundingMode rounding_mode) { 1803 opStatus fs; 1804 1805 /* Post-multiplication sign, before addition. */ 1806 sign ^= multiplicand.sign; 1807 1808 /* If and only if all arguments are normal do we need to do an 1809 extended-precision calculation. */ 1810 if (isFiniteNonZero() && 1811 multiplicand.isFiniteNonZero() && 1812 addend.isFinite()) { 1813 lostFraction lost_fraction; 1814 1815 lost_fraction = multiplySignificand(multiplicand, &addend); 1816 fs = normalize(rounding_mode, lost_fraction); 1817 if (lost_fraction != lfExactlyZero) 1818 fs = (opStatus) (fs | opInexact); 1819 1820 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1821 positive zero unless rounding to minus infinity, except that 1822 adding two like-signed zeroes gives that zero. */ 1823 if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign) 1824 sign = (rounding_mode == rmTowardNegative); 1825 } else { 1826 fs = multiplySpecials(multiplicand); 1827 1828 /* FS can only be opOK or opInvalidOp. There is no more work 1829 to do in the latter case. The IEEE-754R standard says it is 1830 implementation-defined in this case whether, if ADDEND is a 1831 quiet NaN, we raise invalid op; this implementation does so. 1832 1833 If we need to do the addition we can do so with normal 1834 precision. */ 1835 if (fs == opOK) 1836 fs = addOrSubtract(addend, rounding_mode, false); 1837 } 1838 1839 return fs; 1840 } 1841 1842 /* Rounding-mode corrrect round to integral value. */ 1843 IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { 1844 opStatus fs; 1845 1846 // If the exponent is large enough, we know that this value is already 1847 // integral, and the arithmetic below would potentially cause it to saturate 1848 // to +/-Inf. Bail out early instead. 1849 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics)) 1850 return opOK; 1851 1852 // The algorithm here is quite simple: we add 2^(p-1), where p is the 1853 // precision of our format, and then subtract it back off again. The choice 1854 // of rounding modes for the addition/subtraction determines the rounding mode 1855 // for our integral rounding as well. 1856 // NOTE: When the input value is negative, we do subtraction followed by 1857 // addition instead. 1858 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); 1859 IntegerConstant <<= semanticsPrecision(*semantics)-1; 1860 IEEEFloat MagicConstant(*semantics); 1861 fs = MagicConstant.convertFromAPInt(IntegerConstant, false, 1862 rmNearestTiesToEven); 1863 MagicConstant.sign = sign; 1864 1865 if (fs != opOK) 1866 return fs; 1867 1868 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly. 1869 bool inputSign = isNegative(); 1870 1871 fs = add(MagicConstant, rounding_mode); 1872 if (fs != opOK && fs != opInexact) 1873 return fs; 1874 1875 fs = subtract(MagicConstant, rounding_mode); 1876 1877 // Restore the input sign. 1878 if (inputSign != isNegative()) 1879 changeSign(); 1880 1881 return fs; 1882 } 1883 1884 1885 /* Comparison requires normalized numbers. */ 1886 IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { 1887 cmpResult result; 1888 1889 assert(semantics == rhs.semantics); 1890 1891 switch (PackCategoriesIntoKey(category, rhs.category)) { 1892 default: 1893 llvm_unreachable(nullptr); 1894 1895 case PackCategoriesIntoKey(fcNaN, fcZero): 1896 case PackCategoriesIntoKey(fcNaN, fcNormal): 1897 case PackCategoriesIntoKey(fcNaN, fcInfinity): 1898 case PackCategoriesIntoKey(fcNaN, fcNaN): 1899 case PackCategoriesIntoKey(fcZero, fcNaN): 1900 case PackCategoriesIntoKey(fcNormal, fcNaN): 1901 case PackCategoriesIntoKey(fcInfinity, fcNaN): 1902 return cmpUnordered; 1903 1904 case PackCategoriesIntoKey(fcInfinity, fcNormal): 1905 case PackCategoriesIntoKey(fcInfinity, fcZero): 1906 case PackCategoriesIntoKey(fcNormal, fcZero): 1907 if (sign) 1908 return cmpLessThan; 1909 else 1910 return cmpGreaterThan; 1911 1912 case PackCategoriesIntoKey(fcNormal, fcInfinity): 1913 case PackCategoriesIntoKey(fcZero, fcInfinity): 1914 case PackCategoriesIntoKey(fcZero, fcNormal): 1915 if (rhs.sign) 1916 return cmpGreaterThan; 1917 else 1918 return cmpLessThan; 1919 1920 case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1921 if (sign == rhs.sign) 1922 return cmpEqual; 1923 else if (sign) 1924 return cmpLessThan; 1925 else 1926 return cmpGreaterThan; 1927 1928 case PackCategoriesIntoKey(fcZero, fcZero): 1929 return cmpEqual; 1930 1931 case PackCategoriesIntoKey(fcNormal, fcNormal): 1932 break; 1933 } 1934 1935 /* Two normal numbers. Do they have the same sign? */ 1936 if (sign != rhs.sign) { 1937 if (sign) 1938 result = cmpLessThan; 1939 else 1940 result = cmpGreaterThan; 1941 } else { 1942 /* Compare absolute values; invert result if negative. */ 1943 result = compareAbsoluteValue(rhs); 1944 1945 if (sign) { 1946 if (result == cmpLessThan) 1947 result = cmpGreaterThan; 1948 else if (result == cmpGreaterThan) 1949 result = cmpLessThan; 1950 } 1951 } 1952 1953 return result; 1954 } 1955 1956 /// IEEEFloat::convert - convert a value of one floating point type to another. 1957 /// The return value corresponds to the IEEE754 exceptions. *losesInfo 1958 /// records whether the transformation lost information, i.e. whether 1959 /// converting the result back to the original type will produce the 1960 /// original value (this is almost the same as return value==fsOK, but there 1961 /// are edge cases where this is not so). 1962 1963 IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics, 1964 roundingMode rounding_mode, 1965 bool *losesInfo) { 1966 lostFraction lostFraction; 1967 unsigned int newPartCount, oldPartCount; 1968 opStatus fs; 1969 int shift; 1970 const fltSemantics &fromSemantics = *semantics; 1971 1972 lostFraction = lfExactlyZero; 1973 newPartCount = partCountForBits(toSemantics.precision + 1); 1974 oldPartCount = partCount(); 1975 shift = toSemantics.precision - fromSemantics.precision; 1976 1977 bool X86SpecialNan = false; 1978 if (&fromSemantics == &semX87DoubleExtended && 1979 &toSemantics != &semX87DoubleExtended && category == fcNaN && 1980 (!(*significandParts() & 0x8000000000000000ULL) || 1981 !(*significandParts() & 0x4000000000000000ULL))) { 1982 // x86 has some unusual NaNs which cannot be represented in any other 1983 // format; note them here. 1984 X86SpecialNan = true; 1985 } 1986 1987 // If this is a truncation of a denormal number, and the target semantics 1988 // has larger exponent range than the source semantics (this can happen 1989 // when truncating from PowerPC double-double to double format), the 1990 // right shift could lose result mantissa bits. Adjust exponent instead 1991 // of performing excessive shift. 1992 if (shift < 0 && isFiniteNonZero()) { 1993 int exponentChange = significandMSB() + 1 - fromSemantics.precision; 1994 if (exponent + exponentChange < toSemantics.minExponent) 1995 exponentChange = toSemantics.minExponent - exponent; 1996 if (exponentChange < shift) 1997 exponentChange = shift; 1998 if (exponentChange < 0) { 1999 shift -= exponentChange; 2000 exponent += exponentChange; 2001 } 2002 } 2003 2004 // If this is a truncation, perform the shift before we narrow the storage. 2005 if (shift < 0 && (isFiniteNonZero() || category==fcNaN)) 2006 lostFraction = shiftRight(significandParts(), oldPartCount, -shift); 2007 2008 // Fix the storage so it can hold to new value. 2009 if (newPartCount > oldPartCount) { 2010 // The new type requires more storage; make it available. 2011 integerPart *newParts; 2012 newParts = new integerPart[newPartCount]; 2013 APInt::tcSet(newParts, 0, newPartCount); 2014 if (isFiniteNonZero() || category==fcNaN) 2015 APInt::tcAssign(newParts, significandParts(), oldPartCount); 2016 freeSignificand(); 2017 significand.parts = newParts; 2018 } else if (newPartCount == 1 && oldPartCount != 1) { 2019 // Switch to built-in storage for a single part. 2020 integerPart newPart = 0; 2021 if (isFiniteNonZero() || category==fcNaN) 2022 newPart = significandParts()[0]; 2023 freeSignificand(); 2024 significand.part = newPart; 2025 } 2026 2027 // Now that we have the right storage, switch the semantics. 2028 semantics = &toSemantics; 2029 2030 // If this is an extension, perform the shift now that the storage is 2031 // available. 2032 if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) 2033 APInt::tcShiftLeft(significandParts(), newPartCount, shift); 2034 2035 if (isFiniteNonZero()) { 2036 fs = normalize(rounding_mode, lostFraction); 2037 *losesInfo = (fs != opOK); 2038 } else if (category == fcNaN) { 2039 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; 2040 2041 // For x87 extended precision, we want to make a NaN, not a special NaN if 2042 // the input wasn't special either. 2043 if (!X86SpecialNan && semantics == &semX87DoubleExtended) 2044 APInt::tcSetBit(significandParts(), semantics->precision - 1); 2045 2046 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan) 2047 // does not give you back the same bits. This is dubious, and we 2048 // don't currently do it. You're really supposed to get 2049 // an invalid operation signal at runtime, but nobody does that. 2050 fs = opOK; 2051 } else { 2052 *losesInfo = false; 2053 fs = opOK; 2054 } 2055 2056 return fs; 2057 } 2058 2059 /* Convert a floating point number to an integer according to the 2060 rounding mode. If the rounded integer value is out of range this 2061 returns an invalid operation exception and the contents of the 2062 destination parts are unspecified. If the rounded value is in 2063 range but the floating point number is not the exact integer, the C 2064 standard doesn't require an inexact exception to be raised. IEEE 2065 854 does require it so we do that. 2066 2067 Note that for conversions to integer type the C standard requires 2068 round-to-zero to always be used. */ 2069 IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger( 2070 MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned, 2071 roundingMode rounding_mode, bool *isExact) const { 2072 lostFraction lost_fraction; 2073 const integerPart *src; 2074 unsigned int dstPartsCount, truncatedBits; 2075 2076 *isExact = false; 2077 2078 /* Handle the three special cases first. */ 2079 if (category == fcInfinity || category == fcNaN) 2080 return opInvalidOp; 2081 2082 dstPartsCount = partCountForBits(width); 2083 assert(dstPartsCount <= parts.size() && "Integer too big"); 2084 2085 if (category == fcZero) { 2086 APInt::tcSet(parts.data(), 0, dstPartsCount); 2087 // Negative zero can't be represented as an int. 2088 *isExact = !sign; 2089 return opOK; 2090 } 2091 2092 src = significandParts(); 2093 2094 /* Step 1: place our absolute value, with any fraction truncated, in 2095 the destination. */ 2096 if (exponent < 0) { 2097 /* Our absolute value is less than one; truncate everything. */ 2098 APInt::tcSet(parts.data(), 0, dstPartsCount); 2099 /* For exponent -1 the integer bit represents .5, look at that. 2100 For smaller exponents leftmost truncated bit is 0. */ 2101 truncatedBits = semantics->precision -1U - exponent; 2102 } else { 2103 /* We want the most significant (exponent + 1) bits; the rest are 2104 truncated. */ 2105 unsigned int bits = exponent + 1U; 2106 2107 /* Hopelessly large in magnitude? */ 2108 if (bits > width) 2109 return opInvalidOp; 2110 2111 if (bits < semantics->precision) { 2112 /* We truncate (semantics->precision - bits) bits. */ 2113 truncatedBits = semantics->precision - bits; 2114 APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits); 2115 } else { 2116 /* We want at least as many bits as are available. */ 2117 APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision, 2118 0); 2119 APInt::tcShiftLeft(parts.data(), dstPartsCount, 2120 bits - semantics->precision); 2121 truncatedBits = 0; 2122 } 2123 } 2124 2125 /* Step 2: work out any lost fraction, and increment the absolute 2126 value if we would round away from zero. */ 2127 if (truncatedBits) { 2128 lost_fraction = lostFractionThroughTruncation(src, partCount(), 2129 truncatedBits); 2130 if (lost_fraction != lfExactlyZero && 2131 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { 2132 if (APInt::tcIncrement(parts.data(), dstPartsCount)) 2133 return opInvalidOp; /* Overflow. */ 2134 } 2135 } else { 2136 lost_fraction = lfExactlyZero; 2137 } 2138 2139 /* Step 3: check if we fit in the destination. */ 2140 unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1; 2141 2142 if (sign) { 2143 if (!isSigned) { 2144 /* Negative numbers cannot be represented as unsigned. */ 2145 if (omsb != 0) 2146 return opInvalidOp; 2147 } else { 2148 /* It takes omsb bits to represent the unsigned integer value. 2149 We lose a bit for the sign, but care is needed as the 2150 maximally negative integer is a special case. */ 2151 if (omsb == width && 2152 APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb) 2153 return opInvalidOp; 2154 2155 /* This case can happen because of rounding. */ 2156 if (omsb > width) 2157 return opInvalidOp; 2158 } 2159 2160 APInt::tcNegate (parts.data(), dstPartsCount); 2161 } else { 2162 if (omsb >= width + !isSigned) 2163 return opInvalidOp; 2164 } 2165 2166 if (lost_fraction == lfExactlyZero) { 2167 *isExact = true; 2168 return opOK; 2169 } else 2170 return opInexact; 2171 } 2172 2173 /* Same as convertToSignExtendedInteger, except we provide 2174 deterministic values in case of an invalid operation exception, 2175 namely zero for NaNs and the minimal or maximal value respectively 2176 for underflow or overflow. 2177 The *isExact output tells whether the result is exact, in the sense 2178 that converting it back to the original floating point type produces 2179 the original value. This is almost equivalent to result==opOK, 2180 except for negative zeroes. 2181 */ 2182 IEEEFloat::opStatus 2183 IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts, 2184 unsigned int width, bool isSigned, 2185 roundingMode rounding_mode, bool *isExact) const { 2186 opStatus fs; 2187 2188 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, 2189 isExact); 2190 2191 if (fs == opInvalidOp) { 2192 unsigned int bits, dstPartsCount; 2193 2194 dstPartsCount = partCountForBits(width); 2195 assert(dstPartsCount <= parts.size() && "Integer too big"); 2196 2197 if (category == fcNaN) 2198 bits = 0; 2199 else if (sign) 2200 bits = isSigned; 2201 else 2202 bits = width - isSigned; 2203 2204 APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); 2205 if (sign && isSigned) 2206 APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1); 2207 } 2208 2209 return fs; 2210 } 2211 2212 /* Convert an unsigned integer SRC to a floating point number, 2213 rounding according to ROUNDING_MODE. The sign of the floating 2214 point number is not modified. */ 2215 IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts( 2216 const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) { 2217 unsigned int omsb, precision, dstCount; 2218 integerPart *dst; 2219 lostFraction lost_fraction; 2220 2221 category = fcNormal; 2222 omsb = APInt::tcMSB(src, srcCount) + 1; 2223 dst = significandParts(); 2224 dstCount = partCount(); 2225 precision = semantics->precision; 2226 2227 /* We want the most significant PRECISION bits of SRC. There may not 2228 be that many; extract what we can. */ 2229 if (precision <= omsb) { 2230 exponent = omsb - 1; 2231 lost_fraction = lostFractionThroughTruncation(src, srcCount, 2232 omsb - precision); 2233 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); 2234 } else { 2235 exponent = precision - 1; 2236 lost_fraction = lfExactlyZero; 2237 APInt::tcExtract(dst, dstCount, src, omsb, 0); 2238 } 2239 2240 return normalize(rounding_mode, lost_fraction); 2241 } 2242 2243 IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned, 2244 roundingMode rounding_mode) { 2245 unsigned int partCount = Val.getNumWords(); 2246 APInt api = Val; 2247 2248 sign = false; 2249 if (isSigned && api.isNegative()) { 2250 sign = true; 2251 api = -api; 2252 } 2253 2254 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2255 } 2256 2257 /* Convert a two's complement integer SRC to a floating point number, 2258 rounding according to ROUNDING_MODE. ISSIGNED is true if the 2259 integer is signed, in which case it must be sign-extended. */ 2260 IEEEFloat::opStatus 2261 IEEEFloat::convertFromSignExtendedInteger(const integerPart *src, 2262 unsigned int srcCount, bool isSigned, 2263 roundingMode rounding_mode) { 2264 opStatus status; 2265 2266 if (isSigned && 2267 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { 2268 integerPart *copy; 2269 2270 /* If we're signed and negative negate a copy. */ 2271 sign = true; 2272 copy = new integerPart[srcCount]; 2273 APInt::tcAssign(copy, src, srcCount); 2274 APInt::tcNegate(copy, srcCount); 2275 status = convertFromUnsignedParts(copy, srcCount, rounding_mode); 2276 delete [] copy; 2277 } else { 2278 sign = false; 2279 status = convertFromUnsignedParts(src, srcCount, rounding_mode); 2280 } 2281 2282 return status; 2283 } 2284 2285 /* FIXME: should this just take a const APInt reference? */ 2286 IEEEFloat::opStatus 2287 IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts, 2288 unsigned int width, bool isSigned, 2289 roundingMode rounding_mode) { 2290 unsigned int partCount = partCountForBits(width); 2291 APInt api = APInt(width, makeArrayRef(parts, partCount)); 2292 2293 sign = false; 2294 if (isSigned && APInt::tcExtractBit(parts, width - 1)) { 2295 sign = true; 2296 api = -api; 2297 } 2298 2299 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2300 } 2301 2302 IEEEFloat::opStatus 2303 IEEEFloat::convertFromHexadecimalString(StringRef s, 2304 roundingMode rounding_mode) { 2305 lostFraction lost_fraction = lfExactlyZero; 2306 2307 category = fcNormal; 2308 zeroSignificand(); 2309 exponent = 0; 2310 2311 integerPart *significand = significandParts(); 2312 unsigned partsCount = partCount(); 2313 unsigned bitPos = partsCount * integerPartWidth; 2314 bool computedTrailingFraction = false; 2315 2316 // Skip leading zeroes and any (hexa)decimal point. 2317 StringRef::iterator begin = s.begin(); 2318 StringRef::iterator end = s.end(); 2319 StringRef::iterator dot; 2320 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot); 2321 StringRef::iterator firstSignificantDigit = p; 2322 2323 while (p != end) { 2324 integerPart hex_value; 2325 2326 if (*p == '.') { 2327 assert(dot == end && "String contains multiple dots"); 2328 dot = p++; 2329 continue; 2330 } 2331 2332 hex_value = hexDigitValue(*p); 2333 if (hex_value == -1U) 2334 break; 2335 2336 p++; 2337 2338 // Store the number while we have space. 2339 if (bitPos) { 2340 bitPos -= 4; 2341 hex_value <<= bitPos % integerPartWidth; 2342 significand[bitPos / integerPartWidth] |= hex_value; 2343 } else if (!computedTrailingFraction) { 2344 lost_fraction = trailingHexadecimalFraction(p, end, hex_value); 2345 computedTrailingFraction = true; 2346 } 2347 } 2348 2349 /* Hex floats require an exponent but not a hexadecimal point. */ 2350 assert(p != end && "Hex strings require an exponent"); 2351 assert((*p == 'p' || *p == 'P') && "Invalid character in significand"); 2352 assert(p != begin && "Significand has no digits"); 2353 assert((dot == end || p - begin != 1) && "Significand has no digits"); 2354 2355 /* Ignore the exponent if we are zero. */ 2356 if (p != firstSignificantDigit) { 2357 int expAdjustment; 2358 2359 /* Implicit hexadecimal point? */ 2360 if (dot == end) 2361 dot = p; 2362 2363 /* Calculate the exponent adjustment implicit in the number of 2364 significant digits. */ 2365 expAdjustment = static_cast<int>(dot - firstSignificantDigit); 2366 if (expAdjustment < 0) 2367 expAdjustment++; 2368 expAdjustment = expAdjustment * 4 - 1; 2369 2370 /* Adjust for writing the significand starting at the most 2371 significant nibble. */ 2372 expAdjustment += semantics->precision; 2373 expAdjustment -= partsCount * integerPartWidth; 2374 2375 /* Adjust for the given exponent. */ 2376 exponent = totalExponent(p + 1, end, expAdjustment); 2377 } 2378 2379 return normalize(rounding_mode, lost_fraction); 2380 } 2381 2382 IEEEFloat::opStatus 2383 IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, 2384 unsigned sigPartCount, int exp, 2385 roundingMode rounding_mode) { 2386 unsigned int parts, pow5PartCount; 2387 fltSemantics calcSemantics = { 32767, -32767, 0, 0 }; 2388 integerPart pow5Parts[maxPowerOfFiveParts]; 2389 bool isNearest; 2390 2391 isNearest = (rounding_mode == rmNearestTiesToEven || 2392 rounding_mode == rmNearestTiesToAway); 2393 2394 parts = partCountForBits(semantics->precision + 11); 2395 2396 /* Calculate pow(5, abs(exp)). */ 2397 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); 2398 2399 for (;; parts *= 2) { 2400 opStatus sigStatus, powStatus; 2401 unsigned int excessPrecision, truncatedBits; 2402 2403 calcSemantics.precision = parts * integerPartWidth - 1; 2404 excessPrecision = calcSemantics.precision - semantics->precision; 2405 truncatedBits = excessPrecision; 2406 2407 IEEEFloat decSig(calcSemantics, uninitialized); 2408 decSig.makeZero(sign); 2409 IEEEFloat pow5(calcSemantics); 2410 2411 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount, 2412 rmNearestTiesToEven); 2413 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount, 2414 rmNearestTiesToEven); 2415 /* Add exp, as 10^n = 5^n * 2^n. */ 2416 decSig.exponent += exp; 2417 2418 lostFraction calcLostFraction; 2419 integerPart HUerr, HUdistance; 2420 unsigned int powHUerr; 2421 2422 if (exp >= 0) { 2423 /* multiplySignificand leaves the precision-th bit set to 1. */ 2424 calcLostFraction = decSig.multiplySignificand(pow5, nullptr); 2425 powHUerr = powStatus != opOK; 2426 } else { 2427 calcLostFraction = decSig.divideSignificand(pow5); 2428 /* Denormal numbers have less precision. */ 2429 if (decSig.exponent < semantics->minExponent) { 2430 excessPrecision += (semantics->minExponent - decSig.exponent); 2431 truncatedBits = excessPrecision; 2432 if (excessPrecision > calcSemantics.precision) 2433 excessPrecision = calcSemantics.precision; 2434 } 2435 /* Extra half-ulp lost in reciprocal of exponent. */ 2436 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; 2437 } 2438 2439 /* Both multiplySignificand and divideSignificand return the 2440 result with the integer bit set. */ 2441 assert(APInt::tcExtractBit 2442 (decSig.significandParts(), calcSemantics.precision - 1) == 1); 2443 2444 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, 2445 powHUerr); 2446 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(), 2447 excessPrecision, isNearest); 2448 2449 /* Are we guaranteed to round correctly if we truncate? */ 2450 if (HUdistance >= HUerr) { 2451 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), 2452 calcSemantics.precision - excessPrecision, 2453 excessPrecision); 2454 /* Take the exponent of decSig. If we tcExtract-ed less bits 2455 above we must adjust our exponent to compensate for the 2456 implicit right shift. */ 2457 exponent = (decSig.exponent + semantics->precision 2458 - (calcSemantics.precision - excessPrecision)); 2459 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), 2460 decSig.partCount(), 2461 truncatedBits); 2462 return normalize(rounding_mode, calcLostFraction); 2463 } 2464 } 2465 } 2466 2467 IEEEFloat::opStatus 2468 IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { 2469 decimalInfo D; 2470 opStatus fs; 2471 2472 /* Scan the text. */ 2473 StringRef::iterator p = str.begin(); 2474 interpretDecimal(p, str.end(), &D); 2475 2476 /* Handle the quick cases. First the case of no significant digits, 2477 i.e. zero, and then exponents that are obviously too large or too 2478 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp 2479 definitely overflows if 2480 2481 (exp - 1) * L >= maxExponent 2482 2483 and definitely underflows to zero where 2484 2485 (exp + 1) * L <= minExponent - precision 2486 2487 With integer arithmetic the tightest bounds for L are 2488 2489 93/28 < L < 196/59 [ numerator <= 256 ] 2490 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] 2491 */ 2492 2493 // Test if we have a zero number allowing for strings with no null terminators 2494 // and zero decimals with non-zero exponents. 2495 // 2496 // We computed firstSigDigit by ignoring all zeros and dots. Thus if 2497 // D->firstSigDigit equals str.end(), every digit must be a zero and there can 2498 // be at most one dot. On the other hand, if we have a zero with a non-zero 2499 // exponent, then we know that D.firstSigDigit will be non-numeric. 2500 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) { 2501 category = fcZero; 2502 fs = opOK; 2503 2504 /* Check whether the normalized exponent is high enough to overflow 2505 max during the log-rebasing in the max-exponent check below. */ 2506 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { 2507 fs = handleOverflow(rounding_mode); 2508 2509 /* If it wasn't, then it also wasn't high enough to overflow max 2510 during the log-rebasing in the min-exponent check. Check that it 2511 won't overflow min in either check, then perform the min-exponent 2512 check. */ 2513 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || 2514 (D.normalizedExponent + 1) * 28738 <= 2515 8651 * (semantics->minExponent - (int) semantics->precision)) { 2516 /* Underflow to zero and round. */ 2517 category = fcNormal; 2518 zeroSignificand(); 2519 fs = normalize(rounding_mode, lfLessThanHalf); 2520 2521 /* We can finally safely perform the max-exponent check. */ 2522 } else if ((D.normalizedExponent - 1) * 42039 2523 >= 12655 * semantics->maxExponent) { 2524 /* Overflow and round. */ 2525 fs = handleOverflow(rounding_mode); 2526 } else { 2527 integerPart *decSignificand; 2528 unsigned int partCount; 2529 2530 /* A tight upper bound on number of bits required to hold an 2531 N-digit decimal integer is N * 196 / 59. Allocate enough space 2532 to hold the full significand, and an extra part required by 2533 tcMultiplyPart. */ 2534 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; 2535 partCount = partCountForBits(1 + 196 * partCount / 59); 2536 decSignificand = new integerPart[partCount + 1]; 2537 partCount = 0; 2538 2539 /* Convert to binary efficiently - we do almost all multiplication 2540 in an integerPart. When this would overflow do we do a single 2541 bignum multiplication, and then revert again to multiplication 2542 in an integerPart. */ 2543 do { 2544 integerPart decValue, val, multiplier; 2545 2546 val = 0; 2547 multiplier = 1; 2548 2549 do { 2550 if (*p == '.') { 2551 p++; 2552 if (p == str.end()) { 2553 break; 2554 } 2555 } 2556 decValue = decDigitValue(*p++); 2557 assert(decValue < 10U && "Invalid character in significand"); 2558 multiplier *= 10; 2559 val = val * 10 + decValue; 2560 /* The maximum number that can be multiplied by ten with any 2561 digit added without overflowing an integerPart. */ 2562 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); 2563 2564 /* Multiply out the current part. */ 2565 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, 2566 partCount, partCount + 1, false); 2567 2568 /* If we used another part (likely but not guaranteed), increase 2569 the count. */ 2570 if (decSignificand[partCount]) 2571 partCount++; 2572 } while (p <= D.lastSigDigit); 2573 2574 category = fcNormal; 2575 fs = roundSignificandWithExponent(decSignificand, partCount, 2576 D.exponent, rounding_mode); 2577 2578 delete [] decSignificand; 2579 } 2580 2581 return fs; 2582 } 2583 2584 bool IEEEFloat::convertFromStringSpecials(StringRef str) { 2585 if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) { 2586 makeInf(false); 2587 return true; 2588 } 2589 2590 if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) { 2591 makeInf(true); 2592 return true; 2593 } 2594 2595 if (str.equals("nan") || str.equals("NaN")) { 2596 makeNaN(false, false); 2597 return true; 2598 } 2599 2600 if (str.equals("-nan") || str.equals("-NaN")) { 2601 makeNaN(false, true); 2602 return true; 2603 } 2604 2605 return false; 2606 } 2607 2608 IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str, 2609 roundingMode rounding_mode) { 2610 assert(!str.empty() && "Invalid string length"); 2611 2612 // Handle special cases. 2613 if (convertFromStringSpecials(str)) 2614 return opOK; 2615 2616 /* Handle a leading minus sign. */ 2617 StringRef::iterator p = str.begin(); 2618 size_t slen = str.size(); 2619 sign = *p == '-' ? 1 : 0; 2620 if (*p == '-' || *p == '+') { 2621 p++; 2622 slen--; 2623 assert(slen && "String has no digits"); 2624 } 2625 2626 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 2627 assert(slen - 2 && "Invalid string"); 2628 return convertFromHexadecimalString(StringRef(p + 2, slen - 2), 2629 rounding_mode); 2630 } 2631 2632 return convertFromDecimalString(StringRef(p, slen), rounding_mode); 2633 } 2634 2635 /* Write out a hexadecimal representation of the floating point value 2636 to DST, which must be of sufficient size, in the C99 form 2637 [-]0xh.hhhhp[+-]d. Return the number of characters written, 2638 excluding the terminating NUL. 2639 2640 If UPPERCASE, the output is in upper case, otherwise in lower case. 2641 2642 HEXDIGITS digits appear altogether, rounding the value if 2643 necessary. If HEXDIGITS is 0, the minimal precision to display the 2644 number precisely is used instead. If nothing would appear after 2645 the decimal point it is suppressed. 2646 2647 The decimal exponent is always printed and has at least one digit. 2648 Zero values display an exponent of zero. Infinities and NaNs 2649 appear as "infinity" or "nan" respectively. 2650 2651 The above rules are as specified by C99. There is ambiguity about 2652 what the leading hexadecimal digit should be. This implementation 2653 uses whatever is necessary so that the exponent is displayed as 2654 stored. This implies the exponent will fall within the IEEE format 2655 range, and the leading hexadecimal digit will be 0 (for denormals), 2656 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with 2657 any other digits zero). 2658 */ 2659 unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits, 2660 bool upperCase, 2661 roundingMode rounding_mode) const { 2662 char *p; 2663 2664 p = dst; 2665 if (sign) 2666 *dst++ = '-'; 2667 2668 switch (category) { 2669 case fcInfinity: 2670 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); 2671 dst += sizeof infinityL - 1; 2672 break; 2673 2674 case fcNaN: 2675 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); 2676 dst += sizeof NaNU - 1; 2677 break; 2678 2679 case fcZero: 2680 *dst++ = '0'; 2681 *dst++ = upperCase ? 'X': 'x'; 2682 *dst++ = '0'; 2683 if (hexDigits > 1) { 2684 *dst++ = '.'; 2685 memset (dst, '0', hexDigits - 1); 2686 dst += hexDigits - 1; 2687 } 2688 *dst++ = upperCase ? 'P': 'p'; 2689 *dst++ = '0'; 2690 break; 2691 2692 case fcNormal: 2693 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); 2694 break; 2695 } 2696 2697 *dst = 0; 2698 2699 return static_cast<unsigned int>(dst - p); 2700 } 2701 2702 /* Does the hard work of outputting the correctly rounded hexadecimal 2703 form of a normal floating point number with the specified number of 2704 hexadecimal digits. If HEXDIGITS is zero the minimum number of 2705 digits necessary to print the value precisely is output. */ 2706 char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, 2707 bool upperCase, 2708 roundingMode rounding_mode) const { 2709 unsigned int count, valueBits, shift, partsCount, outputDigits; 2710 const char *hexDigitChars; 2711 const integerPart *significand; 2712 char *p; 2713 bool roundUp; 2714 2715 *dst++ = '0'; 2716 *dst++ = upperCase ? 'X': 'x'; 2717 2718 roundUp = false; 2719 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; 2720 2721 significand = significandParts(); 2722 partsCount = partCount(); 2723 2724 /* +3 because the first digit only uses the single integer bit, so 2725 we have 3 virtual zero most-significant-bits. */ 2726 valueBits = semantics->precision + 3; 2727 shift = integerPartWidth - valueBits % integerPartWidth; 2728 2729 /* The natural number of digits required ignoring trailing 2730 insignificant zeroes. */ 2731 outputDigits = (valueBits - significandLSB () + 3) / 4; 2732 2733 /* hexDigits of zero means use the required number for the 2734 precision. Otherwise, see if we are truncating. If we are, 2735 find out if we need to round away from zero. */ 2736 if (hexDigits) { 2737 if (hexDigits < outputDigits) { 2738 /* We are dropping non-zero bits, so need to check how to round. 2739 "bits" is the number of dropped bits. */ 2740 unsigned int bits; 2741 lostFraction fraction; 2742 2743 bits = valueBits - hexDigits * 4; 2744 fraction = lostFractionThroughTruncation (significand, partsCount, bits); 2745 roundUp = roundAwayFromZero(rounding_mode, fraction, bits); 2746 } 2747 outputDigits = hexDigits; 2748 } 2749 2750 /* Write the digits consecutively, and start writing in the location 2751 of the hexadecimal point. We move the most significant digit 2752 left and add the hexadecimal point later. */ 2753 p = ++dst; 2754 2755 count = (valueBits + integerPartWidth - 1) / integerPartWidth; 2756 2757 while (outputDigits && count) { 2758 integerPart part; 2759 2760 /* Put the most significant integerPartWidth bits in "part". */ 2761 if (--count == partsCount) 2762 part = 0; /* An imaginary higher zero part. */ 2763 else 2764 part = significand[count] << shift; 2765 2766 if (count && shift) 2767 part |= significand[count - 1] >> (integerPartWidth - shift); 2768 2769 /* Convert as much of "part" to hexdigits as we can. */ 2770 unsigned int curDigits = integerPartWidth / 4; 2771 2772 if (curDigits > outputDigits) 2773 curDigits = outputDigits; 2774 dst += partAsHex (dst, part, curDigits, hexDigitChars); 2775 outputDigits -= curDigits; 2776 } 2777 2778 if (roundUp) { 2779 char *q = dst; 2780 2781 /* Note that hexDigitChars has a trailing '0'. */ 2782 do { 2783 q--; 2784 *q = hexDigitChars[hexDigitValue (*q) + 1]; 2785 } while (*q == '0'); 2786 assert(q >= p); 2787 } else { 2788 /* Add trailing zeroes. */ 2789 memset (dst, '0', outputDigits); 2790 dst += outputDigits; 2791 } 2792 2793 /* Move the most significant digit to before the point, and if there 2794 is something after the decimal point add it. This must come 2795 after rounding above. */ 2796 p[-1] = p[0]; 2797 if (dst -1 == p) 2798 dst--; 2799 else 2800 p[0] = '.'; 2801 2802 /* Finally output the exponent. */ 2803 *dst++ = upperCase ? 'P': 'p'; 2804 2805 return writeSignedDecimal (dst, exponent); 2806 } 2807 2808 hash_code hash_value(const IEEEFloat &Arg) { 2809 if (!Arg.isFiniteNonZero()) 2810 return hash_combine((uint8_t)Arg.category, 2811 // NaN has no sign, fix it at zero. 2812 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, 2813 Arg.semantics->precision); 2814 2815 // Normal floats need their exponent and significand hashed. 2816 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, 2817 Arg.semantics->precision, Arg.exponent, 2818 hash_combine_range( 2819 Arg.significandParts(), 2820 Arg.significandParts() + Arg.partCount())); 2821 } 2822 2823 // Conversion from APFloat to/from host float/double. It may eventually be 2824 // possible to eliminate these and have everybody deal with APFloats, but that 2825 // will take a while. This approach will not easily extend to long double. 2826 // Current implementation requires integerPartWidth==64, which is correct at 2827 // the moment but could be made more general. 2828 2829 // Denormals have exponent minExponent in APFloat, but minExponent-1 in 2830 // the actual IEEE respresentations. We compensate for that here. 2831 2832 APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { 2833 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended); 2834 assert(partCount()==2); 2835 2836 uint64_t myexponent, mysignificand; 2837 2838 if (isFiniteNonZero()) { 2839 myexponent = exponent+16383; //bias 2840 mysignificand = significandParts()[0]; 2841 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) 2842 myexponent = 0; // denormal 2843 } else if (category==fcZero) { 2844 myexponent = 0; 2845 mysignificand = 0; 2846 } else if (category==fcInfinity) { 2847 myexponent = 0x7fff; 2848 mysignificand = 0x8000000000000000ULL; 2849 } else { 2850 assert(category == fcNaN && "Unknown category"); 2851 myexponent = 0x7fff; 2852 mysignificand = significandParts()[0]; 2853 } 2854 2855 uint64_t words[2]; 2856 words[0] = mysignificand; 2857 words[1] = ((uint64_t)(sign & 1) << 15) | 2858 (myexponent & 0x7fffLL); 2859 return APInt(80, words); 2860 } 2861 2862 APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const { 2863 assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy); 2864 assert(partCount()==2); 2865 2866 uint64_t words[2]; 2867 opStatus fs; 2868 bool losesInfo; 2869 2870 // Convert number to double. To avoid spurious underflows, we re- 2871 // normalize against the "double" minExponent first, and only *then* 2872 // truncate the mantissa. The result of that second conversion 2873 // may be inexact, but should never underflow. 2874 // Declare fltSemantics before APFloat that uses it (and 2875 // saves pointer to it) to ensure correct destruction order. 2876 fltSemantics extendedSemantics = *semantics; 2877 extendedSemantics.minExponent = semIEEEdouble.minExponent; 2878 IEEEFloat extended(*this); 2879 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2880 assert(fs == opOK && !losesInfo); 2881 (void)fs; 2882 2883 IEEEFloat u(extended); 2884 fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 2885 assert(fs == opOK || fs == opInexact); 2886 (void)fs; 2887 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); 2888 2889 // If conversion was exact or resulted in a special case, we're done; 2890 // just set the second double to zero. Otherwise, re-convert back to 2891 // the extended format and compute the difference. This now should 2892 // convert exactly to double. 2893 if (u.isFiniteNonZero() && losesInfo) { 2894 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2895 assert(fs == opOK && !losesInfo); 2896 (void)fs; 2897 2898 IEEEFloat v(extended); 2899 v.subtract(u, rmNearestTiesToEven); 2900 fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 2901 assert(fs == opOK && !losesInfo); 2902 (void)fs; 2903 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); 2904 } else { 2905 words[1] = 0; 2906 } 2907 2908 return APInt(128, words); 2909 } 2910 2911 APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { 2912 assert(semantics == (const llvm::fltSemantics*)&semIEEEquad); 2913 assert(partCount()==2); 2914 2915 uint64_t myexponent, mysignificand, mysignificand2; 2916 2917 if (isFiniteNonZero()) { 2918 myexponent = exponent+16383; //bias 2919 mysignificand = significandParts()[0]; 2920 mysignificand2 = significandParts()[1]; 2921 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) 2922 myexponent = 0; // denormal 2923 } else if (category==fcZero) { 2924 myexponent = 0; 2925 mysignificand = mysignificand2 = 0; 2926 } else if (category==fcInfinity) { 2927 myexponent = 0x7fff; 2928 mysignificand = mysignificand2 = 0; 2929 } else { 2930 assert(category == fcNaN && "Unknown category!"); 2931 myexponent = 0x7fff; 2932 mysignificand = significandParts()[0]; 2933 mysignificand2 = significandParts()[1]; 2934 } 2935 2936 uint64_t words[2]; 2937 words[0] = mysignificand; 2938 words[1] = ((uint64_t)(sign & 1) << 63) | 2939 ((myexponent & 0x7fff) << 48) | 2940 (mysignificand2 & 0xffffffffffffLL); 2941 2942 return APInt(128, words); 2943 } 2944 2945 APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { 2946 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble); 2947 assert(partCount()==1); 2948 2949 uint64_t myexponent, mysignificand; 2950 2951 if (isFiniteNonZero()) { 2952 myexponent = exponent+1023; //bias 2953 mysignificand = *significandParts(); 2954 if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) 2955 myexponent = 0; // denormal 2956 } else if (category==fcZero) { 2957 myexponent = 0; 2958 mysignificand = 0; 2959 } else if (category==fcInfinity) { 2960 myexponent = 0x7ff; 2961 mysignificand = 0; 2962 } else { 2963 assert(category == fcNaN && "Unknown category!"); 2964 myexponent = 0x7ff; 2965 mysignificand = *significandParts(); 2966 } 2967 2968 return APInt(64, ((((uint64_t)(sign & 1) << 63) | 2969 ((myexponent & 0x7ff) << 52) | 2970 (mysignificand & 0xfffffffffffffLL)))); 2971 } 2972 2973 APInt IEEEFloat::convertFloatAPFloatToAPInt() const { 2974 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle); 2975 assert(partCount()==1); 2976 2977 uint32_t myexponent, mysignificand; 2978 2979 if (isFiniteNonZero()) { 2980 myexponent = exponent+127; //bias 2981 mysignificand = (uint32_t)*significandParts(); 2982 if (myexponent == 1 && !(mysignificand & 0x800000)) 2983 myexponent = 0; // denormal 2984 } else if (category==fcZero) { 2985 myexponent = 0; 2986 mysignificand = 0; 2987 } else if (category==fcInfinity) { 2988 myexponent = 0xff; 2989 mysignificand = 0; 2990 } else { 2991 assert(category == fcNaN && "Unknown category!"); 2992 myexponent = 0xff; 2993 mysignificand = (uint32_t)*significandParts(); 2994 } 2995 2996 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | 2997 (mysignificand & 0x7fffff))); 2998 } 2999 3000 APInt IEEEFloat::convertHalfAPFloatToAPInt() const { 3001 assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf); 3002 assert(partCount()==1); 3003 3004 uint32_t myexponent, mysignificand; 3005 3006 if (isFiniteNonZero()) { 3007 myexponent = exponent+15; //bias 3008 mysignificand = (uint32_t)*significandParts(); 3009 if (myexponent == 1 && !(mysignificand & 0x400)) 3010 myexponent = 0; // denormal 3011 } else if (category==fcZero) { 3012 myexponent = 0; 3013 mysignificand = 0; 3014 } else if (category==fcInfinity) { 3015 myexponent = 0x1f; 3016 mysignificand = 0; 3017 } else { 3018 assert(category == fcNaN && "Unknown category!"); 3019 myexponent = 0x1f; 3020 mysignificand = (uint32_t)*significandParts(); 3021 } 3022 3023 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | 3024 (mysignificand & 0x3ff))); 3025 } 3026 3027 // This function creates an APInt that is just a bit map of the floating 3028 // point constant as it would appear in memory. It is not a conversion, 3029 // and treating the result as a normal integer is unlikely to be useful. 3030 3031 APInt IEEEFloat::bitcastToAPInt() const { 3032 if (semantics == (const llvm::fltSemantics*)&semIEEEhalf) 3033 return convertHalfAPFloatToAPInt(); 3034 3035 if (semantics == (const llvm::fltSemantics*)&semIEEEsingle) 3036 return convertFloatAPFloatToAPInt(); 3037 3038 if (semantics == (const llvm::fltSemantics*)&semIEEEdouble) 3039 return convertDoubleAPFloatToAPInt(); 3040 3041 if (semantics == (const llvm::fltSemantics*)&semIEEEquad) 3042 return convertQuadrupleAPFloatToAPInt(); 3043 3044 if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy) 3045 return convertPPCDoubleDoubleAPFloatToAPInt(); 3046 3047 assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && 3048 "unknown format!"); 3049 return convertF80LongDoubleAPFloatToAPInt(); 3050 } 3051 3052 float IEEEFloat::convertToFloat() const { 3053 assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle && 3054 "Float semantics are not IEEEsingle"); 3055 APInt api = bitcastToAPInt(); 3056 return api.bitsToFloat(); 3057 } 3058 3059 double IEEEFloat::convertToDouble() const { 3060 assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble && 3061 "Float semantics are not IEEEdouble"); 3062 APInt api = bitcastToAPInt(); 3063 return api.bitsToDouble(); 3064 } 3065 3066 /// Integer bit is explicit in this format. Intel hardware (387 and later) 3067 /// does not support these bit patterns: 3068 /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") 3069 /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") 3070 /// exponent!=0 nor all 1's, integer bit 0 ("unnormal") 3071 /// exponent = 0, integer bit 1 ("pseudodenormal") 3072 /// At the moment, the first three are treated as NaNs, the last one as Normal. 3073 void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { 3074 assert(api.getBitWidth()==80); 3075 uint64_t i1 = api.getRawData()[0]; 3076 uint64_t i2 = api.getRawData()[1]; 3077 uint64_t myexponent = (i2 & 0x7fff); 3078 uint64_t mysignificand = i1; 3079 uint8_t myintegerbit = mysignificand >> 63; 3080 3081 initialize(&semX87DoubleExtended); 3082 assert(partCount()==2); 3083 3084 sign = static_cast<unsigned int>(i2>>15); 3085 if (myexponent == 0 && mysignificand == 0) { 3086 // exponent, significand meaningless 3087 category = fcZero; 3088 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { 3089 // exponent, significand meaningless 3090 category = fcInfinity; 3091 } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) || 3092 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) { 3093 // exponent meaningless 3094 category = fcNaN; 3095 significandParts()[0] = mysignificand; 3096 significandParts()[1] = 0; 3097 } else { 3098 category = fcNormal; 3099 exponent = myexponent - 16383; 3100 significandParts()[0] = mysignificand; 3101 significandParts()[1] = 0; 3102 if (myexponent==0) // denormal 3103 exponent = -16382; 3104 } 3105 } 3106 3107 void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) { 3108 assert(api.getBitWidth()==128); 3109 uint64_t i1 = api.getRawData()[0]; 3110 uint64_t i2 = api.getRawData()[1]; 3111 opStatus fs; 3112 bool losesInfo; 3113 3114 // Get the first double and convert to our format. 3115 initFromDoubleAPInt(APInt(64, i1)); 3116 fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3117 assert(fs == opOK && !losesInfo); 3118 (void)fs; 3119 3120 // Unless we have a special case, add in second double. 3121 if (isFiniteNonZero()) { 3122 IEEEFloat v(semIEEEdouble, APInt(64, i2)); 3123 fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3124 assert(fs == opOK && !losesInfo); 3125 (void)fs; 3126 3127 add(v, rmNearestTiesToEven); 3128 } 3129 } 3130 3131 void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { 3132 assert(api.getBitWidth()==128); 3133 uint64_t i1 = api.getRawData()[0]; 3134 uint64_t i2 = api.getRawData()[1]; 3135 uint64_t myexponent = (i2 >> 48) & 0x7fff; 3136 uint64_t mysignificand = i1; 3137 uint64_t mysignificand2 = i2 & 0xffffffffffffLL; 3138 3139 initialize(&semIEEEquad); 3140 assert(partCount()==2); 3141 3142 sign = static_cast<unsigned int>(i2>>63); 3143 if (myexponent==0 && 3144 (mysignificand==0 && mysignificand2==0)) { 3145 // exponent, significand meaningless 3146 category = fcZero; 3147 } else if (myexponent==0x7fff && 3148 (mysignificand==0 && mysignificand2==0)) { 3149 // exponent, significand meaningless 3150 category = fcInfinity; 3151 } else if (myexponent==0x7fff && 3152 (mysignificand!=0 || mysignificand2 !=0)) { 3153 // exponent meaningless 3154 category = fcNaN; 3155 significandParts()[0] = mysignificand; 3156 significandParts()[1] = mysignificand2; 3157 } else { 3158 category = fcNormal; 3159 exponent = myexponent - 16383; 3160 significandParts()[0] = mysignificand; 3161 significandParts()[1] = mysignificand2; 3162 if (myexponent==0) // denormal 3163 exponent = -16382; 3164 else 3165 significandParts()[1] |= 0x1000000000000LL; // integer bit 3166 } 3167 } 3168 3169 void IEEEFloat::initFromDoubleAPInt(const APInt &api) { 3170 assert(api.getBitWidth()==64); 3171 uint64_t i = *api.getRawData(); 3172 uint64_t myexponent = (i >> 52) & 0x7ff; 3173 uint64_t mysignificand = i & 0xfffffffffffffLL; 3174 3175 initialize(&semIEEEdouble); 3176 assert(partCount()==1); 3177 3178 sign = static_cast<unsigned int>(i>>63); 3179 if (myexponent==0 && mysignificand==0) { 3180 // exponent, significand meaningless 3181 category = fcZero; 3182 } else if (myexponent==0x7ff && mysignificand==0) { 3183 // exponent, significand meaningless 3184 category = fcInfinity; 3185 } else if (myexponent==0x7ff && mysignificand!=0) { 3186 // exponent meaningless 3187 category = fcNaN; 3188 *significandParts() = mysignificand; 3189 } else { 3190 category = fcNormal; 3191 exponent = myexponent - 1023; 3192 *significandParts() = mysignificand; 3193 if (myexponent==0) // denormal 3194 exponent = -1022; 3195 else 3196 *significandParts() |= 0x10000000000000LL; // integer bit 3197 } 3198 } 3199 3200 void IEEEFloat::initFromFloatAPInt(const APInt &api) { 3201 assert(api.getBitWidth()==32); 3202 uint32_t i = (uint32_t)*api.getRawData(); 3203 uint32_t myexponent = (i >> 23) & 0xff; 3204 uint32_t mysignificand = i & 0x7fffff; 3205 3206 initialize(&semIEEEsingle); 3207 assert(partCount()==1); 3208 3209 sign = i >> 31; 3210 if (myexponent==0 && mysignificand==0) { 3211 // exponent, significand meaningless 3212 category = fcZero; 3213 } else if (myexponent==0xff && mysignificand==0) { 3214 // exponent, significand meaningless 3215 category = fcInfinity; 3216 } else if (myexponent==0xff && mysignificand!=0) { 3217 // sign, exponent, significand meaningless 3218 category = fcNaN; 3219 *significandParts() = mysignificand; 3220 } else { 3221 category = fcNormal; 3222 exponent = myexponent - 127; //bias 3223 *significandParts() = mysignificand; 3224 if (myexponent==0) // denormal 3225 exponent = -126; 3226 else 3227 *significandParts() |= 0x800000; // integer bit 3228 } 3229 } 3230 3231 void IEEEFloat::initFromHalfAPInt(const APInt &api) { 3232 assert(api.getBitWidth()==16); 3233 uint32_t i = (uint32_t)*api.getRawData(); 3234 uint32_t myexponent = (i >> 10) & 0x1f; 3235 uint32_t mysignificand = i & 0x3ff; 3236 3237 initialize(&semIEEEhalf); 3238 assert(partCount()==1); 3239 3240 sign = i >> 15; 3241 if (myexponent==0 && mysignificand==0) { 3242 // exponent, significand meaningless 3243 category = fcZero; 3244 } else if (myexponent==0x1f && mysignificand==0) { 3245 // exponent, significand meaningless 3246 category = fcInfinity; 3247 } else if (myexponent==0x1f && mysignificand!=0) { 3248 // sign, exponent, significand meaningless 3249 category = fcNaN; 3250 *significandParts() = mysignificand; 3251 } else { 3252 category = fcNormal; 3253 exponent = myexponent - 15; //bias 3254 *significandParts() = mysignificand; 3255 if (myexponent==0) // denormal 3256 exponent = -14; 3257 else 3258 *significandParts() |= 0x400; // integer bit 3259 } 3260 } 3261 3262 /// Treat api as containing the bits of a floating point number. Currently 3263 /// we infer the floating point type from the size of the APInt. The 3264 /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful 3265 /// when the size is anything else). 3266 void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) { 3267 if (Sem == &semIEEEhalf) 3268 return initFromHalfAPInt(api); 3269 if (Sem == &semIEEEsingle) 3270 return initFromFloatAPInt(api); 3271 if (Sem == &semIEEEdouble) 3272 return initFromDoubleAPInt(api); 3273 if (Sem == &semX87DoubleExtended) 3274 return initFromF80LongDoubleAPInt(api); 3275 if (Sem == &semIEEEquad) 3276 return initFromQuadrupleAPInt(api); 3277 if (Sem == &semPPCDoubleDoubleLegacy) 3278 return initFromPPCDoubleDoubleAPInt(api); 3279 3280 llvm_unreachable(nullptr); 3281 } 3282 3283 /// Make this number the largest magnitude normal number in the given 3284 /// semantics. 3285 void IEEEFloat::makeLargest(bool Negative) { 3286 // We want (in interchange format): 3287 // sign = {Negative} 3288 // exponent = 1..10 3289 // significand = 1..1 3290 category = fcNormal; 3291 sign = Negative; 3292 exponent = semantics->maxExponent; 3293 3294 // Use memset to set all but the highest integerPart to all ones. 3295 integerPart *significand = significandParts(); 3296 unsigned PartCount = partCount(); 3297 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1)); 3298 3299 // Set the high integerPart especially setting all unused top bits for 3300 // internal consistency. 3301 const unsigned NumUnusedHighBits = 3302 PartCount*integerPartWidth - semantics->precision; 3303 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) 3304 ? (~integerPart(0) >> NumUnusedHighBits) 3305 : 0; 3306 } 3307 3308 /// Make this number the smallest magnitude denormal number in the given 3309 /// semantics. 3310 void IEEEFloat::makeSmallest(bool Negative) { 3311 // We want (in interchange format): 3312 // sign = {Negative} 3313 // exponent = 0..0 3314 // significand = 0..01 3315 category = fcNormal; 3316 sign = Negative; 3317 exponent = semantics->minExponent; 3318 APInt::tcSet(significandParts(), 1, partCount()); 3319 } 3320 3321 void IEEEFloat::makeSmallestNormalized(bool Negative) { 3322 // We want (in interchange format): 3323 // sign = {Negative} 3324 // exponent = 0..0 3325 // significand = 10..0 3326 3327 category = fcNormal; 3328 zeroSignificand(); 3329 sign = Negative; 3330 exponent = semantics->minExponent; 3331 significandParts()[partCountForBits(semantics->precision) - 1] |= 3332 (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth)); 3333 } 3334 3335 IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) { 3336 initFromAPInt(&Sem, API); 3337 } 3338 3339 IEEEFloat::IEEEFloat(float f) { 3340 initFromAPInt(&semIEEEsingle, APInt::floatToBits(f)); 3341 } 3342 3343 IEEEFloat::IEEEFloat(double d) { 3344 initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d)); 3345 } 3346 3347 namespace { 3348 void append(SmallVectorImpl<char> &Buffer, StringRef Str) { 3349 Buffer.append(Str.begin(), Str.end()); 3350 } 3351 3352 /// Removes data from the given significand until it is no more 3353 /// precise than is required for the desired precision. 3354 void AdjustToPrecision(APInt &significand, 3355 int &exp, unsigned FormatPrecision) { 3356 unsigned bits = significand.getActiveBits(); 3357 3358 // 196/59 is a very slight overestimate of lg_2(10). 3359 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; 3360 3361 if (bits <= bitsRequired) return; 3362 3363 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; 3364 if (!tensRemovable) return; 3365 3366 exp += tensRemovable; 3367 3368 APInt divisor(significand.getBitWidth(), 1); 3369 APInt powten(significand.getBitWidth(), 10); 3370 while (true) { 3371 if (tensRemovable & 1) 3372 divisor *= powten; 3373 tensRemovable >>= 1; 3374 if (!tensRemovable) break; 3375 powten *= powten; 3376 } 3377 3378 significand = significand.udiv(divisor); 3379 3380 // Truncate the significand down to its active bit count. 3381 significand = significand.trunc(significand.getActiveBits()); 3382 } 3383 3384 3385 void AdjustToPrecision(SmallVectorImpl<char> &buffer, 3386 int &exp, unsigned FormatPrecision) { 3387 unsigned N = buffer.size(); 3388 if (N <= FormatPrecision) return; 3389 3390 // The most significant figures are the last ones in the buffer. 3391 unsigned FirstSignificant = N - FormatPrecision; 3392 3393 // Round. 3394 // FIXME: this probably shouldn't use 'round half up'. 3395 3396 // Rounding down is just a truncation, except we also want to drop 3397 // trailing zeros from the new result. 3398 if (buffer[FirstSignificant - 1] < '5') { 3399 while (FirstSignificant < N && buffer[FirstSignificant] == '0') 3400 FirstSignificant++; 3401 3402 exp += FirstSignificant; 3403 buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3404 return; 3405 } 3406 3407 // Rounding up requires a decimal add-with-carry. If we continue 3408 // the carry, the newly-introduced zeros will just be truncated. 3409 for (unsigned I = FirstSignificant; I != N; ++I) { 3410 if (buffer[I] == '9') { 3411 FirstSignificant++; 3412 } else { 3413 buffer[I]++; 3414 break; 3415 } 3416 } 3417 3418 // If we carried through, we have exactly one digit of precision. 3419 if (FirstSignificant == N) { 3420 exp += FirstSignificant; 3421 buffer.clear(); 3422 buffer.push_back('1'); 3423 return; 3424 } 3425 3426 exp += FirstSignificant; 3427 buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3428 } 3429 } 3430 3431 void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, 3432 unsigned FormatMaxPadding, bool TruncateZero) const { 3433 switch (category) { 3434 case fcInfinity: 3435 if (isNegative()) 3436 return append(Str, "-Inf"); 3437 else 3438 return append(Str, "+Inf"); 3439 3440 case fcNaN: return append(Str, "NaN"); 3441 3442 case fcZero: 3443 if (isNegative()) 3444 Str.push_back('-'); 3445 3446 if (!FormatMaxPadding) { 3447 if (TruncateZero) 3448 append(Str, "0.0E+0"); 3449 else { 3450 append(Str, "0.0"); 3451 if (FormatPrecision > 1) 3452 Str.append(FormatPrecision - 1, '0'); 3453 append(Str, "e+00"); 3454 } 3455 } else 3456 Str.push_back('0'); 3457 return; 3458 3459 case fcNormal: 3460 break; 3461 } 3462 3463 if (isNegative()) 3464 Str.push_back('-'); 3465 3466 // Decompose the number into an APInt and an exponent. 3467 int exp = exponent - ((int) semantics->precision - 1); 3468 APInt significand(semantics->precision, 3469 makeArrayRef(significandParts(), 3470 partCountForBits(semantics->precision))); 3471 3472 // Set FormatPrecision if zero. We want to do this before we 3473 // truncate trailing zeros, as those are part of the precision. 3474 if (!FormatPrecision) { 3475 // We use enough digits so the number can be round-tripped back to an 3476 // APFloat. The formula comes from "How to Print Floating-Point Numbers 3477 // Accurately" by Steele and White. 3478 // FIXME: Using a formula based purely on the precision is conservative; 3479 // we can print fewer digits depending on the actual value being printed. 3480 3481 // FormatPrecision = 2 + floor(significandBits / lg_2(10)) 3482 FormatPrecision = 2 + semantics->precision * 59 / 196; 3483 } 3484 3485 // Ignore trailing binary zeros. 3486 int trailingZeros = significand.countTrailingZeros(); 3487 exp += trailingZeros; 3488 significand.lshrInPlace(trailingZeros); 3489 3490 // Change the exponent from 2^e to 10^e. 3491 if (exp == 0) { 3492 // Nothing to do. 3493 } else if (exp > 0) { 3494 // Just shift left. 3495 significand = significand.zext(semantics->precision + exp); 3496 significand <<= exp; 3497 exp = 0; 3498 } else { /* exp < 0 */ 3499 int texp = -exp; 3500 3501 // We transform this using the identity: 3502 // (N)(2^-e) == (N)(5^e)(10^-e) 3503 // This means we have to multiply N (the significand) by 5^e. 3504 // To avoid overflow, we have to operate on numbers large 3505 // enough to store N * 5^e: 3506 // log2(N * 5^e) == log2(N) + e * log2(5) 3507 // <= semantics->precision + e * 137 / 59 3508 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) 3509 3510 unsigned precision = semantics->precision + (137 * texp + 136) / 59; 3511 3512 // Multiply significand by 5^e. 3513 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) 3514 significand = significand.zext(precision); 3515 APInt five_to_the_i(precision, 5); 3516 while (true) { 3517 if (texp & 1) significand *= five_to_the_i; 3518 3519 texp >>= 1; 3520 if (!texp) break; 3521 five_to_the_i *= five_to_the_i; 3522 } 3523 } 3524 3525 AdjustToPrecision(significand, exp, FormatPrecision); 3526 3527 SmallVector<char, 256> buffer; 3528 3529 // Fill the buffer. 3530 unsigned precision = significand.getBitWidth(); 3531 APInt ten(precision, 10); 3532 APInt digit(precision, 0); 3533 3534 bool inTrail = true; 3535 while (significand != 0) { 3536 // digit <- significand % 10 3537 // significand <- significand / 10 3538 APInt::udivrem(significand, ten, significand, digit); 3539 3540 unsigned d = digit.getZExtValue(); 3541 3542 // Drop trailing zeros. 3543 if (inTrail && !d) exp++; 3544 else { 3545 buffer.push_back((char) ('0' + d)); 3546 inTrail = false; 3547 } 3548 } 3549 3550 assert(!buffer.empty() && "no characters in buffer!"); 3551 3552 // Drop down to FormatPrecision. 3553 // TODO: don't do more precise calculations above than are required. 3554 AdjustToPrecision(buffer, exp, FormatPrecision); 3555 3556 unsigned NDigits = buffer.size(); 3557 3558 // Check whether we should use scientific notation. 3559 bool FormatScientific; 3560 if (!FormatMaxPadding) 3561 FormatScientific = true; 3562 else { 3563 if (exp >= 0) { 3564 // 765e3 --> 765000 3565 // ^^^ 3566 // But we shouldn't make the number look more precise than it is. 3567 FormatScientific = ((unsigned) exp > FormatMaxPadding || 3568 NDigits + (unsigned) exp > FormatPrecision); 3569 } else { 3570 // Power of the most significant digit. 3571 int MSD = exp + (int) (NDigits - 1); 3572 if (MSD >= 0) { 3573 // 765e-2 == 7.65 3574 FormatScientific = false; 3575 } else { 3576 // 765e-5 == 0.00765 3577 // ^ ^^ 3578 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; 3579 } 3580 } 3581 } 3582 3583 // Scientific formatting is pretty straightforward. 3584 if (FormatScientific) { 3585 exp += (NDigits - 1); 3586 3587 Str.push_back(buffer[NDigits-1]); 3588 Str.push_back('.'); 3589 if (NDigits == 1 && TruncateZero) 3590 Str.push_back('0'); 3591 else 3592 for (unsigned I = 1; I != NDigits; ++I) 3593 Str.push_back(buffer[NDigits-1-I]); 3594 // Fill with zeros up to FormatPrecision. 3595 if (!TruncateZero && FormatPrecision > NDigits - 1) 3596 Str.append(FormatPrecision - NDigits + 1, '0'); 3597 // For !TruncateZero we use lower 'e'. 3598 Str.push_back(TruncateZero ? 'E' : 'e'); 3599 3600 Str.push_back(exp >= 0 ? '+' : '-'); 3601 if (exp < 0) exp = -exp; 3602 SmallVector<char, 6> expbuf; 3603 do { 3604 expbuf.push_back((char) ('0' + (exp % 10))); 3605 exp /= 10; 3606 } while (exp); 3607 // Exponent always at least two digits if we do not truncate zeros. 3608 if (!TruncateZero && expbuf.size() < 2) 3609 expbuf.push_back('0'); 3610 for (unsigned I = 0, E = expbuf.size(); I != E; ++I) 3611 Str.push_back(expbuf[E-1-I]); 3612 return; 3613 } 3614 3615 // Non-scientific, positive exponents. 3616 if (exp >= 0) { 3617 for (unsigned I = 0; I != NDigits; ++I) 3618 Str.push_back(buffer[NDigits-1-I]); 3619 for (unsigned I = 0; I != (unsigned) exp; ++I) 3620 Str.push_back('0'); 3621 return; 3622 } 3623 3624 // Non-scientific, negative exponents. 3625 3626 // The number of digits to the left of the decimal point. 3627 int NWholeDigits = exp + (int) NDigits; 3628 3629 unsigned I = 0; 3630 if (NWholeDigits > 0) { 3631 for (; I != (unsigned) NWholeDigits; ++I) 3632 Str.push_back(buffer[NDigits-I-1]); 3633 Str.push_back('.'); 3634 } else { 3635 unsigned NZeros = 1 + (unsigned) -NWholeDigits; 3636 3637 Str.push_back('0'); 3638 Str.push_back('.'); 3639 for (unsigned Z = 1; Z != NZeros; ++Z) 3640 Str.push_back('0'); 3641 } 3642 3643 for (; I != NDigits; ++I) 3644 Str.push_back(buffer[NDigits-I-1]); 3645 } 3646 3647 bool IEEEFloat::getExactInverse(APFloat *inv) const { 3648 // Special floats and denormals have no exact inverse. 3649 if (!isFiniteNonZero()) 3650 return false; 3651 3652 // Check that the number is a power of two by making sure that only the 3653 // integer bit is set in the significand. 3654 if (significandLSB() != semantics->precision - 1) 3655 return false; 3656 3657 // Get the inverse. 3658 IEEEFloat reciprocal(*semantics, 1ULL); 3659 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK) 3660 return false; 3661 3662 // Avoid multiplication with a denormal, it is not safe on all platforms and 3663 // may be slower than a normal division. 3664 if (reciprocal.isDenormal()) 3665 return false; 3666 3667 assert(reciprocal.isFiniteNonZero() && 3668 reciprocal.significandLSB() == reciprocal.semantics->precision - 1); 3669 3670 if (inv) 3671 *inv = APFloat(reciprocal, *semantics); 3672 3673 return true; 3674 } 3675 3676 bool IEEEFloat::isSignaling() const { 3677 if (!isNaN()) 3678 return false; 3679 3680 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the 3681 // first bit of the trailing significand being 0. 3682 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2); 3683 } 3684 3685 /// IEEE-754R 2008 5.3.1: nextUp/nextDown. 3686 /// 3687 /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with 3688 /// appropriate sign switching before/after the computation. 3689 IEEEFloat::opStatus IEEEFloat::next(bool nextDown) { 3690 // If we are performing nextDown, swap sign so we have -x. 3691 if (nextDown) 3692 changeSign(); 3693 3694 // Compute nextUp(x) 3695 opStatus result = opOK; 3696 3697 // Handle each float category separately. 3698 switch (category) { 3699 case fcInfinity: 3700 // nextUp(+inf) = +inf 3701 if (!isNegative()) 3702 break; 3703 // nextUp(-inf) = -getLargest() 3704 makeLargest(true); 3705 break; 3706 case fcNaN: 3707 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. 3708 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not 3709 // change the payload. 3710 if (isSignaling()) { 3711 result = opInvalidOp; 3712 // For consistency, propagate the sign of the sNaN to the qNaN. 3713 makeNaN(false, isNegative(), nullptr); 3714 } 3715 break; 3716 case fcZero: 3717 // nextUp(pm 0) = +getSmallest() 3718 makeSmallest(false); 3719 break; 3720 case fcNormal: 3721 // nextUp(-getSmallest()) = -0 3722 if (isSmallest() && isNegative()) { 3723 APInt::tcSet(significandParts(), 0, partCount()); 3724 category = fcZero; 3725 exponent = 0; 3726 break; 3727 } 3728 3729 // nextUp(getLargest()) == INFINITY 3730 if (isLargest() && !isNegative()) { 3731 APInt::tcSet(significandParts(), 0, partCount()); 3732 category = fcInfinity; 3733 exponent = semantics->maxExponent + 1; 3734 break; 3735 } 3736 3737 // nextUp(normal) == normal + inc. 3738 if (isNegative()) { 3739 // If we are negative, we need to decrement the significand. 3740 3741 // We only cross a binade boundary that requires adjusting the exponent 3742 // if: 3743 // 1. exponent != semantics->minExponent. This implies we are not in the 3744 // smallest binade or are dealing with denormals. 3745 // 2. Our significand excluding the integral bit is all zeros. 3746 bool WillCrossBinadeBoundary = 3747 exponent != semantics->minExponent && isSignificandAllZeros(); 3748 3749 // Decrement the significand. 3750 // 3751 // We always do this since: 3752 // 1. If we are dealing with a non-binade decrement, by definition we 3753 // just decrement the significand. 3754 // 2. If we are dealing with a normal -> normal binade decrement, since 3755 // we have an explicit integral bit the fact that all bits but the 3756 // integral bit are zero implies that subtracting one will yield a 3757 // significand with 0 integral bit and 1 in all other spots. Thus we 3758 // must just adjust the exponent and set the integral bit to 1. 3759 // 3. If we are dealing with a normal -> denormal binade decrement, 3760 // since we set the integral bit to 0 when we represent denormals, we 3761 // just decrement the significand. 3762 integerPart *Parts = significandParts(); 3763 APInt::tcDecrement(Parts, partCount()); 3764 3765 if (WillCrossBinadeBoundary) { 3766 // Our result is a normal number. Do the following: 3767 // 1. Set the integral bit to 1. 3768 // 2. Decrement the exponent. 3769 APInt::tcSetBit(Parts, semantics->precision - 1); 3770 exponent--; 3771 } 3772 } else { 3773 // If we are positive, we need to increment the significand. 3774 3775 // We only cross a binade boundary that requires adjusting the exponent if 3776 // the input is not a denormal and all of said input's significand bits 3777 // are set. If all of said conditions are true: clear the significand, set 3778 // the integral bit to 1, and increment the exponent. If we have a 3779 // denormal always increment since moving denormals and the numbers in the 3780 // smallest normal binade have the same exponent in our representation. 3781 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes(); 3782 3783 if (WillCrossBinadeBoundary) { 3784 integerPart *Parts = significandParts(); 3785 APInt::tcSet(Parts, 0, partCount()); 3786 APInt::tcSetBit(Parts, semantics->precision - 1); 3787 assert(exponent != semantics->maxExponent && 3788 "We can not increment an exponent beyond the maxExponent allowed" 3789 " by the given floating point semantics."); 3790 exponent++; 3791 } else { 3792 incrementSignificand(); 3793 } 3794 } 3795 break; 3796 } 3797 3798 // If we are performing nextDown, swap sign so we have -nextUp(-x) 3799 if (nextDown) 3800 changeSign(); 3801 3802 return result; 3803 } 3804 3805 void IEEEFloat::makeInf(bool Negative) { 3806 category = fcInfinity; 3807 sign = Negative; 3808 exponent = semantics->maxExponent + 1; 3809 APInt::tcSet(significandParts(), 0, partCount()); 3810 } 3811 3812 void IEEEFloat::makeZero(bool Negative) { 3813 category = fcZero; 3814 sign = Negative; 3815 exponent = semantics->minExponent-1; 3816 APInt::tcSet(significandParts(), 0, partCount()); 3817 } 3818 3819 void IEEEFloat::makeQuiet() { 3820 assert(isNaN()); 3821 APInt::tcSetBit(significandParts(), semantics->precision - 2); 3822 } 3823 3824 int ilogb(const IEEEFloat &Arg) { 3825 if (Arg.isNaN()) 3826 return IEEEFloat::IEK_NaN; 3827 if (Arg.isZero()) 3828 return IEEEFloat::IEK_Zero; 3829 if (Arg.isInfinity()) 3830 return IEEEFloat::IEK_Inf; 3831 if (!Arg.isDenormal()) 3832 return Arg.exponent; 3833 3834 IEEEFloat Normalized(Arg); 3835 int SignificandBits = Arg.getSemantics().precision - 1; 3836 3837 Normalized.exponent += SignificandBits; 3838 Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero); 3839 return Normalized.exponent - SignificandBits; 3840 } 3841 3842 IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) { 3843 auto MaxExp = X.getSemantics().maxExponent; 3844 auto MinExp = X.getSemantics().minExponent; 3845 3846 // If Exp is wildly out-of-scale, simply adding it to X.exponent will 3847 // overflow; clamp it to a safe range before adding, but ensure that the range 3848 // is large enough that the clamp does not change the result. The range we 3849 // need to support is the difference between the largest possible exponent and 3850 // the normalized exponent of half the smallest denormal. 3851 3852 int SignificandBits = X.getSemantics().precision - 1; 3853 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1; 3854 3855 // Clamp to one past the range ends to let normalize handle overlflow. 3856 X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement); 3857 X.normalize(RoundingMode, lfExactlyZero); 3858 if (X.isNaN()) 3859 X.makeQuiet(); 3860 return X; 3861 } 3862 3863 IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) { 3864 Exp = ilogb(Val); 3865 3866 // Quiet signalling nans. 3867 if (Exp == IEEEFloat::IEK_NaN) { 3868 IEEEFloat Quiet(Val); 3869 Quiet.makeQuiet(); 3870 return Quiet; 3871 } 3872 3873 if (Exp == IEEEFloat::IEK_Inf) 3874 return Val; 3875 3876 // 1 is added because frexp is defined to return a normalized fraction in 3877 // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0). 3878 Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1; 3879 return scalbn(Val, -Exp, RM); 3880 } 3881 3882 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S) 3883 : Semantics(&S), 3884 Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) { 3885 assert(Semantics == &semPPCDoubleDouble); 3886 } 3887 3888 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag) 3889 : Semantics(&S), 3890 Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized), 3891 APFloat(semIEEEdouble, uninitialized)}) { 3892 assert(Semantics == &semPPCDoubleDouble); 3893 } 3894 3895 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I) 3896 : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I), 3897 APFloat(semIEEEdouble)}) { 3898 assert(Semantics == &semPPCDoubleDouble); 3899 } 3900 3901 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I) 3902 : Semantics(&S), 3903 Floats(new APFloat[2]{ 3904 APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])), 3905 APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) { 3906 assert(Semantics == &semPPCDoubleDouble); 3907 } 3908 3909 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First, 3910 APFloat &&Second) 3911 : Semantics(&S), 3912 Floats(new APFloat[2]{std::move(First), std::move(Second)}) { 3913 assert(Semantics == &semPPCDoubleDouble); 3914 assert(&Floats[0].getSemantics() == &semIEEEdouble); 3915 assert(&Floats[1].getSemantics() == &semIEEEdouble); 3916 } 3917 3918 DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS) 3919 : Semantics(RHS.Semantics), 3920 Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]), 3921 APFloat(RHS.Floats[1])} 3922 : nullptr) { 3923 assert(Semantics == &semPPCDoubleDouble); 3924 } 3925 3926 DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS) 3927 : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) { 3928 RHS.Semantics = &semBogus; 3929 assert(Semantics == &semPPCDoubleDouble); 3930 } 3931 3932 DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) { 3933 if (Semantics == RHS.Semantics && RHS.Floats) { 3934 Floats[0] = RHS.Floats[0]; 3935 Floats[1] = RHS.Floats[1]; 3936 } else if (this != &RHS) { 3937 this->~DoubleAPFloat(); 3938 new (this) DoubleAPFloat(RHS); 3939 } 3940 return *this; 3941 } 3942 3943 // Implement addition, subtraction, multiplication and division based on: 3944 // "Software for Doubled-Precision Floating-Point Computations", 3945 // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283. 3946 APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa, 3947 const APFloat &c, const APFloat &cc, 3948 roundingMode RM) { 3949 int Status = opOK; 3950 APFloat z = a; 3951 Status |= z.add(c, RM); 3952 if (!z.isFinite()) { 3953 if (!z.isInfinity()) { 3954 Floats[0] = std::move(z); 3955 Floats[1].makeZero(/* Neg = */ false); 3956 return (opStatus)Status; 3957 } 3958 Status = opOK; 3959 auto AComparedToC = a.compareAbsoluteValue(c); 3960 z = cc; 3961 Status |= z.add(aa, RM); 3962 if (AComparedToC == APFloat::cmpGreaterThan) { 3963 // z = cc + aa + c + a; 3964 Status |= z.add(c, RM); 3965 Status |= z.add(a, RM); 3966 } else { 3967 // z = cc + aa + a + c; 3968 Status |= z.add(a, RM); 3969 Status |= z.add(c, RM); 3970 } 3971 if (!z.isFinite()) { 3972 Floats[0] = std::move(z); 3973 Floats[1].makeZero(/* Neg = */ false); 3974 return (opStatus)Status; 3975 } 3976 Floats[0] = z; 3977 APFloat zz = aa; 3978 Status |= zz.add(cc, RM); 3979 if (AComparedToC == APFloat::cmpGreaterThan) { 3980 // Floats[1] = a - z + c + zz; 3981 Floats[1] = a; 3982 Status |= Floats[1].subtract(z, RM); 3983 Status |= Floats[1].add(c, RM); 3984 Status |= Floats[1].add(zz, RM); 3985 } else { 3986 // Floats[1] = c - z + a + zz; 3987 Floats[1] = c; 3988 Status |= Floats[1].subtract(z, RM); 3989 Status |= Floats[1].add(a, RM); 3990 Status |= Floats[1].add(zz, RM); 3991 } 3992 } else { 3993 // q = a - z; 3994 APFloat q = a; 3995 Status |= q.subtract(z, RM); 3996 3997 // zz = q + c + (a - (q + z)) + aa + cc; 3998 // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies. 3999 auto zz = q; 4000 Status |= zz.add(c, RM); 4001 Status |= q.add(z, RM); 4002 Status |= q.subtract(a, RM); 4003 q.changeSign(); 4004 Status |= zz.add(q, RM); 4005 Status |= zz.add(aa, RM); 4006 Status |= zz.add(cc, RM); 4007 if (zz.isZero() && !zz.isNegative()) { 4008 Floats[0] = std::move(z); 4009 Floats[1].makeZero(/* Neg = */ false); 4010 return opOK; 4011 } 4012 Floats[0] = z; 4013 Status |= Floats[0].add(zz, RM); 4014 if (!Floats[0].isFinite()) { 4015 Floats[1].makeZero(/* Neg = */ false); 4016 return (opStatus)Status; 4017 } 4018 Floats[1] = std::move(z); 4019 Status |= Floats[1].subtract(Floats[0], RM); 4020 Status |= Floats[1].add(zz, RM); 4021 } 4022 return (opStatus)Status; 4023 } 4024 4025 APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS, 4026 const DoubleAPFloat &RHS, 4027 DoubleAPFloat &Out, 4028 roundingMode RM) { 4029 if (LHS.getCategory() == fcNaN) { 4030 Out = LHS; 4031 return opOK; 4032 } 4033 if (RHS.getCategory() == fcNaN) { 4034 Out = RHS; 4035 return opOK; 4036 } 4037 if (LHS.getCategory() == fcZero) { 4038 Out = RHS; 4039 return opOK; 4040 } 4041 if (RHS.getCategory() == fcZero) { 4042 Out = LHS; 4043 return opOK; 4044 } 4045 if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity && 4046 LHS.isNegative() != RHS.isNegative()) { 4047 Out.makeNaN(false, Out.isNegative(), nullptr); 4048 return opInvalidOp; 4049 } 4050 if (LHS.getCategory() == fcInfinity) { 4051 Out = LHS; 4052 return opOK; 4053 } 4054 if (RHS.getCategory() == fcInfinity) { 4055 Out = RHS; 4056 return opOK; 4057 } 4058 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal); 4059 4060 APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]), 4061 CC(RHS.Floats[1]); 4062 assert(&A.getSemantics() == &semIEEEdouble); 4063 assert(&AA.getSemantics() == &semIEEEdouble); 4064 assert(&C.getSemantics() == &semIEEEdouble); 4065 assert(&CC.getSemantics() == &semIEEEdouble); 4066 assert(&Out.Floats[0].getSemantics() == &semIEEEdouble); 4067 assert(&Out.Floats[1].getSemantics() == &semIEEEdouble); 4068 return Out.addImpl(A, AA, C, CC, RM); 4069 } 4070 4071 APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS, 4072 roundingMode RM) { 4073 return addWithSpecial(*this, RHS, *this, RM); 4074 } 4075 4076 APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS, 4077 roundingMode RM) { 4078 changeSign(); 4079 auto Ret = add(RHS, RM); 4080 changeSign(); 4081 return Ret; 4082 } 4083 4084 APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS, 4085 APFloat::roundingMode RM) { 4086 const auto &LHS = *this; 4087 auto &Out = *this; 4088 /* Interesting observation: For special categories, finding the lowest 4089 common ancestor of the following layered graph gives the correct 4090 return category: 4091 4092 NaN 4093 / \ 4094 Zero Inf 4095 \ / 4096 Normal 4097 4098 e.g. NaN * NaN = NaN 4099 Zero * Inf = NaN 4100 Normal * Zero = Zero 4101 Normal * Inf = Inf 4102 */ 4103 if (LHS.getCategory() == fcNaN) { 4104 Out = LHS; 4105 return opOK; 4106 } 4107 if (RHS.getCategory() == fcNaN) { 4108 Out = RHS; 4109 return opOK; 4110 } 4111 if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) || 4112 (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) { 4113 Out.makeNaN(false, false, nullptr); 4114 return opOK; 4115 } 4116 if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) { 4117 Out = LHS; 4118 return opOK; 4119 } 4120 if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) { 4121 Out = RHS; 4122 return opOK; 4123 } 4124 assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && 4125 "Special cases not handled exhaustively"); 4126 4127 int Status = opOK; 4128 APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1]; 4129 // t = a * c 4130 APFloat T = A; 4131 Status |= T.multiply(C, RM); 4132 if (!T.isFiniteNonZero()) { 4133 Floats[0] = T; 4134 Floats[1].makeZero(/* Neg = */ false); 4135 return (opStatus)Status; 4136 } 4137 4138 // tau = fmsub(a, c, t), that is -fmadd(-a, c, t). 4139 APFloat Tau = A; 4140 T.changeSign(); 4141 Status |= Tau.fusedMultiplyAdd(C, T, RM); 4142 T.changeSign(); 4143 { 4144 // v = a * d 4145 APFloat V = A; 4146 Status |= V.multiply(D, RM); 4147 // w = b * c 4148 APFloat W = B; 4149 Status |= W.multiply(C, RM); 4150 Status |= V.add(W, RM); 4151 // tau += v + w 4152 Status |= Tau.add(V, RM); 4153 } 4154 // u = t + tau 4155 APFloat U = T; 4156 Status |= U.add(Tau, RM); 4157 4158 Floats[0] = U; 4159 if (!U.isFinite()) { 4160 Floats[1].makeZero(/* Neg = */ false); 4161 } else { 4162 // Floats[1] = (t - u) + tau 4163 Status |= T.subtract(U, RM); 4164 Status |= T.add(Tau, RM); 4165 Floats[1] = T; 4166 } 4167 return (opStatus)Status; 4168 } 4169 4170 APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS, 4171 APFloat::roundingMode RM) { 4172 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4173 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4174 auto Ret = 4175 Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM); 4176 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4177 return Ret; 4178 } 4179 4180 APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) { 4181 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4182 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4183 auto Ret = 4184 Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4185 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4186 return Ret; 4187 } 4188 4189 APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) { 4190 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4191 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4192 auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4193 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4194 return Ret; 4195 } 4196 4197 APFloat::opStatus 4198 DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, 4199 const DoubleAPFloat &Addend, 4200 APFloat::roundingMode RM) { 4201 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4202 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4203 auto Ret = Tmp.fusedMultiplyAdd( 4204 APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()), 4205 APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM); 4206 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4207 return Ret; 4208 } 4209 4210 APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) { 4211 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4212 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4213 auto Ret = Tmp.roundToIntegral(RM); 4214 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4215 return Ret; 4216 } 4217 4218 void DoubleAPFloat::changeSign() { 4219 Floats[0].changeSign(); 4220 Floats[1].changeSign(); 4221 } 4222 4223 APFloat::cmpResult 4224 DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const { 4225 auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]); 4226 if (Result != cmpEqual) 4227 return Result; 4228 Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]); 4229 if (Result == cmpLessThan || Result == cmpGreaterThan) { 4230 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative(); 4231 auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative(); 4232 if (Against && !RHSAgainst) 4233 return cmpLessThan; 4234 if (!Against && RHSAgainst) 4235 return cmpGreaterThan; 4236 if (!Against && !RHSAgainst) 4237 return Result; 4238 if (Against && RHSAgainst) 4239 return (cmpResult)(cmpLessThan + cmpGreaterThan - Result); 4240 } 4241 return Result; 4242 } 4243 4244 APFloat::fltCategory DoubleAPFloat::getCategory() const { 4245 return Floats[0].getCategory(); 4246 } 4247 4248 bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); } 4249 4250 void DoubleAPFloat::makeInf(bool Neg) { 4251 Floats[0].makeInf(Neg); 4252 Floats[1].makeZero(/* Neg = */ false); 4253 } 4254 4255 void DoubleAPFloat::makeZero(bool Neg) { 4256 Floats[0].makeZero(Neg); 4257 Floats[1].makeZero(/* Neg = */ false); 4258 } 4259 4260 void DoubleAPFloat::makeLargest(bool Neg) { 4261 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4262 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull)); 4263 Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull)); 4264 if (Neg) 4265 changeSign(); 4266 } 4267 4268 void DoubleAPFloat::makeSmallest(bool Neg) { 4269 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4270 Floats[0].makeSmallest(Neg); 4271 Floats[1].makeZero(/* Neg = */ false); 4272 } 4273 4274 void DoubleAPFloat::makeSmallestNormalized(bool Neg) { 4275 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4276 Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull)); 4277 if (Neg) 4278 Floats[0].changeSign(); 4279 Floats[1].makeZero(/* Neg = */ false); 4280 } 4281 4282 void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) { 4283 Floats[0].makeNaN(SNaN, Neg, fill); 4284 Floats[1].makeZero(/* Neg = */ false); 4285 } 4286 4287 APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const { 4288 auto Result = Floats[0].compare(RHS.Floats[0]); 4289 // |Float[0]| > |Float[1]| 4290 if (Result == APFloat::cmpEqual) 4291 return Floats[1].compare(RHS.Floats[1]); 4292 return Result; 4293 } 4294 4295 bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const { 4296 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) && 4297 Floats[1].bitwiseIsEqual(RHS.Floats[1]); 4298 } 4299 4300 hash_code hash_value(const DoubleAPFloat &Arg) { 4301 if (Arg.Floats) 4302 return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1])); 4303 return hash_combine(Arg.Semantics); 4304 } 4305 4306 APInt DoubleAPFloat::bitcastToAPInt() const { 4307 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4308 uint64_t Data[] = { 4309 Floats[0].bitcastToAPInt().getRawData()[0], 4310 Floats[1].bitcastToAPInt().getRawData()[0], 4311 }; 4312 return APInt(128, 2, Data); 4313 } 4314 4315 APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S, 4316 roundingMode RM) { 4317 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4318 APFloat Tmp(semPPCDoubleDoubleLegacy); 4319 auto Ret = Tmp.convertFromString(S, RM); 4320 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4321 return Ret; 4322 } 4323 4324 APFloat::opStatus DoubleAPFloat::next(bool nextDown) { 4325 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4326 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4327 auto Ret = Tmp.next(nextDown); 4328 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4329 return Ret; 4330 } 4331 4332 APFloat::opStatus 4333 DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input, 4334 unsigned int Width, bool IsSigned, 4335 roundingMode RM, bool *IsExact) const { 4336 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4337 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4338 .convertToInteger(Input, Width, IsSigned, RM, IsExact); 4339 } 4340 4341 APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input, 4342 bool IsSigned, 4343 roundingMode RM) { 4344 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4345 APFloat Tmp(semPPCDoubleDoubleLegacy); 4346 auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM); 4347 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4348 return Ret; 4349 } 4350 4351 APFloat::opStatus 4352 DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input, 4353 unsigned int InputSize, 4354 bool IsSigned, roundingMode RM) { 4355 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4356 APFloat Tmp(semPPCDoubleDoubleLegacy); 4357 auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM); 4358 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4359 return Ret; 4360 } 4361 4362 APFloat::opStatus 4363 DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input, 4364 unsigned int InputSize, 4365 bool IsSigned, roundingMode RM) { 4366 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4367 APFloat Tmp(semPPCDoubleDoubleLegacy); 4368 auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM); 4369 *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4370 return Ret; 4371 } 4372 4373 unsigned int DoubleAPFloat::convertToHexString(char *DST, 4374 unsigned int HexDigits, 4375 bool UpperCase, 4376 roundingMode RM) const { 4377 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4378 return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4379 .convertToHexString(DST, HexDigits, UpperCase, RM); 4380 } 4381 4382 bool DoubleAPFloat::isDenormal() const { 4383 return getCategory() == fcNormal && 4384 (Floats[0].isDenormal() || Floats[1].isDenormal() || 4385 // (double)(Hi + Lo) == Hi defines a normal number. 4386 Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual); 4387 } 4388 4389 bool DoubleAPFloat::isSmallest() const { 4390 if (getCategory() != fcNormal) 4391 return false; 4392 DoubleAPFloat Tmp(*this); 4393 Tmp.makeSmallest(this->isNegative()); 4394 return Tmp.compare(*this) == cmpEqual; 4395 } 4396 4397 bool DoubleAPFloat::isLargest() const { 4398 if (getCategory() != fcNormal) 4399 return false; 4400 DoubleAPFloat Tmp(*this); 4401 Tmp.makeLargest(this->isNegative()); 4402 return Tmp.compare(*this) == cmpEqual; 4403 } 4404 4405 bool DoubleAPFloat::isInteger() const { 4406 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4407 return Floats[0].isInteger() && Floats[1].isInteger(); 4408 } 4409 4410 void DoubleAPFloat::toString(SmallVectorImpl<char> &Str, 4411 unsigned FormatPrecision, 4412 unsigned FormatMaxPadding, 4413 bool TruncateZero) const { 4414 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4415 APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4416 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero); 4417 } 4418 4419 bool DoubleAPFloat::getExactInverse(APFloat *inv) const { 4420 assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4421 APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4422 if (!inv) 4423 return Tmp.getExactInverse(nullptr); 4424 APFloat Inv(semPPCDoubleDoubleLegacy); 4425 auto Ret = Tmp.getExactInverse(&Inv); 4426 *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt()); 4427 return Ret; 4428 } 4429 4430 DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) { 4431 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4432 return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM), 4433 scalbn(Arg.Floats[1], Exp, RM)); 4434 } 4435 4436 DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp, 4437 APFloat::roundingMode RM) { 4438 assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4439 APFloat First = frexp(Arg.Floats[0], Exp, RM); 4440 APFloat Second = Arg.Floats[1]; 4441 if (Arg.getCategory() == APFloat::fcNormal) 4442 Second = scalbn(Second, -Exp, RM); 4443 return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second)); 4444 } 4445 4446 } // End detail namespace 4447 4448 APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) { 4449 if (usesLayout<IEEEFloat>(Semantics)) { 4450 new (&IEEE) IEEEFloat(std::move(F)); 4451 return; 4452 } 4453 if (usesLayout<DoubleAPFloat>(Semantics)) { 4454 const fltSemantics& S = F.getSemantics(); 4455 new (&Double) 4456 DoubleAPFloat(Semantics, APFloat(std::move(F), S), 4457 APFloat(semIEEEdouble)); 4458 return; 4459 } 4460 llvm_unreachable("Unexpected semantics"); 4461 } 4462 4463 APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) { 4464 APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); 4465 } 4466 4467 hash_code hash_value(const APFloat &Arg) { 4468 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) 4469 return hash_value(Arg.U.IEEE); 4470 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) 4471 return hash_value(Arg.U.Double); 4472 llvm_unreachable("Unexpected semantics"); 4473 } 4474 4475 APFloat::APFloat(const fltSemantics &Semantics, StringRef S) 4476 : APFloat(Semantics) { 4477 convertFromString(S, rmNearestTiesToEven); 4478 } 4479 4480 APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, 4481 roundingMode RM, bool *losesInfo) { 4482 if (&getSemantics() == &ToSemantics) { 4483 *losesInfo = false; 4484 return opOK; 4485 } 4486 if (usesLayout<IEEEFloat>(getSemantics()) && 4487 usesLayout<IEEEFloat>(ToSemantics)) 4488 return U.IEEE.convert(ToSemantics, RM, losesInfo); 4489 if (usesLayout<IEEEFloat>(getSemantics()) && 4490 usesLayout<DoubleAPFloat>(ToSemantics)) { 4491 assert(&ToSemantics == &semPPCDoubleDouble); 4492 auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo); 4493 *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt()); 4494 return Ret; 4495 } 4496 if (usesLayout<DoubleAPFloat>(getSemantics()) && 4497 usesLayout<IEEEFloat>(ToSemantics)) { 4498 auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo); 4499 *this = APFloat(std::move(getIEEE()), ToSemantics); 4500 return Ret; 4501 } 4502 llvm_unreachable("Unexpected semantics"); 4503 } 4504 4505 APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) { 4506 if (isIEEE) { 4507 switch (BitWidth) { 4508 case 16: 4509 return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth)); 4510 case 32: 4511 return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth)); 4512 case 64: 4513 return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth)); 4514 case 80: 4515 return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth)); 4516 case 128: 4517 return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth)); 4518 default: 4519 llvm_unreachable("Unknown floating bit width"); 4520 } 4521 } else { 4522 assert(BitWidth == 128); 4523 return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth)); 4524 } 4525 } 4526 4527 void APFloat::print(raw_ostream &OS) const { 4528 SmallVector<char, 16> Buffer; 4529 toString(Buffer); 4530 OS << Buffer << "\n"; 4531 } 4532 4533 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4534 LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); } 4535 #endif 4536 4537 void APFloat::Profile(FoldingSetNodeID &NID) const { 4538 NID.Add(bitcastToAPInt()); 4539 } 4540 4541 /* Same as convertToInteger(integerPart*, ...), except the result is returned in 4542 an APSInt, whose initial bit-width and signed-ness are used to determine the 4543 precision of the conversion. 4544 */ 4545 APFloat::opStatus APFloat::convertToInteger(APSInt &result, 4546 roundingMode rounding_mode, 4547 bool *isExact) const { 4548 unsigned bitWidth = result.getBitWidth(); 4549 SmallVector<uint64_t, 4> parts(result.getNumWords()); 4550 opStatus status = convertToInteger(parts, bitWidth, result.isSigned(), 4551 rounding_mode, isExact); 4552 // Keeps the original signed-ness. 4553 result = APInt(bitWidth, parts); 4554 return status; 4555 } 4556 4557 } // End llvm namespace 4558 4559 #undef APFLOAT_DISPATCH_ON_SEMANTICS 4560