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(x, parts * integerPartWidth, true,
1720                           rmNearestTiesToEven, &ignored);
1721   if (fs==opInvalidOp) {
1722     delete[] x;
1723     return fs;
1724   }
1725 
1726   fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1727                                         rmNearestTiesToEven);
1728   assert(fs==opOK);   // should always work
1729 
1730   fs = V.multiply(rhs, rmNearestTiesToEven);
1731   assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
1732 
1733   fs = subtract(V, rmNearestTiesToEven);
1734   assert(fs==opOK || fs==opInexact);   // likewise
1735 
1736   if (isZero())
1737     sign = origSign;    // IEEE754 requires this
1738   delete[] x;
1739   return fs;
1740 }
1741 
1742 /* Normalized llvm frem (C fmod).
1743    This is not currently correct in all cases.  */
1744 IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) {
1745   opStatus fs;
1746   fs = modSpecials(rhs);
1747 
1748   if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
1749     IEEEFloat V = *this;
1750     unsigned int origSign = sign;
1751 
1752     fs = V.divide(rhs, rmNearestTiesToEven);
1753     if (fs == opDivByZero)
1754       return fs;
1755 
1756     int parts = partCount();
1757     integerPart *x = new integerPart[parts];
1758     bool ignored;
1759     fs = V.convertToInteger(x, parts * integerPartWidth, true,
1760                             rmTowardZero, &ignored);
1761     if (fs==opInvalidOp) {
1762       delete[] x;
1763       return fs;
1764     }
1765 
1766     fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1767                                           rmNearestTiesToEven);
1768     assert(fs==opOK);   // should always work
1769 
1770     fs = V.multiply(rhs, rmNearestTiesToEven);
1771     assert(fs==opOK || fs==opInexact);   // should not overflow or underflow
1772 
1773     fs = subtract(V, rmNearestTiesToEven);
1774     assert(fs==opOK || fs==opInexact);   // likewise
1775 
1776     if (isZero())
1777       sign = origSign;    // IEEE754 requires this
1778     delete[] x;
1779   }
1780   return fs;
1781 }
1782 
1783 /* Normalized fused-multiply-add.  */
1784 IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand,
1785                                                 const IEEEFloat &addend,
1786                                                 roundingMode rounding_mode) {
1787   opStatus fs;
1788 
1789   /* Post-multiplication sign, before addition.  */
1790   sign ^= multiplicand.sign;
1791 
1792   /* If and only if all arguments are normal do we need to do an
1793      extended-precision calculation.  */
1794   if (isFiniteNonZero() &&
1795       multiplicand.isFiniteNonZero() &&
1796       addend.isFinite()) {
1797     lostFraction lost_fraction;
1798 
1799     lost_fraction = multiplySignificand(multiplicand, &addend);
1800     fs = normalize(rounding_mode, lost_fraction);
1801     if (lost_fraction != lfExactlyZero)
1802       fs = (opStatus) (fs | opInexact);
1803 
1804     /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1805        positive zero unless rounding to minus infinity, except that
1806        adding two like-signed zeroes gives that zero.  */
1807     if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign)
1808       sign = (rounding_mode == rmTowardNegative);
1809   } else {
1810     fs = multiplySpecials(multiplicand);
1811 
1812     /* FS can only be opOK or opInvalidOp.  There is no more work
1813        to do in the latter case.  The IEEE-754R standard says it is
1814        implementation-defined in this case whether, if ADDEND is a
1815        quiet NaN, we raise invalid op; this implementation does so.
1816 
1817        If we need to do the addition we can do so with normal
1818        precision.  */
1819     if (fs == opOK)
1820       fs = addOrSubtract(addend, rounding_mode, false);
1821   }
1822 
1823   return fs;
1824 }
1825 
1826 /* Rounding-mode corrrect round to integral value.  */
1827 IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) {
1828   opStatus fs;
1829 
1830   // If the exponent is large enough, we know that this value is already
1831   // integral, and the arithmetic below would potentially cause it to saturate
1832   // to +/-Inf.  Bail out early instead.
1833   if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
1834     return opOK;
1835 
1836   // The algorithm here is quite simple: we add 2^(p-1), where p is the
1837   // precision of our format, and then subtract it back off again.  The choice
1838   // of rounding modes for the addition/subtraction determines the rounding mode
1839   // for our integral rounding as well.
1840   // NOTE: When the input value is negative, we do subtraction followed by
1841   // addition instead.
1842   APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1843   IntegerConstant <<= semanticsPrecision(*semantics)-1;
1844   IEEEFloat MagicConstant(*semantics);
1845   fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1846                                       rmNearestTiesToEven);
1847   MagicConstant.sign = sign;
1848 
1849   if (fs != opOK)
1850     return fs;
1851 
1852   // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1853   bool inputSign = isNegative();
1854 
1855   fs = add(MagicConstant, rounding_mode);
1856   if (fs != opOK && fs != opInexact)
1857     return fs;
1858 
1859   fs = subtract(MagicConstant, rounding_mode);
1860 
1861   // Restore the input sign.
1862   if (inputSign != isNegative())
1863     changeSign();
1864 
1865   return fs;
1866 }
1867 
1868 
1869 /* Comparison requires normalized numbers.  */
1870 IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const {
1871   cmpResult result;
1872 
1873   assert(semantics == rhs.semantics);
1874 
1875   switch (PackCategoriesIntoKey(category, rhs.category)) {
1876   default:
1877     llvm_unreachable(nullptr);
1878 
1879   case PackCategoriesIntoKey(fcNaN, fcZero):
1880   case PackCategoriesIntoKey(fcNaN, fcNormal):
1881   case PackCategoriesIntoKey(fcNaN, fcInfinity):
1882   case PackCategoriesIntoKey(fcNaN, fcNaN):
1883   case PackCategoriesIntoKey(fcZero, fcNaN):
1884   case PackCategoriesIntoKey(fcNormal, fcNaN):
1885   case PackCategoriesIntoKey(fcInfinity, fcNaN):
1886     return cmpUnordered;
1887 
1888   case PackCategoriesIntoKey(fcInfinity, fcNormal):
1889   case PackCategoriesIntoKey(fcInfinity, fcZero):
1890   case PackCategoriesIntoKey(fcNormal, fcZero):
1891     if (sign)
1892       return cmpLessThan;
1893     else
1894       return cmpGreaterThan;
1895 
1896   case PackCategoriesIntoKey(fcNormal, fcInfinity):
1897   case PackCategoriesIntoKey(fcZero, fcInfinity):
1898   case PackCategoriesIntoKey(fcZero, fcNormal):
1899     if (rhs.sign)
1900       return cmpGreaterThan;
1901     else
1902       return cmpLessThan;
1903 
1904   case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1905     if (sign == rhs.sign)
1906       return cmpEqual;
1907     else if (sign)
1908       return cmpLessThan;
1909     else
1910       return cmpGreaterThan;
1911 
1912   case PackCategoriesIntoKey(fcZero, fcZero):
1913     return cmpEqual;
1914 
1915   case PackCategoriesIntoKey(fcNormal, fcNormal):
1916     break;
1917   }
1918 
1919   /* Two normal numbers.  Do they have the same sign?  */
1920   if (sign != rhs.sign) {
1921     if (sign)
1922       result = cmpLessThan;
1923     else
1924       result = cmpGreaterThan;
1925   } else {
1926     /* Compare absolute values; invert result if negative.  */
1927     result = compareAbsoluteValue(rhs);
1928 
1929     if (sign) {
1930       if (result == cmpLessThan)
1931         result = cmpGreaterThan;
1932       else if (result == cmpGreaterThan)
1933         result = cmpLessThan;
1934     }
1935   }
1936 
1937   return result;
1938 }
1939 
1940 /// IEEEFloat::convert - convert a value of one floating point type to another.
1941 /// The return value corresponds to the IEEE754 exceptions.  *losesInfo
1942 /// records whether the transformation lost information, i.e. whether
1943 /// converting the result back to the original type will produce the
1944 /// original value (this is almost the same as return value==fsOK, but there
1945 /// are edge cases where this is not so).
1946 
1947 IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
1948                                        roundingMode rounding_mode,
1949                                        bool *losesInfo) {
1950   lostFraction lostFraction;
1951   unsigned int newPartCount, oldPartCount;
1952   opStatus fs;
1953   int shift;
1954   const fltSemantics &fromSemantics = *semantics;
1955 
1956   lostFraction = lfExactlyZero;
1957   newPartCount = partCountForBits(toSemantics.precision + 1);
1958   oldPartCount = partCount();
1959   shift = toSemantics.precision - fromSemantics.precision;
1960 
1961   bool X86SpecialNan = false;
1962   if (&fromSemantics == &semX87DoubleExtended &&
1963       &toSemantics != &semX87DoubleExtended && category == fcNaN &&
1964       (!(*significandParts() & 0x8000000000000000ULL) ||
1965        !(*significandParts() & 0x4000000000000000ULL))) {
1966     // x86 has some unusual NaNs which cannot be represented in any other
1967     // format; note them here.
1968     X86SpecialNan = true;
1969   }
1970 
1971   // If this is a truncation of a denormal number, and the target semantics
1972   // has larger exponent range than the source semantics (this can happen
1973   // when truncating from PowerPC double-double to double format), the
1974   // right shift could lose result mantissa bits.  Adjust exponent instead
1975   // of performing excessive shift.
1976   if (shift < 0 && isFiniteNonZero()) {
1977     int exponentChange = significandMSB() + 1 - fromSemantics.precision;
1978     if (exponent + exponentChange < toSemantics.minExponent)
1979       exponentChange = toSemantics.minExponent - exponent;
1980     if (exponentChange < shift)
1981       exponentChange = shift;
1982     if (exponentChange < 0) {
1983       shift -= exponentChange;
1984       exponent += exponentChange;
1985     }
1986   }
1987 
1988   // If this is a truncation, perform the shift before we narrow the storage.
1989   if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
1990     lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
1991 
1992   // Fix the storage so it can hold to new value.
1993   if (newPartCount > oldPartCount) {
1994     // The new type requires more storage; make it available.
1995     integerPart *newParts;
1996     newParts = new integerPart[newPartCount];
1997     APInt::tcSet(newParts, 0, newPartCount);
1998     if (isFiniteNonZero() || category==fcNaN)
1999       APInt::tcAssign(newParts, significandParts(), oldPartCount);
2000     freeSignificand();
2001     significand.parts = newParts;
2002   } else if (newPartCount == 1 && oldPartCount != 1) {
2003     // Switch to built-in storage for a single part.
2004     integerPart newPart = 0;
2005     if (isFiniteNonZero() || category==fcNaN)
2006       newPart = significandParts()[0];
2007     freeSignificand();
2008     significand.part = newPart;
2009   }
2010 
2011   // Now that we have the right storage, switch the semantics.
2012   semantics = &toSemantics;
2013 
2014   // If this is an extension, perform the shift now that the storage is
2015   // available.
2016   if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
2017     APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2018 
2019   if (isFiniteNonZero()) {
2020     fs = normalize(rounding_mode, lostFraction);
2021     *losesInfo = (fs != opOK);
2022   } else if (category == fcNaN) {
2023     *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
2024 
2025     // For x87 extended precision, we want to make a NaN, not a special NaN if
2026     // the input wasn't special either.
2027     if (!X86SpecialNan && semantics == &semX87DoubleExtended)
2028       APInt::tcSetBit(significandParts(), semantics->precision - 1);
2029 
2030     // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2031     // does not give you back the same bits.  This is dubious, and we
2032     // don't currently do it.  You're really supposed to get
2033     // an invalid operation signal at runtime, but nobody does that.
2034     fs = opOK;
2035   } else {
2036     *losesInfo = false;
2037     fs = opOK;
2038   }
2039 
2040   return fs;
2041 }
2042 
2043 /* Convert a floating point number to an integer according to the
2044    rounding mode.  If the rounded integer value is out of range this
2045    returns an invalid operation exception and the contents of the
2046    destination parts are unspecified.  If the rounded value is in
2047    range but the floating point number is not the exact integer, the C
2048    standard doesn't require an inexact exception to be raised.  IEEE
2049    854 does require it so we do that.
2050 
2051    Note that for conversions to integer type the C standard requires
2052    round-to-zero to always be used.  */
2053 IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger(
2054     integerPart *parts, unsigned int width, bool isSigned,
2055     roundingMode rounding_mode, bool *isExact) const {
2056   lostFraction lost_fraction;
2057   const integerPart *src;
2058   unsigned int dstPartsCount, truncatedBits;
2059 
2060   *isExact = false;
2061 
2062   /* Handle the three special cases first.  */
2063   if (category == fcInfinity || category == fcNaN)
2064     return opInvalidOp;
2065 
2066   dstPartsCount = partCountForBits(width);
2067 
2068   if (category == fcZero) {
2069     APInt::tcSet(parts, 0, dstPartsCount);
2070     // Negative zero can't be represented as an int.
2071     *isExact = !sign;
2072     return opOK;
2073   }
2074 
2075   src = significandParts();
2076 
2077   /* Step 1: place our absolute value, with any fraction truncated, in
2078      the destination.  */
2079   if (exponent < 0) {
2080     /* Our absolute value is less than one; truncate everything.  */
2081     APInt::tcSet(parts, 0, dstPartsCount);
2082     /* For exponent -1 the integer bit represents .5, look at that.
2083        For smaller exponents leftmost truncated bit is 0. */
2084     truncatedBits = semantics->precision -1U - exponent;
2085   } else {
2086     /* We want the most significant (exponent + 1) bits; the rest are
2087        truncated.  */
2088     unsigned int bits = exponent + 1U;
2089 
2090     /* Hopelessly large in magnitude?  */
2091     if (bits > width)
2092       return opInvalidOp;
2093 
2094     if (bits < semantics->precision) {
2095       /* We truncate (semantics->precision - bits) bits.  */
2096       truncatedBits = semantics->precision - bits;
2097       APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2098     } else {
2099       /* We want at least as many bits as are available.  */
2100       APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2101       APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2102       truncatedBits = 0;
2103     }
2104   }
2105 
2106   /* Step 2: work out any lost fraction, and increment the absolute
2107      value if we would round away from zero.  */
2108   if (truncatedBits) {
2109     lost_fraction = lostFractionThroughTruncation(src, partCount(),
2110                                                   truncatedBits);
2111     if (lost_fraction != lfExactlyZero &&
2112         roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2113       if (APInt::tcIncrement(parts, dstPartsCount))
2114         return opInvalidOp;     /* Overflow.  */
2115     }
2116   } else {
2117     lost_fraction = lfExactlyZero;
2118   }
2119 
2120   /* Step 3: check if we fit in the destination.  */
2121   unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2122 
2123   if (sign) {
2124     if (!isSigned) {
2125       /* Negative numbers cannot be represented as unsigned.  */
2126       if (omsb != 0)
2127         return opInvalidOp;
2128     } else {
2129       /* It takes omsb bits to represent the unsigned integer value.
2130          We lose a bit for the sign, but care is needed as the
2131          maximally negative integer is a special case.  */
2132       if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2133         return opInvalidOp;
2134 
2135       /* This case can happen because of rounding.  */
2136       if (omsb > width)
2137         return opInvalidOp;
2138     }
2139 
2140     APInt::tcNegate (parts, dstPartsCount);
2141   } else {
2142     if (omsb >= width + !isSigned)
2143       return opInvalidOp;
2144   }
2145 
2146   if (lost_fraction == lfExactlyZero) {
2147     *isExact = true;
2148     return opOK;
2149   } else
2150     return opInexact;
2151 }
2152 
2153 /* Same as convertToSignExtendedInteger, except we provide
2154    deterministic values in case of an invalid operation exception,
2155    namely zero for NaNs and the minimal or maximal value respectively
2156    for underflow or overflow.
2157    The *isExact output tells whether the result is exact, in the sense
2158    that converting it back to the original floating point type produces
2159    the original value.  This is almost equivalent to result==opOK,
2160    except for negative zeroes.
2161 */
2162 IEEEFloat::opStatus IEEEFloat::convertToInteger(integerPart *parts,
2163                                                 unsigned int width,
2164                                                 bool isSigned,
2165                                                 roundingMode rounding_mode,
2166                                                 bool *isExact) const {
2167   opStatus fs;
2168 
2169   fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2170                                     isExact);
2171 
2172   if (fs == opInvalidOp) {
2173     unsigned int bits, dstPartsCount;
2174 
2175     dstPartsCount = partCountForBits(width);
2176 
2177     if (category == fcNaN)
2178       bits = 0;
2179     else if (sign)
2180       bits = isSigned;
2181     else
2182       bits = width - isSigned;
2183 
2184     APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2185     if (sign && isSigned)
2186       APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
2187   }
2188 
2189   return fs;
2190 }
2191 
2192 /* Convert an unsigned integer SRC to a floating point number,
2193    rounding according to ROUNDING_MODE.  The sign of the floating
2194    point number is not modified.  */
2195 IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts(
2196     const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) {
2197   unsigned int omsb, precision, dstCount;
2198   integerPart *dst;
2199   lostFraction lost_fraction;
2200 
2201   category = fcNormal;
2202   omsb = APInt::tcMSB(src, srcCount) + 1;
2203   dst = significandParts();
2204   dstCount = partCount();
2205   precision = semantics->precision;
2206 
2207   /* We want the most significant PRECISION bits of SRC.  There may not
2208      be that many; extract what we can.  */
2209   if (precision <= omsb) {
2210     exponent = omsb - 1;
2211     lost_fraction = lostFractionThroughTruncation(src, srcCount,
2212                                                   omsb - precision);
2213     APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2214   } else {
2215     exponent = precision - 1;
2216     lost_fraction = lfExactlyZero;
2217     APInt::tcExtract(dst, dstCount, src, omsb, 0);
2218   }
2219 
2220   return normalize(rounding_mode, lost_fraction);
2221 }
2222 
2223 IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned,
2224                                                 roundingMode rounding_mode) {
2225   unsigned int partCount = Val.getNumWords();
2226   APInt api = Val;
2227 
2228   sign = false;
2229   if (isSigned && api.isNegative()) {
2230     sign = true;
2231     api = -api;
2232   }
2233 
2234   return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2235 }
2236 
2237 /* Convert a two's complement integer SRC to a floating point number,
2238    rounding according to ROUNDING_MODE.  ISSIGNED is true if the
2239    integer is signed, in which case it must be sign-extended.  */
2240 IEEEFloat::opStatus
2241 IEEEFloat::convertFromSignExtendedInteger(const integerPart *src,
2242                                           unsigned int srcCount, bool isSigned,
2243                                           roundingMode rounding_mode) {
2244   opStatus status;
2245 
2246   if (isSigned &&
2247       APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2248     integerPart *copy;
2249 
2250     /* If we're signed and negative negate a copy.  */
2251     sign = true;
2252     copy = new integerPart[srcCount];
2253     APInt::tcAssign(copy, src, srcCount);
2254     APInt::tcNegate(copy, srcCount);
2255     status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2256     delete [] copy;
2257   } else {
2258     sign = false;
2259     status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2260   }
2261 
2262   return status;
2263 }
2264 
2265 /* FIXME: should this just take a const APInt reference?  */
2266 IEEEFloat::opStatus
2267 IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2268                                           unsigned int width, bool isSigned,
2269                                           roundingMode rounding_mode) {
2270   unsigned int partCount = partCountForBits(width);
2271   APInt api = APInt(width, makeArrayRef(parts, partCount));
2272 
2273   sign = false;
2274   if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
2275     sign = true;
2276     api = -api;
2277   }
2278 
2279   return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2280 }
2281 
2282 IEEEFloat::opStatus
2283 IEEEFloat::convertFromHexadecimalString(StringRef s,
2284                                         roundingMode rounding_mode) {
2285   lostFraction lost_fraction = lfExactlyZero;
2286 
2287   category = fcNormal;
2288   zeroSignificand();
2289   exponent = 0;
2290 
2291   integerPart *significand = significandParts();
2292   unsigned partsCount = partCount();
2293   unsigned bitPos = partsCount * integerPartWidth;
2294   bool computedTrailingFraction = false;
2295 
2296   // Skip leading zeroes and any (hexa)decimal point.
2297   StringRef::iterator begin = s.begin();
2298   StringRef::iterator end = s.end();
2299   StringRef::iterator dot;
2300   StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
2301   StringRef::iterator firstSignificantDigit = p;
2302 
2303   while (p != end) {
2304     integerPart hex_value;
2305 
2306     if (*p == '.') {
2307       assert(dot == end && "String contains multiple dots");
2308       dot = p++;
2309       continue;
2310     }
2311 
2312     hex_value = hexDigitValue(*p);
2313     if (hex_value == -1U)
2314       break;
2315 
2316     p++;
2317 
2318     // Store the number while we have space.
2319     if (bitPos) {
2320       bitPos -= 4;
2321       hex_value <<= bitPos % integerPartWidth;
2322       significand[bitPos / integerPartWidth] |= hex_value;
2323     } else if (!computedTrailingFraction) {
2324       lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2325       computedTrailingFraction = true;
2326     }
2327   }
2328 
2329   /* Hex floats require an exponent but not a hexadecimal point.  */
2330   assert(p != end && "Hex strings require an exponent");
2331   assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2332   assert(p != begin && "Significand has no digits");
2333   assert((dot == end || p - begin != 1) && "Significand has no digits");
2334 
2335   /* Ignore the exponent if we are zero.  */
2336   if (p != firstSignificantDigit) {
2337     int expAdjustment;
2338 
2339     /* Implicit hexadecimal point?  */
2340     if (dot == end)
2341       dot = p;
2342 
2343     /* Calculate the exponent adjustment implicit in the number of
2344        significant digits.  */
2345     expAdjustment = static_cast<int>(dot - firstSignificantDigit);
2346     if (expAdjustment < 0)
2347       expAdjustment++;
2348     expAdjustment = expAdjustment * 4 - 1;
2349 
2350     /* Adjust for writing the significand starting at the most
2351        significant nibble.  */
2352     expAdjustment += semantics->precision;
2353     expAdjustment -= partsCount * integerPartWidth;
2354 
2355     /* Adjust for the given exponent.  */
2356     exponent = totalExponent(p + 1, end, expAdjustment);
2357   }
2358 
2359   return normalize(rounding_mode, lost_fraction);
2360 }
2361 
2362 IEEEFloat::opStatus
2363 IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2364                                         unsigned sigPartCount, int exp,
2365                                         roundingMode rounding_mode) {
2366   unsigned int parts, pow5PartCount;
2367   fltSemantics calcSemantics = { 32767, -32767, 0, 0 };
2368   integerPart pow5Parts[maxPowerOfFiveParts];
2369   bool isNearest;
2370 
2371   isNearest = (rounding_mode == rmNearestTiesToEven ||
2372                rounding_mode == rmNearestTiesToAway);
2373 
2374   parts = partCountForBits(semantics->precision + 11);
2375 
2376   /* Calculate pow(5, abs(exp)).  */
2377   pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2378 
2379   for (;; parts *= 2) {
2380     opStatus sigStatus, powStatus;
2381     unsigned int excessPrecision, truncatedBits;
2382 
2383     calcSemantics.precision = parts * integerPartWidth - 1;
2384     excessPrecision = calcSemantics.precision - semantics->precision;
2385     truncatedBits = excessPrecision;
2386 
2387     IEEEFloat decSig(calcSemantics, uninitialized);
2388     decSig.makeZero(sign);
2389     IEEEFloat pow5(calcSemantics);
2390 
2391     sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2392                                                 rmNearestTiesToEven);
2393     powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2394                                               rmNearestTiesToEven);
2395     /* Add exp, as 10^n = 5^n * 2^n.  */
2396     decSig.exponent += exp;
2397 
2398     lostFraction calcLostFraction;
2399     integerPart HUerr, HUdistance;
2400     unsigned int powHUerr;
2401 
2402     if (exp >= 0) {
2403       /* multiplySignificand leaves the precision-th bit set to 1.  */
2404       calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
2405       powHUerr = powStatus != opOK;
2406     } else {
2407       calcLostFraction = decSig.divideSignificand(pow5);
2408       /* Denormal numbers have less precision.  */
2409       if (decSig.exponent < semantics->minExponent) {
2410         excessPrecision += (semantics->minExponent - decSig.exponent);
2411         truncatedBits = excessPrecision;
2412         if (excessPrecision > calcSemantics.precision)
2413           excessPrecision = calcSemantics.precision;
2414       }
2415       /* Extra half-ulp lost in reciprocal of exponent.  */
2416       powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
2417     }
2418 
2419     /* Both multiplySignificand and divideSignificand return the
2420        result with the integer bit set.  */
2421     assert(APInt::tcExtractBit
2422            (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2423 
2424     HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2425                        powHUerr);
2426     HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2427                                       excessPrecision, isNearest);
2428 
2429     /* Are we guaranteed to round correctly if we truncate?  */
2430     if (HUdistance >= HUerr) {
2431       APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2432                        calcSemantics.precision - excessPrecision,
2433                        excessPrecision);
2434       /* Take the exponent of decSig.  If we tcExtract-ed less bits
2435          above we must adjust our exponent to compensate for the
2436          implicit right shift.  */
2437       exponent = (decSig.exponent + semantics->precision
2438                   - (calcSemantics.precision - excessPrecision));
2439       calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2440                                                        decSig.partCount(),
2441                                                        truncatedBits);
2442       return normalize(rounding_mode, calcLostFraction);
2443     }
2444   }
2445 }
2446 
2447 IEEEFloat::opStatus
2448 IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
2449   decimalInfo D;
2450   opStatus fs;
2451 
2452   /* Scan the text.  */
2453   StringRef::iterator p = str.begin();
2454   interpretDecimal(p, str.end(), &D);
2455 
2456   /* Handle the quick cases.  First the case of no significant digits,
2457      i.e. zero, and then exponents that are obviously too large or too
2458      small.  Writing L for log 10 / log 2, a number d.ddddd*10^exp
2459      definitely overflows if
2460 
2461            (exp - 1) * L >= maxExponent
2462 
2463      and definitely underflows to zero where
2464 
2465            (exp + 1) * L <= minExponent - precision
2466 
2467      With integer arithmetic the tightest bounds for L are
2468 
2469            93/28 < L < 196/59            [ numerator <= 256 ]
2470            42039/12655 < L < 28738/8651  [ numerator <= 65536 ]
2471   */
2472 
2473   // Test if we have a zero number allowing for strings with no null terminators
2474   // and zero decimals with non-zero exponents.
2475   //
2476   // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2477   // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2478   // be at most one dot. On the other hand, if we have a zero with a non-zero
2479   // exponent, then we know that D.firstSigDigit will be non-numeric.
2480   if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
2481     category = fcZero;
2482     fs = opOK;
2483 
2484   /* Check whether the normalized exponent is high enough to overflow
2485      max during the log-rebasing in the max-exponent check below. */
2486   } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
2487     fs = handleOverflow(rounding_mode);
2488 
2489   /* If it wasn't, then it also wasn't high enough to overflow max
2490      during the log-rebasing in the min-exponent check.  Check that it
2491      won't overflow min in either check, then perform the min-exponent
2492      check. */
2493   } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
2494              (D.normalizedExponent + 1) * 28738 <=
2495                8651 * (semantics->minExponent - (int) semantics->precision)) {
2496     /* Underflow to zero and round.  */
2497     category = fcNormal;
2498     zeroSignificand();
2499     fs = normalize(rounding_mode, lfLessThanHalf);
2500 
2501   /* We can finally safely perform the max-exponent check. */
2502   } else if ((D.normalizedExponent - 1) * 42039
2503              >= 12655 * semantics->maxExponent) {
2504     /* Overflow and round.  */
2505     fs = handleOverflow(rounding_mode);
2506   } else {
2507     integerPart *decSignificand;
2508     unsigned int partCount;
2509 
2510     /* A tight upper bound on number of bits required to hold an
2511        N-digit decimal integer is N * 196 / 59.  Allocate enough space
2512        to hold the full significand, and an extra part required by
2513        tcMultiplyPart.  */
2514     partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
2515     partCount = partCountForBits(1 + 196 * partCount / 59);
2516     decSignificand = new integerPart[partCount + 1];
2517     partCount = 0;
2518 
2519     /* Convert to binary efficiently - we do almost all multiplication
2520        in an integerPart.  When this would overflow do we do a single
2521        bignum multiplication, and then revert again to multiplication
2522        in an integerPart.  */
2523     do {
2524       integerPart decValue, val, multiplier;
2525 
2526       val = 0;
2527       multiplier = 1;
2528 
2529       do {
2530         if (*p == '.') {
2531           p++;
2532           if (p == str.end()) {
2533             break;
2534           }
2535         }
2536         decValue = decDigitValue(*p++);
2537         assert(decValue < 10U && "Invalid character in significand");
2538         multiplier *= 10;
2539         val = val * 10 + decValue;
2540         /* The maximum number that can be multiplied by ten with any
2541            digit added without overflowing an integerPart.  */
2542       } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2543 
2544       /* Multiply out the current part.  */
2545       APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2546                             partCount, partCount + 1, false);
2547 
2548       /* If we used another part (likely but not guaranteed), increase
2549          the count.  */
2550       if (decSignificand[partCount])
2551         partCount++;
2552     } while (p <= D.lastSigDigit);
2553 
2554     category = fcNormal;
2555     fs = roundSignificandWithExponent(decSignificand, partCount,
2556                                       D.exponent, rounding_mode);
2557 
2558     delete [] decSignificand;
2559   }
2560 
2561   return fs;
2562 }
2563 
2564 bool IEEEFloat::convertFromStringSpecials(StringRef str) {
2565   if (str.equals("inf") || str.equals("INFINITY")) {
2566     makeInf(false);
2567     return true;
2568   }
2569 
2570   if (str.equals("-inf") || str.equals("-INFINITY")) {
2571     makeInf(true);
2572     return true;
2573   }
2574 
2575   if (str.equals("nan") || str.equals("NaN")) {
2576     makeNaN(false, false);
2577     return true;
2578   }
2579 
2580   if (str.equals("-nan") || str.equals("-NaN")) {
2581     makeNaN(false, true);
2582     return true;
2583   }
2584 
2585   return false;
2586 }
2587 
2588 IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str,
2589                                                  roundingMode rounding_mode) {
2590   assert(!str.empty() && "Invalid string length");
2591 
2592   // Handle special cases.
2593   if (convertFromStringSpecials(str))
2594     return opOK;
2595 
2596   /* Handle a leading minus sign.  */
2597   StringRef::iterator p = str.begin();
2598   size_t slen = str.size();
2599   sign = *p == '-' ? 1 : 0;
2600   if (*p == '-' || *p == '+') {
2601     p++;
2602     slen--;
2603     assert(slen && "String has no digits");
2604   }
2605 
2606   if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2607     assert(slen - 2 && "Invalid string");
2608     return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
2609                                         rounding_mode);
2610   }
2611 
2612   return convertFromDecimalString(StringRef(p, slen), rounding_mode);
2613 }
2614 
2615 /* Write out a hexadecimal representation of the floating point value
2616    to DST, which must be of sufficient size, in the C99 form
2617    [-]0xh.hhhhp[+-]d.  Return the number of characters written,
2618    excluding the terminating NUL.
2619 
2620    If UPPERCASE, the output is in upper case, otherwise in lower case.
2621 
2622    HEXDIGITS digits appear altogether, rounding the value if
2623    necessary.  If HEXDIGITS is 0, the minimal precision to display the
2624    number precisely is used instead.  If nothing would appear after
2625    the decimal point it is suppressed.
2626 
2627    The decimal exponent is always printed and has at least one digit.
2628    Zero values display an exponent of zero.  Infinities and NaNs
2629    appear as "infinity" or "nan" respectively.
2630 
2631    The above rules are as specified by C99.  There is ambiguity about
2632    what the leading hexadecimal digit should be.  This implementation
2633    uses whatever is necessary so that the exponent is displayed as
2634    stored.  This implies the exponent will fall within the IEEE format
2635    range, and the leading hexadecimal digit will be 0 (for denormals),
2636    1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2637    any other digits zero).
2638 */
2639 unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits,
2640                                            bool upperCase,
2641                                            roundingMode rounding_mode) const {
2642   char *p;
2643 
2644   p = dst;
2645   if (sign)
2646     *dst++ = '-';
2647 
2648   switch (category) {
2649   case fcInfinity:
2650     memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2651     dst += sizeof infinityL - 1;
2652     break;
2653 
2654   case fcNaN:
2655     memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2656     dst += sizeof NaNU - 1;
2657     break;
2658 
2659   case fcZero:
2660     *dst++ = '0';
2661     *dst++ = upperCase ? 'X': 'x';
2662     *dst++ = '0';
2663     if (hexDigits > 1) {
2664       *dst++ = '.';
2665       memset (dst, '0', hexDigits - 1);
2666       dst += hexDigits - 1;
2667     }
2668     *dst++ = upperCase ? 'P': 'p';
2669     *dst++ = '0';
2670     break;
2671 
2672   case fcNormal:
2673     dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2674     break;
2675   }
2676 
2677   *dst = 0;
2678 
2679   return static_cast<unsigned int>(dst - p);
2680 }
2681 
2682 /* Does the hard work of outputting the correctly rounded hexadecimal
2683    form of a normal floating point number with the specified number of
2684    hexadecimal digits.  If HEXDIGITS is zero the minimum number of
2685    digits necessary to print the value precisely is output.  */
2686 char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2687                                           bool upperCase,
2688                                           roundingMode rounding_mode) const {
2689   unsigned int count, valueBits, shift, partsCount, outputDigits;
2690   const char *hexDigitChars;
2691   const integerPart *significand;
2692   char *p;
2693   bool roundUp;
2694 
2695   *dst++ = '0';
2696   *dst++ = upperCase ? 'X': 'x';
2697 
2698   roundUp = false;
2699   hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2700 
2701   significand = significandParts();
2702   partsCount = partCount();
2703 
2704   /* +3 because the first digit only uses the single integer bit, so
2705      we have 3 virtual zero most-significant-bits.  */
2706   valueBits = semantics->precision + 3;
2707   shift = integerPartWidth - valueBits % integerPartWidth;
2708 
2709   /* The natural number of digits required ignoring trailing
2710      insignificant zeroes.  */
2711   outputDigits = (valueBits - significandLSB () + 3) / 4;
2712 
2713   /* hexDigits of zero means use the required number for the
2714      precision.  Otherwise, see if we are truncating.  If we are,
2715      find out if we need to round away from zero.  */
2716   if (hexDigits) {
2717     if (hexDigits < outputDigits) {
2718       /* We are dropping non-zero bits, so need to check how to round.
2719          "bits" is the number of dropped bits.  */
2720       unsigned int bits;
2721       lostFraction fraction;
2722 
2723       bits = valueBits - hexDigits * 4;
2724       fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2725       roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2726     }
2727     outputDigits = hexDigits;
2728   }
2729 
2730   /* Write the digits consecutively, and start writing in the location
2731      of the hexadecimal point.  We move the most significant digit
2732      left and add the hexadecimal point later.  */
2733   p = ++dst;
2734 
2735   count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2736 
2737   while (outputDigits && count) {
2738     integerPart part;
2739 
2740     /* Put the most significant integerPartWidth bits in "part".  */
2741     if (--count == partsCount)
2742       part = 0;  /* An imaginary higher zero part.  */
2743     else
2744       part = significand[count] << shift;
2745 
2746     if (count && shift)
2747       part |= significand[count - 1] >> (integerPartWidth - shift);
2748 
2749     /* Convert as much of "part" to hexdigits as we can.  */
2750     unsigned int curDigits = integerPartWidth / 4;
2751 
2752     if (curDigits > outputDigits)
2753       curDigits = outputDigits;
2754     dst += partAsHex (dst, part, curDigits, hexDigitChars);
2755     outputDigits -= curDigits;
2756   }
2757 
2758   if (roundUp) {
2759     char *q = dst;
2760 
2761     /* Note that hexDigitChars has a trailing '0'.  */
2762     do {
2763       q--;
2764       *q = hexDigitChars[hexDigitValue (*q) + 1];
2765     } while (*q == '0');
2766     assert(q >= p);
2767   } else {
2768     /* Add trailing zeroes.  */
2769     memset (dst, '0', outputDigits);
2770     dst += outputDigits;
2771   }
2772 
2773   /* Move the most significant digit to before the point, and if there
2774      is something after the decimal point add it.  This must come
2775      after rounding above.  */
2776   p[-1] = p[0];
2777   if (dst -1 == p)
2778     dst--;
2779   else
2780     p[0] = '.';
2781 
2782   /* Finally output the exponent.  */
2783   *dst++ = upperCase ? 'P': 'p';
2784 
2785   return writeSignedDecimal (dst, exponent);
2786 }
2787 
2788 hash_code hash_value(const IEEEFloat &Arg) {
2789   if (!Arg.isFiniteNonZero())
2790     return hash_combine((uint8_t)Arg.category,
2791                         // NaN has no sign, fix it at zero.
2792                         Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2793                         Arg.semantics->precision);
2794 
2795   // Normal floats need their exponent and significand hashed.
2796   return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2797                       Arg.semantics->precision, Arg.exponent,
2798                       hash_combine_range(
2799                         Arg.significandParts(),
2800                         Arg.significandParts() + Arg.partCount()));
2801 }
2802 
2803 // Conversion from APFloat to/from host float/double.  It may eventually be
2804 // possible to eliminate these and have everybody deal with APFloats, but that
2805 // will take a while.  This approach will not easily extend to long double.
2806 // Current implementation requires integerPartWidth==64, which is correct at
2807 // the moment but could be made more general.
2808 
2809 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
2810 // the actual IEEE respresentations.  We compensate for that here.
2811 
2812 APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const {
2813   assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended);
2814   assert(partCount()==2);
2815 
2816   uint64_t myexponent, mysignificand;
2817 
2818   if (isFiniteNonZero()) {
2819     myexponent = exponent+16383; //bias
2820     mysignificand = significandParts()[0];
2821     if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2822       myexponent = 0;   // denormal
2823   } else if (category==fcZero) {
2824     myexponent = 0;
2825     mysignificand = 0;
2826   } else if (category==fcInfinity) {
2827     myexponent = 0x7fff;
2828     mysignificand = 0x8000000000000000ULL;
2829   } else {
2830     assert(category == fcNaN && "Unknown category");
2831     myexponent = 0x7fff;
2832     mysignificand = significandParts()[0];
2833   }
2834 
2835   uint64_t words[2];
2836   words[0] = mysignificand;
2837   words[1] =  ((uint64_t)(sign & 1) << 15) |
2838               (myexponent & 0x7fffLL);
2839   return APInt(80, words);
2840 }
2841 
2842 APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const {
2843   assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy);
2844   assert(partCount()==2);
2845 
2846   uint64_t words[2];
2847   opStatus fs;
2848   bool losesInfo;
2849 
2850   // Convert number to double.  To avoid spurious underflows, we re-
2851   // normalize against the "double" minExponent first, and only *then*
2852   // truncate the mantissa.  The result of that second conversion
2853   // may be inexact, but should never underflow.
2854   // Declare fltSemantics before APFloat that uses it (and
2855   // saves pointer to it) to ensure correct destruction order.
2856   fltSemantics extendedSemantics = *semantics;
2857   extendedSemantics.minExponent = semIEEEdouble.minExponent;
2858   IEEEFloat extended(*this);
2859   fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2860   assert(fs == opOK && !losesInfo);
2861   (void)fs;
2862 
2863   IEEEFloat u(extended);
2864   fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
2865   assert(fs == opOK || fs == opInexact);
2866   (void)fs;
2867   words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2868 
2869   // If conversion was exact or resulted in a special case, we're done;
2870   // just set the second double to zero.  Otherwise, re-convert back to
2871   // the extended format and compute the difference.  This now should
2872   // convert exactly to double.
2873   if (u.isFiniteNonZero() && losesInfo) {
2874     fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2875     assert(fs == opOK && !losesInfo);
2876     (void)fs;
2877 
2878     IEEEFloat v(extended);
2879     v.subtract(u, rmNearestTiesToEven);
2880     fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo);
2881     assert(fs == opOK && !losesInfo);
2882     (void)fs;
2883     words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
2884   } else {
2885     words[1] = 0;
2886   }
2887 
2888   return APInt(128, words);
2889 }
2890 
2891 APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const {
2892   assert(semantics == (const llvm::fltSemantics*)&semIEEEquad);
2893   assert(partCount()==2);
2894 
2895   uint64_t myexponent, mysignificand, mysignificand2;
2896 
2897   if (isFiniteNonZero()) {
2898     myexponent = exponent+16383; //bias
2899     mysignificand = significandParts()[0];
2900     mysignificand2 = significandParts()[1];
2901     if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2902       myexponent = 0;   // denormal
2903   } else if (category==fcZero) {
2904     myexponent = 0;
2905     mysignificand = mysignificand2 = 0;
2906   } else if (category==fcInfinity) {
2907     myexponent = 0x7fff;
2908     mysignificand = mysignificand2 = 0;
2909   } else {
2910     assert(category == fcNaN && "Unknown category!");
2911     myexponent = 0x7fff;
2912     mysignificand = significandParts()[0];
2913     mysignificand2 = significandParts()[1];
2914   }
2915 
2916   uint64_t words[2];
2917   words[0] = mysignificand;
2918   words[1] = ((uint64_t)(sign & 1) << 63) |
2919              ((myexponent & 0x7fff) << 48) |
2920              (mysignificand2 & 0xffffffffffffLL);
2921 
2922   return APInt(128, words);
2923 }
2924 
2925 APInt IEEEFloat::convertDoubleAPFloatToAPInt() const {
2926   assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble);
2927   assert(partCount()==1);
2928 
2929   uint64_t myexponent, mysignificand;
2930 
2931   if (isFiniteNonZero()) {
2932     myexponent = exponent+1023; //bias
2933     mysignificand = *significandParts();
2934     if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2935       myexponent = 0;   // denormal
2936   } else if (category==fcZero) {
2937     myexponent = 0;
2938     mysignificand = 0;
2939   } else if (category==fcInfinity) {
2940     myexponent = 0x7ff;
2941     mysignificand = 0;
2942   } else {
2943     assert(category == fcNaN && "Unknown category!");
2944     myexponent = 0x7ff;
2945     mysignificand = *significandParts();
2946   }
2947 
2948   return APInt(64, ((((uint64_t)(sign & 1) << 63) |
2949                      ((myexponent & 0x7ff) <<  52) |
2950                      (mysignificand & 0xfffffffffffffLL))));
2951 }
2952 
2953 APInt IEEEFloat::convertFloatAPFloatToAPInt() const {
2954   assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle);
2955   assert(partCount()==1);
2956 
2957   uint32_t myexponent, mysignificand;
2958 
2959   if (isFiniteNonZero()) {
2960     myexponent = exponent+127; //bias
2961     mysignificand = (uint32_t)*significandParts();
2962     if (myexponent == 1 && !(mysignificand & 0x800000))
2963       myexponent = 0;   // denormal
2964   } else if (category==fcZero) {
2965     myexponent = 0;
2966     mysignificand = 0;
2967   } else if (category==fcInfinity) {
2968     myexponent = 0xff;
2969     mysignificand = 0;
2970   } else {
2971     assert(category == fcNaN && "Unknown category!");
2972     myexponent = 0xff;
2973     mysignificand = (uint32_t)*significandParts();
2974   }
2975 
2976   return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2977                     (mysignificand & 0x7fffff)));
2978 }
2979 
2980 APInt IEEEFloat::convertHalfAPFloatToAPInt() const {
2981   assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf);
2982   assert(partCount()==1);
2983 
2984   uint32_t myexponent, mysignificand;
2985 
2986   if (isFiniteNonZero()) {
2987     myexponent = exponent+15; //bias
2988     mysignificand = (uint32_t)*significandParts();
2989     if (myexponent == 1 && !(mysignificand & 0x400))
2990       myexponent = 0;   // denormal
2991   } else if (category==fcZero) {
2992     myexponent = 0;
2993     mysignificand = 0;
2994   } else if (category==fcInfinity) {
2995     myexponent = 0x1f;
2996     mysignificand = 0;
2997   } else {
2998     assert(category == fcNaN && "Unknown category!");
2999     myexponent = 0x1f;
3000     mysignificand = (uint32_t)*significandParts();
3001   }
3002 
3003   return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3004                     (mysignificand & 0x3ff)));
3005 }
3006 
3007 // This function creates an APInt that is just a bit map of the floating
3008 // point constant as it would appear in memory.  It is not a conversion,
3009 // and treating the result as a normal integer is unlikely to be useful.
3010 
3011 APInt IEEEFloat::bitcastToAPInt() const {
3012   if (semantics == (const llvm::fltSemantics*)&semIEEEhalf)
3013     return convertHalfAPFloatToAPInt();
3014 
3015   if (semantics == (const llvm::fltSemantics*)&semIEEEsingle)
3016     return convertFloatAPFloatToAPInt();
3017 
3018   if (semantics == (const llvm::fltSemantics*)&semIEEEdouble)
3019     return convertDoubleAPFloatToAPInt();
3020 
3021   if (semantics == (const llvm::fltSemantics*)&semIEEEquad)
3022     return convertQuadrupleAPFloatToAPInt();
3023 
3024   if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy)
3025     return convertPPCDoubleDoubleAPFloatToAPInt();
3026 
3027   assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended &&
3028          "unknown format!");
3029   return convertF80LongDoubleAPFloatToAPInt();
3030 }
3031 
3032 float IEEEFloat::convertToFloat() const {
3033   assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle &&
3034          "Float semantics are not IEEEsingle");
3035   APInt api = bitcastToAPInt();
3036   return api.bitsToFloat();
3037 }
3038 
3039 double IEEEFloat::convertToDouble() const {
3040   assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble &&
3041          "Float semantics are not IEEEdouble");
3042   APInt api = bitcastToAPInt();
3043   return api.bitsToDouble();
3044 }
3045 
3046 /// Integer bit is explicit in this format.  Intel hardware (387 and later)
3047 /// does not support these bit patterns:
3048 ///  exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3049 ///  exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3050 ///  exponent = 0, integer bit 1 ("pseudodenormal")
3051 ///  exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3052 /// At the moment, the first two are treated as NaNs, the second two as Normal.
3053 void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) {
3054   assert(api.getBitWidth()==80);
3055   uint64_t i1 = api.getRawData()[0];
3056   uint64_t i2 = api.getRawData()[1];
3057   uint64_t myexponent = (i2 & 0x7fff);
3058   uint64_t mysignificand = i1;
3059 
3060   initialize(&semX87DoubleExtended);
3061   assert(partCount()==2);
3062 
3063   sign = static_cast<unsigned int>(i2>>15);
3064   if (myexponent==0 && mysignificand==0) {
3065     // exponent, significand meaningless
3066     category = fcZero;
3067   } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3068     // exponent, significand meaningless
3069     category = fcInfinity;
3070   } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3071     // exponent meaningless
3072     category = fcNaN;
3073     significandParts()[0] = mysignificand;
3074     significandParts()[1] = 0;
3075   } else {
3076     category = fcNormal;
3077     exponent = myexponent - 16383;
3078     significandParts()[0] = mysignificand;
3079     significandParts()[1] = 0;
3080     if (myexponent==0)          // denormal
3081       exponent = -16382;
3082   }
3083 }
3084 
3085 void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) {
3086   assert(api.getBitWidth()==128);
3087   uint64_t i1 = api.getRawData()[0];
3088   uint64_t i2 = api.getRawData()[1];
3089   opStatus fs;
3090   bool losesInfo;
3091 
3092   // Get the first double and convert to our format.
3093   initFromDoubleAPInt(APInt(64, i1));
3094   fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
3095   assert(fs == opOK && !losesInfo);
3096   (void)fs;
3097 
3098   // Unless we have a special case, add in second double.
3099   if (isFiniteNonZero()) {
3100     IEEEFloat v(semIEEEdouble, APInt(64, i2));
3101     fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo);
3102     assert(fs == opOK && !losesInfo);
3103     (void)fs;
3104 
3105     add(v, rmNearestTiesToEven);
3106   }
3107 }
3108 
3109 void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) {
3110   assert(api.getBitWidth()==128);
3111   uint64_t i1 = api.getRawData()[0];
3112   uint64_t i2 = api.getRawData()[1];
3113   uint64_t myexponent = (i2 >> 48) & 0x7fff;
3114   uint64_t mysignificand  = i1;
3115   uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3116 
3117   initialize(&semIEEEquad);
3118   assert(partCount()==2);
3119 
3120   sign = static_cast<unsigned int>(i2>>63);
3121   if (myexponent==0 &&
3122       (mysignificand==0 && mysignificand2==0)) {
3123     // exponent, significand meaningless
3124     category = fcZero;
3125   } else if (myexponent==0x7fff &&
3126              (mysignificand==0 && mysignificand2==0)) {
3127     // exponent, significand meaningless
3128     category = fcInfinity;
3129   } else if (myexponent==0x7fff &&
3130              (mysignificand!=0 || mysignificand2 !=0)) {
3131     // exponent meaningless
3132     category = fcNaN;
3133     significandParts()[0] = mysignificand;
3134     significandParts()[1] = mysignificand2;
3135   } else {
3136     category = fcNormal;
3137     exponent = myexponent - 16383;
3138     significandParts()[0] = mysignificand;
3139     significandParts()[1] = mysignificand2;
3140     if (myexponent==0)          // denormal
3141       exponent = -16382;
3142     else
3143       significandParts()[1] |= 0x1000000000000LL;  // integer bit
3144   }
3145 }
3146 
3147 void IEEEFloat::initFromDoubleAPInt(const APInt &api) {
3148   assert(api.getBitWidth()==64);
3149   uint64_t i = *api.getRawData();
3150   uint64_t myexponent = (i >> 52) & 0x7ff;
3151   uint64_t mysignificand = i & 0xfffffffffffffLL;
3152 
3153   initialize(&semIEEEdouble);
3154   assert(partCount()==1);
3155 
3156   sign = static_cast<unsigned int>(i>>63);
3157   if (myexponent==0 && mysignificand==0) {
3158     // exponent, significand meaningless
3159     category = fcZero;
3160   } else if (myexponent==0x7ff && mysignificand==0) {
3161     // exponent, significand meaningless
3162     category = fcInfinity;
3163   } else if (myexponent==0x7ff && mysignificand!=0) {
3164     // exponent meaningless
3165     category = fcNaN;
3166     *significandParts() = mysignificand;
3167   } else {
3168     category = fcNormal;
3169     exponent = myexponent - 1023;
3170     *significandParts() = mysignificand;
3171     if (myexponent==0)          // denormal
3172       exponent = -1022;
3173     else
3174       *significandParts() |= 0x10000000000000LL;  // integer bit
3175   }
3176 }
3177 
3178 void IEEEFloat::initFromFloatAPInt(const APInt &api) {
3179   assert(api.getBitWidth()==32);
3180   uint32_t i = (uint32_t)*api.getRawData();
3181   uint32_t myexponent = (i >> 23) & 0xff;
3182   uint32_t mysignificand = i & 0x7fffff;
3183 
3184   initialize(&semIEEEsingle);
3185   assert(partCount()==1);
3186 
3187   sign = i >> 31;
3188   if (myexponent==0 && mysignificand==0) {
3189     // exponent, significand meaningless
3190     category = fcZero;
3191   } else if (myexponent==0xff && mysignificand==0) {
3192     // exponent, significand meaningless
3193     category = fcInfinity;
3194   } else if (myexponent==0xff && mysignificand!=0) {
3195     // sign, exponent, significand meaningless
3196     category = fcNaN;
3197     *significandParts() = mysignificand;
3198   } else {
3199     category = fcNormal;
3200     exponent = myexponent - 127;  //bias
3201     *significandParts() = mysignificand;
3202     if (myexponent==0)    // denormal
3203       exponent = -126;
3204     else
3205       *significandParts() |= 0x800000; // integer bit
3206   }
3207 }
3208 
3209 void IEEEFloat::initFromHalfAPInt(const APInt &api) {
3210   assert(api.getBitWidth()==16);
3211   uint32_t i = (uint32_t)*api.getRawData();
3212   uint32_t myexponent = (i >> 10) & 0x1f;
3213   uint32_t mysignificand = i & 0x3ff;
3214 
3215   initialize(&semIEEEhalf);
3216   assert(partCount()==1);
3217 
3218   sign = i >> 15;
3219   if (myexponent==0 && mysignificand==0) {
3220     // exponent, significand meaningless
3221     category = fcZero;
3222   } else if (myexponent==0x1f && mysignificand==0) {
3223     // exponent, significand meaningless
3224     category = fcInfinity;
3225   } else if (myexponent==0x1f && mysignificand!=0) {
3226     // sign, exponent, significand meaningless
3227     category = fcNaN;
3228     *significandParts() = mysignificand;
3229   } else {
3230     category = fcNormal;
3231     exponent = myexponent - 15;  //bias
3232     *significandParts() = mysignificand;
3233     if (myexponent==0)    // denormal
3234       exponent = -14;
3235     else
3236       *significandParts() |= 0x400; // integer bit
3237   }
3238 }
3239 
3240 /// Treat api as containing the bits of a floating point number.  Currently
3241 /// we infer the floating point type from the size of the APInt.  The
3242 /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3243 /// when the size is anything else).
3244 void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) {
3245   if (Sem == &semIEEEhalf)
3246     return initFromHalfAPInt(api);
3247   if (Sem == &semIEEEsingle)
3248     return initFromFloatAPInt(api);
3249   if (Sem == &semIEEEdouble)
3250     return initFromDoubleAPInt(api);
3251   if (Sem == &semX87DoubleExtended)
3252     return initFromF80LongDoubleAPInt(api);
3253   if (Sem == &semIEEEquad)
3254     return initFromQuadrupleAPInt(api);
3255   if (Sem == &semPPCDoubleDoubleLegacy)
3256     return initFromPPCDoubleDoubleAPInt(api);
3257 
3258   llvm_unreachable(nullptr);
3259 }
3260 
3261 /// Make this number the largest magnitude normal number in the given
3262 /// semantics.
3263 void IEEEFloat::makeLargest(bool Negative) {
3264   // We want (in interchange format):
3265   //   sign = {Negative}
3266   //   exponent = 1..10
3267   //   significand = 1..1
3268   category = fcNormal;
3269   sign = Negative;
3270   exponent = semantics->maxExponent;
3271 
3272   // Use memset to set all but the highest integerPart to all ones.
3273   integerPart *significand = significandParts();
3274   unsigned PartCount = partCount();
3275   memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
3276 
3277   // Set the high integerPart especially setting all unused top bits for
3278   // internal consistency.
3279   const unsigned NumUnusedHighBits =
3280     PartCount*integerPartWidth - semantics->precision;
3281   significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
3282                                    ? (~integerPart(0) >> NumUnusedHighBits)
3283                                    : 0;
3284 }
3285 
3286 /// Make this number the smallest magnitude denormal number in the given
3287 /// semantics.
3288 void IEEEFloat::makeSmallest(bool Negative) {
3289   // We want (in interchange format):
3290   //   sign = {Negative}
3291   //   exponent = 0..0
3292   //   significand = 0..01
3293   category = fcNormal;
3294   sign = Negative;
3295   exponent = semantics->minExponent;
3296   APInt::tcSet(significandParts(), 1, partCount());
3297 }
3298 
3299 void IEEEFloat::makeSmallestNormalized(bool Negative) {
3300   // We want (in interchange format):
3301   //   sign = {Negative}
3302   //   exponent = 0..0
3303   //   significand = 10..0
3304 
3305   category = fcNormal;
3306   zeroSignificand();
3307   sign = Negative;
3308   exponent = semantics->minExponent;
3309   significandParts()[partCountForBits(semantics->precision) - 1] |=
3310       (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth));
3311 }
3312 
3313 IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) {
3314   initFromAPInt(&Sem, API);
3315 }
3316 
3317 IEEEFloat::IEEEFloat(float f) {
3318   initFromAPInt(&semIEEEsingle, APInt::floatToBits(f));
3319 }
3320 
3321 IEEEFloat::IEEEFloat(double d) {
3322   initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d));
3323 }
3324 
3325 namespace {
3326   void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3327     Buffer.append(Str.begin(), Str.end());
3328   }
3329 
3330   /// Removes data from the given significand until it is no more
3331   /// precise than is required for the desired precision.
3332   void AdjustToPrecision(APInt &significand,
3333                          int &exp, unsigned FormatPrecision) {
3334     unsigned bits = significand.getActiveBits();
3335 
3336     // 196/59 is a very slight overestimate of lg_2(10).
3337     unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3338 
3339     if (bits <= bitsRequired) return;
3340 
3341     unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3342     if (!tensRemovable) return;
3343 
3344     exp += tensRemovable;
3345 
3346     APInt divisor(significand.getBitWidth(), 1);
3347     APInt powten(significand.getBitWidth(), 10);
3348     while (true) {
3349       if (tensRemovable & 1)
3350         divisor *= powten;
3351       tensRemovable >>= 1;
3352       if (!tensRemovable) break;
3353       powten *= powten;
3354     }
3355 
3356     significand = significand.udiv(divisor);
3357 
3358     // Truncate the significand down to its active bit count.
3359     significand = significand.trunc(significand.getActiveBits());
3360   }
3361 
3362 
3363   void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3364                          int &exp, unsigned FormatPrecision) {
3365     unsigned N = buffer.size();
3366     if (N <= FormatPrecision) return;
3367 
3368     // The most significant figures are the last ones in the buffer.
3369     unsigned FirstSignificant = N - FormatPrecision;
3370 
3371     // Round.
3372     // FIXME: this probably shouldn't use 'round half up'.
3373 
3374     // Rounding down is just a truncation, except we also want to drop
3375     // trailing zeros from the new result.
3376     if (buffer[FirstSignificant - 1] < '5') {
3377       while (FirstSignificant < N && buffer[FirstSignificant] == '0')
3378         FirstSignificant++;
3379 
3380       exp += FirstSignificant;
3381       buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3382       return;
3383     }
3384 
3385     // Rounding up requires a decimal add-with-carry.  If we continue
3386     // the carry, the newly-introduced zeros will just be truncated.
3387     for (unsigned I = FirstSignificant; I != N; ++I) {
3388       if (buffer[I] == '9') {
3389         FirstSignificant++;
3390       } else {
3391         buffer[I]++;
3392         break;
3393       }
3394     }
3395 
3396     // If we carried through, we have exactly one digit of precision.
3397     if (FirstSignificant == N) {
3398       exp += FirstSignificant;
3399       buffer.clear();
3400       buffer.push_back('1');
3401       return;
3402     }
3403 
3404     exp += FirstSignificant;
3405     buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3406   }
3407 }
3408 
3409 void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
3410                          unsigned FormatMaxPadding) const {
3411   switch (category) {
3412   case fcInfinity:
3413     if (isNegative())
3414       return append(Str, "-Inf");
3415     else
3416       return append(Str, "+Inf");
3417 
3418   case fcNaN: return append(Str, "NaN");
3419 
3420   case fcZero:
3421     if (isNegative())
3422       Str.push_back('-');
3423 
3424     if (!FormatMaxPadding)
3425       append(Str, "0.0E+0");
3426     else
3427       Str.push_back('0');
3428     return;
3429 
3430   case fcNormal:
3431     break;
3432   }
3433 
3434   if (isNegative())
3435     Str.push_back('-');
3436 
3437   // Decompose the number into an APInt and an exponent.
3438   int exp = exponent - ((int) semantics->precision - 1);
3439   APInt significand(semantics->precision,
3440                     makeArrayRef(significandParts(),
3441                                  partCountForBits(semantics->precision)));
3442 
3443   // Set FormatPrecision if zero.  We want to do this before we
3444   // truncate trailing zeros, as those are part of the precision.
3445   if (!FormatPrecision) {
3446     // We use enough digits so the number can be round-tripped back to an
3447     // APFloat. The formula comes from "How to Print Floating-Point Numbers
3448     // Accurately" by Steele and White.
3449     // FIXME: Using a formula based purely on the precision is conservative;
3450     // we can print fewer digits depending on the actual value being printed.
3451 
3452     // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3453     FormatPrecision = 2 + semantics->precision * 59 / 196;
3454   }
3455 
3456   // Ignore trailing binary zeros.
3457   int trailingZeros = significand.countTrailingZeros();
3458   exp += trailingZeros;
3459   significand = significand.lshr(trailingZeros);
3460 
3461   // Change the exponent from 2^e to 10^e.
3462   if (exp == 0) {
3463     // Nothing to do.
3464   } else if (exp > 0) {
3465     // Just shift left.
3466     significand = significand.zext(semantics->precision + exp);
3467     significand <<= exp;
3468     exp = 0;
3469   } else { /* exp < 0 */
3470     int texp = -exp;
3471 
3472     // We transform this using the identity:
3473     //   (N)(2^-e) == (N)(5^e)(10^-e)
3474     // This means we have to multiply N (the significand) by 5^e.
3475     // To avoid overflow, we have to operate on numbers large
3476     // enough to store N * 5^e:
3477     //   log2(N * 5^e) == log2(N) + e * log2(5)
3478     //                 <= semantics->precision + e * 137 / 59
3479     //   (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
3480 
3481     unsigned precision = semantics->precision + (137 * texp + 136) / 59;
3482 
3483     // Multiply significand by 5^e.
3484     //   N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
3485     significand = significand.zext(precision);
3486     APInt five_to_the_i(precision, 5);
3487     while (true) {
3488       if (texp & 1) significand *= five_to_the_i;
3489 
3490       texp >>= 1;
3491       if (!texp) break;
3492       five_to_the_i *= five_to_the_i;
3493     }
3494   }
3495 
3496   AdjustToPrecision(significand, exp, FormatPrecision);
3497 
3498   SmallVector<char, 256> buffer;
3499 
3500   // Fill the buffer.
3501   unsigned precision = significand.getBitWidth();
3502   APInt ten(precision, 10);
3503   APInt digit(precision, 0);
3504 
3505   bool inTrail = true;
3506   while (significand != 0) {
3507     // digit <- significand % 10
3508     // significand <- significand / 10
3509     APInt::udivrem(significand, ten, significand, digit);
3510 
3511     unsigned d = digit.getZExtValue();
3512 
3513     // Drop trailing zeros.
3514     if (inTrail && !d) exp++;
3515     else {
3516       buffer.push_back((char) ('0' + d));
3517       inTrail = false;
3518     }
3519   }
3520 
3521   assert(!buffer.empty() && "no characters in buffer!");
3522 
3523   // Drop down to FormatPrecision.
3524   // TODO: don't do more precise calculations above than are required.
3525   AdjustToPrecision(buffer, exp, FormatPrecision);
3526 
3527   unsigned NDigits = buffer.size();
3528 
3529   // Check whether we should use scientific notation.
3530   bool FormatScientific;
3531   if (!FormatMaxPadding)
3532     FormatScientific = true;
3533   else {
3534     if (exp >= 0) {
3535       // 765e3 --> 765000
3536       //              ^^^
3537       // But we shouldn't make the number look more precise than it is.
3538       FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3539                           NDigits + (unsigned) exp > FormatPrecision);
3540     } else {
3541       // Power of the most significant digit.
3542       int MSD = exp + (int) (NDigits - 1);
3543       if (MSD >= 0) {
3544         // 765e-2 == 7.65
3545         FormatScientific = false;
3546       } else {
3547         // 765e-5 == 0.00765
3548         //           ^ ^^
3549         FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
3550       }
3551     }
3552   }
3553 
3554   // Scientific formatting is pretty straightforward.
3555   if (FormatScientific) {
3556     exp += (NDigits - 1);
3557 
3558     Str.push_back(buffer[NDigits-1]);
3559     Str.push_back('.');
3560     if (NDigits == 1)
3561       Str.push_back('0');
3562     else
3563       for (unsigned I = 1; I != NDigits; ++I)
3564         Str.push_back(buffer[NDigits-1-I]);
3565     Str.push_back('E');
3566 
3567     Str.push_back(exp >= 0 ? '+' : '-');
3568     if (exp < 0) exp = -exp;
3569     SmallVector<char, 6> expbuf;
3570     do {
3571       expbuf.push_back((char) ('0' + (exp % 10)));
3572       exp /= 10;
3573     } while (exp);
3574     for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3575       Str.push_back(expbuf[E-1-I]);
3576     return;
3577   }
3578 
3579   // Non-scientific, positive exponents.
3580   if (exp >= 0) {
3581     for (unsigned I = 0; I != NDigits; ++I)
3582       Str.push_back(buffer[NDigits-1-I]);
3583     for (unsigned I = 0; I != (unsigned) exp; ++I)
3584       Str.push_back('0');
3585     return;
3586   }
3587 
3588   // Non-scientific, negative exponents.
3589 
3590   // The number of digits to the left of the decimal point.
3591   int NWholeDigits = exp + (int) NDigits;
3592 
3593   unsigned I = 0;
3594   if (NWholeDigits > 0) {
3595     for (; I != (unsigned) NWholeDigits; ++I)
3596       Str.push_back(buffer[NDigits-I-1]);
3597     Str.push_back('.');
3598   } else {
3599     unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3600 
3601     Str.push_back('0');
3602     Str.push_back('.');
3603     for (unsigned Z = 1; Z != NZeros; ++Z)
3604       Str.push_back('0');
3605   }
3606 
3607   for (; I != NDigits; ++I)
3608     Str.push_back(buffer[NDigits-I-1]);
3609 }
3610 
3611 bool IEEEFloat::getExactInverse(APFloat *inv) const {
3612   // Special floats and denormals have no exact inverse.
3613   if (!isFiniteNonZero())
3614     return false;
3615 
3616   // Check that the number is a power of two by making sure that only the
3617   // integer bit is set in the significand.
3618   if (significandLSB() != semantics->precision - 1)
3619     return false;
3620 
3621   // Get the inverse.
3622   IEEEFloat reciprocal(*semantics, 1ULL);
3623   if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3624     return false;
3625 
3626   // Avoid multiplication with a denormal, it is not safe on all platforms and
3627   // may be slower than a normal division.
3628   if (reciprocal.isDenormal())
3629     return false;
3630 
3631   assert(reciprocal.isFiniteNonZero() &&
3632          reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3633 
3634   if (inv)
3635     *inv = APFloat(reciprocal, *semantics);
3636 
3637   return true;
3638 }
3639 
3640 bool IEEEFloat::isSignaling() const {
3641   if (!isNaN())
3642     return false;
3643 
3644   // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3645   // first bit of the trailing significand being 0.
3646   return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3647 }
3648 
3649 /// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3650 ///
3651 /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3652 /// appropriate sign switching before/after the computation.
3653 IEEEFloat::opStatus IEEEFloat::next(bool nextDown) {
3654   // If we are performing nextDown, swap sign so we have -x.
3655   if (nextDown)
3656     changeSign();
3657 
3658   // Compute nextUp(x)
3659   opStatus result = opOK;
3660 
3661   // Handle each float category separately.
3662   switch (category) {
3663   case fcInfinity:
3664     // nextUp(+inf) = +inf
3665     if (!isNegative())
3666       break;
3667     // nextUp(-inf) = -getLargest()
3668     makeLargest(true);
3669     break;
3670   case fcNaN:
3671     // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3672     // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3673     //                     change the payload.
3674     if (isSignaling()) {
3675       result = opInvalidOp;
3676       // For consistency, propagate the sign of the sNaN to the qNaN.
3677       makeNaN(false, isNegative(), nullptr);
3678     }
3679     break;
3680   case fcZero:
3681     // nextUp(pm 0) = +getSmallest()
3682     makeSmallest(false);
3683     break;
3684   case fcNormal:
3685     // nextUp(-getSmallest()) = -0
3686     if (isSmallest() && isNegative()) {
3687       APInt::tcSet(significandParts(), 0, partCount());
3688       category = fcZero;
3689       exponent = 0;
3690       break;
3691     }
3692 
3693     // nextUp(getLargest()) == INFINITY
3694     if (isLargest() && !isNegative()) {
3695       APInt::tcSet(significandParts(), 0, partCount());
3696       category = fcInfinity;
3697       exponent = semantics->maxExponent + 1;
3698       break;
3699     }
3700 
3701     // nextUp(normal) == normal + inc.
3702     if (isNegative()) {
3703       // If we are negative, we need to decrement the significand.
3704 
3705       // We only cross a binade boundary that requires adjusting the exponent
3706       // if:
3707       //   1. exponent != semantics->minExponent. This implies we are not in the
3708       //   smallest binade or are dealing with denormals.
3709       //   2. Our significand excluding the integral bit is all zeros.
3710       bool WillCrossBinadeBoundary =
3711         exponent != semantics->minExponent && isSignificandAllZeros();
3712 
3713       // Decrement the significand.
3714       //
3715       // We always do this since:
3716       //   1. If we are dealing with a non-binade decrement, by definition we
3717       //   just decrement the significand.
3718       //   2. If we are dealing with a normal -> normal binade decrement, since
3719       //   we have an explicit integral bit the fact that all bits but the
3720       //   integral bit are zero implies that subtracting one will yield a
3721       //   significand with 0 integral bit and 1 in all other spots. Thus we
3722       //   must just adjust the exponent and set the integral bit to 1.
3723       //   3. If we are dealing with a normal -> denormal binade decrement,
3724       //   since we set the integral bit to 0 when we represent denormals, we
3725       //   just decrement the significand.
3726       integerPart *Parts = significandParts();
3727       APInt::tcDecrement(Parts, partCount());
3728 
3729       if (WillCrossBinadeBoundary) {
3730         // Our result is a normal number. Do the following:
3731         // 1. Set the integral bit to 1.
3732         // 2. Decrement the exponent.
3733         APInt::tcSetBit(Parts, semantics->precision - 1);
3734         exponent--;
3735       }
3736     } else {
3737       // If we are positive, we need to increment the significand.
3738 
3739       // We only cross a binade boundary that requires adjusting the exponent if
3740       // the input is not a denormal and all of said input's significand bits
3741       // are set. If all of said conditions are true: clear the significand, set
3742       // the integral bit to 1, and increment the exponent. If we have a
3743       // denormal always increment since moving denormals and the numbers in the
3744       // smallest normal binade have the same exponent in our representation.
3745       bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3746 
3747       if (WillCrossBinadeBoundary) {
3748         integerPart *Parts = significandParts();
3749         APInt::tcSet(Parts, 0, partCount());
3750         APInt::tcSetBit(Parts, semantics->precision - 1);
3751         assert(exponent != semantics->maxExponent &&
3752                "We can not increment an exponent beyond the maxExponent allowed"
3753                " by the given floating point semantics.");
3754         exponent++;
3755       } else {
3756         incrementSignificand();
3757       }
3758     }
3759     break;
3760   }
3761 
3762   // If we are performing nextDown, swap sign so we have -nextUp(-x)
3763   if (nextDown)
3764     changeSign();
3765 
3766   return result;
3767 }
3768 
3769 void IEEEFloat::makeInf(bool Negative) {
3770   category = fcInfinity;
3771   sign = Negative;
3772   exponent = semantics->maxExponent + 1;
3773   APInt::tcSet(significandParts(), 0, partCount());
3774 }
3775 
3776 void IEEEFloat::makeZero(bool Negative) {
3777   category = fcZero;
3778   sign = Negative;
3779   exponent = semantics->minExponent-1;
3780   APInt::tcSet(significandParts(), 0, partCount());
3781 }
3782 
3783 void IEEEFloat::makeQuiet() {
3784   assert(isNaN());
3785   APInt::tcSetBit(significandParts(), semantics->precision - 2);
3786 }
3787 
3788 int ilogb(const IEEEFloat &Arg) {
3789   if (Arg.isNaN())
3790     return IEEEFloat::IEK_NaN;
3791   if (Arg.isZero())
3792     return IEEEFloat::IEK_Zero;
3793   if (Arg.isInfinity())
3794     return IEEEFloat::IEK_Inf;
3795   if (!Arg.isDenormal())
3796     return Arg.exponent;
3797 
3798   IEEEFloat Normalized(Arg);
3799   int SignificandBits = Arg.getSemantics().precision - 1;
3800 
3801   Normalized.exponent += SignificandBits;
3802   Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero);
3803   return Normalized.exponent - SignificandBits;
3804 }
3805 
3806 IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) {
3807   auto MaxExp = X.getSemantics().maxExponent;
3808   auto MinExp = X.getSemantics().minExponent;
3809 
3810   // If Exp is wildly out-of-scale, simply adding it to X.exponent will
3811   // overflow; clamp it to a safe range before adding, but ensure that the range
3812   // is large enough that the clamp does not change the result. The range we
3813   // need to support is the difference between the largest possible exponent and
3814   // the normalized exponent of half the smallest denormal.
3815 
3816   int SignificandBits = X.getSemantics().precision - 1;
3817   int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3818 
3819   // Clamp to one past the range ends to let normalize handle overlflow.
3820   X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3821   X.normalize(RoundingMode, lfExactlyZero);
3822   if (X.isNaN())
3823     X.makeQuiet();
3824   return X;
3825 }
3826 
3827 IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) {
3828   Exp = ilogb(Val);
3829 
3830   // Quiet signalling nans.
3831   if (Exp == IEEEFloat::IEK_NaN) {
3832     IEEEFloat Quiet(Val);
3833     Quiet.makeQuiet();
3834     return Quiet;
3835   }
3836 
3837   if (Exp == IEEEFloat::IEK_Inf)
3838     return Val;
3839 
3840   // 1 is added because frexp is defined to return a normalized fraction in
3841   // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0).
3842   Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1;
3843   return scalbn(Val, -Exp, RM);
3844 }
3845 
3846 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S)
3847     : Semantics(&S),
3848       Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) {
3849   assert(Semantics == &semPPCDoubleDouble);
3850 }
3851 
3852 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag)
3853     : Semantics(&S),
3854       Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized),
3855                             APFloat(semIEEEdouble, uninitialized)}) {
3856   assert(Semantics == &semPPCDoubleDouble);
3857 }
3858 
3859 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I)
3860     : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I),
3861                                            APFloat(semIEEEdouble)}) {
3862   assert(Semantics == &semPPCDoubleDouble);
3863 }
3864 
3865 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I)
3866     : Semantics(&S),
3867       Floats(new APFloat[2]{
3868           APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])),
3869           APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) {
3870   assert(Semantics == &semPPCDoubleDouble);
3871 }
3872 
3873 DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First,
3874                              APFloat &&Second)
3875     : Semantics(&S),
3876       Floats(new APFloat[2]{std::move(First), std::move(Second)}) {
3877   assert(Semantics == &semPPCDoubleDouble);
3878   assert(&Floats[0].getSemantics() == &semIEEEdouble);
3879   assert(&Floats[1].getSemantics() == &semIEEEdouble);
3880 }
3881 
3882 DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS)
3883     : Semantics(RHS.Semantics),
3884       Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]),
3885                                          APFloat(RHS.Floats[1])}
3886                         : nullptr) {
3887   assert(Semantics == &semPPCDoubleDouble);
3888 }
3889 
3890 DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS)
3891     : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) {
3892   RHS.Semantics = &semBogus;
3893   assert(Semantics == &semPPCDoubleDouble);
3894 }
3895 
3896 DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) {
3897   if (Semantics == RHS.Semantics && RHS.Floats) {
3898     Floats[0] = RHS.Floats[0];
3899     Floats[1] = RHS.Floats[1];
3900   } else if (this != &RHS) {
3901     this->~DoubleAPFloat();
3902     new (this) DoubleAPFloat(RHS);
3903   }
3904   return *this;
3905 }
3906 
3907 // Implement addition, subtraction, multiplication and division based on:
3908 // "Software for Doubled-Precision Floating-Point Computations",
3909 // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283.
3910 APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa,
3911                                          const APFloat &c, const APFloat &cc,
3912                                          roundingMode RM) {
3913   int Status = opOK;
3914   APFloat z = a;
3915   Status |= z.add(c, RM);
3916   if (!z.isFinite()) {
3917     if (!z.isInfinity()) {
3918       Floats[0] = std::move(z);
3919       Floats[1].makeZero(/* Neg = */ false);
3920       return (opStatus)Status;
3921     }
3922     Status = opOK;
3923     auto AComparedToC = a.compareAbsoluteValue(c);
3924     z = cc;
3925     Status |= z.add(aa, RM);
3926     if (AComparedToC == APFloat::cmpGreaterThan) {
3927       // z = cc + aa + c + a;
3928       Status |= z.add(c, RM);
3929       Status |= z.add(a, RM);
3930     } else {
3931       // z = cc + aa + a + c;
3932       Status |= z.add(a, RM);
3933       Status |= z.add(c, RM);
3934     }
3935     if (!z.isFinite()) {
3936       Floats[0] = std::move(z);
3937       Floats[1].makeZero(/* Neg = */ false);
3938       return (opStatus)Status;
3939     }
3940     Floats[0] = z;
3941     APFloat zz = aa;
3942     Status |= zz.add(cc, RM);
3943     if (AComparedToC == APFloat::cmpGreaterThan) {
3944       // Floats[1] = a - z + c + zz;
3945       Floats[1] = a;
3946       Status |= Floats[1].subtract(z, RM);
3947       Status |= Floats[1].add(c, RM);
3948       Status |= Floats[1].add(zz, RM);
3949     } else {
3950       // Floats[1] = c - z + a + zz;
3951       Floats[1] = c;
3952       Status |= Floats[1].subtract(z, RM);
3953       Status |= Floats[1].add(a, RM);
3954       Status |= Floats[1].add(zz, RM);
3955     }
3956   } else {
3957     // q = a - z;
3958     APFloat q = a;
3959     Status |= q.subtract(z, RM);
3960 
3961     // zz = q + c + (a - (q + z)) + aa + cc;
3962     // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies.
3963     auto zz = q;
3964     Status |= zz.add(c, RM);
3965     Status |= q.add(z, RM);
3966     Status |= q.subtract(a, RM);
3967     q.changeSign();
3968     Status |= zz.add(q, RM);
3969     Status |= zz.add(aa, RM);
3970     Status |= zz.add(cc, RM);
3971     if (zz.isZero() && !zz.isNegative()) {
3972       Floats[0] = std::move(z);
3973       Floats[1].makeZero(/* Neg = */ false);
3974       return opOK;
3975     }
3976     Floats[0] = z;
3977     Status |= Floats[0].add(zz, RM);
3978     if (!Floats[0].isFinite()) {
3979       Floats[1].makeZero(/* Neg = */ false);
3980       return (opStatus)Status;
3981     }
3982     Floats[1] = std::move(z);
3983     Status |= Floats[1].subtract(Floats[0], RM);
3984     Status |= Floats[1].add(zz, RM);
3985   }
3986   return (opStatus)Status;
3987 }
3988 
3989 APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS,
3990                                                 const DoubleAPFloat &RHS,
3991                                                 DoubleAPFloat &Out,
3992                                                 roundingMode RM) {
3993   if (LHS.getCategory() == fcNaN) {
3994     Out = LHS;
3995     return opOK;
3996   }
3997   if (RHS.getCategory() == fcNaN) {
3998     Out = RHS;
3999     return opOK;
4000   }
4001   if (LHS.getCategory() == fcZero) {
4002     Out = RHS;
4003     return opOK;
4004   }
4005   if (RHS.getCategory() == fcZero) {
4006     Out = LHS;
4007     return opOK;
4008   }
4009   if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity &&
4010       LHS.isNegative() != RHS.isNegative()) {
4011     Out.makeNaN(false, Out.isNegative(), nullptr);
4012     return opInvalidOp;
4013   }
4014   if (LHS.getCategory() == fcInfinity) {
4015     Out = LHS;
4016     return opOK;
4017   }
4018   if (RHS.getCategory() == fcInfinity) {
4019     Out = RHS;
4020     return opOK;
4021   }
4022   assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal);
4023 
4024   APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]),
4025       CC(RHS.Floats[1]);
4026   assert(&A.getSemantics() == &semIEEEdouble);
4027   assert(&AA.getSemantics() == &semIEEEdouble);
4028   assert(&C.getSemantics() == &semIEEEdouble);
4029   assert(&CC.getSemantics() == &semIEEEdouble);
4030   assert(&Out.Floats[0].getSemantics() == &semIEEEdouble);
4031   assert(&Out.Floats[1].getSemantics() == &semIEEEdouble);
4032   return Out.addImpl(A, AA, C, CC, RM);
4033 }
4034 
4035 APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS,
4036                                      roundingMode RM) {
4037   return addWithSpecial(*this, RHS, *this, RM);
4038 }
4039 
4040 APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS,
4041                                           roundingMode RM) {
4042   changeSign();
4043   auto Ret = add(RHS, RM);
4044   changeSign();
4045   return Ret;
4046 }
4047 
4048 APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS,
4049                                           APFloat::roundingMode RM) {
4050   const auto &LHS = *this;
4051   auto &Out = *this;
4052   /* Interesting observation: For special categories, finding the lowest
4053      common ancestor of the following layered graph gives the correct
4054      return category:
4055 
4056         NaN
4057        /   \
4058      Zero  Inf
4059        \   /
4060        Normal
4061 
4062      e.g. NaN * NaN = NaN
4063           Zero * Inf = NaN
4064           Normal * Zero = Zero
4065           Normal * Inf = Inf
4066   */
4067   if (LHS.getCategory() == fcNaN) {
4068     Out = LHS;
4069     return opOK;
4070   }
4071   if (RHS.getCategory() == fcNaN) {
4072     Out = RHS;
4073     return opOK;
4074   }
4075   if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) ||
4076       (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) {
4077     Out.makeNaN(false, false, nullptr);
4078     return opOK;
4079   }
4080   if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) {
4081     Out = LHS;
4082     return opOK;
4083   }
4084   if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) {
4085     Out = RHS;
4086     return opOK;
4087   }
4088   assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal &&
4089          "Special cases not handled exhaustively");
4090 
4091   int Status = opOK;
4092   APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1];
4093   // t = a * c
4094   APFloat T = A;
4095   Status |= T.multiply(C, RM);
4096   if (!T.isFiniteNonZero()) {
4097     Floats[0] = T;
4098     Floats[1].makeZero(/* Neg = */ false);
4099     return (opStatus)Status;
4100   }
4101 
4102   // tau = fmsub(a, c, t), that is -fmadd(-a, c, t).
4103   APFloat Tau = A;
4104   T.changeSign();
4105   Status |= Tau.fusedMultiplyAdd(C, T, RM);
4106   T.changeSign();
4107   {
4108     // v = a * d
4109     APFloat V = A;
4110     Status |= V.multiply(D, RM);
4111     // w = b * c
4112     APFloat W = B;
4113     Status |= W.multiply(C, RM);
4114     Status |= V.add(W, RM);
4115     // tau += v + w
4116     Status |= Tau.add(V, RM);
4117   }
4118   // u = t + tau
4119   APFloat U = T;
4120   Status |= U.add(Tau, RM);
4121 
4122   Floats[0] = U;
4123   if (!U.isFinite()) {
4124     Floats[1].makeZero(/* Neg = */ false);
4125   } else {
4126     // Floats[1] = (t - u) + tau
4127     Status |= T.subtract(U, RM);
4128     Status |= T.add(Tau, RM);
4129     Floats[1] = T;
4130   }
4131   return (opStatus)Status;
4132 }
4133 
4134 APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS,
4135                                         APFloat::roundingMode RM) {
4136   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4137   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4138   auto Ret =
4139       Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM);
4140   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4141   return Ret;
4142 }
4143 
4144 APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) {
4145   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4146   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4147   auto Ret =
4148       Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4149   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4150   return Ret;
4151 }
4152 
4153 APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) {
4154   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4155   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4156   auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()));
4157   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4158   return Ret;
4159 }
4160 
4161 APFloat::opStatus
4162 DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand,
4163                                 const DoubleAPFloat &Addend,
4164                                 APFloat::roundingMode RM) {
4165   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4166   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4167   auto Ret = Tmp.fusedMultiplyAdd(
4168       APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()),
4169       APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM);
4170   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4171   return Ret;
4172 }
4173 
4174 APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) {
4175   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4176   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4177   auto Ret = Tmp.roundToIntegral(RM);
4178   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4179   return Ret;
4180 }
4181 
4182 void DoubleAPFloat::changeSign() {
4183   Floats[0].changeSign();
4184   Floats[1].changeSign();
4185 }
4186 
4187 APFloat::cmpResult
4188 DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const {
4189   auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
4190   if (Result != cmpEqual)
4191     return Result;
4192   Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
4193   if (Result == cmpLessThan || Result == cmpGreaterThan) {
4194     auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4195     auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative();
4196     if (Against && !RHSAgainst)
4197       return cmpLessThan;
4198     if (!Against && RHSAgainst)
4199       return cmpGreaterThan;
4200     if (!Against && !RHSAgainst)
4201       return Result;
4202     if (Against && RHSAgainst)
4203       return (cmpResult)(cmpLessThan + cmpGreaterThan - Result);
4204   }
4205   return Result;
4206 }
4207 
4208 APFloat::fltCategory DoubleAPFloat::getCategory() const {
4209   return Floats[0].getCategory();
4210 }
4211 
4212 bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); }
4213 
4214 void DoubleAPFloat::makeInf(bool Neg) {
4215   Floats[0].makeInf(Neg);
4216   Floats[1].makeZero(/* Neg = */ false);
4217 }
4218 
4219 void DoubleAPFloat::makeZero(bool Neg) {
4220   Floats[0].makeZero(Neg);
4221   Floats[1].makeZero(/* Neg = */ false);
4222 }
4223 
4224 void DoubleAPFloat::makeLargest(bool Neg) {
4225   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4226   Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull));
4227   Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull));
4228   if (Neg)
4229     changeSign();
4230 }
4231 
4232 void DoubleAPFloat::makeSmallest(bool Neg) {
4233   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4234   Floats[0].makeSmallest(Neg);
4235   Floats[1].makeZero(/* Neg = */ false);
4236 }
4237 
4238 void DoubleAPFloat::makeSmallestNormalized(bool Neg) {
4239   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4240   Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull));
4241   if (Neg)
4242     Floats[0].changeSign();
4243   Floats[1].makeZero(/* Neg = */ false);
4244 }
4245 
4246 void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) {
4247   Floats[0].makeNaN(SNaN, Neg, fill);
4248   Floats[1].makeZero(/* Neg = */ false);
4249 }
4250 
4251 APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const {
4252   auto Result = Floats[0].compare(RHS.Floats[0]);
4253   // |Float[0]| > |Float[1]|
4254   if (Result == APFloat::cmpEqual)
4255     return Floats[1].compare(RHS.Floats[1]);
4256   return Result;
4257 }
4258 
4259 bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const {
4260   return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
4261          Floats[1].bitwiseIsEqual(RHS.Floats[1]);
4262 }
4263 
4264 hash_code hash_value(const DoubleAPFloat &Arg) {
4265   if (Arg.Floats)
4266     return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1]));
4267   return hash_combine(Arg.Semantics);
4268 }
4269 
4270 APInt DoubleAPFloat::bitcastToAPInt() const {
4271   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4272   uint64_t Data[] = {
4273       Floats[0].bitcastToAPInt().getRawData()[0],
4274       Floats[1].bitcastToAPInt().getRawData()[0],
4275   };
4276   return APInt(128, 2, Data);
4277 }
4278 
4279 APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S,
4280                                                    roundingMode RM) {
4281   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4282   APFloat Tmp(semPPCDoubleDoubleLegacy);
4283   auto Ret = Tmp.convertFromString(S, RM);
4284   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4285   return Ret;
4286 }
4287 
4288 APFloat::opStatus DoubleAPFloat::next(bool nextDown) {
4289   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4290   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4291   auto Ret = Tmp.next(nextDown);
4292   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4293   return Ret;
4294 }
4295 
4296 APFloat::opStatus DoubleAPFloat::convertToInteger(integerPart *Input,
4297                                                   unsigned int Width,
4298                                                   bool IsSigned,
4299                                                   roundingMode RM,
4300                                                   bool *IsExact) const {
4301   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4302   return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4303       .convertToInteger(Input, Width, IsSigned, RM, IsExact);
4304 }
4305 
4306 APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input,
4307                                                   bool IsSigned,
4308                                                   roundingMode RM) {
4309   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4310   APFloat Tmp(semPPCDoubleDoubleLegacy);
4311   auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM);
4312   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4313   return Ret;
4314 }
4315 
4316 APFloat::opStatus
4317 DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input,
4318                                               unsigned int InputSize,
4319                                               bool IsSigned, roundingMode RM) {
4320   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4321   APFloat Tmp(semPPCDoubleDoubleLegacy);
4322   auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM);
4323   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4324   return Ret;
4325 }
4326 
4327 APFloat::opStatus
4328 DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input,
4329                                               unsigned int InputSize,
4330                                               bool IsSigned, roundingMode RM) {
4331   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4332   APFloat Tmp(semPPCDoubleDoubleLegacy);
4333   auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM);
4334   *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt());
4335   return Ret;
4336 }
4337 
4338 unsigned int DoubleAPFloat::convertToHexString(char *DST,
4339                                                unsigned int HexDigits,
4340                                                bool UpperCase,
4341                                                roundingMode RM) const {
4342   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4343   return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4344       .convertToHexString(DST, HexDigits, UpperCase, RM);
4345 }
4346 
4347 bool DoubleAPFloat::isDenormal() const {
4348   return getCategory() == fcNormal &&
4349          (Floats[0].isDenormal() || Floats[1].isDenormal() ||
4350           // (double)(Hi + Lo) == Hi defines a normal number.
4351           Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual);
4352 }
4353 
4354 bool DoubleAPFloat::isSmallest() const {
4355   if (getCategory() != fcNormal)
4356     return false;
4357   DoubleAPFloat Tmp(*this);
4358   Tmp.makeSmallest(this->isNegative());
4359   return Tmp.compare(*this) == cmpEqual;
4360 }
4361 
4362 bool DoubleAPFloat::isLargest() const {
4363   if (getCategory() != fcNormal)
4364     return false;
4365   DoubleAPFloat Tmp(*this);
4366   Tmp.makeLargest(this->isNegative());
4367   return Tmp.compare(*this) == cmpEqual;
4368 }
4369 
4370 bool DoubleAPFloat::isInteger() const {
4371   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4372   APFloat Tmp(semPPCDoubleDoubleLegacy);
4373   (void)Tmp.add(Floats[0], rmNearestTiesToEven);
4374   (void)Tmp.add(Floats[1], rmNearestTiesToEven);
4375   return Tmp.isInteger();
4376 }
4377 
4378 void DoubleAPFloat::toString(SmallVectorImpl<char> &Str,
4379                              unsigned FormatPrecision,
4380                              unsigned FormatMaxPadding) const {
4381   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4382   APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt())
4383       .toString(Str, FormatPrecision, FormatMaxPadding);
4384 }
4385 
4386 bool DoubleAPFloat::getExactInverse(APFloat *inv) const {
4387   assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4388   APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt());
4389   if (!inv)
4390     return Tmp.getExactInverse(nullptr);
4391   APFloat Inv(semPPCDoubleDoubleLegacy);
4392   auto Ret = Tmp.getExactInverse(&Inv);
4393   *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt());
4394   return Ret;
4395 }
4396 
4397 DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) {
4398   assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4399   return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM),
4400                        scalbn(Arg.Floats[1], Exp, RM));
4401 }
4402 
4403 DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp,
4404                     APFloat::roundingMode RM) {
4405   assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics");
4406   APFloat First = frexp(Arg.Floats[0], Exp, RM);
4407   APFloat Second = Arg.Floats[1];
4408   if (Arg.getCategory() == APFloat::fcNormal)
4409     Second = scalbn(Second, -Exp, RM);
4410   return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second));
4411 }
4412 
4413 } // End detail namespace
4414 
4415 APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) {
4416   if (usesLayout<IEEEFloat>(Semantics)) {
4417     new (&IEEE) IEEEFloat(std::move(F));
4418     return;
4419   }
4420   if (usesLayout<DoubleAPFloat>(Semantics)) {
4421     new (&Double)
4422         DoubleAPFloat(Semantics, APFloat(std::move(F), F.getSemantics()),
4423                       APFloat(semIEEEdouble));
4424     return;
4425   }
4426   llvm_unreachable("Unexpected semantics");
4427 }
4428 
4429 APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) {
4430   APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM));
4431 }
4432 
4433 hash_code hash_value(const APFloat &Arg) {
4434   if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics()))
4435     return hash_value(Arg.U.IEEE);
4436   if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics()))
4437     return hash_value(Arg.U.Double);
4438   llvm_unreachable("Unexpected semantics");
4439 }
4440 
4441 APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
4442     : APFloat(Semantics) {
4443   convertFromString(S, rmNearestTiesToEven);
4444 }
4445 
4446 APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics,
4447                                    roundingMode RM, bool *losesInfo) {
4448   if (&getSemantics() == &ToSemantics)
4449     return opOK;
4450   if (usesLayout<IEEEFloat>(getSemantics()) &&
4451       usesLayout<IEEEFloat>(ToSemantics))
4452     return U.IEEE.convert(ToSemantics, RM, losesInfo);
4453   if (usesLayout<IEEEFloat>(getSemantics()) &&
4454       usesLayout<DoubleAPFloat>(ToSemantics)) {
4455     assert(&ToSemantics == &semPPCDoubleDouble);
4456     auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo);
4457     *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
4458     return Ret;
4459   }
4460   if (usesLayout<DoubleAPFloat>(getSemantics()) &&
4461       usesLayout<IEEEFloat>(ToSemantics)) {
4462     auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo);
4463     *this = APFloat(std::move(getIEEE()), ToSemantics);
4464     return Ret;
4465   }
4466   llvm_unreachable("Unexpected semantics");
4467 }
4468 
4469 APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) {
4470   if (isIEEE) {
4471     switch (BitWidth) {
4472     case 16:
4473       return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth));
4474     case 32:
4475       return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth));
4476     case 64:
4477       return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth));
4478     case 80:
4479       return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth));
4480     case 128:
4481       return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth));
4482     default:
4483       llvm_unreachable("Unknown floating bit width");
4484     }
4485   } else {
4486     assert(BitWidth == 128);
4487     return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
4488   }
4489 }
4490 
4491 void APFloat::print(raw_ostream &OS) const {
4492   SmallVector<char, 16> Buffer;
4493   toString(Buffer);
4494   OS << Buffer << "\n";
4495 }
4496 
4497 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4498 LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); }
4499 #endif
4500 
4501 void APFloat::Profile(FoldingSetNodeID &NID) const {
4502   NID.Add(bitcastToAPInt());
4503 }
4504 
4505 /* Same as convertToInteger(integerPart*, ...), except the result is returned in
4506    an APSInt, whose initial bit-width and signed-ness are used to determine the
4507    precision of the conversion.
4508  */
4509 APFloat::opStatus APFloat::convertToInteger(APSInt &result,
4510                                             roundingMode rounding_mode,
4511                                             bool *isExact) const {
4512   unsigned bitWidth = result.getBitWidth();
4513   SmallVector<uint64_t, 4> parts(result.getNumWords());
4514   opStatus status = convertToInteger(parts.data(), bitWidth, result.isSigned(),
4515                                      rounding_mode, isExact);
4516   // Keeps the original signed-ness.
4517   result = APInt(bitWidth, parts);
4518   return status;
4519 }
4520 
4521 } // End llvm namespace
4522 
4523 #undef APFLOAT_DISPATCH_ON_SEMANTICS
4524