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