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/FoldingSet.h"
00017 #include "llvm/Support/MathExtras.h"
00018 #include <cstring>
00019 
00020 using namespace llvm;
00021 
00022 #define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
00023 
00024 /* Assumed in hexadecimal significand parsing, and conversion to
00025    hexadecimal strings.  */
00026 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
00027 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
00028 
00029 namespace llvm {
00030 
00031   /* Represents floating point arithmetic semantics.  */
00032   struct fltSemantics {
00033     /* The largest E such that 2^E is representable; this matches the
00034        definition of IEEE 754.  */
00035     exponent_t maxExponent;
00036 
00037     /* The smallest E such that 2^E is a normalized number; this
00038        matches the definition of IEEE 754.  */
00039     exponent_t minExponent;
00040 
00041     /* Number of bits in the significand.  This includes the integer
00042        bit.  */
00043     unsigned int precision;
00044 
00045     /* True if arithmetic is supported.  */
00046     unsigned int arithmeticOK;
00047   };
00048 
00049   const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
00050   const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
00051   const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
00052   const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
00053   const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
00054 
00055   // The PowerPC format consists of two doubles.  It does not map cleanly
00056   // onto the usual format above.  For now only storage of constants of
00057   // this type is supported, no arithmetic.
00058   const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
00059 
00060   /* A tight upper bound on number of parts required to hold the value
00061      pow(5, power) is
00062 
00063        power * 815 / (351 * integerPartWidth) + 1
00064        
00065      However, whilst the result may require only this many parts,
00066      because we are multiplying two values to get it, the
00067      multiplication may require an extra part with the excess part
00068      being zero (consider the trivial case of 1 * 1, tcFullMultiply
00069      requires two parts to hold the single-part result).  So we add an
00070      extra one to guarantee enough space whilst multiplying.  */
00071   const unsigned int maxExponent = 16383;
00072   const unsigned int maxPrecision = 113;
00073   const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
00074   const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
00075                                                 / (351 * integerPartWidth));
00076 }
00077 
00078 /* Put a bunch of private, handy routines in an anonymous namespace.  */
00079 namespace {
00080 
00081   static inline unsigned int
00082   partCountForBits(unsigned int bits)
00083   {
00084     return ((bits) + integerPartWidth - 1) / integerPartWidth;
00085   }
00086 
00087   /* Returns 0U-9U.  Return values >= 10U are not digits.  */
00088   static inline unsigned int
00089   decDigitValue(unsigned int c)
00090   {
00091     return c - '0';
00092   }
00093 
00094   static unsigned int
00095   hexDigitValue(unsigned int c)
00096   {
00097     unsigned int r;
00098 
00099     r = c - '0';
00100     if(r <= 9)
00101       return r;
00102 
00103     r = c - 'A';
00104     if(r <= 5)
00105       return r + 10;
00106 
00107     r = c - 'a';
00108     if(r <= 5)
00109       return r + 10;
00110 
00111     return -1U;
00112   }
00113 
00114   static inline void
00115   assertArithmeticOK(const llvm::fltSemantics &semantics) {
00116     assert(semantics.arithmeticOK
00117            && "Compile-time arithmetic does not support these semantics");
00118   }
00119 
00120   /* Return the value of a decimal exponent of the form
00121      [+-]ddddddd.
00122 
00123      If the exponent overflows, returns a large exponent with the
00124      appropriate sign.  */
00125   static int
00126   readExponent(const char *p)
00127   {
00128     bool isNegative;
00129     unsigned int absExponent;
00130     const unsigned int overlargeExponent = 24000;  /* FIXME.  */
00131 
00132     isNegative = (*p == '-');
00133     if (*p == '-' || *p == '+')
00134       p++;
00135 
00136     absExponent = decDigitValue(*p++);
00137     assert (absExponent < 10U);
00138 
00139     for (;;) {
00140       unsigned int value;
00141 
00142       value = decDigitValue(*p);
00143       if (value >= 10U)
00144         break;
00145 
00146       p++;
00147       value += absExponent * 10;
00148       if (absExponent >= overlargeExponent) {
00149         absExponent = overlargeExponent;
00150         break;
00151       }
00152       absExponent = value;
00153     }
00154 
00155     if (isNegative)
00156       return -(int) absExponent;
00157     else
00158       return (int) absExponent;
00159   }
00160 
00161   /* This is ugly and needs cleaning up, but I don't immediately see
00162      how whilst remaining safe.  */
00163   static int
00164   totalExponent(const char *p, int exponentAdjustment)
00165   {
00166     int unsignedExponent;
00167     bool negative, overflow;
00168     int exponent;
00169 
00170     /* Move past the exponent letter and sign to the digits.  */
00171     p++;
00172     negative = *p == '-';
00173     if(*p == '-' || *p == '+')
00174       p++;
00175 
00176     unsignedExponent = 0;
00177     overflow = false;
00178     for(;;) {
00179       unsigned int value;
00180 
00181       value = decDigitValue(*p);
00182       if(value >= 10U)
00183         break;
00184 
00185       p++;
00186       unsignedExponent = unsignedExponent * 10 + value;
00187       if(unsignedExponent > 65535)
00188         overflow = true;
00189     }
00190 
00191     if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
00192       overflow = true;
00193 
00194     if(!overflow) {
00195       exponent = unsignedExponent;
00196       if(negative)
00197         exponent = -exponent;
00198       exponent += exponentAdjustment;
00199       if(exponent > 65535 || exponent < -65536)
00200         overflow = true;
00201     }
00202 
00203     if(overflow)
00204       exponent = negative ? -65536: 65535;
00205 
00206     return exponent;
00207   }
00208 
00209   static const char *
00210   skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
00211   {
00212     *dot = 0;
00213     while(*p == '0')
00214       p++;
00215 
00216     if(*p == '.') {
00217       *dot = p++;
00218       while(*p == '0')
00219         p++;
00220     }
00221 
00222     return p;
00223   }
00224 
00225   /* Given a normal decimal floating point number of the form
00226 
00227        dddd.dddd[eE][+-]ddd
00228 
00229      where the decimal point and exponent are optional, fill out the
00230      structure D.  Exponent is appropriate if the significand is
00231      treated as an integer, and normalizedExponent if the significand
00232      is taken to have the decimal point after a single leading
00233      non-zero digit.
00234 
00235      If the value is zero, V->firstSigDigit points to a non-digit, and
00236      the return exponent is zero.
00237   */
00238   struct decimalInfo {
00239     const char *firstSigDigit;
00240     const char *lastSigDigit;
00241     int exponent;
00242     int normalizedExponent;
00243   };
00244 
00245   static void
00246   interpretDecimal(const char *p, decimalInfo *D)
00247   {
00248     const char *dot;
00249 
00250     p = skipLeadingZeroesAndAnyDot (p, &dot);
00251 
00252     D->firstSigDigit = p;
00253     D->exponent = 0;
00254     D->normalizedExponent = 0;
00255 
00256     for (;;) {
00257       if (*p == '.') {
00258         assert(dot == 0);
00259         dot = p++;
00260       }
00261       if (decDigitValue(*p) >= 10U)
00262         break;
00263       p++;
00264     }
00265 
00266     /* If number is all zerooes accept any exponent.  */
00267     if (p != D->firstSigDigit) {
00268       if (*p == 'e' || *p == 'E')
00269         D->exponent = readExponent(p + 1);
00270 
00271       /* Implied decimal point?  */
00272       if (!dot)
00273         dot = p;
00274 
00275       /* Drop insignificant trailing zeroes.  */
00276       do
00277         do
00278           p--;
00279         while (*p == '0');
00280       while (*p == '.');
00281 
00282       /* Adjust the exponents for any decimal point.  */
00283       D->exponent += static_cast<exponent_t>((dot - p) - (dot > p));
00284       D->normalizedExponent = (D->exponent +
00285                 static_cast<exponent_t>((p - D->firstSigDigit)
00286                                         - (dot > D->firstSigDigit && dot < p)));
00287     }
00288 
00289     D->lastSigDigit = p;
00290   }
00291 
00292   /* Return the trailing fraction of a hexadecimal number.
00293      DIGITVALUE is the first hex digit of the fraction, P points to
00294      the next digit.  */
00295   static lostFraction
00296   trailingHexadecimalFraction(const char *p, unsigned int digitValue)
00297   {
00298     unsigned int hexDigit;
00299 
00300     /* If the first trailing digit isn't 0 or 8 we can work out the
00301        fraction immediately.  */
00302     if(digitValue > 8)
00303       return lfMoreThanHalf;
00304     else if(digitValue < 8 && digitValue > 0)
00305       return lfLessThanHalf;
00306 
00307     /* Otherwise we need to find the first non-zero digit.  */
00308     while(*p == '0')
00309       p++;
00310 
00311     hexDigit = hexDigitValue(*p);
00312 
00313     /* If we ran off the end it is exactly zero or one-half, otherwise
00314        a little more.  */
00315     if(hexDigit == -1U)
00316       return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
00317     else
00318       return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
00319   }
00320 
00321   /* Return the fraction lost were a bignum truncated losing the least
00322      significant BITS bits.  */
00323   static lostFraction
00324   lostFractionThroughTruncation(const integerPart *parts,
00325                                 unsigned int partCount,
00326                                 unsigned int bits)
00327   {
00328     unsigned int lsb;
00329 
00330     lsb = APInt::tcLSB(parts, partCount);
00331 
00332     /* Note this is guaranteed true if bits == 0, or LSB == -1U.  */
00333     if(bits <= lsb)
00334       return lfExactlyZero;
00335     if(bits == lsb + 1)
00336       return lfExactlyHalf;
00337     if(bits <= partCount * integerPartWidth
00338        && APInt::tcExtractBit(parts, bits - 1))
00339       return lfMoreThanHalf;
00340 
00341     return lfLessThanHalf;
00342   }
00343 
00344   /* Shift DST right BITS bits noting lost fraction.  */
00345   static lostFraction
00346   shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
00347   {
00348     lostFraction lost_fraction;
00349 
00350     lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
00351 
00352     APInt::tcShiftRight(dst, parts, bits);
00353 
00354     return lost_fraction;
00355   }
00356 
00357   /* Combine the effect of two lost fractions.  */
00358   static lostFraction
00359   combineLostFractions(lostFraction moreSignificant,
00360                        lostFraction lessSignificant)
00361   {
00362     if(lessSignificant != lfExactlyZero) {
00363       if(moreSignificant == lfExactlyZero)
00364         moreSignificant = lfLessThanHalf;
00365       else if(moreSignificant == lfExactlyHalf)
00366         moreSignificant = lfMoreThanHalf;
00367     }
00368 
00369     return moreSignificant;
00370   }
00371 
00372   /* The error from the true value, in half-ulps, on multiplying two
00373      floating point numbers, which differ from the value they
00374      approximate by at most HUE1 and HUE2 half-ulps, is strictly less
00375      than the returned value.
00376 
00377      See "How to Read Floating Point Numbers Accurately" by William D
00378      Clinger.  */
00379   static unsigned int
00380   HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
00381   {
00382     assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
00383 
00384     if (HUerr1 + HUerr2 == 0)
00385       return inexactMultiply * 2;  /* <= inexactMultiply half-ulps.  */
00386     else
00387       return inexactMultiply + 2 * (HUerr1 + HUerr2);
00388   }
00389 
00390   /* The number of ulps from the boundary (zero, or half if ISNEAREST)
00391      when the least significant BITS are truncated.  BITS cannot be
00392      zero.  */
00393   static integerPart
00394   ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
00395   {
00396     unsigned int count, partBits;
00397     integerPart part, boundary;
00398 
00399     assert (bits != 0);
00400 
00401     bits--;
00402     count = bits / integerPartWidth;
00403     partBits = bits % integerPartWidth + 1;
00404 
00405     part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
00406 
00407     if (isNearest)
00408       boundary = (integerPart) 1 << (partBits - 1);
00409     else
00410       boundary = 0;
00411 
00412     if (count == 0) {
00413       if (part - boundary <= boundary - part)
00414         return part - boundary;
00415       else
00416         return boundary - part;
00417     }
00418 
00419     if (part == boundary) {
00420       while (--count)
00421         if (parts[count])
00422           return ~(integerPart) 0; /* A lot.  */
00423 
00424       return parts[0];
00425     } else if (part == boundary - 1) {
00426       while (--count)
00427         if (~parts[count])
00428           return ~(integerPart) 0; /* A lot.  */
00429 
00430       return -parts[0];
00431     }
00432 
00433     return ~(integerPart) 0; /* A lot.  */
00434   }
00435 
00436   /* Place pow(5, power) in DST, and return the number of parts used.
00437      DST must be at least one part larger than size of the answer.  */
00438   static unsigned int
00439   powerOf5(integerPart *dst, unsigned int power)
00440   {
00441     static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
00442                                                     15625, 78125 };
00443     static integerPart pow5s[maxPowerOfFiveParts * 2 + 5] = { 78125 * 5 };
00444     static unsigned int partsCount[16] = { 1 };
00445 
00446     integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
00447     unsigned int result;
00448 
00449     assert(power <= maxExponent);
00450 
00451     p1 = dst;
00452     p2 = scratch;
00453 
00454     *p1 = firstEightPowers[power & 7];
00455     power >>= 3;
00456 
00457     result = 1;
00458     pow5 = pow5s;
00459 
00460     for (unsigned int n = 0; power; power >>= 1, n++) {
00461       unsigned int pc;
00462 
00463       pc = partsCount[n];
00464 
00465       /* Calculate pow(5,pow(2,n+3)) if we haven't yet.  */
00466       if (pc == 0) {
00467         pc = partsCount[n - 1];
00468         APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
00469         pc *= 2;
00470         if (pow5[pc - 1] == 0)
00471           pc--;
00472         partsCount[n] = pc;
00473       }
00474 
00475       if (power & 1) {
00476         integerPart *tmp;
00477 
00478         APInt::tcFullMultiply(p2, p1, pow5, result, pc);
00479         result += pc;
00480         if (p2[result - 1] == 0)
00481           result--;
00482 
00483         /* Now result is in p1 with partsCount parts and p2 is scratch
00484            space.  */
00485         tmp = p1, p1 = p2, p2 = tmp;
00486       }
00487 
00488       pow5 += pc;
00489     }
00490 
00491     if (p1 != dst)
00492       APInt::tcAssign(dst, p1, result);
00493 
00494     return result;
00495   }
00496 
00497   /* Zero at the end to avoid modular arithmetic when adding one; used
00498      when rounding up during hexadecimal output.  */
00499   static const char hexDigitsLower[] = "0123456789abcdef0";
00500   static const char hexDigitsUpper[] = "0123456789ABCDEF0";
00501   static const char infinityL[] = "infinity";
00502   static const char infinityU[] = "INFINITY";
00503   static const char NaNL[] = "nan";
00504   static const char NaNU[] = "NAN";
00505 
00506   /* Write out an integerPart in hexadecimal, starting with the most
00507      significant nibble.  Write out exactly COUNT hexdigits, return
00508      COUNT.  */
00509   static unsigned int
00510   partAsHex (char *dst, integerPart part, unsigned int count,
00511              const char *hexDigitChars)
00512   {
00513     unsigned int result = count;
00514 
00515     assert (count != 0 && count <= integerPartWidth / 4);
00516 
00517     part >>= (integerPartWidth - 4 * count);
00518     while (count--) {
00519       dst[count] = hexDigitChars[part & 0xf];
00520       part >>= 4;
00521     }
00522 
00523     return result;
00524   }
00525 
00526   /* Write out an unsigned decimal integer.  */
00527   static char *
00528   writeUnsignedDecimal (char *dst, unsigned int n)
00529   {
00530     char buff[40], *p;
00531 
00532     p = buff;
00533     do
00534       *p++ = '0' + n % 10;
00535     while (n /= 10);
00536 
00537     do
00538       *dst++ = *--p;
00539     while (p != buff);
00540 
00541     return dst;
00542   }
00543 
00544   /* Write out a signed decimal integer.  */
00545   static char *
00546   writeSignedDecimal (char *dst, int value)
00547   {
00548     if (value < 0) {
00549       *dst++ = '-';
00550       dst = writeUnsignedDecimal(dst, -(unsigned) value);
00551     } else
00552       dst = writeUnsignedDecimal(dst, value);
00553 
00554     return dst;
00555   }
00556 }
00557 
00558 /* Constructors.  */
00559 void
00560 APFloat::initialize(const fltSemantics *ourSemantics)
00561 {
00562   unsigned int count;
00563 
00564   semantics = ourSemantics;
00565   count = partCount();
00566   if(count > 1)
00567     significand.parts = new integerPart[count];
00568 }
00569 
00570 void
00571 APFloat::freeSignificand()
00572 {
00573   if(partCount() > 1)
00574     delete [] significand.parts;
00575 }
00576 
00577 void
00578 APFloat::assign(const APFloat &rhs)
00579 {
00580   assert(semantics == rhs.semantics);
00581 
00582   sign = rhs.sign;
00583   category = rhs.category;
00584   exponent = rhs.exponent;
00585   sign2 = rhs.sign2;
00586   exponent2 = rhs.exponent2;
00587   if(category == fcNormal || category == fcNaN)
00588     copySignificand(rhs);
00589 }
00590 
00591 void
00592 APFloat::copySignificand(const APFloat &rhs)
00593 {
00594   assert(category == fcNormal || category == fcNaN);
00595   assert(rhs.partCount() >= partCount());
00596 
00597   APInt::tcAssign(significandParts(), rhs.significandParts(),
00598                   partCount());
00599 }
00600 
00601 /* Make this number a NaN, with an arbitrary but deterministic value
00602    for the significand.  */
00603 void
00604 APFloat::makeNaN(void)
00605 {
00606   category = fcNaN;
00607   APInt::tcSet(significandParts(), ~0U, partCount());
00608 }
00609 
00610 APFloat &
00611 APFloat::operator=(const APFloat &rhs)
00612 {
00613   if(this != &rhs) {
00614     if(semantics != rhs.semantics) {
00615       freeSignificand();
00616       initialize(rhs.semantics);
00617     }
00618     assign(rhs);
00619   }
00620 
00621   return *this;
00622 }
00623 
00624 bool
00625 APFloat::bitwiseIsEqual(const APFloat &rhs) const {
00626   if (this == &rhs)
00627     return true;
00628   if (semantics != rhs.semantics ||
00629       category != rhs.category ||
00630       sign != rhs.sign)
00631     return false;
00632   if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
00633       sign2 != rhs.sign2)
00634     return false;
00635   if (category==fcZero || category==fcInfinity)
00636     return true;
00637   else if (category==fcNormal && exponent!=rhs.exponent)
00638     return false;
00639   else if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
00640            exponent2!=rhs.exponent2)
00641     return false;
00642   else {
00643     int i= partCount();
00644     const integerPart* p=significandParts();
00645     const integerPart* q=rhs.significandParts();
00646     for (; i>0; i--, p++, q++) {
00647       if (*p != *q)
00648         return false;
00649     }
00650     return true;
00651   }
00652 }
00653 
00654 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
00655 {
00656   assertArithmeticOK(ourSemantics);
00657   initialize(&ourSemantics);
00658   sign = 0;
00659   zeroSignificand();
00660   exponent = ourSemantics.precision - 1;
00661   significandParts()[0] = value;
00662   normalize(rmNearestTiesToEven, lfExactlyZero);
00663 }
00664 
00665 APFloat::APFloat(const fltSemantics &ourSemantics,
00666                  fltCategory ourCategory, bool negative)
00667 {
00668   assertArithmeticOK(ourSemantics);
00669   initialize(&ourSemantics);
00670   category = ourCategory;
00671   sign = negative;
00672   if(category == fcNormal)
00673     category = fcZero;
00674   else if (ourCategory == fcNaN)
00675     makeNaN();
00676 }
00677 
00678 APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
00679 {
00680   assertArithmeticOK(ourSemantics);
00681   initialize(&ourSemantics);
00682   convertFromString(text, rmNearestTiesToEven);
00683 }
00684 
00685 APFloat::APFloat(const APFloat &rhs)
00686 {
00687   initialize(rhs.semantics);
00688   assign(rhs);
00689 }
00690 
00691 APFloat::~APFloat()
00692 {
00693   freeSignificand();
00694 }
00695 
00696 // Profile - This method 'profiles' an APFloat for use with FoldingSet.
00697 void APFloat::Profile(FoldingSetNodeID& ID) const {
00698   ID.Add(bitcastToAPInt());
00699 }
00700 
00701 unsigned int
00702 APFloat::partCount() const
00703 {
00704   return partCountForBits(semantics->precision + 1);
00705 }
00706 
00707 unsigned int
00708 APFloat::semanticsPrecision(const fltSemantics &semantics)
00709 {
00710   return semantics.precision;
00711 }
00712 
00713 const integerPart *
00714 APFloat::significandParts() const
00715 {
00716   return const_cast<APFloat *>(this)->significandParts();
00717 }
00718 
00719 integerPart *
00720 APFloat::significandParts()
00721 {
00722   assert(category == fcNormal || category == fcNaN);
00723 
00724   if(partCount() > 1)
00725     return significand.parts;
00726   else
00727     return &significand.part;
00728 }
00729 
00730 void
00731 APFloat::zeroSignificand()
00732 {
00733   category = fcNormal;
00734   APInt::tcSet(significandParts(), 0, partCount());
00735 }
00736 
00737 /* Increment an fcNormal floating point number's significand.  */
00738 void
00739 APFloat::incrementSignificand()
00740 {
00741   integerPart carry;
00742 
00743   carry = APInt::tcIncrement(significandParts(), partCount());
00744 
00745   /* Our callers should never cause us to overflow.  */
00746   assert(carry == 0);
00747 }
00748 
00749 /* Add the significand of the RHS.  Returns the carry flag.  */
00750 integerPart
00751 APFloat::addSignificand(const APFloat &rhs)
00752 {
00753   integerPart *parts;
00754 
00755   parts = significandParts();
00756 
00757   assert(semantics == rhs.semantics);
00758   assert(exponent == rhs.exponent);
00759 
00760   return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
00761 }
00762 
00763 /* Subtract the significand of the RHS with a borrow flag.  Returns
00764    the borrow flag.  */
00765 integerPart
00766 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
00767 {
00768   integerPart *parts;
00769 
00770   parts = significandParts();
00771 
00772   assert(semantics == rhs.semantics);
00773   assert(exponent == rhs.exponent);
00774 
00775   return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
00776                            partCount());
00777 }
00778 
00779 /* Multiply the significand of the RHS.  If ADDEND is non-NULL, add it
00780    on to the full-precision result of the multiplication.  Returns the
00781    lost fraction.  */
00782 lostFraction
00783 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
00784 {
00785   unsigned int omsb;        // One, not zero, based MSB.
00786   unsigned int partsCount, newPartsCount, precision;
00787   integerPart *lhsSignificand;
00788   integerPart scratch[4];
00789   integerPart *fullSignificand;
00790   lostFraction lost_fraction;
00791   bool ignored;
00792 
00793   assert(semantics == rhs.semantics);
00794 
00795   precision = semantics->precision;
00796   newPartsCount = partCountForBits(precision * 2);
00797 
00798   if(newPartsCount > 4)
00799     fullSignificand = new integerPart[newPartsCount];
00800   else
00801     fullSignificand = scratch;
00802 
00803   lhsSignificand = significandParts();
00804   partsCount = partCount();
00805 
00806   APInt::tcFullMultiply(fullSignificand, lhsSignificand,
00807                         rhs.significandParts(), partsCount, partsCount);
00808 
00809   lost_fraction = lfExactlyZero;
00810   omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
00811   exponent += rhs.exponent;
00812 
00813   if(addend) {
00814     Significand savedSignificand = significand;
00815     const fltSemantics *savedSemantics = semantics;
00816     fltSemantics extendedSemantics;
00817     opStatus status;
00818     unsigned int extendedPrecision;
00819 
00820     /* Normalize our MSB.  */
00821     extendedPrecision = precision + precision - 1;
00822     if(omsb != extendedPrecision)
00823       {
00824         APInt::tcShiftLeft(fullSignificand, newPartsCount,
00825                            extendedPrecision - omsb);
00826         exponent -= extendedPrecision - omsb;
00827       }
00828 
00829     /* Create new semantics.  */
00830     extendedSemantics = *semantics;
00831     extendedSemantics.precision = extendedPrecision;
00832 
00833     if(newPartsCount == 1)
00834       significand.part = fullSignificand[0];
00835     else
00836       significand.parts = fullSignificand;
00837     semantics = &extendedSemantics;
00838 
00839     APFloat extendedAddend(*addend);
00840     status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
00841     assert(status == opOK);
00842     lost_fraction = addOrSubtractSignificand(extendedAddend, false);
00843 
00844     /* Restore our state.  */
00845     if(newPartsCount == 1)
00846       fullSignificand[0] = significand.part;
00847     significand = savedSignificand;
00848     semantics = savedSemantics;
00849 
00850     omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
00851   }
00852 
00853   exponent -= (precision - 1);
00854 
00855   if(omsb > precision) {
00856     unsigned int bits, significantParts;
00857     lostFraction lf;
00858 
00859     bits = omsb - precision;
00860     significantParts = partCountForBits(omsb);
00861     lf = shiftRight(fullSignificand, significantParts, bits);
00862     lost_fraction = combineLostFractions(lf, lost_fraction);
00863     exponent += bits;
00864   }
00865 
00866   APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
00867 
00868   if(newPartsCount > 4)
00869     delete [] fullSignificand;
00870 
00871   return lost_fraction;
00872 }
00873 
00874 /* Multiply the significands of LHS and RHS to DST.  */
00875 lostFraction
00876 APFloat::divideSignificand(const APFloat &rhs)
00877 {
00878   unsigned int bit, i, partsCount;
00879   const integerPart *rhsSignificand;
00880   integerPart *lhsSignificand, *dividend, *divisor;
00881   integerPart scratch[4];
00882   lostFraction lost_fraction;
00883 
00884   assert(semantics == rhs.semantics);
00885 
00886   lhsSignificand = significandParts();
00887   rhsSignificand = rhs.significandParts();
00888   partsCount = partCount();
00889 
00890   if(partsCount > 2)
00891     dividend = new integerPart[partsCount * 2];
00892   else
00893     dividend = scratch;
00894 
00895   divisor = dividend + partsCount;
00896 
00897   /* Copy the dividend and divisor as they will be modified in-place.  */
00898   for(i = 0; i < partsCount; i++) {
00899     dividend[i] = lhsSignificand[i];
00900     divisor[i] = rhsSignificand[i];
00901     lhsSignificand[i] = 0;
00902   }
00903 
00904   exponent -= rhs.exponent;
00905 
00906   unsigned int precision = semantics->precision;
00907 
00908   /* Normalize the divisor.  */
00909   bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
00910   if(bit) {
00911     exponent += bit;
00912     APInt::tcShiftLeft(divisor, partsCount, bit);
00913   }
00914 
00915   /* Normalize the dividend.  */
00916   bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
00917   if(bit) {
00918     exponent -= bit;
00919     APInt::tcShiftLeft(dividend, partsCount, bit);
00920   }
00921 
00922   /* Ensure the dividend >= divisor initially for the loop below.
00923      Incidentally, this means that the division loop below is
00924      guaranteed to set the integer bit to one.  */
00925   if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
00926     exponent--;
00927     APInt::tcShiftLeft(dividend, partsCount, 1);
00928     assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
00929   }
00930 
00931   /* Long division.  */
00932   for(bit = precision; bit; bit -= 1) {
00933     if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
00934       APInt::tcSubtract(dividend, divisor, 0, partsCount);
00935       APInt::tcSetBit(lhsSignificand, bit - 1);
00936     }
00937 
00938     APInt::tcShiftLeft(dividend, partsCount, 1);
00939   }
00940 
00941   /* Figure out the lost fraction.  */
00942   int cmp = APInt::tcCompare(dividend, divisor, partsCount);
00943 
00944   if(cmp > 0)
00945     lost_fraction = lfMoreThanHalf;
00946   else if(cmp == 0)
00947     lost_fraction = lfExactlyHalf;
00948   else if(APInt::tcIsZero(dividend, partsCount))
00949     lost_fraction = lfExactlyZero;
00950   else
00951     lost_fraction = lfLessThanHalf;
00952 
00953   if(partsCount > 2)
00954     delete [] dividend;
00955 
00956   return lost_fraction;
00957 }
00958 
00959 unsigned int
00960 APFloat::significandMSB() const
00961 {
00962   return APInt::tcMSB(significandParts(), partCount());
00963 }
00964 
00965 unsigned int
00966 APFloat::significandLSB() const
00967 {
00968   return APInt::tcLSB(significandParts(), partCount());
00969 }
00970 
00971 /* Note that a zero result is NOT normalized to fcZero.  */
00972 lostFraction
00973 APFloat::shiftSignificandRight(unsigned int bits)
00974 {
00975   /* Our exponent should not overflow.  */
00976   assert((exponent_t) (exponent + bits) >= exponent);
00977 
00978   exponent += bits;
00979 
00980   return shiftRight(significandParts(), partCount(), bits);
00981 }
00982 
00983 /* Shift the significand left BITS bits, subtract BITS from its exponent.  */
00984 void
00985 APFloat::shiftSignificandLeft(unsigned int bits)
00986 {
00987   assert(bits < semantics->precision);
00988 
00989   if(bits) {
00990     unsigned int partsCount = partCount();
00991 
00992     APInt::tcShiftLeft(significandParts(), partsCount, bits);
00993     exponent -= bits;
00994 
00995     assert(!APInt::tcIsZero(significandParts(), partsCount));
00996   }
00997 }
00998 
00999 APFloat::cmpResult
01000 APFloat::compareAbsoluteValue(const APFloat &rhs) const
01001 {
01002   int compare;
01003 
01004   assert(semantics == rhs.semantics);
01005   assert(category == fcNormal);
01006   assert(rhs.category == fcNormal);
01007 
01008   compare = exponent - rhs.exponent;
01009 
01010   /* If exponents are equal, do an unsigned bignum comparison of the
01011      significands.  */
01012   if(compare == 0)
01013     compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
01014                                partCount());
01015 
01016   if(compare > 0)
01017     return cmpGreaterThan;
01018   else if(compare < 0)
01019     return cmpLessThan;
01020   else
01021     return cmpEqual;
01022 }
01023 
01024 /* Handle overflow.  Sign is preserved.  We either become infinity or
01025    the largest finite number.  */
01026 APFloat::opStatus
01027 APFloat::handleOverflow(roundingMode rounding_mode)
01028 {
01029   /* Infinity?  */
01030   if(rounding_mode == rmNearestTiesToEven
01031      || rounding_mode == rmNearestTiesToAway
01032      || (rounding_mode == rmTowardPositive && !sign)
01033      || (rounding_mode == rmTowardNegative && sign))
01034     {
01035       category = fcInfinity;
01036       return (opStatus) (opOverflow | opInexact);
01037     }
01038 
01039   /* Otherwise we become the largest finite number.  */
01040   category = fcNormal;
01041   exponent = semantics->maxExponent;
01042   APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
01043                                    semantics->precision);
01044 
01045   return opInexact;
01046 }
01047 
01048 /* Returns TRUE if, when truncating the current number, with BIT the
01049    new LSB, with the given lost fraction and rounding mode, the result
01050    would need to be rounded away from zero (i.e., by increasing the
01051    signficand).  This routine must work for fcZero of both signs, and
01052    fcNormal numbers.  */
01053 bool
01054 APFloat::roundAwayFromZero(roundingMode rounding_mode,
01055                            lostFraction lost_fraction,
01056                            unsigned int bit) const
01057 {
01058   /* NaNs and infinities should not have lost fractions.  */
01059   assert(category == fcNormal || category == fcZero);
01060 
01061   /* Current callers never pass this so we don't handle it.  */
01062   assert(lost_fraction != lfExactlyZero);
01063 
01064   switch(rounding_mode) {
01065   default:
01066     assert(0);
01067 
01068   case rmNearestTiesToAway:
01069     return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
01070 
01071   case rmNearestTiesToEven:
01072     if(lost_fraction == lfMoreThanHalf)
01073       return true;
01074 
01075     /* Our zeroes don't have a significand to test.  */
01076     if(lost_fraction == lfExactlyHalf && category != fcZero)
01077       return APInt::tcExtractBit(significandParts(), bit);
01078 
01079     return false;
01080 
01081   case rmTowardZero:
01082     return false;
01083 
01084   case rmTowardPositive:
01085     return sign == false;
01086 
01087   case rmTowardNegative:
01088     return sign == true;
01089   }
01090 }
01091 
01092 APFloat::opStatus
01093 APFloat::normalize(roundingMode rounding_mode,
01094                    lostFraction lost_fraction)
01095 {
01096   unsigned int omsb;                /* One, not zero, based MSB.  */
01097   int exponentChange;
01098 
01099   if(category != fcNormal)
01100     return opOK;
01101 
01102   /* Before rounding normalize the exponent of fcNormal numbers.  */
01103   omsb = significandMSB() + 1;
01104 
01105   if(omsb) {
01106     /* OMSB is numbered from 1.  We want to place it in the integer
01107        bit numbered PRECISON if possible, with a compensating change in
01108        the exponent.  */
01109     exponentChange = omsb - semantics->precision;
01110 
01111     /* If the resulting exponent is too high, overflow according to
01112        the rounding mode.  */
01113     if(exponent + exponentChange > semantics->maxExponent)
01114       return handleOverflow(rounding_mode);
01115 
01116     /* Subnormal numbers have exponent minExponent, and their MSB
01117        is forced based on that.  */
01118     if(exponent + exponentChange < semantics->minExponent)
01119       exponentChange = semantics->minExponent - exponent;
01120 
01121     /* Shifting left is easy as we don't lose precision.  */
01122     if(exponentChange < 0) {
01123       assert(lost_fraction == lfExactlyZero);
01124 
01125       shiftSignificandLeft(-exponentChange);
01126 
01127       return opOK;
01128     }
01129 
01130     if(exponentChange > 0) {
01131       lostFraction lf;
01132 
01133       /* Shift right and capture any new lost fraction.  */
01134       lf = shiftSignificandRight(exponentChange);
01135 
01136       lost_fraction = combineLostFractions(lf, lost_fraction);
01137 
01138       /* Keep OMSB up-to-date.  */
01139       if(omsb > (unsigned) exponentChange)
01140         omsb -= exponentChange;
01141       else
01142         omsb = 0;
01143     }
01144   }
01145 
01146   /* Now round the number according to rounding_mode given the lost
01147      fraction.  */
01148 
01149   /* As specified in IEEE 754, since we do not trap we do not report
01150      underflow for exact results.  */
01151   if(lost_fraction == lfExactlyZero) {
01152     /* Canonicalize zeroes.  */
01153     if(omsb == 0)
01154       category = fcZero;
01155 
01156     return opOK;
01157   }
01158 
01159   /* Increment the significand if we're rounding away from zero.  */
01160   if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
01161     if(omsb == 0)
01162       exponent = semantics->minExponent;
01163 
01164     incrementSignificand();
01165     omsb = significandMSB() + 1;
01166 
01167     /* Did the significand increment overflow?  */
01168     if(omsb == (unsigned) semantics->precision + 1) {
01169       /* Renormalize by incrementing the exponent and shifting our
01170          significand right one.  However if we already have the
01171          maximum exponent we overflow to infinity.  */
01172       if(exponent == semantics->maxExponent) {
01173         category = fcInfinity;
01174 
01175         return (opStatus) (opOverflow | opInexact);
01176       }
01177 
01178       shiftSignificandRight(1);
01179 
01180       return opInexact;
01181     }
01182   }
01183 
01184   /* The normal case - we were and are not denormal, and any
01185      significand increment above didn't overflow.  */
01186   if(omsb == semantics->precision)
01187     return opInexact;
01188 
01189   /* We have a non-zero denormal.  */
01190   assert(omsb < semantics->precision);
01191 
01192   /* Canonicalize zeroes.  */
01193   if(omsb == 0)
01194     category = fcZero;
01195 
01196   /* The fcZero case is a denormal that underflowed to zero.  */
01197   return (opStatus) (opUnderflow | opInexact);
01198 }
01199 
01200 APFloat::opStatus
01201 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
01202 {
01203   switch(convolve(category, rhs.category)) {
01204   default:
01205     assert(0);
01206 
01207   case convolve(fcNaN, fcZero):
01208   case convolve(fcNaN, fcNormal):
01209   case convolve(fcNaN, fcInfinity):
01210   case convolve(fcNaN, fcNaN):
01211   case convolve(fcNormal, fcZero):
01212   case convolve(fcInfinity, fcNormal):
01213   case convolve(fcInfinity, fcZero):
01214     return opOK;
01215 
01216   case convolve(fcZero, fcNaN):
01217   case convolve(fcNormal, fcNaN):
01218   case convolve(fcInfinity,