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