1 //===-- APFloat.cpp - Implement APFloat class -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a class to represent arbitrary precision floating
11 // point values and provide a variety of arithmetic operations on them.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APSInt.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/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
28 /// A macro used to combine two fcCategory enums into one key which can be used
29 /// in a switch statement to classify how the interaction of two APFloat's
30 /// categories affects an operation.
32 /// TODO: If clang source code is ever allowed to use constexpr in its own
33 /// codebase, change this into a static inline function.
34 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
36 /* Assumed in hexadecimal significand parsing, and conversion to
37 hexadecimal strings. */
38 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
39 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
43 /* Represents floating point arithmetic semantics. */
45 /* The largest E such that 2^E is representable; this matches the
46 definition of IEEE 754. */
47 APFloat::ExponentType maxExponent;
49 /* The smallest E such that 2^E is a normalized number; this
50 matches the definition of IEEE 754. */
51 APFloat::ExponentType minExponent;
53 /* Number of bits in the significand. This includes the integer
55 unsigned int precision;
58 const fltSemantics APFloat::IEEEhalf = { 15, -14, 11 };
59 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24 };
60 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53 };
61 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113 };
62 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64 };
63 const fltSemantics APFloat::Bogus = { 0, 0, 0 };
65 /* The PowerPC format consists of two doubles. It does not map cleanly
66 onto the usual format above. It is approximated using twice the
67 mantissa bits. Note that for exponents near the double minimum,
68 we no longer can represent the full 106 mantissa bits, so those
69 will be treated as denormal numbers.
71 FIXME: While this approximation is equivalent to what GCC uses for
72 compile-time arithmetic on PPC double-double numbers, it is not able
73 to represent all possible values held by a PPC double-double number,
74 for example: (long double) 1.0 + (long double) 0x1p-106
75 Should this be replaced by a full emulation of PPC double-double? */
76 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53 };
78 /* A tight upper bound on number of parts required to hold the value
81 power * 815 / (351 * integerPartWidth) + 1
83 However, whilst the result may require only this many parts,
84 because we are multiplying two values to get it, the
85 multiplication may require an extra part with the excess part
86 being zero (consider the trivial case of 1 * 1, tcFullMultiply
87 requires two parts to hold the single-part result). So we add an
88 extra one to guarantee enough space whilst multiplying. */
89 const unsigned int maxExponent = 16383;
90 const unsigned int maxPrecision = 113;
91 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
92 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
93 / (351 * integerPartWidth));
96 /* A bunch of private, handy routines. */
98 static inline unsigned int
99 partCountForBits(unsigned int bits)
101 return ((bits) + integerPartWidth - 1) / integerPartWidth;
104 /* Returns 0U-9U. Return values >= 10U are not digits. */
105 static inline unsigned int
106 decDigitValue(unsigned int c)
111 /* Return the value of a decimal exponent of the form
114 If the exponent overflows, returns a large exponent with the
117 readExponent(StringRef::iterator begin, StringRef::iterator end)
120 unsigned int absExponent;
121 const unsigned int overlargeExponent = 24000; /* FIXME. */
122 StringRef::iterator p = begin;
124 assert(p != end && "Exponent has no digits");
126 isNegative = (*p == '-');
127 if (*p == '-' || *p == '+') {
129 assert(p != end && "Exponent has no digits");
132 absExponent = decDigitValue(*p++);
133 assert(absExponent < 10U && "Invalid character in exponent");
135 for (; p != end; ++p) {
138 value = decDigitValue(*p);
139 assert(value < 10U && "Invalid character in exponent");
141 value += absExponent * 10;
142 if (absExponent >= overlargeExponent) {
143 absExponent = overlargeExponent;
144 p = end; /* outwit assert below */
150 assert(p == end && "Invalid exponent in exponent");
153 return -(int) absExponent;
155 return (int) absExponent;
158 /* This is ugly and needs cleaning up, but I don't immediately see
159 how whilst remaining safe. */
161 totalExponent(StringRef::iterator p, StringRef::iterator end,
162 int exponentAdjustment)
164 int unsignedExponent;
165 bool negative, overflow;
168 assert(p != end && "Exponent has no digits");
170 negative = *p == '-';
171 if (*p == '-' || *p == '+') {
173 assert(p != end && "Exponent has no digits");
176 unsignedExponent = 0;
178 for (; p != end; ++p) {
181 value = decDigitValue(*p);
182 assert(value < 10U && "Invalid character in exponent");
184 unsignedExponent = unsignedExponent * 10 + value;
185 if (unsignedExponent > 32767) {
191 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
195 exponent = unsignedExponent;
197 exponent = -exponent;
198 exponent += exponentAdjustment;
199 if (exponent > 32767 || exponent < -32768)
204 exponent = negative ? -32768: 32767;
209 static StringRef::iterator
210 skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end,
211 StringRef::iterator *dot)
213 StringRef::iterator p = begin;
215 while (*p == '0' && p != end)
221 assert(end - begin != 1 && "Significand has no digits");
223 while (*p == '0' && p != end)
230 /* Given a normal decimal floating point number of the form
234 where the decimal point and exponent are optional, fill out the
235 structure D. Exponent is appropriate if the significand is
236 treated as an integer, and normalizedExponent if the significand
237 is taken to have the decimal point after a single leading
240 If the value is zero, V->firstSigDigit points to a non-digit, and
241 the return exponent is zero.
244 const char *firstSigDigit;
245 const char *lastSigDigit;
247 int normalizedExponent;
251 interpretDecimal(StringRef::iterator begin, StringRef::iterator end,
254 StringRef::iterator dot = end;
255 StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot);
257 D->firstSigDigit = p;
259 D->normalizedExponent = 0;
261 for (; p != end; ++p) {
263 assert(dot == end && "String contains multiple dots");
268 if (decDigitValue(*p) >= 10U)
273 assert((*p == 'e' || *p == 'E') && "Invalid character in significand");
274 assert(p != begin && "Significand has no digits");
275 assert((dot == end || p - begin != 1) && "Significand has no digits");
277 /* p points to the first non-digit in the string */
278 D->exponent = readExponent(p + 1, end);
280 /* Implied decimal point? */
285 /* If number is all zeroes accept any exponent. */
286 if (p != D->firstSigDigit) {
287 /* Drop insignificant trailing zeroes. */
292 while (p != begin && *p == '0');
293 while (p != begin && *p == '.');
296 /* Adjust the exponents for any decimal point. */
297 D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p));
298 D->normalizedExponent = (D->exponent +
299 static_cast<APFloat::ExponentType>((p - D->firstSigDigit)
300 - (dot > D->firstSigDigit && dot < p)));
306 /* Return the trailing fraction of a hexadecimal number.
307 DIGITVALUE is the first hex digit of the fraction, P points to
310 trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end,
311 unsigned int digitValue)
313 unsigned int hexDigit;
315 /* If the first trailing digit isn't 0 or 8 we can work out the
316 fraction immediately. */
318 return lfMoreThanHalf;
319 else if (digitValue < 8 && digitValue > 0)
320 return lfLessThanHalf;
322 // Otherwise we need to find the first non-zero digit.
323 while (p != end && (*p == '0' || *p == '.'))
326 assert(p != end && "Invalid trailing hexadecimal fraction!");
328 hexDigit = hexDigitValue(*p);
330 /* If we ran off the end it is exactly zero or one-half, otherwise
333 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
335 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
338 /* Return the fraction lost were a bignum truncated losing the least
339 significant BITS bits. */
341 lostFractionThroughTruncation(const integerPart *parts,
342 unsigned int partCount,
347 lsb = APInt::tcLSB(parts, partCount);
349 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
351 return lfExactlyZero;
353 return lfExactlyHalf;
354 if (bits <= partCount * integerPartWidth &&
355 APInt::tcExtractBit(parts, bits - 1))
356 return lfMoreThanHalf;
358 return lfLessThanHalf;
361 /* Shift DST right BITS bits noting lost fraction. */
363 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
365 lostFraction lost_fraction;
367 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
369 APInt::tcShiftRight(dst, parts, bits);
371 return lost_fraction;
374 /* Combine the effect of two lost fractions. */
376 combineLostFractions(lostFraction moreSignificant,
377 lostFraction lessSignificant)
379 if (lessSignificant != lfExactlyZero) {
380 if (moreSignificant == lfExactlyZero)
381 moreSignificant = lfLessThanHalf;
382 else if (moreSignificant == lfExactlyHalf)
383 moreSignificant = lfMoreThanHalf;
386 return moreSignificant;
389 /* The error from the true value, in half-ulps, on multiplying two
390 floating point numbers, which differ from the value they
391 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
392 than the returned value.
394 See "How to Read Floating Point Numbers Accurately" by William D
397 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
399 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
401 if (HUerr1 + HUerr2 == 0)
402 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
404 return inexactMultiply + 2 * (HUerr1 + HUerr2);
407 /* The number of ulps from the boundary (zero, or half if ISNEAREST)
408 when the least significant BITS are truncated. BITS cannot be
411 ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
413 unsigned int count, partBits;
414 integerPart part, boundary;
419 count = bits / integerPartWidth;
420 partBits = bits % integerPartWidth + 1;
422 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
425 boundary = (integerPart) 1 << (partBits - 1);
430 if (part - boundary <= boundary - part)
431 return part - boundary;
433 return boundary - part;
436 if (part == boundary) {
439 return ~(integerPart) 0; /* A lot. */
442 } else if (part == boundary - 1) {
445 return ~(integerPart) 0; /* A lot. */
450 return ~(integerPart) 0; /* A lot. */
453 /* Place pow(5, power) in DST, and return the number of parts used.
454 DST must be at least one part larger than size of the answer. */
456 powerOf5(integerPart *dst, unsigned int power)
458 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
460 integerPart pow5s[maxPowerOfFiveParts * 2 + 5];
461 pow5s[0] = 78125 * 5;
463 unsigned int partsCount[16] = { 1 };
464 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
466 assert(power <= maxExponent);
471 *p1 = firstEightPowers[power & 7];
477 for (unsigned int n = 0; power; power >>= 1, n++) {
482 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
484 pc = partsCount[n - 1];
485 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
487 if (pow5[pc - 1] == 0)
495 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
497 if (p2[result - 1] == 0)
500 /* Now result is in p1 with partsCount parts and p2 is scratch
502 tmp = p1, p1 = p2, p2 = tmp;
509 APInt::tcAssign(dst, p1, result);
514 /* Zero at the end to avoid modular arithmetic when adding one; used
515 when rounding up during hexadecimal output. */
516 static const char hexDigitsLower[] = "0123456789abcdef0";
517 static const char hexDigitsUpper[] = "0123456789ABCDEF0";
518 static const char infinityL[] = "infinity";
519 static const char infinityU[] = "INFINITY";
520 static const char NaNL[] = "nan";
521 static const char NaNU[] = "NAN";
523 /* Write out an integerPart in hexadecimal, starting with the most
524 significant nibble. Write out exactly COUNT hexdigits, return
527 partAsHex (char *dst, integerPart part, unsigned int count,
528 const char *hexDigitChars)
530 unsigned int result = count;
532 assert(count != 0 && count <= integerPartWidth / 4);
534 part >>= (integerPartWidth - 4 * count);
536 dst[count] = hexDigitChars[part & 0xf];
543 /* Write out an unsigned decimal integer. */
545 writeUnsignedDecimal (char *dst, unsigned int n)
561 /* Write out a signed decimal integer. */
563 writeSignedDecimal (char *dst, int value)
567 dst = writeUnsignedDecimal(dst, -(unsigned) value);
569 dst = writeUnsignedDecimal(dst, value);
576 APFloat::initialize(const fltSemantics *ourSemantics)
580 semantics = ourSemantics;
583 significand.parts = new integerPart[count];
587 APFloat::freeSignificand()
590 delete [] significand.parts;
594 APFloat::assign(const APFloat &rhs)
596 assert(semantics == rhs.semantics);
599 category = rhs.category;
600 exponent = rhs.exponent;
601 if (isFiniteNonZero() || category == fcNaN)
602 copySignificand(rhs);
606 APFloat::copySignificand(const APFloat &rhs)
608 assert(isFiniteNonZero() || category == fcNaN);
609 assert(rhs.partCount() >= partCount());
611 APInt::tcAssign(significandParts(), rhs.significandParts(),
615 /* Make this number a NaN, with an arbitrary but deterministic value
616 for the significand. If double or longer, this is a signalling NaN,
617 which may not be ideal. If float, this is QNaN(0). */
618 void APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill)
623 integerPart *significand = significandParts();
624 unsigned numParts = partCount();
626 // Set the significand bits to the fill.
627 if (!fill || fill->getNumWords() < numParts)
628 APInt::tcSet(significand, 0, numParts);
630 APInt::tcAssign(significand, fill->getRawData(),
631 std::min(fill->getNumWords(), numParts));
633 // Zero out the excess bits of the significand.
634 unsigned bitsToPreserve = semantics->precision - 1;
635 unsigned part = bitsToPreserve / 64;
636 bitsToPreserve %= 64;
637 significand[part] &= ((1ULL << bitsToPreserve) - 1);
638 for (part++; part != numParts; ++part)
639 significand[part] = 0;
642 unsigned QNaNBit = semantics->precision - 2;
645 // We always have to clear the QNaN bit to make it an SNaN.
646 APInt::tcClearBit(significand, QNaNBit);
648 // If there are no bits set in the payload, we have to set
649 // *something* to make it a NaN instead of an infinity;
650 // conventionally, this is the next bit down from the QNaN bit.
651 if (APInt::tcIsZero(significand, numParts))
652 APInt::tcSetBit(significand, QNaNBit - 1);
654 // We always have to set the QNaN bit to make it a QNaN.
655 APInt::tcSetBit(significand, QNaNBit);
658 // For x87 extended precision, we want to make a NaN, not a
659 // pseudo-NaN. Maybe we should expose the ability to make
661 if (semantics == &APFloat::x87DoubleExtended)
662 APInt::tcSetBit(significand, QNaNBit + 1);
665 APFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative,
667 APFloat value(Sem, uninitialized);
668 value.makeNaN(SNaN, Negative, fill);
673 APFloat::operator=(const APFloat &rhs)
676 if (semantics != rhs.semantics) {
678 initialize(rhs.semantics);
687 APFloat::operator=(APFloat &&rhs) {
690 semantics = rhs.semantics;
691 significand = rhs.significand;
692 exponent = rhs.exponent;
693 category = rhs.category;
696 rhs.semantics = &Bogus;
701 APFloat::isDenormal() const {
702 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
703 (APInt::tcExtractBit(significandParts(),
704 semantics->precision - 1) == 0);
708 APFloat::isSmallest() const {
709 // The smallest number by magnitude in our format will be the smallest
710 // denormal, i.e. the floating point number with exponent being minimum
711 // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0).
712 return isFiniteNonZero() && exponent == semantics->minExponent &&
713 significandMSB() == 0;
716 bool APFloat::isSignificandAllOnes() const {
717 // Test if the significand excluding the integral bit is all ones. This allows
718 // us to test for binade boundaries.
719 const integerPart *Parts = significandParts();
720 const unsigned PartCount = partCount();
721 for (unsigned i = 0; i < PartCount - 1; i++)
725 // Set the unused high bits to all ones when we compare.
726 const unsigned NumHighBits =
727 PartCount*integerPartWidth - semantics->precision + 1;
728 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
729 "fill than integerPartWidth");
730 const integerPart HighBitFill =
731 ~integerPart(0) << (integerPartWidth - NumHighBits);
732 if (~(Parts[PartCount - 1] | HighBitFill))
738 bool APFloat::isSignificandAllZeros() const {
739 // Test if the significand excluding the integral bit is all zeros. This
740 // allows us to test for binade boundaries.
741 const integerPart *Parts = significandParts();
742 const unsigned PartCount = partCount();
744 for (unsigned i = 0; i < PartCount - 1; i++)
748 const unsigned NumHighBits =
749 PartCount*integerPartWidth - semantics->precision + 1;
750 assert(NumHighBits <= integerPartWidth && "Can not have more high bits to "
751 "clear than integerPartWidth");
752 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
754 if (Parts[PartCount - 1] & HighBitMask)
761 APFloat::isLargest() const {
762 // The largest number by magnitude in our format will be the floating point
763 // number with maximum exponent and with significand that is all ones.
764 return isFiniteNonZero() && exponent == semantics->maxExponent
765 && isSignificandAllOnes();
769 APFloat::bitwiseIsEqual(const APFloat &rhs) const {
772 if (semantics != rhs.semantics ||
773 category != rhs.category ||
776 if (category==fcZero || category==fcInfinity)
778 else if (isFiniteNonZero() && exponent!=rhs.exponent)
782 const integerPart* p=significandParts();
783 const integerPart* q=rhs.significandParts();
784 for (; i>0; i--, p++, q++) {
792 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) {
793 initialize(&ourSemantics);
797 exponent = ourSemantics.precision - 1;
798 significandParts()[0] = value;
799 normalize(rmNearestTiesToEven, lfExactlyZero);
802 APFloat::APFloat(const fltSemantics &ourSemantics) {
803 initialize(&ourSemantics);
808 APFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
809 // Allocates storage if necessary but does not initialize it.
810 initialize(&ourSemantics);
813 APFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) {
814 initialize(&ourSemantics);
815 convertFromString(text, rmNearestTiesToEven);
818 APFloat::APFloat(const APFloat &rhs) {
819 initialize(rhs.semantics);
823 APFloat::APFloat(APFloat &&rhs) : semantics(&Bogus) {
824 *this = std::move(rhs);
832 // Profile - This method 'profiles' an APFloat for use with FoldingSet.
833 void APFloat::Profile(FoldingSetNodeID& ID) const {
834 ID.Add(bitcastToAPInt());
838 APFloat::partCount() const
840 return partCountForBits(semantics->precision + 1);
844 APFloat::semanticsPrecision(const fltSemantics &semantics)
846 return semantics.precision;
850 APFloat::significandParts() const
852 return const_cast<APFloat *>(this)->significandParts();
856 APFloat::significandParts()
859 return significand.parts;
861 return &significand.part;
865 APFloat::zeroSignificand()
867 APInt::tcSet(significandParts(), 0, partCount());
870 /* Increment an fcNormal floating point number's significand. */
872 APFloat::incrementSignificand()
876 carry = APInt::tcIncrement(significandParts(), partCount());
878 /* Our callers should never cause us to overflow. */
883 /* Add the significand of the RHS. Returns the carry flag. */
885 APFloat::addSignificand(const APFloat &rhs)
889 parts = significandParts();
891 assert(semantics == rhs.semantics);
892 assert(exponent == rhs.exponent);
894 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
897 /* Subtract the significand of the RHS with a borrow flag. Returns
900 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
904 parts = significandParts();
906 assert(semantics == rhs.semantics);
907 assert(exponent == rhs.exponent);
909 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
913 /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
914 on to the full-precision result of the multiplication. Returns the
917 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
919 unsigned int omsb; // One, not zero, based MSB.
920 unsigned int partsCount, newPartsCount, precision;
921 integerPart *lhsSignificand;
922 integerPart scratch[4];
923 integerPart *fullSignificand;
924 lostFraction lost_fraction;
927 assert(semantics == rhs.semantics);
929 precision = semantics->precision;
930 newPartsCount = partCountForBits(precision * 2);
932 if (newPartsCount > 4)
933 fullSignificand = new integerPart[newPartsCount];
935 fullSignificand = scratch;
937 lhsSignificand = significandParts();
938 partsCount = partCount();
940 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
941 rhs.significandParts(), partsCount, partsCount);
943 lost_fraction = lfExactlyZero;
944 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
945 exponent += rhs.exponent;
947 // Assume the operands involved in the multiplication are single-precision
948 // FP, and the two multiplicants are:
949 // *this = a23 . a22 ... a0 * 2^e1
950 // rhs = b23 . b22 ... b0 * 2^e2
951 // the result of multiplication is:
952 // *this = c47 c46 . c45 ... c0 * 2^(e1+e2)
953 // Note that there are two significant bits at the left-hand side of the
954 // radix point. Move the radix point toward left by one bit, and adjust
955 // exponent accordingly.
959 // The intermediate result of the multiplication has "2 * precision"
960 // signicant bit; adjust the addend to be consistent with mul result.
962 Significand savedSignificand = significand;
963 const fltSemantics *savedSemantics = semantics;
964 fltSemantics extendedSemantics;
966 unsigned int extendedPrecision;
968 /* Normalize our MSB. */
969 extendedPrecision = 2 * precision;
970 if (omsb != extendedPrecision) {
971 assert(extendedPrecision > omsb);
972 APInt::tcShiftLeft(fullSignificand, newPartsCount,
973 extendedPrecision - omsb);
974 exponent -= extendedPrecision - omsb;
977 /* Create new semantics. */
978 extendedSemantics = *semantics;
979 extendedSemantics.precision = extendedPrecision;
981 if (newPartsCount == 1)
982 significand.part = fullSignificand[0];
984 significand.parts = fullSignificand;
985 semantics = &extendedSemantics;
987 APFloat extendedAddend(*addend);
988 status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored);
989 assert(status == opOK);
991 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
993 /* Restore our state. */
994 if (newPartsCount == 1)
995 fullSignificand[0] = significand.part;
996 significand = savedSignificand;
997 semantics = savedSemantics;
999 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1002 // Convert the result having "2 * precision" significant-bits back to the one
1003 // having "precision" significant-bits. First, move the radix point from
1004 // poision "2*precision - 1" to "precision - 1". The exponent need to be
1005 // adjusted by "2*precision - 1" - "precision - 1" = "precision".
1006 exponent -= precision;
1008 // In case MSB resides at the left-hand side of radix point, shift the
1009 // mantissa right by some amount to make sure the MSB reside right before
1010 // the radix point (i.e. "MSB . rest-significant-bits").
1012 // Note that the result is not normalized when "omsb < precision". So, the
1013 // caller needs to call APFloat::normalize() if normalized value is expected.
1014 if (omsb > precision) {
1015 unsigned int bits, significantParts;
1018 bits = omsb - precision;
1019 significantParts = partCountForBits(omsb);
1020 lf = shiftRight(fullSignificand, significantParts, bits);
1021 lost_fraction = combineLostFractions(lf, lost_fraction);
1025 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
1027 if (newPartsCount > 4)
1028 delete [] fullSignificand;
1030 return lost_fraction;
1033 /* Multiply the significands of LHS and RHS to DST. */
1035 APFloat::divideSignificand(const APFloat &rhs)
1037 unsigned int bit, i, partsCount;
1038 const integerPart *rhsSignificand;
1039 integerPart *lhsSignificand, *dividend, *divisor;
1040 integerPart scratch[4];
1041 lostFraction lost_fraction;
1043 assert(semantics == rhs.semantics);
1045 lhsSignificand = significandParts();
1046 rhsSignificand = rhs.significandParts();
1047 partsCount = partCount();
1050 dividend = new integerPart[partsCount * 2];
1054 divisor = dividend + partsCount;
1056 /* Copy the dividend and divisor as they will be modified in-place. */
1057 for (i = 0; i < partsCount; i++) {
1058 dividend[i] = lhsSignificand[i];
1059 divisor[i] = rhsSignificand[i];
1060 lhsSignificand[i] = 0;
1063 exponent -= rhs.exponent;
1065 unsigned int precision = semantics->precision;
1067 /* Normalize the divisor. */
1068 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
1071 APInt::tcShiftLeft(divisor, partsCount, bit);
1074 /* Normalize the dividend. */
1075 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
1078 APInt::tcShiftLeft(dividend, partsCount, bit);
1081 /* Ensure the dividend >= divisor initially for the loop below.
1082 Incidentally, this means that the division loop below is
1083 guaranteed to set the integer bit to one. */
1084 if (APInt::tcCompare(dividend, divisor, partsCount) < 0) {
1086 APInt::tcShiftLeft(dividend, partsCount, 1);
1087 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
1090 /* Long division. */
1091 for (bit = precision; bit; bit -= 1) {
1092 if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
1093 APInt::tcSubtract(dividend, divisor, 0, partsCount);
1094 APInt::tcSetBit(lhsSignificand, bit - 1);
1097 APInt::tcShiftLeft(dividend, partsCount, 1);
1100 /* Figure out the lost fraction. */
1101 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
1104 lost_fraction = lfMoreThanHalf;
1106 lost_fraction = lfExactlyHalf;
1107 else if (APInt::tcIsZero(dividend, partsCount))
1108 lost_fraction = lfExactlyZero;
1110 lost_fraction = lfLessThanHalf;
1115 return lost_fraction;
1119 APFloat::significandMSB() const
1121 return APInt::tcMSB(significandParts(), partCount());
1125 APFloat::significandLSB() const
1127 return APInt::tcLSB(significandParts(), partCount());
1130 /* Note that a zero result is NOT normalized to fcZero. */
1132 APFloat::shiftSignificandRight(unsigned int bits)
1134 /* Our exponent should not overflow. */
1135 assert((ExponentType) (exponent + bits) >= exponent);
1139 return shiftRight(significandParts(), partCount(), bits);
1142 /* Shift the significand left BITS bits, subtract BITS from its exponent. */
1144 APFloat::shiftSignificandLeft(unsigned int bits)
1146 assert(bits < semantics->precision);
1149 unsigned int partsCount = partCount();
1151 APInt::tcShiftLeft(significandParts(), partsCount, bits);
1154 assert(!APInt::tcIsZero(significandParts(), partsCount));
1159 APFloat::compareAbsoluteValue(const APFloat &rhs) const
1163 assert(semantics == rhs.semantics);
1164 assert(isFiniteNonZero());
1165 assert(rhs.isFiniteNonZero());
1167 compare = exponent - rhs.exponent;
1169 /* If exponents are equal, do an unsigned bignum comparison of the
1172 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
1176 return cmpGreaterThan;
1177 else if (compare < 0)
1183 /* Handle overflow. Sign is preserved. We either become infinity or
1184 the largest finite number. */
1186 APFloat::handleOverflow(roundingMode rounding_mode)
1189 if (rounding_mode == rmNearestTiesToEven ||
1190 rounding_mode == rmNearestTiesToAway ||
1191 (rounding_mode == rmTowardPositive && !sign) ||
1192 (rounding_mode == rmTowardNegative && sign)) {
1193 category = fcInfinity;
1194 return (opStatus) (opOverflow | opInexact);
1197 /* Otherwise we become the largest finite number. */
1198 category = fcNormal;
1199 exponent = semantics->maxExponent;
1200 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
1201 semantics->precision);
1206 /* Returns TRUE if, when truncating the current number, with BIT the
1207 new LSB, with the given lost fraction and rounding mode, the result
1208 would need to be rounded away from zero (i.e., by increasing the
1209 signficand). This routine must work for fcZero of both signs, and
1210 fcNormal numbers. */
1212 APFloat::roundAwayFromZero(roundingMode rounding_mode,
1213 lostFraction lost_fraction,
1214 unsigned int bit) const
1216 /* NaNs and infinities should not have lost fractions. */
1217 assert(isFiniteNonZero() || category == fcZero);
1219 /* Current callers never pass this so we don't handle it. */
1220 assert(lost_fraction != lfExactlyZero);
1222 switch (rounding_mode) {
1223 case rmNearestTiesToAway:
1224 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1226 case rmNearestTiesToEven:
1227 if (lost_fraction == lfMoreThanHalf)
1230 /* Our zeroes don't have a significand to test. */
1231 if (lost_fraction == lfExactlyHalf && category != fcZero)
1232 return APInt::tcExtractBit(significandParts(), bit);
1239 case rmTowardPositive:
1240 return sign == false;
1242 case rmTowardNegative:
1243 return sign == true;
1245 llvm_unreachable("Invalid rounding mode found");
1249 APFloat::normalize(roundingMode rounding_mode,
1250 lostFraction lost_fraction)
1252 unsigned int omsb; /* One, not zero, based MSB. */
1255 if (!isFiniteNonZero())
1258 /* Before rounding normalize the exponent of fcNormal numbers. */
1259 omsb = significandMSB() + 1;
1262 /* OMSB is numbered from 1. We want to place it in the integer
1263 bit numbered PRECISION if possible, with a compensating change in
1265 exponentChange = omsb - semantics->precision;
1267 /* If the resulting exponent is too high, overflow according to
1268 the rounding mode. */
1269 if (exponent + exponentChange > semantics->maxExponent)
1270 return handleOverflow(rounding_mode);
1272 /* Subnormal numbers have exponent minExponent, and their MSB
1273 is forced based on that. */
1274 if (exponent + exponentChange < semantics->minExponent)
1275 exponentChange = semantics->minExponent - exponent;
1277 /* Shifting left is easy as we don't lose precision. */
1278 if (exponentChange < 0) {
1279 assert(lost_fraction == lfExactlyZero);
1281 shiftSignificandLeft(-exponentChange);
1286 if (exponentChange > 0) {
1289 /* Shift right and capture any new lost fraction. */
1290 lf = shiftSignificandRight(exponentChange);
1292 lost_fraction = combineLostFractions(lf, lost_fraction);
1294 /* Keep OMSB up-to-date. */
1295 if (omsb > (unsigned) exponentChange)
1296 omsb -= exponentChange;
1302 /* Now round the number according to rounding_mode given the lost
1305 /* As specified in IEEE 754, since we do not trap we do not report
1306 underflow for exact results. */
1307 if (lost_fraction == lfExactlyZero) {
1308 /* Canonicalize zeroes. */
1315 /* Increment the significand if we're rounding away from zero. */
1316 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1318 exponent = semantics->minExponent;
1320 incrementSignificand();
1321 omsb = significandMSB() + 1;
1323 /* Did the significand increment overflow? */
1324 if (omsb == (unsigned) semantics->precision + 1) {
1325 /* Renormalize by incrementing the exponent and shifting our
1326 significand right one. However if we already have the
1327 maximum exponent we overflow to infinity. */
1328 if (exponent == semantics->maxExponent) {
1329 category = fcInfinity;
1331 return (opStatus) (opOverflow | opInexact);
1334 shiftSignificandRight(1);
1340 /* The normal case - we were and are not denormal, and any
1341 significand increment above didn't overflow. */
1342 if (omsb == semantics->precision)
1345 /* We have a non-zero denormal. */
1346 assert(omsb < semantics->precision);
1348 /* Canonicalize zeroes. */
1352 /* The fcZero case is a denormal that underflowed to zero. */
1353 return (opStatus) (opUnderflow | opInexact);
1357 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1359 switch (PackCategoriesIntoKey(category, rhs.category)) {
1361 llvm_unreachable(nullptr);
1363 case PackCategoriesIntoKey(fcNaN, fcZero):
1364 case PackCategoriesIntoKey(fcNaN, fcNormal):
1365 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1366 case PackCategoriesIntoKey(fcNaN, fcNaN):
1367 case PackCategoriesIntoKey(fcNormal, fcZero):
1368 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1369 case PackCategoriesIntoKey(fcInfinity, fcZero):
1372 case PackCategoriesIntoKey(fcZero, fcNaN):
1373 case PackCategoriesIntoKey(fcNormal, fcNaN):
1374 case PackCategoriesIntoKey(fcInfinity, fcNaN):
1375 // We need to be sure to flip the sign here for subtraction because we
1376 // don't have a separate negate operation so -NaN becomes 0 - NaN here.
1377 sign = rhs.sign ^ subtract;
1379 copySignificand(rhs);
1382 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1383 case PackCategoriesIntoKey(fcZero, fcInfinity):
1384 category = fcInfinity;
1385 sign = rhs.sign ^ subtract;
1388 case PackCategoriesIntoKey(fcZero, fcNormal):
1390 sign = rhs.sign ^ subtract;
1393 case PackCategoriesIntoKey(fcZero, fcZero):
1394 /* Sign depends on rounding mode; handled by caller. */
1397 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1398 /* Differently signed infinities can only be validly
1400 if (((sign ^ rhs.sign)!=0) != subtract) {
1407 case PackCategoriesIntoKey(fcNormal, fcNormal):
1412 /* Add or subtract two normal numbers. */
1414 APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1417 lostFraction lost_fraction;
1420 /* Determine if the operation on the absolute values is effectively
1421 an addition or subtraction. */
1422 subtract ^= (sign ^ rhs.sign) ? true : false;
1424 /* Are we bigger exponent-wise than the RHS? */
1425 bits = exponent - rhs.exponent;
1427 /* Subtraction is more subtle than one might naively expect. */
1429 APFloat temp_rhs(rhs);
1433 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1434 lost_fraction = lfExactlyZero;
1435 } else if (bits > 0) {
1436 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1437 shiftSignificandLeft(1);
1440 lost_fraction = shiftSignificandRight(-bits - 1);
1441 temp_rhs.shiftSignificandLeft(1);
1446 carry = temp_rhs.subtractSignificand
1447 (*this, lost_fraction != lfExactlyZero);
1448 copySignificand(temp_rhs);
1451 carry = subtractSignificand
1452 (temp_rhs, lost_fraction != lfExactlyZero);
1455 /* Invert the lost fraction - it was on the RHS and
1457 if (lost_fraction == lfLessThanHalf)
1458 lost_fraction = lfMoreThanHalf;
1459 else if (lost_fraction == lfMoreThanHalf)
1460 lost_fraction = lfLessThanHalf;
1462 /* The code above is intended to ensure that no borrow is
1468 APFloat temp_rhs(rhs);
1470 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1471 carry = addSignificand(temp_rhs);
1473 lost_fraction = shiftSignificandRight(-bits);
1474 carry = addSignificand(rhs);
1477 /* We have a guard bit; generating a carry cannot happen. */
1482 return lost_fraction;
1486 APFloat::multiplySpecials(const APFloat &rhs)
1488 switch (PackCategoriesIntoKey(category, rhs.category)) {
1490 llvm_unreachable(nullptr);
1492 case PackCategoriesIntoKey(fcNaN, fcZero):
1493 case PackCategoriesIntoKey(fcNaN, fcNormal):
1494 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1495 case PackCategoriesIntoKey(fcNaN, fcNaN):
1499 case PackCategoriesIntoKey(fcZero, fcNaN):
1500 case PackCategoriesIntoKey(fcNormal, fcNaN):
1501 case PackCategoriesIntoKey(fcInfinity, fcNaN):
1504 copySignificand(rhs);
1507 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1508 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1509 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1510 category = fcInfinity;
1513 case PackCategoriesIntoKey(fcZero, fcNormal):
1514 case PackCategoriesIntoKey(fcNormal, fcZero):
1515 case PackCategoriesIntoKey(fcZero, fcZero):
1519 case PackCategoriesIntoKey(fcZero, fcInfinity):
1520 case PackCategoriesIntoKey(fcInfinity, fcZero):
1524 case PackCategoriesIntoKey(fcNormal, fcNormal):
1530 APFloat::divideSpecials(const APFloat &rhs)
1532 switch (PackCategoriesIntoKey(category, rhs.category)) {
1534 llvm_unreachable(nullptr);
1536 case PackCategoriesIntoKey(fcZero, fcNaN):
1537 case PackCategoriesIntoKey(fcNormal, fcNaN):
1538 case PackCategoriesIntoKey(fcInfinity, fcNaN):
1540 copySignificand(rhs);
1541 case PackCategoriesIntoKey(fcNaN, fcZero):
1542 case PackCategoriesIntoKey(fcNaN, fcNormal):
1543 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1544 case PackCategoriesIntoKey(fcNaN, fcNaN):
1546 case PackCategoriesIntoKey(fcInfinity, fcZero):
1547 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1548 case PackCategoriesIntoKey(fcZero, fcInfinity):
1549 case PackCategoriesIntoKey(fcZero, fcNormal):
1552 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1556 case PackCategoriesIntoKey(fcNormal, fcZero):
1557 category = fcInfinity;
1560 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1561 case PackCategoriesIntoKey(fcZero, fcZero):
1565 case PackCategoriesIntoKey(fcNormal, fcNormal):
1571 APFloat::modSpecials(const APFloat &rhs)
1573 switch (PackCategoriesIntoKey(category, rhs.category)) {
1575 llvm_unreachable(nullptr);
1577 case PackCategoriesIntoKey(fcNaN, fcZero):
1578 case PackCategoriesIntoKey(fcNaN, fcNormal):
1579 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1580 case PackCategoriesIntoKey(fcNaN, fcNaN):
1581 case PackCategoriesIntoKey(fcZero, fcInfinity):
1582 case PackCategoriesIntoKey(fcZero, fcNormal):
1583 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1586 case PackCategoriesIntoKey(fcZero, fcNaN):
1587 case PackCategoriesIntoKey(fcNormal, fcNaN):
1588 case PackCategoriesIntoKey(fcInfinity, fcNaN):
1591 copySignificand(rhs);
1594 case PackCategoriesIntoKey(fcNormal, fcZero):
1595 case PackCategoriesIntoKey(fcInfinity, fcZero):
1596 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1597 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1598 case PackCategoriesIntoKey(fcZero, fcZero):
1602 case PackCategoriesIntoKey(fcNormal, fcNormal):
1609 APFloat::changeSign()
1611 /* Look mummy, this one's easy. */
1616 APFloat::clearSign()
1618 /* So is this one. */
1623 APFloat::copySign(const APFloat &rhs)
1629 /* Normalized addition or subtraction. */
1631 APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
1636 fs = addOrSubtractSpecials(rhs, subtract);
1638 /* This return code means it was not a simple case. */
1639 if (fs == opDivByZero) {
1640 lostFraction lost_fraction;
1642 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1643 fs = normalize(rounding_mode, lost_fraction);
1645 /* Can only be zero if we lost no fraction. */
1646 assert(category != fcZero || lost_fraction == lfExactlyZero);
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);
1660 /* Normalized addition. */
1662 APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1664 return addOrSubtract(rhs, rounding_mode, false);
1667 /* Normalized subtraction. */
1669 APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1671 return addOrSubtract(rhs, rounding_mode, true);
1674 /* Normalized multiply. */
1676 APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1681 fs = multiplySpecials(rhs);
1683 if (isFiniteNonZero()) {
1684 lostFraction lost_fraction = multiplySignificand(rhs, nullptr);
1685 fs = normalize(rounding_mode, lost_fraction);
1686 if (lost_fraction != lfExactlyZero)
1687 fs = (opStatus) (fs | opInexact);
1693 /* Normalized divide. */
1695 APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1700 fs = divideSpecials(rhs);
1702 if (isFiniteNonZero()) {
1703 lostFraction lost_fraction = divideSignificand(rhs);
1704 fs = normalize(rounding_mode, lost_fraction);
1705 if (lost_fraction != lfExactlyZero)
1706 fs = (opStatus) (fs | opInexact);
1712 /* Normalized remainder. This is not currently correct in all cases. */
1714 APFloat::remainder(const APFloat &rhs)
1718 unsigned int origSign = sign;
1720 fs = V.divide(rhs, rmNearestTiesToEven);
1721 if (fs == opDivByZero)
1724 int parts = partCount();
1725 integerPart *x = new integerPart[parts];
1727 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1728 rmNearestTiesToEven, &ignored);
1729 if (fs==opInvalidOp)
1732 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1733 rmNearestTiesToEven);
1734 assert(fs==opOK); // should always work
1736 fs = V.multiply(rhs, rmNearestTiesToEven);
1737 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1739 fs = subtract(V, rmNearestTiesToEven);
1740 assert(fs==opOK || fs==opInexact); // likewise
1743 sign = origSign; // IEEE754 requires this
1748 /* Normalized llvm frem (C fmod).
1749 This is not currently correct in all cases. */
1751 APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1754 fs = modSpecials(rhs);
1756 if (isFiniteNonZero() && rhs.isFiniteNonZero()) {
1758 unsigned int origSign = sign;
1760 fs = V.divide(rhs, rmNearestTiesToEven);
1761 if (fs == opDivByZero)
1764 int parts = partCount();
1765 integerPart *x = new integerPart[parts];
1767 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1768 rmTowardZero, &ignored);
1769 if (fs==opInvalidOp)
1772 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1773 rmNearestTiesToEven);
1774 assert(fs==opOK); // should always work
1776 fs = V.multiply(rhs, rounding_mode);
1777 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1779 fs = subtract(V, rounding_mode);
1780 assert(fs==opOK || fs==opInexact); // likewise
1783 sign = origSign; // IEEE754 requires this
1789 /* Normalized fused-multiply-add. */
1791 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
1792 const APFloat &addend,
1793 roundingMode rounding_mode)
1797 /* Post-multiplication sign, before addition. */
1798 sign ^= multiplicand.sign;
1800 /* If and only if all arguments are normal do we need to do an
1801 extended-precision calculation. */
1802 if (isFiniteNonZero() &&
1803 multiplicand.isFiniteNonZero() &&
1804 addend.isFiniteNonZero()) {
1805 lostFraction lost_fraction;
1807 lost_fraction = multiplySignificand(multiplicand, &addend);
1808 fs = normalize(rounding_mode, lost_fraction);
1809 if (lost_fraction != lfExactlyZero)
1810 fs = (opStatus) (fs | opInexact);
1812 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1813 positive zero unless rounding to minus infinity, except that
1814 adding two like-signed zeroes gives that zero. */
1815 if (category == fcZero && sign != addend.sign)
1816 sign = (rounding_mode == rmTowardNegative);
1818 fs = multiplySpecials(multiplicand);
1820 /* FS can only be opOK or opInvalidOp. There is no more work
1821 to do in the latter case. The IEEE-754R standard says it is
1822 implementation-defined in this case whether, if ADDEND is a
1823 quiet NaN, we raise invalid op; this implementation does so.
1825 If we need to do the addition we can do so with normal
1828 fs = addOrSubtract(addend, rounding_mode, false);
1834 /* Rounding-mode corrrect round to integral value. */
1835 APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
1838 // If the exponent is large enough, we know that this value is already
1839 // integral, and the arithmetic below would potentially cause it to saturate
1840 // to +/-Inf. Bail out early instead.
1841 if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics))
1844 // The algorithm here is quite simple: we add 2^(p-1), where p is the
1845 // precision of our format, and then subtract it back off again. The choice
1846 // of rounding modes for the addition/subtraction determines the rounding mode
1847 // for our integral rounding as well.
1848 // NOTE: When the input value is negative, we do subtraction followed by
1849 // addition instead.
1850 APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
1851 IntegerConstant <<= semanticsPrecision(*semantics)-1;
1852 APFloat MagicConstant(*semantics);
1853 fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
1854 rmNearestTiesToEven);
1855 MagicConstant.copySign(*this);
1860 // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
1861 bool inputSign = isNegative();
1863 fs = add(MagicConstant, rounding_mode);
1864 if (fs != opOK && fs != opInexact)
1867 fs = subtract(MagicConstant, rounding_mode);
1869 // Restore the input sign.
1870 if (inputSign != isNegative())
1877 /* Comparison requires normalized numbers. */
1879 APFloat::compare(const APFloat &rhs) const
1883 assert(semantics == rhs.semantics);
1885 switch (PackCategoriesIntoKey(category, rhs.category)) {
1887 llvm_unreachable(nullptr);
1889 case PackCategoriesIntoKey(fcNaN, fcZero):
1890 case PackCategoriesIntoKey(fcNaN, fcNormal):
1891 case PackCategoriesIntoKey(fcNaN, fcInfinity):
1892 case PackCategoriesIntoKey(fcNaN, fcNaN):
1893 case PackCategoriesIntoKey(fcZero, fcNaN):
1894 case PackCategoriesIntoKey(fcNormal, fcNaN):
1895 case PackCategoriesIntoKey(fcInfinity, fcNaN):
1896 return cmpUnordered;
1898 case PackCategoriesIntoKey(fcInfinity, fcNormal):
1899 case PackCategoriesIntoKey(fcInfinity, fcZero):
1900 case PackCategoriesIntoKey(fcNormal, fcZero):
1904 return cmpGreaterThan;
1906 case PackCategoriesIntoKey(fcNormal, fcInfinity):
1907 case PackCategoriesIntoKey(fcZero, fcInfinity):
1908 case PackCategoriesIntoKey(fcZero, fcNormal):
1910 return cmpGreaterThan;
1914 case PackCategoriesIntoKey(fcInfinity, fcInfinity):
1915 if (sign == rhs.sign)
1920 return cmpGreaterThan;
1922 case PackCategoriesIntoKey(fcZero, fcZero):
1925 case PackCategoriesIntoKey(fcNormal, fcNormal):
1929 /* Two normal numbers. Do they have the same sign? */
1930 if (sign != rhs.sign) {
1932 result = cmpLessThan;
1934 result = cmpGreaterThan;
1936 /* Compare absolute values; invert result if negative. */
1937 result = compareAbsoluteValue(rhs);
1940 if (result == cmpLessThan)
1941 result = cmpGreaterThan;
1942 else if (result == cmpGreaterThan)
1943 result = cmpLessThan;
1950 /// APFloat::convert - convert a value of one floating point type to another.
1951 /// The return value corresponds to the IEEE754 exceptions. *losesInfo
1952 /// records whether the transformation lost information, i.e. whether
1953 /// converting the result back to the original type will produce the
1954 /// original value (this is almost the same as return value==fsOK, but there
1955 /// are edge cases where this is not so).
1958 APFloat::convert(const fltSemantics &toSemantics,
1959 roundingMode rounding_mode, bool *losesInfo)
1961 lostFraction lostFraction;
1962 unsigned int newPartCount, oldPartCount;
1965 const fltSemantics &fromSemantics = *semantics;
1967 lostFraction = lfExactlyZero;
1968 newPartCount = partCountForBits(toSemantics.precision + 1);
1969 oldPartCount = partCount();
1970 shift = toSemantics.precision - fromSemantics.precision;
1972 bool X86SpecialNan = false;
1973 if (&fromSemantics == &APFloat::x87DoubleExtended &&
1974 &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN &&
1975 (!(*significandParts() & 0x8000000000000000ULL) ||
1976 !(*significandParts() & 0x4000000000000000ULL))) {
1977 // x86 has some unusual NaNs which cannot be represented in any other
1978 // format; note them here.
1979 X86SpecialNan = true;
1982 // If this is a truncation of a denormal number, and the target semantics
1983 // has larger exponent range than the source semantics (this can happen
1984 // when truncating from PowerPC double-double to double format), the
1985 // right shift could lose result mantissa bits. Adjust exponent instead
1986 // of performing excessive shift.
1987 if (shift < 0 && isFiniteNonZero()) {
1988 int exponentChange = significandMSB() + 1 - fromSemantics.precision;
1989 if (exponent + exponentChange < toSemantics.minExponent)
1990 exponentChange = toSemantics.minExponent - exponent;
1991 if (exponentChange < shift)
1992 exponentChange = shift;
1993 if (exponentChange < 0) {
1994 shift -= exponentChange;
1995 exponent += exponentChange;
1999 // If this is a truncation, perform the shift before we narrow the storage.
2000 if (shift < 0 && (isFiniteNonZero() || category==fcNaN))
2001 lostFraction = shiftRight(significandParts(), oldPartCount, -shift);
2003 // Fix the storage so it can hold to new value.
2004 if (newPartCount > oldPartCount) {
2005 // The new type requires more storage; make it available.
2006 integerPart *newParts;
2007 newParts = new integerPart[newPartCount];
2008 APInt::tcSet(newParts, 0, newPartCount);
2009 if (isFiniteNonZero() || category==fcNaN)
2010 APInt::tcAssign(newParts, significandParts(), oldPartCount);
2012 significand.parts = newParts;
2013 } else if (newPartCount == 1 && oldPartCount != 1) {
2014 // Switch to built-in storage for a single part.
2015 integerPart newPart = 0;
2016 if (isFiniteNonZero() || category==fcNaN)
2017 newPart = significandParts()[0];
2019 significand.part = newPart;
2022 // Now that we have the right storage, switch the semantics.
2023 semantics = &toSemantics;
2025 // If this is an extension, perform the shift now that the storage is
2027 if (shift > 0 && (isFiniteNonZero() || category==fcNaN))
2028 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
2030 if (isFiniteNonZero()) {
2031 fs = normalize(rounding_mode, lostFraction);
2032 *losesInfo = (fs != opOK);
2033 } else if (category == fcNaN) {
2034 *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan;
2036 // For x87 extended precision, we want to make a NaN, not a special NaN if
2037 // the input wasn't special either.
2038 if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended)
2039 APInt::tcSetBit(significandParts(), semantics->precision - 1);
2041 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
2042 // does not give you back the same bits. This is dubious, and we
2043 // don't currently do it. You're really supposed to get
2044 // an invalid operation signal at runtime, but nobody does that.
2054 /* Convert a floating point number to an integer according to the
2055 rounding mode. If the rounded integer value is out of range this
2056 returns an invalid operation exception and the contents of the
2057 destination parts are unspecified. If the rounded value is in
2058 range but the floating point number is not the exact integer, the C
2059 standard doesn't require an inexact exception to be raised. IEEE
2060 854 does require it so we do that.
2062 Note that for conversions to integer type the C standard requires
2063 round-to-zero to always be used. */
2065 APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
2067 roundingMode rounding_mode,
2068 bool *isExact) const
2070 lostFraction lost_fraction;
2071 const integerPart *src;
2072 unsigned int dstPartsCount, truncatedBits;
2076 /* Handle the three special cases first. */
2077 if (category == fcInfinity || category == fcNaN)
2080 dstPartsCount = partCountForBits(width);
2082 if (category == fcZero) {
2083 APInt::tcSet(parts, 0, dstPartsCount);
2084 // Negative zero can't be represented as an int.
2089 src = significandParts();
2091 /* Step 1: place our absolute value, with any fraction truncated, in
2094 /* Our absolute value is less than one; truncate everything. */
2095 APInt::tcSet(parts, 0, dstPartsCount);
2096 /* For exponent -1 the integer bit represents .5, look at that.
2097 For smaller exponents leftmost truncated bit is 0. */
2098 truncatedBits = semantics->precision -1U - exponent;
2100 /* We want the most significant (exponent + 1) bits; the rest are
2102 unsigned int bits = exponent + 1U;
2104 /* Hopelessly large in magnitude? */
2108 if (bits < semantics->precision) {
2109 /* We truncate (semantics->precision - bits) bits. */
2110 truncatedBits = semantics->precision - bits;
2111 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
2113 /* We want at least as many bits as are available. */
2114 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
2115 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
2120 /* Step 2: work out any lost fraction, and increment the absolute
2121 value if we would round away from zero. */
2122 if (truncatedBits) {
2123 lost_fraction = lostFractionThroughTruncation(src, partCount(),
2125 if (lost_fraction != lfExactlyZero &&
2126 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2127 if (APInt::tcIncrement(parts, dstPartsCount))
2128 return opInvalidOp; /* Overflow. */
2131 lost_fraction = lfExactlyZero;
2134 /* Step 3: check if we fit in the destination. */
2135 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
2139 /* Negative numbers cannot be represented as unsigned. */
2143 /* It takes omsb bits to represent the unsigned integer value.
2144 We lose a bit for the sign, but care is needed as the
2145 maximally negative integer is a special case. */
2146 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2149 /* This case can happen because of rounding. */
2154 APInt::tcNegate (parts, dstPartsCount);
2156 if (omsb >= width + !isSigned)
2160 if (lost_fraction == lfExactlyZero) {
2167 /* Same as convertToSignExtendedInteger, except we provide
2168 deterministic values in case of an invalid operation exception,
2169 namely zero for NaNs and the minimal or maximal value respectively
2170 for underflow or overflow.
2171 The *isExact output tells whether the result is exact, in the sense
2172 that converting it back to the original floating point type produces
2173 the original value. This is almost equivalent to result==opOK,
2174 except for negative zeroes.
2177 APFloat::convertToInteger(integerPart *parts, unsigned int width,
2179 roundingMode rounding_mode, bool *isExact) const
2183 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2186 if (fs == opInvalidOp) {
2187 unsigned int bits, dstPartsCount;
2189 dstPartsCount = partCountForBits(width);
2191 if (category == fcNaN)
2196 bits = width - isSigned;
2198 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
2199 if (sign && isSigned)
2200 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
2206 /* Same as convertToInteger(integerPart*, ...), except the result is returned in
2207 an APSInt, whose initial bit-width and signed-ness are used to determine the
2208 precision of the conversion.
2211 APFloat::convertToInteger(APSInt &result,
2212 roundingMode rounding_mode, bool *isExact) const
2214 unsigned bitWidth = result.getBitWidth();
2215 SmallVector<uint64_t, 4> parts(result.getNumWords());
2216 opStatus status = convertToInteger(
2217 parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact);
2218 // Keeps the original signed-ness.
2219 result = APInt(bitWidth, parts);
2223 /* Convert an unsigned integer SRC to a floating point number,
2224 rounding according to ROUNDING_MODE. The sign of the floating
2225 point number is not modified. */
2227 APFloat::convertFromUnsignedParts(const integerPart *src,
2228 unsigned int srcCount,
2229 roundingMode rounding_mode)
2231 unsigned int omsb, precision, dstCount;
2233 lostFraction lost_fraction;
2235 category = fcNormal;
2236 omsb = APInt::tcMSB(src, srcCount) + 1;
2237 dst = significandParts();
2238 dstCount = partCount();
2239 precision = semantics->precision;
2241 /* We want the most significant PRECISION bits of SRC. There may not
2242 be that many; extract what we can. */
2243 if (precision <= omsb) {
2244 exponent = omsb - 1;
2245 lost_fraction = lostFractionThroughTruncation(src, srcCount,
2247 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
2249 exponent = precision - 1;
2250 lost_fraction = lfExactlyZero;
2251 APInt::tcExtract(dst, dstCount, src, omsb, 0);
2254 return normalize(rounding_mode, lost_fraction);
2258 APFloat::convertFromAPInt(const APInt &Val,
2260 roundingMode rounding_mode)
2262 unsigned int partCount = Val.getNumWords();
2266 if (isSigned && api.isNegative()) {
2271 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2274 /* Convert a two's complement integer SRC to a floating point number,
2275 rounding according to ROUNDING_MODE. ISSIGNED is true if the
2276 integer is signed, in which case it must be sign-extended. */
2278 APFloat::convertFromSignExtendedInteger(const integerPart *src,
2279 unsigned int srcCount,
2281 roundingMode rounding_mode)
2286 APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
2289 /* If we're signed and negative negate a copy. */
2291 copy = new integerPart[srcCount];
2292 APInt::tcAssign(copy, src, srcCount);
2293 APInt::tcNegate(copy, srcCount);
2294 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2298 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2304 /* FIXME: should this just take a const APInt reference? */
2306 APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
2307 unsigned int width, bool isSigned,
2308 roundingMode rounding_mode)
2310 unsigned int partCount = partCountForBits(width);
2311 APInt api = APInt(width, makeArrayRef(parts, partCount));
2314 if (isSigned && APInt::tcExtractBit(parts, width - 1)) {
2319 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
2323 APFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode)
2325 lostFraction lost_fraction = lfExactlyZero;
2327 category = fcNormal;
2331 integerPart *significand = significandParts();
2332 unsigned partsCount = partCount();
2333 unsigned bitPos = partsCount * integerPartWidth;
2334 bool computedTrailingFraction = false;
2336 // Skip leading zeroes and any (hexa)decimal point.
2337 StringRef::iterator begin = s.begin();
2338 StringRef::iterator end = s.end();
2339 StringRef::iterator dot;
2340 StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot);
2341 StringRef::iterator firstSignificantDigit = p;
2344 integerPart hex_value;
2347 assert(dot == end && "String contains multiple dots");
2352 hex_value = hexDigitValue(*p);
2353 if (hex_value == -1U)
2358 // Store the number while we have space.
2361 hex_value <<= bitPos % integerPartWidth;
2362 significand[bitPos / integerPartWidth] |= hex_value;
2363 } else if (!computedTrailingFraction) {
2364 lost_fraction = trailingHexadecimalFraction(p, end, hex_value);
2365 computedTrailingFraction = true;
2369 /* Hex floats require an exponent but not a hexadecimal point. */
2370 assert(p != end && "Hex strings require an exponent");
2371 assert((*p == 'p' || *p == 'P') && "Invalid character in significand");
2372 assert(p != begin && "Significand has no digits");
2373 assert((dot == end || p - begin != 1) && "Significand has no digits");
2375 /* Ignore the exponent if we are zero. */
2376 if (p != firstSignificantDigit) {
2379 /* Implicit hexadecimal point? */
2383 /* Calculate the exponent adjustment implicit in the number of
2384 significant digits. */
2385 expAdjustment = static_cast<int>(dot - firstSignificantDigit);
2386 if (expAdjustment < 0)
2388 expAdjustment = expAdjustment * 4 - 1;
2390 /* Adjust for writing the significand starting at the most
2391 significant nibble. */
2392 expAdjustment += semantics->precision;
2393 expAdjustment -= partsCount * integerPartWidth;
2395 /* Adjust for the given exponent. */
2396 exponent = totalExponent(p + 1, end, expAdjustment);
2399 return normalize(rounding_mode, lost_fraction);
2403 APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2404 unsigned sigPartCount, int exp,
2405 roundingMode rounding_mode)
2407 unsigned int parts, pow5PartCount;
2408 fltSemantics calcSemantics = { 32767, -32767, 0 };
2409 integerPart pow5Parts[maxPowerOfFiveParts];
2412 isNearest = (rounding_mode == rmNearestTiesToEven ||
2413 rounding_mode == rmNearestTiesToAway);
2415 parts = partCountForBits(semantics->precision + 11);
2417 /* Calculate pow(5, abs(exp)). */
2418 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2420 for (;; parts *= 2) {
2421 opStatus sigStatus, powStatus;
2422 unsigned int excessPrecision, truncatedBits;
2424 calcSemantics.precision = parts * integerPartWidth - 1;
2425 excessPrecision = calcSemantics.precision - semantics->precision;
2426 truncatedBits = excessPrecision;
2428 APFloat decSig = APFloat::getZero(calcSemantics, sign);
2429 APFloat pow5(calcSemantics);
2431 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2432 rmNearestTiesToEven);
2433 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2434 rmNearestTiesToEven);
2435 /* Add exp, as 10^n = 5^n * 2^n. */
2436 decSig.exponent += exp;
2438 lostFraction calcLostFraction;
2439 integerPart HUerr, HUdistance;
2440 unsigned int powHUerr;
2443 /* multiplySignificand leaves the precision-th bit set to 1. */
2444 calcLostFraction = decSig.multiplySignificand(pow5, nullptr);
2445 powHUerr = powStatus != opOK;
2447 calcLostFraction = decSig.divideSignificand(pow5);
2448 /* Denormal numbers have less precision. */
2449 if (decSig.exponent < semantics->minExponent) {
2450 excessPrecision += (semantics->minExponent - decSig.exponent);
2451 truncatedBits = excessPrecision;
2452 if (excessPrecision > calcSemantics.precision)
2453 excessPrecision = calcSemantics.precision;
2455 /* Extra half-ulp lost in reciprocal of exponent. */
2456 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
2459 /* Both multiplySignificand and divideSignificand return the
2460 result with the integer bit set. */
2461 assert(APInt::tcExtractBit
2462 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2464 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2466 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2467 excessPrecision, isNearest);
2469 /* Are we guaranteed to round correctly if we truncate? */
2470 if (HUdistance >= HUerr) {
2471 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2472 calcSemantics.precision - excessPrecision,
2474 /* Take the exponent of decSig. If we tcExtract-ed less bits
2475 above we must adjust our exponent to compensate for the
2476 implicit right shift. */
2477 exponent = (decSig.exponent + semantics->precision
2478 - (calcSemantics.precision - excessPrecision));
2479 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2482 return normalize(rounding_mode, calcLostFraction);
2488 APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode)
2493 /* Scan the text. */
2494 StringRef::iterator p = str.begin();
2495 interpretDecimal(p, str.end(), &D);
2497 /* Handle the quick cases. First the case of no significant digits,
2498 i.e. zero, and then exponents that are obviously too large or too
2499 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2500 definitely overflows if
2502 (exp - 1) * L >= maxExponent
2504 and definitely underflows to zero where
2506 (exp + 1) * L <= minExponent - precision
2508 With integer arithmetic the tightest bounds for L are
2510 93/28 < L < 196/59 [ numerator <= 256 ]
2511 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2514 // Test if we have a zero number allowing for strings with no null terminators
2515 // and zero decimals with non-zero exponents.
2517 // We computed firstSigDigit by ignoring all zeros and dots. Thus if
2518 // D->firstSigDigit equals str.end(), every digit must be a zero and there can
2519 // be at most one dot. On the other hand, if we have a zero with a non-zero
2520 // exponent, then we know that D.firstSigDigit will be non-numeric.
2521 if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) {
2525 /* Check whether the normalized exponent is high enough to overflow
2526 max during the log-rebasing in the max-exponent check below. */
2527 } else if (D.normalizedExponent - 1 > INT_MAX / 42039) {
2528 fs = handleOverflow(rounding_mode);
2530 /* If it wasn't, then it also wasn't high enough to overflow max
2531 during the log-rebasing in the min-exponent check. Check that it
2532 won't overflow min in either check, then perform the min-exponent
2534 } else if (D.normalizedExponent - 1 < INT_MIN / 42039 ||
2535 (D.normalizedExponent + 1) * 28738 <=
2536 8651 * (semantics->minExponent - (int) semantics->precision)) {
2537 /* Underflow to zero and round. */
2538 category = fcNormal;
2540 fs = normalize(rounding_mode, lfLessThanHalf);
2542 /* We can finally safely perform the max-exponent check. */
2543 } else if ((D.normalizedExponent - 1) * 42039
2544 >= 12655 * semantics->maxExponent) {
2545 /* Overflow and round. */
2546 fs = handleOverflow(rounding_mode);
2548 integerPart *decSignificand;
2549 unsigned int partCount;
2551 /* A tight upper bound on number of bits required to hold an
2552 N-digit decimal integer is N * 196 / 59. Allocate enough space
2553 to hold the full significand, and an extra part required by
2555 partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1;
2556 partCount = partCountForBits(1 + 196 * partCount / 59);
2557 decSignificand = new integerPart[partCount + 1];
2560 /* Convert to binary efficiently - we do almost all multiplication
2561 in an integerPart. When this would overflow do we do a single
2562 bignum multiplication, and then revert again to multiplication
2563 in an integerPart. */
2565 integerPart decValue, val, multiplier;
2573 if (p == str.end()) {
2577 decValue = decDigitValue(*p++);
2578 assert(decValue < 10U && "Invalid character in significand");
2580 val = val * 10 + decValue;
2581 /* The maximum number that can be multiplied by ten with any
2582 digit added without overflowing an integerPart. */
2583 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2585 /* Multiply out the current part. */
2586 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2587 partCount, partCount + 1, false);
2589 /* If we used another part (likely but not guaranteed), increase
2591 if (decSignificand[partCount])
2593 } while (p <= D.lastSigDigit);
2595 category = fcNormal;
2596 fs = roundSignificandWithExponent(decSignificand, partCount,
2597 D.exponent, rounding_mode);
2599 delete [] decSignificand;
2606 APFloat::convertFromStringSpecials(StringRef str) {
2607 if (str.equals("inf") || str.equals("INFINITY")) {
2612 if (str.equals("-inf") || str.equals("-INFINITY")) {
2617 if (str.equals("nan") || str.equals("NaN")) {
2618 makeNaN(false, false);
2622 if (str.equals("-nan") || str.equals("-NaN")) {
2623 makeNaN(false, true);
2631 APFloat::convertFromString(StringRef str, roundingMode rounding_mode)
2633 assert(!str.empty() && "Invalid string length");
2635 // Handle special cases.
2636 if (convertFromStringSpecials(str))
2639 /* Handle a leading minus sign. */
2640 StringRef::iterator p = str.begin();
2641 size_t slen = str.size();
2642 sign = *p == '-' ? 1 : 0;
2643 if (*p == '-' || *p == '+') {
2646 assert(slen && "String has no digits");
2649 if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
2650 assert(slen - 2 && "Invalid string");
2651 return convertFromHexadecimalString(StringRef(p + 2, slen - 2),
2655 return convertFromDecimalString(StringRef(p, slen), rounding_mode);
2658 /* Write out a hexadecimal representation of the floating point value
2659 to DST, which must be of sufficient size, in the C99 form
2660 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2661 excluding the terminating NUL.
2663 If UPPERCASE, the output is in upper case, otherwise in lower case.
2665 HEXDIGITS digits appear altogether, rounding the value if
2666 necessary. If HEXDIGITS is 0, the minimal precision to display the
2667 number precisely is used instead. If nothing would appear after
2668 the decimal point it is suppressed.
2670 The decimal exponent is always printed and has at least one digit.
2671 Zero values display an exponent of zero. Infinities and NaNs
2672 appear as "infinity" or "nan" respectively.
2674 The above rules are as specified by C99. There is ambiguity about
2675 what the leading hexadecimal digit should be. This implementation
2676 uses whatever is necessary so that the exponent is displayed as
2677 stored. This implies the exponent will fall within the IEEE format
2678 range, and the leading hexadecimal digit will be 0 (for denormals),
2679 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2680 any other digits zero).
2683 APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2684 bool upperCase, roundingMode rounding_mode) const
2694 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2695 dst += sizeof infinityL - 1;
2699 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2700 dst += sizeof NaNU - 1;
2705 *dst++ = upperCase ? 'X': 'x';
2707 if (hexDigits > 1) {
2709 memset (dst, '0', hexDigits - 1);
2710 dst += hexDigits - 1;
2712 *dst++ = upperCase ? 'P': 'p';
2717 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2723 return static_cast<unsigned int>(dst - p);
2726 /* Does the hard work of outputting the correctly rounded hexadecimal
2727 form of a normal floating point number with the specified number of
2728 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2729 digits necessary to print the value precisely is output. */
2731 APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2733 roundingMode rounding_mode) const
2735 unsigned int count, valueBits, shift, partsCount, outputDigits;
2736 const char *hexDigitChars;
2737 const integerPart *significand;
2742 *dst++ = upperCase ? 'X': 'x';
2745 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2747 significand = significandParts();
2748 partsCount = partCount();
2750 /* +3 because the first digit only uses the single integer bit, so
2751 we have 3 virtual zero most-significant-bits. */
2752 valueBits = semantics->precision + 3;
2753 shift = integerPartWidth - valueBits % integerPartWidth;
2755 /* The natural number of digits required ignoring trailing
2756 insignificant zeroes. */
2757 outputDigits = (valueBits - significandLSB () + 3) / 4;
2759 /* hexDigits of zero means use the required number for the
2760 precision. Otherwise, see if we are truncating. If we are,
2761 find out if we need to round away from zero. */
2763 if (hexDigits < outputDigits) {
2764 /* We are dropping non-zero bits, so need to check how to round.
2765 "bits" is the number of dropped bits. */
2767 lostFraction fraction;
2769 bits = valueBits - hexDigits * 4;
2770 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2771 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2773 outputDigits = hexDigits;
2776 /* Write the digits consecutively, and start writing in the location
2777 of the hexadecimal point. We move the most significant digit
2778 left and add the hexadecimal point later. */
2781 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2783 while (outputDigits && count) {
2786 /* Put the most significant integerPartWidth bits in "part". */
2787 if (--count == partsCount)
2788 part = 0; /* An imaginary higher zero part. */
2790 part = significand[count] << shift;
2793 part |= significand[count - 1] >> (integerPartWidth - shift);
2795 /* Convert as much of "part" to hexdigits as we can. */
2796 unsigned int curDigits = integerPartWidth / 4;
2798 if (curDigits > outputDigits)
2799 curDigits = outputDigits;
2800 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2801 outputDigits -= curDigits;
2807 /* Note that hexDigitChars has a trailing '0'. */
2810 *q = hexDigitChars[hexDigitValue (*q) + 1];
2811 } while (*q == '0');
2814 /* Add trailing zeroes. */
2815 memset (dst, '0', outputDigits);
2816 dst += outputDigits;
2819 /* Move the most significant digit to before the point, and if there
2820 is something after the decimal point add it. This must come
2821 after rounding above. */
2828 /* Finally output the exponent. */
2829 *dst++ = upperCase ? 'P': 'p';
2831 return writeSignedDecimal (dst, exponent);
2834 hash_code llvm::hash_value(const APFloat &Arg) {
2835 if (!Arg.isFiniteNonZero())
2836 return hash_combine((uint8_t)Arg.category,
2837 // NaN has no sign, fix it at zero.
2838 Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2839 Arg.semantics->precision);
2841 // Normal floats need their exponent and significand hashed.
2842 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2843 Arg.semantics->precision, Arg.exponent,
2845 Arg.significandParts(),
2846 Arg.significandParts() + Arg.partCount()));
2849 // Conversion from APFloat to/from host float/double. It may eventually be
2850 // possible to eliminate these and have everybody deal with APFloats, but that
2851 // will take a while. This approach will not easily extend to long double.
2852 // Current implementation requires integerPartWidth==64, which is correct at
2853 // the moment but could be made more general.
2855 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
2856 // the actual IEEE respresentations. We compensate for that here.
2859 APFloat::convertF80LongDoubleAPFloatToAPInt() const
2861 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
2862 assert(partCount()==2);
2864 uint64_t myexponent, mysignificand;
2866 if (isFiniteNonZero()) {
2867 myexponent = exponent+16383; //bias
2868 mysignificand = significandParts()[0];
2869 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2870 myexponent = 0; // denormal
2871 } else if (category==fcZero) {
2874 } else if (category==fcInfinity) {
2875 myexponent = 0x7fff;
2876 mysignificand = 0x8000000000000000ULL;
2878 assert(category == fcNaN && "Unknown category");
2879 myexponent = 0x7fff;
2880 mysignificand = significandParts()[0];
2884 words[0] = mysignificand;
2885 words[1] = ((uint64_t)(sign & 1) << 15) |
2886 (myexponent & 0x7fffLL);
2887 return APInt(80, words);
2891 APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2893 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
2894 assert(partCount()==2);
2900 // Convert number to double. To avoid spurious underflows, we re-
2901 // normalize against the "double" minExponent first, and only *then*
2902 // truncate the mantissa. The result of that second conversion
2903 // may be inexact, but should never underflow.
2904 // Declare fltSemantics before APFloat that uses it (and
2905 // saves pointer to it) to ensure correct destruction order.
2906 fltSemantics extendedSemantics = *semantics;
2907 extendedSemantics.minExponent = IEEEdouble.minExponent;
2908 APFloat extended(*this);
2909 fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2910 assert(fs == opOK && !losesInfo);
2913 APFloat u(extended);
2914 fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2915 assert(fs == opOK || fs == opInexact);
2917 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2919 // If conversion was exact or resulted in a special case, we're done;
2920 // just set the second double to zero. Otherwise, re-convert back to
2921 // the extended format and compute the difference. This now should
2922 // convert exactly to double.
2923 if (u.isFiniteNonZero() && losesInfo) {
2924 fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo);
2925 assert(fs == opOK && !losesInfo);
2928 APFloat v(extended);
2929 v.subtract(u, rmNearestTiesToEven);
2930 fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo);
2931 assert(fs == opOK && !losesInfo);
2933 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
2938 return APInt(128, words);
2942 APFloat::convertQuadrupleAPFloatToAPInt() const
2944 assert(semantics == (const llvm::fltSemantics*)&IEEEquad);
2945 assert(partCount()==2);
2947 uint64_t myexponent, mysignificand, mysignificand2;
2949 if (isFiniteNonZero()) {
2950 myexponent = exponent+16383; //bias
2951 mysignificand = significandParts()[0];
2952 mysignificand2 = significandParts()[1];
2953 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2954 myexponent = 0; // denormal
2955 } else if (category==fcZero) {
2957 mysignificand = mysignificand2 = 0;
2958 } else if (category==fcInfinity) {
2959 myexponent = 0x7fff;
2960 mysignificand = mysignificand2 = 0;
2962 assert(category == fcNaN && "Unknown category!");
2963 myexponent = 0x7fff;
2964 mysignificand = significandParts()[0];
2965 mysignificand2 = significandParts()[1];
2969 words[0] = mysignificand;
2970 words[1] = ((uint64_t)(sign & 1) << 63) |
2971 ((myexponent & 0x7fff) << 48) |
2972 (mysignificand2 & 0xffffffffffffLL);
2974 return APInt(128, words);
2978 APFloat::convertDoubleAPFloatToAPInt() const
2980 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
2981 assert(partCount()==1);
2983 uint64_t myexponent, mysignificand;
2985 if (isFiniteNonZero()) {
2986 myexponent = exponent+1023; //bias
2987 mysignificand = *significandParts();
2988 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2989 myexponent = 0; // denormal
2990 } else if (category==fcZero) {
2993 } else if (category==fcInfinity) {
2997 assert(category == fcNaN && "Unknown category!");
2999 mysignificand = *significandParts();
3002 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
3003 ((myexponent & 0x7ff) << 52) |
3004 (mysignificand & 0xfffffffffffffLL))));
3008 APFloat::convertFloatAPFloatToAPInt() const
3010 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
3011 assert(partCount()==1);
3013 uint32_t myexponent, mysignificand;
3015 if (isFiniteNonZero()) {
3016 myexponent = exponent+127; //bias
3017 mysignificand = (uint32_t)*significandParts();
3018 if (myexponent == 1 && !(mysignificand & 0x800000))
3019 myexponent = 0; // denormal
3020 } else if (category==fcZero) {
3023 } else if (category==fcInfinity) {
3027 assert(category == fcNaN && "Unknown category!");
3029 mysignificand = (uint32_t)*significandParts();
3032 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3033 (mysignificand & 0x7fffff)));
3037 APFloat::convertHalfAPFloatToAPInt() const
3039 assert(semantics == (const llvm::fltSemantics*)&IEEEhalf);
3040 assert(partCount()==1);
3042 uint32_t myexponent, mysignificand;
3044 if (isFiniteNonZero()) {
3045 myexponent = exponent+15; //bias
3046 mysignificand = (uint32_t)*significandParts();
3047 if (myexponent == 1 && !(mysignificand & 0x400))
3048 myexponent = 0; // denormal
3049 } else if (category==fcZero) {
3052 } else if (category==fcInfinity) {
3056 assert(category == fcNaN && "Unknown category!");
3058 mysignificand = (uint32_t)*significandParts();
3061 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3062 (mysignificand & 0x3ff)));
3065 // This function creates an APInt that is just a bit map of the floating
3066 // point constant as it would appear in memory. It is not a conversion,
3067 // and treating the result as a normal integer is unlikely to be useful.
3070 APFloat::bitcastToAPInt() const
3072 if (semantics == (const llvm::fltSemantics*)&IEEEhalf)
3073 return convertHalfAPFloatToAPInt();
3075 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
3076 return convertFloatAPFloatToAPInt();
3078 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
3079 return convertDoubleAPFloatToAPInt();
3081 if (semantics == (const llvm::fltSemantics*)&IEEEquad)
3082 return convertQuadrupleAPFloatToAPInt();
3084 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
3085 return convertPPCDoubleDoubleAPFloatToAPInt();
3087 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
3089 return convertF80LongDoubleAPFloatToAPInt();
3093 APFloat::convertToFloat() const
3095 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle &&
3096 "Float semantics are not IEEEsingle");
3097 APInt api = bitcastToAPInt();
3098 return api.bitsToFloat();
3102 APFloat::convertToDouble() const
3104 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble &&
3105 "Float semantics are not IEEEdouble");
3106 APInt api = bitcastToAPInt();
3107 return api.bitsToDouble();
3110 /// Integer bit is explicit in this format. Intel hardware (387 and later)
3111 /// does not support these bit patterns:
3112 /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity")
3113 /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN")
3114 /// exponent = 0, integer bit 1 ("pseudodenormal")
3115 /// exponent!=0 nor all 1's, integer bit 0 ("unnormal")
3116 /// At the moment, the first two are treated as NaNs, the second two as Normal.
3118 APFloat::initFromF80LongDoubleAPInt(const APInt &api)
3120 assert(api.getBitWidth()==80);
3121 uint64_t i1 = api.getRawData()[0];
3122 uint64_t i2 = api.getRawData()[1];
3123 uint64_t myexponent = (i2 & 0x7fff);
3124 uint64_t mysignificand = i1;
3126 initialize(&APFloat::x87DoubleExtended);
3127 assert(partCount()==2);
3129 sign = static_cast<unsigned int>(i2>>15);
3130 if (myexponent==0 && mysignificand==0) {
3131 // exponent, significand meaningless
3133 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3134 // exponent, significand meaningless
3135 category = fcInfinity;
3136 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3137 // exponent meaningless
3139 significandParts()[0] = mysignificand;
3140 significandParts()[1] = 0;
3142 category = fcNormal;
3143 exponent = myexponent - 16383;
3144 significandParts()[0] = mysignificand;
3145 significandParts()[1] = 0;
3146 if (myexponent==0) // denormal
3152 APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
3154 assert(api.getBitWidth()==128);
3155 uint64_t i1 = api.getRawData()[0];
3156 uint64_t i2 = api.getRawData()[1];
3160 // Get the first double and convert to our format.
3161 initFromDoubleAPInt(APInt(64, i1));
3162 fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3163 assert(fs == opOK && !losesInfo);
3166 // Unless we have a special case, add in second double.
3167 if (isFiniteNonZero()) {
3168 APFloat v(IEEEdouble, APInt(64, i2));
3169 fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo);
3170 assert(fs == opOK && !losesInfo);
3173 add(v, rmNearestTiesToEven);
3178 APFloat::initFromQuadrupleAPInt(const APInt &api)
3180 assert(api.getBitWidth()==128);
3181 uint64_t i1 = api.getRawData()[0];
3182 uint64_t i2 = api.getRawData()[1];
3183 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3184 uint64_t mysignificand = i1;
3185 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3187 initialize(&APFloat::IEEEquad);
3188 assert(partCount()==2);
3190 sign = static_cast<unsigned int>(i2>>63);
3191 if (myexponent==0 &&
3192 (mysignificand==0 && mysignificand2==0)) {
3193 // exponent, significand meaningless
3195 } else if (myexponent==0x7fff &&
3196 (mysignificand==0 && mysignificand2==0)) {
3197 // exponent, significand meaningless
3198 category = fcInfinity;
3199 } else if (myexponent==0x7fff &&
3200 (mysignificand!=0 || mysignificand2 !=0)) {
3201 // exponent meaningless
3203 significandParts()[0] = mysignificand;
3204 significandParts()[1] = mysignificand2;
3206 category = fcNormal;
3207 exponent = myexponent - 16383;
3208 significandParts()[0] = mysignificand;
3209 significandParts()[1] = mysignificand2;
3210 if (myexponent==0) // denormal
3213 significandParts()[1] |= 0x1000000000000LL; // integer bit
3218 APFloat::initFromDoubleAPInt(const APInt &api)
3220 assert(api.getBitWidth()==64);
3221 uint64_t i = *api.getRawData();
3222 uint64_t myexponent = (i >> 52) & 0x7ff;
3223 uint64_t mysignificand = i & 0xfffffffffffffLL;
3225 initialize(&APFloat::IEEEdouble);
3226 assert(partCount()==1);
3228 sign = static_cast<unsigned int>(i>>63);
3229 if (myexponent==0 && mysignificand==0) {
3230 // exponent, significand meaningless
3232 } else if (myexponent==0x7ff && mysignificand==0) {
3233 // exponent, significand meaningless
3234 category = fcInfinity;
3235 } else if (myexponent==0x7ff && mysignificand!=0) {
3236 // exponent meaningless
3238 *significandParts() = mysignificand;
3240 category = fcNormal;
3241 exponent = myexponent - 1023;
3242 *significandParts() = mysignificand;
3243 if (myexponent==0) // denormal
3246 *significandParts() |= 0x10000000000000LL; // integer bit
3251 APFloat::initFromFloatAPInt(const APInt & api)
3253 assert(api.getBitWidth()==32);
3254 uint32_t i = (uint32_t)*api.getRawData();
3255 uint32_t myexponent = (i >> 23) & 0xff;
3256 uint32_t mysignificand = i & 0x7fffff;
3258 initialize(&APFloat::IEEEsingle);
3259 assert(partCount()==1);
3262 if (myexponent==0 && mysignificand==0) {
3263 // exponent, significand meaningless
3265 } else if (myexponent==0xff && mysignificand==0) {
3266 // exponent, significand meaningless
3267 category = fcInfinity;
3268 } else if (myexponent==0xff && mysignificand!=0) {
3269 // sign, exponent, significand meaningless
3271 *significandParts() = mysignificand;
3273 category = fcNormal;
3274 exponent = myexponent - 127; //bias
3275 *significandParts() = mysignificand;
3276 if (myexponent==0) // denormal
3279 *significandParts() |= 0x800000; // integer bit
3284 APFloat::initFromHalfAPInt(const APInt & api)
3286 assert(api.getBitWidth()==16);
3287 uint32_t i = (uint32_t)*api.getRawData();
3288 uint32_t myexponent = (i >> 10) & 0x1f;
3289 uint32_t mysignificand = i & 0x3ff;
3291 initialize(&APFloat::IEEEhalf);
3292 assert(partCount()==1);
3295 if (myexponent==0 && mysignificand==0) {
3296 // exponent, significand meaningless
3298 } else if (myexponent==0x1f && mysignificand==0) {
3299 // exponent, significand meaningless
3300 category = fcInfinity;
3301 } else if (myexponent==0x1f && mysignificand!=0) {
3302 // sign, exponent, significand meaningless
3304 *significandParts() = mysignificand;
3306 category = fcNormal;
3307 exponent = myexponent - 15; //bias
3308 *significandParts() = mysignificand;
3309 if (myexponent==0) // denormal
3312 *significandParts() |= 0x400; // integer bit
3316 /// Treat api as containing the bits of a floating point number. Currently
3317 /// we infer the floating point type from the size of the APInt. The
3318 /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
3319 /// when the size is anything else).
3321 APFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api)
3323 if (Sem == &IEEEhalf)
3324 return initFromHalfAPInt(api);
3325 if (Sem == &IEEEsingle)
3326 return initFromFloatAPInt(api);
3327 if (Sem == &IEEEdouble)
3328 return initFromDoubleAPInt(api);
3329 if (Sem == &x87DoubleExtended)
3330 return initFromF80LongDoubleAPInt(api);
3331 if (Sem == &IEEEquad)
3332 return initFromQuadrupleAPInt(api);
3333 if (Sem == &PPCDoubleDouble)
3334 return initFromPPCDoubleDoubleAPInt(api);
3336 llvm_unreachable(nullptr);
3340 APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
3344 return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth));
3346 return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth));
3348 return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth));
3350 return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth));
3353 return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth));
3354 return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth));
3356 llvm_unreachable("Unknown floating bit width");
3360 /// Make this number the largest magnitude normal number in the given
3362 void APFloat::makeLargest(bool Negative) {
3363 // We want (in interchange format):
3364 // sign = {Negative}
3366 // significand = 1..1
3367 category = fcNormal;
3369 exponent = semantics->maxExponent;
3371 // Use memset to set all but the highest integerPart to all ones.
3372 integerPart *significand = significandParts();
3373 unsigned PartCount = partCount();
3374 memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1));
3376 // Set the high integerPart especially setting all unused top bits for
3377 // internal consistency.
3378 const unsigned NumUnusedHighBits =
3379 PartCount*integerPartWidth - semantics->precision;
3380 significand[PartCount - 1] = ~integerPart(0) >> NumUnusedHighBits;
3383 /// Make this number the smallest magnitude denormal number in the given
3385 void APFloat::makeSmallest(bool Negative) {
3386 // We want (in interchange format):
3387 // sign = {Negative}
3389 // significand = 0..01
3390 category = fcNormal;
3392 exponent = semantics->minExponent;
3393 APInt::tcSet(significandParts(), 1, partCount());
3397 APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
3398 // We want (in interchange format):
3399 // sign = {Negative}
3401 // significand = 1..1
3402 APFloat Val(Sem, uninitialized);
3403 Val.makeLargest(Negative);
3407 APFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) {
3408 // We want (in interchange format):
3409 // sign = {Negative}
3411 // significand = 0..01
3412 APFloat Val(Sem, uninitialized);
3413 Val.makeSmallest(Negative);
3417 APFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) {
3418 APFloat Val(Sem, uninitialized);
3420 // We want (in interchange format):
3421 // sign = {Negative}
3423 // significand = 10..0
3425 Val.category = fcNormal;
3426 Val.zeroSignificand();
3427 Val.sign = Negative;
3428 Val.exponent = Sem.minExponent;
3429 Val.significandParts()[partCountForBits(Sem.precision)-1] |=
3430 (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth));
3435 APFloat::APFloat(const fltSemantics &Sem, const APInt &API) {
3436 initFromAPInt(&Sem, API);
3439 APFloat::APFloat(float f) {
3440 initFromAPInt(&IEEEsingle, APInt::floatToBits(f));
3443 APFloat::APFloat(double d) {
3444 initFromAPInt(&IEEEdouble, APInt::doubleToBits(d));
3448 void append(SmallVectorImpl<char> &Buffer, StringRef Str) {
3449 Buffer.append(Str.begin(), Str.end());
3452 /// Removes data from the given significand until it is no more
3453 /// precise than is required for the desired precision.
3454 void AdjustToPrecision(APInt &significand,
3455 int &exp, unsigned FormatPrecision) {
3456 unsigned bits = significand.getActiveBits();
3458 // 196/59 is a very slight overestimate of lg_2(10).
3459 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3461 if (bits <= bitsRequired) return;
3463 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3464 if (!tensRemovable) return;
3466 exp += tensRemovable;
3468 APInt divisor(significand.getBitWidth(), 1);
3469 APInt powten(significand.getBitWidth(), 10);
3471 if (tensRemovable & 1)
3473 tensRemovable >>= 1;
3474 if (!tensRemovable) break;
3478 significand = significand.udiv(divisor);
3480 // Truncate the significand down to its active bit count.
3481 significand = significand.trunc(significand.getActiveBits());
3485 void AdjustToPrecision(SmallVectorImpl<char> &buffer,
3486 int &exp, unsigned FormatPrecision) {
3487 unsigned N = buffer.size();
3488 if (N <= FormatPrecision) return;
3490 // The most significant figures are the last ones in the buffer.
3491 unsigned FirstSignificant = N - FormatPrecision;
3494 // FIXME: this probably shouldn't use 'round half up'.
3496 // Rounding down is just a truncation, except we also want to drop
3497 // trailing zeros from the new result.
3498 if (buffer[FirstSignificant - 1] < '5') {
3499 while (FirstSignificant < N && buffer[FirstSignificant] == '0')
3502 exp += FirstSignificant;
3503 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3507 // Rounding up requires a decimal add-with-carry. If we continue
3508 // the carry, the newly-introduced zeros will just be truncated.
3509 for (unsigned I = FirstSignificant; I != N; ++I) {
3510 if (buffer[I] == '9') {
3518 // If we carried through, we have exactly one digit of precision.
3519 if (FirstSignificant == N) {
3520 exp += FirstSignificant;
3522 buffer.push_back('1');
3526 exp += FirstSignificant;
3527 buffer.erase(&buffer[0], &buffer[FirstSignificant]);
3531 void APFloat::toString(SmallVectorImpl<char> &Str,
3532 unsigned FormatPrecision,
3533 unsigned FormatMaxPadding) const {
3537 return append(Str, "-Inf");
3539 return append(Str, "+Inf");
3541 case fcNaN: return append(Str, "NaN");
3547 if (!FormatMaxPadding)
3548 append(Str, "0.0E+0");
3560 // Decompose the number into an APInt and an exponent.
3561 int exp = exponent - ((int) semantics->precision - 1);
3562 APInt significand(semantics->precision,
3563 makeArrayRef(significandParts(),
3564 partCountForBits(semantics->precision)));
3566 // Set FormatPrecision if zero. We want to do this before we
3567 // truncate trailing zeros, as those are part of the precision.
3568 if (!FormatPrecision) {
3569 // We use enough digits so the number can be round-tripped back to an
3570 // APFloat. The formula comes from "How to Print Floating-Point Numbers
3571 // Accurately" by Steele and White.
3572 // FIXME: Using a formula based purely on the precision is conservative;
3573 // we can print fewer digits depending on the actual value being printed.
3575 // FormatPrecision = 2 + floor(significandBits / lg_2(10))
3576 FormatPrecision = 2 + semantics->precision * 59 / 196;
3579 // Ignore trailing binary zeros.
3580 int trailingZeros = significand.countTrailingZeros();
3581 exp += trailingZeros;
3582 significand = significand.lshr(trailingZeros);
3584 // Change the exponent from 2^e to 10^e.
3587 } else if (exp > 0) {
3589 significand = significand.zext(semantics->precision + exp);
3590 significand <<= exp;
3592 } else { /* exp < 0 */
3595 // We transform this using the identity:
3596 // (N)(2^-e) == (N)(5^e)(10^-e)
3597 // This means we have to multiply N (the significand) by 5^e.
3598 // To avoid overflow, we have to operate on numbers large
3599 // enough to store N * 5^e:
3600 // log2(N * 5^e) == log2(N) + e * log2(5)
3601 // <= semantics->precision + e * 137 / 59
3602 // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59)
3604 unsigned precision = semantics->precision + (137 * texp + 136) / 59;
3606 // Multiply significand by 5^e.
3607 // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8)
3608 significand = significand.zext(precision);
3609 APInt five_to_the_i(precision, 5);
3611 if (texp & 1) significand *= five_to_the_i;
3615 five_to_the_i *= five_to_the_i;
3619 AdjustToPrecision(significand, exp, FormatPrecision);
3621 SmallVector<char, 256> buffer;
3624 unsigned precision = significand.getBitWidth();
3625 APInt ten(precision, 10);
3626 APInt digit(precision, 0);
3628 bool inTrail = true;
3629 while (significand != 0) {
3630 // digit <- significand % 10
3631 // significand <- significand / 10
3632 APInt::udivrem(significand, ten, significand, digit);
3634 unsigned d = digit.getZExtValue();
3636 // Drop trailing zeros.
3637 if (inTrail && !d) exp++;
3639 buffer.push_back((char) ('0' + d));
3644 assert(!buffer.empty() && "no characters in buffer!");
3646 // Drop down to FormatPrecision.
3647 // TODO: don't do more precise calculations above than are required.
3648 AdjustToPrecision(buffer, exp, FormatPrecision);
3650 unsigned NDigits = buffer.size();
3652 // Check whether we should use scientific notation.
3653 bool FormatScientific;
3654 if (!FormatMaxPadding)
3655 FormatScientific = true;
3660 // But we shouldn't make the number look more precise than it is.
3661 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3662 NDigits + (unsigned) exp > FormatPrecision);
3664 // Power of the most significant digit.
3665 int MSD = exp + (int) (NDigits - 1);
3668 FormatScientific = false;
3670 // 765e-5 == 0.00765
3672 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
3677 // Scientific formatting is pretty straightforward.
3678 if (FormatScientific) {
3679 exp += (NDigits - 1);
3681 Str.push_back(buffer[NDigits-1]);
3686 for (unsigned I = 1; I != NDigits; ++I)
3687 Str.push_back(buffer[NDigits-1-I]);
3690 Str.push_back(exp >= 0 ? '+' : '-');
3691 if (exp < 0) exp = -exp;
3692 SmallVector<char, 6> expbuf;
3694 expbuf.push_back((char) ('0' + (exp % 10)));
3697 for (unsigned I = 0, E = expbuf.size(); I != E; ++I)
3698 Str.push_back(expbuf[E-1-I]);
3702 // Non-scientific, positive exponents.
3704 for (unsigned I = 0; I != NDigits; ++I)
3705 Str.push_back(buffer[NDigits-1-I]);
3706 for (unsigned I = 0; I != (unsigned) exp; ++I)
3711 // Non-scientific, negative exponents.
3713 // The number of digits to the left of the decimal point.
3714 int NWholeDigits = exp + (int) NDigits;
3717 if (NWholeDigits > 0) {
3718 for (; I != (unsigned) NWholeDigits; ++I)
3719 Str.push_back(buffer[NDigits-I-1]);
3722 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3726 for (unsigned Z = 1; Z != NZeros; ++Z)
3730 for (; I != NDigits; ++I)
3731 Str.push_back(buffer[NDigits-I-1]);
3734 bool APFloat::getExactInverse(APFloat *inv) const {
3735 // Special floats and denormals have no exact inverse.
3736 if (!isFiniteNonZero())
3739 // Check that the number is a power of two by making sure that only the
3740 // integer bit is set in the significand.
3741 if (significandLSB() != semantics->precision - 1)
3745 APFloat reciprocal(*semantics, 1ULL);
3746 if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
3749 // Avoid multiplication with a denormal, it is not safe on all platforms and
3750 // may be slower than a normal division.
3751 if (reciprocal.isDenormal())
3754 assert(reciprocal.isFiniteNonZero() &&
3755 reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
3763 bool APFloat::isSignaling() const {
3767 // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the
3768 // first bit of the trailing significand being 0.
3769 return !APInt::tcExtractBit(significandParts(), semantics->precision - 2);
3772 /// IEEE-754R 2008 5.3.1: nextUp/nextDown.
3774 /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with
3775 /// appropriate sign switching before/after the computation.
3776 APFloat::opStatus APFloat::next(bool nextDown) {
3777 // If we are performing nextDown, swap sign so we have -x.
3781 // Compute nextUp(x)
3782 opStatus result = opOK;
3784 // Handle each float category separately.
3787 // nextUp(+inf) = +inf
3790 // nextUp(-inf) = -getLargest()
3794 // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag.
3795 // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not
3796 // change the payload.
3797 if (isSignaling()) {
3798 result = opInvalidOp;
3799 // For consistency, propagate the sign of the sNaN to the qNaN.
3800 makeNaN(false, isNegative(), nullptr);
3804 // nextUp(pm 0) = +getSmallest()
3805 makeSmallest(false);
3808 // nextUp(-getSmallest()) = -0
3809 if (isSmallest() && isNegative()) {
3810 APInt::tcSet(significandParts(), 0, partCount());
3816 // nextUp(getLargest()) == INFINITY
3817 if (isLargest() && !isNegative()) {
3818 APInt::tcSet(significandParts(), 0, partCount());
3819 category = fcInfinity;
3820 exponent = semantics->maxExponent + 1;
3824 // nextUp(normal) == normal + inc.
3826 // If we are negative, we need to decrement the significand.
3828 // We only cross a binade boundary that requires adjusting the exponent
3830 // 1. exponent != semantics->minExponent. This implies we are not in the
3831 // smallest binade or are dealing with denormals.
3832 // 2. Our significand excluding the integral bit is all zeros.
3833 bool WillCrossBinadeBoundary =
3834 exponent != semantics->minExponent && isSignificandAllZeros();
3836 // Decrement the significand.
3838 // We always do this since:
3839 // 1. If we are dealing with a non-binade decrement, by definition we
3840 // just decrement the significand.
3841 // 2. If we are dealing with a normal -> normal binade decrement, since
3842 // we have an explicit integral bit the fact that all bits but the
3843 // integral bit are zero implies that subtracting one will yield a
3844 // significand with 0 integral bit and 1 in all other spots. Thus we
3845 // must just adjust the exponent and set the integral bit to 1.
3846 // 3. If we are dealing with a normal -> denormal binade decrement,
3847 // since we set the integral bit to 0 when we represent denormals, we
3848 // just decrement the significand.
3849 integerPart *Parts = significandParts();
3850 APInt::tcDecrement(Parts, partCount());
3852 if (WillCrossBinadeBoundary) {
3853 // Our result is a normal number. Do the following:
3854 // 1. Set the integral bit to 1.
3855 // 2. Decrement the exponent.
3856 APInt::tcSetBit(Parts, semantics->precision - 1);
3860 // If we are positive, we need to increment the significand.
3862 // We only cross a binade boundary that requires adjusting the exponent if
3863 // the input is not a denormal and all of said input's significand bits
3864 // are set. If all of said conditions are true: clear the significand, set
3865 // the integral bit to 1, and increment the exponent. If we have a
3866 // denormal always increment since moving denormals and the numbers in the
3867 // smallest normal binade have the same exponent in our representation.
3868 bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes();
3870 if (WillCrossBinadeBoundary) {
3871 integerPart *Parts = significandParts();
3872 APInt::tcSet(Parts, 0, partCount());
3873 APInt::tcSetBit(Parts, semantics->precision - 1);
3874 assert(exponent != semantics->maxExponent &&
3875 "We can not increment an exponent beyond the maxExponent allowed"
3876 " by the given floating point semantics.");
3879 incrementSignificand();
3885 // If we are performing nextDown, swap sign so we have -nextUp(-x)
3893 APFloat::makeInf(bool Negative) {
3894 category = fcInfinity;
3896 exponent = semantics->maxExponent + 1;
3897 APInt::tcSet(significandParts(), 0, partCount());
3901 APFloat::makeZero(bool Negative) {
3904 exponent = semantics->minExponent-1;
3905 APInt::tcSet(significandParts(), 0, partCount());