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/FoldingSet.h"
19 #include "llvm/Support/MathExtras.h"
23 #define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
25 /* Assumed in hexadecimal significand parsing, and conversion to
26 hexadecimal strings. */
27 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
31 /* Represents floating point arithmetic semantics. */
33 /* The largest E such that 2^E is representable; this matches the
34 definition of IEEE 754. */
35 exponent_t maxExponent;
37 /* The smallest E such that 2^E is a normalized number; this
38 matches the definition of IEEE 754. */
39 exponent_t minExponent;
41 /* Number of bits in the significand. This includes the integer
43 unsigned int precision;
45 /* True if arithmetic is supported. */
46 unsigned int arithmeticOK;
49 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
50 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
51 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
52 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, true };
53 const fltSemantics APFloat::Bogus = { 0, 0, 0, true };
55 // The PowerPC format consists of two doubles. It does not map cleanly
56 // onto the usual format above. For now only storage of constants of
57 // this type is supported, no arithmetic.
58 const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022, 106, false };
60 /* A tight upper bound on number of parts required to hold the value
63 power * 815 / (351 * integerPartWidth) + 1
65 However, whilst the result may require only this many parts,
66 because we are multiplying two values to get it, the
67 multiplication may require an extra part with the excess part
68 being zero (consider the trivial case of 1 * 1, tcFullMultiply
69 requires two parts to hold the single-part result). So we add an
70 extra one to guarantee enough space whilst multiplying. */
71 const unsigned int maxExponent = 16383;
72 const unsigned int maxPrecision = 113;
73 const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1;
74 const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815)
75 / (351 * integerPartWidth));
78 /* Put a bunch of private, handy routines in an anonymous namespace. */
82 partCountForBits(unsigned int bits)
84 return ((bits) + integerPartWidth - 1) / integerPartWidth;
87 /* Returns 0U-9U. Return values >= 10U are not digits. */
89 decDigitValue(unsigned int c)
95 hexDigitValue(unsigned int c)
115 assertArithmeticOK(const llvm::fltSemantics &semantics) {
116 assert(semantics.arithmeticOK
117 && "Compile-time arithmetic does not support these semantics");
120 /* Return the value of a decimal exponent of the form
123 If the exponent overflows, returns a large exponent with the
126 readExponent(const char *p)
129 unsigned int absExponent;
130 const unsigned int overlargeExponent = 24000; /* FIXME. */
132 isNegative = (*p == '-');
133 if (*p == '-' || *p == '+')
136 absExponent = decDigitValue(*p++);
137 assert (absExponent < 10U);
142 value = decDigitValue(*p);
147 value += absExponent * 10;
148 if (absExponent >= overlargeExponent) {
149 absExponent = overlargeExponent;
156 return -(int) absExponent;
158 return (int) absExponent;
161 /* This is ugly and needs cleaning up, but I don't immediately see
162 how whilst remaining safe. */
164 totalExponent(const char *p, int exponentAdjustment)
166 integerPart unsignedExponent;
167 bool negative, overflow;
170 /* Move past the exponent letter and sign to the digits. */
172 negative = *p == '-';
173 if(*p == '-' || *p == '+')
176 unsignedExponent = 0;
181 value = decDigitValue(*p);
186 unsignedExponent = unsignedExponent * 10 + value;
187 if(unsignedExponent > 65535)
191 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
195 exponent = unsignedExponent;
197 exponent = -exponent;
198 exponent += exponentAdjustment;
199 if(exponent > 65535 || exponent < -65536)
204 exponent = negative ? -65536: 65535;
210 skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
225 /* Given a normal decimal floating point number of the form
229 where the decimal point and exponent are optional, fill out the
230 structure D. Exponent is appropriate if the significand is
231 treated as an integer, and normalizedExponent if the significand
232 is taken to have the decimal point after a single leading
235 If the value is zero, V->firstSigDigit points to a non-digit, and
236 the return exponent is zero.
239 const char *firstSigDigit;
240 const char *lastSigDigit;
242 int normalizedExponent;
246 interpretDecimal(const char *p, decimalInfo *D)
250 p = skipLeadingZeroesAndAnyDot (p, &dot);
252 D->firstSigDigit = p;
254 D->normalizedExponent = 0;
261 if (decDigitValue(*p) >= 10U)
266 /* If number is all zerooes accept any exponent. */
267 if (p != D->firstSigDigit) {
268 if (*p == 'e' || *p == 'E')
269 D->exponent = readExponent(p + 1);
271 /* Implied decimal point? */
275 /* Drop insignificant trailing zeroes. */
282 /* Adjust the exponents for any decimal point. */
283 D->exponent += (dot - p) - (dot > p);
284 D->normalizedExponent = (D->exponent + (p - D->firstSigDigit)
285 - (dot > D->firstSigDigit && dot < p));
291 /* Return the trailing fraction of a hexadecimal number.
292 DIGITVALUE is the first hex digit of the fraction, P points to
295 trailingHexadecimalFraction(const char *p, unsigned int digitValue)
297 unsigned int hexDigit;
299 /* If the first trailing digit isn't 0 or 8 we can work out the
300 fraction immediately. */
302 return lfMoreThanHalf;
303 else if(digitValue < 8 && digitValue > 0)
304 return lfLessThanHalf;
306 /* Otherwise we need to find the first non-zero digit. */
310 hexDigit = hexDigitValue(*p);
312 /* If we ran off the end it is exactly zero or one-half, otherwise
315 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
317 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
320 /* Return the fraction lost were a bignum truncated losing the least
321 significant BITS bits. */
323 lostFractionThroughTruncation(const integerPart *parts,
324 unsigned int partCount,
329 lsb = APInt::tcLSB(parts, partCount);
331 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
333 return lfExactlyZero;
335 return lfExactlyHalf;
336 if(bits <= partCount * integerPartWidth
337 && APInt::tcExtractBit(parts, bits - 1))
338 return lfMoreThanHalf;
340 return lfLessThanHalf;
343 /* Shift DST right BITS bits noting lost fraction. */
345 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
347 lostFraction lost_fraction;
349 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
351 APInt::tcShiftRight(dst, parts, bits);
353 return lost_fraction;
356 /* Combine the effect of two lost fractions. */
358 combineLostFractions(lostFraction moreSignificant,
359 lostFraction lessSignificant)
361 if(lessSignificant != lfExactlyZero) {
362 if(moreSignificant == lfExactlyZero)
363 moreSignificant = lfLessThanHalf;
364 else if(moreSignificant == lfExactlyHalf)
365 moreSignificant = lfMoreThanHalf;
368 return moreSignificant;
371 /* The error from the true value, in half-ulps, on multiplying two
372 floating point numbers, which differ from the value they
373 approximate by at most HUE1 and HUE2 half-ulps, is strictly less
374 than the returned value.
376 See "How to Read Floating Point Numbers Accurately" by William D
379 HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
381 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
383 if (HUerr1 + HUerr2 == 0)
384 return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */
386 return inexactMultiply + 2 * (HUerr1 + HUerr2);
389 /* The number of ulps from the boundary (zero, or half if ISNEAREST)
390 when the least significant BITS are truncated. BITS cannot be
393 ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
395 unsigned int count, partBits;
396 integerPart part, boundary;
401 count = bits / integerPartWidth;
402 partBits = bits % integerPartWidth + 1;
404 part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits));
407 boundary = (integerPart) 1 << (partBits - 1);
412 if (part - boundary <= boundary - part)
413 return part - boundary;
415 return boundary - part;
418 if (part == boundary) {
421 return ~(integerPart) 0; /* A lot. */
424 } else if (part == boundary - 1) {
427 return ~(integerPart) 0; /* A lot. */
432 return ~(integerPart) 0; /* A lot. */
435 /* Place pow(5, power) in DST, and return the number of parts used.
436 DST must be at least one part larger than size of the answer. */
438 powerOf5(integerPart *dst, unsigned int power)
440 static integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
442 static integerPart pow5s[maxPowerOfFiveParts * 2 + 5] = { 78125 * 5 };
443 static unsigned int partsCount[16] = { 1 };
445 integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5;
448 assert(power <= maxExponent);
453 *p1 = firstEightPowers[power & 7];
459 for (unsigned int n = 0; power; power >>= 1, n++) {
464 /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */
466 pc = partsCount[n - 1];
467 APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc);
469 if (pow5[pc - 1] == 0)
477 APInt::tcFullMultiply(p2, p1, pow5, result, pc);
479 if (p2[result - 1] == 0)
482 /* Now result is in p1 with partsCount parts and p2 is scratch
484 tmp = p1, p1 = p2, p2 = tmp;
491 APInt::tcAssign(dst, p1, result);
496 /* Zero at the end to avoid modular arithmetic when adding one; used
497 when rounding up during hexadecimal output. */
498 static const char hexDigitsLower[] = "0123456789abcdef0";
499 static const char hexDigitsUpper[] = "0123456789ABCDEF0";
500 static const char infinityL[] = "infinity";
501 static const char infinityU[] = "INFINITY";
502 static const char NaNL[] = "nan";
503 static const char NaNU[] = "NAN";
505 /* Write out an integerPart in hexadecimal, starting with the most
506 significant nibble. Write out exactly COUNT hexdigits, return
509 partAsHex (char *dst, integerPart part, unsigned int count,
510 const char *hexDigitChars)
512 unsigned int result = count;
514 assert (count != 0 && count <= integerPartWidth / 4);
516 part >>= (integerPartWidth - 4 * count);
518 dst[count] = hexDigitChars[part & 0xf];
525 /* Write out an unsigned decimal integer. */
527 writeUnsignedDecimal (char *dst, unsigned int n)
543 /* Write out a signed decimal integer. */
545 writeSignedDecimal (char *dst, int value)
549 dst = writeUnsignedDecimal(dst, -(unsigned) value);
551 dst = writeUnsignedDecimal(dst, value);
559 APFloat::initialize(const fltSemantics *ourSemantics)
563 semantics = ourSemantics;
566 significand.parts = new integerPart[count];
570 APFloat::freeSignificand()
573 delete [] significand.parts;
577 APFloat::assign(const APFloat &rhs)
579 assert(semantics == rhs.semantics);
582 category = rhs.category;
583 exponent = rhs.exponent;
585 exponent2 = rhs.exponent2;
586 if(category == fcNormal || category == fcNaN)
587 copySignificand(rhs);
591 APFloat::copySignificand(const APFloat &rhs)
593 assert(category == fcNormal || category == fcNaN);
594 assert(rhs.partCount() >= partCount());
596 APInt::tcAssign(significandParts(), rhs.significandParts(),
600 /* Make this number a NaN, with an arbitrary but deterministic value
601 for the significand. */
603 APFloat::makeNaN(void)
606 APInt::tcSet(significandParts(), ~0U, partCount());
610 APFloat::operator=(const APFloat &rhs)
613 if(semantics != rhs.semantics) {
615 initialize(rhs.semantics);
624 APFloat::bitwiseIsEqual(const APFloat &rhs) const {
627 if (semantics != rhs.semantics ||
628 category != rhs.category ||
631 if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
634 if (category==fcZero || category==fcInfinity)
636 else if (category==fcNormal && exponent!=rhs.exponent)
638 else if (semantics==(const llvm::fltSemantics*)&PPCDoubleDouble &&
639 exponent2!=rhs.exponent2)
643 const integerPart* p=significandParts();
644 const integerPart* q=rhs.significandParts();
645 for (; i>0; i--, p++, q++) {
653 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
655 assertArithmeticOK(ourSemantics);
656 initialize(&ourSemantics);
659 exponent = ourSemantics.precision - 1;
660 significandParts()[0] = value;
661 normalize(rmNearestTiesToEven, lfExactlyZero);
664 APFloat::APFloat(const fltSemantics &ourSemantics,
665 fltCategory ourCategory, bool negative)
667 assertArithmeticOK(ourSemantics);
668 initialize(&ourSemantics);
669 category = ourCategory;
671 if(category == fcNormal)
673 else if (ourCategory == fcNaN)
677 APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
679 assertArithmeticOK(ourSemantics);
680 initialize(&ourSemantics);
681 convertFromString(text, rmNearestTiesToEven);
684 APFloat::APFloat(const APFloat &rhs)
686 initialize(rhs.semantics);
695 // Profile - This method 'profiles' an APFloat for use with FoldingSet.
696 void APFloat::Profile(FoldingSetNodeID& ID) const {
697 ID.Add(convertToAPInt());
701 APFloat::partCount() const
703 return partCountForBits(semantics->precision + 1);
707 APFloat::semanticsPrecision(const fltSemantics &semantics)
709 return semantics.precision;
713 APFloat::significandParts() const
715 return const_cast<APFloat *>(this)->significandParts();
719 APFloat::significandParts()
721 assert(category == fcNormal || category == fcNaN);
724 return significand.parts;
726 return &significand.part;
730 APFloat::zeroSignificand()
733 APInt::tcSet(significandParts(), 0, partCount());
736 /* Increment an fcNormal floating point number's significand. */
738 APFloat::incrementSignificand()
742 carry = APInt::tcIncrement(significandParts(), partCount());
744 /* Our callers should never cause us to overflow. */
748 /* Add the significand of the RHS. Returns the carry flag. */
750 APFloat::addSignificand(const APFloat &rhs)
754 parts = significandParts();
756 assert(semantics == rhs.semantics);
757 assert(exponent == rhs.exponent);
759 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
762 /* Subtract the significand of the RHS with a borrow flag. Returns
765 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
769 parts = significandParts();
771 assert(semantics == rhs.semantics);
772 assert(exponent == rhs.exponent);
774 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
778 /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
779 on to the full-precision result of the multiplication. Returns the
782 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
784 unsigned int omsb; // One, not zero, based MSB.
785 unsigned int partsCount, newPartsCount, precision;
786 integerPart *lhsSignificand;
787 integerPart scratch[4];
788 integerPart *fullSignificand;
789 lostFraction lost_fraction;
791 assert(semantics == rhs.semantics);
793 precision = semantics->precision;
794 newPartsCount = partCountForBits(precision * 2);
796 if(newPartsCount > 4)
797 fullSignificand = new integerPart[newPartsCount];
799 fullSignificand = scratch;
801 lhsSignificand = significandParts();
802 partsCount = partCount();
804 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
805 rhs.significandParts(), partsCount, partsCount);
807 lost_fraction = lfExactlyZero;
808 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
809 exponent += rhs.exponent;
812 Significand savedSignificand = significand;
813 const fltSemantics *savedSemantics = semantics;
814 fltSemantics extendedSemantics;
816 unsigned int extendedPrecision;
818 /* Normalize our MSB. */
819 extendedPrecision = precision + precision - 1;
820 if(omsb != extendedPrecision)
822 APInt::tcShiftLeft(fullSignificand, newPartsCount,
823 extendedPrecision - omsb);
824 exponent -= extendedPrecision - omsb;
827 /* Create new semantics. */
828 extendedSemantics = *semantics;
829 extendedSemantics.precision = extendedPrecision;
831 if(newPartsCount == 1)
832 significand.part = fullSignificand[0];
834 significand.parts = fullSignificand;
835 semantics = &extendedSemantics;
837 APFloat extendedAddend(*addend);
838 status = extendedAddend.convert(extendedSemantics, rmTowardZero);
839 assert(status == opOK);
840 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
842 /* Restore our state. */
843 if(newPartsCount == 1)
844 fullSignificand[0] = significand.part;
845 significand = savedSignificand;
846 semantics = savedSemantics;
848 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
851 exponent -= (precision - 1);
853 if(omsb > precision) {
854 unsigned int bits, significantParts;
857 bits = omsb - precision;
858 significantParts = partCountForBits(omsb);
859 lf = shiftRight(fullSignificand, significantParts, bits);
860 lost_fraction = combineLostFractions(lf, lost_fraction);
864 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
866 if(newPartsCount > 4)
867 delete [] fullSignificand;
869 return lost_fraction;
872 /* Multiply the significands of LHS and RHS to DST. */
874 APFloat::divideSignificand(const APFloat &rhs)
876 unsigned int bit, i, partsCount;
877 const integerPart *rhsSignificand;
878 integerPart *lhsSignificand, *dividend, *divisor;
879 integerPart scratch[4];
880 lostFraction lost_fraction;
882 assert(semantics == rhs.semantics);
884 lhsSignificand = significandParts();
885 rhsSignificand = rhs.significandParts();
886 partsCount = partCount();
889 dividend = new integerPart[partsCount * 2];
893 divisor = dividend + partsCount;
895 /* Copy the dividend and divisor as they will be modified in-place. */
896 for(i = 0; i < partsCount; i++) {
897 dividend[i] = lhsSignificand[i];
898 divisor[i] = rhsSignificand[i];
899 lhsSignificand[i] = 0;
902 exponent -= rhs.exponent;
904 unsigned int precision = semantics->precision;
906 /* Normalize the divisor. */
907 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
910 APInt::tcShiftLeft(divisor, partsCount, bit);
913 /* Normalize the dividend. */
914 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
917 APInt::tcShiftLeft(dividend, partsCount, bit);
920 /* Ensure the dividend >= divisor initially for the loop below.
921 Incidentally, this means that the division loop below is
922 guaranteed to set the integer bit to one. */
923 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
925 APInt::tcShiftLeft(dividend, partsCount, 1);
926 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
930 for(bit = precision; bit; bit -= 1) {
931 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
932 APInt::tcSubtract(dividend, divisor, 0, partsCount);
933 APInt::tcSetBit(lhsSignificand, bit - 1);
936 APInt::tcShiftLeft(dividend, partsCount, 1);
939 /* Figure out the lost fraction. */
940 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
943 lost_fraction = lfMoreThanHalf;
945 lost_fraction = lfExactlyHalf;
946 else if(APInt::tcIsZero(dividend, partsCount))
947 lost_fraction = lfExactlyZero;
949 lost_fraction = lfLessThanHalf;
954 return lost_fraction;
958 APFloat::significandMSB() const
960 return APInt::tcMSB(significandParts(), partCount());
964 APFloat::significandLSB() const
966 return APInt::tcLSB(significandParts(), partCount());
969 /* Note that a zero result is NOT normalized to fcZero. */
971 APFloat::shiftSignificandRight(unsigned int bits)
973 /* Our exponent should not overflow. */
974 assert((exponent_t) (exponent + bits) >= exponent);
978 return shiftRight(significandParts(), partCount(), bits);
981 /* Shift the significand left BITS bits, subtract BITS from its exponent. */
983 APFloat::shiftSignificandLeft(unsigned int bits)
985 assert(bits < semantics->precision);
988 unsigned int partsCount = partCount();
990 APInt::tcShiftLeft(significandParts(), partsCount, bits);
993 assert(!APInt::tcIsZero(significandParts(), partsCount));
998 APFloat::compareAbsoluteValue(const APFloat &rhs) const
1002 assert(semantics == rhs.semantics);
1003 assert(category == fcNormal);
1004 assert(rhs.category == fcNormal);
1006 compare = exponent - rhs.exponent;
1008 /* If exponents are equal, do an unsigned bignum comparison of the
1011 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
1015 return cmpGreaterThan;
1016 else if(compare < 0)
1022 /* Handle overflow. Sign is preserved. We either become infinity or
1023 the largest finite number. */
1025 APFloat::handleOverflow(roundingMode rounding_mode)
1028 if(rounding_mode == rmNearestTiesToEven
1029 || rounding_mode == rmNearestTiesToAway
1030 || (rounding_mode == rmTowardPositive && !sign)
1031 || (rounding_mode == rmTowardNegative && sign))
1033 category = fcInfinity;
1034 return (opStatus) (opOverflow | opInexact);
1037 /* Otherwise we become the largest finite number. */
1038 category = fcNormal;
1039 exponent = semantics->maxExponent;
1040 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
1041 semantics->precision);
1046 /* Returns TRUE if, when truncating the current number, with BIT the
1047 new LSB, with the given lost fraction and rounding mode, the result
1048 would need to be rounded away from zero (i.e., by increasing the
1049 signficand). This routine must work for fcZero of both signs, and
1050 fcNormal numbers. */
1052 APFloat::roundAwayFromZero(roundingMode rounding_mode,
1053 lostFraction lost_fraction,
1054 unsigned int bit) const
1056 /* NaNs and infinities should not have lost fractions. */
1057 assert(category == fcNormal || category == fcZero);
1059 /* Current callers never pass this so we don't handle it. */
1060 assert(lost_fraction != lfExactlyZero);
1062 switch(rounding_mode) {
1066 case rmNearestTiesToAway:
1067 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
1069 case rmNearestTiesToEven:
1070 if(lost_fraction == lfMoreThanHalf)
1073 /* Our zeroes don't have a significand to test. */
1074 if(lost_fraction == lfExactlyHalf && category != fcZero)
1075 return APInt::tcExtractBit(significandParts(), bit);
1082 case rmTowardPositive:
1083 return sign == false;
1085 case rmTowardNegative:
1086 return sign == true;
1091 APFloat::normalize(roundingMode rounding_mode,
1092 lostFraction lost_fraction)
1094 unsigned int omsb; /* One, not zero, based MSB. */
1097 if(category != fcNormal)
1100 /* Before rounding normalize the exponent of fcNormal numbers. */
1101 omsb = significandMSB() + 1;
1104 /* OMSB is numbered from 1. We want to place it in the integer
1105 bit numbered PRECISON if possible, with a compensating change in
1107 exponentChange = omsb - semantics->precision;
1109 /* If the resulting exponent is too high, overflow according to
1110 the rounding mode. */
1111 if(exponent + exponentChange > semantics->maxExponent)
1112 return handleOverflow(rounding_mode);
1114 /* Subnormal numbers have exponent minExponent, and their MSB
1115 is forced based on that. */
1116 if(exponent + exponentChange < semantics->minExponent)
1117 exponentChange = semantics->minExponent - exponent;
1119 /* Shifting left is easy as we don't lose precision. */
1120 if(exponentChange < 0) {
1121 assert(lost_fraction == lfExactlyZero);
1123 shiftSignificandLeft(-exponentChange);
1128 if(exponentChange > 0) {
1131 /* Shift right and capture any new lost fraction. */
1132 lf = shiftSignificandRight(exponentChange);
1134 lost_fraction = combineLostFractions(lf, lost_fraction);
1136 /* Keep OMSB up-to-date. */
1137 if(omsb > (unsigned) exponentChange)
1138 omsb -= exponentChange;
1144 /* Now round the number according to rounding_mode given the lost
1147 /* As specified in IEEE 754, since we do not trap we do not report
1148 underflow for exact results. */
1149 if(lost_fraction == lfExactlyZero) {
1150 /* Canonicalize zeroes. */
1157 /* Increment the significand if we're rounding away from zero. */
1158 if(roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1160 exponent = semantics->minExponent;
1162 incrementSignificand();
1163 omsb = significandMSB() + 1;
1165 /* Did the significand increment overflow? */
1166 if(omsb == (unsigned) semantics->precision + 1) {
1167 /* Renormalize by incrementing the exponent and shifting our
1168 significand right one. However if we already have the
1169 maximum exponent we overflow to infinity. */
1170 if(exponent == semantics->maxExponent) {
1171 category = fcInfinity;
1173 return (opStatus) (opOverflow | opInexact);
1176 shiftSignificandRight(1);
1182 /* The normal case - we were and are not denormal, and any
1183 significand increment above didn't overflow. */
1184 if(omsb == semantics->precision)
1187 /* We have a non-zero denormal. */
1188 assert(omsb < semantics->precision);
1190 /* Canonicalize zeroes. */
1194 /* The fcZero case is a denormal that underflowed to zero. */
1195 return (opStatus) (opUnderflow | opInexact);
1199 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
1201 switch(convolve(category, rhs.category)) {
1205 case convolve(fcNaN, fcZero):
1206 case convolve(fcNaN, fcNormal):
1207 case convolve(fcNaN, fcInfinity):
1208 case convolve(fcNaN, fcNaN):
1209 case convolve(fcNormal, fcZero):
1210 case convolve(fcInfinity, fcNormal):
1211 case convolve(fcInfinity, fcZero):
1214 case convolve(fcZero, fcNaN):
1215 case convolve(fcNormal, fcNaN):
1216 case convolve(fcInfinity, fcNaN):
1218 copySignificand(rhs);
1221 case convolve(fcNormal, fcInfinity):
1222 case convolve(fcZero, fcInfinity):
1223 category = fcInfinity;
1224 sign = rhs.sign ^ subtract;
1227 case convolve(fcZero, fcNormal):
1229 sign = rhs.sign ^ subtract;
1232 case convolve(fcZero, fcZero):
1233 /* Sign depends on rounding mode; handled by caller. */
1236 case convolve(fcInfinity, fcInfinity):
1237 /* Differently signed infinities can only be validly
1239 if((sign ^ rhs.sign) != subtract) {
1246 case convolve(fcNormal, fcNormal):
1251 /* Add or subtract two normal numbers. */
1253 APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
1256 lostFraction lost_fraction;
1259 /* Determine if the operation on the absolute values is effectively
1260 an addition or subtraction. */
1261 subtract ^= (sign ^ rhs.sign) ? true : false;
1263 /* Are we bigger exponent-wise than the RHS? */
1264 bits = exponent - rhs.exponent;
1266 /* Subtraction is more subtle than one might naively expect. */
1268 APFloat temp_rhs(rhs);
1272 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1273 lost_fraction = lfExactlyZero;
1274 } else if (bits > 0) {
1275 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1276 shiftSignificandLeft(1);
1279 lost_fraction = shiftSignificandRight(-bits - 1);
1280 temp_rhs.shiftSignificandLeft(1);
1285 carry = temp_rhs.subtractSignificand
1286 (*this, lost_fraction != lfExactlyZero);
1287 copySignificand(temp_rhs);
1290 carry = subtractSignificand
1291 (temp_rhs, lost_fraction != lfExactlyZero);
1294 /* Invert the lost fraction - it was on the RHS and
1296 if(lost_fraction == lfLessThanHalf)
1297 lost_fraction = lfMoreThanHalf;
1298 else if(lost_fraction == lfMoreThanHalf)
1299 lost_fraction = lfLessThanHalf;
1301 /* The code above is intended to ensure that no borrow is
1306 APFloat temp_rhs(rhs);
1308 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1309 carry = addSignificand(temp_rhs);
1311 lost_fraction = shiftSignificandRight(-bits);
1312 carry = addSignificand(rhs);
1315 /* We have a guard bit; generating a carry cannot happen. */
1319 return lost_fraction;
1323 APFloat::multiplySpecials(const APFloat &rhs)
1325 switch(convolve(category, rhs.category)) {
1329 case convolve(fcNaN, fcZero):
1330 case convolve(fcNaN, fcNormal):
1331 case convolve(fcNaN, fcInfinity):
1332 case convolve(fcNaN, fcNaN):
1335 case convolve(fcZero, fcNaN):
1336 case convolve(fcNormal, fcNaN):
1337 case convolve(fcInfinity, fcNaN):
1339 copySignificand(rhs);
1342 case convolve(fcNormal, fcInfinity):
1343 case convolve(fcInfinity, fcNormal):
1344 case convolve(fcInfinity, fcInfinity):
1345 category = fcInfinity;
1348 case convolve(fcZero, fcNormal):
1349 case convolve(fcNormal, fcZero):
1350 case convolve(fcZero, fcZero):
1354 case convolve(fcZero, fcInfinity):
1355 case convolve(fcInfinity, fcZero):
1359 case convolve(fcNormal, fcNormal):
1365 APFloat::divideSpecials(const APFloat &rhs)
1367 switch(convolve(category, rhs.category)) {
1371 case convolve(fcNaN, fcZero):
1372 case convolve(fcNaN, fcNormal):
1373 case convolve(fcNaN, fcInfinity):
1374 case convolve(fcNaN, fcNaN):
1375 case convolve(fcInfinity, fcZero):
1376 case convolve(fcInfinity, fcNormal):
1377 case convolve(fcZero, fcInfinity):
1378 case convolve(fcZero, fcNormal):
1381 case convolve(fcZero, fcNaN):
1382 case convolve(fcNormal, fcNaN):
1383 case convolve(fcInfinity, fcNaN):
1385 copySignificand(rhs);
1388 case convolve(fcNormal, fcInfinity):
1392 case convolve(fcNormal, fcZero):
1393 category = fcInfinity;
1396 case convolve(fcInfinity, fcInfinity):
1397 case convolve(fcZero, fcZero):
1401 case convolve(fcNormal, fcNormal):
1408 APFloat::changeSign()
1410 /* Look mummy, this one's easy. */
1415 APFloat::clearSign()
1417 /* So is this one. */
1422 APFloat::copySign(const APFloat &rhs)
1428 /* Normalized addition or subtraction. */
1430 APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
1435 assertArithmeticOK(*semantics);
1437 fs = addOrSubtractSpecials(rhs, subtract);
1439 /* This return code means it was not a simple case. */
1440 if(fs == opDivByZero) {
1441 lostFraction lost_fraction;
1443 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1444 fs = normalize(rounding_mode, lost_fraction);
1446 /* Can only be zero if we lost no fraction. */
1447 assert(category != fcZero || lost_fraction == lfExactlyZero);
1450 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1451 positive zero unless rounding to minus infinity, except that
1452 adding two like-signed zeroes gives that zero. */
1453 if(category == fcZero) {
1454 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1455 sign = (rounding_mode == rmTowardNegative);
1461 /* Normalized addition. */
1463 APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1465 return addOrSubtract(rhs, rounding_mode, false);
1468 /* Normalized subtraction. */
1470 APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1472 return addOrSubtract(rhs, rounding_mode, true);
1475 /* Normalized multiply. */
1477 APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1481 assertArithmeticOK(*semantics);
1483 fs = multiplySpecials(rhs);
1485 if(category == fcNormal) {
1486 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1487 fs = normalize(rounding_mode, lost_fraction);
1488 if(lost_fraction != lfExactlyZero)
1489 fs = (opStatus) (fs | opInexact);
1495 /* Normalized divide. */
1497 APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1501 assertArithmeticOK(*semantics);
1503 fs = divideSpecials(rhs);
1505 if(category == fcNormal) {
1506 lostFraction lost_fraction = divideSignificand(rhs);
1507 fs = normalize(rounding_mode, lost_fraction);
1508 if(lost_fraction != lfExactlyZero)
1509 fs = (opStatus) (fs | opInexact);
1515 /* Normalized remainder. This is not currently doing TRT. */
1517 APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1521 unsigned int origSign = sign;
1523 assertArithmeticOK(*semantics);
1524 fs = V.divide(rhs, rmNearestTiesToEven);
1525 if (fs == opDivByZero)
1528 int parts = partCount();
1529 integerPart *x = new integerPart[parts];
1530 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1531 rmNearestTiesToEven);
1532 if (fs==opInvalidOp)
1535 fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true,
1536 rmNearestTiesToEven);
1537 assert(fs==opOK); // should always work
1539 fs = V.multiply(rhs, rounding_mode);
1540 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1542 fs = subtract(V, rounding_mode);
1543 assert(fs==opOK || fs==opInexact); // likewise
1546 sign = origSign; // IEEE754 requires this
1551 /* Normalized fused-multiply-add. */
1553 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
1554 const APFloat &addend,
1555 roundingMode rounding_mode)
1559 assertArithmeticOK(*semantics);
1561 /* Post-multiplication sign, before addition. */
1562 sign ^= multiplicand.sign;
1564 /* If and only if all arguments are normal do we need to do an
1565 extended-precision calculation. */
1566 if(category == fcNormal
1567 && multiplicand.category == fcNormal
1568 && addend.category == fcNormal) {
1569 lostFraction lost_fraction;
1571 lost_fraction = multiplySignificand(multiplicand, &addend);
1572 fs = normalize(rounding_mode, lost_fraction);
1573 if(lost_fraction != lfExactlyZero)
1574 fs = (opStatus) (fs | opInexact);
1576 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1577 positive zero unless rounding to minus infinity, except that
1578 adding two like-signed zeroes gives that zero. */
1579 if(category == fcZero && sign != addend.sign)
1580 sign = (rounding_mode == rmTowardNegative);
1582 fs = multiplySpecials(multiplicand);
1584 /* FS can only be opOK or opInvalidOp. There is no more work
1585 to do in the latter case. The IEEE-754R standard says it is
1586 implementation-defined in this case whether, if ADDEND is a
1587 quiet NaN, we raise invalid op; this implementation does so.
1589 If we need to do the addition we can do so with normal
1592 fs = addOrSubtract(addend, rounding_mode, false);
1598 /* Comparison requires normalized numbers. */
1600 APFloat::compare(const APFloat &rhs) const
1604 assertArithmeticOK(*semantics);
1605 assert(semantics == rhs.semantics);
1607 switch(convolve(category, rhs.category)) {
1611 case convolve(fcNaN, fcZero):
1612 case convolve(fcNaN, fcNormal):
1613 case convolve(fcNaN, fcInfinity):
1614 case convolve(fcNaN, fcNaN):
1615 case convolve(fcZero, fcNaN):
1616 case convolve(fcNormal, fcNaN):
1617 case convolve(fcInfinity, fcNaN):
1618 return cmpUnordered;
1620 case convolve(fcInfinity, fcNormal):
1621 case convolve(fcInfinity, fcZero):
1622 case convolve(fcNormal, fcZero):
1626 return cmpGreaterThan;
1628 case convolve(fcNormal, fcInfinity):
1629 case convolve(fcZero, fcInfinity):
1630 case convolve(fcZero, fcNormal):
1632 return cmpGreaterThan;
1636 case convolve(fcInfinity, fcInfinity):
1637 if(sign == rhs.sign)
1642 return cmpGreaterThan;
1644 case convolve(fcZero, fcZero):
1647 case convolve(fcNormal, fcNormal):
1651 /* Two normal numbers. Do they have the same sign? */
1652 if(sign != rhs.sign) {
1654 result = cmpLessThan;
1656 result = cmpGreaterThan;
1658 /* Compare absolute values; invert result if negative. */
1659 result = compareAbsoluteValue(rhs);
1662 if(result == cmpLessThan)
1663 result = cmpGreaterThan;
1664 else if(result == cmpGreaterThan)
1665 result = cmpLessThan;
1673 APFloat::convert(const fltSemantics &toSemantics,
1674 roundingMode rounding_mode)
1676 lostFraction lostFraction;
1677 unsigned int newPartCount, oldPartCount;
1680 assertArithmeticOK(*semantics);
1681 lostFraction = lfExactlyZero;
1682 newPartCount = partCountForBits(toSemantics.precision + 1);
1683 oldPartCount = partCount();
1685 /* Handle storage complications. If our new form is wider,
1686 re-allocate our bit pattern into wider storage. If it is
1687 narrower, we ignore the excess parts, but if narrowing to a
1688 single part we need to free the old storage.
1689 Be careful not to reference significandParts for zeroes
1690 and infinities, since it aborts. */
1691 if (newPartCount > oldPartCount) {
1692 integerPart *newParts;
1693 newParts = new integerPart[newPartCount];
1694 APInt::tcSet(newParts, 0, newPartCount);
1695 if (category==fcNormal || category==fcNaN)
1696 APInt::tcAssign(newParts, significandParts(), oldPartCount);
1698 significand.parts = newParts;
1699 } else if (newPartCount < oldPartCount) {
1700 /* Capture any lost fraction through truncation of parts so we get
1701 correct rounding whilst normalizing. */
1702 if (category==fcNormal)
1703 lostFraction = lostFractionThroughTruncation
1704 (significandParts(), oldPartCount, toSemantics.precision);
1705 if (newPartCount == 1) {
1706 integerPart newPart = 0;
1707 if (category==fcNormal || category==fcNaN)
1708 newPart = significandParts()[0];
1710 significand.part = newPart;
1714 if(category == fcNormal) {
1715 /* Re-interpret our bit-pattern. */
1716 exponent += toSemantics.precision - semantics->precision;
1717 semantics = &toSemantics;
1718 fs = normalize(rounding_mode, lostFraction);
1719 } else if (category == fcNaN) {
1720 int shift = toSemantics.precision - semantics->precision;
1721 // Do this now so significandParts gets the right answer
1722 semantics = &toSemantics;
1723 // No normalization here, just truncate
1725 APInt::tcShiftLeft(significandParts(), newPartCount, shift);
1727 APInt::tcShiftRight(significandParts(), newPartCount, -shift);
1728 // gcc forces the Quiet bit on, which means (float)(double)(float_sNan)
1729 // does not give you back the same bits. This is dubious, and we
1730 // don't currently do it. You're really supposed to get
1731 // an invalid operation signal at runtime, but nobody does that.
1734 semantics = &toSemantics;
1741 /* Convert a floating point number to an integer according to the
1742 rounding mode. If the rounded integer value is out of range this
1743 returns an invalid operation exception and the contents of the
1744 destination parts are unspecified. If the rounded value is in
1745 range but the floating point number is not the exact integer, the C
1746 standard doesn't require an inexact exception to be raised. IEEE
1747 854 does require it so we do that.
1749 Note that for conversions to integer type the C standard requires
1750 round-to-zero to always be used. */
1752 APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
1754 roundingMode rounding_mode) const
1756 lostFraction lost_fraction;
1757 const integerPart *src;
1758 unsigned int dstPartsCount, truncatedBits;
1760 assertArithmeticOK(*semantics);
1762 /* Handle the three special cases first. */
1763 if(category == fcInfinity || category == fcNaN)
1766 dstPartsCount = partCountForBits(width);
1768 if(category == fcZero) {
1769 APInt::tcSet(parts, 0, dstPartsCount);
1773 src = significandParts();
1775 /* Step 1: place our absolute value, with any fraction truncated, in
1778 /* Our absolute value is less than one; truncate everything. */
1779 APInt::tcSet(parts, 0, dstPartsCount);
1780 truncatedBits = semantics->precision;
1782 /* We want the most significant (exponent + 1) bits; the rest are
1784 unsigned int bits = exponent + 1U;
1786 /* Hopelessly large in magnitude? */
1790 if (bits < semantics->precision) {
1791 /* We truncate (semantics->precision - bits) bits. */
1792 truncatedBits = semantics->precision - bits;
1793 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
1795 /* We want at least as many bits as are available. */
1796 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
1797 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
1802 /* Step 2: work out any lost fraction, and increment the absolute
1803 value if we would round away from zero. */
1804 if (truncatedBits) {
1805 lost_fraction = lostFractionThroughTruncation(src, partCount(),
1807 if (lost_fraction != lfExactlyZero
1808 && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
1809 if (APInt::tcIncrement(parts, dstPartsCount))
1810 return opInvalidOp; /* Overflow. */
1813 lost_fraction = lfExactlyZero;
1816 /* Step 3: check if we fit in the destination. */
1817 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
1821 /* Negative numbers cannot be represented as unsigned. */
1825 /* It takes omsb bits to represent the unsigned integer value.
1826 We lose a bit for the sign, but care is needed as the
1827 maximally negative integer is a special case. */
1828 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
1831 /* This case can happen because of rounding. */
1836 APInt::tcNegate (parts, dstPartsCount);
1838 if (omsb >= width + !isSigned)
1842 if (lost_fraction == lfExactlyZero)
1848 /* Same as convertToSignExtendedInteger, except we provide
1849 deterministic values in case of an invalid operation exception,
1850 namely zero for NaNs and the minimal or maximal value respectively
1851 for underflow or overflow. */
1853 APFloat::convertToInteger(integerPart *parts, unsigned int width,
1855 roundingMode rounding_mode) const
1859 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode);
1861 if (fs == opInvalidOp) {
1862 unsigned int bits, dstPartsCount;
1864 dstPartsCount = partCountForBits(width);
1866 if (category == fcNaN)
1871 bits = width - isSigned;
1873 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
1874 if (sign && isSigned)
1875 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
1881 /* Convert an unsigned integer SRC to a floating point number,
1882 rounding according to ROUNDING_MODE. The sign of the floating
1883 point number is not modified. */
1885 APFloat::convertFromUnsignedParts(const integerPart *src,
1886 unsigned int srcCount,
1887 roundingMode rounding_mode)
1889 unsigned int omsb, precision, dstCount;
1891 lostFraction lost_fraction;
1893 assertArithmeticOK(*semantics);
1894 category = fcNormal;
1895 omsb = APInt::tcMSB(src, srcCount) + 1;
1896 dst = significandParts();
1897 dstCount = partCount();
1898 precision = semantics->precision;
1900 /* We want the most significant PRECISON bits of SRC. There may not
1901 be that many; extract what we can. */
1902 if (precision <= omsb) {
1903 exponent = omsb - 1;
1904 lost_fraction = lostFractionThroughTruncation(src, srcCount,
1906 APInt::tcExtract(dst, dstCount, src, precision, omsb - precision);
1908 exponent = precision - 1;
1909 lost_fraction = lfExactlyZero;
1910 APInt::tcExtract(dst, dstCount, src, omsb, 0);
1913 return normalize(rounding_mode, lost_fraction);
1917 APFloat::convertFromAPInt(const APInt &Val,
1919 roundingMode rounding_mode)
1921 unsigned int partCount = Val.getNumWords();
1925 if (isSigned && api.isNegative()) {
1930 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
1933 /* Convert a two's complement integer SRC to a floating point number,
1934 rounding according to ROUNDING_MODE. ISSIGNED is true if the
1935 integer is signed, in which case it must be sign-extended. */
1937 APFloat::convertFromSignExtendedInteger(const integerPart *src,
1938 unsigned int srcCount,
1940 roundingMode rounding_mode)
1944 assertArithmeticOK(*semantics);
1946 && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) {
1949 /* If we're signed and negative negate a copy. */
1951 copy = new integerPart[srcCount];
1952 APInt::tcAssign(copy, src, srcCount);
1953 APInt::tcNegate(copy, srcCount);
1954 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
1958 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
1964 /* FIXME: should this just take a const APInt reference? */
1966 APFloat::convertFromZeroExtendedInteger(const integerPart *parts,
1967 unsigned int width, bool isSigned,
1968 roundingMode rounding_mode)
1970 unsigned int partCount = partCountForBits(width);
1971 APInt api = APInt(width, partCount, parts);
1974 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
1979 return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode);
1983 APFloat::convertFromHexadecimalString(const char *p,
1984 roundingMode rounding_mode)
1986 lostFraction lost_fraction;
1987 integerPart *significand;
1988 unsigned int bitPos, partsCount;
1989 const char *dot, *firstSignificantDigit;
1993 category = fcNormal;
1995 significand = significandParts();
1996 partsCount = partCount();
1997 bitPos = partsCount * integerPartWidth;
1999 /* Skip leading zeroes and any (hexa)decimal point. */
2000 p = skipLeadingZeroesAndAnyDot(p, &dot);
2001 firstSignificantDigit = p;
2004 integerPart hex_value;
2011 hex_value = hexDigitValue(*p);
2012 if(hex_value == -1U) {
2013 lost_fraction = lfExactlyZero;
2019 /* Store the number whilst 4-bit nibbles remain. */
2022 hex_value <<= bitPos % integerPartWidth;
2023 significand[bitPos / integerPartWidth] |= hex_value;
2025 lost_fraction = trailingHexadecimalFraction(p, hex_value);
2026 while(hexDigitValue(*p) != -1U)
2032 /* Hex floats require an exponent but not a hexadecimal point. */
2033 assert(*p == 'p' || *p == 'P');
2035 /* Ignore the exponent if we are zero. */
2036 if(p != firstSignificantDigit) {
2039 /* Implicit hexadecimal point? */
2043 /* Calculate the exponent adjustment implicit in the number of
2044 significant digits. */
2045 expAdjustment = dot - firstSignificantDigit;
2046 if(expAdjustment < 0)
2048 expAdjustment = expAdjustment * 4 - 1;
2050 /* Adjust for writing the significand starting at the most
2051 significant nibble. */
2052 expAdjustment += semantics->precision;
2053 expAdjustment -= partsCount * integerPartWidth;
2055 /* Adjust for the given exponent. */
2056 exponent = totalExponent(p, expAdjustment);
2059 return normalize(rounding_mode, lost_fraction);
2063 APFloat::roundSignificandWithExponent(const integerPart *decSigParts,
2064 unsigned sigPartCount, int exp,
2065 roundingMode rounding_mode)
2067 unsigned int parts, pow5PartCount;
2068 fltSemantics calcSemantics = { 32767, -32767, 0, true };
2069 integerPart pow5Parts[maxPowerOfFiveParts];
2072 isNearest = (rounding_mode == rmNearestTiesToEven
2073 || rounding_mode == rmNearestTiesToAway);
2075 parts = partCountForBits(semantics->precision + 11);
2077 /* Calculate pow(5, abs(exp)). */
2078 pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2080 for (;; parts *= 2) {
2081 opStatus sigStatus, powStatus;
2082 unsigned int excessPrecision, truncatedBits;
2084 calcSemantics.precision = parts * integerPartWidth - 1;
2085 excessPrecision = calcSemantics.precision - semantics->precision;
2086 truncatedBits = excessPrecision;
2088 APFloat decSig(calcSemantics, fcZero, sign);
2089 APFloat pow5(calcSemantics, fcZero, false);
2091 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2092 rmNearestTiesToEven);
2093 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2094 rmNearestTiesToEven);
2095 /* Add exp, as 10^n = 5^n * 2^n. */
2096 decSig.exponent += exp;
2098 lostFraction calcLostFraction;
2099 integerPart HUerr, HUdistance, powHUerr;
2102 /* multiplySignificand leaves the precision-th bit set to 1. */
2103 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2104 powHUerr = powStatus != opOK;
2106 calcLostFraction = decSig.divideSignificand(pow5);
2107 /* Denormal numbers have less precision. */
2108 if (decSig.exponent < semantics->minExponent) {
2109 excessPrecision += (semantics->minExponent - decSig.exponent);
2110 truncatedBits = excessPrecision;
2111 if (excessPrecision > calcSemantics.precision)
2112 excessPrecision = calcSemantics.precision;
2114 /* Extra half-ulp lost in reciprocal of exponent. */
2115 powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0: 2;
2118 /* Both multiplySignificand and divideSignificand return the
2119 result with the integer bit set. */
2120 assert (APInt::tcExtractBit
2121 (decSig.significandParts(), calcSemantics.precision - 1) == 1);
2123 HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK,
2125 HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(),
2126 excessPrecision, isNearest);
2128 /* Are we guaranteed to round correctly if we truncate? */
2129 if (HUdistance >= HUerr) {
2130 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2131 calcSemantics.precision - excessPrecision,
2133 /* Take the exponent of decSig. If we tcExtract-ed less bits
2134 above we must adjust our exponent to compensate for the
2135 implicit right shift. */
2136 exponent = (decSig.exponent + semantics->precision
2137 - (calcSemantics.precision - excessPrecision));
2138 calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(),
2141 return normalize(rounding_mode, calcLostFraction);
2147 APFloat::convertFromDecimalString(const char *p, roundingMode rounding_mode)
2152 /* Scan the text. */
2153 interpretDecimal(p, &D);
2155 /* Handle the quick cases. First the case of no significant digits,
2156 i.e. zero, and then exponents that are obviously too large or too
2157 small. Writing L for log 10 / log 2, a number d.ddddd*10^exp
2158 definitely overflows if
2160 (exp - 1) * L >= maxExponent
2162 and definitely underflows to zero where
2164 (exp + 1) * L <= minExponent - precision
2166 With integer arithmetic the tightest bounds for L are
2168 93/28 < L < 196/59 [ numerator <= 256 ]
2169 42039/12655 < L < 28738/8651 [ numerator <= 65536 ]
2172 if (decDigitValue(*D.firstSigDigit) >= 10U) {
2175 } else if ((D.normalizedExponent + 1) * 28738
2176 <= 8651 * (semantics->minExponent - (int) semantics->precision)) {
2177 /* Underflow to zero and round. */
2179 fs = normalize(rounding_mode, lfLessThanHalf);
2180 } else if ((D.normalizedExponent - 1) * 42039
2181 >= 12655 * semantics->maxExponent) {
2182 /* Overflow and round. */
2183 fs = handleOverflow(rounding_mode);
2185 integerPart *decSignificand;
2186 unsigned int partCount;
2188 /* A tight upper bound on number of bits required to hold an
2189 N-digit decimal integer is N * 196 / 59. Allocate enough space
2190 to hold the full significand, and an extra part required by
2192 partCount = (D.lastSigDigit - D.firstSigDigit) + 1;
2193 partCount = partCountForBits(1 + 196 * partCount / 59);
2194 decSignificand = new integerPart[partCount + 1];
2197 /* Convert to binary efficiently - we do almost all multiplication
2198 in an integerPart. When this would overflow do we do a single
2199 bignum multiplication, and then revert again to multiplication
2200 in an integerPart. */
2202 integerPart decValue, val, multiplier;
2211 decValue = decDigitValue(*p++);
2213 val = val * 10 + decValue;
2214 /* The maximum number that can be multiplied by ten with any
2215 digit added without overflowing an integerPart. */
2216 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2218 /* Multiply out the current part. */
2219 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2220 partCount, partCount + 1, false);
2222 /* If we used another part (likely but not guaranteed), increase
2224 if (decSignificand[partCount])
2226 } while (p <= D.lastSigDigit);
2228 category = fcNormal;
2229 fs = roundSignificandWithExponent(decSignificand, partCount,
2230 D.exponent, rounding_mode);
2232 delete [] decSignificand;
2239 APFloat::convertFromString(const char *p, roundingMode rounding_mode)
2241 assertArithmeticOK(*semantics);
2243 /* Handle a leading minus sign. */
2249 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
2250 return convertFromHexadecimalString(p + 2, rounding_mode);
2252 return convertFromDecimalString(p, rounding_mode);
2255 /* Write out a hexadecimal representation of the floating point value
2256 to DST, which must be of sufficient size, in the C99 form
2257 [-]0xh.hhhhp[+-]d. Return the number of characters written,
2258 excluding the terminating NUL.
2260 If UPPERCASE, the output is in upper case, otherwise in lower case.
2262 HEXDIGITS digits appear altogether, rounding the value if
2263 necessary. If HEXDIGITS is 0, the minimal precision to display the
2264 number precisely is used instead. If nothing would appear after
2265 the decimal point it is suppressed.
2267 The decimal exponent is always printed and has at least one digit.
2268 Zero values display an exponent of zero. Infinities and NaNs
2269 appear as "infinity" or "nan" respectively.
2271 The above rules are as specified by C99. There is ambiguity about
2272 what the leading hexadecimal digit should be. This implementation
2273 uses whatever is necessary so that the exponent is displayed as
2274 stored. This implies the exponent will fall within the IEEE format
2275 range, and the leading hexadecimal digit will be 0 (for denormals),
2276 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with
2277 any other digits zero).
2280 APFloat::convertToHexString(char *dst, unsigned int hexDigits,
2281 bool upperCase, roundingMode rounding_mode) const
2285 assertArithmeticOK(*semantics);
2293 memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1);
2294 dst += sizeof infinityL - 1;
2298 memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1);
2299 dst += sizeof NaNU - 1;
2304 *dst++ = upperCase ? 'X': 'x';
2306 if (hexDigits > 1) {
2308 memset (dst, '0', hexDigits - 1);
2309 dst += hexDigits - 1;
2311 *dst++ = upperCase ? 'P': 'p';
2316 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2325 /* Does the hard work of outputting the correctly rounded hexadecimal
2326 form of a normal floating point number with the specified number of
2327 hexadecimal digits. If HEXDIGITS is zero the minimum number of
2328 digits necessary to print the value precisely is output. */
2330 APFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
2332 roundingMode rounding_mode) const
2334 unsigned int count, valueBits, shift, partsCount, outputDigits;
2335 const char *hexDigitChars;
2336 const integerPart *significand;
2341 *dst++ = upperCase ? 'X': 'x';
2344 hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower;
2346 significand = significandParts();
2347 partsCount = partCount();
2349 /* +3 because the first digit only uses the single integer bit, so
2350 we have 3 virtual zero most-significant-bits. */
2351 valueBits = semantics->precision + 3;
2352 shift = integerPartWidth - valueBits % integerPartWidth;
2354 /* The natural number of digits required ignoring trailing
2355 insignificant zeroes. */
2356 outputDigits = (valueBits - significandLSB () + 3) / 4;
2358 /* hexDigits of zero means use the required number for the
2359 precision. Otherwise, see if we are truncating. If we are,
2360 find out if we need to round away from zero. */
2362 if (hexDigits < outputDigits) {
2363 /* We are dropping non-zero bits, so need to check how to round.
2364 "bits" is the number of dropped bits. */
2366 lostFraction fraction;
2368 bits = valueBits - hexDigits * 4;
2369 fraction = lostFractionThroughTruncation (significand, partsCount, bits);
2370 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2372 outputDigits = hexDigits;
2375 /* Write the digits consecutively, and start writing in the location
2376 of the hexadecimal point. We move the most significant digit
2377 left and add the hexadecimal point later. */
2380 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2382 while (outputDigits && count) {
2385 /* Put the most significant integerPartWidth bits in "part". */
2386 if (--count == partsCount)
2387 part = 0; /* An imaginary higher zero part. */
2389 part = significand[count] << shift;
2392 part |= significand[count - 1] >> (integerPartWidth - shift);
2394 /* Convert as much of "part" to hexdigits as we can. */
2395 unsigned int curDigits = integerPartWidth / 4;
2397 if (curDigits > outputDigits)
2398 curDigits = outputDigits;
2399 dst += partAsHex (dst, part, curDigits, hexDigitChars);
2400 outputDigits -= curDigits;
2406 /* Note that hexDigitChars has a trailing '0'. */
2409 *q = hexDigitChars[hexDigitValue (*q) + 1];
2410 } while (*q == '0');
2413 /* Add trailing zeroes. */
2414 memset (dst, '0', outputDigits);
2415 dst += outputDigits;
2418 /* Move the most significant digit to before the point, and if there
2419 is something after the decimal point add it. This must come
2420 after rounding above. */
2427 /* Finally output the exponent. */
2428 *dst++ = upperCase ? 'P': 'p';
2430 return writeSignedDecimal (dst, exponent);
2433 // For good performance it is desirable for different APFloats
2434 // to produce different integers.
2436 APFloat::getHashValue() const
2438 if (category==fcZero) return sign<<8 | semantics->precision ;
2439 else if (category==fcInfinity) return sign<<9 | semantics->precision;
2440 else if (category==fcNaN) return 1<<10 | semantics->precision;
2442 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
2443 const integerPart* p = significandParts();
2444 for (int i=partCount(); i>0; i--, p++)
2445 hash ^= ((uint32_t)*p) ^ (*p)>>32;
2450 // Conversion from APFloat to/from host float/double. It may eventually be
2451 // possible to eliminate these and have everybody deal with APFloats, but that
2452 // will take a while. This approach will not easily extend to long double.
2453 // Current implementation requires integerPartWidth==64, which is correct at
2454 // the moment but could be made more general.
2456 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
2457 // the actual IEEE respresentations. We compensate for that here.
2460 APFloat::convertF80LongDoubleAPFloatToAPInt() const
2462 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended);
2463 assert (partCount()==2);
2465 uint64_t myexponent, mysignificand;
2467 if (category==fcNormal) {
2468 myexponent = exponent+16383; //bias
2469 mysignificand = significandParts()[0];
2470 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2471 myexponent = 0; // denormal
2472 } else if (category==fcZero) {
2475 } else if (category==fcInfinity) {
2476 myexponent = 0x7fff;
2477 mysignificand = 0x8000000000000000ULL;
2479 assert(category == fcNaN && "Unknown category");
2480 myexponent = 0x7fff;
2481 mysignificand = significandParts()[0];
2485 words[0] = (((uint64_t)sign & 1) << 63) |
2486 ((myexponent & 0x7fff) << 48) |
2487 ((mysignificand >>16) & 0xffffffffffffLL);
2488 words[1] = mysignificand & 0xffff;
2489 return APInt(80, 2, words);
2493 APFloat::convertPPCDoubleDoubleAPFloatToAPInt() const
2495 assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble);
2496 assert (partCount()==2);
2498 uint64_t myexponent, mysignificand, myexponent2, mysignificand2;
2500 if (category==fcNormal) {
2501 myexponent = exponent + 1023; //bias
2502 myexponent2 = exponent2 + 1023;
2503 mysignificand = significandParts()[0];
2504 mysignificand2 = significandParts()[1];
2505 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2506 myexponent = 0; // denormal
2507 if (myexponent2==1 && !(mysignificand2 & 0x10000000000000LL))
2508 myexponent2 = 0; // denormal
2509 } else if (category==fcZero) {
2514 } else if (category==fcInfinity) {
2520 assert(category == fcNaN && "Unknown category");
2522 mysignificand = significandParts()[0];
2523 myexponent2 = exponent2;
2524 mysignificand2 = significandParts()[1];
2528 words[0] = (((uint64_t)sign & 1) << 63) |
2529 ((myexponent & 0x7ff) << 52) |
2530 (mysignificand & 0xfffffffffffffLL);
2531 words[1] = (((uint64_t)sign2 & 1) << 63) |
2532 ((myexponent2 & 0x7ff) << 52) |
2533 (mysignificand2 & 0xfffffffffffffLL);
2534 return APInt(128, 2, words);
2538 APFloat::convertDoubleAPFloatToAPInt() const
2540 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
2541 assert (partCount()==1);
2543 uint64_t myexponent, mysignificand;
2545 if (category==fcNormal) {
2546 myexponent = exponent+1023; //bias
2547 mysignificand = *significandParts();
2548 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2549 myexponent = 0; // denormal
2550 } else if (category==fcZero) {
2553 } else if (category==fcInfinity) {
2557 assert(category == fcNaN && "Unknown category!");
2559 mysignificand = *significandParts();
2562 return APInt(64, (((((uint64_t)sign & 1) << 63) |
2563 ((myexponent & 0x7ff) << 52) |
2564 (mysignificand & 0xfffffffffffffLL))));
2568 APFloat::convertFloatAPFloatToAPInt() const
2570 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
2571 assert (partCount()==1);
2573 uint32_t myexponent, mysignificand;
2575 if (category==fcNormal) {
2576 myexponent = exponent+127; //bias
2577 mysignificand = *significandParts();
2578 if (myexponent == 1 && !(mysignificand & 0x800000))
2579 myexponent = 0; // denormal
2580 } else if (category==fcZero) {
2583 } else if (category==fcInfinity) {
2587 assert(category == fcNaN && "Unknown category!");
2589 mysignificand = *significandParts();
2592 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2593 (mysignificand & 0x7fffff)));
2596 // This function creates an APInt that is just a bit map of the floating
2597 // point constant as it would appear in memory. It is not a conversion,
2598 // and treating the result as a normal integer is unlikely to be useful.
2601 APFloat::convertToAPInt() const
2603 if (semantics == (const llvm::fltSemantics*)&IEEEsingle)
2604 return convertFloatAPFloatToAPInt();
2606 if (semantics == (const llvm::fltSemantics*)&IEEEdouble)
2607 return convertDoubleAPFloatToAPInt();
2609 if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble)
2610 return convertPPCDoubleDoubleAPFloatToAPInt();
2612 assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended &&
2614 return convertF80LongDoubleAPFloatToAPInt();
2618 APFloat::convertToFloat() const
2620 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
2621 APInt api = convertToAPInt();
2622 return api.bitsToFloat();
2626 APFloat::convertToDouble() const
2628 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
2629 APInt api = convertToAPInt();
2630 return api.bitsToDouble();
2633 /// Integer bit is explicit in this format. Current Intel book does not
2634 /// define meaning of:
2635 /// exponent = all 1's, integer bit not set.
2636 /// exponent = 0, integer bit set. (formerly "psuedodenormals")
2637 /// exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
2639 APFloat::initFromF80LongDoubleAPInt(const APInt &api)
2641 assert(api.getBitWidth()==80);
2642 uint64_t i1 = api.getRawData()[0];
2643 uint64_t i2 = api.getRawData()[1];
2644 uint64_t myexponent = (i1 >> 48) & 0x7fff;
2645 uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
2648 initialize(&APFloat::x87DoubleExtended);
2649 assert(partCount()==2);
2652 if (myexponent==0 && mysignificand==0) {
2653 // exponent, significand meaningless
2655 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
2656 // exponent, significand meaningless
2657 category = fcInfinity;
2658 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
2659 // exponent meaningless
2661 significandParts()[0] = mysignificand;
2662 significandParts()[1] = 0;
2664 category = fcNormal;
2665 exponent = myexponent - 16383;
2666 significandParts()[0] = mysignificand;
2667 significandParts()[1] = 0;
2668 if (myexponent==0) // denormal
2674 APFloat::initFromPPCDoubleDoubleAPInt(const APInt &api)
2676 assert(api.getBitWidth()==128);
2677 uint64_t i1 = api.getRawData()[0];
2678 uint64_t i2 = api.getRawData()[1];
2679 uint64_t myexponent = (i1 >> 52) & 0x7ff;
2680 uint64_t mysignificand = i1 & 0xfffffffffffffLL;
2681 uint64_t myexponent2 = (i2 >> 52) & 0x7ff;
2682 uint64_t mysignificand2 = i2 & 0xfffffffffffffLL;
2684 initialize(&APFloat::PPCDoubleDouble);
2685 assert(partCount()==2);
2689 if (myexponent==0 && mysignificand==0) {
2690 // exponent, significand meaningless
2691 // exponent2 and significand2 are required to be 0; we don't check
2693 } else if (myexponent==0x7ff && mysignificand==0) {
2694 // exponent, significand meaningless
2695 // exponent2 and significand2 are required to be 0; we don't check
2696 category = fcInfinity;
2697 } else if (myexponent==0x7ff && mysignificand!=0) {
2698 // exponent meaningless. So is the whole second word, but keep it
2701 exponent2 = myexponent2;
2702 significandParts()[0] = mysignificand;
2703 significandParts()[1] = mysignificand2;
2705 category = fcNormal;
2706 // Note there is no category2; the second word is treated as if it is
2707 // fcNormal, although it might be something else considered by itself.
2708 exponent = myexponent - 1023;
2709 exponent2 = myexponent2 - 1023;
2710 significandParts()[0] = mysignificand;
2711 significandParts()[1] = mysignificand2;
2712 if (myexponent==0) // denormal
2715 significandParts()[0] |= 0x10000000000000LL; // integer bit
2719 significandParts()[1] |= 0x10000000000000LL; // integer bit
2724 APFloat::initFromDoubleAPInt(const APInt &api)
2726 assert(api.getBitWidth()==64);
2727 uint64_t i = *api.getRawData();
2728 uint64_t myexponent = (i >> 52) & 0x7ff;
2729 uint64_t mysignificand = i & 0xfffffffffffffLL;
2731 initialize(&APFloat::IEEEdouble);
2732 assert(partCount()==1);
2735 if (myexponent==0 && mysignificand==0) {
2736 // exponent, significand meaningless
2738 } else if (myexponent==0x7ff && mysignificand==0) {
2739 // exponent, significand meaningless
2740 category = fcInfinity;
2741 } else if (myexponent==0x7ff && mysignificand!=0) {
2742 // exponent meaningless
2744 *significandParts() = mysignificand;
2746 category = fcNormal;
2747 exponent = myexponent - 1023;
2748 *significandParts() = mysignificand;
2749 if (myexponent==0) // denormal
2752 *significandParts() |= 0x10000000000000LL; // integer bit
2757 APFloat::initFromFloatAPInt(const APInt & api)
2759 assert(api.getBitWidth()==32);
2760 uint32_t i = (uint32_t)*api.getRawData();
2761 uint32_t myexponent = (i >> 23) & 0xff;
2762 uint32_t mysignificand = i & 0x7fffff;
2764 initialize(&APFloat::IEEEsingle);
2765 assert(partCount()==1);
2768 if (myexponent==0 && mysignificand==0) {
2769 // exponent, significand meaningless
2771 } else if (myexponent==0xff && mysignificand==0) {
2772 // exponent, significand meaningless
2773 category = fcInfinity;
2774 } else if (myexponent==0xff && mysignificand!=0) {
2775 // sign, exponent, significand meaningless
2777 *significandParts() = mysignificand;
2779 category = fcNormal;
2780 exponent = myexponent - 127; //bias
2781 *significandParts() = mysignificand;
2782 if (myexponent==0) // denormal
2785 *significandParts() |= 0x800000; // integer bit
2789 /// Treat api as containing the bits of a floating point number. Currently
2790 /// we infer the floating point type from the size of the APInt. The
2791 /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful
2792 /// when the size is anything else).
2794 APFloat::initFromAPInt(const APInt& api, bool isIEEE)
2796 if (api.getBitWidth() == 32)
2797 return initFromFloatAPInt(api);
2798 else if (api.getBitWidth()==64)
2799 return initFromDoubleAPInt(api);
2800 else if (api.getBitWidth()==80)
2801 return initFromF80LongDoubleAPInt(api);
2802 else if (api.getBitWidth()==128 && !isIEEE)
2803 return initFromPPCDoubleDoubleAPInt(api);
2808 APFloat::APFloat(const APInt& api, bool isIEEE)
2810 initFromAPInt(api, isIEEE);
2813 APFloat::APFloat(float f)
2815 APInt api = APInt(32, 0);
2816 initFromAPInt(api.floatToBits(f));
2819 APFloat::APFloat(double d)
2821 APInt api = APInt(64, 0);
2822 initFromAPInt(api.doubleToBits(d));