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