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