21 #include "llvm/Config/llvm-config.h" 29 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ 31 if (usesLayout<IEEEFloat>(getSemantics())) \ 32 return U.IEEE.METHOD_CALL; \ 33 if (usesLayout<DoubleAPFloat>(getSemantics())) \ 34 return U.Double.METHOD_CALL; \ 35 llvm_unreachable("Unexpected semantics"); \ 46 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) 124 case S_x87DoubleExtended:
125 return x87DoubleExtended();
128 case S_PPCDoubleDouble:
129 return PPCDoubleDouble();
143 return S_x87DoubleExtended;
147 return S_PPCDoubleDouble;
215 static inline unsigned int 222 static inline unsigned int 236 unsigned int absExponent;
237 const unsigned int overlargeExponent = 24000;
241 if (p == end || ((*p ==
'-' || *p ==
'+') && (p + 1) == end)) {
245 isNegative = (*p ==
'-');
246 if (*p ==
'-' || *p ==
'+') {
253 if (absExponent >= 10U)
254 return createError(
"Invalid character in exponent");
256 for (; p !=
end; ++p) {
261 return createError(
"Invalid character in exponent");
263 absExponent = absExponent * 10U + value;
264 if (absExponent >= overlargeExponent) {
265 absExponent = overlargeExponent;
271 return -(int) absExponent;
273 return (
int) absExponent;
280 int exponentAdjustment) {
281 int unsignedExponent;
282 bool negative, overflow;
288 negative = *p ==
'-';
289 if (*p ==
'-' || *p ==
'+') {
295 unsignedExponent = 0;
297 for (; p !=
end; ++p) {
302 return createError(
"Invalid character in exponent");
304 unsignedExponent = unsignedExponent * 10 + value;
305 if (unsignedExponent > 32767) {
311 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
315 exponent = unsignedExponent;
317 exponent = -exponent;
318 exponent += exponentAdjustment;
319 if (exponent > 32767 || exponent < -32768)
324 exponent = negative ? -32768: 32767;
334 while (p != end && *p ==
'0')
337 if (p != end && *p ==
'.') {
340 if (end - begin == 1)
343 while (p != end && *p ==
'0')
376 return PtrOrErr.takeError();
383 for (; p !=
end; ++p) {
386 return createError(
"String contains multiple dots");
396 if (*p !=
'e' && *p !=
'E')
397 return createError(
"Invalid character in significand");
400 if (dot != end && p - begin == 1)
406 return ExpOrErr.takeError();
421 while (p != begin && *p ==
'0');
422 while (p != begin && *p ==
'.');
441 unsigned int digitValue) {
442 unsigned int hexDigit;
448 else if (digitValue < 8 && digitValue > 0)
452 while (p != end && (*p ==
'0' || *p ==
'.'))
456 return createError(
"Invalid trailing hexadecimal fraction!");
472 unsigned int partCount,
501 return lost_fraction;
516 return moreSignificant;
527 HUerrBound(
bool inexactMultiply,
unsigned int HUerr1,
unsigned int HUerr2)
529 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
531 if (HUerr1 + HUerr2 == 0)
532 return inexactMultiply * 2;
534 return inexactMultiply + 2 * (HUerr1 + HUerr2);
543 unsigned int count, partBits;
560 if (part - boundary <= boundary - part)
561 return part - boundary;
563 return boundary - part;
566 if (part == boundary) {
572 }
else if (part == boundary - 1) {
589 pow5s[0] = 78125 * 5;
591 unsigned int partsCount[16] = { 1 };
594 assert(power <= maxExponent);
599 *p1 = firstEightPowers[power & 7];
605 for (
unsigned int n = 0; power; power >>= 1, n++) {
612 pc = partsCount[n - 1];
615 if (pow5[pc - 1] == 0)
625 if (p2[result - 1] == 0)
650 static const char NaNL[] =
"nan";
651 static const char NaNU[] =
"NAN";
658 const char *hexDigitChars)
660 unsigned int result =
count;
666 dst[
count] = hexDigitChars[part & 0xf];
709 semantics = ourSemantics;
712 significand.parts =
new integerPart[
count];
715 void IEEEFloat::freeSignificand() {
717 delete [] significand.parts;
720 void IEEEFloat::assign(
const IEEEFloat &rhs) {
721 assert(semantics == rhs.semantics);
724 category = rhs.category;
725 exponent = rhs.exponent;
726 if (isFiniteNonZero() || category == fcNaN)
727 copySignificand(rhs);
730 void IEEEFloat::copySignificand(
const IEEEFloat &rhs) {
731 assert(isFiniteNonZero() || category == fcNaN);
732 assert(rhs.partCount() >= partCount());
741 void IEEEFloat::makeNaN(
bool SNaN,
bool Negative,
const APInt *fill) {
746 unsigned numParts = partCount();
756 unsigned bitsToPreserve = semantics->precision - 1;
757 unsigned part = bitsToPreserve / 64;
758 bitsToPreserve %= 64;
759 significand[part] &= ((1ULL << bitsToPreserve) - 1);
760 for (part++; part != numParts; ++part)
761 significand[part] = 0;
764 unsigned QNaNBit = semantics->precision - 2;
783 if (semantics == &semX87DoubleExtended)
789 if (semantics != rhs.semantics) {
802 semantics = rhs.semantics;
803 significand = rhs.significand;
804 exponent = rhs.exponent;
805 category = rhs.category;
812 bool IEEEFloat::isDenormal()
const {
813 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
815 semantics->precision - 1) == 0);
818 bool IEEEFloat::isSmallest()
const {
822 return isFiniteNonZero() && exponent == semantics->
minExponent &&
823 significandMSB() == 0;
826 bool IEEEFloat::isSignificandAllOnes()
const {
830 const unsigned PartCount = partCount();
831 for (
unsigned i = 0; i < PartCount - 1; i++)
836 const unsigned NumHighBits =
837 PartCount*integerPartWidth - semantics->precision + 1;
838 assert(NumHighBits <= integerPartWidth &&
"Can not have more high bits to " 839 "fill than integerPartWidth");
841 ~
integerPart(0) << (integerPartWidth - NumHighBits);
842 if (~(Parts[PartCount - 1] | HighBitFill))
848 bool IEEEFloat::isSignificandAllZeros()
const {
852 const unsigned PartCount = partCount();
854 for (
unsigned i = 0; i < PartCount - 1; i++)
858 const unsigned NumHighBits =
859 PartCount*integerPartWidth - semantics->precision + 1;
860 assert(NumHighBits <= integerPartWidth &&
"Can not have more high bits to " 861 "clear than integerPartWidth");
864 if (Parts[PartCount - 1] & HighBitMask)
870 bool IEEEFloat::isLargest()
const {
873 return isFiniteNonZero() && exponent == semantics->maxExponent
874 && isSignificandAllOnes();
877 bool IEEEFloat::isInteger()
const {
879 if (!isFinite())
return false;
882 return compare(truncated) == cmpEqual;
885 bool IEEEFloat::bitwiseIsEqual(
const IEEEFloat &rhs)
const {
888 if (semantics != rhs.semantics ||
889 category != rhs.category ||
892 if (category==fcZero || category==fcInfinity)
895 if (isFiniteNonZero() && exponent != rhs.exponent)
898 return std::equal(significandParts(), significandParts() + partCount(),
899 rhs.significandParts());
908 significandParts()[0] = value;
924 initialize(rhs.semantics);
929 *
this = std::move(rhs);
934 unsigned int IEEEFloat::partCount()
const {
939 return const_cast<IEEEFloat *
>(
this)->significandParts();
944 return significand.parts;
946 return &significand.part;
949 void IEEEFloat::zeroSignificand() {
954 void IEEEFloat::incrementSignificand() {
968 parts = significandParts();
970 assert(semantics == rhs.semantics);
971 assert(exponent == rhs.exponent);
973 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
982 parts = significandParts();
984 assert(semantics == rhs.semantics);
985 assert(exponent == rhs.exponent);
997 unsigned int partsCount, newPartsCount, precision;
1004 assert(semantics == rhs.semantics);
1012 if (newPartsCount > 4)
1015 fullSignificand = scratch;
1017 lhsSignificand = significandParts();
1018 partsCount = partCount();
1021 rhs.significandParts(), partsCount, partsCount);
1024 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1025 exponent += rhs.exponent;
1043 Significand savedSignificand = significand;
1047 unsigned int extendedPrecision;
1050 extendedPrecision = 2 * precision + 1;
1051 if (omsb != extendedPrecision - 1) {
1052 assert(extendedPrecision > omsb);
1054 (extendedPrecision - 1) - omsb);
1055 exponent -= (extendedPrecision - 1) - omsb;
1059 extendedSemantics = *semantics;
1060 extendedSemantics.
precision = extendedPrecision;
1062 if (newPartsCount == 1)
1063 significand.part = fullSignificand[0];
1065 significand.parts = fullSignificand;
1066 semantics = &extendedSemantics;
1079 lost_fraction = extendedAddend.shiftSignificandRight(1);
1081 "Lost precision while shifting addend for fused-multiply-add.");
1083 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
1086 if (newPartsCount == 1)
1087 fullSignificand[0] = significand.part;
1088 significand = savedSignificand;
1089 semantics = savedSemantics;
1091 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1098 exponent -= precision + 1;
1107 if (omsb > precision) {
1108 unsigned int bits, significantParts;
1111 bits = omsb - precision;
1113 lf =
shiftRight(fullSignificand, significantParts, bits);
1120 if (newPartsCount > 4)
1121 delete [] fullSignificand;
1123 return lost_fraction;
1127 return multiplySignificand(rhs,
IEEEFloat(*semantics));
1132 unsigned int bit, i, partsCount;
1138 assert(semantics == rhs.semantics);
1140 lhsSignificand = significandParts();
1141 rhsSignificand = rhs.significandParts();
1142 partsCount = partCount();
1149 divisor = dividend + partsCount;
1152 for (i = 0; i < partsCount; i++) {
1153 dividend[i] = lhsSignificand[i];
1154 divisor[i] = rhsSignificand[i];
1155 lhsSignificand[i] = 0;
1158 exponent -= rhs.exponent;
1160 unsigned int precision = semantics->
precision;
1163 bit = precision -
APInt::tcMSB(divisor, partsCount) - 1;
1170 bit = precision -
APInt::tcMSB(dividend, partsCount) - 1;
1186 for (bit = precision; bit; bit -= 1) {
1210 return lost_fraction;
1213 unsigned int IEEEFloat::significandMSB()
const {
1217 unsigned int IEEEFloat::significandLSB()
const {
1228 return shiftRight(significandParts(), partCount(), bits);
1232 void IEEEFloat::shiftSignificandLeft(
unsigned int bits) {
1233 assert(bits < semantics->precision);
1236 unsigned int partsCount = partCount();
1249 assert(semantics == rhs.semantics);
1253 compare = exponent - rhs.exponent;
1263 else if (compare < 0)
1295 bool IEEEFloat::roundAwayFromZero(
roundingMode rounding_mode,
1297 unsigned int bit)
const {
1304 switch (rounding_mode) {
1339 omsb = significandMSB() + 1;
1345 exponentChange = omsb - semantics->
precision;
1349 if (exponent + exponentChange > semantics->
maxExponent)
1350 return handleOverflow(rounding_mode);
1354 if (exponent + exponentChange < semantics->minExponent)
1355 exponentChange = semantics->
minExponent - exponent;
1358 if (exponentChange < 0) {
1361 shiftSignificandLeft(-exponentChange);
1366 if (exponentChange > 0) {
1370 lf = shiftSignificandRight(exponentChange);
1375 if (omsb > (
unsigned) exponentChange)
1376 omsb -= exponentChange;
1396 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1400 incrementSignificand();
1401 omsb = significandMSB() + 1;
1404 if (omsb == (
unsigned) semantics->
precision + 1) {
1414 shiftSignificandRight(1);
1426 assert(omsb < semantics->precision);
1458 copySignificand(rhs);
1479 if (((sign ^ rhs.sign)!=0) != subtract) {
1500 subtract ^=
static_cast<bool>(sign ^ rhs.sign);
1503 bits = exponent - rhs.exponent;
1511 else if (bits > 0) {
1512 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1513 shiftSignificandLeft(1);
1515 lost_fraction = shiftSignificandRight(-bits - 1);
1516 temp_rhs.shiftSignificandLeft(1);
1521 carry = temp_rhs.subtractSignificand
1523 copySignificand(temp_rhs);
1526 carry = subtractSignificand
1545 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1546 carry = addSignificand(temp_rhs);
1548 lost_fraction = shiftSignificandRight(-bits);
1549 carry = addSignificand(rhs);
1557 return lost_fraction;
1577 copySignificand(rhs);
1611 copySignificand(rhs);
1662 copySignificand(rhs);
1690 fs = addOrSubtractSpecials(rhs, subtract);
1696 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1697 fs = normalize(rounding_mode, lost_fraction);
1706 if (category ==
fcZero) {
1707 if (rhs.category !=
fcZero || (sign == rhs.sign) == subtract)
1717 return addOrSubtract(rhs, rounding_mode,
false);
1723 return addOrSubtract(rhs, rounding_mode,
true);
1732 fs = multiplySpecials(rhs);
1736 fs = normalize(rounding_mode, lost_fraction);
1750 fs = divideSpecials(rhs);
1754 fs = normalize(rounding_mode, lost_fraction);
1766 unsigned int origSign = sign;
1772 int parts = partCount();
1802 fs = modSpecials(rhs);
1803 unsigned int origSign = sign;
1827 sign ^= multiplicand.sign;
1836 lost_fraction = multiplySignificand(multiplicand, addend);
1837 fs = normalize(rounding_mode, lost_fraction);
1847 fs = multiplySpecials(multiplicand);
1857 fs = addOrSubtract(addend, rounding_mode,
false);
1884 MagicConstant.sign = sign;
1892 fs =
add(MagicConstant, rounding_mode);
1896 fs =
subtract(MagicConstant, rounding_mode);
1910 assert(semantics == rhs.semantics);
1942 if (sign == rhs.sign)
1957 if (sign != rhs.sign) {
1988 unsigned int newPartCount, oldPartCount;
1995 oldPartCount = partCount();
1998 bool X86SpecialNan =
false;
1999 if (&fromSemantics == &semX87DoubleExtended &&
2000 &toSemantics != &semX87DoubleExtended && category ==
fcNaN &&
2001 (!(*significandParts() & 0x8000000000000000ULL) ||
2002 !(*significandParts() & 0x4000000000000000ULL))) {
2005 X86SpecialNan =
true;
2014 int exponentChange = significandMSB() + 1 - fromSemantics.
precision;
2015 if (exponent + exponentChange < toSemantics.
minExponent)
2016 exponentChange = toSemantics.
minExponent - exponent;
2017 if (exponentChange < shift)
2018 exponentChange = shift;
2019 if (exponentChange < 0) {
2020 shift -= exponentChange;
2021 exponent += exponentChange;
2027 lostFraction =
shiftRight(significandParts(), oldPartCount, -shift);
2030 if (newPartCount > oldPartCount) {
2038 significand.parts = newParts;
2039 }
else if (newPartCount == 1 && oldPartCount != 1) {
2043 newPart = significandParts()[0];
2045 significand.part = newPart;
2049 semantics = &toSemantics;
2057 fs = normalize(rounding_mode, lostFraction);
2058 *losesInfo = (fs !=
opOK);
2059 }
else if (category ==
fcNaN) {
2060 *losesInfo = lostFraction !=
lfExactlyZero || X86SpecialNan;
2064 if (!X86SpecialNan && semantics == &semX87DoubleExtended)
2095 unsigned int dstPartsCount, truncatedBits;
2104 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2106 if (category ==
fcZero) {
2113 src = significandParts();
2122 truncatedBits = semantics->
precision -1U - exponent;
2126 unsigned int bits = exponent + 1U;
2132 if (bits < semantics->precision) {
2148 if (truncatedBits) {
2152 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2172 if (omsb == width &&
2183 if (omsb >= width + !isSigned)
2205 unsigned int width,
bool isSigned,
2209 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2213 unsigned int bits, dstPartsCount;
2216 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2218 if (category ==
fcNaN)
2223 bits = width - isSigned;
2226 if (sign && isSigned)
2238 unsigned int omsb, precision, dstCount;
2244 dst = significandParts();
2245 dstCount = partCount();
2250 if (precision <= omsb) {
2251 exponent = omsb - 1;
2256 exponent = precision - 1;
2261 return normalize(rounding_mode, lost_fraction);
2275 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2283 unsigned int srcCount,
bool isSigned,
2296 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2300 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2309 unsigned int width,
bool isSigned,
2320 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2324 IEEEFloat::convertFromHexadecimalString(
StringRef s,
2333 unsigned partsCount = partCount();
2335 bool computedTrailingFraction =
false;
2343 return PtrOrErr.takeError();
2352 return createError(
"String contains multiple dots");
2358 if (hex_value == -1U)
2368 }
else if (!computedTrailingFraction) {
2371 return FractOrErr.takeError();
2372 lost_fraction = *FractOrErr;
2373 computedTrailingFraction =
true;
2379 return createError(
"Hex strings require an exponent");
2380 if (*p !=
'p' && *p !=
'P')
2381 return createError(
"Invalid character in significand");
2384 if (dot != end && p - begin == 1)
2388 if (p != firstSignificantDigit) {
2397 expAdjustment =
static_cast<int>(dot - firstSignificantDigit);
2398 if (expAdjustment < 0)
2400 expAdjustment = expAdjustment * 4 - 1;
2410 return ExpOrErr.takeError();
2411 exponent = *ExpOrErr;
2414 return normalize(rounding_mode, lost_fraction);
2418 IEEEFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
2419 unsigned sigPartCount,
int exp,
2421 unsigned int parts, pow5PartCount;
2432 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2434 for (;; parts *= 2) {
2436 unsigned int excessPrecision, truncatedBits;
2440 truncatedBits = excessPrecision;
2446 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2448 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2451 decSig.exponent += exp;
2455 unsigned int powHUerr;
2459 calcLostFraction = decSig.multiplySignificand(pow5);
2460 powHUerr = powStatus !=
opOK;
2462 calcLostFraction = decSig.divideSignificand(pow5);
2465 excessPrecision += (semantics->
minExponent - decSig.exponent);
2466 truncatedBits = excessPrecision;
2467 if (excessPrecision > calcSemantics.
precision)
2468 excessPrecision = calcSemantics.
precision;
2477 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
2482 excessPrecision, isNearest);
2485 if (HUdistance >= HUerr) {
2486 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2487 calcSemantics.
precision - excessPrecision,
2492 exponent = (decSig.exponent + semantics->
precision 2493 - (calcSemantics.
precision - excessPrecision));
2497 return normalize(rounding_mode, calcLostFraction);
2510 return std::move(Err);
2543 fs = handleOverflow(rounding_mode);
2561 fs = handleOverflow(rounding_mode);
2564 unsigned int partCount;
2588 if (p == str.
end()) {
2593 if (decValue >= 10U) {
2594 delete[] decSignificand;
2595 return createError(
"Invalid character in significand");
2598 val = val * 10 + decValue;
2605 partCount, partCount + 1,
false);
2609 if (decSignificand[partCount])
2614 fs = roundSignificandWithExponent(decSignificand, partCount,
2617 delete [] decSignificand;
2623 bool IEEEFloat::convertFromStringSpecials(
StringRef str) {
2653 if (convertFromStringSpecials(str))
2658 size_t slen = str.
size();
2659 sign = *p ==
'-' ? 1 : 0;
2660 if (*p ==
'-' || *p ==
'+') {
2667 if (slen >= 2 && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
2670 return convertFromHexadecimalString(
StringRef(p + 2, slen - 2),
2674 return convertFromDecimalString(
StringRef(p, slen), rounding_mode);
2712 memcpy (dst, upperCase ? infinityU: infinityL,
sizeof infinityU - 1);
2713 dst +=
sizeof infinityL - 1;
2717 memcpy (dst, upperCase ? NaNU: NaNL,
sizeof NaNU - 1);
2718 dst +=
sizeof NaNU - 1;
2723 *dst++ = upperCase ?
'X':
'x';
2725 if (hexDigits > 1) {
2727 memset (dst,
'0', hexDigits - 1);
2728 dst += hexDigits - 1;
2730 *dst++ = upperCase ?
'P':
'p';
2735 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2741 return static_cast<unsigned int>(dst - p);
2748 char *IEEEFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
2751 unsigned int count, valueBits, shift, partsCount, outputDigits;
2752 const char *hexDigitChars;
2758 *dst++ = upperCase ?
'X':
'x';
2763 significand = significandParts();
2764 partsCount = partCount();
2773 outputDigits = (valueBits - significandLSB () + 3) / 4;
2779 if (hexDigits < outputDigits) {
2785 bits = valueBits - hexDigits * 4;
2787 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2789 outputDigits = hexDigits;
2799 while (outputDigits && count) {
2803 if (--count == partsCount)
2806 part = significand[
count] << shift;
2814 if (curDigits > outputDigits)
2815 curDigits = outputDigits;
2816 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
2817 outputDigits -= curDigits;
2827 }
while (*q ==
'0');
2831 memset (dst,
'0', outputDigits);
2832 dst += outputDigits;
2845 *dst++ = upperCase ?
'P':
'p';
2854 Arg.
isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2858 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2861 Arg.significandParts(),
2862 Arg.significandParts() + Arg.partCount()));
2874 APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt()
const {
2878 uint64_t myexponent, mysignificand;
2881 myexponent = exponent+16383;
2882 mysignificand = significandParts()[0];
2883 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2885 }
else if (category==
fcZero) {
2889 myexponent = 0x7fff;
2890 mysignificand = 0x8000000000000000ULL;
2893 myexponent = 0x7fff;
2894 mysignificand = significandParts()[0];
2898 words[0] = mysignificand;
2899 words[1] = ((uint64_t)(sign & 1) << 15) |
2900 (myexponent & 0x7fffLL);
2901 return APInt(80, words);
2904 APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt()
const {
2929 words[0] = *u.convertDoubleAPFloatToAPInt().
getRawData();
2945 words[1] = *v.convertDoubleAPFloatToAPInt().
getRawData();
2950 return APInt(128, words);
2953 APInt IEEEFloat::convertQuadrupleAPFloatToAPInt()
const {
2957 uint64_t myexponent, mysignificand, mysignificand2;
2960 myexponent = exponent+16383;
2961 mysignificand = significandParts()[0];
2962 mysignificand2 = significandParts()[1];
2963 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2965 }
else if (category==
fcZero) {
2967 mysignificand = mysignificand2 = 0;
2969 myexponent = 0x7fff;
2970 mysignificand = mysignificand2 = 0;
2973 myexponent = 0x7fff;
2974 mysignificand = significandParts()[0];
2975 mysignificand2 = significandParts()[1];
2979 words[0] = mysignificand;
2980 words[1] = ((uint64_t)(sign & 1) << 63) |
2981 ((myexponent & 0x7fff) << 48) |
2982 (mysignificand2 & 0xffffffffffffLL);
2984 return APInt(128, words);
2987 APInt IEEEFloat::convertDoubleAPFloatToAPInt()
const {
2991 uint64_t myexponent, mysignificand;
2994 myexponent = exponent+1023;
2995 mysignificand = *significandParts();
2996 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2998 }
else if (category==
fcZero) {
3007 mysignificand = *significandParts();
3010 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
3011 ((myexponent & 0x7ff) << 52) |
3012 (mysignificand & 0xfffffffffffffLL))));
3015 APInt IEEEFloat::convertFloatAPFloatToAPInt()
const {
3019 uint32_t myexponent, mysignificand;
3022 myexponent = exponent+127;
3023 mysignificand = (
uint32_t)*significandParts();
3024 if (myexponent == 1 && !(mysignificand & 0x800000))
3026 }
else if (category==
fcZero) {
3035 mysignificand = (
uint32_t)*significandParts();
3038 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3039 (mysignificand & 0x7fffff)));
3042 APInt IEEEFloat::convertHalfAPFloatToAPInt()
const {
3046 uint32_t myexponent, mysignificand;
3049 myexponent = exponent+15;
3050 mysignificand = (
uint32_t)*significandParts();
3051 if (myexponent == 1 && !(mysignificand & 0x400))
3053 }
else if (category==
fcZero) {
3062 mysignificand = (
uint32_t)*significandParts();
3065 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3066 (mysignificand & 0x3ff)));
3075 return convertHalfAPFloatToAPInt();
3078 return convertFloatAPFloatToAPInt();
3081 return convertDoubleAPFloatToAPInt();
3084 return convertQuadrupleAPFloatToAPInt();
3087 return convertPPCDoubleDoubleAPFloatToAPInt();
3091 return convertF80LongDoubleAPFloatToAPInt();
3096 "Float semantics are not IEEEsingle");
3103 "Float semantics are not IEEEdouble");
3115 void IEEEFloat::initFromF80LongDoubleAPInt(
const APInt &api) {
3119 uint64_t myexponent = (i2 & 0x7fff);
3120 uint64_t mysignificand = i1;
3121 uint8_t myintegerbit = mysignificand >> 63;
3123 initialize(&semX87DoubleExtended);
3126 sign =
static_cast<unsigned int>(i2>>15);
3127 if (myexponent == 0 && mysignificand == 0) {
3130 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3133 }
else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3134 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3137 significandParts()[0] = mysignificand;
3138 significandParts()[1] = 0;
3141 exponent = myexponent - 16383;
3142 significandParts()[0] = mysignificand;
3143 significandParts()[1] = 0;
3149 void IEEEFloat::initFromPPCDoubleDoubleAPInt(
const APInt &api) {
3157 initFromDoubleAPInt(
APInt(64, i1));
3173 void IEEEFloat::initFromQuadrupleAPInt(
const APInt &api) {
3177 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3178 uint64_t mysignificand = i1;
3179 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3181 initialize(&semIEEEquad);
3184 sign =
static_cast<unsigned int>(i2>>63);
3185 if (myexponent==0 &&
3186 (mysignificand==0 && mysignificand2==0)) {
3189 }
else if (myexponent==0x7fff &&
3190 (mysignificand==0 && mysignificand2==0)) {
3193 }
else if (myexponent==0x7fff &&
3194 (mysignificand!=0 || mysignificand2 !=0)) {
3197 significandParts()[0] = mysignificand;
3198 significandParts()[1] = mysignificand2;
3201 exponent = myexponent - 16383;
3202 significandParts()[0] = mysignificand;
3203 significandParts()[1] = mysignificand2;
3207 significandParts()[1] |= 0x1000000000000LL;
3211 void IEEEFloat::initFromDoubleAPInt(
const APInt &api) {
3214 uint64_t myexponent = (i >> 52) & 0x7ff;
3215 uint64_t mysignificand = i & 0xfffffffffffffLL;
3217 initialize(&semIEEEdouble);
3220 sign =
static_cast<unsigned int>(i>>63);
3221 if (myexponent==0 && mysignificand==0) {
3224 }
else if (myexponent==0x7ff && mysignificand==0) {
3227 }
else if (myexponent==0x7ff && mysignificand!=0) {
3230 *significandParts() = mysignificand;
3233 exponent = myexponent - 1023;
3234 *significandParts() = mysignificand;
3238 *significandParts() |= 0x10000000000000LL;
3242 void IEEEFloat::initFromFloatAPInt(
const APInt &api) {
3245 uint32_t myexponent = (i >> 23) & 0xff;
3246 uint32_t mysignificand = i & 0x7fffff;
3248 initialize(&semIEEEsingle);
3252 if (myexponent==0 && mysignificand==0) {
3255 }
else if (myexponent==0xff && mysignificand==0) {
3258 }
else if (myexponent==0xff && mysignificand!=0) {
3261 *significandParts() = mysignificand;
3264 exponent = myexponent - 127;
3265 *significandParts() = mysignificand;
3269 *significandParts() |= 0x800000;
3273 void IEEEFloat::initFromHalfAPInt(
const APInt &api) {
3276 uint32_t myexponent = (i >> 10) & 0x1f;
3277 uint32_t mysignificand = i & 0x3ff;
3279 initialize(&semIEEEhalf);
3283 if (myexponent==0 && mysignificand==0) {
3286 }
else if (myexponent==0x1f && mysignificand==0) {
3289 }
else if (myexponent==0x1f && mysignificand!=0) {
3292 *significandParts() = mysignificand;
3295 exponent = myexponent - 15;
3296 *significandParts() = mysignificand;
3300 *significandParts() |= 0x400;
3309 if (Sem == &semIEEEhalf)
3310 return initFromHalfAPInt(api);
3311 if (Sem == &semIEEEsingle)
3312 return initFromFloatAPInt(api);
3313 if (Sem == &semIEEEdouble)
3314 return initFromDoubleAPInt(api);
3315 if (Sem == &semX87DoubleExtended)
3316 return initFromF80LongDoubleAPInt(api);
3317 if (Sem == &semIEEEquad)
3318 return initFromQuadrupleAPInt(api);
3319 if (Sem == &semPPCDoubleDoubleLegacy)
3320 return initFromPPCDoubleDoubleAPInt(api);
3338 unsigned PartCount = partCount();
3339 memset(significand, 0xFF,
sizeof(
integerPart)*(PartCount - 1));
3343 const unsigned NumUnusedHighBits =
3378 initFromAPInt(&Sem, API);
3396 void AdjustToPrecision(
APInt &significand,
3397 int &exp,
unsigned FormatPrecision) {
3401 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3403 if (bits <= bitsRequired)
return;
3405 unsigned tensRemovable = (bits -
bitsRequired) * 59 / 196;
3406 if (!tensRemovable)
return;
3408 exp += tensRemovable;
3413 if (tensRemovable & 1)
3415 tensRemovable >>= 1;
3416 if (!tensRemovable)
break;
3420 significand = significand.
udiv(divisor);
3428 int &exp,
unsigned FormatPrecision) {
3429 unsigned N = buffer.
size();
3430 if (N <= FormatPrecision)
return;
3433 unsigned FirstSignificant = N - FormatPrecision;
3440 if (buffer[FirstSignificant - 1] <
'5') {
3441 while (FirstSignificant < N && buffer[FirstSignificant] ==
'0')
3444 exp += FirstSignificant;
3445 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3451 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
3452 if (buffer[
I] ==
'9') {
3461 if (FirstSignificant == N) {
3462 exp += FirstSignificant;
3468 exp += FirstSignificant;
3469 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3474 unsigned FormatMaxPadding,
bool TruncateZero)
const {
3478 return append(Str,
"-Inf");
3480 return append(Str,
"+Inf");
3488 if (!FormatMaxPadding) {
3493 if (FormatPrecision > 1)
3494 Str.
append(FormatPrecision - 1,
'0');
3509 int exp = exponent - ((int) semantics->
precision - 1);
3516 if (!FormatPrecision) {
3524 FormatPrecision = 2 + semantics->
precision * 59 / 196;
3529 exp += trailingZeros;
3535 }
else if (exp > 0) {
3538 significand <<= exp;
3552 unsigned precision = semantics->
precision + (137 * texp + 136) / 59;
3556 significand = significand.
zext(precision);
3557 APInt five_to_the_i(precision, 5);
3559 if (texp & 1) significand *= five_to_the_i;
3563 five_to_the_i *= five_to_the_i;
3567 AdjustToPrecision(significand, exp, FormatPrecision);
3573 APInt ten(precision, 10);
3574 APInt digit(precision, 0);
3576 bool inTrail =
true;
3577 while (significand != 0) {
3585 if (inTrail && !d) exp++;
3592 assert(!buffer.
empty() &&
"no characters in buffer!");
3596 AdjustToPrecision(buffer, exp, FormatPrecision);
3598 unsigned NDigits = buffer.
size();
3601 bool FormatScientific;
3602 if (!FormatMaxPadding)
3603 FormatScientific =
true;
3609 FormatScientific = ((
unsigned) exp > FormatMaxPadding ||
3610 NDigits + (
unsigned) exp > FormatPrecision);
3613 int MSD = exp + (int) (NDigits - 1);
3616 FormatScientific =
false;
3620 FormatScientific = ((
unsigned) -MSD) > FormatMaxPadding;
3626 if (FormatScientific) {
3627 exp += (NDigits - 1);
3631 if (NDigits == 1 && TruncateZero)
3634 for (
unsigned I = 1;
I != NDigits; ++
I)
3637 if (!TruncateZero && FormatPrecision > NDigits - 1)
3638 Str.
append(FormatPrecision - NDigits + 1,
'0');
3640 Str.
push_back(TruncateZero ?
'E' :
'e');
3643 if (exp < 0) exp = -exp;
3646 expbuf.
push_back((
char) (
'0' + (exp % 10)));
3650 if (!TruncateZero && expbuf.
size() < 2)
3652 for (
unsigned I = 0,
E = expbuf.
size();
I !=
E; ++
I)
3659 for (
unsigned I = 0;
I != NDigits; ++
I)
3669 int NWholeDigits = exp + (int) NDigits;
3672 if (NWholeDigits > 0) {
3673 for (; I != (
unsigned) NWholeDigits; ++
I)
3677 unsigned NZeros = 1 + (
unsigned) -NWholeDigits;
3681 for (
unsigned Z = 1; Z != NZeros; ++Z)
3685 for (; I != NDigits; ++
I)
3696 if (significandLSB() != semantics->
precision - 1)
3710 reciprocal.significandLSB() == reciprocal.semantics->
precision - 1);
3713 *inv =
APFloat(reciprocal, *semantics);
3788 bool WillCrossBinadeBoundary =
3789 exponent != semantics->
minExponent && isSignificandAllZeros();
3807 if (WillCrossBinadeBoundary) {
3823 bool WillCrossBinadeBoundary = !
isDenormal() && isSignificandAllOnes();
3825 if (WillCrossBinadeBoundary) {
3830 "We can not increment an exponent beyond the maxExponent allowed" 3831 " by the given floating point semantics.");
3834 incrementSignificand();
3874 return Arg.exponent;
3879 Normalized.exponent += SignificandBits;
3881 return Normalized.exponent - SignificandBits;
3895 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3898 X.exponent += std::min(
std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3921 return scalbn(Val, -Exp, RM);
3954 Floats(new
APFloat[2]{std::move(First), std::move(Second)}) {
3956 assert(&Floats[0].getSemantics() == &semIEEEdouble);
3957 assert(&Floats[1].getSemantics() == &semIEEEdouble);
3975 if (
Semantics == RHS.Semantics && RHS.Floats) {
3976 Floats[0] = RHS.Floats[0];
3977 Floats[1] = RHS.Floats[1];
3978 }
else if (
this != &RHS) {
3993 Status |= z.
add(c, RM);
3996 Floats[0] = std::move(z);
3997 Floats[1].makeZero(
false);
4001 auto AComparedToC = a.compareAbsoluteValue(c);
4003 Status |= z.
add(aa, RM);
4006 Status |= z.
add(c, RM);
4007 Status |= z.
add(a, RM);
4010 Status |= z.
add(a, RM);
4011 Status |= z.
add(c, RM);
4014 Floats[0] = std::move(z);
4015 Floats[1].makeZero(
false);
4020 Status |= zz.
add(cc, RM);
4024 Status |= Floats[1].subtract(z, RM);
4025 Status |= Floats[1].add(c, RM);
4026 Status |= Floats[1].add(zz, RM);
4030 Status |= Floats[1].subtract(z, RM);
4031 Status |= Floats[1].add(a, RM);
4032 Status |= Floats[1].add(zz, RM);
4042 Status |= zz.
add(c, RM);
4043 Status |= q.
add(z, RM);
4046 Status |= zz.add(q, RM);
4047 Status |= zz.add(aa, RM);
4048 Status |= zz.add(cc, RM);
4049 if (zz.isZero() && !zz.isNegative()) {
4050 Floats[0] = std::move(z);
4051 Floats[1].makeZero(
false);
4055 Status |= Floats[0].add(zz, RM);
4056 if (!Floats[0].isFinite()) {
4057 Floats[1].makeZero(
false);
4060 Floats[1] = std::move(z);
4061 Status |= Floats[1].subtract(Floats[0], RM);
4062 Status |= Floats[1].add(zz, RM);
4102 APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]),
C(RHS.Floats[0]),
4110 return Out.addImpl(A, AA,
C, CC, RM);
4115 return addWithSpecial(*
this, RHS, *
this, RM);
4128 const auto &LHS = *
this;
4155 Out.
makeNaN(
false,
false,
nullptr);
4167 "Special cases not handled exhaustively");
4170 APFloat A = Floats[0],
B = Floats[1],
C = RHS.Floats[0],
D = RHS.Floats[1];
4176 Floats[1].makeZero(
false);
4192 Status |= V.
add(W, RM);
4194 Status |= Tau.
add(V, RM);
4198 Status |= U.
add(Tau, RM);
4202 Floats[1].makeZero(
false);
4206 Status |= T.
add(Tau, RM);
4261 Floats[0].changeSign();
4262 Floats[1].changeSign();
4267 auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
4270 Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
4272 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4273 auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative();
4274 if (Against && !RHSAgainst)
4276 if (!Against && RHSAgainst)
4278 if (!Against && !RHSAgainst)
4280 if (Against && RHSAgainst)
4287 return Floats[0].getCategory();
4293 Floats[0].makeInf(Neg);
4294 Floats[1].makeZero(
false);
4298 Floats[0].makeZero(Neg);
4299 Floats[1].makeZero(
false);
4304 Floats[0] =
APFloat(semIEEEdouble,
APInt(64, 0x7fefffffffffffffull));
4305 Floats[1] =
APFloat(semIEEEdouble,
APInt(64, 0x7c8ffffffffffffeull));
4312 Floats[0].makeSmallest(Neg);
4313 Floats[1].makeZero(
false);
4318 Floats[0] =
APFloat(semIEEEdouble,
APInt(64, 0x0360000000000000ull));
4320 Floats[0].changeSign();
4321 Floats[1].makeZero(
false);
4325 Floats[0].makeNaN(SNaN, Neg, fill);
4326 Floats[1].makeZero(
false);
4330 auto Result = Floats[0].compare(RHS.Floats[0]);
4333 return Floats[1].compare(RHS.Floats[1]);
4338 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
4339 Floats[1].bitwiseIsEqual(RHS.Floats[1]);
4351 Floats[0].bitcastToAPInt().getRawData()[0],
4352 Floats[1].bitcastToAPInt().getRawData()[0],
4354 return APInt(128, 2, Data);
4360 APFloat Tmp(semPPCDoubleDoubleLegacy);
4369 auto Ret = Tmp.
next(nextDown);
4376 unsigned int Width,
bool IsSigned,
4380 .convertToInteger(Input, Width, IsSigned, RM, IsExact);
4387 APFloat Tmp(semPPCDoubleDoubleLegacy);
4395 unsigned int InputSize,
4398 APFloat Tmp(semPPCDoubleDoubleLegacy);
4406 unsigned int InputSize,
4409 APFloat Tmp(semPPCDoubleDoubleLegacy);
4416 unsigned int HexDigits,
4421 .convertToHexString(DST, HexDigits, UpperCase, RM);
4426 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
4428 Floats[0].compare(Floats[0] + Floats[1]) !=
cmpEqual);
4449 return Floats[0].isInteger() && Floats[1].isInteger();
4453 unsigned FormatPrecision,
4454 unsigned FormatMaxPadding,
4455 bool TruncateZero)
const {
4458 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
4466 APFloat Inv(semPPCDoubleDoubleLegacy);
4473 assert(Arg.Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4475 scalbn(Arg.Floats[1], Exp, RM));
4480 assert(Arg.Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4482 APFloat Second = Arg.Floats[1];
4484 Second =
scalbn(Second, -Exp, RM);
4485 return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second));
4491 if (usesLayout<IEEEFloat>(Semantics)) {
4495 if (usesLayout<DoubleAPFloat>(Semantics)) {
4511 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.
getSemantics()))
4513 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.
getSemantics()))
4521 assert(StatusOrErr &&
"Invalid floating point representation");
4532 usesLayout<IEEEFloat>(ToSemantics))
4533 return U.IEEE.convert(ToSemantics, RM, losesInfo);
4535 usesLayout<DoubleAPFloat>(ToSemantics)) {
4536 assert(&ToSemantics == &semPPCDoubleDouble);
4537 auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo);
4538 *
this =
APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
4542 usesLayout<IEEEFloat>(ToSemantics)) {
4543 auto Ret = getIEEE().
convert(ToSemantics, RM, losesInfo);
4544 *
this =
APFloat(std::move(getIEEE()), ToSemantics);
4575 OS << Buffer <<
"\n";
4578 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4592 bool *isExact)
const {
4596 rounding_mode, isExact);
4598 result =
APInt(bitWidth, parts);
4604 #undef APFLOAT_DISPATCH_ON_SEMANTICS cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
opStatus roundToIntegral(roundingMode RM)
static APFloatBase::integerPart ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, bool isNearest)
DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
static const fltSemantics & IEEEquad() LLVM_READNONE
fltCategory
Category of internally-represented number.
const_iterator end(StringRef path)
Get end iterator over path.
static bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
void makeNaN(bool SNaN, bool Neg, const APInt *fill)
bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static unsigned int partAsHex(char *dst, APFloatBase::integerPart part, unsigned int count, const char *hexDigitChars)
friend DoubleAPFloat scalbn(DoubleAPFloat X, int Exp, roundingMode)
static unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
uint64_t getZExtValue() const
Get zero extended value.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
const unsigned int maxPowerOfFiveParts
void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
float convertToFloat() const
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Converts this value into a decimal string.
void makeSmallest(bool Neg)
static void tcExtract(WordType *, unsigned dstCount, const WordType *, unsigned srcBits, unsigned srcLSB)
Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to DST, of dstCOUNT parts...
opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
friend hash_code hash_value(const DoubleAPFloat &Arg)
cmpResult compareAbsoluteValue(const IEEEFloat &) const
unsigned getNumWords() const
Get the number of words.
opStatus roundToIntegral(roundingMode)
void push_back(const T &Elt)
opStatus remainder(const DoubleAPFloat &RHS)
static lostFraction shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits)
static int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
APInt zext(unsigned width) const
Zero extend to a new width.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits)
Set the least significant BITS and clear the rest.
static Expected< StringRef::iterator > skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, StringRef::iterator *dot)
RoundingMode
Rounding mode used for floating point operations.
void makeInf(bool Neg=false)
static int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
static unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
APInt trunc(unsigned width) const
Truncate to new width.
const fltSemantics & getSemantics() const
friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode)
Expected< opStatus > convertFromString(StringRef, roundingMode)
opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
static const llvm::fltSemantics & EnumToSemantics(Semantics S)
constexpr int bitsRequired(unsigned n)
Determine the number of bits required to store integers with values < n.
opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
opStatus next(bool nextDown)
unsigned getBitWidth() const
Return the number of bits in the APInt.
fltCategory getCategory() const
const char * firstSigDigit
static Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative. ...
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
opStatus divide(const APFloat &RHS, roundingMode RM)
static Error createError(const Twine &Err)
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
unsigned int convertToHexString(char *dst, unsigned int hexDigits, bool upperCase, roundingMode) const
Write out a hexadecimal representation of the floating point value to DST, which must be of sufficien...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static const fltSemantics semX87DoubleExtended
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
static APInt doubleToBits(double V)
Converts a double to APInt bits.
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
IEEEFloat(const fltSemantics &)
static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE=false)
Returns a float which is bitcasted from an all one value int.
void makeLargest(bool Neg=false)
Make this number the largest magnitude normal number in the given semantics.
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
void makeSmallestNormalized(bool Neg)
Tagged union holding either a T or a Error.
opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
opStatus convertFromSignExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
static const char infinityU[]
static ExponentType semanticsMaxExponent(const fltSemantics &)
void makeSmallest(bool Neg=false)
Make this number the smallest magnitude denormal number in the given semantics.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
static unsigned int powerOf5(APFloatBase::integerPart *dst, unsigned int power)
static Expected< lostFraction > trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, unsigned int digitValue)
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
unsigned getActiveBits() const
Compute the number of active bits in the value.
opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
static unsigned int HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
static unsigned int semanticsSizeInBits(const fltSemantics &)
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
LLVM_NODISCARD size_t size() const
size - Get the string size.
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
opStatus subtract(const APFloat &RHS, roundingMode RM)
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
static const fltSemantics & IEEEdouble() LLVM_READNONE
APInt bitcastToAPInt() const
DoubleAPFloat(const fltSemantics &S)
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, bool, roundingMode)
static ExponentType semanticsMinExponent(const fltSemantics &)
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
static const unsigned integerPartWidth
static const char infinityL[]
Expected< opStatus > convertFromString(StringRef, roundingMode)
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
bool isNegative() const
Determine sign of this APInt.
static char * writeUnsignedDecimal(char *dst, unsigned int n)
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
opStatus mod(const DoubleAPFloat &RHS)
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
static const fltSemantics semPPCDoubleDoubleLegacy
static Error interpretDecimal(StringRef::iterator begin, StringRef::iterator end, decimalInfo *D)
static WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
const unsigned int maxPowerOfFiveExponent
bool getExactInverse(APFloat *inv) const
If this value has an exact multiplicative inverse, store it in inv and return true.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
opStatus remainder(const IEEEFloat &)
IEEE remainder.
opStatus multiply(const APFloat &RHS, roundingMode RM)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
const unsigned int maxPrecision
opStatus roundToIntegral(roundingMode RM)
static const fltSemantics semIEEEdouble
friend int ilogb(const IEEEFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
static WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
iterator erase(const_iterator CI)
bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
void consumeError(Error Err)
Consume a Error without doing anything.
static unsigned int partCountForBits(unsigned int bits)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static void tcSet(WordType *, WordType, unsigned)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts...
void makeLargest(bool Neg)
static const fltSemantics semBogus
APFloatBase::ExponentType minExponent
friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode)
void makeZero(bool Neg=false)
static const fltSemantics & IEEEsingle() LLVM_READNONE
static Expected< int > totalExponent(StringRef::iterator p, StringRef::iterator end, int exponentAdjustment)
static ErrorSuccess success()
Create a success value.
void Profile(FoldingSetNodeID &NID) const
Used to insert APFloat objects, or objects that contain APFloat objects, into FoldingSets.
static const fltSemantics & IEEEhalf() LLVM_READNONE
opStatus add(const IEEEFloat &, roundingMode)
Align max(MaybeAlign Lhs, Align Rhs)
bool isFiniteNonZero() const
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
void print(raw_ostream &) const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
static void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
bool isFiniteNonZero() const
opStatus next(bool nextDown)
opStatus subtract(const IEEEFloat &, roundingMode)
static const char hexDigitsLower[]
bool isSmallest() const
Returns true if and only if the number has the smallest possible non-zero magnitude in the current se...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static unsigned int semanticsPrecision(const fltSemantics &)
static lostFraction lostFractionThroughTruncation(const APFloatBase::integerPart *parts, unsigned int partCount, unsigned int bits)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const fltSemantics & getSemantics() const
APInt::WordType integerPart
bool getExactInverse(APFloat *inv) const
Class for arbitrary precision integers.
cmpResult compare(const DoubleAPFloat &RHS) const
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static const char hexDigitsUpper[]
bool isZero() const
Returns true if and only if the float is plus or minus zero.
opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
opStatus mod(const APFloat &RHS)
An opaque object representing a hash code.
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
const char * lastSigDigit
static const fltSemantics semIEEEsingle
APFloatBase::ExponentType maxExponent
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
static Expected< int > readExponent(StringRef::iterator begin, StringRef::iterator end)
bool getExactInverse(APFloat *inv) const
static lostFraction combineLostFractions(lostFraction moreSignificant, lostFraction lessSignificant)
static const fltSemantics semIEEEquad
static char * writeSignedDecimal(char *dst, int value)
bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
static void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
opStatus add(const APFloat &RHS, roundingMode RM)
opStatus
IEEE-754R 7: Default exception handling.
Expected< opStatus > convertFromString(StringRef, roundingMode)
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
LLVM_NODISCARD bool empty() const
This corresponds to "fpround.tozero".
static const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real...
static void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
static const fltSemantics semPPCDoubleDouble
static void tcFullMultiply(WordType *, const WordType *, const WordType *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
static unsigned int decDigitValue(unsigned int c)
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
float bitsToFloat() const
Converts APInt bits to a float.
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
opStatus divide(const IEEEFloat &, roundingMode)
double convertToDouble() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
*ViewGraph Emit a dot run dot
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
opStatus remainder(const APFloat &RHS)
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
static unsigned tcMSB(const WordType *parts, unsigned n)
opStatus multiply(const IEEEFloat &, roundingMode)
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Lightweight error class with error context and mandatory checking.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
This class implements an extremely fast bulk output stream that can only output to a stream...
friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
Returns: X * 2^Exp for integral exponents.
static void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
StringRef - Represent a constant reference to a string, i.e.
static int tcMultiplyPart(WordType *dst, const WordType *src, WordType multiplier, WordType carry, unsigned srcParts, unsigned dstParts, bool add)
DST += SRC * MULTIPLIER + PART if add is true DST = SRC * MULTIPLIER + PART if add is false...
#define PackCategoriesIntoKey(_lhs, _rhs)
A macro used to combine two fcCategory enums into one key which can be used in a switch statement to ...
opStatus convertFromZeroExtendedInteger(const integerPart *Input, unsigned int InputSize, bool IsSigned, roundingMode RM)
APInt bitcastToAPInt() const
static void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
friend hash_code hash_value(const IEEEFloat &Arg)
Overload to compute a hash code for an APFloat value.
OutputIt copy(R &&Range, OutputIt Out)
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
APInt bitcastToAPInt() const
static APInt floatToBits(float V)
Converts a float to APInt bits.
double bitsToDouble() const
Converts APInt bits to a double.
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
static const fltSemantics semIEEEhalf
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...