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