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