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