23#include "llvm/Config/llvm-config.h"
31#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
33 if (usesLayout<IEEEFloat>(getSemantics())) \
34 return U.IEEE.METHOD_CALL; \
35 if (usesLayout<DoubleAPFloat>(getSemantics())) \
36 return U.Double.METHOD_CALL; \
37 llvm_unreachable("Unexpected semantics"); \
48#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
296 return A.maxExponent <=
B.maxExponent &&
A.minExponent >=
B.minExponent &&
297 A.precision <=
B.precision;
377 if (Src.maxExponent >= Dst.maxExponent || Src.minExponent <= Dst.minExponent)
385 return Dst.precision >= Src.precision;
425static inline unsigned int
439 unsigned int absExponent;
440 const unsigned int overlargeExponent = 24000;
444 if (p == end || ((*p ==
'-' || *p ==
'+') && (p + 1) == end)) {
448 isNegative = (*p ==
'-');
449 if (*p ==
'-' || *p ==
'+') {
456 if (absExponent >= 10U)
457 return createError(
"Invalid character in exponent");
459 for (; p != end; ++p) {
464 return createError(
"Invalid character in exponent");
466 absExponent = absExponent * 10U +
value;
467 if (absExponent >= overlargeExponent) {
468 absExponent = overlargeExponent;
474 return -(int) absExponent;
476 return (
int) absExponent;
483 int exponentAdjustment) {
484 int unsignedExponent;
485 bool negative, overflow;
491 negative = *p ==
'-';
492 if (*p ==
'-' || *p ==
'+') {
498 unsignedExponent = 0;
500 for (; p != end; ++p) {
505 return createError(
"Invalid character in exponent");
507 unsignedExponent = unsignedExponent * 10 +
value;
508 if (unsignedExponent > 32767) {
514 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
518 exponent = unsignedExponent;
520 exponent = -exponent;
521 exponent += exponentAdjustment;
522 if (exponent > 32767 || exponent < -32768)
527 exponent = negative ? -32768: 32767;
537 while (p != end && *p ==
'0')
540 if (p != end && *p ==
'.') {
543 if (end - begin == 1)
546 while (p != end && *p ==
'0')
579 return PtrOrErr.takeError();
582 D->firstSigDigit = p;
584 D->normalizedExponent = 0;
586 for (; p != end; ++p) {
589 return createError(
"String contains multiple dots");
599 if (*p !=
'e' && *p !=
'E')
600 return createError(
"Invalid character in significand");
603 if (dot != end && p - begin == 1)
609 return ExpOrErr.takeError();
610 D->exponent = *ExpOrErr;
618 if (p !=
D->firstSigDigit) {
624 while (p != begin && *p ==
'0');
625 while (p != begin && *p ==
'.');
630 D->normalizedExponent = (
D->exponent +
632 - (dot >
D->firstSigDigit && dot < p)));
644 unsigned int digitValue) {
645 unsigned int hexDigit;
651 else if (digitValue < 8 && digitValue > 0)
655 while (p != end && (*p ==
'0' || *p ==
'.'))
659 return createError(
"Invalid trailing hexadecimal fraction!");
661 hexDigit = hexDigitValue(*p);
665 if (hexDigit == UINT_MAX)
675 unsigned int partCount,
704 return lost_fraction;
719 return moreSignificant;
730HUerrBound(
bool inexactMultiply,
unsigned int HUerr1,
unsigned int HUerr2)
732 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
734 if (HUerr1 + HUerr2 == 0)
735 return inexactMultiply * 2;
737 return inexactMultiply + 2 * (HUerr1 + HUerr2);
746 unsigned int count, partBits;
763 if (part - boundary <= boundary - part)
764 return part - boundary;
766 return boundary - part;
769 if (part == boundary) {
775 }
else if (part == boundary - 1) {
792 pow5s[0] = 78125 * 5;
794 unsigned int partsCount = 1;
802 *p1 = firstEightPowers[power & 7];
808 for (
unsigned int n = 0; power; power >>= 1, n++) {
812 partsCount, partsCount);
814 if (pow5[partsCount - 1] == 0)
822 result += partsCount;
823 if (p2[result - 1] == 0)
848static const char NaNL[] =
"nan";
849static const char NaNU[] =
"NAN";
856 const char *hexDigitChars)
858 unsigned int result =
count;
864 dst[
count] = hexDigitChars[part & 0xf];
908 switch (
X.getCategory()) {
918 if (
X.isDenormal() ||
X.isSmallestNormalized())
921 if (
X.getExactLog2() != INT_MIN)
929void IEEEFloat::initialize(
const fltSemantics *ourSemantics) {
932 semantics = ourSemantics;
938void IEEEFloat::freeSignificand() {
940 delete [] significand.parts;
943void IEEEFloat::assign(
const IEEEFloat &rhs) {
944 assert(semantics == rhs.semantics);
947 category = rhs.category;
948 exponent = rhs.exponent;
950 copySignificand(rhs);
953void IEEEFloat::copySignificand(
const IEEEFloat &rhs) {
955 assert(rhs.partCount() >= partCount());
970 "This floating point format does not support signed values");
974 exponent = exponentNaN();
977 unsigned numParts = partCount();
990 fill = &fill_storage;
994 if (!
fill ||
fill->getNumWords() < numParts)
998 std::min(
fill->getNumWords(), numParts));
1001 unsigned bitsToPreserve = semantics->
precision - 1;
1002 unsigned part = bitsToPreserve / 64;
1003 bitsToPreserve %= 64;
1004 significand[part] &= ((1ULL << bitsToPreserve) - 1);
1005 for (part++; part != numParts; ++part)
1006 significand[part] = 0;
1038 if (semantics != rhs.semantics) {
1040 initialize(rhs.semantics);
1051 semantics = rhs.semantics;
1052 significand = rhs.significand;
1053 exponent = rhs.exponent;
1054 category = rhs.category;
1072 significandMSB() == 0;
1077 isSignificandAllZerosExceptMSB();
1080unsigned int IEEEFloat::getNumHighBits()
const {
1087 const unsigned int NumHighBits = (semantics->
precision > 1)
1093bool IEEEFloat::isSignificandAllOnes()
const {
1098 for (
unsigned i = 0; i < PartCount - 1; i++)
1103 const unsigned NumHighBits = getNumHighBits();
1104 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
1105 "Can not have more high bits to fill than integerPartWidth");
1108 if ((semantics->
precision <= 1) || (~(Parts[PartCount - 1] | HighBitFill)))
1114bool IEEEFloat::isSignificandAllOnesExceptLSB()
const {
1123 for (
unsigned i = 0; i < PartCount - 1; i++) {
1124 if (~Parts[i] & ~
unsigned{!i})
1129 const unsigned NumHighBits = getNumHighBits();
1130 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
1131 "Can not have more high bits to fill than integerPartWidth");
1134 if (~(Parts[PartCount - 1] | HighBitFill | 0x1))
1140bool IEEEFloat::isSignificandAllZeros()
const {
1146 for (
unsigned i = 0; i < PartCount - 1; i++)
1151 const unsigned NumHighBits = getNumHighBits();
1153 "clear than integerPartWidth");
1154 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
1156 if ((semantics->
precision > 1) && (Parts[PartCount - 1] & HighBitMask))
1162bool IEEEFloat::isSignificandAllZerosExceptMSB()
const {
1166 for (
unsigned i = 0; i < PartCount - 1; i++) {
1171 const unsigned NumHighBits = getNumHighBits();
1174 return ((semantics->
precision <= 1) || (Parts[PartCount - 1] == MSBMask));
1185 ? isSignificandAllOnesExceptLSB()
1190 return IsMaxExp && isSignificandAllOnes();
1205 if (semantics != rhs.semantics ||
1206 category != rhs.category ||
1215 return std::equal(significandParts(), significandParts() + partCount(),
1216 rhs.significandParts());
1220 initialize(&ourSemantics);
1225 significandParts()[0] =
value;
1230 initialize(&ourSemantics);
1246 initialize(rhs.semantics);
1251 *
this = std::move(rhs);
1256unsigned int IEEEFloat::partCount()
const {
1261 return const_cast<IEEEFloat *
>(
this)->significandParts();
1265 if (partCount() > 1)
1266 return significand.parts;
1268 return &significand.part;
1271void IEEEFloat::zeroSignificand() {
1276void IEEEFloat::incrementSignificand() {
1290 parts = significandParts();
1292 assert(semantics == rhs.semantics);
1293 assert(exponent == rhs.exponent);
1295 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
1304 parts = significandParts();
1306 assert(semantics == rhs.semantics);
1307 assert(exponent == rhs.exponent);
1316lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs,
1318 bool ignoreAddend) {
1320 unsigned int partsCount, newPartsCount, precision;
1327 assert(semantics == rhs.semantics);
1335 if (newPartsCount > 4)
1338 fullSignificand = scratch;
1340 lhsSignificand = significandParts();
1341 partsCount = partCount();
1344 rhs.significandParts(), partsCount, partsCount);
1347 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1348 exponent += rhs.exponent;
1362 if (!ignoreAddend && addend.isNonZero()) {
1366 Significand savedSignificand = significand;
1370 unsigned int extendedPrecision;
1373 extendedPrecision = 2 * precision + 1;
1374 if (omsb != extendedPrecision - 1) {
1375 assert(extendedPrecision > omsb);
1377 (extendedPrecision - 1) - omsb);
1378 exponent -= (extendedPrecision - 1) - omsb;
1382 extendedSemantics = *semantics;
1383 extendedSemantics.
precision = extendedPrecision;
1385 if (newPartsCount == 1)
1386 significand.part = fullSignificand[0];
1388 significand.parts = fullSignificand;
1389 semantics = &extendedSemantics;
1403 lost_fraction = extendedAddend.shiftSignificandRight(1);
1405 "Lost precision while shifting addend for fused-multiply-add.");
1407 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
1410 if (newPartsCount == 1)
1411 fullSignificand[0] = significand.part;
1412 significand = savedSignificand;
1413 semantics = savedSemantics;
1415 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1422 exponent -= precision + 1;
1431 if (omsb > precision) {
1432 unsigned int bits, significantParts;
1435 bits = omsb - precision;
1437 lf =
shiftRight(fullSignificand, significantParts, bits);
1444 if (newPartsCount > 4)
1445 delete [] fullSignificand;
1447 return lost_fraction;
1450lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs) {
1456 return multiplySignificand(rhs,
IEEEFloat(*semantics), !semantics->
hasZero);
1460lostFraction IEEEFloat::divideSignificand(
const IEEEFloat &rhs) {
1461 unsigned int bit, i, partsCount;
1467 assert(semantics == rhs.semantics);
1469 lhsSignificand = significandParts();
1470 rhsSignificand = rhs.significandParts();
1471 partsCount = partCount();
1478 divisor = dividend + partsCount;
1481 for (i = 0; i < partsCount; i++) {
1482 dividend[i] = lhsSignificand[i];
1483 divisor[i] = rhsSignificand[i];
1484 lhsSignificand[i] = 0;
1487 exponent -= rhs.exponent;
1489 unsigned int precision = semantics->
precision;
1492 bit = precision -
APInt::tcMSB(divisor, partsCount) - 1;
1499 bit = precision -
APInt::tcMSB(dividend, partsCount) - 1;
1515 for (bit = precision; bit; bit -= 1) {
1539 return lost_fraction;
1542unsigned int IEEEFloat::significandMSB()
const {
1546unsigned int IEEEFloat::significandLSB()
const {
1551lostFraction IEEEFloat::shiftSignificandRight(
unsigned int bits) {
1557 return shiftRight(significandParts(), partCount(), bits);
1561void IEEEFloat::shiftSignificandLeft(
unsigned int bits) {
1562 assert(bits < semantics->precision ||
1563 (semantics->
precision == 1 && bits <= 1));
1566 unsigned int partsCount = partCount();
1578 assert(semantics == rhs.semantics);
1582 compare = exponent - rhs.exponent;
1649bool IEEEFloat::roundAwayFromZero(
roundingMode rounding_mode,
1651 unsigned int bit)
const {
1658 switch (rounding_mode) {
1696 omsb = significandMSB() + 1;
1703 exponentChange = omsb - semantics->
precision;
1707 if (exponent + exponentChange > semantics->
maxExponent)
1708 return handleOverflow(rounding_mode);
1712 if (exponent + exponentChange < semantics->minExponent)
1713 exponentChange = semantics->
minExponent - exponent;
1716 if (exponentChange < 0) {
1719 shiftSignificandLeft(-exponentChange);
1724 if (exponentChange > 0) {
1728 lf = shiftSignificandRight(exponentChange);
1733 if (omsb > (
unsigned) exponentChange)
1734 omsb -= exponentChange;
1744 exponent == semantics->
maxExponent && isSignificandAllOnes())
1745 return handleOverflow(rounding_mode);
1766 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1770 incrementSignificand();
1771 omsb = significandMSB() + 1;
1774 if (omsb == (
unsigned) semantics->
precision + 1) {
1785 shiftSignificandRight(1);
1794 exponent == semantics->
maxExponent && isSignificandAllOnes())
1795 return handleOverflow(rounding_mode);
1804 assert(omsb < semantics->precision);
1879lostFraction IEEEFloat::addOrSubtractSignificand(
const IEEEFloat &rhs,
1887 subtract ^=
static_cast<bool>(sign ^ rhs.sign);
1890 bits = exponent - rhs.exponent;
1896 "This floating point format does not support signed values");
1899 bool lost_fraction_is_from_rhs =
false;
1903 else if (bits > 0) {
1904 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
1905 lost_fraction_is_from_rhs =
true;
1906 shiftSignificandLeft(1);
1908 lost_fraction = shiftSignificandRight(-bits - 1);
1909 temp_rhs.shiftSignificandLeft(1);
1916 lost_fraction !=
lfExactlyZero && !lost_fraction_is_from_rhs;
1925 carry = temp_rhs.subtractSignificand(*
this, borrow);
1926 copySignificand(temp_rhs);
1929 bool borrow = lost_fraction !=
lfExactlyZero && lost_fraction_is_from_rhs;
1938 carry = subtractSignificand(temp_rhs, borrow);
1941 if (lost_fraction !=
lfExactlyZero && lost_fraction_is_from_rhs) {
1955 lost_fraction = temp_rhs.shiftSignificandRight(bits);
1956 carry = addSignificand(temp_rhs);
1958 lost_fraction = shiftSignificandRight(-bits);
1959 carry = addSignificand(rhs);
1967 return lost_fraction;
2156 fs = addOrSubtractSpecials(rhs,
subtract);
2162 lost_fraction = addOrSubtractSignificand(rhs,
subtract);
2163 fs = normalize(rounding_mode, lost_fraction);
2172 if (category ==
fcZero) {
2186 return addOrSubtract(rhs, rounding_mode,
false);
2192 return addOrSubtract(rhs, rounding_mode,
true);
2201 fs = multiplySpecials(rhs);
2207 fs = normalize(rounding_mode, lost_fraction);
2221 fs = divideSpecials(rhs);
2227 fs = normalize(rounding_mode, lost_fraction);
2238 unsigned int origSign = sign;
2241 fs = remainderSpecials(rhs);
2348 fs = modSpecials(rhs);
2349 unsigned int origSign = sign;
2370 if (!semantics->
hasZero && this->isSmallest())
2390 sign ^= multiplicand.sign;
2399 lost_fraction = multiplySignificand(multiplicand, addend);
2400 fs = normalize(rounding_mode, lost_fraction);
2413 fs = multiplySpecials(multiplicand);
2423 fs = addOrSubtract(addend, rounding_mode,
false);
2497 MagicConstant.sign = sign;
2503 fs =
add(MagicConstant, rounding_mode);
2507 subtract(MagicConstant, rounding_mode);
2520 assert(semantics == rhs.semantics);
2552 if (sign == rhs.sign)
2567 if (sign != rhs.sign) {
2598 unsigned int newPartCount, oldPartCount;
2606 oldPartCount = partCount();
2609 bool X86SpecialNan =
false;
2612 (!(*significandParts() & 0x8000000000000000ULL) ||
2613 !(*significandParts() & 0x4000000000000000ULL))) {
2616 X86SpecialNan =
true;
2627 int omsb = significandMSB() + 1;
2628 int exponentChange = omsb - fromSemantics.
precision;
2629 if (exponent + exponentChange < toSemantics.
minExponent)
2630 exponentChange = toSemantics.
minExponent - exponent;
2631 if (exponentChange < shift)
2632 exponentChange = shift;
2633 if (exponentChange < 0) {
2634 shift -= exponentChange;
2635 exponent += exponentChange;
2636 }
else if (omsb <= -shift) {
2637 exponentChange = omsb + shift - 1;
2638 shift -= exponentChange;
2639 exponent += exponentChange;
2650 if (newPartCount > oldPartCount) {
2658 significand.parts = newParts;
2659 }
else if (newPartCount == 1 && oldPartCount != 1) {
2663 newPart = significandParts()[0];
2665 significand.part = newPart;
2669 semantics = &toSemantics;
2678 *losesInfo = (fs !=
opOK);
2679 }
else if (category ==
fcNaN) {
2714 }
else if (category ==
fcZero &&
2747 unsigned int dstPartsCount, truncatedBits;
2756 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2758 if (category ==
fcZero) {
2765 src = significandParts();
2774 truncatedBits = semantics->
precision -1U - exponent;
2778 unsigned int bits = exponent + 1U;
2784 if (bits < semantics->precision) {
2786 truncatedBits = semantics->
precision - bits;
2800 if (truncatedBits) {
2804 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2824 if (omsb == width &&
2861 fs = convertToSignExtendedInteger(parts, width,
isSigned, rounding_mode,
2865 unsigned int bits, dstPartsCount;
2868 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2870 if (category ==
fcNaN)
2890 unsigned int omsb, precision, dstCount;
2896 dst = significandParts();
2897 dstCount = partCount();
2902 if (precision <= omsb) {
2903 exponent = omsb - 1;
2908 exponent = precision - 1;
2913 return normalize(rounding_mode, lost_fraction);
2927 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2931IEEEFloat::convertFromHexadecimalString(
StringRef s,
2940 unsigned partsCount = partCount();
2942 bool computedTrailingFraction =
false;
2950 return PtrOrErr.takeError();
2959 return createError(
"String contains multiple dots");
2964 hex_value = hexDigitValue(*p);
2965 if (hex_value == UINT_MAX)
2975 }
else if (!computedTrailingFraction) {
2978 return FractOrErr.takeError();
2979 lost_fraction = *FractOrErr;
2980 computedTrailingFraction =
true;
2986 return createError(
"Hex strings require an exponent");
2987 if (*p !=
'p' && *p !=
'P')
2988 return createError(
"Invalid character in significand");
2991 if (dot != end && p - begin == 1)
2995 if (p != firstSignificantDigit) {
3004 expAdjustment =
static_cast<int>(
dot - firstSignificantDigit);
3005 if (expAdjustment < 0)
3007 expAdjustment = expAdjustment * 4 - 1;
3017 return ExpOrErr.takeError();
3018 exponent = *ExpOrErr;
3021 return normalize(rounding_mode, lost_fraction);
3025IEEEFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
3026 unsigned sigPartCount,
int exp,
3028 unsigned int parts, pow5PartCount;
3039 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
3041 for (;; parts *= 2) {
3043 unsigned int excessPrecision, truncatedBits;
3047 truncatedBits = excessPrecision;
3050 decSig.makeZero(sign);
3053 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
3055 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
3058 decSig.exponent += exp;
3062 unsigned int powHUerr;
3066 calcLostFraction = decSig.multiplySignificand(pow5);
3067 powHUerr = powStatus !=
opOK;
3069 calcLostFraction = decSig.divideSignificand(pow5);
3072 excessPrecision += (semantics->
minExponent - decSig.exponent);
3073 truncatedBits = excessPrecision;
3074 if (excessPrecision > calcSemantics.
precision)
3075 excessPrecision = calcSemantics.
precision;
3084 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
3089 excessPrecision, isNearest);
3092 if (HUdistance >= HUerr) {
3093 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
3094 calcSemantics.
precision - excessPrecision,
3099 exponent = (decSig.exponent + semantics->
precision
3100 - (calcSemantics.
precision - excessPrecision));
3104 return normalize(rounding_mode, calcLostFraction);
3117 return std::move(Err);
3153 }
else if (
D.normalizedExponent - 1 > INT_MAX / 42039) {
3154 fs = handleOverflow(rounding_mode);
3160 }
else if (
D.normalizedExponent - 1 < INT_MIN / 42039 ||
3161 (
D.normalizedExponent + 1) * 28738 <=
3169 }
else if ((
D.normalizedExponent - 1) * 42039
3172 fs = handleOverflow(rounding_mode);
3175 unsigned int partCount;
3181 partCount =
static_cast<unsigned int>(
D.lastSigDigit -
D.firstSigDigit) + 1;
3199 if (p == str.
end()) {
3204 if (decValue >= 10U) {
3205 delete[] decSignificand;
3206 return createError(
"Invalid character in significand");
3209 val = val * 10 + decValue;
3212 }
while (p <=
D.lastSigDigit && multiplier <= (~ (
integerPart) 0 - 9) / 10);
3216 partCount, partCount + 1,
false);
3220 if (decSignificand[partCount])
3222 }
while (p <=
D.lastSigDigit);
3225 fs = roundSignificandWithExponent(decSignificand, partCount,
3226 D.exponent, rounding_mode);
3228 delete [] decSignificand;
3234bool IEEEFloat::convertFromStringSpecials(
StringRef str) {
3235 const size_t MIN_NAME_SIZE = 3;
3237 if (str.
size() < MIN_NAME_SIZE)
3240 if (str ==
"inf" || str ==
"INFINITY" || str ==
"+Inf") {
3247 if (str.
size() < MIN_NAME_SIZE)
3250 if (str ==
"inf" || str ==
"INFINITY" || str ==
"Inf") {
3259 if (str.
size() < MIN_NAME_SIZE)
3266 makeNaN(IsSignaling, IsNegative);
3271 if (str.
front() ==
'(') {
3273 if (str.
size() <= 2 || str.
back() !=
')')
3280 unsigned Radix = 10;
3281 if (str[0] ==
'0') {
3282 if (str.
size() > 1 && tolower(str[1]) ==
'x') {
3293 makeNaN(IsSignaling, IsNegative, &Payload);
3307 if (convertFromStringSpecials(str))
3312 size_t slen = str.
size();
3313 sign = *p ==
'-' ? 1 : 0;
3316 "This floating point format does not support signed values");
3318 if (*p ==
'-' || *p ==
'+') {
3325 if (slen >= 2 && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
3328 return convertFromHexadecimalString(
StringRef(p + 2, slen - 2),
3332 return convertFromDecimalString(
StringRef(p, slen), rounding_mode);
3376 dst +=
sizeof NaNU - 1;
3381 *dst++ = upperCase ?
'X':
'x';
3383 if (hexDigits > 1) {
3385 memset (dst,
'0', hexDigits - 1);
3386 dst += hexDigits - 1;
3388 *dst++ = upperCase ?
'P':
'p';
3393 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
3399 return static_cast<unsigned int>(dst - p);
3406char *IEEEFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
3409 unsigned int count, valueBits, shift, partsCount, outputDigits;
3410 const char *hexDigitChars;
3416 *dst++ = upperCase ?
'X':
'x';
3421 significand = significandParts();
3422 partsCount = partCount();
3431 outputDigits = (valueBits - significandLSB () + 3) / 4;
3437 if (hexDigits < outputDigits) {
3443 bits = valueBits - hexDigits * 4;
3445 roundUp = roundAwayFromZero(rounding_mode, fraction, bits);
3447 outputDigits = hexDigits;
3457 while (outputDigits &&
count) {
3461 if (--
count == partsCount)
3464 part = significand[
count] << shift;
3472 if (curDigits > outputDigits)
3473 curDigits = outputDigits;
3474 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
3475 outputDigits -= curDigits;
3484 *
q = hexDigitChars[hexDigitValue (*q) + 1];
3485 }
while (*q ==
'0');
3489 memset (dst,
'0', outputDigits);
3490 dst += outputDigits;
3503 *dst++ = upperCase ?
'P':
'p';
3519 Arg.significandParts(),
3520 Arg.significandParts() + Arg.partCount()));
3532APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt()
const {
3536 uint64_t myexponent, mysignificand;
3539 myexponent = exponent+16383;
3540 mysignificand = significandParts()[0];
3541 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
3543 }
else if (category==
fcZero) {
3547 myexponent = 0x7fff;
3548 mysignificand = 0x8000000000000000ULL;
3551 myexponent = 0x7fff;
3552 mysignificand = significandParts()[0];
3556 words[0] = mysignificand;
3557 words[1] = ((
uint64_t)(sign & 1) << 15) |
3558 (myexponent & 0x7fffLL);
3559 return APInt(80, words);
3562APInt IEEEFloat::convertPPCDoubleDoubleLegacyAPFloatToAPInt()
const {
3587 words[0] = *
u.convertDoubleAPFloatToAPInt().getRawData();
3593 if (
u.isFiniteNonZero() && losesInfo) {
3603 words[1] = *
v.convertDoubleAPFloatToAPInt().getRawData();
3608 return APInt(128, words);
3611template <const fltSemantics &S>
3612APInt IEEEFloat::convertIEEEFloatToAPInt()
const {
3616 constexpr unsigned int trailing_significand_bits = S.precision - 1;
3617 constexpr int integer_bit_part = trailing_significand_bits /
integerPartWidth;
3620 constexpr uint64_t significand_mask = integer_bit - 1;
3621 constexpr unsigned int exponent_bits =
3622 trailing_significand_bits ? (S.sizeInBits - 1 - trailing_significand_bits)
3624 static_assert(exponent_bits < 64);
3632 myexponent = exponent + bias;
3633 std::copy_n(significandParts(), mysignificand.size(),
3634 mysignificand.begin());
3635 if (myexponent == 1 &&
3636 !(significandParts()[integer_bit_part] & integer_bit))
3638 }
else if (category ==
fcZero) {
3641 myexponent = ::exponentZero(S) + bias;
3642 mysignificand.fill(0);
3647 myexponent = ::exponentInf(S) + bias;
3648 mysignificand.fill(0);
3653 myexponent = ::exponentNaN(S) + bias;
3654 std::copy_n(significandParts(), mysignificand.size(),
3655 mysignificand.begin());
3657 std::array<
uint64_t, (S.sizeInBits + 63) / 64> words;
3659 std::copy_n(mysignificand.begin(), mysignificand.size(), words.begin());
3660 if constexpr (significand_mask != 0 || trailing_significand_bits == 0) {
3662 words[mysignificand.size() - 1] &= significand_mask;
3664 std::fill(words_iter, words.end(),
uint64_t{0});
3665 constexpr size_t last_word = words.size() - 1;
3667 << ((S.sizeInBits - 1) % 64);
3668 words[last_word] |= shifted_sign;
3669 uint64_t shifted_exponent = (myexponent & exponent_mask)
3670 << (trailing_significand_bits % 64);
3671 words[last_word] |= shifted_exponent;
3672 if constexpr (last_word == 0) {
3673 return APInt(S.sizeInBits, words[0]);
3675 return APInt(S.sizeInBits, words);
3678APInt IEEEFloat::convertQuadrupleAPFloatToAPInt()
const {
3679 assert(partCount() == 2);
3680 return convertIEEEFloatToAPInt<semIEEEquad>();
3683APInt IEEEFloat::convertDoubleAPFloatToAPInt()
const {
3685 return convertIEEEFloatToAPInt<semIEEEdouble>();
3688APInt IEEEFloat::convertFloatAPFloatToAPInt()
const {
3690 return convertIEEEFloatToAPInt<semIEEEsingle>();
3693APInt IEEEFloat::convertBFloatAPFloatToAPInt()
const {
3694 assert(partCount() == 1);
3695 return convertIEEEFloatToAPInt<semBFloat>();
3698APInt IEEEFloat::convertHalfAPFloatToAPInt()
const {
3700 return convertIEEEFloatToAPInt<semIEEEhalf>();
3703APInt IEEEFloat::convertFloat8E5M2APFloatToAPInt()
const {
3704 assert(partCount() == 1);
3705 return convertIEEEFloatToAPInt<semFloat8E5M2>();
3708APInt IEEEFloat::convertFloat8E5M2FNUZAPFloatToAPInt()
const {
3709 assert(partCount() == 1);
3710 return convertIEEEFloatToAPInt<semFloat8E5M2FNUZ>();
3713APInt IEEEFloat::convertFloat8E4M3APFloatToAPInt()
const {
3714 assert(partCount() == 1);
3715 return convertIEEEFloatToAPInt<semFloat8E4M3>();
3718APInt IEEEFloat::convertFloat8E4M3FNAPFloatToAPInt()
const {
3719 assert(partCount() == 1);
3720 return convertIEEEFloatToAPInt<semFloat8E4M3FN>();
3723APInt IEEEFloat::convertFloat8E4M3FNUZAPFloatToAPInt()
const {
3724 assert(partCount() == 1);
3725 return convertIEEEFloatToAPInt<semFloat8E4M3FNUZ>();
3728APInt IEEEFloat::convertFloat8E4M3B11FNUZAPFloatToAPInt()
const {
3729 assert(partCount() == 1);
3730 return convertIEEEFloatToAPInt<semFloat8E4M3B11FNUZ>();
3733APInt IEEEFloat::convertFloat8E3M4APFloatToAPInt()
const {
3734 assert(partCount() == 1);
3735 return convertIEEEFloatToAPInt<semFloat8E3M4>();
3738APInt IEEEFloat::convertFloatTF32APFloatToAPInt()
const {
3739 assert(partCount() == 1);
3740 return convertIEEEFloatToAPInt<semFloatTF32>();
3743APInt IEEEFloat::convertFloat8E8M0FNUAPFloatToAPInt()
const {
3744 assert(partCount() == 1);
3745 return convertIEEEFloatToAPInt<semFloat8E8M0FNU>();
3748APInt IEEEFloat::convertFloat6E3M2FNAPFloatToAPInt()
const {
3749 assert(partCount() == 1);
3750 return convertIEEEFloatToAPInt<semFloat6E3M2FN>();
3753APInt IEEEFloat::convertFloat6E2M3FNAPFloatToAPInt()
const {
3754 assert(partCount() == 1);
3755 return convertIEEEFloatToAPInt<semFloat6E2M3FN>();
3758APInt IEEEFloat::convertFloat4E2M1FNAPFloatToAPInt()
const {
3759 assert(partCount() == 1);
3760 return convertIEEEFloatToAPInt<semFloat4E2M1FN>();
3769 return convertHalfAPFloatToAPInt();
3772 return convertBFloatAPFloatToAPInt();
3775 return convertFloatAPFloatToAPInt();
3778 return convertDoubleAPFloatToAPInt();
3781 return convertQuadrupleAPFloatToAPInt();
3784 return convertPPCDoubleDoubleLegacyAPFloatToAPInt();
3787 return convertFloat8E5M2APFloatToAPInt();
3790 return convertFloat8E5M2FNUZAPFloatToAPInt();
3793 return convertFloat8E4M3APFloatToAPInt();
3796 return convertFloat8E4M3FNAPFloatToAPInt();
3799 return convertFloat8E4M3FNUZAPFloatToAPInt();
3802 return convertFloat8E4M3B11FNUZAPFloatToAPInt();
3805 return convertFloat8E3M4APFloatToAPInt();
3808 return convertFloatTF32APFloatToAPInt();
3811 return convertFloat8E8M0FNUAPFloatToAPInt();
3814 return convertFloat6E3M2FNAPFloatToAPInt();
3817 return convertFloat6E2M3FNAPFloatToAPInt();
3820 return convertFloat4E2M1FNAPFloatToAPInt();
3824 return convertF80LongDoubleAPFloatToAPInt();
3829 "Float semantics are not IEEEsingle");
3836 "Float semantics are not IEEEdouble");
3841#ifdef HAS_IEE754_FLOAT128
3842float128 IEEEFloat::convertToQuad()
const {
3844 "Float semantics are not IEEEquads");
3846 return api.bitsToQuad();
3857void IEEEFloat::initFromF80LongDoubleAPInt(
const APInt &api) {
3860 uint64_t myexponent = (i2 & 0x7fff);
3862 uint8_t myintegerbit = mysignificand >> 63;
3867 sign =
static_cast<unsigned int>(i2>>15);
3868 if (myexponent == 0 && mysignificand == 0) {
3870 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3872 }
else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3873 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3875 exponent = exponentNaN();
3876 significandParts()[0] = mysignificand;
3877 significandParts()[1] = 0;
3880 exponent = myexponent - 16383;
3881 significandParts()[0] = mysignificand;
3882 significandParts()[1] = 0;
3888void IEEEFloat::initFromPPCDoubleDoubleLegacyAPInt(
const APInt &api) {
3895 initFromDoubleAPInt(
APInt(64, i1));
3916void IEEEFloat::initFromFloat8E8M0FNUAPInt(
const APInt &api) {
3917 const uint64_t exponent_mask = 0xff;
3919 uint64_t myexponent = (val & exponent_mask);
3922 assert(partCount() == 1);
3931 significandParts()[0] = mysignificand;
3935 if (val == exponent_mask) {
3937 exponent = exponentNaN();
3942 exponent = myexponent - 127;
3944template <const fltSemantics &S>
3945void IEEEFloat::initFromIEEEAPInt(
const APInt &api) {
3949 constexpr uint64_t significand_mask = integer_bit - 1;
3950 constexpr unsigned int trailing_significand_bits = S.precision - 1;
3951 constexpr unsigned int stored_significand_parts =
3953 constexpr unsigned int exponent_bits =
3954 S.sizeInBits - 1 - trailing_significand_bits;
3955 static_assert(exponent_bits < 64);
3957 constexpr int bias = -(S.minExponent - 1);
3961 std::array<integerPart, stored_significand_parts> mysignificand;
3962 std::copy_n(api.
getRawData(), mysignificand.size(), mysignificand.begin());
3963 if constexpr (significand_mask != 0) {
3964 mysignificand[mysignificand.size() - 1] &= significand_mask;
3971 (last_word >> (trailing_significand_bits % 64)) & exponent_mask;
3974 assert(partCount() == mysignificand.size());
3976 sign =
static_cast<unsigned int>(last_word >> ((S.sizeInBits - 1) % 64));
3978 bool all_zero_significand =
3981 bool is_zero = myexponent == 0 && all_zero_significand;
3984 if (myexponent - bias == ::exponentInf(S) && all_zero_significand) {
3993 is_nan = myexponent - bias == ::exponentNaN(S) && !all_zero_significand;
3995 bool all_ones_significand =
3996 std::all_of(mysignificand.begin(), mysignificand.end() - 1,
3997 [](
integerPart bits) { return bits == ~integerPart{0}; }) &&
3998 (!significand_mask ||
3999 mysignificand[mysignificand.size() - 1] == significand_mask);
4000 is_nan = myexponent - bias == ::exponentNaN(S) && all_ones_significand;
4008 std::copy_n(mysignificand.begin(), mysignificand.size(),
4009 significandParts());
4019 exponent = myexponent - bias;
4020 std::copy_n(mysignificand.begin(), mysignificand.size(), significandParts());
4021 if (myexponent == 0)
4022 exponent = S.minExponent;
4024 significandParts()[mysignificand.size()-1] |= integer_bit;
4027void IEEEFloat::initFromQuadrupleAPInt(
const APInt &api) {
4028 initFromIEEEAPInt<semIEEEquad>(api);
4031void IEEEFloat::initFromDoubleAPInt(
const APInt &api) {
4032 initFromIEEEAPInt<semIEEEdouble>(api);
4035void IEEEFloat::initFromFloatAPInt(
const APInt &api) {
4036 initFromIEEEAPInt<semIEEEsingle>(api);
4039void IEEEFloat::initFromBFloatAPInt(
const APInt &api) {
4040 initFromIEEEAPInt<semBFloat>(api);
4043void IEEEFloat::initFromHalfAPInt(
const APInt &api) {
4044 initFromIEEEAPInt<semIEEEhalf>(api);
4047void IEEEFloat::initFromFloat8E5M2APInt(
const APInt &api) {
4048 initFromIEEEAPInt<semFloat8E5M2>(api);
4051void IEEEFloat::initFromFloat8E5M2FNUZAPInt(
const APInt &api) {
4052 initFromIEEEAPInt<semFloat8E5M2FNUZ>(api);
4055void IEEEFloat::initFromFloat8E4M3APInt(
const APInt &api) {
4056 initFromIEEEAPInt<semFloat8E4M3>(api);
4059void IEEEFloat::initFromFloat8E4M3FNAPInt(
const APInt &api) {
4060 initFromIEEEAPInt<semFloat8E4M3FN>(api);
4063void IEEEFloat::initFromFloat8E4M3FNUZAPInt(
const APInt &api) {
4064 initFromIEEEAPInt<semFloat8E4M3FNUZ>(api);
4067void IEEEFloat::initFromFloat8E4M3B11FNUZAPInt(
const APInt &api) {
4068 initFromIEEEAPInt<semFloat8E4M3B11FNUZ>(api);
4071void IEEEFloat::initFromFloat8E3M4APInt(
const APInt &api) {
4072 initFromIEEEAPInt<semFloat8E3M4>(api);
4075void IEEEFloat::initFromFloatTF32APInt(
const APInt &api) {
4076 initFromIEEEAPInt<semFloatTF32>(api);
4079void IEEEFloat::initFromFloat6E3M2FNAPInt(
const APInt &api) {
4080 initFromIEEEAPInt<semFloat6E3M2FN>(api);
4083void IEEEFloat::initFromFloat6E2M3FNAPInt(
const APInt &api) {
4084 initFromIEEEAPInt<semFloat6E2M3FN>(api);
4087void IEEEFloat::initFromFloat4E2M1FNAPInt(
const APInt &api) {
4088 initFromIEEEAPInt<semFloat4E2M1FN>(api);
4095 return initFromHalfAPInt(api);
4097 return initFromBFloatAPInt(api);
4099 return initFromFloatAPInt(api);
4101 return initFromDoubleAPInt(api);
4103 return initFromF80LongDoubleAPInt(api);
4105 return initFromQuadrupleAPInt(api);
4107 return initFromPPCDoubleDoubleLegacyAPInt(api);
4109 return initFromFloat8E5M2APInt(api);
4111 return initFromFloat8E5M2FNUZAPInt(api);
4113 return initFromFloat8E4M3APInt(api);
4115 return initFromFloat8E4M3FNAPInt(api);
4117 return initFromFloat8E4M3FNUZAPInt(api);
4119 return initFromFloat8E4M3B11FNUZAPInt(api);
4121 return initFromFloat8E3M4APInt(api);
4123 return initFromFloatTF32APInt(api);
4125 return initFromFloat8E8M0FNUAPInt(api);
4127 return initFromFloat6E3M2FNAPInt(api);
4129 return initFromFloat6E2M3FNAPInt(api);
4131 return initFromFloat4E2M1FNAPInt(api);
4138void IEEEFloat::makeLargest(
bool Negative) {
4139 if (Negative && !semantics->hasSignedRepr)
4141 "This floating point format does not support signed values");
4148 exponent = semantics->maxExponent;
4152 unsigned PartCount = partCount();
4153 memset(significand, 0xFF,
sizeof(
integerPart)*(PartCount - 1));
4157 const unsigned NumUnusedHighBits =
4158 PartCount*integerPartWidth - semantics->precision;
4159 significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth)
4164 (semantics->precision > 1))
4170void IEEEFloat::makeSmallest(
bool Negative) {
4171 if (Negative && !semantics->hasSignedRepr)
4173 "This floating point format does not support signed values");
4180 exponent = semantics->minExponent;
4184void IEEEFloat::makeSmallestNormalized(
bool Negative) {
4185 if (Negative && !semantics->hasSignedRepr)
4187 "This floating point format does not support signed values");
4196 exponent = semantics->minExponent;
4201 initFromAPInt(&Sem, API);
4204IEEEFloat::IEEEFloat(
float f) {
4208IEEEFloat::IEEEFloat(
double d) {
4214 Buffer.
append(Str.begin(), Str.end());
4219 void AdjustToPrecision(
APInt &significand,
4220 int &exp,
unsigned FormatPrecision) {
4224 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
4226 if (bits <= bitsRequired)
return;
4228 unsigned tensRemovable = (bits - bitsRequired) * 59 / 196;
4229 if (!tensRemovable)
return;
4231 exp += tensRemovable;
4236 if (tensRemovable & 1)
4238 tensRemovable >>= 1;
4239 if (!tensRemovable)
break;
4243 significand = significand.
udiv(divisor);
4251 int &exp,
unsigned FormatPrecision) {
4252 unsigned N = buffer.
size();
4253 if (
N <= FormatPrecision)
return;
4256 unsigned FirstSignificant =
N - FormatPrecision;
4263 if (buffer[FirstSignificant - 1] <
'5') {
4264 while (FirstSignificant <
N && buffer[FirstSignificant] ==
'0')
4267 exp += FirstSignificant;
4268 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
4274 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
4275 if (buffer[
I] ==
'9') {
4284 if (FirstSignificant ==
N) {
4285 exp += FirstSignificant;
4291 exp += FirstSignificant;
4292 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
4296 APInt significand,
unsigned FormatPrecision,
4297 unsigned FormatMaxPadding,
bool TruncateZero) {
4298 const int semanticsPrecision = significand.
getBitWidth();
4305 if (!FormatPrecision) {
4313 FormatPrecision = 2 + semanticsPrecision * 59 / 196;
4318 exp += trailingZeros;
4324 }
else if (exp > 0) {
4326 significand = significand.
zext(semanticsPrecision + exp);
4327 significand <<= exp;
4341 unsigned precision = semanticsPrecision + (137 * texp + 136) / 59;
4345 significand = significand.
zext(precision);
4346 APInt five_to_the_i(precision, 5);
4349 significand *= five_to_the_i;
4354 five_to_the_i *= five_to_the_i;
4358 AdjustToPrecision(significand, exp, FormatPrecision);
4363 unsigned precision = significand.getBitWidth();
4364 if (precision < 4) {
4367 significand = significand.zext(precision);
4369 APInt ten(precision, 10);
4370 APInt digit(precision, 0);
4372 bool inTrail =
true;
4373 while (significand != 0) {
4378 unsigned d = digit.getZExtValue();
4389 assert(!buffer.
empty() &&
"no characters in buffer!");
4393 AdjustToPrecision(buffer, exp, FormatPrecision);
4395 unsigned NDigits = buffer.
size();
4398 bool FormatScientific;
4399 if (!FormatMaxPadding)
4400 FormatScientific =
true;
4406 FormatScientific = ((
unsigned) exp > FormatMaxPadding ||
4407 NDigits + (
unsigned) exp > FormatPrecision);
4410 int MSD = exp + (int) (NDigits - 1);
4413 FormatScientific =
false;
4417 FormatScientific = ((
unsigned) -MSD) > FormatMaxPadding;
4423 if (FormatScientific) {
4424 exp += (NDigits - 1);
4426 Str.push_back(buffer[NDigits-1]);
4428 if (NDigits == 1 && TruncateZero)
4431 for (
unsigned I = 1;
I != NDigits; ++
I)
4432 Str.push_back(buffer[NDigits-1-
I]);
4434 if (!TruncateZero && FormatPrecision > NDigits - 1)
4435 Str.append(FormatPrecision - NDigits + 1,
'0');
4437 Str.push_back(TruncateZero ?
'E' :
'e');
4439 Str.push_back(exp >= 0 ?
'+' :
'-');
4444 expbuf.
push_back((
char) (
'0' + (exp % 10)));
4448 if (!TruncateZero && expbuf.
size() < 2)
4450 for (
unsigned I = 0, E = expbuf.
size();
I != E; ++
I)
4451 Str.push_back(expbuf[E-1-
I]);
4457 for (
unsigned I = 0;
I != NDigits; ++
I)
4458 Str.push_back(buffer[NDigits-1-
I]);
4467 int NWholeDigits = exp + (int) NDigits;
4470 if (NWholeDigits > 0) {
4472 Str.push_back(buffer[NDigits-
I-1]);
4475 unsigned NZeros = 1 + (
unsigned) -NWholeDigits;
4479 for (
unsigned Z = 1;
Z != NZeros; ++
Z)
4483 for (;
I != NDigits; ++
I)
4484 Str.push_back(buffer[NDigits-
I-1]);
4490 unsigned FormatMaxPadding,
bool TruncateZero)
const {
4494 return append(Str,
"-Inf");
4496 return append(Str,
"+Inf");
4498 case fcNaN:
return append(Str,
"NaN");
4504 if (!FormatMaxPadding) {
4506 append(Str,
"0.0E+0");
4509 if (FormatPrecision > 1)
4510 Str.append(FormatPrecision - 1,
'0');
4511 append(Str,
"e+00");
4523 int exp = exponent - ((int) semantics->precision - 1);
4525 semantics->precision,
4528 toStringImpl(Str, isNegative(), exp, significand, FormatPrecision,
4529 FormatMaxPadding, TruncateZero);
4533int IEEEFloat::getExactLog2Abs()
const {
4541 for (
int i = 0; i < PartCount; ++i) {
4547 if (exponent != semantics->minExponent)
4550 int CountrParts = 0;
4551 for (
int i = 0; i < PartCount;
4553 if (Parts[i] != 0) {
4554 return exponent - semantics->precision + CountrParts +
4562bool IEEEFloat::isSignaling()
const {
4599 if (isSignaling()) {
4600 result = opInvalidOp;
4602 makeNaN(
false, isNegative(),
nullptr);
4607 makeSmallest(
false);
4611 if (isSmallest() && isNegative()) {
4617 if (!semantics->hasZero)
4618 makeSmallestNormalized(
false);
4622 if (isLargest() && !isNegative()) {
4627 }
else if (semantics->nonFiniteBehavior ==
4634 category = fcInfinity;
4635 exponent = semantics->maxExponent + 1;
4649 bool WillCrossBinadeBoundary =
4650 exponent != semantics->minExponent && isSignificandAllZeros();
4668 if (WillCrossBinadeBoundary) {
4687 (!isDenormal() && isSignificandAllOnes());
4689 if (WillCrossBinadeBoundary) {
4693 assert(exponent != semantics->maxExponent &&
4694 "We can not increment an exponent beyond the maxExponent allowed"
4695 " by the given floating point semantics.");
4698 incrementSignificand();
4712 return ::exponentNaN(*semantics);
4716 return ::exponentInf(*semantics);
4720 return ::exponentZero(*semantics);
4723void IEEEFloat::makeInf(
bool Negative) {
4729 makeNaN(
false, Negative);
4732 category = fcInfinity;
4738void IEEEFloat::makeZero(
bool Negative) {
4739 if (!semantics->hasZero)
4752void IEEEFloat::makeQuiet() {
4766 return Arg.exponent;
4771 Normalized.exponent += SignificandBits;
4773 return Normalized.exponent - SignificandBits;
4777 auto MaxExp =
X.getSemantics().maxExponent;
4778 auto MinExp =
X.getSemantics().minExponent;
4786 int SignificandBits =
X.getSemantics().precision - 1;
4787 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
4790 X.exponent += std::clamp(Exp, -MaxIncrement - 1, MaxIncrement);
4813 return scalbn(Val, -Exp, RM);
4846 Floats(new
APFloat[2]{std::move(
First), std::move(Second)}) {
4853 : Semantics(
RHS.Semantics),
4861 : Semantics(
RHS.Semantics), Floats(
RHS.Floats) {
4863 RHS.Floats =
nullptr;
4868 if (Semantics ==
RHS.Semantics &&
RHS.Floats) {
4869 Floats[0] =
RHS.Floats[0];
4870 Floats[1] =
RHS.Floats[1];
4871 }
else if (
this != &
RHS) {
4904 Floats[0] = std::move(z);
4905 Floats[1].makeZero(
false);
4909 auto AComparedToC = a.compareAbsoluteValue(c);
4922 Floats[0] = std::move(z);
4923 Floats[1].makeZero(
false);
4958 Floats[0] = std::move(z);
4959 Floats[1].makeZero(
false);
4965 Floats[1].makeZero(
false);
4968 Floats[1] = std::move(z);
4976 const DoubleAPFloat &RHS,
4996 LHS.isNegative() !=
RHS.isNegative()) {
4997 Out.makeNaN(
false, Out.isNegative(),
nullptr);
5018 return Out.addImpl(
A, AA,
C, CC, RM);
5023 return addWithSpecial(*
this,
RHS, *
this, RM);
5036 const auto &
LHS = *
this;
5063 Out.makeNaN(
false,
false,
nullptr);
5075 "Special cases not handled exhaustively");
5082 if (!
T.isFiniteNonZero()) {
5084 Floats[1].makeZero(
false);
5106 Status |= U.add(Tau, RM);
5109 if (!U.isFinite()) {
5110 Floats[1].makeZero(
false);
5171 if (!
Hi.isFiniteNonZero() ||
Lo.isZero()) {
5172 Floats[0] = std::move(RoundedHi);
5173 Floats[1].makeZero(
false);
5185 const APFloat RoundingError = Rounded - ToRound;
5186 if (TieBreaker.isNonZero() &&
5187 TieBreaker.isNegative() != RoundingError.
isNegative() &&
5188 abs(RoundingError).isExactlyValue(0.5))
5197 if (RoundedHi !=
Hi) {
5202 RoundedHi = RoundToNearestHelper(
Hi, RoundedHi,
Lo);
5204 Floats[0] = std::move(RoundedHi);
5205 Floats[1].makeZero(
false);
5218 LoRoundingMode = RM;
5226 RoundedLo = RoundToNearestHelper(
Lo, RoundedLo,
Hi);
5229 std::tie(RoundedHi, RoundedLo) =
fastTwoSum(RoundedHi, RoundedLo);
5231 Floats[0] = std::move(RoundedHi);
5232 Floats[1] = std::move(RoundedLo);
5244 const cmpResult HiPartCmp = Floats[0].compareAbsoluteValue(
RHS.Floats[0]);
5249 if (Floats[1].
isZero() &&
RHS.Floats[1].isZero())
5255 const bool ThisIsSubtractive =
5257 const bool RHSIsSubtractive =
5258 RHS.Floats[0].isNegative() !=
RHS.Floats[1].isNegative();
5268 if (
RHS.Floats[1].isZero())
5275 if (ThisIsSubtractive != RHSIsSubtractive)
5280 const cmpResult LoPartCmp = Floats[1].compareAbsoluteValue(
RHS.Floats[1]);
5282 if (ThisIsSubtractive) {
5302 Floats[0].makeInf(Neg);
5303 Floats[1].makeZero(
false);
5307 Floats[0].makeZero(Neg);
5308 Floats[1].makeZero(
false);
5321 Floats[0].makeSmallest(Neg);
5322 Floats[1].makeZero(
false);
5330 Floats[1].makeZero(
false);
5334 Floats[0].makeNaN(SNaN, Neg,
fill);
5335 Floats[1].makeZero(
false);
5339 auto Result = Floats[0].
compare(
RHS.Floats[0]);
5432 if (InLattice(HiOld, NextLo)) {
5434 Floats[1] = std::move(NextLo);
5441 Largest.makeLargest(
false);
5471 if (!InLattice(NextHi, NextLo))
5475 Floats[0] = std::move(NextHi);
5476 Floats[1] = std::move(NextLo);
5526 const unsigned PositiveOverflowWidth = IsSigned ? Width - 1 : Width;
5527 if (HiExactLog2 >= 0 &&
5528 static_cast<unsigned>(HiExactLog2) == PositiveOverflowWidth) {
5538 Input, Width,
true, RM, &LoIsExact);
5551 *IsExact = RoundStatus ==
opOK;
5563 APSInt LoResult{Width, !IsSigned};
5575 *IsExact = RoundStatus ==
opOK;
5581 unsigned int Width,
bool IsSigned,
5584 convertToSignExtendedInteger(Input, Width, IsSigned, RM, IsExact);
5588 assert(DstPartsCount <= Input.
size() &&
"Integer too big");
5596 Bits = Width - IsSigned;
5641 if (SrcMSB == UINT_MAX) {
5648 const unsigned SrcBitWidth = SrcMSB + 1;
5665 return handleOverflow(RM);
5671 bool HiAsIntIsExact;
5678 const APInt Error = SrcInt.zext(HiAsInt.getBitWidth()) - HiAsInt;
5688 if (
Error.isNegative()) {
5696 const unsigned ErrorActiveBits =
Error.getSignificantBits() - 1;
5698 if (ErrorActiveBits > LoPrecision) {
5699 const unsigned RoundingBoundary = ErrorActiveBits - LoPrecision;
5703 if (
Error.countTrailingZeros() == RoundingBoundary - 1)
5722 Floats[0] = std::move(
Hi);
5723 Floats[1] = std::move(
Lo);
5728 return handleOverflow(RM);
5734 Largest.makeLargest(
false);
5736 return handleOverflow(RM);
5748 const bool NegateInput = IsSigned && Input.
isNegative();
5761 unsigned int HexDigits,
5773 Floats[0] != Floats[0] + Floats[1]);
5807 unsigned FormatPrecision,
5808 unsigned FormatMaxPadding,
5809 bool TruncateZero)
const {
5812 .
toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
5832 if (
Lo.isZero() ||
Hi.isNegative() ==
Lo.isNegative())
5834 if (
Hi.getExactLog2Abs() == INT_MIN)
5838 return IlogbResult - 1;
5845 scalbn(Arg.Floats[1], Exp, RM));
5860 Quiet.getFirst().makeQuiet();
5882 const bool SignsDisagree =
Hi.isNegative() !=
Lo.isNegative();
5899 LoRoundingMode = RM;
5900 Second =
scalbn(
Lo, -Exp, LoRoundingMode);
5908 if (RecomposedLo !=
Lo) {
5912 const APFloat RoundingError = RecomposedLo -
Lo;
5917 const APFloat ScaledUlpOfSecond =
5919 const bool IsMidpoint =
abs(RoundingError) == ScaledUlpOfSecond;
5920 const bool RoundedLoAway =
5925 if (IsMidpoint && RoundedLoAway)
5941 if (Second.
isZero() && SignsDisagree &&
Hi.getExactLog2Abs() != INT_MIN)
5951APFloat::Storage::Storage(IEEEFloat
F,
const fltSemantics &Semantics) {
5952 if (usesLayout<IEEEFloat>(Semantics)) {
5953 new (&
IEEE) IEEEFloat(std::move(
F));
5956 if (usesLayout<DoubleAPFloat>(Semantics)) {
5959 DoubleAPFloat(Semantics,
APFloat(std::move(
F), S),
5972 if (APFloat::usesLayout<detail::IEEEFloat>(Arg.
getSemantics()))
5974 if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.
getSemantics()))
5982 assert(StatusOrErr &&
"Invalid floating point representation");
6052 *Inv = std::move(Reciprocal);
6064 usesLayout<IEEEFloat>(ToSemantics))
6065 return U.IEEE.convert(ToSemantics, RM, losesInfo);
6067 usesLayout<DoubleAPFloat>(ToSemantics)) {
6070 *
this =
APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
6074 usesLayout<IEEEFloat>(ToSemantics)) {
6075 auto Ret = getIEEE().
convert(ToSemantics, RM, losesInfo);
6076 *
this =
APFloat(std::move(getIEEE()), ToSemantics);
6092#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6105 bool *isExact)
const {
6109 rounding_mode, isExact);
6111 result =
APInt(bitWidth, parts);
6119 "Float semantics is not representable by IEEEdouble");
6128#ifdef HAS_IEE754_FLOAT128
6129float128 APFloat::convertToQuad()
const {
6131 return getIEEE().convertToQuad();
6133 "Float semantics is not representable by IEEEquad");
6139 return Temp.getIEEE().convertToQuad();
6147 "Float semantics is not representable by IEEEsingle");
6158#undef APFLOAT_DISPATCH_ON_SEMANTICS
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define PackCategoriesIntoKey(_lhs, _rhs)
A macro used to combine two fcCategory enums into one key which can be used in a switch statement to ...
This file declares a class to represent arbitrary precision floating point values and provide a varie...
#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL)
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static bool isNeg(Value *V)
Returns true if the operation is a negation of V, and it works for both integers and floats.
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live value
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
Utilities for dealing with flags related to floating point properties and mode controls.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
LLVM_ABI void Profile(FoldingSetNodeID &NID) const
Used to insert APFloat objects, or objects that contain APFloat objects, into FoldingSets.
opStatus divide(const APFloat &RHS, roundingMode RM)
bool isFiniteNonZero() const
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
LLVM_READONLY int getExactLog2Abs() const
opStatus subtract(const APFloat &RHS, roundingMode RM)
bool bitwiseIsEqual(const APFloat &RHS) const
bool getExactInverse(APFloat *Inv) const
If this value is normal and has an exact, normal, multiplicative inverse, store it in inv and return ...
LLVM_ABI double convertToDouble() const
Converts this APFloat to host double value.
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
opStatus add(const APFloat &RHS, roundingMode RM)
static LLVM_ABI APFloat getAllOnesValue(const fltSemantics &Semantics)
Returns a float which is bitcasted from an all one value int.
const fltSemantics & getSemantics() const
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
opStatus remainder(const APFloat &RHS)
APInt bitcastToAPInt() const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
opStatus next(bool nextDown)
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM)
static APFloat getSmallest(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) finite number in the given semantics.
LLVM_ABI FPClassTest classify() const
Return the FPClassTest which will return true for the value.
opStatus mod(const APFloat &RHS)
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
fltCategory getCategory() const
LLVM_DUMP_METHOD void dump() const
LLVM_ABI void print(raw_ostream &) const
opStatus roundToIntegral(roundingMode RM)
static bool hasSignificand(const fltSemantics &Sem)
Returns true if the given semantics has actual significand.
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
cmpResult compare(const APFloat &RHS) const
Class for arbitrary precision integers.
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void tcSetBit(WordType *, unsigned bit)
Set the given bit of a bignum. Zero-based.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
static LLVM_ABI void tcSet(WordType *, WordType, unsigned)
Sets the least significant part of a bignum to the input value, and zeroes out higher parts.
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
static LLVM_ABI int tcExtractBit(const WordType *, unsigned bit)
Extract the given bit of a bignum; returns 0 or 1. Zero-based.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
static LLVM_ABI WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned)
DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static LLVM_ABI 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,...
unsigned getActiveBits() const
Compute the number of active bits in the value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static LLVM_ABI int tcCompare(const WordType *, const WordType *, unsigned)
Comparison (unsigned) of two bignums.
static APInt floatToBits(float V)
Converts a float to APInt bits.
static LLVM_ABI void tcAssign(WordType *, const WordType *, unsigned)
Assign one bignum to another.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static LLVM_ABI void tcShiftRight(WordType *, unsigned Words, unsigned Count)
Shift a bignum right Count bits.
static LLVM_ABI void tcFullMultiply(WordType *, const WordType *, const WordType *, unsigned, unsigned)
DST = LHS * RHS, where DST has width the sum of the widths of the operands.
unsigned getNumWords() const
Get the number of words.
bool isNegative() const
Determine sign of this APInt.
static LLVM_ABI void tcClearBit(WordType *, unsigned bit)
Clear the given bit of a bignum. Zero-based.
void negate()
Negate this APInt in place.
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
unsigned countr_zero() const
Count the number of trailing zero bits.
static LLVM_ABI unsigned tcLSB(const WordType *, unsigned n)
Returns the bit number of the least or most significant set bit of a number.
static LLVM_ABI void tcShiftLeft(WordType *, unsigned Words, unsigned Count)
Shift a bignum left Count bits.
static LLVM_ABI bool tcIsZero(const WordType *, unsigned)
Returns true if a bignum is zero, false otherwise.
static LLVM_ABI unsigned tcMSB(const WordType *parts, unsigned n)
Returns the bit number of the most significant set bit of a number.
float bitsToFloat() const
Converts APInt bits to a float.
static LLVM_ABI 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.
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
static LLVM_ABI WordType tcSubtract(WordType *, const WordType *, WordType carry, unsigned)
DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
static LLVM_ABI void tcNegate(WordType *, unsigned)
Negate a bignum in-place.
static APInt doubleToBits(double V)
Converts a double to APInt bits.
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
double bitsToDouble() const
Converts APInt bits to a double.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
constexpr bool empty() const
empty - Check if the string is empty.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
char back() const
back - Get the last character in the string.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
char front() const
front - Get the first character in the string.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI void makeSmallestNormalized(bool Neg)
LLVM_ABI DoubleAPFloat & operator=(const DoubleAPFloat &RHS)
LLVM_ABI void changeSign()
LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode)
LLVM_ABI bool isLargest() const
LLVM_ABI opStatus remainder(const DoubleAPFloat &RHS)
LLVM_ABI opStatus multiply(const DoubleAPFloat &RHS, roundingMode RM)
LLVM_ABI fltCategory getCategory() const
LLVM_ABI bool bitwiseIsEqual(const DoubleAPFloat &RHS) const
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const
LLVM_ABI opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
LLVM_ABI APInt bitcastToAPInt() const
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
LLVM_ABI bool isSmallest() const
LLVM_ABI opStatus subtract(const DoubleAPFloat &RHS, roundingMode RM)
LLVM_ABI cmpResult compareAbsoluteValue(const DoubleAPFloat &RHS) const
LLVM_ABI bool isDenormal() const
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
LLVM_ABI void makeSmallest(bool Neg)
LLVM_ABI friend int ilogb(const DoubleAPFloat &X)
LLVM_ABI opStatus next(bool nextDown)
LLVM_ABI void makeInf(bool Neg)
LLVM_ABI bool isInteger() const
LLVM_ABI void makeZero(bool Neg)
LLVM_ABI opStatus divide(const DoubleAPFloat &RHS, roundingMode RM)
LLVM_ABI bool isSmallestNormalized() const
LLVM_ABI opStatus mod(const DoubleAPFloat &RHS)
LLVM_ABI DoubleAPFloat(const fltSemantics &S)
LLVM_ABI void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision, unsigned FormatMaxPadding, bool TruncateZero=true) const
LLVM_ABI void makeLargest(bool Neg)
LLVM_ABI cmpResult compare(const DoubleAPFloat &RHS) const
LLVM_ABI opStatus roundToIntegral(roundingMode RM)
LLVM_ABI opStatus fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, const DoubleAPFloat &Addend, roundingMode RM)
LLVM_ABI unsigned int convertToHexString(char *DST, unsigned int HexDigits, bool UpperCase, roundingMode RM) const
LLVM_ABI bool isNegative() const
LLVM_ABI opStatus add(const DoubleAPFloat &RHS, roundingMode RM)
LLVM_ABI void makeNaN(bool SNaN, bool Neg, const APInt *fill)
LLVM_ABI 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...
LLVM_ABI friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
LLVM_ABI cmpResult compareAbsoluteValue(const IEEEFloat &) const
LLVM_ABI opStatus mod(const IEEEFloat &)
C fmod, or llvm frem.
fltCategory getCategory() const
LLVM_ABI opStatus convertFromAPInt(const APInt &, bool, roundingMode)
bool isFiniteNonZero() const
bool needsCleanup() const
Returns whether this instance allocated memory.
LLVM_ABI APInt bitcastToAPInt() const
LLVM_ABI cmpResult compare(const IEEEFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
bool isNegative() const
IEEE-754R isSignMinus: Returns true if and only if the current value is negative.
LLVM_ABI opStatus divide(const IEEEFloat &, roundingMode)
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
LLVM_ABI opStatus remainder(const IEEEFloat &)
IEEE remainder.
LLVM_ABI double convertToDouble() const
LLVM_ABI float convertToFloat() const
LLVM_ABI opStatus subtract(const IEEEFloat &, roundingMode)
LLVM_ABI void makeInf(bool Neg=false)
LLVM_ABI bool isSmallestNormalized() const
Returns true if this is the smallest (by magnitude) normalized finite number in the given semantics.
LLVM_ABI void makeQuiet()
LLVM_ABI bool isLargest() const
Returns true if and only if the number has the largest possible finite magnitude in the current seman...
LLVM_ABI opStatus add(const IEEEFloat &, roundingMode)
bool isFinite() const
Returns true if and only if the current value is zero, subnormal, or normal.
LLVM_ABI Expected< opStatus > convertFromString(StringRef, roundingMode)
LLVM_ABI void makeNaN(bool SNaN=false, bool Neg=false, const APInt *fill=nullptr)
LLVM_ABI opStatus multiply(const IEEEFloat &, roundingMode)
LLVM_ABI opStatus roundToIntegral(roundingMode)
LLVM_ABI IEEEFloat & operator=(const IEEEFloat &)
LLVM_ABI bool bitwiseIsEqual(const IEEEFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
LLVM_ABI void makeSmallestNormalized(bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
LLVM_ABI bool isInteger() const
Returns true if and only if the number is an exact integer.
LLVM_ABI IEEEFloat(const fltSemantics &)
LLVM_ABI opStatus fusedMultiplyAdd(const IEEEFloat &, const IEEEFloat &, roundingMode)
LLVM_ABI friend int ilogb(const IEEEFloat &Arg)
bool isInfinity() const
IEEE-754R isInfinite(): Returns true if and only if the float is infinity.
const fltSemantics & getSemantics() const
bool isZero() const
Returns true if and only if the float is plus or minus zero.
LLVM_ABI bool isSignaling() const
Returns true if and only if the float is a signaling NaN.
LLVM_ABI void makeZero(bool Neg=false)
LLVM_ABI opStatus convert(const fltSemantics &, roundingMode, bool *)
IEEEFloat::convert - convert a value of one floating point type to another.
LLVM_ABI void changeSign()
LLVM_ABI bool isDenormal() const
IEEE-754R isSubnormal(): Returns true if and only if the float is a denormal.
LLVM_ABI opStatus convertToInteger(MutableArrayRef< integerPart >, unsigned int, bool, roundingMode, bool *) const
LLVM_ABI bool isSmallest() const
Returns true if and only if the number has the smallest possible non-zero magnitude in the current se...
An opaque object representing a hash code.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
static constexpr opStatus opInexact
APFloatBase::roundingMode roundingMode
LLVM_ABI SlowDynamicAPInt abs(const SlowDynamicAPInt &X)
Redeclarations of friend declarations above to make it discoverable by lookups.
static constexpr fltCategory fcNaN
static constexpr opStatus opDivByZero
static constexpr opStatus opOverflow
static constexpr cmpResult cmpLessThan
static void tcSetLeastSignificantBits(APInt::WordType *dst, unsigned parts, unsigned bits)
APFloatBase::opStatus opStatus
static constexpr roundingMode rmTowardPositive
static constexpr uninitializedTag uninitialized
static constexpr fltCategory fcZero
static constexpr opStatus opOK
static constexpr cmpResult cmpGreaterThan
static constexpr unsigned integerPartWidth
APFloatBase::ExponentType ExponentType
LLVM_ABI hash_code hash_value(const IEEEFloat &Arg)
static constexpr fltCategory fcNormal
static constexpr opStatus opInvalidOp
LLVM_ABI IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode)
LLVM_ABI IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM)
APFloatBase::integerPart integerPart
static constexpr cmpResult cmpUnordered
static constexpr roundingMode rmTowardNegative
static constexpr fltCategory fcInfinity
APFloatBase::cmpResult cmpResult
static constexpr roundingMode rmNearestTiesToAway
static constexpr roundingMode rmTowardZero
static constexpr opStatus opUnderflow
static constexpr roundingMode rmNearestTiesToEven
LLVM_ABI int ilogb(const IEEEFloat &Arg)
static constexpr cmpResult cmpEqual
static std::pair< APFloat, APFloat > fastTwoSum(APFloat X, APFloat Y)
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
static unsigned int partAsHex(char *dst, APFloatBase::integerPart part, unsigned int count, const char *hexDigitChars)
static constexpr fltSemantics semBogus
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
static const char infinityL[]
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
hash_code hash_value(const FixedPointSemantics &Val)
int popcount(T Value) noexcept
Count the number of set bits in a value.
static constexpr unsigned int partCountForBits(unsigned int bits)
static constexpr fltSemantics semFloat8E8M0FNU
static unsigned int HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2)
static unsigned int powerOf5(APFloatBase::integerPart *dst, unsigned int power)
static constexpr fltSemantics semFloat6E2M3FN
static APFloat harrisonUlp(const APFloat &X)
static constexpr APFloatBase::ExponentType exponentZero(const fltSemantics &semantics)
static Expected< int > totalExponent(StringRef::iterator p, StringRef::iterator end, int exponentAdjustment)
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
static constexpr fltSemantics semIEEEquad
const unsigned int maxPowerOfFiveExponent
static constexpr fltSemantics semFloat6E3M2FN
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
static char * writeUnsignedDecimal(char *dst, unsigned int n)
static constexpr fltSemantics semFloat8E4M3FNUZ
const unsigned int maxPrecision
static constexpr fltSemantics semIEEEdouble
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
static constexpr fltSemantics semFloat8E4M3FN
static const char infinityU[]
lostFraction
Enum that represents what fraction of the LSB truncated bits of an fp number represent.
static constexpr fltSemantics semPPCDoubleDouble
static Error interpretDecimal(StringRef::iterator begin, StringRef::iterator end, decimalInfo *D)
static constexpr fltSemantics semFloat8E5M2FNUZ
LLVM_ABI bool isFinite(const Loop *L)
Return true if this loop can be assumed to run for a finite number of iterations.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
const unsigned int maxPowerOfFiveParts
static constexpr fltSemantics semIEEEsingle
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
static constexpr fltSemantics semFloat4E2M1FN
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static constexpr APFloatBase::ExponentType exponentNaN(const fltSemantics &semantics)
static Error createError(const Twine &Err)
static constexpr fltSemantics semIEEEhalf
static constexpr fltSemantics semPPCDoubleDoubleLegacy
static lostFraction shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits)
static constexpr fltSemantics semFloat8E5M2
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
static const char hexDigitsUpper[]
const unsigned int maxExponent
static unsigned int decDigitValue(unsigned int c)
static constexpr fltSemantics semFloat8E4M3B11FNUZ
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static lostFraction combineLostFractions(lostFraction moreSignificant, lostFraction lessSignificant)
static Expected< StringRef::iterator > skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, StringRef::iterator *dot)
RoundingMode
Rounding mode.
static constexpr fltSemantics semX87DoubleExtended
static constexpr fltSemantics semFloatTF32
static constexpr APFloatBase::ExponentType exponentInf(const fltSemantics &semantics)
static lostFraction lostFractionThroughTruncation(const APFloatBase::integerPart *parts, unsigned int partCount, unsigned int bits)
APFloat neg(APFloat X)
Returns the negated value of the argument.
static APFloatBase::integerPart ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, bool isNearest)
static char * writeSignedDecimal(char *dst, int value)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
static constexpr fltSemantics semBFloat
static Expected< lostFraction > trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, unsigned int digitValue)
void consumeError(Error Err)
Consume a Error without doing anything.
static constexpr fltSemantics semFloat8E3M4
static Expected< int > readExponent(StringRef::iterator begin, StringRef::iterator end)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
static constexpr fltSemantics semFloat8E4M3
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
static const char hexDigitsLower[]
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
static LLVM_ABI const fltSemantics & Float6E3M2FN() LLVM_READNONE
static constexpr roundingMode rmNearestTiesToAway
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
static LLVM_ABI const fltSemantics & PPCDoubleDoubleLegacy() LLVM_READNONE
static constexpr roundingMode rmTowardNegative
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
static constexpr roundingMode rmNearestTiesToEven
static LLVM_ABI unsigned int semanticsSizeInBits(const fltSemantics &)
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
static LLVM_ABI const fltSemantics & Float8E4M3() LLVM_READNONE
static LLVM_ABI unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
static LLVM_ABI const fltSemantics & Float8E4M3FN() LLVM_READNONE
static LLVM_ABI const fltSemantics & PPCDoubleDouble() LLVM_READNONE
static constexpr roundingMode rmTowardZero
static LLVM_ABI const fltSemantics & x87DoubleExtended() LLVM_READNONE
uninitializedTag
Convenience enum used to construct an uninitialized APFloat.
static LLVM_ABI const fltSemantics & IEEEquad() LLVM_READNONE
static LLVM_ABI const fltSemantics & Float4E2M1FN() LLVM_READNONE
static LLVM_ABI const fltSemantics & Float8E8M0FNU() LLVM_READNONE
static LLVM_ABI bool hasSignBitInMSB(const fltSemantics &)
static LLVM_ABI const fltSemantics & Float8E4M3B11FNUZ() LLVM_READNONE
static LLVM_ABI const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real.
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
static LLVM_ABI const fltSemantics & IEEEdouble() LLVM_READNONE
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
static LLVM_ABI const fltSemantics & Float8E5M2() LLVM_READNONE
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
static constexpr unsigned integerPartWidth
static LLVM_ABI const fltSemantics & IEEEhalf() LLVM_READNONE
APInt::WordType integerPart
static constexpr roundingMode rmTowardPositive
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
static LLVM_ABI const fltSemantics & Float8E4M3FNUZ() LLVM_READNONE
static LLVM_ABI const fltSemantics & BFloat() LLVM_READNONE
static LLVM_ABI const fltSemantics & FloatTF32() LLVM_READNONE
static LLVM_ABI bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B)
static LLVM_ABI const fltSemantics & Float8E5M2FNUZ() LLVM_READNONE
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
static LLVM_ABI const fltSemantics & Float6E2M3FN() LLVM_READNONE
fltCategory
Category of internally-represented number.
@ S_PPCDoubleDoubleLegacy
static LLVM_ABI const fltSemantics & Float8E3M4() LLVM_READNONE
opStatus
IEEE-754R 7: Default exception handling.
int32_t ExponentType
A signed type to represent a floating point numbers unbiased exponent.
static LLVM_ABI unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
const char * lastSigDigit
const char * firstSigDigit
APFloatBase::ExponentType maxExponent
fltNonfiniteBehavior nonFiniteBehavior
APFloatBase::ExponentType minExponent
fltNanEncoding nanEncoding