22 #include "llvm/Config/llvm-config.h" 30 #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ 32 if (usesLayout<IEEEFloat>(getSemantics())) \ 33 return U.IEEE.METHOD_CALL; \ 34 if (usesLayout<DoubleAPFloat>(getSemantics())) \ 35 return U.Double.METHOD_CALL; \ 36 llvm_unreachable("Unexpected semantics"); \ 47 #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) 176 static inline unsigned int 183 static inline unsigned int 198 unsigned int absExponent;
199 const unsigned int overlargeExponent = 24000;
202 assert(p != end &&
"Exponent has no digits");
204 isNegative = (*p ==
'-');
205 if (*p ==
'-' || *p ==
'+') {
207 assert(p != end &&
"Exponent has no digits");
211 assert(absExponent < 10U &&
"Invalid character in exponent");
213 for (; p !=
end; ++p) {
217 assert(value < 10U &&
"Invalid character in exponent");
219 value += absExponent * 10;
220 if (absExponent >= overlargeExponent) {
221 absExponent = overlargeExponent;
228 assert(p == end &&
"Invalid exponent in exponent");
231 return -(int) absExponent;
233 return (
int) absExponent;
240 int exponentAdjustment)
242 int unsignedExponent;
243 bool negative, overflow;
246 assert(p != end &&
"Exponent has no digits");
248 negative = *p ==
'-';
249 if (*p ==
'-' || *p ==
'+') {
251 assert(p != end &&
"Exponent has no digits");
254 unsignedExponent = 0;
256 for (; p !=
end; ++p) {
260 assert(value < 10U &&
"Invalid character in exponent");
262 unsignedExponent = unsignedExponent * 10 + value;
263 if (unsignedExponent > 32767) {
269 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
273 exponent = unsignedExponent;
275 exponent = -exponent;
276 exponent += exponentAdjustment;
277 if (exponent > 32767 || exponent < -32768)
282 exponent = negative ? -32768: 32767;
293 while (p != end && *p ==
'0')
296 if (p != end && *p ==
'.') {
299 assert(end - begin != 1 &&
"Significand has no digits");
301 while (p != end && *p ==
'0')
339 for (; p !=
end; ++p) {
341 assert(dot == end &&
"String contains multiple dots");
351 assert((*p ==
'e' || *p ==
'E') &&
"Invalid character in significand");
352 assert(p != begin &&
"Significand has no digits");
353 assert((dot == end || p - begin != 1) &&
"Significand has no digits");
370 while (p != begin && *p ==
'0');
371 while (p != begin && *p ==
'.');
389 unsigned int digitValue)
391 unsigned int hexDigit;
397 else if (digitValue < 8 && digitValue > 0)
401 while (p != end && (*p ==
'0' || *p ==
'.'))
404 assert(p != end &&
"Invalid trailing hexadecimal fraction!");
420 unsigned int partCount,
449 return lost_fraction;
464 return moreSignificant;
475 HUerrBound(
bool inexactMultiply,
unsigned int HUerr1,
unsigned int HUerr2)
477 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
479 if (HUerr1 + HUerr2 == 0)
480 return inexactMultiply * 2;
482 return inexactMultiply + 2 * (HUerr1 + HUerr2);
491 unsigned int count, partBits;
508 if (part - boundary <= boundary - part)
509 return part - boundary;
511 return boundary - part;
514 if (part == boundary) {
520 }
else if (part == boundary - 1) {
537 pow5s[0] = 78125 * 5;
539 unsigned int partsCount[16] = { 1 };
542 assert(power <= maxExponent);
547 *p1 = firstEightPowers[power & 7];
553 for (
unsigned int n = 0; power; power >>= 1, n++) {
560 pc = partsCount[n - 1];
563 if (pow5[pc - 1] == 0)
573 if (p2[result - 1] == 0)
598 static const char NaNL[] =
"nan";
599 static const char NaNU[] =
"NAN";
606 const char *hexDigitChars)
608 unsigned int result =
count;
614 dst[
count] = hexDigitChars[part & 0xf];
657 semantics = ourSemantics;
660 significand.parts =
new integerPart[
count];
663 void IEEEFloat::freeSignificand() {
665 delete [] significand.parts;
668 void IEEEFloat::assign(
const IEEEFloat &rhs) {
669 assert(semantics == rhs.semantics);
672 category = rhs.category;
673 exponent = rhs.exponent;
674 if (isFiniteNonZero() || category == fcNaN)
675 copySignificand(rhs);
678 void IEEEFloat::copySignificand(
const IEEEFloat &rhs) {
679 assert(isFiniteNonZero() || category == fcNaN);
680 assert(rhs.partCount() >= partCount());
689 void IEEEFloat::makeNaN(
bool SNaN,
bool Negative,
const APInt *fill) {
694 unsigned numParts = partCount();
704 unsigned bitsToPreserve = semantics->precision - 1;
705 unsigned part = bitsToPreserve / 64;
706 bitsToPreserve %= 64;
707 significand[part] &= ((1ULL << bitsToPreserve) - 1);
708 for (part++; part != numParts; ++part)
709 significand[part] = 0;
712 unsigned QNaNBit = semantics->precision - 2;
731 if (semantics == &semX87DoubleExtended)
737 if (semantics != rhs.semantics) {
750 semantics = rhs.semantics;
751 significand = rhs.significand;
752 exponent = rhs.exponent;
753 category = rhs.category;
760 bool IEEEFloat::isDenormal()
const {
761 return isFiniteNonZero() && (exponent == semantics->minExponent) &&
763 semantics->precision - 1) == 0);
766 bool IEEEFloat::isSmallest()
const {
770 return isFiniteNonZero() && exponent == semantics->
minExponent &&
771 significandMSB() == 0;
774 bool IEEEFloat::isSignificandAllOnes()
const {
778 const unsigned PartCount = partCount();
779 for (
unsigned i = 0; i < PartCount - 1; i++)
784 const unsigned NumHighBits =
785 PartCount*integerPartWidth - semantics->precision + 1;
786 assert(NumHighBits <= integerPartWidth &&
"Can not have more high bits to " 787 "fill than integerPartWidth");
789 ~
integerPart(0) << (integerPartWidth - NumHighBits);
790 if (~(Parts[PartCount - 1] | HighBitFill))
796 bool IEEEFloat::isSignificandAllZeros()
const {
800 const unsigned PartCount = partCount();
802 for (
unsigned i = 0; i < PartCount - 1; i++)
806 const unsigned NumHighBits =
807 PartCount*integerPartWidth - semantics->precision + 1;
808 assert(NumHighBits <= integerPartWidth &&
"Can not have more high bits to " 809 "clear than integerPartWidth");
812 if (Parts[PartCount - 1] & HighBitMask)
818 bool IEEEFloat::isLargest()
const {
821 return isFiniteNonZero() && exponent == semantics->maxExponent
822 && isSignificandAllOnes();
825 bool IEEEFloat::isInteger()
const {
827 if (!isFinite())
return false;
830 return compare(truncated) == cmpEqual;
833 bool IEEEFloat::bitwiseIsEqual(
const IEEEFloat &rhs)
const {
836 if (semantics != rhs.semantics ||
837 category != rhs.category ||
840 if (category==fcZero || category==fcInfinity)
843 if (isFiniteNonZero() && exponent != rhs.exponent)
846 return std::equal(significandParts(), significandParts() + partCount(),
847 rhs.significandParts());
856 significandParts()[0] = value;
872 initialize(rhs.semantics);
877 *
this = std::move(rhs);
882 unsigned int IEEEFloat::partCount()
const {
887 return const_cast<IEEEFloat *
>(
this)->significandParts();
892 return significand.parts;
894 return &significand.part;
897 void IEEEFloat::zeroSignificand() {
902 void IEEEFloat::incrementSignificand() {
916 parts = significandParts();
918 assert(semantics == rhs.semantics);
919 assert(exponent == rhs.exponent);
921 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
930 parts = significandParts();
932 assert(semantics == rhs.semantics);
933 assert(exponent == rhs.exponent);
945 unsigned int partsCount, newPartsCount, precision;
952 assert(semantics == rhs.semantics);
960 if (newPartsCount > 4)
963 fullSignificand = scratch;
965 lhsSignificand = significandParts();
966 partsCount = partCount();
969 rhs.significandParts(), partsCount, partsCount);
972 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
973 exponent += rhs.exponent;
991 Significand savedSignificand = significand;
995 unsigned int extendedPrecision;
998 extendedPrecision = 2 * precision + 1;
999 if (omsb != extendedPrecision - 1) {
1000 assert(extendedPrecision > omsb);
1002 (extendedPrecision - 1) - omsb);
1003 exponent -= (extendedPrecision - 1) - omsb;
1007 extendedSemantics = *semantics;
1008 extendedSemantics.
precision = extendedPrecision;
1010 if (newPartsCount == 1)
1011 significand.part = fullSignificand[0];
1013 significand.parts = fullSignificand;
1014 semantics = &extendedSemantics;
1024 lost_fraction = extendedAddend.shiftSignificandRight(1);
1026 "Lost precision while shifting addend for fused-multiply-add.");
1028 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
1031 if (newPartsCount == 1)
1032 fullSignificand[0] = significand.part;
1033 significand = savedSignificand;
1034 semantics = savedSemantics;
1036 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1043 exponent -= precision + 1;
1052 if (omsb > precision) {
1053 unsigned int bits, significantParts;
1056 bits = omsb - precision;
1058 lf =
shiftRight(fullSignificand, significantParts, bits);
1065 if (newPartsCount > 4)
1066 delete [] fullSignificand;
1068 return lost_fraction;
1073 unsigned int bit, i, partsCount;
1079 assert(semantics == rhs.semantics);
1081 lhsSignificand = significandParts();
1082 rhsSignificand = rhs.significandParts();
1083 partsCount = partCount();
1090 divisor = dividend + partsCount;
1093 for (i = 0; i < partsCount; i++) {
1094 dividend[i] = lhsSignificand[i];
1095 divisor[i] = rhsSignificand[i];
1096 lhsSignificand[i] = 0;
1099 exponent -= rhs.exponent;
1101 unsigned int precision = semantics->
precision;
1104 bit = precision -
APInt::tcMSB(divisor, partsCount) - 1;
1111 bit = precision -
APInt::tcMSB(dividend, partsCount) - 1;
1127 for (bit = precision; bit; bit -= 1) {
1151 return lost_fraction;
1154 unsigned int IEEEFloat::significandMSB()
const {
1158 unsigned int IEEEFloat::significandLSB()
const {
1169 return shiftRight(significandParts(), partCount(), bits);
1173 void IEEEFloat::shiftSignificandLeft(
unsigned int bits) {
1174 assert(bits < semantics->precision);
1177 unsigned int partsCount = partCount();
1190 assert(semantics == rhs.semantics);
1194 compare = exponent - rhs.exponent;
1204 else if (compare < 0)
1236 bool IEEEFloat::roundAwayFromZero(
roundingMode rounding_mode,
1238 unsigned int bit)
const {
1245 switch (rounding_mode) {
1280 omsb = significandMSB() + 1;
1286 exponentChange = omsb - semantics->
precision;
1290 if (exponent + exponentChange > semantics->
maxExponent)
1291 return handleOverflow(rounding_mode);
1295 if (exponent + exponentChange < semantics->minExponent)
1296 exponentChange = semantics->
minExponent - exponent;
1299 if (exponentChange < 0) {
1302 shiftSignificandLeft(-exponentChange);
1307 if (exponentChange > 0) {
1311 lf = shiftSignificandRight(exponentChange);
1316 if (omsb > (
unsigned) exponentChange)
1317 omsb -= exponentChange;
1337 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1341 incrementSignificand();
1342 omsb = significandMSB() + 1;
1345 if (omsb == (
unsigned) semantics->
precision + 1) {
1355 shiftSignificandRight(1);
1367 assert(omsb < semantics->precision);
1399 copySignificand(rhs);
1420 if (((sign ^ rhs.sign)!=0) != subtract) {
1441 subtract ^=
static_cast<bool>(sign ^ rhs.sign);
1444 bits = exponent - rhs.exponent;
1454 }
else if (bits > 0) {
1455 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1456 shiftSignificandLeft(1);
1459 lost_fraction = shiftSignificandRight(-bits - 1);
1460 temp_rhs.shiftSignificandLeft(1);
1465 carry = temp_rhs.subtractSignificand
1467 copySignificand(temp_rhs);
1470 carry = subtractSignificand
1489 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1490 carry = addSignificand(temp_rhs);
1492 lost_fraction = shiftSignificandRight(-bits);
1493 carry = addSignificand(rhs);
1501 return lost_fraction;
1521 copySignificand(rhs);
1555 copySignificand(rhs);
1606 copySignificand(rhs);
1634 fs = addOrSubtractSpecials(rhs, subtract);
1640 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1641 fs = normalize(rounding_mode, lost_fraction);
1650 if (category ==
fcZero) {
1651 if (rhs.category !=
fcZero || (sign == rhs.sign) == subtract)
1661 return addOrSubtract(rhs, rounding_mode,
false);
1667 return addOrSubtract(rhs, rounding_mode,
true);
1676 fs = multiplySpecials(rhs);
1679 lostFraction lost_fraction = multiplySignificand(rhs,
nullptr);
1680 fs = normalize(rounding_mode, lost_fraction);
1694 fs = divideSpecials(rhs);
1698 fs = normalize(rounding_mode, lost_fraction);
1710 unsigned int origSign = sign;
1716 int parts = partCount();
1746 fs = modSpecials(rhs);
1747 unsigned int origSign = sign;
1771 sign ^= multiplicand.sign;
1780 lost_fraction = multiplySignificand(multiplicand, &addend);
1781 fs = normalize(rounding_mode, lost_fraction);
1791 fs = multiplySpecials(multiplicand);
1801 fs = addOrSubtract(addend, rounding_mode,
false);
1828 MagicConstant.sign = sign;
1836 fs =
add(MagicConstant, rounding_mode);
1840 fs =
subtract(MagicConstant, rounding_mode);
1854 assert(semantics == rhs.semantics);
1886 if (sign == rhs.sign)
1901 if (sign != rhs.sign) {
1932 unsigned int newPartCount, oldPartCount;
1939 oldPartCount = partCount();
1942 bool X86SpecialNan =
false;
1943 if (&fromSemantics == &semX87DoubleExtended &&
1944 &toSemantics != &semX87DoubleExtended && category ==
fcNaN &&
1945 (!(*significandParts() & 0x8000000000000000ULL) ||
1946 !(*significandParts() & 0x4000000000000000ULL))) {
1949 X86SpecialNan =
true;
1958 int exponentChange = significandMSB() + 1 - fromSemantics.
precision;
1959 if (exponent + exponentChange < toSemantics.
minExponent)
1960 exponentChange = toSemantics.
minExponent - exponent;
1961 if (exponentChange < shift)
1962 exponentChange = shift;
1963 if (exponentChange < 0) {
1964 shift -= exponentChange;
1965 exponent += exponentChange;
1971 lostFraction =
shiftRight(significandParts(), oldPartCount, -shift);
1974 if (newPartCount > oldPartCount) {
1982 significand.parts = newParts;
1983 }
else if (newPartCount == 1 && oldPartCount != 1) {
1987 newPart = significandParts()[0];
1989 significand.part = newPart;
1993 semantics = &toSemantics;
2001 fs = normalize(rounding_mode, lostFraction);
2002 *losesInfo = (fs !=
opOK);
2003 }
else if (category ==
fcNaN) {
2004 *losesInfo = lostFraction !=
lfExactlyZero || X86SpecialNan;
2008 if (!X86SpecialNan && semantics == &semX87DoubleExtended)
2039 unsigned int dstPartsCount, truncatedBits;
2048 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2050 if (category ==
fcZero) {
2057 src = significandParts();
2066 truncatedBits = semantics->
precision -1U - exponent;
2070 unsigned int bits = exponent + 1U;
2076 if (bits < semantics->precision) {
2092 if (truncatedBits) {
2096 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2116 if (omsb == width &&
2127 if (omsb >= width + !isSigned)
2149 unsigned int width,
bool isSigned,
2153 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2157 unsigned int bits, dstPartsCount;
2160 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2162 if (category ==
fcNaN)
2167 bits = width - isSigned;
2170 if (sign && isSigned)
2182 unsigned int omsb, precision, dstCount;
2188 dst = significandParts();
2189 dstCount = partCount();
2194 if (precision <= omsb) {
2195 exponent = omsb - 1;
2200 exponent = precision - 1;
2205 return normalize(rounding_mode, lost_fraction);
2219 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2227 unsigned int srcCount,
bool isSigned,
2240 status = convertFromUnsignedParts(copy, srcCount, rounding_mode);
2244 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2253 unsigned int width,
bool isSigned,
2264 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2268 IEEEFloat::convertFromHexadecimalString(
StringRef s,
2277 unsigned partsCount = partCount();
2279 bool computedTrailingFraction =
false;
2292 assert(dot == end &&
"String contains multiple dots");
2298 if (hex_value == -1U)
2308 }
else if (!computedTrailingFraction) {
2310 computedTrailingFraction =
true;
2315 assert(p != end &&
"Hex strings require an exponent");
2316 assert((*p ==
'p' || *p ==
'P') &&
"Invalid character in significand");
2317 assert(p != begin &&
"Significand has no digits");
2318 assert((dot == end || p - begin != 1) &&
"Significand has no digits");
2321 if (p != firstSignificantDigit) {
2330 expAdjustment =
static_cast<int>(dot - firstSignificantDigit);
2331 if (expAdjustment < 0)
2333 expAdjustment = expAdjustment * 4 - 1;
2344 return normalize(rounding_mode, lost_fraction);
2348 IEEEFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
2349 unsigned sigPartCount,
int exp,
2351 unsigned int parts, pow5PartCount;
2362 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2364 for (;; parts *= 2) {
2366 unsigned int excessPrecision, truncatedBits;
2370 truncatedBits = excessPrecision;
2376 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2378 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2381 decSig.exponent +=
exp;
2385 unsigned int powHUerr;
2389 calcLostFraction = decSig.multiplySignificand(pow5,
nullptr);
2390 powHUerr = powStatus !=
opOK;
2392 calcLostFraction = decSig.divideSignificand(pow5);
2395 excessPrecision += (semantics->
minExponent - decSig.exponent);
2396 truncatedBits = excessPrecision;
2397 if (excessPrecision > calcSemantics.
precision)
2398 excessPrecision = calcSemantics.
precision;
2407 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
2412 excessPrecision, isNearest);
2415 if (HUdistance >= HUerr) {
2416 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2417 calcSemantics.
precision - excessPrecision,
2422 exponent = (decSig.exponent + semantics->
precision 2423 - (calcSemantics.
precision - excessPrecision));
2427 return normalize(rounding_mode, calcLostFraction);
2472 fs = handleOverflow(rounding_mode);
2490 fs = handleOverflow(rounding_mode);
2493 unsigned int partCount;
2517 if (p == str.
end()) {
2522 assert(decValue < 10U &&
"Invalid character in significand");
2524 val = val * 10 + decValue;
2531 partCount, partCount + 1,
false);
2535 if (decSignificand[partCount])
2540 fs = roundSignificandWithExponent(decSignificand, partCount,
2543 delete [] decSignificand;
2549 bool IEEEFloat::convertFromStringSpecials(
StringRef str) {
2578 if (convertFromStringSpecials(str))
2583 size_t slen = str.
size();
2584 sign = *p ==
'-' ? 1 : 0;
2585 if (*p ==
'-' || *p ==
'+') {
2588 assert(slen &&
"String has no digits");
2591 if (slen >= 2 && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
2592 assert(slen - 2 &&
"Invalid string");
2593 return convertFromHexadecimalString(
StringRef(p + 2, slen - 2),
2597 return convertFromDecimalString(
StringRef(p, slen), rounding_mode);
2635 memcpy (dst, upperCase ? infinityU: infinityL,
sizeof infinityU - 1);
2636 dst +=
sizeof infinityL - 1;
2640 memcpy (dst, upperCase ? NaNU: NaNL,
sizeof NaNU - 1);
2641 dst +=
sizeof NaNU - 1;
2646 *dst++ = upperCase ?
'X':
'x';
2648 if (hexDigits > 1) {
2650 memset (dst,
'0', hexDigits - 1);
2651 dst += hexDigits - 1;
2653 *dst++ = upperCase ?
'P':
'p';
2658 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2664 return static_cast<unsigned int>(dst - p);
2671 char *IEEEFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
2674 unsigned int count, valueBits, shift, partsCount, outputDigits;
2675 const char *hexDigitChars;
2681 *dst++ = upperCase ?
'X':
'x';
2686 significand = significandParts();
2687 partsCount = partCount();
2696 outputDigits = (valueBits - significandLSB () + 3) / 4;
2702 if (hexDigits < outputDigits) {
2708 bits = valueBits - hexDigits * 4;
2710 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
2712 outputDigits = hexDigits;
2722 while (outputDigits && count) {
2726 if (--count == partsCount)
2729 part = significand[
count] << shift;
2737 if (curDigits > outputDigits)
2738 curDigits = outputDigits;
2739 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
2740 outputDigits -= curDigits;
2750 }
while (*q ==
'0');
2754 memset (dst,
'0', outputDigits);
2755 dst += outputDigits;
2768 *dst++ = upperCase ?
'P':
'p';
2777 Arg.
isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign,
2781 return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign,
2784 Arg.significandParts(),
2785 Arg.significandParts() + Arg.partCount()));
2797 APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt()
const {
2801 uint64_t myexponent, mysignificand;
2804 myexponent = exponent+16383;
2805 mysignificand = significandParts()[0];
2806 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
2808 }
else if (category==
fcZero) {
2812 myexponent = 0x7fff;
2813 mysignificand = 0x8000000000000000ULL;
2816 myexponent = 0x7fff;
2817 mysignificand = significandParts()[0];
2821 words[0] = mysignificand;
2822 words[1] = ((uint64_t)(sign & 1) << 15) |
2823 (myexponent & 0x7fffLL);
2824 return APInt(80, words);
2827 APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt()
const {
2852 words[0] = *u.convertDoubleAPFloatToAPInt().
getRawData();
2868 words[1] = *v.convertDoubleAPFloatToAPInt().
getRawData();
2873 return APInt(128, words);
2876 APInt IEEEFloat::convertQuadrupleAPFloatToAPInt()
const {
2880 uint64_t myexponent, mysignificand, mysignificand2;
2883 myexponent = exponent+16383;
2884 mysignificand = significandParts()[0];
2885 mysignificand2 = significandParts()[1];
2886 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
2888 }
else if (category==
fcZero) {
2890 mysignificand = mysignificand2 = 0;
2892 myexponent = 0x7fff;
2893 mysignificand = mysignificand2 = 0;
2896 myexponent = 0x7fff;
2897 mysignificand = significandParts()[0];
2898 mysignificand2 = significandParts()[1];
2902 words[0] = mysignificand;
2903 words[1] = ((uint64_t)(sign & 1) << 63) |
2904 ((myexponent & 0x7fff) << 48) |
2905 (mysignificand2 & 0xffffffffffffLL);
2907 return APInt(128, words);
2910 APInt IEEEFloat::convertDoubleAPFloatToAPInt()
const {
2914 uint64_t myexponent, mysignificand;
2917 myexponent = exponent+1023;
2918 mysignificand = *significandParts();
2919 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
2921 }
else if (category==
fcZero) {
2930 mysignificand = *significandParts();
2933 return APInt(64, ((((uint64_t)(sign & 1) << 63) |
2934 ((myexponent & 0x7ff) << 52) |
2935 (mysignificand & 0xfffffffffffffLL))));
2938 APInt IEEEFloat::convertFloatAPFloatToAPInt()
const {
2942 uint32_t myexponent, mysignificand;
2945 myexponent = exponent+127;
2946 mysignificand = (
uint32_t)*significandParts();
2947 if (myexponent == 1 && !(mysignificand & 0x800000))
2949 }
else if (category==
fcZero) {
2958 mysignificand = (
uint32_t)*significandParts();
2961 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
2962 (mysignificand & 0x7fffff)));
2965 APInt IEEEFloat::convertHalfAPFloatToAPInt()
const {
2969 uint32_t myexponent, mysignificand;
2972 myexponent = exponent+15;
2973 mysignificand = (
uint32_t)*significandParts();
2974 if (myexponent == 1 && !(mysignificand & 0x400))
2976 }
else if (category==
fcZero) {
2985 mysignificand = (
uint32_t)*significandParts();
2988 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
2989 (mysignificand & 0x3ff)));
2998 return convertHalfAPFloatToAPInt();
3001 return convertFloatAPFloatToAPInt();
3004 return convertDoubleAPFloatToAPInt();
3007 return convertQuadrupleAPFloatToAPInt();
3010 return convertPPCDoubleDoubleAPFloatToAPInt();
3014 return convertF80LongDoubleAPFloatToAPInt();
3019 "Float semantics are not IEEEsingle");
3026 "Float semantics are not IEEEdouble");
3038 void IEEEFloat::initFromF80LongDoubleAPInt(
const APInt &api) {
3042 uint64_t myexponent = (i2 & 0x7fff);
3043 uint64_t mysignificand = i1;
3044 uint8_t myintegerbit = mysignificand >> 63;
3046 initialize(&semX87DoubleExtended);
3049 sign =
static_cast<unsigned int>(i2>>15);
3050 if (myexponent == 0 && mysignificand == 0) {
3053 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3056 }
else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3057 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3060 significandParts()[0] = mysignificand;
3061 significandParts()[1] = 0;
3064 exponent = myexponent - 16383;
3065 significandParts()[0] = mysignificand;
3066 significandParts()[1] = 0;
3072 void IEEEFloat::initFromPPCDoubleDoubleAPInt(
const APInt &api) {
3080 initFromDoubleAPInt(
APInt(64, i1));
3096 void IEEEFloat::initFromQuadrupleAPInt(
const APInt &api) {
3100 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3101 uint64_t mysignificand = i1;
3102 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3104 initialize(&semIEEEquad);
3107 sign =
static_cast<unsigned int>(i2>>63);
3108 if (myexponent==0 &&
3109 (mysignificand==0 && mysignificand2==0)) {
3112 }
else if (myexponent==0x7fff &&
3113 (mysignificand==0 && mysignificand2==0)) {
3116 }
else if (myexponent==0x7fff &&
3117 (mysignificand!=0 || mysignificand2 !=0)) {
3120 significandParts()[0] = mysignificand;
3121 significandParts()[1] = mysignificand2;
3124 exponent = myexponent - 16383;
3125 significandParts()[0] = mysignificand;
3126 significandParts()[1] = mysignificand2;
3130 significandParts()[1] |= 0x1000000000000LL;
3134 void IEEEFloat::initFromDoubleAPInt(
const APInt &api) {
3137 uint64_t myexponent = (i >> 52) & 0x7ff;
3138 uint64_t mysignificand = i & 0xfffffffffffffLL;
3140 initialize(&semIEEEdouble);
3143 sign =
static_cast<unsigned int>(i>>63);
3144 if (myexponent==0 && mysignificand==0) {
3147 }
else if (myexponent==0x7ff && mysignificand==0) {
3150 }
else if (myexponent==0x7ff && mysignificand!=0) {
3153 *significandParts() = mysignificand;
3156 exponent = myexponent - 1023;
3157 *significandParts() = mysignificand;
3161 *significandParts() |= 0x10000000000000LL;
3165 void IEEEFloat::initFromFloatAPInt(
const APInt &api) {
3168 uint32_t myexponent = (i >> 23) & 0xff;
3169 uint32_t mysignificand = i & 0x7fffff;
3171 initialize(&semIEEEsingle);
3175 if (myexponent==0 && mysignificand==0) {
3178 }
else if (myexponent==0xff && mysignificand==0) {
3181 }
else if (myexponent==0xff && mysignificand!=0) {
3184 *significandParts() = mysignificand;
3187 exponent = myexponent - 127;
3188 *significandParts() = mysignificand;
3192 *significandParts() |= 0x800000;
3196 void IEEEFloat::initFromHalfAPInt(
const APInt &api) {
3199 uint32_t myexponent = (i >> 10) & 0x1f;
3200 uint32_t mysignificand = i & 0x3ff;
3202 initialize(&semIEEEhalf);
3206 if (myexponent==0 && mysignificand==0) {
3209 }
else if (myexponent==0x1f && mysignificand==0) {
3212 }
else if (myexponent==0x1f && mysignificand!=0) {
3215 *significandParts() = mysignificand;
3218 exponent = myexponent - 15;
3219 *significandParts() = mysignificand;
3223 *significandParts() |= 0x400;
3232 if (Sem == &semIEEEhalf)
3233 return initFromHalfAPInt(api);
3234 if (Sem == &semIEEEsingle)
3235 return initFromFloatAPInt(api);
3236 if (Sem == &semIEEEdouble)
3237 return initFromDoubleAPInt(api);
3238 if (Sem == &semX87DoubleExtended)
3239 return initFromF80LongDoubleAPInt(api);
3240 if (Sem == &semIEEEquad)
3241 return initFromQuadrupleAPInt(api);
3242 if (Sem == &semPPCDoubleDoubleLegacy)
3243 return initFromPPCDoubleDoubleAPInt(api);
3261 unsigned PartCount = partCount();
3266 const unsigned NumUnusedHighBits =
3301 initFromAPInt(&Sem, API);
3319 void AdjustToPrecision(
APInt &significand,
3320 int &exp,
unsigned FormatPrecision) {
3324 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3326 if (bits <= bitsRequired)
return;
3328 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
3329 if (!tensRemovable)
return;
3331 exp += tensRemovable;
3336 if (tensRemovable & 1)
3338 tensRemovable >>= 1;
3339 if (!tensRemovable)
break;
3343 significand = significand.
udiv(divisor);
3351 int &exp,
unsigned FormatPrecision) {
3352 unsigned N = buffer.
size();
3353 if (N <= FormatPrecision)
return;
3356 unsigned FirstSignificant = N - FormatPrecision;
3363 if (buffer[FirstSignificant - 1] <
'5') {
3364 while (FirstSignificant < N && buffer[FirstSignificant] ==
'0')
3367 exp += FirstSignificant;
3368 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3374 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
3375 if (buffer[
I] ==
'9') {
3384 if (FirstSignificant == N) {
3385 exp += FirstSignificant;
3391 exp += FirstSignificant;
3392 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3397 unsigned FormatMaxPadding,
bool TruncateZero)
const {
3401 return append(Str,
"-Inf");
3403 return append(Str,
"+Inf");
3411 if (!FormatMaxPadding) {
3416 if (FormatPrecision > 1)
3417 Str.
append(FormatPrecision - 1,
'0');
3432 int exp = exponent - ((int) semantics->
precision - 1);
3439 if (!FormatPrecision) {
3447 FormatPrecision = 2 + semantics->
precision * 59 / 196;
3452 exp += trailingZeros;
3458 }
else if (exp > 0) {
3461 significand <<=
exp;
3475 unsigned precision = semantics->
precision + (137 * texp + 136) / 59;
3479 significand = significand.
zext(precision);
3480 APInt five_to_the_i(precision, 5);
3482 if (texp & 1) significand *= five_to_the_i;
3486 five_to_the_i *= five_to_the_i;
3490 AdjustToPrecision(significand, exp, FormatPrecision);
3496 APInt ten(precision, 10);
3497 APInt digit(precision, 0);
3499 bool inTrail =
true;
3500 while (significand != 0) {
3508 if (inTrail && !d) exp++;
3515 assert(!buffer.
empty() &&
"no characters in buffer!");
3519 AdjustToPrecision(buffer, exp, FormatPrecision);
3521 unsigned NDigits = buffer.
size();
3524 bool FormatScientific;
3525 if (!FormatMaxPadding)
3526 FormatScientific =
true;
3532 FormatScientific = ((
unsigned) exp > FormatMaxPadding ||
3533 NDigits + (
unsigned) exp > FormatPrecision);
3536 int MSD = exp + (int) (NDigits - 1);
3539 FormatScientific =
false;
3543 FormatScientific = ((
unsigned) -MSD) > FormatMaxPadding;
3549 if (FormatScientific) {
3550 exp += (NDigits - 1);
3554 if (NDigits == 1 && TruncateZero)
3557 for (
unsigned I = 1;
I != NDigits; ++
I)
3560 if (!TruncateZero && FormatPrecision > NDigits - 1)
3561 Str.
append(FormatPrecision - NDigits + 1,
'0');
3563 Str.
push_back(TruncateZero ?
'E' :
'e');
3566 if (exp < 0) exp = -
exp;
3569 expbuf.
push_back((
char) (
'0' + (exp % 10)));
3573 if (!TruncateZero && expbuf.
size() < 2)
3575 for (
unsigned I = 0,
E = expbuf.
size();
I !=
E; ++
I)
3582 for (
unsigned I = 0;
I != NDigits; ++
I)
3592 int NWholeDigits = exp + (int) NDigits;
3595 if (NWholeDigits > 0) {
3596 for (; I != (
unsigned) NWholeDigits; ++
I)
3600 unsigned NZeros = 1 + (
unsigned) -NWholeDigits;
3604 for (
unsigned Z = 1; Z != NZeros; ++Z)
3608 for (; I != NDigits; ++
I)
3619 if (significandLSB() != semantics->
precision - 1)
3633 reciprocal.significandLSB() == reciprocal.semantics->
precision - 1);
3636 *inv =
APFloat(reciprocal, *semantics);
3711 bool WillCrossBinadeBoundary =
3712 exponent != semantics->
minExponent && isSignificandAllZeros();
3730 if (WillCrossBinadeBoundary) {
3746 bool WillCrossBinadeBoundary = !
isDenormal() && isSignificandAllOnes();
3748 if (WillCrossBinadeBoundary) {
3753 "We can not increment an exponent beyond the maxExponent allowed" 3754 " by the given floating point semantics.");
3757 incrementSignificand();
3797 return Arg.exponent;
3802 Normalized.exponent += SignificandBits;
3804 return Normalized.exponent - SignificandBits;
3818 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
3821 X.exponent += std::min(
std::max(Exp, -MaxIncrement - 1), MaxIncrement);
3844 return scalbn(Val, -Exp, RM);
3850 assert(Semantics == &semPPCDoubleDouble);
3857 assert(Semantics == &semPPCDoubleDouble);
3861 : Semantics(&S), Floats(new
APFloat[2]{
APFloat(semIEEEdouble, I),
3863 assert(Semantics == &semPPCDoubleDouble);
3871 assert(Semantics == &semPPCDoubleDouble);
3877 Floats(new
APFloat[2]{std::move(First), std::move(Second)}) {
3878 assert(Semantics == &semPPCDoubleDouble);
3879 assert(&Floats[0].getSemantics() == &semIEEEdouble);
3880 assert(&Floats[1].getSemantics() == &semIEEEdouble);
3884 : Semantics(RHS.Semantics),
3888 assert(Semantics == &semPPCDoubleDouble);
3892 : Semantics(RHS.Semantics), Floats(
std::move(RHS.Floats)) {
3894 assert(Semantics == &semPPCDoubleDouble);
3898 if (Semantics == RHS.Semantics && RHS.Floats) {
3899 Floats[0] = RHS.Floats[0];
3900 Floats[1] = RHS.Floats[1];
3901 }
else if (
this != &RHS) {
3916 Status |= z.
add(c, RM);
3919 Floats[0] = std::move(z);
3920 Floats[1].makeZero(
false);
3924 auto AComparedToC = a.compareAbsoluteValue(c);
3926 Status |= z.
add(aa, RM);
3929 Status |= z.
add(c, RM);
3930 Status |= z.
add(a, RM);
3933 Status |= z.
add(a, RM);
3934 Status |= z.
add(c, RM);
3937 Floats[0] = std::move(z);
3938 Floats[1].makeZero(
false);
3943 Status |= zz.
add(cc, RM);
3947 Status |= Floats[1].subtract(z, RM);
3948 Status |= Floats[1].add(c, RM);
3949 Status |= Floats[1].add(zz, RM);
3953 Status |= Floats[1].subtract(z, RM);
3954 Status |= Floats[1].add(a, RM);
3955 Status |= Floats[1].add(zz, RM);
3965 Status |= zz.
add(c, RM);
3966 Status |= q.
add(z, RM);
3969 Status |= zz.add(q, RM);
3970 Status |= zz.add(aa, RM);
3971 Status |= zz.add(cc, RM);
3972 if (zz.isZero() && !zz.isNegative()) {
3973 Floats[0] = std::move(z);
3974 Floats[1].makeZero(
false);
3978 Status |= Floats[0].add(zz, RM);
3979 if (!Floats[0].isFinite()) {
3980 Floats[1].makeZero(
false);
3983 Floats[1] = std::move(z);
3984 Status |= Floats[1].subtract(Floats[0], RM);
3985 Status |= Floats[1].add(zz, RM);
4025 APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]),
C(RHS.Floats[0]),
4033 return Out.addImpl(A, AA,
C, CC, RM);
4038 return addWithSpecial(*
this, RHS, *
this, RM);
4051 const auto &LHS = *
this;
4078 Out.
makeNaN(
false,
false,
nullptr);
4090 "Special cases not handled exhaustively");
4093 APFloat A = Floats[0],
B = Floats[1],
C = RHS.Floats[0],
D = RHS.Floats[1];
4099 Floats[1].makeZero(
false);
4115 Status |= V.
add(W, RM);
4117 Status |= Tau.
add(V, RM);
4121 Status |= U.
add(Tau, RM);
4125 Floats[1].makeZero(
false);
4129 Status |= T.
add(Tau, RM);
4137 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4146 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4155 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4166 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4176 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4184 Floats[0].changeSign();
4185 Floats[1].changeSign();
4190 auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]);
4193 Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]);
4195 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4196 auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative();
4197 if (Against && !RHSAgainst)
4199 if (!Against && RHSAgainst)
4201 if (!Against && !RHSAgainst)
4203 if (Against && RHSAgainst)
4210 return Floats[0].getCategory();
4216 Floats[0].makeInf(Neg);
4217 Floats[1].makeZero(
false);
4221 Floats[0].makeZero(Neg);
4222 Floats[1].makeZero(
false);
4226 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4227 Floats[0] =
APFloat(semIEEEdouble,
APInt(64, 0x7fefffffffffffffull));
4228 Floats[1] =
APFloat(semIEEEdouble,
APInt(64, 0x7c8ffffffffffffeull));
4234 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4235 Floats[0].makeSmallest(Neg);
4236 Floats[1].makeZero(
false);
4240 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4241 Floats[0] =
APFloat(semIEEEdouble,
APInt(64, 0x0360000000000000ull));
4243 Floats[0].changeSign();
4244 Floats[1].makeZero(
false);
4248 Floats[0].makeNaN(SNaN, Neg, fill);
4249 Floats[1].makeZero(
false);
4253 auto Result = Floats[0].compare(RHS.Floats[0]);
4256 return Floats[1].compare(RHS.Floats[1]);
4261 return Floats[0].bitwiseIsEqual(RHS.Floats[0]) &&
4262 Floats[1].bitwiseIsEqual(RHS.Floats[1]);
4272 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4274 Floats[0].bitcastToAPInt().getRawData()[0],
4275 Floats[1].bitcastToAPInt().getRawData()[0],
4277 return APInt(128, 2, Data);
4282 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4283 APFloat Tmp(semPPCDoubleDoubleLegacy);
4290 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4292 auto Ret = Tmp.
next(nextDown);
4299 unsigned int Width,
bool IsSigned,
4301 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4303 .convertToInteger(Input, Width, IsSigned, RM, IsExact);
4309 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4310 APFloat Tmp(semPPCDoubleDoubleLegacy);
4318 unsigned int InputSize,
4320 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4321 APFloat Tmp(semPPCDoubleDoubleLegacy);
4329 unsigned int InputSize,
4331 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4332 APFloat Tmp(semPPCDoubleDoubleLegacy);
4339 unsigned int HexDigits,
4342 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4344 .convertToHexString(DST, HexDigits, UpperCase, RM);
4349 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
4351 Floats[0].compare(Floats[0] + Floats[1]) !=
cmpEqual);
4371 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4372 return Floats[0].isInteger() && Floats[1].isInteger();
4376 unsigned FormatPrecision,
4377 unsigned FormatMaxPadding,
4378 bool TruncateZero)
const {
4379 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4381 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
4385 assert(Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4389 APFloat Inv(semPPCDoubleDoubleLegacy);
4396 assert(Arg.Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4398 scalbn(Arg.Floats[1], Exp, RM));
4403 assert(Arg.Semantics == &semPPCDoubleDouble &&
"Unexpected Semantics");
4405 APFloat Second = Arg.Floats[1];
4407 Second =
scalbn(Second, -Exp, RM);
4408 return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second));
4414 if (usesLayout<IEEEFloat>(Semantics)) {
4418 if (usesLayout<DoubleAPFloat>(Semantics)) {
4432 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.
getSemantics()))
4434 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.
getSemantics()))
4451 usesLayout<IEEEFloat>(ToSemantics))
4452 return U.IEEE.convert(ToSemantics, RM, losesInfo);
4454 usesLayout<DoubleAPFloat>(ToSemantics)) {
4455 assert(&ToSemantics == &semPPCDoubleDouble);
4456 auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo);
4457 *
this =
APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
4461 usesLayout<IEEEFloat>(ToSemantics)) {
4462 auto Ret = getIEEE().
convert(ToSemantics, RM, losesInfo);
4463 *
this =
APFloat(std::move(getIEEE()), ToSemantics);
4494 OS << Buffer <<
"\n";
4497 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4511 bool *isExact)
const {
4515 rounding_mode, isExact);
4517 result =
APInt(bitWidth, parts);
4523 #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)
static StringRef::iterator skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, StringRef::iterator *dot)
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.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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.
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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.
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)
opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
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
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)
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...
opStatus convertFromString(StringRef, roundingMode)
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.
static int readExponent(StringRef::iterator begin, StringRef::iterator end)
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)
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.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
static unsigned int powerOf5(APFloatBase::integerPart *dst, unsigned int power)
void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
opStatus next(bool nextDown)
IEEE-754R 5.3.1: nextUp/nextDown.
opStatus subtract(const APFloat &RHS, roundingMode RM)
opStatus convertFromString(StringRef, roundingMode)
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[]
static int totalExponent(StringRef::iterator p, StringRef::iterator end, int exponentAdjustment)
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 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)
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.
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)
signed short ExponentType
A signed type to represent a floating point numbers unbiased exponent.
void makeZero(bool Neg=false)
static const fltSemantics & IEEEsingle() LLVM_READNONE
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)
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 lostFraction trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, unsigned int digitValue)
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 void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringRef > StandardNames)
Initialize the set of available library functions based on the specified target triple.
static const char hexDigitsUpper[]
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
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.
bool getExactInverse(APFloat *inv) const
static lostFraction combineLostFractions(lostFraction moreSignificant, lostFraction lessSignificant)
static const fltSemantics semIEEEquad
amdgpu Simplify well known AMD library false Value Value * Arg
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.
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
opStatus add(const APFloat &RHS, roundingMode RM)
opStatus
IEEE-754R 7: Default exception handling.
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
LLVM_NODISCARD bool empty() const
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 double.
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)
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 interpretDecimal(StringRef::iterator begin, StringRef::iterator end, decimalInfo *D)
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
opStatus convertFromString(StringRef, roundingMode)