34 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
38 #define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1]
98 static inline unsigned int
105 static inline unsigned int
120 unsigned int absExponent;
121 const unsigned int overlargeExponent = 24000;
124 assert(p != end &&
"Exponent has no digits");
126 isNegative = (*p ==
'-');
127 if (*p ==
'-' || *p ==
'+') {
129 assert(p != end &&
"Exponent has no digits");
133 assert(absExponent < 10U &&
"Invalid character in exponent");
135 for (; p !=
end; ++p) {
139 assert(value < 10U &&
"Invalid character in exponent");
141 value += absExponent * 10;
142 if (absExponent >= overlargeExponent) {
143 absExponent = overlargeExponent;
150 assert(p == end &&
"Invalid exponent in exponent");
153 return -(int) absExponent;
155 return (
int) absExponent;
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) {
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;
215 while (*p ==
'0' && p != end)
221 assert(end - begin != 1 &&
"Significand has no digits");
223 while (*p ==
'0' && p != end)
261 for (; p !=
end; ++p) {
263 assert(dot == end &&
"String contains multiple dots");
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");
292 while (p != begin && *p ==
'0');
293 while (p != begin && *p ==
'.');
311 unsigned int digitValue)
313 unsigned int hexDigit;
319 else if (digitValue < 8 && digitValue > 0)
323 while (p != end && (*p ==
'0' || *p ==
'.'))
326 assert(p != end &&
"Invalid trailing hexadecimal fraction!");
342 unsigned int partCount,
371 return lost_fraction;
386 return moreSignificant;
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;
404 return inexactMultiply + 2 * (HUerr1 + HUerr2);
413 unsigned int count, partBits;
430 if (part - boundary <= boundary - part)
431 return part - boundary;
433 return boundary - part;
436 if (part == boundary) {
442 }
else if (part == boundary - 1) {
458 static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125,
461 pow5s[0] = 78125 * 5;
463 unsigned int partsCount[16] = { 1 };
471 *p1 = firstEightPowers[power & 7];
477 for (
unsigned int n = 0; power; power >>= 1, n++) {
484 pc = partsCount[n - 1];
487 if (pow5[pc - 1] == 0)
497 if (p2[result - 1] == 0)
502 tmp = p1, p1 = p2, p2 = tmp;
520 static const char NaNL[] =
"nan";
521 static const char NaNU[] =
"NAN";
528 const char *hexDigitChars)
530 unsigned int result = count;
536 dst[count] = hexDigitChars[part & 0xf];
580 semantics = ourSemantics;
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;
602 copySignificand(rhs);
606 APFloat::copySignificand(
const APFloat &rhs)
609 assert(rhs.partCount() >= partCount());
618 void APFloat::makeNaN(
bool SNaN,
bool Negative,
const APInt *fill)
624 unsigned numParts = partCount();
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;
668 value.makeNaN(SNaN, Negative, fill);
676 if (semantics != rhs.semantics) {
678 initialize(rhs.semantics);
699 significandMSB() == 0;
702 bool APFloat::isSignificandAllOnes()
const {
706 const unsigned PartCount = partCount();
707 for (
unsigned i = 0; i < PartCount - 1; i++)
712 const unsigned NumHighBits =
715 "fill than integerPartWidth");
718 if (~(Parts[PartCount - 1] | HighBitFill))
724 bool APFloat::isSignificandAllZeros()
const {
728 const unsigned PartCount = partCount();
730 for (
unsigned i = 0; i < PartCount - 1; i++)
734 const unsigned NumHighBits =
737 "clear than integerPartWidth");
740 if (Parts[PartCount - 1] & HighBitMask)
751 && isSignificandAllOnes();
758 if (semantics != rhs.semantics ||
759 category != rhs.category ||
770 for (; i>0; i--, p++, q++) {
779 initialize(&ourSemantics);
784 significandParts()[0] = value;
789 initialize(&ourSemantics);
796 initialize(&ourSemantics);
800 initialize(&ourSemantics);
805 initialize(rhs.semantics);
820 APFloat::partCount()
const
832 APFloat::significandParts()
const
834 return const_cast<APFloat *
>(
this)->significandParts();
838 APFloat::significandParts()
841 return significand.parts;
843 return &significand.part;
847 APFloat::zeroSignificand()
854 APFloat::incrementSignificand()
867 APFloat::addSignificand(
const APFloat &rhs)
871 parts = significandParts();
873 assert(semantics == rhs.semantics);
874 assert(exponent == rhs.exponent);
876 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
886 parts = significandParts();
888 assert(semantics == rhs.semantics);
889 assert(exponent == rhs.exponent);
899 APFloat::multiplySignificand(
const APFloat &rhs,
const APFloat *addend)
902 unsigned int partsCount, newPartsCount, precision;
909 assert(semantics == rhs.semantics);
914 if (newPartsCount > 4)
917 fullSignificand = scratch;
919 lhsSignificand = significandParts();
920 partsCount = partCount();
923 rhs.significandParts(), partsCount, partsCount);
926 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
927 exponent += rhs.exponent;
944 Significand savedSignificand = significand;
948 unsigned int extendedPrecision;
951 extendedPrecision = 2 * precision;
952 if (omsb != extendedPrecision) {
953 assert(extendedPrecision > omsb);
955 extendedPrecision - omsb);
956 exponent -= extendedPrecision - omsb;
960 extendedSemantics = *semantics;
961 extendedSemantics.
precision = extendedPrecision;
963 if (newPartsCount == 1)
964 significand.part = fullSignificand[0];
966 significand.parts = fullSignificand;
967 semantics = &extendedSemantics;
969 APFloat extendedAddend(*addend);
970 status = extendedAddend.convert(extendedSemantics,
rmTowardZero, &ignored);
971 assert(status ==
opOK);
973 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
976 if (newPartsCount == 1)
977 fullSignificand[0] = significand.part;
978 significand = savedSignificand;
979 semantics = savedSemantics;
981 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
988 exponent -= precision;
996 if (omsb > precision) {
997 unsigned int bits, significantParts;
1000 bits = omsb - precision;
1002 lf =
shiftRight(fullSignificand, significantParts, bits);
1009 if (newPartsCount > 4)
1010 delete [] fullSignificand;
1012 return lost_fraction;
1017 APFloat::divideSignificand(
const APFloat &rhs)
1019 unsigned int bit, i, partsCount;
1025 assert(semantics == rhs.semantics);
1027 lhsSignificand = significandParts();
1028 rhsSignificand = rhs.significandParts();
1029 partsCount = partCount();
1036 divisor = dividend + partsCount;
1039 for (i = 0; i < partsCount; i++) {
1040 dividend[i] = lhsSignificand[i];
1041 divisor[i] = rhsSignificand[i];
1042 lhsSignificand[i] = 0;
1045 exponent -= rhs.exponent;
1047 unsigned int precision = semantics->
precision;
1050 bit = precision -
APInt::tcMSB(divisor, partsCount) - 1;
1057 bit = precision -
APInt::tcMSB(dividend, partsCount) - 1;
1073 for (bit = precision; bit; bit -= 1) {
1097 return lost_fraction;
1101 APFloat::significandMSB()
const
1107 APFloat::significandLSB()
const
1114 APFloat::shiftSignificandRight(
unsigned int bits)
1121 return shiftRight(significandParts(), partCount(), bits);
1126 APFloat::shiftSignificandLeft(
unsigned int bits)
1128 assert(bits < semantics->precision);
1131 unsigned int partsCount = partCount();
1141 APFloat::compareAbsoluteValue(
const APFloat &rhs)
const
1145 assert(semantics == rhs.semantics);
1149 compare = exponent - rhs.exponent;
1159 else if (compare < 0)
1168 APFloat::handleOverflow(roundingMode rounding_mode)
1194 APFloat::roundAwayFromZero(roundingMode rounding_mode,
1196 unsigned int bit)
const
1204 switch (rounding_mode) {
1222 return sign ==
false;
1225 return sign ==
true;
1231 APFloat::normalize(roundingMode rounding_mode,
1241 omsb = significandMSB() + 1;
1247 exponentChange = omsb - semantics->
precision;
1251 if (exponent + exponentChange > semantics->
maxExponent)
1252 return handleOverflow(rounding_mode);
1256 if (exponent + exponentChange < semantics->minExponent)
1257 exponentChange = semantics->
minExponent - exponent;
1260 if (exponentChange < 0) {
1263 shiftSignificandLeft(-exponentChange);
1268 if (exponentChange > 0) {
1272 lf = shiftSignificandRight(exponentChange);
1277 if (omsb > (
unsigned) exponentChange)
1278 omsb -= exponentChange;
1298 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1302 incrementSignificand();
1303 omsb = significandMSB() + 1;
1306 if (omsb == (
unsigned) semantics->
precision + 1) {
1316 shiftSignificandRight(1);
1328 assert(omsb < semantics->precision);
1339 APFloat::addOrSubtractSpecials(
const APFloat &rhs,
bool subtract)
1359 copySignificand(rhs);
1380 if (((sign ^ rhs.sign)!=0) != subtract) {
1394 APFloat::addOrSubtractSignificand(
const APFloat &rhs,
bool subtract)
1402 subtract ^= (sign ^ rhs.sign) ?
true :
false;
1405 bits = exponent - rhs.exponent;
1413 reverse = compareAbsoluteValue(temp_rhs) ==
cmpLessThan;
1415 }
else if (bits > 0) {
1416 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1417 shiftSignificandLeft(1);
1420 lost_fraction = shiftSignificandRight(-bits - 1);
1421 temp_rhs.shiftSignificandLeft(1);
1426 carry = temp_rhs.subtractSignificand
1428 copySignificand(temp_rhs);
1431 carry = subtractSignificand
1450 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1451 carry = addSignificand(temp_rhs);
1453 lost_fraction = shiftSignificandRight(-bits);
1454 carry = addSignificand(rhs);
1462 return lost_fraction;
1466 APFloat::multiplySpecials(
const APFloat &rhs)
1484 copySignificand(rhs);
1510 APFloat::divideSpecials(
const APFloat &rhs)
1520 copySignificand(rhs);
1551 APFloat::modSpecials(
const APFloat &rhs)
1571 copySignificand(rhs);
1611 APFloat::addOrSubtract(
const APFloat &rhs, roundingMode rounding_mode,
1616 fs = addOrSubtractSpecials(rhs, subtract);
1622 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1623 fs = normalize(rounding_mode, lost_fraction);
1632 if (category ==
fcZero) {
1633 if (rhs.category !=
fcZero || (sign == rhs.sign) == subtract)
1644 return addOrSubtract(rhs, rounding_mode,
false);
1651 return addOrSubtract(rhs, rounding_mode,
true);
1661 fs = multiplySpecials(rhs);
1664 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1665 fs = normalize(rounding_mode, lost_fraction);
1680 fs = divideSpecials(rhs);
1684 fs = normalize(rounding_mode, lost_fraction);
1698 unsigned int origSign = sign;
1704 int parts = partCount();
1734 fs = modSpecials(rhs);
1738 unsigned int origSign = sign;
1744 int parts = partCount();
1756 fs = V.
multiply(rhs, rounding_mode);
1778 sign ^= multiplicand.sign;
1787 lost_fraction = multiplySignificand(multiplicand, &addend);
1788 fs = normalize(rounding_mode, lost_fraction);
1795 if (category ==
fcZero && sign != addend.sign)
1798 fs = multiplySpecials(multiplicand);
1808 fs = addOrSubtract(addend, rounding_mode,
false);
1832 APFloat MagicConstant(*semantics);
1843 fs =
add(MagicConstant, rounding_mode);
1847 fs =
subtract(MagicConstant, rounding_mode);
1863 assert(semantics == rhs.semantics);
1895 if (sign == rhs.sign)
1910 if (sign != rhs.sign) {
1917 result = compareAbsoluteValue(rhs);
1942 unsigned int newPartCount, oldPartCount;
1949 oldPartCount = partCount();
1952 bool X86SpecialNan =
false;
1955 (!(*significandParts() & 0x8000000000000000ULL) ||
1956 !(*significandParts() & 0x4000000000000000ULL))) {
1959 X86SpecialNan =
true;
1968 int exponentChange = significandMSB() + 1 - fromSemantics.
precision;
1969 if (exponent + exponentChange < toSemantics.
minExponent)
1970 exponentChange = toSemantics.
minExponent - exponent;
1971 if (exponentChange < shift)
1972 exponentChange = shift;
1973 if (exponentChange < 0) {
1974 shift -= exponentChange;
1975 exponent += exponentChange;
1981 lostFraction =
shiftRight(significandParts(), oldPartCount, -shift);
1984 if (newPartCount > oldPartCount) {
1992 significand.parts = newParts;
1993 }
else if (newPartCount == 1 && oldPartCount != 1) {
1997 newPart = significandParts()[0];
1999 significand.part = newPart;
2003 semantics = &toSemantics;
2011 fs = normalize(rounding_mode, lostFraction);
2012 *losesInfo = (fs !=
opOK);
2013 }
else if (category ==
fcNaN) {
2014 *losesInfo = lostFraction !=
lfExactlyZero || X86SpecialNan;
2045 APFloat::convertToSignExtendedInteger(
integerPart *parts,
unsigned int width,
2047 roundingMode rounding_mode,
2048 bool *isExact)
const
2052 unsigned int dstPartsCount, truncatedBits;
2062 if (category ==
fcZero) {
2069 src = significandParts();
2078 truncatedBits = semantics->
precision -1U - exponent;
2082 unsigned int bits = exponent + 1U;
2088 if (bits < semantics->precision) {
2090 truncatedBits = semantics->
precision - bits;
2102 if (truncatedBits) {
2106 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2115 unsigned int omsb =
APInt::tcMSB(parts, dstPartsCount) + 1;
2126 if (omsb == width &&
APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
2136 if (omsb >= width + !isSigned)
2163 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2167 unsigned int bits, dstPartsCount;
2171 if (category ==
fcNaN)
2176 bits = width - isSigned;
2179 if (sign && isSigned)
2197 parts.data(), bitWidth, result.
isSigned(), rounding_mode, isExact);
2199 result =
APInt(bitWidth, parts);
2207 APFloat::convertFromUnsignedParts(
const integerPart *src,
2208 unsigned int srcCount,
2209 roundingMode rounding_mode)
2211 unsigned int omsb, precision, dstCount;
2217 dst = significandParts();
2218 dstCount = partCount();
2223 if (precision <= omsb) {
2224 exponent = omsb - 1;
2229 exponent = precision - 1;
2234 return normalize(rounding_mode, lost_fraction);
2251 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2259 unsigned int srcCount,
2274 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2278 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2287 unsigned int width,
bool isSigned,
2299 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2303 APFloat::convertFromHexadecimalString(
StringRef s, roundingMode rounding_mode)
2312 unsigned partsCount = partCount();
2314 bool computedTrailingFraction =
false;
2327 assert(dot == end &&
"String contains multiple dots");
2333 if (hex_value == -1U)
2343 }
else if (!computedTrailingFraction) {
2345 computedTrailingFraction =
true;
2350 assert(p != end &&
"Hex strings require an exponent");
2351 assert((*p ==
'p' || *p ==
'P') &&
"Invalid character in significand");
2352 assert(p != begin &&
"Significand has no digits");
2353 assert((dot == end || p - begin != 1) &&
"Significand has no digits");
2356 if (p != firstSignificantDigit) {
2365 expAdjustment =
static_cast<int>(dot - firstSignificantDigit);
2366 if (expAdjustment < 0)
2368 expAdjustment = expAdjustment * 4 - 1;
2379 return normalize(rounding_mode, lost_fraction);
2383 APFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
2384 unsigned sigPartCount,
int exp,
2385 roundingMode rounding_mode)
2387 unsigned int parts, pow5PartCount;
2398 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2400 for (;; parts *= 2) {
2402 unsigned int excessPrecision, truncatedBits;
2404 calcSemantics.
precision = parts * integerPartWidth - 1;
2406 truncatedBits = excessPrecision;
2411 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2413 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2416 decSig.exponent +=
exp;
2420 unsigned int powHUerr;
2424 calcLostFraction = decSig.multiplySignificand(pow5, NULL);
2425 powHUerr = powStatus !=
opOK;
2427 calcLostFraction = decSig.divideSignificand(pow5);
2430 excessPrecision += (semantics->
minExponent - decSig.exponent);
2431 truncatedBits = excessPrecision;
2432 if (excessPrecision > calcSemantics.
precision)
2433 excessPrecision = calcSemantics.
precision;
2442 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
2447 excessPrecision, isNearest);
2450 if (HUdistance >= HUerr) {
2451 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2452 calcSemantics.
precision - excessPrecision,
2457 exponent = (decSig.exponent + semantics->
precision
2458 - (calcSemantics.
precision - excessPrecision));
2462 return normalize(rounding_mode, calcLostFraction);
2468 APFloat::convertFromDecimalString(
StringRef str, roundingMode rounding_mode)
2508 fs = handleOverflow(rounding_mode);
2526 fs = handleOverflow(rounding_mode);
2529 unsigned int partCount;
2553 if (p == str.
end()) {
2558 assert(decValue < 10U &&
"Invalid character in significand");
2560 val = val * 10 + decValue;
2567 partCount, partCount + 1,
false);
2571 if (decSignificand[partCount])
2576 fs = roundSignificandWithExponent(decSignificand, partCount,
2579 delete [] decSignificand;
2586 APFloat::convertFromStringSpecials(
StringRef str) {
2598 makeNaN(
false,
false);
2603 makeNaN(
false,
true);
2613 assert(!str.
empty() &&
"Invalid string length");
2616 if (convertFromStringSpecials(str))
2621 size_t slen = str.
size();
2622 sign = *p ==
'-' ? 1 : 0;
2623 if (*p ==
'-' || *p ==
'+') {
2626 assert(slen &&
"String has no digits");
2629 if (slen >= 2 && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
2630 assert(slen - 2 &&
"Invalid string");
2631 return convertFromHexadecimalString(
StringRef(p + 2, slen - 2),
2635 return convertFromDecimalString(
StringRef(p, slen), rounding_mode);
2675 dst +=
sizeof infinityL - 1;
2680 dst +=
sizeof NaNU - 1;
2685 *dst++ = upperCase ?
'X':
'x';
2687 if (hexDigits > 1) {
2689 memset (dst,
'0', hexDigits - 1);
2690 dst += hexDigits - 1;
2692 *dst++ = upperCase ?
'P':
'p';
2697 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2703 return static_cast<unsigned int>(dst - p);
2711 APFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
2713 roundingMode rounding_mode)
const
2715 unsigned int count, valueBits, shift, partsCount, outputDigits;
2716 const char *hexDigitChars;
2722 *dst++ = upperCase ?
'X':
'x';
2727 significand = significandParts();
2728 partsCount = partCount();
2737 outputDigits = (valueBits - significandLSB () + 3) / 4;
2743 if (hexDigits < outputDigits) {
2749 bits = valueBits - hexDigits * 4;
2751 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2753 outputDigits = hexDigits;
2761 count = (valueBits + integerPartWidth - 1) / integerPartWidth;
2763 while (outputDigits && count) {
2767 if (--count == partsCount)
2770 part = significand[count] << shift;
2773 part |= significand[count - 1] >> (integerPartWidth - shift);
2776 unsigned int curDigits = integerPartWidth / 4;
2778 if (curDigits > outputDigits)
2779 curDigits = outputDigits;
2780 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
2781 outputDigits -= curDigits;
2791 }
while (*q ==
'0');
2795 memset (dst,
'0', outputDigits);
2796 dst += outputDigits;
2809 *dst++ = upperCase ?
'P':
'p';
2818 Arg.
isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2822 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2825 Arg.significandParts(),
2826 Arg.significandParts() + Arg.partCount()));
2839 APFloat::convertF80LongDoubleAPFloatToAPInt()
const
2842 assert(partCount()==2);
2844 uint64_t myexponent, mysignificand;
2847 myexponent = exponent+16383;
2848 mysignificand = significandParts()[0];
2849 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2851 }
else if (category==
fcZero) {
2855 myexponent = 0x7fff;
2856 mysignificand = 0x8000000000000000ULL;
2858 assert(category ==
fcNaN &&
"Unknown category");
2859 myexponent = 0x7fff;
2860 mysignificand = significandParts()[0];
2864 words[0] = mysignificand;
2865 words[1] = ((uint64_t)(sign & 1) << 15) |
2866 (myexponent & 0x7fffLL);
2867 return APInt(80, words);
2871 APFloat::convertPPCDoubleDoubleAPFloatToAPInt()
const
2874 assert(partCount()==2);
2890 assert(fs ==
opOK && !losesInfo);
2897 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
2903 if (u.isFiniteNonZero() && losesInfo) {
2905 assert(fs ==
opOK && !losesInfo);
2911 assert(fs ==
opOK && !losesInfo);
2913 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
2918 return APInt(128, words);
2922 APFloat::convertQuadrupleAPFloatToAPInt()
const
2925 assert(partCount()==2);
2927 uint64_t myexponent, mysignificand, mysignificand2;
2930 myexponent = exponent+16383;
2931 mysignificand = significandParts()[0];
2932 mysignificand2 = significandParts()[1];
2933 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2935 }
else if (category==
fcZero) {
2937 mysignificand = mysignificand2 = 0;
2939 myexponent = 0x7fff;
2940 mysignificand = mysignificand2 = 0;
2942 assert(category ==
fcNaN &&
"Unknown category!");
2943 myexponent = 0x7fff;
2944 mysignificand = significandParts()[0];
2945 mysignificand2 = significandParts()[1];
2949 words[0] = mysignificand;
2950 words[1] = ((uint64_t)(sign & 1) << 63) |
2951 ((myexponent & 0x7fff) << 48) |
2952 (mysignificand2 & 0xffffffffffffLL);
2954 return APInt(128, words);
2958 APFloat::convertDoubleAPFloatToAPInt()
const
2961 assert(partCount()==1);
2963 uint64_t myexponent, mysignificand;
2966 myexponent = exponent+1023;
2967 mysignificand = *significandParts();
2968 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2970 }
else if (category==
fcZero) {
2977 assert(category ==
fcNaN &&
"Unknown category!");
2979 mysignificand = *significandParts();
2982 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
2983 ((myexponent & 0x7ff) << 52) |
2984 (mysignificand & 0xfffffffffffffLL))));
2988 APFloat::convertFloatAPFloatToAPInt()
const
2991 assert(partCount()==1);
2993 uint32_t myexponent, mysignificand;
2996 myexponent = exponent+127;
2997 mysignificand = (uint32_t)*significandParts();
2998 if (myexponent == 1 && !(mysignificand & 0x800000))
3000 }
else if (category==
fcZero) {
3007 assert(category ==
fcNaN &&
"Unknown category!");
3009 mysignificand = (uint32_t)*significandParts();
3012 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3013 (mysignificand & 0x7fffff)));
3017 APFloat::convertHalfAPFloatToAPInt()
const
3020 assert(partCount()==1);
3022 uint32_t myexponent, mysignificand;
3025 myexponent = exponent+15;
3026 mysignificand = (uint32_t)*significandParts();
3027 if (myexponent == 1 && !(mysignificand & 0x400))
3029 }
else if (category==
fcZero) {
3036 assert(category ==
fcNaN &&
"Unknown category!");
3038 mysignificand = (uint32_t)*significandParts();
3041 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3042 (mysignificand & 0x3ff)));
3053 return convertHalfAPFloatToAPInt();
3056 return convertFloatAPFloatToAPInt();
3059 return convertDoubleAPFloatToAPInt();
3062 return convertQuadrupleAPFloatToAPInt();
3065 return convertPPCDoubleDoubleAPFloatToAPInt();
3069 return convertF80LongDoubleAPFloatToAPInt();
3076 "Float semantics are not IEEEsingle");
3085 "Float semantics are not IEEEdouble");
3098 APFloat::initFromF80LongDoubleAPInt(
const APInt &api)
3103 uint64_t myexponent = (i2 & 0x7fff);
3104 uint64_t mysignificand = i1;
3107 assert(partCount()==2);
3109 sign =
static_cast<unsigned int>(i2>>15);
3110 if (myexponent==0 && mysignificand==0) {
3113 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3116 }
else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
3119 significandParts()[0] = mysignificand;
3120 significandParts()[1] = 0;
3123 exponent = myexponent - 16383;
3124 significandParts()[0] = mysignificand;
3125 significandParts()[1] = 0;
3132 APFloat::initFromPPCDoubleDoubleAPInt(
const APInt &api)
3141 initFromDoubleAPInt(
APInt(64, i1));
3143 assert(fs ==
opOK && !losesInfo);
3150 assert(fs ==
opOK && !losesInfo);
3158 APFloat::initFromQuadrupleAPInt(
const APInt &api)
3163 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3164 uint64_t mysignificand = i1;
3165 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3168 assert(partCount()==2);
3170 sign =
static_cast<unsigned int>(i2>>63);
3171 if (myexponent==0 &&
3172 (mysignificand==0 && mysignificand2==0)) {
3175 }
else if (myexponent==0x7fff &&
3176 (mysignificand==0 && mysignificand2==0)) {
3179 }
else if (myexponent==0x7fff &&
3180 (mysignificand!=0 || mysignificand2 !=0)) {
3183 significandParts()[0] = mysignificand;
3184 significandParts()[1] = mysignificand2;
3187 exponent = myexponent - 16383;
3188 significandParts()[0] = mysignificand;
3189 significandParts()[1] = mysignificand2;
3193 significandParts()[1] |= 0x1000000000000LL;
3198 APFloat::initFromDoubleAPInt(
const APInt &api)
3202 uint64_t myexponent = (i >> 52) & 0x7ff;
3203 uint64_t mysignificand = i & 0xfffffffffffffLL;
3206 assert(partCount()==1);
3208 sign =
static_cast<unsigned int>(i>>63);
3209 if (myexponent==0 && mysignificand==0) {
3212 }
else if (myexponent==0x7ff && mysignificand==0) {
3215 }
else if (myexponent==0x7ff && mysignificand!=0) {
3218 *significandParts() = mysignificand;
3221 exponent = myexponent - 1023;
3222 *significandParts() = mysignificand;
3226 *significandParts() |= 0x10000000000000LL;
3231 APFloat::initFromFloatAPInt(
const APInt & api)
3235 uint32_t myexponent = (i >> 23) & 0xff;
3236 uint32_t mysignificand = i & 0x7fffff;
3239 assert(partCount()==1);
3242 if (myexponent==0 && mysignificand==0) {
3245 }
else if (myexponent==0xff && mysignificand==0) {
3248 }
else if (myexponent==0xff && mysignificand!=0) {
3251 *significandParts() = mysignificand;
3254 exponent = myexponent - 127;
3255 *significandParts() = mysignificand;
3259 *significandParts() |= 0x800000;
3264 APFloat::initFromHalfAPInt(
const APInt & api)
3268 uint32_t myexponent = (i >> 10) & 0x1f;
3269 uint32_t mysignificand = i & 0x3ff;
3272 assert(partCount()==1);
3275 if (myexponent==0 && mysignificand==0) {
3278 }
else if (myexponent==0x1f && mysignificand==0) {
3281 }
else if (myexponent==0x1f && mysignificand!=0) {
3284 *significandParts() = mysignificand;
3287 exponent = myexponent - 15;
3288 *significandParts() = mysignificand;
3292 *significandParts() |= 0x400;
3304 return initFromHalfAPInt(api);
3306 return initFromFloatAPInt(api);
3308 return initFromDoubleAPInt(api);
3310 return initFromF80LongDoubleAPInt(api);
3312 return initFromQuadrupleAPInt(api);
3314 return initFromPPCDoubleDoubleAPInt(api);
3342 void APFloat::makeLargest(
bool Negative) {
3353 unsigned PartCount = partCount();
3358 const unsigned NumUnusedHighBits =
3359 PartCount*integerPartWidth - semantics->
precision;
3360 significand[PartCount - 1] = ~
integerPart(0) >> NumUnusedHighBits;
3365 void APFloat::makeSmallest(
bool Negative) {
3383 Val.makeLargest(Negative);
3393 Val.makeSmallest(Negative);
3406 Val.zeroSignificand();
3407 Val.sign = Negative;
3416 initFromAPInt(&Sem, API);
3434 void AdjustToPrecision(
APInt &significand,
3435 int &exp,
unsigned FormatPrecision) {
3439 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3441 if (bits <= bitsRequired)
return;
3443 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3444 if (!tensRemovable)
return;
3446 exp += tensRemovable;
3451 if (tensRemovable & 1)
3453 tensRemovable >>= 1;
3454 if (!tensRemovable)
break;
3458 significand = significand.
udiv(divisor);
3466 int &exp,
unsigned FormatPrecision) {
3467 unsigned N = buffer.
size();
3468 if (N <= FormatPrecision)
return;
3471 unsigned FirstSignificant = N - FormatPrecision;
3478 if (buffer[FirstSignificant - 1] <
'5') {
3479 while (FirstSignificant < N && buffer[FirstSignificant] ==
'0')
3482 exp += FirstSignificant;
3483 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3489 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
3490 if (buffer[
I] ==
'9') {
3499 if (FirstSignificant == N) {
3500 exp += FirstSignificant;
3506 exp += FirstSignificant;
3507 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3512 unsigned FormatPrecision,
3513 unsigned FormatMaxPadding)
const {
3517 return append(Str,
"-Inf");
3519 return append(Str,
"+Inf");
3527 if (!FormatMaxPadding)
3541 int exp = exponent - ((int) semantics->
precision - 1);
3548 if (!FormatPrecision) {
3556 FormatPrecision = 2 + semantics->
precision * 59 / 196;
3561 exp += trailingZeros;
3562 significand = significand.
lshr(trailingZeros);
3567 }
else if (exp > 0) {
3570 significand <<=
exp;
3584 unsigned precision = semantics->
precision + (137 * texp + 136) / 59;
3588 significand = significand.
zext(precision);
3589 APInt five_to_the_i(precision, 5);
3591 if (texp & 1) significand *= five_to_the_i;
3595 five_to_the_i *= five_to_the_i;
3599 AdjustToPrecision(significand, exp, FormatPrecision);
3605 APInt ten(precision, 10);
3606 APInt digit(precision, 0);
3608 bool inTrail =
true;
3609 while (significand != 0) {
3617 if (inTrail && !d) exp++;
3624 assert(!buffer.
empty() &&
"no characters in buffer!");
3628 AdjustToPrecision(buffer, exp, FormatPrecision);
3630 unsigned NDigits = buffer.
size();
3633 bool FormatScientific;
3634 if (!FormatMaxPadding)
3635 FormatScientific =
true;
3641 FormatScientific = ((
unsigned) exp > FormatMaxPadding ||
3642 NDigits + (
unsigned) exp > FormatPrecision);
3645 int MSD = exp + (int) (NDigits - 1);
3648 FormatScientific =
false;
3652 FormatScientific = ((
unsigned) -MSD) > FormatMaxPadding;
3658 if (FormatScientific) {
3659 exp += (NDigits - 1);
3666 for (
unsigned I = 1;
I != NDigits; ++
I)
3671 if (exp < 0) exp = -
exp;
3674 expbuf.
push_back((
char) (
'0' + (exp % 10)));
3677 for (
unsigned I = 0, E = expbuf.
size();
I != E; ++
I)
3684 for (
unsigned I = 0;
I != NDigits; ++
I)
3694 int NWholeDigits = exp + (int) NDigits;
3697 if (NWholeDigits > 0) {
3698 for (; I != (
unsigned) NWholeDigits; ++
I)
3702 unsigned NZeros = 1 + (
unsigned) -NWholeDigits;
3706 for (
unsigned Z = 1; Z != NZeros; ++Z)
3710 for (; I != NDigits; ++
I)
3721 if (significandLSB() != semantics->
precision - 1)
3725 APFloat reciprocal(*semantics, 1ULL);
3735 reciprocal.significandLSB() == reciprocal.semantics->
precision - 1);
3785 makeSmallest(
false);
3813 bool WillCrossBinadeBoundary =
3814 exponent != semantics->
minExponent && isSignificandAllZeros();
3832 if (WillCrossBinadeBoundary) {
3848 bool WillCrossBinadeBoundary = !
isDenormal() && isSignificandAllOnes();
3850 if (WillCrossBinadeBoundary) {
3855 "We can not increment an exponent beyond the maxExponent allowed"
3856 " by the given floating point semantics.");
3859 incrementSignificand();
3873 APFloat::makeInf(
bool Negative) {
3881 APFloat::makeZero(
bool Negative) {
opStatus divide(const APFloat &, roundingMode)
void push_back(const T &Elt)
static void tcExtract(integerPart *, unsigned int dstCount, const integerPart *, unsigned int srcBits, unsigned int srcLSB)
const_iterator end(StringRef path)
Get end iterator over path.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
uint64_t getZExtValue() const
Get zero extended value.
static void tcSetLeastSignificantBits(integerPart *, unsigned int, unsigned int bits)
Set the least significant BITS and clear the rest.
const unsigned int maxPowerOfFiveParts
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
APFloat & operator=(const APFloat &)
size_t size() const
size - Get the string size.
void Profile(FoldingSetNodeID &NID) const
static char * writeSignedDecimal(char *dst, int value)
static const fltSemantics IEEEdouble
static int totalExponent(StringRef::iterator p, StringRef::iterator end, int exponentAdjustment)
static lostFraction lostFractionThroughTruncation(const integerPart *parts, unsigned int partCount, unsigned int bits)
APFloat(const fltSemantics &)
static unsigned int decDigitValue(unsigned int c)
static unsigned int HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
static void interpretDecimal(StringRef::iterator begin, StringRef::iterator end, decimalInfo *D)
const_iterator begin(StringRef path)
Get begin iterator over path.
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
static lostFraction shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
static integerPart tcIncrement(integerPart *, unsigned int)
Increment a bignum in-place. Return the carry flag.
static const fltSemantics Bogus
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
bool isFiniteNonZero() const
static unsigned int tcLSB(const integerPart *, unsigned int)
opStatus convertFromString(StringRef, roundingMode)
bool bitwiseIsEqual(const APFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
bool isNegative() const
Determine sign of this APInt.
static const fltSemantics x87DoubleExtended
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
signed short ExponentType
A signed type to represent a floating point numbers unbiased exponent.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, bool *) const
#define llvm_unreachable(msg)
static int tcMultiplyPart(integerPart *dst, const integerPart *src, integerPart multiplier, integerPart carry, unsigned int srcParts, unsigned int dstParts, bool add)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
static bool tcIsZero(const integerPart *, unsigned int)
Returns true if a bignum is zero, false otherwise.
static const fltSemantics IEEEquad
ID
LLVM Calling Convention Representation.
static void tcNegate(integerPart *, unsigned int)
Negate a bignum in-place.
void copySign(const APFloat &)
static void tcAssign(integerPart *, const integerPart *, unsigned int)
Assign one bignum to another.
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
static void tcShiftLeft(integerPart *, unsigned int parts, unsigned int count)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
static int tcExtractBit(const integerPart *, unsigned int bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
static lostFraction trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, unsigned int digitValue)
static void tcClearBit(integerPart *, unsigned int bit)
Clear the given bit of a bignum. Zero-based.
unsigned getActiveBits() const
Compute the number of active bits in the value.
hash_code hash_value(const APFloat &Arg)
static unsigned int semanticsPrecision(const fltSemantics &)
double convertToDouble() const
unsigned int convertToHexString(char *dst, unsigned int hexDigits, bool upperCase, roundingMode) const
opStatus mod(const APFloat &, roundingMode)
C fmod, or llvm frem.
static integerPart tcDecrement(integerPart *, unsigned int)
Decrement a bignum in-place. Return the borrow flag.
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
static unsigned int partAsHex(char *dst, integerPart part, unsigned int count, const char *hexDigitChars)
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
static void tcSet(integerPart *, integerPart, unsigned int)
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
const char * firstSigDigit
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
cmpResult compare(const APFloat &) const
static integerPart ulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest)
hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6)
const unsigned int integerPartWidth
const unsigned int maxPowerOfFiveExponent
opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
static unsigned int tcFullMultiply(integerPart *, const integerPart *, const integerPart *, unsigned, unsigned)
unsigned getBitWidth() const
Return the number of bits in the APInt.
opStatus convert(const fltSemantics &, roundingMode, bool *)
APFloat::ExponentType minExponent
const unsigned int maxPrecision
static integerPart tcAdd(integerPart *, const integerPart *, integerPart carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static unsigned int powerOf5(integerPart *dst, unsigned int power)
void append(in_iter in_start, in_iter in_end)
uint64_t NextPowerOf2(uint64_t A)
iterator erase(iterator I)
static int tcCompare(const integerPart *, const integerPart *, unsigned int)
Comparison (unsigned) of two bignums.
static const fltSemantics IEEEhalf
bool needsCleanup() const
Returns whether this instance allocated memory.
static void tcSetBit(integerPart *, unsigned int bit)
Set the given bit of a bignum. Zero-based.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
static const fltSemantics PPCDoubleDouble
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
static APInt LLVM_ATTRIBUTE_UNUSED_RESULT doubleToBits(double V)
Converts a double to APInt bits.
opStatus add(const APFloat &, roundingMode)
static StringRef::iterator skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, StringRef::iterator *dot)
opStatus multiply(const APFloat &, roundingMode)
static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE=false)
static const char infinityL[]
Class for arbitrary precision integers.
static const char hexDigitsLower[]
APInt bitcastToAPInt() const
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
An opaque object representing a hash code.
bool equals(StringRef RHS) const
static unsigned int tcMSB(const integerPart *parts, unsigned int n)
const char * lastSigDigit
double bitsToDouble() const
Converts APInt bits to a double.
static const char hexDigitsUpper[]
static const char infinityU[]
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3) const
static const fltSemantics IEEEsingle
const uint64_t * getRawData() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const
Unsigned division operation.
opStatus roundToIntegral(roundingMode)
static APInt LLVM_ATTRIBUTE_UNUSED_RESULT floatToBits(float V)
Converts a float to APInt bits.
static char * writeUnsignedDecimal(char *dst, unsigned int n)
float convertToFloat() const
static int readExponent(StringRef::iterator begin, StringRef::iterator end)
const unsigned int maxExponent
static integerPart tcSubtract(integerPart *, const integerPart *, integerPart carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static lostFraction combineLostFractions(lostFraction moreSignificant, lostFraction lessSignificant)
static unsigned int partCountForBits(unsigned int bits)
APFloat::ExponentType maxExponent
static unsigned hexDigitValue(char C)
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
bool isZero() const
Returns true if and only if the float is plus or minus zero.
#define PackCategoriesIntoKey(_lhs, _rhs)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
#define COMPILE_TIME_ASSERT(cond)
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
opStatus remainder(const APFloat &)
IEEE remainder.
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
bool getExactInverse(APFloat *inv) const
static void tcShiftRight(integerPart *, unsigned int parts, unsigned int count)
opStatus subtract(const APFloat &, roundingMode)
float bitsToFloat() const
Converts APInt bits to a double.
bool empty() const
empty - Check if the string is empty.
unsigned getNumWords() const
Get the number of words.