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