LCOV - code coverage report
Current view: top level - lib/Support - APFloat.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1746 1966 88.8 %
Date: 2017-09-14 15:23:50 Functions: 164 178 92.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13