LLVM API Documentation

APFloat.cpp
Go to the documentation of this file.
00001 //===-- APFloat.cpp - Implement APFloat class -----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a class to represent arbitrary precision floating
00011 // point values and provide a variety of arithmetic operations on them.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/ADT/APFloat.h"
00016 #include "llvm/ADT/APSInt.h"
00017 #include "llvm/ADT/FoldingSet.h"
00018 #include "llvm/ADT/Hashing.h"
00019 #include "llvm/ADT/StringExtras.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include "llvm/Support/ErrorHandling.h"
00022 #include "llvm/Support/MathExtras.h"
00023 #include <cstring>
00024 #include <limits.h>
00025 
00026 using namespace llvm;
00027 
00028 /// A macro used to combine two fcCategory enums into one key which can be used
00029 /// in a switch statement to classify how the interaction of two APFloat's
00030 /// categories affects an operation.
00031 ///
00032 /// TODO: If clang source code is ever allowed to use constexpr in its own
00033 /// codebase, change this into a static inline function.
00034 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
00035 
00036 /* Assumed in hexadecimal significand parsing, and conversion to
00037    hexadecimal strings.  */
00038 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
00039 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
00040 
00041 namespace llvm {
00042 
00043   /* Represents floating point arithmetic semantics.  */
00044   struct fltSemantics {
00045     /* The largest E such that 2^E is representable; this matches the
00046        definition of IEEE 754.  */
00047     APFloat::ExponentType maxExponent;
00048 
00049     /* The smallest E such that 2^E is a normalized number; this
00050        matches the definition of IEEE 754.  */
00051     APFloat::ExponentType minExponent;
00052 
00053     /* Number of bits in the significand.  This includes the integer
00054        bit.  */
00055     unsigned int precision;
00056   };
00057 
00058   const fltSemantics APFloat::IEEEhalf = { 15, -14, 11 };
00059   const fltSemantics APFloat::IEEEsingle = { 127, -126, 24 };
00060   const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53 };
00061   const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113 };
00062   const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64 };
00063   const fltSemantics APFloat::Bogus = { 0, 0, 0 };
00064 
00065   /* The PowerPC format consists of two doubles.  It does not map cleanly
00066      onto the usual format above.  It is approximated using twice the
00067      mantissa bits.  Note that for exponents near the double minimum,
00068      we no longer can represent the full 106 mantissa bits, so those
00069      will be treated as denormal numbers.
00070 
00071      FIXME: While this approximation is equivalent to what GCC uses for
00072      compile-time arithmetic on PPC double-double numbers, it is not able
00073      to represent all possible values held by a PPC double-double number,
00074      for example: (long double) 1.0 + (long double) 0x1p-106
00075      Should this be replaced by a full emulation of PPC double-double?  */
00076   const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53 };
00077 
00078   /* A tight upper bound on number of parts required to hold the value
00079      pow(5, power) is
00080 
00081        power * 815 / (351 * integerPartWidth) + 1
00082 
00083      However, whilst the result may require only this many parts,
00084      because we are multiplying two values to get it, the
00085      multiplication may require an extra part with the excess part
00086      being zero (consider the trivial case of 1 * 1, tcFullMultiply
00087      requires two parts to hold the single-part result).  So we add an
00088      extra one to guarantee enough space whilst multiplying.  */
00089   const unsigned int maxExponent = 16383;
00090   const unsigned int maxPrecision = 113;
00091   const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
00092   const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
00093                                                 / (351 * integerPartWidth));
00094 }
00095 
00096 /* A bunch of private, handy routines.  */
00097 
00098 static inline unsigned int
00099 partCountForBits(unsigned int bits)
00100 {
00101   return ((bits) + integerPartWidth - 1) / integerPartWidth;
00102 }
00103 
00104 /* Returns 0U-9U.  Return values >= 10U are not digits.  */
00105 static inline unsigned int
00106 decDigitValue(unsigned int c)
00107 {
00108   return c - '0';
00109 }
00110 
00111 /* Return the value of a decimal exponent of the form
00112    [+-]ddddddd.
00113 
00114    If the exponent overflows, returns a large exponent with the
00115    appropriate sign.  */
00116 static int
00117 readExponent(StringRef::iterator begin, StringRef::iterator end)
00118 {
00119   bool isNegative;
00120   unsigned int absExponent;
00121   const unsigned int overlargeExponent = 24000;  /* FIXME.  */
00122   StringRef::iterator p = begin;
00123 
00124   assert(p != end && "Exponent has no digits");
00125 
00126   isNegative = (*p == '-');
00127   if (*p == '-' || *p == '+') {
00128     p++;
00129     assert(p != end && "Exponent has no digits");
00130   }
00131 
00132   absExponent = decDigitValue(*p++);
00133   assert(absExponent < 10U && "Invalid character in exponent");
00134 
00135   for (; p != end; ++p) {
00136     unsigned int value;
00137 
00138     value = decDigitValue(*p);
00139     assert(value < 10U && "Invalid character in exponent");
00140 
00141     value += absExponent * 10;
00142     if (absExponent >= overlargeExponent) {
00143       absExponent = overlargeExponent;
00144       p = end;  /* outwit assert below */
00145       break;
00146     }
00147     absExponent = value;
00148   }
00149 
00150   assert(p == end && "Invalid exponent in exponent");
00151 
00152   if (isNegative)
00153     return -(int) absExponent;
00154   else
00155     return (int) absExponent;
00156 }
00157 
00158 /* This is ugly and needs cleaning up, but I don't immediately see
00159    how whilst remaining safe.  */
00160 static int
00161 totalExponent(StringRef::iterator p, StringRef::iterator end,
00162               int exponentAdjustment)
00163 {
00164   int unsignedExponent;
00165   bool negative, overflow;
00166   int exponent = 0;
00167 
00168   assert(p != end && "Exponent has no digits");
00169 
00170   negative = *p == '-';
00171   if (*p == '-' || *p == '+') {
00172     p++;
00173     assert(p != end && "Exponent has no digits");
00174   }
00175 
00176   unsignedExponent = 0;
00177   overflow = false;
00178   for (; p != end; ++p) {
00179     unsigned int value;
00180 
00181     value = decDigitValue(*p);
00182     assert(value < 10U && "Invalid character in exponent");
00183 
00184     unsignedExponent = unsignedExponent * 10 + value;
00185     if (unsignedExponent > 32767) {
00186       overflow = true;
00187       break;
00188     }
00189   }
00190 
00191   if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
00192     overflow = true;
00193 
00194   if (!overflow) {
00195     exponent = unsignedExponent;
00196     if (negative)
00197       exponent = -exponent;
00198     exponent += exponentAdjustment;
00199     if (exponent > 32767 || exponent < -32768)
00200       overflow = true;
00201   }
00202 
00203   if (overflow)
00204     exponent = negative ? -32768: 32767;
00205 
00206   return exponent;
00207 }
00208 
00209 static StringRef::iterator
00210 skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
00211                            StringRef::iterator *dot)
00212 {
00213   StringRef::iterator p = begin;
00214   *dot = end;
00215   while (p != end && *p == '0')
00216     p++;
00217 
00218   if (p != end && *p == '.') {
00219     *dot = p++;
00220 
00221     assert(end - begin != 1 && "Significand has no digits");
00222 
00223     while (p != end && *p == '0')
00224       p++;
00225   }
00226 
00227   return p;
00228 }
00229 
00230 /* Given a normal decimal floating point number of the form
00231 
00232      dddd.dddd[eE][+-]ddd
00233 
00234    where the decimal point and exponent are optional, fill out the
00235    structure D.  Exponent is appropriate if the significand is
00236    treated as an integer, and normalizedExponent if the significand
00237    is taken to have the decimal point after a single leading
00238    non-zero digit.
00239 
00240    If the value is zero, V->firstSigDigit points to a non-digit, and
00241    the return exponent is zero.
00242 */
00243 struct decimalInfo {
00244   const char *firstSigDigit;
00245   const char *lastSigDigit;
00246   int exponent;
00247   int normalizedExponent;
00248 };
00249 
00250 static void
00251 interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
00252                  decimalInfo *D)
00253 {
00254   StringRef::iterator dot = end;
00255   StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
00256 
00257   D->firstSigDigit = p;
00258   D->exponent = 0;
00259   D->normalizedExponent = 0;
00260 
00261   for (; p != end; ++p) {
00262     if (*p == '.') {
00263       assert(dot == end && "String contains multiple dots");
00264       dot = p++;
00265       if (p == end)
00266         break;
00267     }
00268     if (decDigitValue(*p) >= 10U)
00269       break;
00270   }
00271 
00272   if (p != end) {
00273     assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
00274     assert(p != begin && "Significand has no digits");
00275     assert((dot == end || p - begin != 1) && "Significand has no digits");
00276 
00277     /* p points to the first non-digit in the string */
00278     D->exponent = readExponent(p + 1, end);
00279 
00280     /* Implied decimal point?  */
00281     if (dot == end)
00282       dot = p;
00283   }
00284 
00285   /* If number is all zeroes accept any exponent.  */
00286   if (p != D->firstSigDigit) {
00287     /* Drop insignificant trailing zeroes.  */
00288     if (p != begin) {
00289       do
00290         do
00291           p--;
00292         while (p != begin && *p == '0');
00293       while (p != begin && *p == '.');
00294     }
00295 
00296     /* Adjust the exponents for any decimal point.  */
00297     D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
00298     D->normalizedExponent = (D->exponent +
00299               static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
00300                                       - (dot > D->firstSigDigit && dot < p)));
00301   }
00302 
00303   D->lastSigDigit = p;
00304 }
00305 
00306 /* Return the trailing fraction of a hexadecimal number.
00307    DIGITVALUE is the first hex digit of the fraction, P points to
00308    the next digit.  */
00309 static lostFraction
00310 trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
00311                             unsigned int digitValue)
00312 {
00313   unsigned int hexDigit;
00314 
00315   /* If the first trailing digit isn't 0 or 8 we can work out the
00316      fraction immediately.  */
00317   if (digitValue > 8)
00318     return lfMoreThanHalf;
00319   else if (digitValue < 8 && digitValue > 0)
00320     return lfLessThanHalf;
00321 
00322   // Otherwise we need to find the first non-zero digit.
00323   while (p != end && (*p == '0' || *p == '.'))
00324     p++;
00325 
00326   assert(p != end && "Invalid trailing hexadecimal fraction!");
00327 
00328   hexDigit = hexDigitValue(*p);
00329 
00330   /* If we ran off the end it is exactly zero or one-half, otherwise
00331      a little more.  */
00332   if (hexDigit == -1U)
00333     return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
00334   else
00335     return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
00336 }
00337 
00338 /* Return the fraction lost were a bignum truncated losing the least
00339    significant BITS bits.  */
00340 static lostFraction
00341 lostFractionThroughTruncation(const integerPart *parts,
00342                               unsigned int partCount,
00343                               unsigned int bits)
00344 {
00345   unsigned int lsb;
00346 
00347   lsb = APInt::tcLSB(parts, partCount);
00348 
00349   /* Note this is guaranteed true if bits == 0, or LSB == -1U.  */
00350   if (bits <= lsb)
00351     return lfExactlyZero;
00352   if (bits == lsb + 1)
00353     return lfExactlyHalf;
00354   if (bits <= partCount * integerPartWidth &&
00355       APInt::tcExtractBit(parts, bits - 1))
00356     return lfMoreThanHalf;
00357 
00358   return lfLessThanHalf;
00359 }
00360 
00361 /* Shift DST right BITS bits noting lost fraction.  */
00362 static lostFraction
00363 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
00364 {
00365   lostFraction lost_fraction;
00366 
00367   lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
00368 
00369   APInt::tcShiftRight(dst, parts, bits);
00370 
00371   return lost_fraction;
00372 }
00373 
00374 /* Combine the effect of two lost fractions.  */
00375 static lostFraction
00376 combineLostFractions(lostFraction moreSignificant,
00377                      lostFraction lessSignificant)
00378 {
00379   if (lessSignificant != lfExactlyZero) {
00380     if (moreSignificant == lfExactlyZero)
00381       moreSignificant = lfLessThanHalf;
00382     else if (moreSignificant == lfExactlyHalf)
00383       moreSignificant = lfMoreThanHalf;
00384   }
00385 
00386   return moreSignificant;
00387 }
00388 
00389 /* The error from the true value, in half-ulps, on multiplying two
00390    floating point numbers, which differ from the value they
00391    approximate by at most HUE1 and HUE2 half-ulps, is strictly less
00392    than the returned value.
00393 
00394    See "How to Read Floating Point Numbers Accurately" by William D
00395    Clinger.  */
00396 static unsigned int
00397 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
00398 {
00399   assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
00400 
00401   if (HUerr1 + HUerr2 == 0)
00402     return inexactMultiply * 2;  /* <= inexactMultiply half-ulps.  */
00403   else
00404     return inexactMultiply + 2 * (HUerr1 + HUerr2);
00405 }
00406 
00407 /* The number of ulps from the boundary (zero, or half if ISNEAREST)
00408    when the least significant BITS are truncated.  BITS cannot be
00409    zero.  */
00410 static integerPart
00411 ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
00412 {
00413   unsigned int count, partBits;
00414   integerPart part, boundary;
00415 
00416   assert(bits != 0);
00417 
00418   bits--;
00419   count = bits / integerPartWidth;
00420   partBits = bits % integerPartWidth + 1;
00421 
00422   part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
00423 
00424   if (isNearest)
00425     boundary = (integerPart) 1 << (partBits - 1);
00426   else
00427     boundary = 0;
00428 
00429   if (count == 0) {
00430     if (part - boundary <= boundary - part)
00431       return part - boundary;
00432     else
00433       return boundary - part;
00434   }
00435 
00436   if (part == boundary) {
00437     while (--count)
00438       if (parts[count])
00439         return ~(integerPart) 0; /* A lot.  */
00440 
00441     return parts[0];
00442   } else if (part == boundary - 1) {
00443     while (--count)
00444       if (~parts[count])
00445         return ~(integerPart) 0; /* A lot.  */
00446 
00447     return -parts[0];
00448   }
00449 
00450   return ~(integerPart) 0; /* A lot.  */
00451 }
00452 
00453 /* Place pow(5, power) in DST, and return the number of parts used.
00454    DST must be at least one part larger than size of the answer.  */
00455 static unsigned int
00456 powerOf5(integerPart *dst, unsigned int power)
00457 {
00458   static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
00459                                                   15625, 78125 };
00460   integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
00461   pow5s[0] = 78125 * 5;
00462 
00463   unsigned int partsCount[16] = { 1 };
00464   integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
00465   unsigned int result;
00466   assert(power <= maxExponent);
00467 
00468   p1 = dst;
00469   p2 = scratch;
00470 
00471   *p1 = firstEightPowers[power & 7];
00472   power >>= 3;
00473 
00474   result = 1;
00475   pow5 = pow5s;
00476 
00477   for (unsigned int n = 0; power; power >>= 1, n++) {
00478     unsigned int pc;
00479 
00480     pc = partsCount[n];
00481 
00482     /* Calculate pow(5,pow(2,n+3)) if we haven't yet.  */
00483     if (pc == 0) {
00484       pc = partsCount[n - 1];
00485       APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
00486       pc *= 2;
00487       if (pow5[pc - 1] == 0)
00488         pc--;
00489       partsCount[n] = pc;
00490     }
00491 
00492     if (power & 1) {
00493       integerPart *tmp;
00494 
00495       APInt::tcFullMultiply(p2, p1, pow5, result, pc);
00496       result += pc;
00497       if (p2[result - 1] == 0)
00498         result--;
00499 
00500       /* Now result is in p1 with partsCount parts and p2 is scratch
00501          space.  */
00502       tmp = p1, p1 = p2, p2 = tmp;
00503     }
00504 
00505     pow5 += pc;
00506   }
00507 
00508   if (p1 != dst)
00509     APInt::tcAssign(dst, p1, result);
00510 
00511   return result;
00512 }
00513 
00514 /* Zero at the end to avoid modular arithmetic when adding one; used
00515    when rounding up during hexadecimal output.  */
00516 static const char hexDigitsLower[] = "0123456789abcdef0";
00517 static const char hexDigitsUpper[] = "0123456789ABCDEF0";
00518 static const char infinityL[] = "infinity";
00519 static const char infinityU[] = "INFINITY";
00520 static const char NaNL[] = "nan";
00521 static const char NaNU[] = "NAN";
00522 
00523 /* Write out an integerPart in hexadecimal, starting with the most
00524    significant nibble.  Write out exactly COUNT hexdigits, return
00525    COUNT.  */
00526 static unsigned int
00527 partAsHex (char *dst, integerPart part, unsigned int count,
00528            const char *hexDigitChars)
00529 {
00530   unsigned int result = count;
00531 
00532   assert(count != 0 && count <= integerPartWidth / 4);
00533 
00534   part >>= (integerPartWidth - 4 * count);
00535   while (count--) {
00536     dst[count] = hexDigitChars[part & 0xf];
00537     part >>= 4;
00538   }
00539 
00540   return result;
00541 }
00542 
00543 /* Write out an unsigned decimal integer.  */
00544 static char *
00545 writeUnsignedDecimal (char *dst, unsigned int n)
00546 {
00547   char buff[40], *p;
00548 
00549   p = buff;
00550   do
00551     *p++ = '0' + n % 10;
00552   while (n /= 10);
00553 
00554   do
00555     *dst++ = *--p;
00556   while (p != buff);
00557 
00558   return dst;
00559 }
00560 
00561 /* Write out a signed decimal integer.  */
00562 static char *
00563 writeSignedDecimal (char *dst, int value)
00564 {
00565   if (value < 0) {
00566     *dst++ = '-';
00567     dst = writeUnsignedDecimal(dst, -(unsigned) value);
00568   } else
00569     dst = writeUnsignedDecimal(dst, value);
00570 
00571   return dst;
00572 }
00573 
00574 /* Constructors.  */
00575 void
00576 APFloat::initialize(const fltSemantics *ourSemantics)
00577 {
00578   unsigned int count;
00579 
00580   semantics = ourSemantics;
00581   count = partCount();
00582   if (count > 1)
00583     significand.parts = new integerPart[count];
00584 }
00585 
00586 void
00587 APFloat::freeSignificand()
00588 {
00589   if (needsCleanup())
00590     delete [] significand.parts;
00591 }
00592 
00593 void
00594 APFloat::assign(const APFloat &rhs)
00595 {
00596   assert(semantics == rhs.semantics);
00597 
00598   sign = rhs.sign;
00599   category = rhs.category;
00600   exponent = rhs.exponent;
00601   if (isFiniteNonZero() || category == fcNaN)
00602     copySignificand(rhs);
00603 }
00604 
00605 void
00606 APFloat::copySignificand(const APFloat &rhs)
00607 {
00608   assert(isFiniteNonZero() || category == fcNaN);
00609   assert(rhs.partCount() >= partCount());
00610 
00611   APInt::tcAssign(significandParts(), rhs.significandParts(),
00612                   partCount());
00613 }
00614 
00615 /* Make this number a NaN, with an arbitrary but deterministic value
00616    for the significand.  If double or longer, this is a signalling NaN,
00617    which may not be ideal.  If float, this is QNaN(0).  */
00618 void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
00619 {
00620   category = fcNaN;
00621   sign = Negative;
00622 
00623   integerPart *significand = significandParts();
00624   unsigned numParts = partCount();
00625 
00626   // Set the significand bits to the fill.
00627   if (!fill || fill->getNumWords() < numParts)
00628     APInt::tcSet(significand, 0, numParts);
00629   if (fill) {
00630     APInt::tcAssign(significand, fill->getRawData(),
00631                     std::min(fill->getNumWords(), numParts));
00632 
00633     // Zero out the excess bits of the significand.
00634     unsigned bitsToPreserve = semantics->precision - 1;
00635     unsigned part = bitsToPreserve / 64;
00636     bitsToPreserve %= 64;
00637     significand[part] &= ((1ULL << bitsToPreserve) - 1);
00638     for (part++; part != numParts; ++part)
00639       significand[part] = 0;
00640   }
00641 
00642   unsigned QNaNBit = semantics->precision - 2;
00643 
00644   if (SNaN) {
00645     // We always have to clear the QNaN bit to make it an SNaN.
00646     APInt::tcClearBit(significand, QNaNBit);
00647 
00648     // If there are no bits set in the payload, we have to set
00649     // *something* to make it a NaN instead of an infinity;
00650     // conventionally, this is the next bit down from the QNaN bit.
00651     if (APInt::tcIsZero(significand, numParts))
00652       APInt::tcSetBit(significand, QNaNBit - 1);
00653   } else {
00654     // We always have to set the QNaN bit to make it a QNaN.
00655     APInt::tcSetBit(significand, QNaNBit);
00656   }
00657 
00658   // For x87 extended precision, we want to make a NaN, not a
00659   // pseudo-NaN.  Maybe we should expose the ability to make
00660   // pseudo-NaNs?
00661   if (semantics == &APFloat::x87DoubleExtended)
00662     APInt::tcSetBit(significand, QNaNBit + 1);
00663 }
00664 
00665 APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
00666                          const APInt *fill) {
00667   APFloat value(Sem, uninitialized);
00668   value.makeNaN(SNaN, Negative, fill);
00669   return value;
00670 }
00671 
00672 APFloat &
00673 APFloat::operator=(const APFloat &rhs)
00674 {
00675   if (this != &rhs) {
00676     if (semantics != rhs.semantics) {
00677       freeSignificand();
00678       initialize(rhs.semantics);
00679     }
00680     assign(rhs);
00681   }
00682 
00683   return *this;
00684 }
00685 
00686 APFloat &
00687 APFloat::operator=(APFloat &&rhs) {
00688   freeSignificand();
00689 
00690   semantics = rhs.semantics;
00691   significand = rhs.significand;
00692   exponent = rhs.exponent;
00693   category = rhs.category;
00694   sign = rhs.sign;
00695 
00696   rhs.semantics = &Bogus;
00697   return *this;
00698 }
00699 
00700 bool
00701 APFloat::isDenormal() const {
00702   return isFiniteNonZero() && (exponent == semantics->minExponent) &&
00703          (APInt::tcExtractBit(significandParts(), 
00704                               semantics->precision - 1) == 0);
00705 }
00706 
00707 bool
00708 APFloat::isSmallest() const {
00709   // The smallest number by magnitude in our format will be the smallest
00710   // denormal, i.e. the floating point number with exponent being minimum
00711   // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
00712   return isFiniteNonZero() && exponent == semantics->minExponent &&
00713     significandMSB() == 0;
00714 }
00715 
00716 bool APFloat::isSignificandAllOnes() const {
00717   // Test if the significand excluding the integral bit is all ones. This allows
00718   // us to test for binade boundaries.
00719   const integerPart *Parts = significandParts();
00720   const unsigned PartCount = partCount();
00721   for (unsigned i = 0; i < PartCount - 1; i++)
00722     if (~Parts[i])
00723       return false;
00724 
00725   // Set the unused high bits to all ones when we compare.
00726   const unsigned NumHighBits =
00727     PartCount*integerPartWidth - semantics->precision + 1;
00728   assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
00729          "fill than integerPartWidth");
00730   const integerPart HighBitFill =
00731     ~integerPart(0) << (integerPartWidth - NumHighBits);
00732   if (~(Parts[PartCount - 1] | HighBitFill))
00733     return false;
00734 
00735   return true;
00736 }
00737 
00738 bool APFloat::isSignificandAllZeros() const {
00739   // Test if the significand excluding the integral bit is all zeros. This
00740   // allows us to test for binade boundaries.
00741   const integerPart *Parts = significandParts();
00742   const unsigned PartCount = partCount();
00743 
00744   for (unsigned i = 0; i < PartCount - 1; i++)
00745     if (Parts[i])
00746       return false;
00747 
00748   const unsigned NumHighBits =
00749     PartCount*integerPartWidth - semantics->precision + 1;
00750   assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
00751          "clear than integerPartWidth");
00752   const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
00753 
00754   if (Parts[PartCount - 1] & HighBitMask)
00755     return false;
00756 
00757   return true;
00758 }
00759 
00760 bool
00761 APFloat::isLargest() const {
00762   // The largest number by magnitude in our format will be the floating point
00763   // number with maximum exponent and with significand that is all ones.
00764   return isFiniteNonZero() && exponent == semantics->maxExponent
00765     && isSignificandAllOnes();
00766 }
00767 
00768 bool
00769 APFloat::bitwiseIsEqual(const APFloat &rhs) const {
00770   if (this == &rhs)
00771     return true;
00772   if (semantics != rhs.semantics ||
00773       category != rhs.category ||
00774       sign != rhs.sign)
00775     return false;
00776   if (category==fcZero || category==fcInfinity)
00777     return true;
00778   else if (isFiniteNonZero() && exponent!=rhs.exponent)
00779     return false;
00780   else {
00781     int i= partCount();
00782     const integerPart* p=significandParts();
00783     const integerPart* q=rhs.significandParts();
00784     for (; i>0; i--, p++, q++) {
00785       if (*p != *q)
00786         return false;
00787     }
00788     return true;
00789   }
00790 }
00791 
00792 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
00793   initialize(&ourSemantics);
00794   sign = 0;
00795   category = fcNormal;
00796   zeroSignificand();
00797   exponent = ourSemantics.precision - 1;
00798   significandParts()[0] = value;
00799   normalize(rmNearestTiesToEven, lfExactlyZero);
00800 }
00801 
00802 APFloat::APFloat(const fltSemantics &ourSemantics) {
00803   initialize(&ourSemantics);
00804   category = fcZero;
00805   sign = false;
00806 }
00807 
00808 APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
00809   // Allocates storage if necessary but does not initialize it.
00810   initialize(&ourSemantics);
00811 }
00812 
00813 APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
00814   initialize(&ourSemantics);
00815   convertFromString(text, rmNearestTiesToEven);
00816 }
00817 
00818 APFloat::APFloat(const APFloat &rhs) {
00819   initialize(rhs.semantics);
00820   assign(rhs);
00821 }
00822 
00823 APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
00824   *this = std::move(rhs);
00825 }
00826 
00827 APFloat::~APFloat()
00828 {
00829   freeSignificand();
00830 }
00831 
00832 // Profile - This method 'profiles' an APFloat for use with FoldingSet.
00833 void APFloat::Profile(FoldingSetNodeID& ID) const {
00834   ID.Add(bitcastToAPInt());
00835 }
00836 
00837 unsigned int
00838 APFloat::partCount() const
00839 {
00840   return partCountForBits(semantics->precision + 1);
00841 }
00842 
00843 unsigned int
00844 APFloat::semanticsPrecision(const fltSemantics &semantics)
00845 {
00846   return semantics.precision;
00847 }
00848 
00849 const integerPart *
00850 APFloat::significandParts() const
00851 {
00852   return const_cast<APFloat *>(this)->significandParts();
00853 }
00854 
00855 integerPart *
00856 APFloat::significandParts()
00857 {
00858   if (partCount() > 1)
00859     return significand.parts;
00860   else
00861     return &significand.part;
00862 }
00863 
00864 void
00865 APFloat::zeroSignificand()
00866 {
00867   APInt::tcSet(significandParts(), 0, partCount());
00868 }
00869 
00870 /* Increment an fcNormal floating point number's significand.  */
00871 void
00872 APFloat::incrementSignificand()
00873 {
00874   integerPart carry;
00875 
00876   carry = APInt::tcIncrement(significandParts(), partCount());
00877 
00878   /* Our callers should never cause us to overflow.  */
00879   assert(carry == 0);
00880   (void)carry;
00881 }
00882 
00883 /* Add the significand of the RHS.  Returns the carry flag.  */
00884 integerPart
00885 APFloat::addSignificand(const APFloat &rhs)
00886 {
00887   integerPart *parts;
00888 
00889   parts = significandParts();
00890 
00891   assert(semantics == rhs.semantics);
00892   assert(exponent == rhs.exponent);
00893 
00894   return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
00895 }
00896 
00897 /* Subtract the significand of the RHS with a borrow flag.  Returns
00898    the borrow flag.  */
00899 integerPart
00900 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
00901 {
00902   integerPart *parts;
00903 
00904   parts = significandParts();
00905 
00906   assert(semantics == rhs.semantics);
00907   assert(exponent == rhs.exponent);
00908 
00909   return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
00910                            partCount());
00911 }
00912 
00913 /* Multiply the significand of the RHS.  If ADDEND is non-NULL, add it
00914    on to the full-precision result of the multiplication.  Returns the
00915    lost fraction.  */
00916 lostFraction
00917 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
00918 {
00919   unsigned int omsb;        // One, not zero, based MSB.
00920   unsigned int partsCount, newPartsCount, precision;
00921   integerPart *lhsSignificand;
00922   integerPart scratch[4];
00923   integerPart *fullSignificand;
00924   lostFraction lost_fraction;
00925   bool ignored;
00926 
00927   assert(semantics == rhs.semantics);
00928 
00929   precision = semantics->precision;
00930   newPartsCount = partCountForBits(precision * 2);
00931 
00932   if (newPartsCount > 4)
00933     fullSignificand = new integerPart[newPartsCount];
00934   else
00935     fullSignificand = scratch;
00936 
00937   lhsSignificand = significandParts();
00938   partsCount = partCount();
00939 
00940   APInt::tcFullMultiply(fullSignificand, lhsSignificand,
00941                         rhs.significandParts(), partsCount, partsCount);
00942 
00943   lost_fraction = lfExactlyZero;
00944   omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
00945   exponent += rhs.exponent;
00946 
00947   // Assume the operands involved in the multiplication are single-precision
00948   // FP, and the two multiplicants are:
00949   //   *this = a23 . a22 ... a0 * 2^e1
00950   //     rhs = b23 . b22 ... b0 * 2^e2
00951   // the result of multiplication is:
00952   //   *this = c47 c46 . c45 ... c0 * 2^(e1+e2)
00953   // Note that there are two significant bits at the left-hand side of the 
00954   // radix point. Move the radix point toward left by one bit, and adjust
00955   // exponent accordingly.
00956   exponent += 1;
00957 
00958   if (addend) {
00959     // The intermediate result of the multiplication has "2 * precision" 
00960     // signicant bit; adjust the addend to be consistent with mul result.
00961     //
00962     Significand savedSignificand = significand;
00963     const fltSemantics *savedSemantics = semantics;
00964     fltSemantics extendedSemantics;
00965     opStatus status;
00966     unsigned int extendedPrecision;
00967 
00968     /* Normalize our MSB.  */
00969     extendedPrecision = 2 * precision;
00970     if (omsb != extendedPrecision) {
00971       assert(extendedPrecision > omsb);
00972       APInt::tcShiftLeft(fullSignificand, newPartsCount,
00973                          extendedPrecision - omsb);
00974       exponent -= extendedPrecision - omsb;
00975     }
00976 
00977     /* Create new semantics.  */
00978     extendedSemantics = *semantics;
00979     extendedSemantics.precision = extendedPrecision;
00980 
00981     if (newPartsCount == 1)
00982       significand.part = fullSignificand[0];
00983     else
00984       significand.parts = fullSignificand;
00985     semantics = &extendedSemantics;
00986 
00987     APFloat extendedAddend(*addend);
00988     status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
00989     assert(status == opOK);
00990     (void)status;
00991     lost_fraction = addOrSubtractSignificand(extendedAddend, false);
00992 
00993     /* Restore our state.  */
00994     if (newPartsCount == 1)
00995       fullSignificand[0] = significand.part;
00996     significand = savedSignificand;
00997     semantics = savedSemantics;
00998 
00999     omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
01000   }
01001 
01002   // Convert the result having "2 * precision" significant-bits back to the one
01003   // having "precision" significant-bits. First, move the radix point from 
01004   // poision "2*precision - 1" to "precision - 1". The exponent need to be
01005   // adjusted by "2*precision - 1" - "precision - 1" = "precision".
01006   exponent -= precision;
01007 
01008   // In case MSB resides at the left-hand side of radix point, shift the
01009   // mantissa right by some amount to make sure the MSB reside right before
01010   // the radix point (i.e. "MSB . rest-significant-bits").
01011   //
01012   // Note that the result is not normalized when "omsb < precision". So, the
01013   // caller needs to call APFloat::normalize() if normalized value is expected.
01014   if (omsb > precision) {
01015     unsigned int bits, significantParts;
01016     lostFraction lf;
01017 
01018     bits = omsb - precision;
01019     significantParts = partCountForBits(omsb);
01020     lf = shiftRight(fullSignificand, significantParts, bits);
01021     lost_fraction = combineLostFractions(lf, lost_fraction);
01022     exponent += bits;
01023   }
01024 
01025   APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
01026 
01027   if (newPartsCount > 4)
01028     delete [] fullSignificand;
01029 
01030   return lost_fraction;
01031 }
01032 
01033 /* Multiply the significands of LHS and RHS to DST.  */
01034 lostFraction
01035 APFloat::divideSignificand(const APFloat &rhs)
01036 {
01037   unsigned int bit, i, partsCount;
01038   const integerPart *rhsSignificand;
01039   integerPart *lhsSignificand, *dividend, *divisor;
01040   integerPart scratch[4];
01041   lostFraction lost_fraction;
01042 
01043   assert(semantics == rhs.semantics);
01044 
01045   lhsSignificand = significandParts();
01046   rhsSignificand = rhs.significandParts();
01047   partsCount = partCount();
01048 
01049   if (partsCount > 2)
01050     dividend = new integerPart[partsCount * 2];
01051   else
01052     dividend = scratch;
01053 
01054   divisor = dividend + partsCount;
01055 
01056   /* Copy the dividend and divisor as they will be modified in-place.  */
01057   for (i = 0; i < partsCount; i++) {
01058     dividend[i] = lhsSignificand[i];
01059     divisor[i] = rhsSignificand[i];
01060     lhsSignificand[i] = 0;
01061   }
01062 
01063   exponent -= rhs.exponent;
01064 
01065   unsigned int precision = semantics->precision;
01066 
01067   /* Normalize the divisor.  */
01068   bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
01069   if (bit) {
01070     exponent += bit;
01071     APInt::tcShiftLeft(divisor, partsCount, bit);
01072   }
01073 
01074   /* Normalize the dividend.  */
01075   bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
01076   if (bit) {
01077     exponent -= bit;
01078     APInt::tcShiftLeft(dividend, partsCount, bit);
01079   }
01080 
01081   /* Ensure the dividend >= divisor initially for the loop below.
01082      Incidentally, this means that the division loop below is
01083      guaranteed to set the integer bit to one.  */
01084   if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
01085     exponent--;
01086     APInt::tcShiftLeft(dividend, partsCount, 1);
01087     assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
01088   }
01089 
01090   /* Long division.  */
01091   for (bit = precision; bit; bit -= 1) {
01092     if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
01093       APInt::tcSubtract(dividend, divisor, 0, partsCount);
01094       APInt::tcSetBit(lhsSignificand, bit - 1);
01095     }
01096 
01097     APInt::tcShiftLeft(dividend, partsCount, 1);
01098   }
01099 
01100   /* Figure out the lost fraction.  */
01101   int cmp = APInt::tcCompare(dividend, divisor, partsCount);
01102 
01103   if (cmp > 0)
01104     lost_fraction = lfMoreThanHalf;
01105   else if (cmp == 0)
01106     lost_fraction = lfExactlyHalf;
01107   else if (APInt::tcIsZero(dividend, partsCount))
01108     lost_fraction = lfExactlyZero;
01109   else
01110     lost_fraction = lfLessThanHalf;
01111 
01112   if (partsCount > 2)
01113     delete [] dividend;
01114 
01115   return lost_fraction;
01116 }
01117 
01118 unsigned int
01119 APFloat::significandMSB() const
01120 {
01121   return APInt::tcMSB(significandParts(), partCount());
01122 }
01123 
01124 unsigned int
01125 APFloat::significandLSB() const
01126 {
01127   return APInt::tcLSB(significandParts(), partCount());
01128 }
01129 
01130 /* Note that a zero result is NOT normalized to fcZero.  */
01131 lostFraction
01132 APFloat::shiftSignificandRight(unsigned int bits)
01133 {
01134   /* Our exponent should not overflow.  */
01135   assert((ExponentType) (exponent + bits) >= exponent);
01136 
01137   exponent += bits;
01138 
01139   return shiftRight(significandParts(), partCount(), bits);
01140 }
01141 
01142 /* Shift the significand left BITS bits, subtract BITS from its exponent.  */
01143 void
01144 APFloat::shiftSignificandLeft(unsigned int bits)
01145 {
01146   assert(bits < semantics->precision);
01147 
01148   if (bits) {
01149     unsigned int partsCount = partCount();
01150 
01151     APInt::tcShiftLeft(significandParts(), partsCount, bits);
01152     exponent -= bits;
01153 
01154     assert(!APInt::tcIsZero(significandParts(), partsCount));
01155   }
01156 }
01157 
01158 APFloat::cmpResult
01159 APFloat::compareAbsoluteValue(const APFloat &rhs) const
01160 {
01161   int compare;
01162 
01163   assert(semantics == rhs.semantics);
01164   assert(isFiniteNonZero());
01165   assert(rhs.isFiniteNonZero());
01166 
01167   compare = exponent - rhs.exponent;
01168 
01169   /* If exponents are equal, do an unsigned bignum comparison of the
01170      significands.  */
01171   if (compare == 0)
01172     compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
01173                                partCount());
01174 
01175   if (compare > 0)
01176     return cmpGreaterThan;
01177   else if (compare < 0)
01178     return cmpLessThan;
01179   else
01180     return cmpEqual;
01181 }
01182 
01183 /* Handle overflow.  Sign is preserved.  We either become infinity or
01184    the largest finite number.  */
01185 APFloat::opStatus
01186 APFloat::handleOverflow(roundingMode rounding_mode)
01187 {
01188   /* Infinity?  */
01189   if (rounding_mode == rmNearestTiesToEven ||
01190       rounding_mode == rmNearestTiesToAway ||
01191       (rounding_mode == rmTowardPositive && !sign) ||
01192       (rounding_mode == rmTowardNegative && sign)) {
01193     category = fcInfinity;
01194     return (opStatus) (opOverflow | opInexact);
01195   }
01196 
01197   /* Otherwise we become the largest finite number.  */
01198   category = fcNormal;
01199   exponent = semantics->maxExponent;
01200   APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
01201                                    semantics->precision);
01202 
01203   return opInexact;
01204 }
01205 
01206 /* Returns TRUE if, when truncating the current number, with BIT the
01207    new LSB, with the given lost fraction and rounding mode, the result
01208    would need to be rounded away from zero (i.e., by increasing the
01209    signficand).  This routine must work for fcZero of both signs, and
01210    fcNormal numbers.  */
01211 bool
01212 APFloat::roundAwayFromZero(roundingMode rounding_mode,
01213                            lostFraction lost_fraction,
01214                            unsigned int bit) const
01215 {
01216   /* NaNs and infinities should not have lost fractions.  */
01217   assert(isFiniteNonZero() || category == fcZero);
01218 
01219   /* Current callers never pass this so we don't handle it.  */
01220   assert(lost_fraction != lfExactlyZero);
01221 
01222   switch (rounding_mode) {
01223   case rmNearestTiesToAway:
01224     return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
01225 
01226   case rmNearestTiesToEven:
01227     if (lost_fraction == lfMoreThanHalf)
01228       return true;
01229 
01230     /* Our zeroes don't have a significand to test.  */
01231     if (lost_fraction == lfExactlyHalf && category != fcZero)
01232       return APInt::tcExtractBit(significandParts(), bit);
01233 
01234     return false;
01235 
01236   case rmTowardZero:
01237     return false;
01238 
01239   case rmTowardPositive:
01240     return sign == false;
01241 
01242   case rmTowardNegative:
01243     return sign == true;
01244   }
01245   llvm_unreachable("Invalid rounding mode found");
01246 }
01247 
01248 APFloat::opStatus
01249 APFloat::normalize(roundingMode rounding_mode,
01250                    lostFraction lost_fraction)
01251 {
01252   unsigned int omsb;                /* One, not zero, based MSB.  */
01253   int exponentChange;
01254 
01255   if (!isFiniteNonZero())
01256     return opOK;
01257 
01258   /* Before rounding normalize the exponent of fcNormal numbers.  */
01259   omsb = significandMSB() + 1;
01260 
01261   if (omsb) {
01262     /* OMSB is numbered from 1.  We want to place it in the integer
01263        bit numbered PRECISION if possible, with a compensating change in
01264        the exponent.  */
01265     exponentChange = omsb - semantics->precision;
01266 
01267     /* If the resulting exponent is too high, overflow according to
01268        the rounding mode.  */
01269     if (exponent + exponentChange > semantics->maxExponent)
01270       return handleOverflow(rounding_mode);
01271 
01272     /* Subnormal numbers have exponent minExponent, and their MSB
01273        is forced based on that.  */
01274     if (exponent + exponentChange < semantics->minExponent)
01275       exponentChange = semantics->minExponent - exponent;
01276 
01277     /* Shifting left is easy as we don't lose precision.  */
01278     if (exponentChange < 0) {
01279       assert(lost_fraction == lfExactlyZero);
01280 
01281       shiftSignificandLeft(-exponentChange);
01282 
01283       return opOK;
01284     }
01285 
01286     if (exponentChange > 0) {
01287       lostFraction lf;
01288 
01289       /* Shift right and capture any new lost fraction.  */
01290       lf = shiftSignificandRight(exponentChange);
01291 
01292       lost_fraction = combineLostFractions(lf, lost_fraction);
01293 
01294       /* Keep OMSB up-to-date.  */
01295       if (omsb > (unsigned) exponentChange)
01296         omsb -= exponentChange;
01297       else
01298         omsb = 0;
01299     }
01300   }
01301 
01302   /* Now round the number according to rounding_mode given the lost
01303      fraction.  */
01304 
01305   /* As specified in IEEE 754, since we do not trap we do not report
01306      underflow for exact results.  */
01307   if (lost_fraction == lfExactlyZero) {
01308     /* Canonicalize zeroes.  */
01309     if (omsb == 0)
01310       category = fcZero;
01311 
01312     return opOK;
01313   }
01314 
01315   /* Increment the significand if we're rounding away from zero.  */
01316   if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
01317     if (omsb == 0)
01318       exponent = semantics->minExponent;
01319 
01320     incrementSignificand();
01321     omsb = significandMSB() + 1;
01322 
01323     /* Did the significand increment overflow?  */
01324     if (omsb == (unsigned) semantics->precision + 1) {
01325       /* Renormalize by incrementing the exponent and shifting our
01326          significand right one.  However if we already have the
01327          maximum exponent we overflow to infinity.  */
01328       if (exponent == semantics->maxExponent) {
01329         category = fcInfinity;
01330 
01331         return (opStatus) (opOverflow | opInexact);
01332       }
01333 
01334       shiftSignificandRight(1);
01335 
01336       return opInexact;
01337     }
01338   }
01339 
01340   /* The normal case - we were and are not denormal, and any
01341      significand increment above didn't overflow.  */
01342   if (omsb == semantics->precision)
01343     return opInexact;
01344 
01345   /* We have a non-zero denormal.  */
01346   assert(omsb < semantics->precision);
01347 
01348   /* Canonicalize zeroes.  */
01349   if (omsb == 0)
01350     category = fcZero;
01351 
01352   /* The fcZero case is a denormal that underflowed to zero.  */
01353   return (opStatus) (opUnderflow | opInexact);
01354 }
01355 
01356 APFloat::opStatus
01357 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
01358 {
01359   switch (PackCategoriesIntoKey(category, rhs.category)) {
01360   default:
01361     llvm_unreachable(nullptr);
01362 
01363   case PackCategoriesIntoKey(fcNaN, fcZero):
01364   case PackCategoriesIntoKey(fcNaN, fcNormal):
01365   case PackCategoriesIntoKey(fcNaN, fcInfinity):
01366   case PackCategoriesIntoKey(fcNaN, fcNaN):
01367   case PackCategoriesIntoKey(fcNormal, fcZero):
01368   case PackCategoriesIntoKey(fcInfinity, fcNormal):
01369   case PackCategoriesIntoKey(fcInfinity, fcZero):
01370     return opOK;
01371 
01372   case PackCategoriesIntoKey(fcZero, fcNaN):
01373   case PackCategoriesIntoKey(fcNormal, fcNaN):
01374   case PackCategoriesIntoKey(fcInfinity, fcNaN):
01375     // We need to be sure to flip the sign here for subtraction because we
01376     // don't have a separate negate operation so -NaN becomes 0 - NaN here.
01377     sign = rhs.sign ^ subtract;
01378     category = fcNaN;
01379     copySignificand(rhs);
01380     return opOK;
01381 
01382   case PackCategoriesIntoKey(fcNormal, fcInfinity):
01383   case PackCategoriesIntoKey(fcZero, fcInfinity):
01384     category = fcInfinity;
01385     sign = rhs.sign ^ subtract;
01386     return opOK;
01387 
01388   case PackCategoriesIntoKey(fcZero, fcNormal):
01389     assign(rhs);
01390     sign = rhs.sign ^ subtract;
01391     return opOK;
01392 
01393   case PackCategoriesIntoKey(fcZero, fcZero):
01394     /* Sign depends on rounding mode; handled by caller.  */
01395     return opOK;
01396 
01397   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
01398     /* Differently signed infinities can only be validly
01399        subtracted.  */
01400     if (((sign ^ rhs.sign)!=0) != subtract) {
01401       makeNaN();
01402       return opInvalidOp;
01403     }
01404 
01405     return opOK;
01406 
01407   case PackCategoriesIntoKey(fcNormal, fcNormal):
01408     return opDivByZero;
01409   }
01410 }
01411 
01412 /* Add or subtract two normal numbers.  */
01413 lostFraction
01414 APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
01415 {
01416   integerPart carry;
01417   lostFraction lost_fraction;
01418   int bits;
01419 
01420   /* Determine if the operation on the absolute values is effectively
01421      an addition or subtraction.  */
01422   subtract ^= (sign ^ rhs.sign) ? true : false;
01423 
01424   /* Are we bigger exponent-wise than the RHS?  */
01425   bits = exponent - rhs.exponent;
01426 
01427   /* Subtraction is more subtle than one might naively expect.  */
01428   if (subtract) {
01429     APFloat temp_rhs(rhs);
01430     bool reverse;
01431 
01432     if (bits == 0) {
01433       reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
01434       lost_fraction = lfExactlyZero;
01435     } else if (bits > 0) {
01436       lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
01437       shiftSignificandLeft(1);
01438       reverse = false;
01439     } else {
01440       lost_fraction = shiftSignificandRight(-bits - 1);
01441       temp_rhs.shiftSignificandLeft(1);
01442       reverse = true;
01443     }
01444 
01445     if (reverse) {
01446       carry = temp_rhs.subtractSignificand
01447         (*this, lost_fraction != lfExactlyZero);
01448       copySignificand(temp_rhs);
01449       sign = !sign;
01450     } else {
01451       carry = subtractSignificand
01452         (temp_rhs, lost_fraction != lfExactlyZero);
01453     }
01454 
01455     /* Invert the lost fraction - it was on the RHS and
01456        subtracted.  */
01457     if (lost_fraction == lfLessThanHalf)
01458       lost_fraction = lfMoreThanHalf;
01459     else if (lost_fraction == lfMoreThanHalf)
01460       lost_fraction = lfLessThanHalf;
01461 
01462     /* The code above is intended to ensure that no borrow is
01463        necessary.  */
01464     assert(!carry);
01465     (void)carry;
01466   } else {
01467     if (bits > 0) {
01468       APFloat temp_rhs(rhs);
01469 
01470       lost_fraction = temp_rhs.shiftSignificandRight(bits);
01471       carry = addSignificand(temp_rhs);
01472     } else {
01473       lost_fraction = shiftSignificandRight(-bits);
01474       carry = addSignificand(rhs);
01475     }
01476 
01477     /* We have a guard bit; generating a carry cannot happen.  */
01478     assert(!carry);
01479     (void)carry;
01480   }
01481 
01482   return lost_fraction;
01483 }
01484 
01485 APFloat::opStatus
01486 APFloat::multiplySpecials(const APFloat &rhs)
01487 {
01488   switch (PackCategoriesIntoKey(category, rhs.category)) {
01489   default:
01490     llvm_unreachable(nullptr);
01491 
01492   case PackCategoriesIntoKey(fcNaN, fcZero):
01493   case PackCategoriesIntoKey(fcNaN, fcNormal):
01494   case PackCategoriesIntoKey(fcNaN, fcInfinity):
01495   case PackCategoriesIntoKey(fcNaN, fcNaN):
01496     sign = false;
01497     return opOK;
01498 
01499   case PackCategoriesIntoKey(fcZero, fcNaN):
01500   case PackCategoriesIntoKey(fcNormal, fcNaN):
01501   case PackCategoriesIntoKey(fcInfinity, fcNaN):
01502     sign = false;
01503     category = fcNaN;
01504     copySignificand(rhs);
01505     return opOK;
01506 
01507   case PackCategoriesIntoKey(fcNormal, fcInfinity):
01508   case PackCategoriesIntoKey(fcInfinity, fcNormal):
01509   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
01510     category = fcInfinity;
01511     return opOK;
01512 
01513   case PackCategoriesIntoKey(fcZero, fcNormal):
01514   case PackCategoriesIntoKey(fcNormal, fcZero):
01515   case PackCategoriesIntoKey(fcZero, fcZero):
01516     category = fcZero;
01517     return opOK;
01518 
01519   case PackCategoriesIntoKey(fcZero, fcInfinity):
01520   case PackCategoriesIntoKey(fcInfinity, fcZero):
01521     makeNaN();
01522     return opInvalidOp;
01523 
01524   case PackCategoriesIntoKey(fcNormal, fcNormal):
01525     return opOK;
01526   }
01527 }
01528 
01529 APFloat::opStatus
01530 APFloat::divideSpecials(const APFloat &rhs)
01531 {
01532   switch (PackCategoriesIntoKey(category, rhs.category)) {
01533   default:
01534     llvm_unreachable(nullptr);
01535 
01536   case PackCategoriesIntoKey(fcZero, fcNaN):
01537   case PackCategoriesIntoKey(fcNormal, fcNaN):
01538   case PackCategoriesIntoKey(fcInfinity, fcNaN):
01539     category = fcNaN;
01540     copySignificand(rhs);
01541   case PackCategoriesIntoKey(fcNaN, fcZero):
01542   case PackCategoriesIntoKey(fcNaN, fcNormal):
01543   case PackCategoriesIntoKey(fcNaN, fcInfinity):
01544   case PackCategoriesIntoKey(fcNaN, fcNaN):
01545     sign = false;
01546   case PackCategoriesIntoKey(fcInfinity, fcZero):
01547   case PackCategoriesIntoKey(fcInfinity, fcNormal):
01548   case PackCategoriesIntoKey(fcZero, fcInfinity):
01549   case PackCategoriesIntoKey(fcZero, fcNormal):
01550     return opOK;
01551 
01552   case PackCategoriesIntoKey(fcNormal, fcInfinity):
01553     category = fcZero;
01554     return opOK;
01555 
01556   case PackCategoriesIntoKey(fcNormal, fcZero):
01557     category = fcInfinity;
01558     return opDivByZero;
01559 
01560   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
01561   case PackCategoriesIntoKey(fcZero, fcZero):
01562     makeNaN();
01563     return opInvalidOp;
01564 
01565   case PackCategoriesIntoKey(fcNormal, fcNormal):
01566     return opOK;
01567   }
01568 }
01569 
01570 APFloat::opStatus
01571 APFloat::modSpecials(const APFloat &rhs)
01572 {
01573   switch (PackCategoriesIntoKey(category, rhs.category)) {
01574   default:
01575     llvm_unreachable(nullptr);
01576 
01577   case PackCategoriesIntoKey(fcNaN, fcZero):
01578   case PackCategoriesIntoKey(fcNaN, fcNormal):
01579   case PackCategoriesIntoKey(fcNaN, fcInfinity):
01580   case PackCategoriesIntoKey(fcNaN, fcNaN):
01581   case PackCategoriesIntoKey(fcZero, fcInfinity):
01582   case PackCategoriesIntoKey(fcZero, fcNormal):
01583   case PackCategoriesIntoKey(fcNormal, fcInfinity):
01584     return opOK;
01585 
01586   case PackCategoriesIntoKey(fcZero, fcNaN):
01587   case PackCategoriesIntoKey(fcNormal, fcNaN):
01588   case PackCategoriesIntoKey(fcInfinity, fcNaN):
01589     sign = false;
01590     category = fcNaN;
01591     copySignificand(rhs);
01592     return opOK;
01593 
01594   case PackCategoriesIntoKey(fcNormal, fcZero):
01595   case PackCategoriesIntoKey(fcInfinity, fcZero):
01596   case PackCategoriesIntoKey(fcInfinity, fcNormal):
01597   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
01598   case PackCategoriesIntoKey(fcZero, fcZero):
01599     makeNaN();
01600     return opInvalidOp;
01601 
01602   case PackCategoriesIntoKey(fcNormal, fcNormal):
01603     return opOK;
01604   }
01605 }
01606 
01607 /* Change sign.  */
01608 void
01609 APFloat::changeSign()
01610 {
01611   /* Look mummy, this one's easy.  */
01612   sign = !sign;
01613 }
01614 
01615 void
01616 APFloat::clearSign()
01617 {
01618   /* So is this one. */
01619   sign = 0;
01620 }
01621 
01622 void
01623 APFloat::copySign(const APFloat &rhs)
01624 {
01625   /* And this one. */
01626   sign = rhs.sign;
01627 }
01628 
01629 /* Normalized addition or subtraction.  */
01630 APFloat::opStatus
01631 APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
01632                        bool subtract)
01633 {
01634   opStatus fs;
01635 
01636   fs = addOrSubtractSpecials(rhs, subtract);
01637 
01638   /* This return code means it was not a simple case.  */
01639   if (fs == opDivByZero) {
01640     lostFraction lost_fraction;
01641 
01642     lost_fraction = addOrSubtractSignificand(rhs, subtract);
01643     fs = normalize(rounding_mode, lost_fraction);
01644 
01645     /* Can only be zero if we lost no fraction.  */
01646     assert(category != fcZero || lost_fraction == lfExactlyZero);
01647   }
01648 
01649   /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
01650      positive zero unless rounding to minus infinity, except that
01651      adding two like-signed zeroes gives that zero.  */
01652   if (category == fcZero) {
01653     if (rhs.category != fcZero || (sign == rhs.sign) == subtract)
01654       sign = (rounding_mode == rmTowardNegative);
01655   }
01656 
01657   return fs;
01658 }
01659 
01660 /* Normalized addition.  */
01661 APFloat::opStatus
01662 APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
01663 {
01664   return addOrSubtract(rhs, rounding_mode, false);
01665 }
01666 
01667 /* Normalized subtraction.  */
01668 APFloat::opStatus
01669 APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
01670 {
01671   return addOrSubtract(rhs, rounding_mode, true);
01672 }
01673 
01674 /* Normalized multiply.  */
01675 APFloat::opStatus
01676 APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
01677 {
01678   opStatus fs;
01679 
01680   sign ^= rhs.sign;
01681   fs = multiplySpecials(rhs);
01682 
01683   if (isFiniteNonZero()) {
01684     lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
01685     fs = normalize(rounding_mode, lost_fraction);
01686     if (lost_fraction != lfExactlyZero)
01687       fs = (opStatus) (fs | opInexact);
01688   }
01689 
01690   return fs;
01691 }
01692 
01693 /* Normalized divide.  */
01694 APFloat::opStatus
01695 APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
01696 {
01697   opStatus fs;
01698 
01699   sign ^= rhs.sign;
01700   fs = divideSpecials(rhs);
01701 
01702   if (isFiniteNonZero()) {
01703     lostFraction lost_fraction = divideSignificand(rhs);
01704     fs = normalize(rounding_mode, lost_fraction);
01705     if (lost_fraction != lfExactlyZero)
01706       fs = (opStatus) (fs | opInexact);
01707   }
01708 
01709   return fs;
01710 }
01711 
01712 /* Normalized remainder.  This is not currently correct in all cases.  */
01713 APFloat::opStatus
01714 APFloat::remainder(const APFloat &rhs)
01715 {
01716   opStatus fs;
01717   APFloat V = *this;
01718   unsigned int origSign = sign;
01719 
01720   fs = V.divide(rhs, rmNearestTiesToEven);
01721   if (fs == opDivByZero)
01722     return fs;
01723 
01724   int parts = partCount();
01725   integerPart *x = new integerPart[parts];
01726   bool ignored;
01727   fs = V.convertToInteger(x, parts * integerPartWidth, true,
01728                           rmNearestTiesToEven, &ignored);
01729   if (fs==opInvalidOp)
01730     return fs;
01731 
01732   fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
01733                                         rmNearestTiesToEven);
01734   assert(fs==opOK);   // should always work
01735 
01736   fs = V.multiply(rhs, rmNearestTiesToEven);
01737   assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
01738 
01739   fs = subtract(V, rmNearestTiesToEven);
01740   assert(fs==opOK || fs==opInexact);   // likewise
01741 
01742   if (isZero())
01743     sign = origSign;    // IEEE754 requires this
01744   delete[] x;
01745   return fs;
01746 }
01747 
01748 /* Normalized llvm frem (C fmod).
01749    This is not currently correct in all cases.  */
01750 APFloat::opStatus
01751 APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
01752 {
01753   opStatus fs;
01754   fs = modSpecials(rhs);
01755 
01756   if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
01757     APFloat V = *this;
01758     unsigned int origSign = sign;
01759 
01760     fs = V.divide(rhs, rmNearestTiesToEven);
01761     if (fs == opDivByZero)
01762       return fs;
01763 
01764     int parts = partCount();
01765     integerPart *x = new integerPart[parts];
01766     bool ignored;
01767     fs = V.convertToInteger(x, parts * integerPartWidth, true,
01768                             rmTowardZero, &ignored);
01769     if (fs==opInvalidOp)
01770       return fs;
01771 
01772     fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
01773                                           rmNearestTiesToEven);
01774     assert(fs==opOK);   // should always work
01775 
01776     fs = V.multiply(rhs, rounding_mode);
01777     assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
01778 
01779     fs = subtract(V, rounding_mode);
01780     assert(fs==opOK || fs==opInexact);   // likewise
01781 
01782     if (isZero())
01783       sign = origSign;    // IEEE754 requires this
01784     delete[] x;
01785   }
01786   return fs;
01787 }
01788 
01789 /* Normalized fused-multiply-add.  */
01790 APFloat::opStatus
01791 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
01792                           const APFloat &addend,
01793                           roundingMode rounding_mode)
01794 {
01795   opStatus fs;
01796 
01797   /* Post-multiplication sign, before addition.  */
01798   sign ^= multiplicand.sign;
01799 
01800   /* If and only if all arguments are normal do we need to do an
01801      extended-precision calculation.  */
01802   if (isFiniteNonZero() &&
01803       multiplicand.isFiniteNonZero() &&
01804       addend.isFiniteNonZero()) {
01805     lostFraction lost_fraction;
01806 
01807     lost_fraction = multiplySignificand(multiplicand, &addend);
01808     fs = normalize(rounding_mode, lost_fraction);
01809     if (lost_fraction != lfExactlyZero)
01810       fs = (opStatus) (fs | opInexact);
01811 
01812     /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
01813        positive zero unless rounding to minus infinity, except that
01814        adding two like-signed zeroes gives that zero.  */
01815     if (category == fcZero && sign != addend.sign)
01816       sign = (rounding_mode == rmTowardNegative);
01817   } else {
01818     fs = multiplySpecials(multiplicand);
01819 
01820     /* FS can only be opOK or opInvalidOp.  There is no more work
01821        to do in the latter case.  The IEEE-754R standard says it is
01822        implementation-defined in this case whether, if ADDEND is a
01823        quiet NaN, we raise invalid op; this implementation does so.
01824 
01825        If we need to do the addition we can do so with normal
01826        precision.  */
01827     if (fs == opOK)
01828       fs = addOrSubtract(addend, rounding_mode, false);
01829   }
01830 
01831   return fs;
01832 }
01833 
01834 /* Rounding-mode corrrect round to integral value.  */
01835 APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
01836   opStatus fs;
01837 
01838   // If the exponent is large enough, we know that this value is already
01839   // integral, and the arithmetic below would potentially cause it to saturate
01840   // to +/-Inf.  Bail out early instead.
01841   if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
01842     return opOK;
01843 
01844   // The algorithm here is quite simple: we add 2^(p-1), where p is the
01845   // precision of our format, and then subtract it back off again.  The choice
01846   // of rounding modes for the addition/subtraction determines the rounding mode
01847   // for our integral rounding as well.
01848   // NOTE: When the input value is negative, we do subtraction followed by
01849   // addition instead.
01850   APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
01851   IntegerConstant <<= semanticsPrecision(*semantics)-1;
01852   APFloat MagicConstant(*semantics);
01853   fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
01854                                       rmNearestTiesToEven);
01855   MagicConstant.copySign(*this);
01856 
01857   if (fs != opOK)
01858     return fs;
01859 
01860   // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
01861   bool inputSign = isNegative();
01862 
01863   fs = add(MagicConstant, rounding_mode);
01864   if (fs != opOK && fs != opInexact)
01865     return fs;
01866 
01867   fs = subtract(MagicConstant, rounding_mode);
01868 
01869   // Restore the input sign.
01870   if (inputSign != isNegative())
01871     changeSign();
01872 
01873   return fs;
01874 }
01875 
01876 
01877 /* Comparison requires normalized numbers.  */
01878 APFloat::cmpResult
01879 APFloat::compare(const APFloat &rhs) const
01880 {
01881   cmpResult result;
01882 
01883   assert(semantics == rhs.semantics);
01884 
01885   switch (PackCategoriesIntoKey(category, rhs.category)) {
01886   default:
01887     llvm_unreachable(nullptr);
01888 
01889   case PackCategoriesIntoKey(fcNaN, fcZero):
01890   case PackCategoriesIntoKey(fcNaN, fcNormal):
01891   case PackCategoriesIntoKey(fcNaN, fcInfinity):
01892   case PackCategoriesIntoKey(fcNaN, fcNaN):
01893   case PackCategoriesIntoKey(fcZero, fcNaN):
01894   case PackCategoriesIntoKey(fcNormal, fcNaN):
01895   case PackCategoriesIntoKey(fcInfinity, fcNaN):
01896     return cmpUnordered;
01897 
01898   case PackCategoriesIntoKey(fcInfinity, fcNormal):
01899   case PackCategoriesIntoKey(fcInfinity, fcZero):
01900   case PackCategoriesIntoKey(fcNormal, fcZero):
01901     if (sign)
01902       return cmpLessThan;
01903     else
01904       return cmpGreaterThan;
01905 
01906   case PackCategoriesIntoKey(fcNormal, fcInfinity):
01907   case PackCategoriesIntoKey(fcZero, fcInfinity):
01908   case PackCategoriesIntoKey(fcZero, fcNormal):
01909     if (rhs.sign)
01910       return cmpGreaterThan;
01911     else
01912       return cmpLessThan;
01913 
01914   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
01915     if (sign == rhs.sign)
01916       return cmpEqual;
01917     else if (sign)
01918       return cmpLessThan;
01919     else
01920       return cmpGreaterThan;
01921 
01922   case PackCategoriesIntoKey(fcZero, fcZero):
01923     return cmpEqual;
01924 
01925   case PackCategoriesIntoKey(fcNormal, fcNormal):
01926     break;
01927   }
01928 
01929   /* Two normal numbers.  Do they have the same sign?  */
01930   if (sign != rhs.sign) {
01931     if (sign)
01932       result = cmpLessThan;
01933     else
01934       result = cmpGreaterThan;
01935   } else {
01936     /* Compare absolute values; invert result if negative.  */
01937     result = compareAbsoluteValue(rhs);
01938 
01939     if (sign) {
01940       if (result == cmpLessThan)
01941         result = cmpGreaterThan;
01942       else if (result == cmpGreaterThan)
01943         result = cmpLessThan;
01944     }
01945   }
01946 
01947   return result;
01948 }
01949 
01950 /// APFloat::convert - convert a value of one floating point type to another.
01951 /// The return value corresponds to the IEEE754 exceptions.  *losesInfo
01952 /// records whether the transformation lost information, i.e. whether
01953 /// converting the result back to the original type will produce the
01954 /// original value (this is almost the same as return value==fsOK, but there
01955 /// are edge cases where this is not so).
01956 
01957 APFloat::opStatus
01958 APFloat::convert(const fltSemantics &toSemantics,
01959                  roundingMode rounding_mode, bool *losesInfo)
01960 {
01961   lostFraction lostFraction;
01962   unsigned int newPartCount, oldPartCount;
01963   opStatus fs;
01964   int shift;
01965   const fltSemantics &fromSemantics = *semantics;
01966 
01967   lostFraction = lfExactlyZero;
01968   newPartCount = partCountForBits(toSemantics.precision + 1);
01969   oldPartCount = partCount();
01970   shift = toSemantics.precision - fromSemantics.precision;
01971 
01972   bool X86SpecialNan = false;
01973   if (&fromSemantics == &APFloat::x87DoubleExtended &&
01974       &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
01975       (!(*significandParts() & 0x8000000000000000ULL) ||
01976        !(*significandParts() & 0x4000000000000000ULL))) {
01977     // x86 has some unusual NaNs which cannot be represented in any other
01978     // format; note them here.
01979     X86SpecialNan = true;
01980   }
01981 
01982   // If this is a truncation of a denormal number, and the target semantics
01983   // has larger exponent range than the source semantics (this can happen
01984   // when truncating from PowerPC double-double to double format), the
01985   // right shift could lose result mantissa bits.  Adjust exponent instead
01986   // of performing excessive shift.
01987   if (shift < 0 && isFiniteNonZero()) {
01988     int exponentChange = significandMSB() + 1 - fromSemantics.precision;
01989     if (exponent + exponentChange < toSemantics.minExponent)
01990       exponentChange = toSemantics.minExponent - exponent;
01991     if (exponentChange < shift)
01992       exponentChange = shift;
01993     if (exponentChange < 0) {
01994       shift -= exponentChange;
01995       exponent += exponentChange;
01996     }
01997   }
01998 
01999   // If this is a truncation, perform the shift before we narrow the storage.
02000   if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
02001     lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
02002 
02003   // Fix the storage so it can hold to new value.
02004   if (newPartCount > oldPartCount) {
02005     // The new type requires more storage; make it available.
02006     integerPart *newParts;
02007     newParts = new integerPart[newPartCount];
02008     APInt::tcSet(newParts, 0, newPartCount);
02009     if (isFiniteNonZero() || category==fcNaN)
02010       APInt::tcAssign(newParts, significandParts(), oldPartCount);
02011     freeSignificand();
02012     significand.parts = newParts;
02013   } else if (newPartCount == 1 && oldPartCount != 1) {
02014     // Switch to built-in storage for a single part.
02015     integerPart newPart = 0;
02016     if (isFiniteNonZero() || category==fcNaN)
02017       newPart = significandParts()[0];
02018     freeSignificand();
02019     significand.part = newPart;
02020   }
02021 
02022   // Now that we have the right storage, switch the semantics.
02023   semantics = &toSemantics;
02024 
02025   // If this is an extension, perform the shift now that the storage is
02026   // available.
02027   if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
02028     APInt::tcShiftLeft(significandParts(), newPartCount, shift);
02029 
02030   if (isFiniteNonZero()) {
02031     fs = normalize(rounding_mode, lostFraction);
02032     *losesInfo = (fs != opOK);
02033   } else if (category == fcNaN) {
02034     *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
02035 
02036     // For x87 extended precision, we want to make a NaN, not a special NaN if
02037     // the input wasn't special either.
02038     if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
02039       APInt::tcSetBit(significandParts(), semantics->precision - 1);
02040 
02041     // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
02042     // does not give you back the same bits.  This is dubious, and we
02043     // don't currently do it.  You're really supposed to get
02044     // an invalid operation signal at runtime, but nobody does that.
02045     fs = opOK;
02046   } else {
02047     *losesInfo = false;
02048     fs = opOK;
02049   }
02050 
02051   return fs;
02052 }
02053 
02054 /* Convert a floating point number to an integer according to the
02055    rounding mode.  If the rounded integer value is out of range this
02056    returns an invalid operation exception and the contents of the
02057    destination parts are unspecified.  If the rounded value is in
02058    range but the floating point number is not the exact integer, the C
02059    standard doesn't require an inexact exception to be raised.  IEEE
02060    854 does require it so we do that.
02061 
02062    Note that for conversions to integer type the C standard requires
02063    round-to-zero to always be used.  */
02064 APFloat::opStatus
02065 APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
02066                                       bool isSigned,
02067                                       roundingMode rounding_mode,
02068                                       bool *isExact) const
02069 {
02070   lostFraction lost_fraction;
02071   const integerPart *src;
02072   unsigned int dstPartsCount, truncatedBits;
02073 
02074   *isExact = false;
02075 
02076   /* Handle the three special cases first.  */
02077   if (category == fcInfinity || category == fcNaN)
02078     return opInvalidOp;
02079 
02080   dstPartsCount = partCountForBits(width);
02081 
02082   if (category == fcZero) {
02083     APInt::tcSet(parts, 0, dstPartsCount);
02084     // Negative zero can't be represented as an int.
02085     *isExact = !sign;
02086     return opOK;
02087   }
02088 
02089   src = significandParts();
02090 
02091   /* Step 1: place our absolute value, with any fraction truncated, in
02092      the destination.  */
02093   if (exponent < 0) {
02094     /* Our absolute value is less than one; truncate everything.  */
02095     APInt::tcSet(parts, 0, dstPartsCount);
02096     /* For exponent -1 the integer bit represents .5, look at that.
02097        For smaller exponents leftmost truncated bit is 0. */
02098     truncatedBits = semantics->precision -1U - exponent;
02099   } else {
02100     /* We want the most significant (exponent + 1) bits; the rest are
02101        truncated.  */
02102     unsigned int bits = exponent + 1U;
02103 
02104     /* Hopelessly large in magnitude?  */
02105     if (bits > width)
02106       return opInvalidOp;
02107 
02108     if (bits < semantics->precision) {
02109       /* We truncate (semantics->precision - bits) bits.  */
02110       truncatedBits = semantics->precision - bits;
02111       APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
02112     } else {
02113       /* We want at least as many bits as are available.  */
02114       APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
02115       APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
02116       truncatedBits = 0;
02117     }
02118   }
02119 
02120   /* Step 2: work out any lost fraction, and increment the absolute
02121      value if we would round away from zero.  */
02122   if (truncatedBits) {
02123     lost_fraction = lostFractionThroughTruncation(src, partCount(),
02124                                                   truncatedBits);
02125     if (lost_fraction != lfExactlyZero &&
02126         roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
02127       if (APInt::tcIncrement(parts, dstPartsCount))
02128         return opInvalidOp;     /* Overflow.  */
02129     }
02130   } else {
02131     lost_fraction = lfExactlyZero;
02132   }
02133 
02134   /* Step 3: check if we fit in the destination.  */
02135   unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
02136 
02137   if (sign) {
02138     if (!isSigned) {
02139       /* Negative numbers cannot be represented as unsigned.  */
02140       if (omsb != 0)
02141         return opInvalidOp;
02142     } else {
02143       /* It takes omsb bits to represent the unsigned integer value.
02144          We lose a bit for the sign, but care is needed as the
02145          maximally negative integer is a special case.  */
02146       if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
02147         return opInvalidOp;
02148 
02149       /* This case can happen because of rounding.  */
02150       if (omsb > width)
02151         return opInvalidOp;
02152     }
02153 
02154     APInt::tcNegate (parts, dstPartsCount);
02155   } else {
02156     if (omsb >= width + !isSigned)
02157       return opInvalidOp;
02158   }
02159 
02160   if (lost_fraction == lfExactlyZero) {
02161     *isExact = true;
02162     return opOK;
02163   } else
02164     return opInexact;
02165 }
02166 
02167 /* Same as convertToSignExtendedInteger, except we provide
02168    deterministic values in case of an invalid operation exception,
02169    namely zero for NaNs and the minimal or maximal value respectively
02170    for underflow or overflow.
02171    The *isExact output tells whether the result is exact, in the sense
02172    that converting it back to the original floating point type produces
02173    the original value.  This is almost equivalent to result==opOK,
02174    except for negative zeroes.
02175 */
02176 APFloat::opStatus
02177 APFloat::convertToInteger(integerPart *parts, unsigned int width,
02178                           bool isSigned,
02179                           roundingMode rounding_mode, bool *isExact) const
02180 {
02181   opStatus fs;
02182 
02183   fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
02184                                     isExact);
02185 
02186   if (fs == opInvalidOp) {
02187     unsigned int bits, dstPartsCount;
02188 
02189     dstPartsCount = partCountForBits(width);
02190 
02191     if (category == fcNaN)
02192       bits = 0;
02193     else if (sign)
02194       bits = isSigned;
02195     else
02196       bits = width - isSigned;
02197 
02198     APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
02199     if (sign && isSigned)
02200       APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
02201   }
02202 
02203   return fs;
02204 }
02205 
02206 /* Same as convertToInteger(integerPart*, ...), except the result is returned in
02207    an APSInt, whose initial bit-width and signed-ness are used to determine the
02208    precision of the conversion.
02209  */
02210 APFloat::opStatus
02211 APFloat::convertToInteger(APSInt &result,
02212                           roundingMode rounding_mode, bool *isExact) const
02213 {
02214   unsigned bitWidth = result.getBitWidth();
02215   SmallVector<uint64_t, 4> parts(result.getNumWords());
02216   opStatus status = convertToInteger(
02217     parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
02218   // Keeps the original signed-ness.
02219   result = APInt(bitWidth, parts);
02220   return status;
02221 }
02222 
02223 /* Convert an unsigned integer SRC to a floating point number,
02224    rounding according to ROUNDING_MODE.  The sign of the floating
02225    point number is not modified.  */
02226 APFloat::opStatus
02227 APFloat::convertFromUnsignedParts(const integerPart *src,
02228                                   unsigned int srcCount,
02229                                   roundingMode rounding_mode)
02230 {
02231   unsigned int omsb, precision, dstCount;
02232   integerPart *dst;
02233   lostFraction lost_fraction;
02234 
02235   category = fcNormal;
02236   omsb = APInt::tcMSB(src, srcCount) + 1;
02237   dst = significandParts();
02238   dstCount = partCount();
02239   precision = semantics->precision;
02240 
02241   /* We want the most significant PRECISION bits of SRC.  There may not
02242      be that many; extract what we can.  */
02243   if (precision <= omsb) {
02244     exponent = omsb - 1;
02245     lost_fraction = lostFractionThroughTruncation(src, srcCount,
02246                                                   omsb - precision);
02247     APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
02248   } else {
02249     exponent = precision - 1;
02250     lost_fraction = lfExactlyZero;
02251     APInt::tcExtract(dst, dstCount, src, omsb, 0);
02252   }
02253 
02254   return normalize(rounding_mode, lost_fraction);
02255 }
02256 
02257 APFloat::opStatus
02258 APFloat::convertFromAPInt(const APInt &Val,
02259                           bool isSigned,
02260                           roundingMode rounding_mode)
02261 {
02262   unsigned int partCount = Val.getNumWords();
02263   APInt api = Val;
02264 
02265   sign = false;
02266   if (isSigned && api.isNegative()) {
02267     sign = true;
02268     api = -api;
02269   }
02270 
02271   return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
02272 }
02273 
02274 /* Convert a two's complement integer SRC to a floating point number,
02275    rounding according to ROUNDING_MODE.  ISSIGNED is true if the
02276    integer is signed, in which case it must be sign-extended.  */
02277 APFloat::opStatus
02278 APFloat::convertFromSignExtendedInteger(const integerPart *src,
02279                                         unsigned int srcCount,
02280                                         bool isSigned,
02281                                         roundingMode rounding_mode)
02282 {
02283   opStatus status;
02284 
02285   if (isSigned &&
02286       APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
02287     integerPart *copy;
02288 
02289     /* If we're signed and negative negate a copy.  */
02290     sign = true;
02291     copy = new integerPart[srcCount];
02292     APInt::tcAssign(copy, src, srcCount);
02293     APInt::tcNegate(copy, srcCount);
02294     status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
02295     delete [] copy;
02296   } else {
02297     sign = false;
02298     status = convertFromUnsignedParts(src, srcCount, rounding_mode);
02299   }
02300 
02301   return status;
02302 }
02303 
02304 /* FIXME: should this just take a const APInt reference?  */
02305 APFloat::opStatus
02306 APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
02307                                         unsigned int width, bool isSigned,
02308                                         roundingMode rounding_mode)
02309 {
02310   unsigned int partCount = partCountForBits(width);
02311   APInt api = APInt(width, makeArrayRef(parts, partCount));
02312 
02313   sign = false;
02314   if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
02315     sign = true;
02316     api = -api;
02317   }
02318 
02319   return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
02320 }
02321 
02322 APFloat::opStatus
02323 APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
02324 {
02325   lostFraction lost_fraction = lfExactlyZero;
02326 
02327   category = fcNormal;
02328   zeroSignificand();
02329   exponent = 0;
02330 
02331   integerPart *significand = significandParts();
02332   unsigned partsCount = partCount();
02333   unsigned bitPos = partsCount * integerPartWidth;
02334   bool computedTrailingFraction = false;
02335 
02336   // Skip leading zeroes and any (hexa)decimal point.
02337   StringRef::iterator begin = s.begin();
02338   StringRef::iterator end = s.end();
02339   StringRef::iterator dot;
02340   StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
02341   StringRef::iterator firstSignificantDigit = p;
02342 
02343   while (p != end) {
02344     integerPart hex_value;
02345 
02346     if (*p == '.') {
02347       assert(dot == end && "String contains multiple dots");
02348       dot = p++;
02349       continue;
02350     }
02351 
02352     hex_value = hexDigitValue(*p);
02353     if (hex_value == -1U)
02354       break;
02355 
02356     p++;
02357 
02358     // Store the number while we have space.
02359     if (bitPos) {
02360       bitPos -= 4;
02361       hex_value <<= bitPos % integerPartWidth;
02362       significand[bitPos / integerPartWidth] |= hex_value;
02363     } else if (!computedTrailingFraction) {
02364       lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
02365       computedTrailingFraction = true;
02366     }
02367   }
02368 
02369   /* Hex floats require an exponent but not a hexadecimal point.  */
02370   assert(p != end && "Hex strings require an exponent");
02371   assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
02372   assert(p != begin && "Significand has no digits");
02373   assert((dot == end || p - begin != 1) && "Significand has no digits");
02374 
02375   /* Ignore the exponent if we are zero.  */
02376   if (p != firstSignificantDigit) {
02377     int expAdjustment;
02378 
02379     /* Implicit hexadecimal point?  */
02380     if (dot == end)
02381       dot = p;
02382 
02383     /* Calculate the exponent adjustment implicit in the number of
02384        significant digits.  */
02385     expAdjustment = static_cast<int>(dot - firstSignificantDigit);
02386     if (expAdjustment < 0)
02387       expAdjustment++;
02388     expAdjustment = expAdjustment * 4 - 1;
02389 
02390     /* Adjust for writing the significand starting at the most
02391        significant nibble.  */
02392     expAdjustment += semantics->precision;
02393     expAdjustment -= partsCount * integerPartWidth;
02394 
02395     /* Adjust for the given exponent.  */
02396     exponent = totalExponent(p + 1, end, expAdjustment);
02397   }
02398 
02399   return normalize(rounding_mode, lost_fraction);
02400 }
02401 
02402 APFloat::opStatus
02403 APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
02404                                       unsigned sigPartCount, int exp,
02405                                       roundingMode rounding_mode)
02406 {
02407   unsigned int parts, pow5PartCount;
02408   fltSemantics calcSemantics = { 32767, -32767, 0 };
02409   integerPart pow5Parts[maxPowerOfFiveParts];
02410   bool isNearest;
02411 
02412   isNearest = (rounding_mode == rmNearestTiesToEven ||
02413                rounding_mode == rmNearestTiesToAway);
02414 
02415   parts = partCountForBits(semantics->precision + 11);
02416 
02417   /* Calculate pow(5, abs(exp)).  */
02418   pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
02419 
02420   for (;; parts *= 2) {
02421     opStatus sigStatus, powStatus;
02422     unsigned int excessPrecision, truncatedBits;
02423 
02424     calcSemantics.precision = parts * integerPartWidth - 1;
02425     excessPrecision = calcSemantics.precision - semantics->precision;
02426     truncatedBits = excessPrecision;
02427 
02428     APFloat decSig = APFloat::getZero(calcSemantics, sign);
02429     APFloat pow5(calcSemantics);
02430 
02431     sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
02432                                                 rmNearestTiesToEven);
02433     powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
02434                                               rmNearestTiesToEven);
02435     /* Add exp, as 10^n = 5^n * 2^n.  */
02436     decSig.exponent += exp;
02437 
02438     lostFraction calcLostFraction;
02439     integerPart HUerr, HUdistance;
02440     unsigned int powHUerr;
02441 
02442     if (exp >= 0) {
02443       /* multiplySignificand leaves the precision-th bit set to 1.  */
02444       calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
02445       powHUerr = powStatus != opOK;
02446     } else {
02447       calcLostFraction = decSig.divideSignificand(pow5);
02448       /* Denormal numbers have less precision.  */
02449       if (decSig.exponent < semantics->minExponent) {
02450         excessPrecision += (semantics->minExponent - decSig.exponent);
02451         truncatedBits = excessPrecision;
02452         if (excessPrecision > calcSemantics.precision)
02453           excessPrecision = calcSemantics.precision;
02454       }
02455       /* Extra half-ulp lost in reciprocal of exponent.  */
02456       powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
02457     }
02458 
02459     /* Both multiplySignificand and divideSignificand return the
02460        result with the integer bit set.  */
02461     assert(APInt::tcExtractBit
02462            (decSig.significandParts(), calcSemantics.precision - 1) == 1);
02463 
02464     HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
02465                        powHUerr);
02466     HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
02467                                       excessPrecision, isNearest);
02468 
02469     /* Are we guaranteed to round correctly if we truncate?  */
02470     if (HUdistance >= HUerr) {
02471       APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
02472                        calcSemantics.precision - excessPrecision,
02473                        excessPrecision);
02474       /* Take the exponent of decSig.  If we tcExtract-ed less bits
02475          above we must adjust our exponent to compensate for the
02476          implicit right shift.  */
02477       exponent = (decSig.exponent + semantics->precision
02478                   - (calcSemantics.precision - excessPrecision));
02479       calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
02480                                                        decSig.partCount(),
02481                                                        truncatedBits);
02482       return normalize(rounding_mode, calcLostFraction);
02483     }
02484   }
02485 }
02486 
02487 APFloat::opStatus
02488 APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
02489 {
02490   decimalInfo D;
02491   opStatus fs;
02492 
02493   /* Scan the text.  */
02494   StringRef::iterator p = str.begin();
02495   interpretDecimal(p, str.end(), &D);
02496 
02497   /* Handle the quick cases.  First the case of no significant digits,
02498      i.e. zero, and then exponents that are obviously too large or too
02499      small.  Writing L for log 10 / log 2, a number d.ddddd*10^exp
02500      definitely overflows if
02501 
02502            (exp - 1) * L >= maxExponent
02503 
02504      and definitely underflows to zero where
02505 
02506            (exp + 1) * L <= minExponent - precision
02507 
02508      With integer arithmetic the tightest bounds for L are
02509 
02510            93/28 < L < 196/59            [ numerator <= 256 ]
02511            42039/12655 < L < 28738/8651  [ numerator <= 65536 ]
02512   */
02513 
02514   // Test if we have a zero number allowing for strings with no null terminators
02515   // and zero decimals with non-zero exponents.
02516   // 
02517   // We computed firstSigDigit by ignoring all zeros and dots. Thus if
02518   // D->firstSigDigit equals str.end(), every digit must be a zero and there can
02519   // be at most one dot. On the other hand, if we have a zero with a non-zero
02520   // exponent, then we know that D.firstSigDigit will be non-numeric.
02521   if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
02522     category = fcZero;
02523     fs = opOK;
02524 
02525   /* Check whether the normalized exponent is high enough to overflow
02526      max during the log-rebasing in the max-exponent check below. */
02527   } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
02528     fs = handleOverflow(rounding_mode);
02529 
02530   /* If it wasn't, then it also wasn't high enough to overflow max
02531      during the log-rebasing in the min-exponent check.  Check that it
02532      won't overflow min in either check, then perform the min-exponent
02533      check. */
02534   } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
02535              (D.normalizedExponent + 1) * 28738 <=
02536                8651 * (semantics->minExponent - (int) semantics->precision)) {
02537     /* Underflow to zero and round.  */
02538     category = fcNormal;
02539     zeroSignificand();
02540     fs = normalize(rounding_mode, lfLessThanHalf);
02541 
02542   /* We can finally safely perform the max-exponent check. */
02543   } else if ((D.normalizedExponent - 1) * 42039
02544              >= 12655 * semantics->maxExponent) {
02545     /* Overflow and round.  */
02546     fs = handleOverflow(rounding_mode);
02547   } else {
02548     integerPart *decSignificand;
02549     unsigned int partCount;
02550 
02551     /* A tight upper bound on number of bits required to hold an
02552        N-digit decimal integer is N * 196 / 59.  Allocate enough space
02553        to hold the full significand, and an extra part required by
02554        tcMultiplyPart.  */
02555     partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
02556     partCount = partCountForBits(1 + 196 * partCount / 59);
02557     decSignificand = new integerPart[partCount + 1];
02558     partCount = 0;
02559 
02560     /* Convert to binary efficiently - we do almost all multiplication
02561        in an integerPart.  When this would overflow do we do a single
02562        bignum multiplication, and then revert again to multiplication
02563        in an integerPart.  */
02564     do {
02565       integerPart decValue, val, multiplier;
02566 
02567       val = 0;
02568       multiplier = 1;
02569 
02570       do {
02571         if (*p == '.') {
02572           p++;
02573           if (p == str.end()) {
02574             break;
02575           }
02576         }
02577         decValue = decDigitValue(*p++);
02578         assert(decValue < 10U && "Invalid character in significand");
02579         multiplier *= 10;
02580         val = val * 10 + decValue;
02581         /* The maximum number that can be multiplied by ten with any
02582            digit added without overflowing an integerPart.  */
02583       } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
02584 
02585       /* Multiply out the current part.  */
02586       APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
02587                             partCount, partCount + 1, false);
02588 
02589       /* If we used another part (likely but not guaranteed), increase
02590          the count.  */
02591       if (decSignificand[partCount])
02592         partCount++;
02593     } while (p <= D.lastSigDigit);
02594 
02595     category = fcNormal;
02596     fs = roundSignificandWithExponent(decSignificand, partCount,
02597                                       D.exponent, rounding_mode);
02598 
02599     delete [] decSignificand;
02600   }
02601 
02602   return fs;
02603 }
02604 
02605 bool
02606 APFloat::convertFromStringSpecials(StringRef str) {
02607   if (str.equals("inf") || str.equals("INFINITY")) {
02608     makeInf(false);
02609     return true;
02610   }
02611 
02612   if (str.equals("-inf") || str.equals("-INFINITY")) {
02613     makeInf(true);
02614     return true;
02615   }
02616 
02617   if (str.equals("nan") || str.equals("NaN")) {
02618     makeNaN(false, false);
02619     return true;
02620   }
02621 
02622   if (str.equals("-nan") || str.equals("-NaN")) {
02623     makeNaN(false, true);
02624     return true;
02625   }
02626 
02627   return false;
02628 }
02629 
02630 APFloat::opStatus
02631 APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
02632 {
02633   assert(!str.empty() && "Invalid string length");
02634 
02635   // Handle special cases.
02636   if (convertFromStringSpecials(str))
02637     return opOK;
02638 
02639   /* Handle a leading minus sign.  */
02640   StringRef::iterator p = str.begin();
02641   size_t slen = str.size();
02642   sign = *p == '-' ? 1 : 0;
02643   if (*p == '-' || *p == '+') {
02644     p++;
02645     slen--;
02646     assert(slen && "String has no digits");
02647   }
02648 
02649   if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02650     assert(slen - 2 && "Invalid string");
02651     return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
02652                                         rounding_mode);
02653   }
02654 
02655   return convertFromDecimalString(StringRef(p, slen), rounding_mode);
02656 }
02657 
02658 /* Write out a hexadecimal representation of the floating point value
02659    to DST, which must be of sufficient size, in the C99 form
02660    [-]0xh.hhhhp[+-]d.  Return the number of characters written,
02661    excluding the terminating NUL.
02662 
02663    If UPPERCASE, the output is in upper case, otherwise in lower case.
02664 
02665    HEXDIGITS digits appear altogether, rounding the value if
02666    necessary.  If HEXDIGITS is 0, the minimal precision to display the
02667    number precisely is used instead.  If nothing would appear after
02668    the decimal point it is suppressed.
02669 
02670    The decimal exponent is always printed and has at least one digit.
02671    Zero values display an exponent of zero.  Infinities and NaNs
02672    appear as "infinity" or "nan" respectively.
02673 
02674    The above rules are as specified by C99.  There is ambiguity about
02675    what the leading hexadecimal digit should be.  This implementation
02676    uses whatever is necessary so that the exponent is displayed as
02677    stored.  This implies the exponent will fall within the IEEE format
02678    range, and the leading hexadecimal digit will be 0 (for denormals),
02679    1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
02680    any other digits zero).
02681 */
02682 unsigned int
02683 APFloat::convertToHexString(char *dst, unsigned int hexDigits,
02684                             bool upperCase, roundingMode rounding_mode) const
02685 {
02686   char *p;
02687 
02688   p = dst;
02689   if (sign)
02690     *dst++ = '-';
02691 
02692   switch (category) {
02693   case fcInfinity:
02694     memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
02695     dst += sizeof infinityL - 1;
02696     break;
02697 
02698   case fcNaN:
02699     memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
02700     dst += sizeof NaNU - 1;
02701     break;
02702 
02703   case fcZero:
02704     *dst++ = '0';
02705     *dst++ = upperCase ? 'X': 'x';
02706     *dst++ = '0';
02707     if (hexDigits > 1) {
02708       *dst++ = '.';
02709       memset (dst, '0', hexDigits - 1);
02710       dst += hexDigits - 1;
02711     }
02712     *dst++ = upperCase ? 'P': 'p';
02713     *dst++ = '0';
02714     break;
02715 
02716   case fcNormal:
02717     dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
02718     break;
02719   }
02720 
02721   *dst = 0;
02722 
02723   return static_cast<unsigned int>(dst - p);
02724 }
02725 
02726 /* Does the hard work of outputting the correctly rounded hexadecimal
02727    form of a normal floating point number with the specified number of
02728    hexadecimal digits.  If HEXDIGITS is zero the minimum number of
02729    digits necessary to print the value precisely is output.  */
02730 char *
02731 APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
02732                                   bool upperCase,
02733                                   roundingMode rounding_mode) const
02734 {
02735   unsigned int count, valueBits, shift, partsCount, outputDigits;
02736   const char *hexDigitChars;
02737   const integerPart *significand;
02738   char *p;
02739   bool roundUp;
02740 
02741   *dst++ = '0';
02742   *dst++ = upperCase ? 'X': 'x';
02743 
02744   roundUp = false;
02745   hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
02746 
02747   significand = significandParts();
02748   partsCount = partCount();
02749 
02750   /* +3 because the first digit only uses the single integer bit, so
02751      we have 3 virtual zero most-significant-bits.  */
02752   valueBits = semantics->precision + 3;
02753   shift = integerPartWidth - valueBits % integerPartWidth;
02754 
02755   /* The natural number of digits required ignoring trailing
02756      insignificant zeroes.  */
02757   outputDigits = (valueBits - significandLSB () + 3) / 4;
02758 
02759   /* hexDigits of zero means use the required number for the
02760      precision.  Otherwise, see if we are truncating.  If we are,
02761      find out if we need to round away from zero.  */
02762   if (hexDigits) {
02763     if (hexDigits < outputDigits) {
02764       /* We are dropping non-zero bits, so need to check how to round.
02765          "bits" is the number of dropped bits.  */
02766       unsigned int bits;
02767       lostFraction fraction;
02768 
02769       bits = valueBits - hexDigits * 4;
02770       fraction = lostFractionThroughTruncation (significand, partsCount, bits);
02771       roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
02772     }
02773     outputDigits = hexDigits;
02774   }
02775 
02776   /* Write the digits consecutively, and start writing in the location
02777      of the hexadecimal point.  We move the most significant digit
02778      left and add the hexadecimal point later.  */
02779   p = ++dst;
02780 
02781   count = (valueBits + integerPartWidth - 1) / integerPartWidth;
02782 
02783   while (outputDigits && count) {
02784     integerPart part;
02785 
02786     /* Put the most significant integerPartWidth bits in "part".  */
02787     if (--count == partsCount)
02788       part = 0;  /* An imaginary higher zero part.  */
02789     else
02790       part = significand[count] << shift;
02791 
02792     if (count && shift)
02793       part |= significand[count - 1] >> (integerPartWidth - shift);
02794 
02795     /* Convert as much of "part" to hexdigits as we can.  */
02796     unsigned int curDigits = integerPartWidth / 4;
02797 
02798     if (curDigits > outputDigits)
02799       curDigits = outputDigits;
02800     dst += partAsHex (dst, part, curDigits, hexDigitChars);
02801     outputDigits -= curDigits;
02802   }
02803 
02804   if (roundUp) {
02805     char *q = dst;
02806 
02807     /* Note that hexDigitChars has a trailing '0'.  */
02808     do {
02809       q--;
02810       *q = hexDigitChars[hexDigitValue (*q) + 1];
02811     } while (*q == '0');
02812     assert(q >= p);
02813   } else {
02814     /* Add trailing zeroes.  */
02815     memset (dst, '0', outputDigits);
02816     dst += outputDigits;
02817   }
02818 
02819   /* Move the most significant digit to before the point, and if there
02820      is something after the decimal point add it.  This must come
02821      after rounding above.  */
02822   p[-1] = p[0];
02823   if (dst -1 == p)
02824     dst--;
02825   else
02826     p[0] = '.';
02827 
02828   /* Finally output the exponent.  */
02829   *dst++ = upperCase ? 'P': 'p';
02830 
02831   return writeSignedDecimal (dst, exponent);
02832 }
02833 
02834 hash_code llvm::hash_value(const APFloat &Arg) {
02835   if (!Arg.isFiniteNonZero())
02836     return hash_combine((uint8_t)Arg.category,
02837                         // NaN has no sign, fix it at zero.
02838                         Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
02839                         Arg.semantics->precision);
02840 
02841   // Normal floats need their exponent and significand hashed.
02842   return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
02843                       Arg.semantics->precision, Arg.exponent,
02844                       hash_combine_range(
02845                         Arg.significandParts(),
02846                         Arg.significandParts() + Arg.partCount()));
02847 }
02848 
02849 // Conversion from APFloat to/from host float/double.  It may eventually be
02850 // possible to eliminate these and have everybody deal with APFloats, but that
02851 // will take a while.  This approach will not easily extend to long double.
02852 // Current implementation requires integerPartWidth==64, which is correct at
02853 // the moment but could be made more general.
02854 
02855 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
02856 // the actual IEEE respresentations.  We compensate for that here.
02857 
02858 APInt
02859 APFloat::convertF80LongDoubleAPFloatToAPInt() const
02860 {
02861   assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
02862   assert(partCount()==2);
02863 
02864   uint64_t myexponent, mysignificand;
02865 
02866   if (isFiniteNonZero()) {
02867     myexponent = exponent+16383; //bias
02868     mysignificand = significandParts()[0];
02869     if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
02870       myexponent = 0;   // denormal
02871   } else if (category==fcZero) {
02872     myexponent = 0;
02873     mysignificand = 0;
02874   } else if (category==fcInfinity) {
02875     myexponent = 0x7fff;
02876     mysignificand = 0x8000000000000000ULL;
02877   } else {
02878     assert(category == fcNaN && "Unknown category");
02879     myexponent = 0x7fff;
02880     mysignificand = significandParts()[0];
02881   }
02882 
02883   uint64_t words[2];
02884   words[0] = mysignificand;
02885   words[1] =  ((uint64_t)(sign & 1) << 15) |
02886               (myexponent & 0x7fffLL);
02887   return APInt(80, words);
02888 }
02889 
02890 APInt
02891 APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
02892 {
02893   assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
02894   assert(partCount()==2);
02895 
02896   uint64_t words[2];
02897   opStatus fs;
02898   bool losesInfo;
02899 
02900   // Convert number to double.  To avoid spurious underflows, we re-
02901   // normalize against the "double" minExponent first, and only *then*
02902   // truncate the mantissa.  The result of that second conversion
02903   // may be inexact, but should never underflow.
02904   // Declare fltSemantics before APFloat that uses it (and
02905   // saves pointer to it) to ensure correct destruction order.
02906   fltSemantics extendedSemantics = *semantics;
02907   extendedSemantics.minExponent = IEEEdouble.minExponent;
02908   APFloat extended(*this);
02909   fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
02910   assert(fs == opOK && !losesInfo);
02911   (void)fs;
02912 
02913   APFloat u(extended);
02914   fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
02915   assert(fs == opOK || fs == opInexact);
02916   (void)fs;
02917   words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
02918 
02919   // If conversion was exact or resulted in a special case, we're done;
02920   // just set the second double to zero.  Otherwise, re-convert back to
02921   // the extended format and compute the difference.  This now should
02922   // convert exactly to double.
02923   if (u.isFiniteNonZero() && losesInfo) {
02924     fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
02925     assert(fs == opOK && !losesInfo);
02926     (void)fs;
02927 
02928     APFloat v(extended);
02929     v.subtract(u, rmNearestTiesToEven);
02930     fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
02931     assert(fs == opOK && !losesInfo);
02932     (void)fs;
02933     words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
02934   } else {
02935     words[1] = 0;
02936   }
02937 
02938   return APInt(128, words);
02939 }
02940 
02941 APInt
02942 APFloat::convertQuadrupleAPFloatToAPInt() const
02943 {
02944   assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
02945   assert(partCount()==2);
02946 
02947   uint64_t myexponent, mysignificand, mysignificand2;
02948 
02949   if (isFiniteNonZero()) {
02950     myexponent = exponent+16383; //bias
02951     mysignificand = significandParts()[0];
02952     mysignificand2 = significandParts()[1];
02953     if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
02954       myexponent = 0;   // denormal
02955   } else if (category==fcZero) {
02956     myexponent = 0;
02957     mysignificand = mysignificand2 = 0;
02958   } else if (category==fcInfinity) {
02959     myexponent = 0x7fff;
02960     mysignificand = mysignificand2 = 0;
02961   } else {
02962     assert(category == fcNaN && "Unknown category!");
02963     myexponent = 0x7fff;
02964     mysignificand = significandParts()[0];
02965     mysignificand2 = significandParts()[1];
02966   }
02967 
02968   uint64_t words[2];
02969   words[0] = mysignificand;
02970   words[1] = ((uint64_t)(sign & 1) << 63) |
02971              ((myexponent & 0x7fff) << 48) |
02972              (mysignificand2 & 0xffffffffffffLL);
02973 
02974   return APInt(128, words);
02975 }
02976 
02977 APInt
02978 APFloat::convertDoubleAPFloatToAPInt() const
02979 {
02980   assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
02981   assert(partCount()==1);
02982 
02983   uint64_t myexponent, mysignificand;
02984 
02985   if (isFiniteNonZero()) {
02986     myexponent = exponent+1023; //bias
02987     mysignificand = *significandParts();
02988     if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
02989       myexponent = 0;   // denormal
02990   } else if (category==fcZero) {
02991     myexponent = 0;
02992     mysignificand = 0;
02993   } else if (category==fcInfinity) {
02994     myexponent = 0x7ff;
02995     mysignificand = 0;
02996   } else {
02997     assert(category == fcNaN && "Unknown category!");
02998     myexponent = 0x7ff;
02999     mysignificand = *significandParts();
03000   }
03001 
03002   return APInt(64, ((((uint64_t)(sign & 1) << 63) |
03003                      ((myexponent & 0x7ff) <<  52) |
03004                      (mysignificand & 0xfffffffffffffLL))));
03005 }
03006 
03007 APInt
03008 APFloat::convertFloatAPFloatToAPInt() const
03009 {
03010   assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
03011   assert(partCount()==1);
03012 
03013   uint32_t myexponent, mysignificand;
03014 
03015   if (isFiniteNonZero()) {
03016     myexponent = exponent+127; //bias
03017     mysignificand = (uint32_t)*significandParts();
03018     if (myexponent == 1 && !(mysignificand & 0x800000))
03019       myexponent = 0;   // denormal
03020   } else if (category==fcZero) {
03021     myexponent = 0;
03022     mysignificand = 0;
03023   } else if (category==fcInfinity) {
03024     myexponent = 0xff;
03025     mysignificand = 0;
03026   } else {
03027     assert(category == fcNaN && "Unknown category!");
03028     myexponent = 0xff;
03029     mysignificand = (uint32_t)*significandParts();
03030   }
03031 
03032   return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
03033                     (mysignificand & 0x7fffff)));
03034 }
03035 
03036 APInt
03037 APFloat::convertHalfAPFloatToAPInt() const
03038 {
03039   assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
03040   assert(partCount()==1);
03041 
03042   uint32_t myexponent, mysignificand;
03043 
03044   if (isFiniteNonZero()) {
03045     myexponent = exponent+15; //bias
03046     mysignificand = (uint32_t)*significandParts();
03047     if (myexponent == 1 && !(mysignificand & 0x400))
03048       myexponent = 0;   // denormal
03049   } else if (category==fcZero) {
03050     myexponent = 0;
03051     mysignificand = 0;
03052   } else if (category==fcInfinity) {
03053     myexponent = 0x1f;
03054     mysignificand = 0;
03055   } else {
03056     assert(category == fcNaN && "Unknown category!");
03057     myexponent = 0x1f;
03058     mysignificand = (uint32_t)*significandParts();
03059   }
03060 
03061   return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
03062                     (mysignificand & 0x3ff)));
03063 }
03064 
03065 // This function creates an APInt that is just a bit map of the floating
03066 // point constant as it would appear in memory.  It is not a conversion,
03067 // and treating the result as a normal integer is unlikely to be useful.
03068 
03069 APInt
03070 APFloat::bitcastToAPInt() const
03071 {
03072   if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
03073     return convertHalfAPFloatToAPInt();
03074 
03075   if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
03076     return convertFloatAPFloatToAPInt();
03077 
03078   if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
03079     return convertDoubleAPFloatToAPInt();
03080 
03081   if (semantics == (const llvm::fltSemantics*)&IEEEquad)
03082     return convertQuadrupleAPFloatToAPInt();
03083 
03084   if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
03085     return convertPPCDoubleDoubleAPFloatToAPInt();
03086 
03087   assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
03088          "unknown format!");
03089   return convertF80LongDoubleAPFloatToAPInt();
03090 }
03091 
03092 float
03093 APFloat::convertToFloat() const
03094 {
03095   assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
03096          "Float semantics are not IEEEsingle");
03097   APInt api = bitcastToAPInt();
03098   return api.bitsToFloat();
03099 }
03100 
03101 double
03102 APFloat::convertToDouble() const
03103 {
03104   assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
03105          "Float semantics are not IEEEdouble");
03106   APInt api = bitcastToAPInt();
03107   return api.bitsToDouble();
03108 }
03109 
03110 /// Integer bit is explicit in this format.  Intel hardware (387 and later)
03111 /// does not support these bit patterns:
03112 ///  exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
03113 ///  exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
03114 ///  exponent = 0, integer bit 1 ("pseudodenormal")
03115 ///  exponent!=0 nor all 1's, integer bit 0 ("unnormal")
03116 /// At the moment, the first two are treated as NaNs, the second two as Normal.
03117 void
03118 APFloat::initFromF80LongDoubleAPInt(const APInt &api)
03119 {
03120   assert(api.getBitWidth()==80);
03121   uint64_t i1 = api.getRawData()[0];
03122   uint64_t i2 = api.getRawData()[1];
03123   uint64_t myexponent = (i2 & 0x7fff);
03124   uint64_t mysignificand = i1;
03125 
03126   initialize(&APFloat::x87DoubleExtended);
03127   assert(partCount()==2);
03128 
03129   sign = static_cast<unsigned int>(i2>>15);
03130   if (myexponent==0 && mysignificand==0) {
03131     // exponent, significand meaningless
03132     category = fcZero;
03133   } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
03134     // exponent, significand meaningless
03135     category = fcInfinity;
03136   } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
03137     // exponent meaningless
03138     category = fcNaN;
03139     significandParts()[0] = mysignificand;
03140     significandParts()[1] = 0;
03141   } else {
03142     category = fcNormal;
03143     exponent = myexponent - 16383;
03144     significandParts()[0] = mysignificand;
03145     significandParts()[1] = 0;
03146     if (myexponent==0)          // denormal
03147       exponent = -16382;
03148   }
03149 }
03150 
03151 void
03152 APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
03153 {
03154   assert(api.getBitWidth()==128);
03155   uint64_t i1 = api.getRawData()[0];
03156   uint64_t i2 = api.getRawData()[1];
03157   opStatus fs;
03158   bool losesInfo;
03159 
03160   // Get the first double and convert to our format.
03161   initFromDoubleAPInt(APInt(64, i1));
03162   fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
03163   assert(fs == opOK && !losesInfo);
03164   (void)fs;
03165 
03166   // Unless we have a special case, add in second double.
03167   if (isFiniteNonZero()) {
03168     APFloat v(IEEEdouble, APInt(64, i2));
03169     fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
03170     assert(fs == opOK && !losesInfo);
03171     (void)fs;
03172 
03173     add(v, rmNearestTiesToEven);
03174   }
03175 }
03176 
03177 void
03178 APFloat::initFromQuadrupleAPInt(const APInt &api)
03179 {
03180   assert(api.getBitWidth()==128);
03181   uint64_t i1 = api.getRawData()[0];
03182   uint64_t i2 = api.getRawData()[1];
03183   uint64_t myexponent = (i2 >> 48) & 0x7fff;
03184   uint64_t mysignificand  = i1;
03185   uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
03186 
03187   initialize(&APFloat::IEEEquad);
03188   assert(partCount()==2);
03189 
03190   sign = static_cast<unsigned int>(i2>>63);
03191   if (myexponent==0 &&
03192       (mysignificand==0 && mysignificand2==0)) {
03193     // exponent, significand meaningless
03194     category = fcZero;
03195   } else if (myexponent==0x7fff &&
03196              (mysignificand==0 && mysignificand2==0)) {
03197     // exponent, significand meaningless
03198     category = fcInfinity;
03199   } else if (myexponent==0x7fff &&
03200              (mysignificand!=0 || mysignificand2 !=0)) {
03201     // exponent meaningless
03202     category = fcNaN;
03203     significandParts()[0] = mysignificand;
03204     significandParts()[1] = mysignificand2;
03205   } else {
03206     category = fcNormal;
03207     exponent = myexponent - 16383;
03208     significandParts()[0] = mysignificand;
03209     significandParts()[1] = mysignificand2;
03210     if (myexponent==0)          // denormal
03211       exponent = -16382;
03212     else
03213       significandParts()[1] |= 0x1000000000000LL;  // integer bit
03214   }
03215 }
03216 
03217 void
03218 APFloat::initFromDoubleAPInt(const APInt &api)
03219 {
03220   assert(api.getBitWidth()==64);
03221   uint64_t i = *api.getRawData();
03222   uint64_t myexponent = (i >> 52) & 0x7ff;
03223   uint64_t mysignificand = i & 0xfffffffffffffLL;
03224 
03225   initialize(&APFloat::IEEEdouble);
03226   assert(partCount()==1);
03227 
03228   sign = static_cast<unsigned int>(i>>63);
03229   if (myexponent==0 && mysignificand==0) {
03230     // exponent, significand meaningless
03231     category = fcZero;
03232   } else if (myexponent==0x7ff && mysignificand==0) {
03233     // exponent, significand meaningless
03234     category = fcInfinity;
03235   } else if (myexponent==0x7ff && mysignificand!=0) {
03236     // exponent meaningless
03237     category = fcNaN;
03238     *significandParts() = mysignificand;
03239   } else {
03240     category = fcNormal;
03241     exponent = myexponent - 1023;
03242     *significandParts() = mysignificand;
03243     if (myexponent==0)          // denormal
03244       exponent = -1022;
03245     else
03246       *significandParts() |= 0x10000000000000LL;  // integer bit
03247   }
03248 }
03249 
03250 void
03251 APFloat::initFromFloatAPInt(const APInt & api)
03252 {
03253   assert(api.getBitWidth()==32);
03254   uint32_t i = (uint32_t)*api.getRawData();
03255   uint32_t myexponent = (i >> 23) & 0xff;
03256   uint32_t mysignificand = i & 0x7fffff;
03257 
03258   initialize(&APFloat::IEEEsingle);
03259   assert(partCount()==1);
03260 
03261   sign = i >> 31;
03262   if (myexponent==0 && mysignificand==0) {
03263     // exponent, significand meaningless
03264     category = fcZero;
03265   } else if (myexponent==0xff && mysignificand==0) {
03266     // exponent, significand meaningless
03267     category = fcInfinity;
03268   } else if (myexponent==0xff && mysignificand!=0) {
03269     // sign, exponent, significand meaningless
03270     category = fcNaN;
03271     *significandParts() = mysignificand;
03272   } else {
03273     category = fcNormal;
03274     exponent = myexponent - 127;  //bias
03275     *significandParts() = mysignificand;
03276     if (myexponent==0)    // denormal
03277       exponent = -126;
03278     else
03279       *significandParts() |= 0x800000; // integer bit
03280   }
03281 }
03282 
03283 void
03284 APFloat::initFromHalfAPInt(const APInt & api)
03285 {
03286   assert(api.getBitWidth()==16);
03287   uint32_t i = (uint32_t)*api.getRawData();
03288   uint32_t myexponent = (i >> 10) & 0x1f;
03289   uint32_t mysignificand = i & 0x3ff;
03290 
03291   initialize(&APFloat::IEEEhalf);
03292   assert(partCount()==1);
03293 
03294   sign = i >> 15;
03295   if (myexponent==0 && mysignificand==0) {
03296     // exponent, significand meaningless
03297     category = fcZero;
03298   } else if (myexponent==0x1f && mysignificand==0) {
03299     // exponent, significand meaningless
03300     category = fcInfinity;
03301   } else if (myexponent==0x1f && mysignificand!=0) {
03302     // sign, exponent, significand meaningless
03303     category = fcNaN;
03304     *significandParts() = mysignificand;
03305   } else {
03306     category = fcNormal;
03307     exponent = myexponent - 15;  //bias
03308     *significandParts() = mysignificand;
03309     if (myexponent==0)    // denormal
03310       exponent = -14;
03311     else
03312       *significandParts() |= 0x400; // integer bit
03313   }
03314 }
03315 
03316 /// Treat api as containing the bits of a floating point number.  Currently
03317 /// we infer the floating point type from the size of the APInt.  The
03318 /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
03319 /// when the size is anything else).
03320 void
03321 APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
03322 {
03323   if (Sem == &IEEEhalf)
03324     return initFromHalfAPInt(api);
03325   if (Sem == &IEEEsingle)
03326     return initFromFloatAPInt(api);
03327   if (Sem == &IEEEdouble)
03328     return initFromDoubleAPInt(api);
03329   if (Sem == &x87DoubleExtended)
03330     return initFromF80LongDoubleAPInt(api);
03331   if (Sem == &IEEEquad)
03332     return initFromQuadrupleAPInt(api);
03333   if (Sem == &PPCDoubleDouble)
03334     return initFromPPCDoubleDoubleAPInt(api);
03335 
03336   llvm_unreachable(nullptr);
03337 }
03338 
03339 APFloat
03340 APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
03341 {
03342   switch (BitWidth) {
03343   case 16:
03344     return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
03345   case 32:
03346     return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
03347   case 64:
03348     return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
03349   case 80:
03350     return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
03351   case 128:
03352     if (isIEEE)
03353       return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
03354     return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
03355   default:
03356     llvm_unreachable("Unknown floating bit width");
03357   }
03358 }
03359 
03360 /// Make this number the largest magnitude normal number in the given
03361 /// semantics.
03362 void APFloat::makeLargest(bool Negative) {
03363   // We want (in interchange format):
03364   //   sign = {Negative}
03365   //   exponent = 1..10
03366   //   significand = 1..1
03367   category = fcNormal;
03368   sign = Negative;
03369   exponent = semantics->maxExponent;
03370 
03371   // Use memset to set all but the highest integerPart to all ones.
03372   integerPart *significand = significandParts();
03373   unsigned PartCount = partCount();
03374   memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
03375 
03376   // Set the high integerPart especially setting all unused top bits for
03377   // internal consistency.
03378   const unsigned NumUnusedHighBits =
03379     PartCount*integerPartWidth - semantics->precision;
03380   significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
03381                                    ? (~integerPart(0) >> NumUnusedHighBits)
03382                                    : 0;
03383 }
03384 
03385 /// Make this number the smallest magnitude denormal number in the given
03386 /// semantics.
03387 void APFloat::makeSmallest(bool Negative) {
03388   // We want (in interchange format):
03389   //   sign = {Negative}
03390   //   exponent = 0..0
03391   //   significand = 0..01
03392   category = fcNormal;
03393   sign = Negative;
03394   exponent = semantics->minExponent;
03395   APInt::tcSet(significandParts(), 1, partCount());
03396 }
03397 
03398 
03399 APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
03400   // We want (in interchange format):
03401   //   sign = {Negative}
03402   //   exponent = 1..10
03403   //   significand = 1..1
03404   APFloat Val(Sem, uninitialized);
03405   Val.makeLargest(Negative);
03406   return Val;
03407 }
03408 
03409 APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
03410   // We want (in interchange format):
03411   //   sign = {Negative}
03412   //   exponent = 0..0
03413   //   significand = 0..01
03414   APFloat Val(Sem, uninitialized);
03415   Val.makeSmallest(Negative);
03416   return Val;
03417 }
03418 
03419 APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
03420   APFloat Val(Sem, uninitialized);
03421 
03422   // We want (in interchange format):
03423   //   sign = {Negative}
03424   //   exponent = 0..0
03425   //   significand = 10..0
03426 
03427   Val.category = fcNormal;
03428   Val.zeroSignificand();
03429   Val.sign = Negative;
03430   Val.exponent = Sem.minExponent;
03431   Val.significandParts()[partCountForBits(Sem.precision)-1] |=
03432     (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
03433 
03434   return Val;
03435 }
03436 
03437 APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
03438   initFromAPInt(&Sem, API);
03439 }
03440 
03441 APFloat::APFloat(float f) {
03442   initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
03443 }
03444 
03445 APFloat::APFloat(double d) {
03446   initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
03447 }
03448 
03449 namespace {
03450   void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
03451     Buffer.append(Str.begin(), Str.end());
03452   }
03453 
03454   /// Removes data from the given significand until it is no more
03455   /// precise than is required for the desired precision.
03456   void AdjustToPrecision(APInt &significand,
03457                          int &exp, unsigned FormatPrecision) {
03458     unsigned bits = significand.getActiveBits();
03459 
03460     // 196/59 is a very slight overestimate of lg_2(10).
03461     unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
03462 
03463     if (bits <= bitsRequired) return;
03464 
03465     unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
03466     if (!tensRemovable) return;
03467 
03468     exp += tensRemovable;
03469 
03470     APInt divisor(significand.getBitWidth(), 1);
03471     APInt powten(significand.getBitWidth(), 10);
03472     while (true) {
03473       if (tensRemovable & 1)
03474         divisor *= powten;
03475       tensRemovable >>= 1;
03476       if (!tensRemovable) break;
03477       powten *= powten;
03478     }
03479 
03480     significand = significand.udiv(divisor);
03481 
03482     // Truncate the significand down to its active bit count.
03483     significand = significand.trunc(significand.getActiveBits());
03484   }
03485 
03486 
03487   void AdjustToPrecision(SmallVectorImpl<char> &buffer,
03488                          int &exp, unsigned FormatPrecision) {
03489     unsigned N = buffer.size();
03490     if (N <= FormatPrecision) return;
03491 
03492     // The most significant figures are the last ones in the buffer.
03493     unsigned FirstSignificant = N - FormatPrecision;
03494 
03495     // Round.
03496     // FIXME: this probably shouldn't use 'round half up'.
03497 
03498     // Rounding down is just a truncation, except we also want to drop
03499     // trailing zeros from the new result.
03500     if (buffer[FirstSignificant - 1] < '5') {
03501       while (FirstSignificant < N && buffer[FirstSignificant] == '0')
03502         FirstSignificant++;
03503 
03504       exp += FirstSignificant;
03505       buffer.erase(&buffer[0], &buffer[FirstSignificant]);
03506       return;
03507     }
03508 
03509     // Rounding up requires a decimal add-with-carry.  If we continue
03510     // the carry, the newly-introduced zeros will just be truncated.
03511     for (unsigned I = FirstSignificant; I != N; ++I) {
03512       if (buffer[I] == '9') {
03513         FirstSignificant++;
03514       } else {
03515         buffer[I]++;
03516         break;
03517       }
03518     }
03519 
03520     // If we carried through, we have exactly one digit of precision.
03521     if (FirstSignificant == N) {
03522       exp += FirstSignificant;
03523       buffer.clear();
03524       buffer.push_back('1');
03525       return;
03526     }
03527 
03528     exp += FirstSignificant;
03529     buffer.erase(&buffer[0], &buffer[FirstSignificant]);
03530   }
03531 }
03532 
03533 void APFloat::toString(SmallVectorImpl<char> &Str,
03534                        unsigned FormatPrecision,
03535                        unsigned FormatMaxPadding) const {
03536   switch (category) {
03537   case fcInfinity:
03538     if (isNegative())
03539       return append(Str, "-Inf");
03540     else
03541       return append(Str, "+Inf");
03542 
03543   case fcNaN: return append(Str, "NaN");
03544 
03545   case fcZero:
03546     if (isNegative())
03547       Str.push_back('-');
03548 
03549     if (!FormatMaxPadding)
03550       append(Str, "0.0E+0");
03551     else
03552       Str.push_back('0');
03553     return;
03554 
03555   case fcNormal:
03556     break;
03557   }
03558 
03559   if (isNegative())
03560     Str.push_back('-');
03561 
03562   // Decompose the number into an APInt and an exponent.
03563   int exp = exponent - ((int) semantics->precision - 1);
03564   APInt significand(semantics->precision,
03565                     makeArrayRef(significandParts(),
03566                                  partCountForBits(semantics->precision)));
03567 
03568   // Set FormatPrecision if zero.  We want to do this before we
03569   // truncate trailing zeros, as those are part of the precision.
03570   if (!FormatPrecision) {
03571     // We use enough digits so the number can be round-tripped back to an
03572     // APFloat. The formula comes from "How to Print Floating-Point Numbers
03573     // Accurately" by Steele and White.
03574     // FIXME: Using a formula based purely on the precision is conservative;
03575     // we can print fewer digits depending on the actual value being printed.
03576 
03577     // FormatPrecision = 2 + floor(significandBits / lg_2(10))
03578     FormatPrecision = 2 + semantics->precision * 59 / 196;
03579   }
03580 
03581   // Ignore trailing binary zeros.
03582   int trailingZeros = significand.countTrailingZeros();
03583   exp += trailingZeros;
03584   significand = significand.lshr(trailingZeros);
03585 
03586   // Change the exponent from 2^e to 10^e.
03587   if (exp == 0) {
03588     // Nothing to do.
03589   } else if (exp > 0) {
03590     // Just shift left.
03591     significand = significand.zext(semantics->precision + exp);
03592     significand <<= exp;
03593     exp = 0;
03594   } else { /* exp < 0 */
03595     int texp = -exp;
03596 
03597     // We transform this using the identity:
03598     //   (N)(2^-e) == (N)(5^e)(10^-e)
03599     // This means we have to multiply N (the significand) by 5^e.
03600     // To avoid overflow, we have to operate on numbers large
03601     // enough to store N * 5^e:
03602     //   log2(N * 5^e) == log2(N) + e * log2(5)
03603     //                 <= semantics->precision + e * 137 / 59
03604     //   (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
03605 
03606     unsigned precision = semantics->precision + (137 * texp + 136) / 59;
03607 
03608     // Multiply significand by 5^e.
03609     //   N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
03610     significand = significand.zext(precision);
03611     APInt five_to_the_i(precision, 5);
03612     while (true) {
03613       if (texp & 1) significand *= five_to_the_i;
03614 
03615       texp >>= 1;
03616       if (!texp) break;
03617       five_to_the_i *= five_to_the_i;
03618     }
03619   }
03620 
03621   AdjustToPrecision(significand, exp, FormatPrecision);
03622 
03623   SmallVector<char, 256> buffer;
03624 
03625   // Fill the buffer.
03626   unsigned precision = significand.getBitWidth();
03627   APInt ten(precision, 10);
03628   APInt digit(precision, 0);
03629 
03630   bool inTrail = true;
03631   while (significand != 0) {
03632     // digit <- significand % 10
03633     // significand <- significand / 10
03634     APInt::udivrem(significand, ten, significand, digit);
03635 
03636     unsigned d = digit.getZExtValue();
03637 
03638     // Drop trailing zeros.
03639     if (inTrail && !d) exp++;
03640     else {
03641       buffer.push_back((char) ('0' + d));
03642       inTrail = false;
03643     }
03644   }
03645 
03646   assert(!buffer.empty() && "no characters in buffer!");
03647 
03648   // Drop down to FormatPrecision.
03649   // TODO: don't do more precise calculations above than are required.
03650   AdjustToPrecision(buffer, exp, FormatPrecision);
03651 
03652   unsigned NDigits = buffer.size();
03653 
03654   // Check whether we should use scientific notation.
03655   bool FormatScientific;
03656   if (!FormatMaxPadding)
03657     FormatScientific = true;
03658   else {
03659     if (exp >= 0) {
03660       // 765e3 --> 765000
03661       //              ^^^
03662       // But we shouldn't make the number look more precise than it is.
03663       FormatScientific = ((unsigned) exp > FormatMaxPadding ||
03664                           NDigits + (unsigned) exp > FormatPrecision);
03665     } else {
03666       // Power of the most significant digit.
03667       int MSD = exp + (int) (NDigits - 1);
03668       if (MSD >= 0) {
03669         // 765e-2 == 7.65
03670         FormatScientific = false;
03671       } else {
03672         // 765e-5 == 0.00765
03673         //           ^ ^^
03674         FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
03675       }
03676     }
03677   }
03678 
03679   // Scientific formatting is pretty straightforward.
03680   if (FormatScientific) {
03681     exp += (NDigits - 1);
03682 
03683     Str.push_back(buffer[NDigits-1]);
03684     Str.push_back('.');
03685     if (NDigits == 1)
03686       Str.push_back('0');
03687     else
03688       for (unsigned I = 1; I != NDigits; ++I)
03689         Str.push_back(buffer[NDigits-1-I]);
03690     Str.push_back('E');
03691 
03692     Str.push_back(exp >= 0 ? '+' : '-');
03693     if (exp < 0) exp = -exp;
03694     SmallVector<char, 6> expbuf;
03695     do {
03696       expbuf.push_back((char) ('0' + (exp % 10)));
03697       exp /= 10;
03698     } while (exp);
03699     for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
03700       Str.push_back(expbuf[E-1-I]);
03701     return;
03702   }
03703 
03704   // Non-scientific, positive exponents.
03705   if (exp >= 0) {
03706     for (unsigned I = 0; I != NDigits; ++I)
03707       Str.push_back(buffer[NDigits-1-I]);
03708     for (unsigned I = 0; I != (unsigned) exp; ++I)
03709       Str.push_back('0');
03710     return;
03711   }
03712 
03713   // Non-scientific, negative exponents.
03714 
03715   // The number of digits to the left of the decimal point.
03716   int NWholeDigits = exp + (int) NDigits;
03717 
03718   unsigned I = 0;
03719   if (NWholeDigits > 0) {
03720     for (; I != (unsigned) NWholeDigits; ++I)
03721       Str.push_back(buffer[NDigits-I-1]);
03722     Str.push_back('.');
03723   } else {
03724     unsigned NZeros = 1 + (unsigned) -NWholeDigits;
03725 
03726     Str.push_back('0');
03727     Str.push_back('.');
03728     for (unsigned Z = 1; Z != NZeros; ++Z)
03729       Str.push_back('0');
03730   }
03731 
03732   for (; I != NDigits; ++I)
03733     Str.push_back(buffer[NDigits-I-1]);
03734 }
03735 
03736 bool APFloat::getExactInverse(APFloat *inv) const {
03737   // Special floats and denormals have no exact inverse.
03738   if (!isFiniteNonZero())
03739     return false;
03740 
03741   // Check that the number is a power of two by making sure that only the
03742   // integer bit is set in the significand.
03743   if (significandLSB() != semantics->precision - 1)
03744     return false;
03745 
03746   // Get the inverse.
03747   APFloat reciprocal(*semantics, 1ULL);
03748   if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
03749     return false;
03750 
03751   // Avoid multiplication with a denormal, it is not safe on all platforms and
03752   // may be slower than a normal division.
03753   if (reciprocal.isDenormal())
03754     return false;
03755 
03756   assert(reciprocal.isFiniteNonZero() &&
03757          reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
03758 
03759   if (inv)
03760     *inv = reciprocal;
03761 
03762   return true;
03763 }
03764 
03765 bool APFloat::isSignaling() const {
03766   if (!isNaN())
03767     return false;
03768 
03769   // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
03770   // first bit of the trailing significand being 0.
03771   return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
03772 }
03773 
03774 /// IEEE-754R 2008 5.3.1: nextUp/nextDown.
03775 ///
03776 /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
03777 /// appropriate sign switching before/after the computation.
03778 APFloat::opStatus APFloat::next(bool nextDown) {
03779   // If we are performing nextDown, swap sign so we have -x.
03780   if (nextDown)
03781     changeSign();
03782 
03783   // Compute nextUp(x)
03784   opStatus result = opOK;
03785 
03786   // Handle each float category separately.
03787   switch (category) {
03788   case fcInfinity:
03789     // nextUp(+inf) = +inf
03790     if (!isNegative())
03791       break;
03792     // nextUp(-inf) = -getLargest()
03793     makeLargest(true);
03794     break;
03795   case fcNaN:
03796     // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
03797     // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
03798     //                     change the payload.
03799     if (isSignaling()) {
03800       result = opInvalidOp;
03801       // For consistency, propagate the sign of the sNaN to the qNaN.
03802       makeNaN(false, isNegative(), nullptr);
03803     }
03804     break;
03805   case fcZero:
03806     // nextUp(pm 0) = +getSmallest()
03807     makeSmallest(false);
03808     break;
03809   case fcNormal:
03810     // nextUp(-getSmallest()) = -0
03811     if (isSmallest() && isNegative()) {
03812       APInt::tcSet(significandParts(), 0, partCount());
03813       category = fcZero;
03814       exponent = 0;
03815       break;
03816     }
03817 
03818     // nextUp(getLargest()) == INFINITY
03819     if (isLargest() && !isNegative()) {
03820       APInt::tcSet(significandParts(), 0, partCount());
03821       category = fcInfinity;
03822       exponent = semantics->maxExponent + 1;
03823       break;
03824     }
03825 
03826     // nextUp(normal) == normal + inc.
03827     if (isNegative()) {
03828       // If we are negative, we need to decrement the significand.
03829 
03830       // We only cross a binade boundary that requires adjusting the exponent
03831       // if:
03832       //   1. exponent != semantics->minExponent. This implies we are not in the
03833       //   smallest binade or are dealing with denormals.
03834       //   2. Our significand excluding the integral bit is all zeros.
03835       bool WillCrossBinadeBoundary =
03836         exponent != semantics->minExponent && isSignificandAllZeros();
03837 
03838       // Decrement the significand.
03839       //
03840       // We always do this since:
03841       //   1. If we are dealing with a non-binade decrement, by definition we
03842       //   just decrement the significand.
03843       //   2. If we are dealing with a normal -> normal binade decrement, since
03844       //   we have an explicit integral bit the fact that all bits but the
03845       //   integral bit are zero implies that subtracting one will yield a
03846       //   significand with 0 integral bit and 1 in all other spots. Thus we
03847       //   must just adjust the exponent and set the integral bit to 1.
03848       //   3. If we are dealing with a normal -> denormal binade decrement,
03849       //   since we set the integral bit to 0 when we represent denormals, we
03850       //   just decrement the significand.
03851       integerPart *Parts = significandParts();
03852       APInt::tcDecrement(Parts, partCount());
03853 
03854       if (WillCrossBinadeBoundary) {
03855         // Our result is a normal number. Do the following:
03856         // 1. Set the integral bit to 1.
03857         // 2. Decrement the exponent.
03858         APInt::tcSetBit(Parts, semantics->precision - 1);
03859         exponent--;
03860       }
03861     } else {
03862       // If we are positive, we need to increment the significand.
03863 
03864       // We only cross a binade boundary that requires adjusting the exponent if
03865       // the input is not a denormal and all of said input's significand bits
03866       // are set. If all of said conditions are true: clear the significand, set
03867       // the integral bit to 1, and increment the exponent. If we have a
03868       // denormal always increment since moving denormals and the numbers in the
03869       // smallest normal binade have the same exponent in our representation.
03870       bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
03871 
03872       if (WillCrossBinadeBoundary) {
03873         integerPart *Parts = significandParts();
03874         APInt::tcSet(Parts, 0, partCount());
03875         APInt::tcSetBit(Parts, semantics->precision - 1);
03876         assert(exponent != semantics->maxExponent &&
03877                "We can not increment an exponent beyond the maxExponent allowed"
03878                " by the given floating point semantics.");
03879         exponent++;
03880       } else {
03881         incrementSignificand();
03882       }
03883     }
03884     break;
03885   }
03886 
03887   // If we are performing nextDown, swap sign so we have -nextUp(-x)
03888   if (nextDown)
03889     changeSign();
03890 
03891   return result;
03892 }
03893 
03894 void
03895 APFloat::makeInf(bool Negative) {
03896   category = fcInfinity;
03897   sign = Negative;
03898   exponent = semantics->maxExponent + 1;
03899   APInt::tcSet(significandParts(), 0, partCount());
03900 }
03901 
03902 void
03903 APFloat::makeZero(bool Negative) {
03904   category = fcZero;
03905   sign = Negative;
03906   exponent = semantics->minExponent-1;
03907   APInt::tcSet(significandParts(), 0, partCount());  
03908 }