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