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