21#include "llvm/Config/llvm-config.h"
29#define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \
31 if (usesLayout<IEEEFloat>(getSemantics())) \
32 return U.IEEE.METHOD_CALL; \
33 if (usesLayout<DoubleAPFloat>(getSemantics())) \
34 return U.Double.METHOD_CALL; \
35 llvm_unreachable("Unexpected semantics"); \
46#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs))
275static inline unsigned int
282static inline unsigned int
296 unsigned int absExponent;
297 const unsigned int overlargeExponent = 24000;
301 if (p == end || ((*p ==
'-' || *p ==
'+') && (p + 1) == end)) {
305 isNegative = (*p ==
'-');
306 if (*p ==
'-' || *p ==
'+') {
313 if (absExponent >= 10U)
314 return createError(
"Invalid character in exponent");
316 for (; p != end; ++p) {
321 return createError(
"Invalid character in exponent");
323 absExponent = absExponent * 10U +
value;
324 if (absExponent >= overlargeExponent) {
325 absExponent = overlargeExponent;
331 return -(int) absExponent;
333 return (
int) absExponent;
340 int exponentAdjustment) {
341 int unsignedExponent;
342 bool negative, overflow;
348 negative = *p ==
'-';
349 if (*p ==
'-' || *p ==
'+') {
355 unsignedExponent = 0;
357 for (; p != end; ++p) {
362 return createError(
"Invalid character in exponent");
364 unsignedExponent = unsignedExponent * 10 +
value;
365 if (unsignedExponent > 32767) {
371 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
375 exponent = unsignedExponent;
377 exponent = -exponent;
378 exponent += exponentAdjustment;
379 if (exponent > 32767 || exponent < -32768)
384 exponent = negative ? -32768: 32767;
394 while (p != end && *p ==
'0')
397 if (p != end && *p ==
'.') {
400 if (end - begin == 1)
403 while (p != end && *p ==
'0')
436 return PtrOrErr.takeError();
439 D->firstSigDigit = p;
441 D->normalizedExponent = 0;
443 for (; p != end; ++p) {
446 return createError(
"String contains multiple dots");
456 if (*p !=
'e' && *p !=
'E')
457 return createError(
"Invalid character in significand");
460 if (dot != end && p - begin == 1)
466 return ExpOrErr.takeError();
467 D->exponent = *ExpOrErr;
475 if (p !=
D->firstSigDigit) {
481 while (p != begin && *p ==
'0');
482 while (p != begin && *p ==
'.');
487 D->normalizedExponent = (
D->exponent +
489 - (dot >
D->firstSigDigit && dot < p)));
501 unsigned int digitValue) {
502 unsigned int hexDigit;
508 else if (digitValue < 8 && digitValue > 0)
512 while (p != end && (*p ==
'0' || *p ==
'.'))
516 return createError(
"Invalid trailing hexadecimal fraction!");
518 hexDigit = hexDigitValue(*p);
532 unsigned int partCount,
561 return lost_fraction;
576 return moreSignificant;
587HUerrBound(
bool inexactMultiply,
unsigned int HUerr1,
unsigned int HUerr2)
589 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
591 if (HUerr1 + HUerr2 == 0)
592 return inexactMultiply * 2;
594 return inexactMultiply + 2 * (HUerr1 + HUerr2);
603 unsigned int count, partBits;
620 if (part - boundary <= boundary - part)
621 return part - boundary;
623 return boundary - part;
626 if (part == boundary) {
632 }
else if (part == boundary - 1) {
649 pow5s[0] = 78125 * 5;
651 unsigned int partsCount[16] = { 1 };
659 *p1 = firstEightPowers[power & 7];
665 for (
unsigned int n = 0; power; power >>= 1, n++) {
672 pc = partsCount[n - 1];
675 if (pow5[pc - 1] == 0)
685 if (p2[result - 1] == 0)
710static const char NaNL[] =
"nan";
711static const char NaNU[] =
"NAN";
718 const char *hexDigitChars)
720 unsigned int result =
count;
726 dst[
count] = hexDigitChars[part & 0xf];
766void IEEEFloat::initialize(
const fltSemantics *ourSemantics) {
769 semantics = ourSemantics;
775void IEEEFloat::freeSignificand() {
777 delete [] significand.parts;
780void IEEEFloat::assign(
const IEEEFloat &rhs) {
781 assert(semantics == rhs.semantics);
784 category = rhs.category;
785 exponent = rhs.exponent;
787 copySignificand(rhs);
790void IEEEFloat::copySignificand(
const IEEEFloat &rhs) {
792 assert(rhs.partCount() >= partCount());
804 exponent = exponentNaN();
807 unsigned numParts = partCount();
815 fill = &fill_storage;
826 unsigned bitsToPreserve = semantics->
precision - 1;
827 unsigned part = bitsToPreserve / 64;
828 bitsToPreserve %= 64;
829 significand[part] &= ((1ULL << bitsToPreserve) - 1);
830 for (part++; part != numParts; ++part)
831 significand[part] = 0;
834 unsigned QNaNBit = semantics->
precision - 2;
859 if (semantics != rhs.semantics) {
861 initialize(rhs.semantics);
872 semantics = rhs.semantics;
873 significand = rhs.significand;
874 exponent = rhs.exponent;
875 category = rhs.category;
893 significandMSB() == 0;
898 isSignificandAllZerosExceptMSB();
901bool IEEEFloat::isSignificandAllOnes()
const {
906 for (
unsigned i = 0; i < PartCount - 1; i++)
911 const unsigned NumHighBits =
913 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
914 "Can not have more high bits to fill than integerPartWidth");
917 if (~(Parts[PartCount - 1] | HighBitFill))
923bool IEEEFloat::isSignificandAllOnesExceptLSB()
const {
932 for (
unsigned i = 0; i < PartCount - 1; i++) {
933 if (~Parts[i] & ~
unsigned{!i})
938 const unsigned NumHighBits =
940 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
941 "Can not have more high bits to fill than integerPartWidth");
944 if (~(Parts[PartCount - 1] | HighBitFill | 0x1))
950bool IEEEFloat::isSignificandAllZeros()
const {
956 for (
unsigned i = 0; i < PartCount - 1; i++)
961 const unsigned NumHighBits =
964 "clear than integerPartWidth");
965 const integerPart HighBitMask = ~integerPart(0) >> NumHighBits;
967 if (Parts[PartCount - 1] & HighBitMask)
973bool IEEEFloat::isSignificandAllZerosExceptMSB()
const {
977 for (
unsigned i = 0; i < PartCount - 1; i++) {
982 const unsigned NumHighBits =
994 isSignificandAllOnesExceptLSB();
999 isSignificandAllOnes();
1014 if (semantics != rhs.semantics ||
1015 category != rhs.category ||
1024 return std::equal(significandParts(), significandParts() + partCount(),
1025 rhs.significandParts());
1029 initialize(&ourSemantics);
1034 significandParts()[0] =
value;
1039 initialize(&ourSemantics);
1049 initialize(rhs.semantics);
1054 *
this = std::move(rhs);
1059unsigned int IEEEFloat::partCount()
const {
1064 return const_cast<IEEEFloat *
>(
this)->significandParts();
1068 if (partCount() > 1)
1069 return significand.parts;
1071 return &significand.part;
1074void IEEEFloat::zeroSignificand() {
1079void IEEEFloat::incrementSignificand() {
1093 parts = significandParts();
1095 assert(semantics == rhs.semantics);
1096 assert(exponent == rhs.exponent);
1098 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
1104 integerPart borrow) {
1107 parts = significandParts();
1109 assert(semantics == rhs.semantics);
1110 assert(exponent == rhs.exponent);
1119lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs,
1122 unsigned int partsCount, newPartsCount, precision;
1129 assert(semantics == rhs.semantics);
1137 if (newPartsCount > 4)
1140 fullSignificand = scratch;
1142 lhsSignificand = significandParts();
1143 partsCount = partCount();
1146 rhs.significandParts(), partsCount, partsCount);
1149 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1150 exponent += rhs.exponent;
1164 if (addend.isNonZero()) {
1168 Significand savedSignificand = significand;
1172 unsigned int extendedPrecision;
1175 extendedPrecision = 2 * precision + 1;
1176 if (omsb != extendedPrecision - 1) {
1177 assert(extendedPrecision > omsb);
1179 (extendedPrecision - 1) - omsb);
1180 exponent -= (extendedPrecision - 1) - omsb;
1184 extendedSemantics = *semantics;
1185 extendedSemantics.
precision = extendedPrecision;
1187 if (newPartsCount == 1)
1188 significand.part = fullSignificand[0];
1190 significand.parts = fullSignificand;
1191 semantics = &extendedSemantics;
1204 lost_fraction = extendedAddend.shiftSignificandRight(1);
1206 "Lost precision while shifting addend for fused-multiply-add.");
1208 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
1211 if (newPartsCount == 1)
1212 fullSignificand[0] = significand.part;
1213 significand = savedSignificand;
1214 semantics = savedSemantics;
1216 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1223 exponent -= precision + 1;
1232 if (omsb > precision) {
1233 unsigned int bits, significantParts;
1236 bits = omsb - precision;
1245 if (newPartsCount > 4)
1246 delete [] fullSignificand;
1248 return lost_fraction;
1251lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs) {
1252 return multiplySignificand(rhs,
IEEEFloat(*semantics));
1256lostFraction IEEEFloat::divideSignificand(
const IEEEFloat &rhs) {
1257 unsigned int bit, i, partsCount;
1263 assert(semantics == rhs.semantics);
1265 lhsSignificand = significandParts();
1266 rhsSignificand = rhs.significandParts();
1267 partsCount = partCount();
1274 divisor = dividend + partsCount;
1277 for (i = 0; i < partsCount; i++) {
1278 dividend[i] = lhsSignificand[i];
1279 divisor[i] = rhsSignificand[i];
1280 lhsSignificand[i] = 0;
1283 exponent -= rhs.exponent;
1285 unsigned int precision = semantics->
precision;
1288 bit = precision -
APInt::tcMSB(divisor, partsCount) - 1;
1295 bit = precision -
APInt::tcMSB(dividend, partsCount) - 1;
1311 for (bit = precision; bit; bit -= 1) {
1335 return lost_fraction;
1338unsigned int IEEEFloat::significandMSB()
const {
1342unsigned int IEEEFloat::significandLSB()
const {
1357void IEEEFloat::shiftSignificandLeft(
unsigned int bits) {
1358 assert(bits < semantics->precision);
1361 unsigned int partsCount = partCount();
1374 assert(semantics == rhs.semantics);
1378 compare = exponent - rhs.exponent;
1442bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode,
1444 unsigned int bit)
const {
1451 switch (rounding_mode) {
1489 omsb = significandMSB() + 1;
1495 exponentChange = omsb - semantics->
precision;
1499 if (exponent + exponentChange > semantics->
maxExponent)
1500 return handleOverflow(rounding_mode);
1504 if (exponent + exponentChange < semantics->minExponent)
1505 exponentChange = semantics->
minExponent - exponent;
1508 if (exponentChange < 0) {
1511 shiftSignificandLeft(-exponentChange);
1516 if (exponentChange > 0) {
1520 lf = shiftSignificandRight(exponentChange);
1525 if (omsb > (
unsigned) exponentChange)
1526 omsb -= exponentChange;
1533 exponent == semantics->
maxExponent && isSignificandAllOnes())
1534 return handleOverflow(rounding_mode);
1550 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1554 incrementSignificand();
1555 omsb = significandMSB() + 1;
1558 if (omsb == (
unsigned) semantics->
precision + 1) {
1568 shiftSignificandRight(1);
1574 exponent == semantics->
maxExponent && isSignificandAllOnes())
1575 return handleOverflow(rounding_mode);
1584 assert(omsb < semantics->precision);
1651lostFraction IEEEFloat::addOrSubtractSignificand(
const IEEEFloat &rhs,
1659 subtract ^=
static_cast<bool>(sign ^ rhs.sign);
1662 bits = exponent - rhs.exponent;
1670 else if (
bits > 0) {
1671 lost_fraction = temp_rhs.shiftSignificandRight(
bits - 1);
1672 shiftSignificandLeft(1);
1674 lost_fraction = shiftSignificandRight(-
bits - 1);
1675 temp_rhs.shiftSignificandLeft(1);
1680 carry = temp_rhs.subtractSignificand
1682 copySignificand(temp_rhs);
1685 carry = subtractSignificand
1704 lost_fraction = temp_rhs.shiftSignificandRight(
bits);
1705 carry = addSignificand(temp_rhs);
1707 lost_fraction = shiftSignificandRight(-
bits);
1708 carry = addSignificand(rhs);
1716 return lost_fraction;
1896 roundingMode rounding_mode,
1900 fs = addOrSubtractSpecials(rhs,
subtract);
1906 lost_fraction = addOrSubtractSignificand(rhs,
subtract);
1907 fs = normalize(rounding_mode, lost_fraction);
1916 if (category ==
fcZero) {
1927 return addOrSubtract(rhs, rounding_mode,
false);
1933 return addOrSubtract(rhs, rounding_mode,
true);
1942 fs = multiplySpecials(rhs);
1946 fs = normalize(rounding_mode, lost_fraction);
1960 fs = divideSpecials(rhs);
1964 fs = normalize(rounding_mode, lost_fraction);
1975 unsigned int origSign = sign;
1978 fs = remainderSpecials(rhs);
2080 fs = modSpecials(rhs);
2081 unsigned int origSign = sign;
2108 sign ^= multiplicand.sign;
2117 lost_fraction = multiplySignificand(multiplicand, addend);
2118 fs = normalize(rounding_mode, lost_fraction);
2128 fs = multiplySpecials(multiplicand);
2138 fs = addOrSubtract(addend, rounding_mode,
false);
2211 MagicConstant.sign = sign;
2217 fs =
add(MagicConstant, rounding_mode);
2221 subtract(MagicConstant, rounding_mode);
2235 assert(semantics == rhs.semantics);
2267 if (sign == rhs.sign)
2282 if (sign != rhs.sign) {
2313 unsigned int newPartCount, oldPartCount;
2321 oldPartCount = partCount();
2324 bool X86SpecialNan =
false;
2327 (!(*significandParts() & 0x8000000000000000ULL) ||
2328 !(*significandParts() & 0x4000000000000000ULL))) {
2331 X86SpecialNan =
true;
2342 int omsb = significandMSB() + 1;
2343 int exponentChange = omsb - fromSemantics.
precision;
2344 if (exponent + exponentChange < toSemantics.
minExponent)
2345 exponentChange = toSemantics.
minExponent - exponent;
2346 if (exponentChange < shift)
2347 exponentChange = shift;
2348 if (exponentChange < 0) {
2349 shift -= exponentChange;
2350 exponent += exponentChange;
2351 }
else if (omsb <= -shift) {
2352 exponentChange = omsb + shift - 1;
2353 shift -= exponentChange;
2354 exponent += exponentChange;
2365 if (newPartCount > oldPartCount) {
2373 significand.parts = newParts;
2374 }
else if (newPartCount == 1 && oldPartCount != 1) {
2378 newPart = significandParts()[0];
2380 significand.part = newPart;
2384 semantics = &toSemantics;
2393 *losesInfo = (fs !=
opOK);
2394 }
else if (category ==
fcNaN) {
2443 roundingMode rounding_mode,
bool *isExact)
const {
2446 unsigned int dstPartsCount, truncatedBits;
2455 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2457 if (category ==
fcZero) {
2464 src = significandParts();
2473 truncatedBits = semantics->
precision -1U - exponent;
2477 unsigned int bits = exponent + 1U;
2483 if (bits < semantics->precision) {
2499 if (truncatedBits) {
2503 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2523 if (omsb == width &&
2560 fs = convertToSignExtendedInteger(parts, width,
isSigned, rounding_mode,
2564 unsigned int bits, dstPartsCount;
2567 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2569 if (category ==
fcNaN)
2588 const integerPart *src,
unsigned int srcCount, roundingMode rounding_mode) {
2589 unsigned int omsb, precision, dstCount;
2595 dst = significandParts();
2596 dstCount = partCount();
2601 if (precision <= omsb) {
2602 exponent = omsb - 1;
2607 exponent = precision - 1;
2612 return normalize(rounding_mode, lost_fraction);
2626 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2634 unsigned int srcCount,
bool isSigned,
2647 status = convertFromUnsignedParts(
copy, srcCount, rounding_mode);
2651 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2671 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2675IEEEFloat::convertFromHexadecimalString(
StringRef s,
2676 roundingMode rounding_mode) {
2684 unsigned partsCount = partCount();
2686 bool computedTrailingFraction =
false;
2694 return PtrOrErr.takeError();
2703 return createError(
"String contains multiple dots");
2708 hex_value = hexDigitValue(*p);
2709 if (hex_value == -1U)
2719 }
else if (!computedTrailingFraction) {
2722 return FractOrErr.takeError();
2723 lost_fraction = *FractOrErr;
2724 computedTrailingFraction =
true;
2730 return createError(
"Hex strings require an exponent");
2731 if (*p !=
'p' && *p !=
'P')
2732 return createError(
"Invalid character in significand");
2735 if (dot != end && p - begin == 1)
2739 if (p != firstSignificantDigit) {
2748 expAdjustment =
static_cast<int>(
dot - firstSignificantDigit);
2749 if (expAdjustment < 0)
2751 expAdjustment = expAdjustment * 4 - 1;
2761 return ExpOrErr.takeError();
2762 exponent = *ExpOrErr;
2765 return normalize(rounding_mode, lost_fraction);
2769IEEEFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
2770 unsigned sigPartCount,
int exp,
2771 roundingMode rounding_mode) {
2772 unsigned int parts, pow5PartCount;
2783 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2785 for (;; parts *= 2) {
2787 unsigned int excessPrecision, truncatedBits;
2791 truncatedBits = excessPrecision;
2794 decSig.makeZero(sign);
2797 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2799 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2802 decSig.exponent += exp;
2806 unsigned int powHUerr;
2810 calcLostFraction = decSig.multiplySignificand(pow5);
2811 powHUerr = powStatus !=
opOK;
2813 calcLostFraction = decSig.divideSignificand(pow5);
2816 excessPrecision += (semantics->
minExponent - decSig.exponent);
2817 truncatedBits = excessPrecision;
2818 if (excessPrecision > calcSemantics.
precision)
2819 excessPrecision = calcSemantics.
precision;
2828 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
2833 excessPrecision, isNearest);
2836 if (HUdistance >= HUerr) {
2837 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2838 calcSemantics.
precision - excessPrecision,
2843 exponent = (decSig.exponent + semantics->
precision
2844 - (calcSemantics.
precision - excessPrecision));
2848 return normalize(rounding_mode, calcLostFraction);
2854IEEEFloat::convertFromDecimalString(
StringRef str, roundingMode rounding_mode) {
2861 return std::move(Err);
2893 }
else if (
D.normalizedExponent - 1 > INT_MAX / 42039) {
2894 fs = handleOverflow(rounding_mode);
2900 }
else if (
D.normalizedExponent - 1 < INT_MIN / 42039 ||
2901 (
D.normalizedExponent + 1) * 28738 <=
2909 }
else if ((
D.normalizedExponent - 1) * 42039
2912 fs = handleOverflow(rounding_mode);
2915 unsigned int partCount;
2921 partCount =
static_cast<unsigned int>(
D.lastSigDigit -
D.firstSigDigit) + 1;
2939 if (p == str.
end()) {
2944 if (decValue >= 10U) {
2945 delete[] decSignificand;
2946 return createError(
"Invalid character in significand");
2949 val = val * 10 + decValue;
2952 }
while (p <=
D.lastSigDigit && multiplier <= (~ (
integerPart) 0 - 9) / 10);
2956 partCount, partCount + 1,
false);
2960 if (decSignificand[partCount])
2962 }
while (p <=
D.lastSigDigit);
2965 fs = roundSignificandWithExponent(decSignificand, partCount,
2966 D.exponent, rounding_mode);
2968 delete [] decSignificand;
2974bool IEEEFloat::convertFromStringSpecials(
StringRef str) {
2975 const size_t MIN_NAME_SIZE = 3;
2977 if (str.
size() < MIN_NAME_SIZE)
2985 bool IsNegative = str.
front() ==
'-';
2988 if (str.
size() < MIN_NAME_SIZE)
2998 bool IsSignaling = str.
front() ==
's' || str.
front() ==
'S';
3001 if (str.
size() < MIN_NAME_SIZE)
3010 makeNaN(IsSignaling, IsNegative);
3015 if (str.
front() ==
'(') {
3017 if (str.
size() <= 2 || str.
back() !=
')')
3024 unsigned Radix = 10;
3025 if (str[0] ==
'0') {
3026 if (str.
size() > 1 && tolower(str[1]) ==
'x') {
3036 makeNaN(IsSignaling, IsNegative, &Payload);
3050 if (convertFromStringSpecials(str))
3055 size_t slen = str.
size();
3056 sign = *p ==
'-' ? 1 : 0;
3057 if (*p ==
'-' || *p ==
'+') {
3064 if (slen >= 2 && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
3067 return convertFromHexadecimalString(
StringRef(p + 2, slen - 2),
3071 return convertFromDecimalString(
StringRef(p, slen), rounding_mode);
3115 dst +=
sizeof NaNU - 1;
3120 *dst++ = upperCase ?
'X':
'x';
3122 if (hexDigits > 1) {
3124 memset (dst,
'0', hexDigits - 1);
3125 dst += hexDigits - 1;
3127 *dst++ = upperCase ?
'P':
'p';
3132 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
3138 return static_cast<unsigned int>(dst - p);
3145char *IEEEFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
3147 roundingMode rounding_mode)
const {
3148 unsigned int count, valueBits, shift, partsCount, outputDigits;
3149 const char *hexDigitChars;
3155 *dst++ = upperCase ?
'X':
'x';
3160 significand = significandParts();
3161 partsCount = partCount();
3170 outputDigits = (valueBits - significandLSB () + 3) / 4;
3176 if (hexDigits < outputDigits) {
3182 bits = valueBits - hexDigits * 4;
3184 roundUp = roundAwayFromZero(rounding_mode, fraction,
bits);
3186 outputDigits = hexDigits;
3196 while (outputDigits &&
count) {
3200 if (--
count == partsCount)
3203 part = significand[
count] << shift;
3211 if (curDigits > outputDigits)
3212 curDigits = outputDigits;
3213 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
3214 outputDigits -= curDigits;
3223 *q = hexDigitChars[hexDigitValue (*q) + 1];
3224 }
while (*q ==
'0');
3228 memset (dst,
'0', outputDigits);
3229 dst += outputDigits;
3242 *dst++ = upperCase ?
'P':
'p';
3248 if (!
Arg.isFiniteNonZero())
3251 Arg.isNaN() ? (uint8_t)0 : (uint8_t)
Arg.sign,
3252 Arg.semantics->precision);
3256 Arg.semantics->precision,
Arg.exponent,
3258 Arg.significandParts(),
3259 Arg.significandParts() +
Arg.partCount()));
3271APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt()
const {
3275 uint64_t myexponent, mysignificand;
3278 myexponent = exponent+16383;
3279 mysignificand = significandParts()[0];
3280 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
3282 }
else if (category==
fcZero) {
3286 myexponent = 0x7fff;
3287 mysignificand = 0x8000000000000000ULL;
3290 myexponent = 0x7fff;
3291 mysignificand = significandParts()[0];
3295 words[0] = mysignificand;
3296 words[1] = ((
uint64_t)(sign & 1) << 15) |
3297 (myexponent & 0x7fffLL);
3298 return APInt(80, words);
3301APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt()
const {
3326 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
3332 if (u.isFiniteNonZero() && losesInfo) {
3342 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
3347 return APInt(128, words);
3350APInt IEEEFloat::convertQuadrupleAPFloatToAPInt()
const {
3354 uint64_t myexponent, mysignificand, mysignificand2;
3357 myexponent = exponent+16383;
3358 mysignificand = significandParts()[0];
3359 mysignificand2 = significandParts()[1];
3360 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
3362 }
else if (category==
fcZero) {
3364 mysignificand = mysignificand2 = 0;
3366 myexponent = 0x7fff;
3367 mysignificand = mysignificand2 = 0;
3370 myexponent = 0x7fff;
3371 mysignificand = significandParts()[0];
3372 mysignificand2 = significandParts()[1];
3376 words[0] = mysignificand;
3377 words[1] = ((
uint64_t)(sign & 1) << 63) |
3378 ((myexponent & 0x7fff) << 48) |
3379 (mysignificand2 & 0xffffffffffffLL);
3381 return APInt(128, words);
3384APInt IEEEFloat::convertDoubleAPFloatToAPInt()
const {
3388 uint64_t myexponent, mysignificand;
3391 myexponent = exponent+1023;
3392 mysignificand = *significandParts();
3393 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3395 }
else if (category==
fcZero) {
3404 mysignificand = *significandParts();
3408 ((myexponent & 0x7ff) << 52) |
3409 (mysignificand & 0xfffffffffffffLL))));
3412APInt IEEEFloat::convertFloatAPFloatToAPInt()
const {
3416 uint32_t myexponent, mysignificand;
3419 myexponent = exponent+127;
3420 mysignificand = (
uint32_t)*significandParts();
3421 if (myexponent == 1 && !(mysignificand & 0x800000))
3423 }
else if (category==
fcZero) {
3432 mysignificand = (
uint32_t)*significandParts();
3435 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3436 (mysignificand & 0x7fffff)));
3439APInt IEEEFloat::convertBFloatAPFloatToAPInt()
const {
3441 assert(partCount() == 1);
3443 uint32_t myexponent, mysignificand;
3446 myexponent = exponent + 127;
3447 mysignificand = (
uint32_t)*significandParts();
3448 if (myexponent == 1 && !(mysignificand & 0x80))
3450 }
else if (category ==
fcZero) {
3459 mysignificand = (
uint32_t)*significandParts();
3462 return APInt(16, (((sign & 1) << 15) | ((myexponent & 0xff) << 7) |
3463 (mysignificand & 0x7f)));
3466APInt IEEEFloat::convertHalfAPFloatToAPInt()
const {
3470 uint32_t myexponent, mysignificand;
3473 myexponent = exponent+15;
3474 mysignificand = (
uint32_t)*significandParts();
3475 if (myexponent == 1 && !(mysignificand & 0x400))
3477 }
else if (category==
fcZero) {
3486 mysignificand = (
uint32_t)*significandParts();
3489 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3490 (mysignificand & 0x3ff)));
3493APInt IEEEFloat::convertFloat8E5M2APFloatToAPInt()
const {
3495 assert(partCount() == 1);
3497 uint32_t myexponent, mysignificand;
3500 myexponent = exponent + 15;
3501 mysignificand = (
uint32_t)*significandParts();
3502 if (myexponent == 1 && !(mysignificand & 0x4))
3504 }
else if (category ==
fcZero) {
3513 mysignificand = (
uint32_t)*significandParts();
3516 return APInt(8, (((sign & 1) << 7) | ((myexponent & 0x1f) << 2) |
3517 (mysignificand & 0x3)));
3520APInt IEEEFloat::convertFloat8E4M3FNAPFloatToAPInt()
const {
3522 assert(partCount() == 1);
3524 uint32_t myexponent, mysignificand;
3527 myexponent = exponent + 7;
3528 mysignificand = (
uint32_t)*significandParts();
3529 if (myexponent == 1 && !(mysignificand & 0x8))
3531 }
else if (category ==
fcZero) {
3540 mysignificand = (
uint32_t)*significandParts();
3543 return APInt(8, (((sign & 1) << 7) | ((myexponent & 0xf) << 3) |
3544 (mysignificand & 0x7)));
3553 return convertHalfAPFloatToAPInt();
3556 return convertBFloatAPFloatToAPInt();
3559 return convertFloatAPFloatToAPInt();
3562 return convertDoubleAPFloatToAPInt();
3565 return convertQuadrupleAPFloatToAPInt();
3568 return convertPPCDoubleDoubleAPFloatToAPInt();
3571 return convertFloat8E5M2APFloatToAPInt();
3574 return convertFloat8E4M3FNAPFloatToAPInt();
3578 return convertF80LongDoubleAPFloatToAPInt();
3583 "Float semantics are not IEEEsingle");
3590 "Float semantics are not IEEEdouble");
3602void IEEEFloat::initFromF80LongDoubleAPInt(
const APInt &api) {
3605 uint64_t myexponent = (i2 & 0x7fff);
3607 uint8_t myintegerbit = mysignificand >> 63;
3612 sign =
static_cast<unsigned int>(i2>>15);
3613 if (myexponent == 0 && mysignificand == 0) {
3615 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3617 }
else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3618 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3620 exponent = exponentNaN();
3621 significandParts()[0] = mysignificand;
3622 significandParts()[1] = 0;
3625 exponent = myexponent - 16383;
3626 significandParts()[0] = mysignificand;
3627 significandParts()[1] = 0;
3633void IEEEFloat::initFromPPCDoubleDoubleAPInt(
const APInt &api) {
3640 initFromDoubleAPInt(
APInt(64, i1));
3656void IEEEFloat::initFromQuadrupleAPInt(
const APInt &api) {
3659 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3661 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3666 sign =
static_cast<unsigned int>(i2>>63);
3667 if (myexponent==0 &&
3668 (mysignificand==0 && mysignificand2==0)) {
3670 }
else if (myexponent==0x7fff &&
3671 (mysignificand==0 && mysignificand2==0)) {
3673 }
else if (myexponent==0x7fff &&
3674 (mysignificand!=0 || mysignificand2 !=0)) {
3676 exponent = exponentNaN();
3677 significandParts()[0] = mysignificand;
3678 significandParts()[1] = mysignificand2;
3681 exponent = myexponent - 16383;
3682 significandParts()[0] = mysignificand;
3683 significandParts()[1] = mysignificand2;
3687 significandParts()[1] |= 0x1000000000000LL;
3691void IEEEFloat::initFromDoubleAPInt(
const APInt &api) {
3693 uint64_t myexponent = (i >> 52) & 0x7ff;
3694 uint64_t mysignificand = i & 0xfffffffffffffLL;
3699 sign =
static_cast<unsigned int>(i>>63);
3700 if (myexponent==0 && mysignificand==0) {
3702 }
else if (myexponent==0x7ff && mysignificand==0) {
3704 }
else if (myexponent==0x7ff && mysignificand!=0) {
3706 exponent = exponentNaN();
3707 *significandParts() = mysignificand;
3710 exponent = myexponent - 1023;
3711 *significandParts() = mysignificand;
3715 *significandParts() |= 0x10000000000000LL;
3719void IEEEFloat::initFromFloatAPInt(
const APInt &api) {
3721 uint32_t myexponent = (i >> 23) & 0xff;
3722 uint32_t mysignificand = i & 0x7fffff;
3728 if (myexponent==0 && mysignificand==0) {
3730 }
else if (myexponent==0xff && mysignificand==0) {
3732 }
else if (myexponent==0xff && mysignificand!=0) {
3734 exponent = exponentNaN();
3735 *significandParts() = mysignificand;
3738 exponent = myexponent - 127;
3739 *significandParts() = mysignificand;
3743 *significandParts() |= 0x800000;
3747void IEEEFloat::initFromBFloatAPInt(
const APInt &api) {
3749 uint32_t myexponent = (i >> 7) & 0xff;
3753 assert(partCount() == 1);
3756 if (myexponent == 0 && mysignificand == 0) {
3758 }
else if (myexponent == 0xff && mysignificand == 0) {
3760 }
else if (myexponent == 0xff && mysignificand != 0) {
3762 exponent = exponentNaN();
3763 *significandParts() = mysignificand;
3766 exponent = myexponent - 127;
3767 *significandParts() = mysignificand;
3768 if (myexponent == 0)
3771 *significandParts() |= 0x80;
3775void IEEEFloat::initFromHalfAPInt(
const APInt &api) {
3777 uint32_t myexponent = (i >> 10) & 0x1f;
3778 uint32_t mysignificand = i & 0x3ff;
3784 if (myexponent==0 && mysignificand==0) {
3786 }
else if (myexponent==0x1f && mysignificand==0) {
3788 }
else if (myexponent==0x1f && mysignificand!=0) {
3790 exponent = exponentNaN();
3791 *significandParts() = mysignificand;
3794 exponent = myexponent - 15;
3795 *significandParts() = mysignificand;
3799 *significandParts() |= 0x400;
3803void IEEEFloat::initFromFloat8E5M2APInt(
const APInt &api) {
3805 uint32_t myexponent = (i >> 2) & 0x1f;
3809 assert(partCount() == 1);
3812 if (myexponent == 0 && mysignificand == 0) {
3814 }
else if (myexponent == 0x1f && mysignificand == 0) {
3816 }
else if (myexponent == 0x1f && mysignificand != 0) {
3818 exponent = exponentNaN();
3819 *significandParts() = mysignificand;
3822 exponent = myexponent - 15;
3823 *significandParts() = mysignificand;
3824 if (myexponent == 0)
3827 *significandParts() |= 0x4;
3831void IEEEFloat::initFromFloat8E4M3FNAPInt(
const APInt &api) {
3833 uint32_t myexponent = (i >> 3) & 0xf;
3837 assert(partCount() == 1);
3840 if (myexponent == 0 && mysignificand == 0) {
3842 }
else if (myexponent == 0xf && mysignificand == 7) {
3844 exponent = exponentNaN();
3845 *significandParts() = mysignificand;
3848 exponent = myexponent - 7;
3849 *significandParts() = mysignificand;
3850 if (myexponent == 0)
3853 *significandParts() |= 0x8;
3861 return initFromHalfAPInt(api);
3863 return initFromBFloatAPInt(api);
3865 return initFromFloatAPInt(api);
3867 return initFromDoubleAPInt(api);
3869 return initFromF80LongDoubleAPInt(api);
3871 return initFromQuadrupleAPInt(api);
3873 return initFromPPCDoubleDoubleAPInt(api);
3875 return initFromFloat8E5M2APInt(api);
3877 return initFromFloat8E4M3FNAPInt(api);
3895 unsigned PartCount = partCount();
3896 memset(significand, 0xFF,
sizeof(
integerPart)*(PartCount - 1));
3900 const unsigned NumUnusedHighBits =
3937 initFromAPInt(&Sem, API);
3950 Buffer.
append(Str.begin(), Str.end());
3955 void AdjustToPrecision(
APInt &significand,
3956 int &exp,
unsigned FormatPrecision) {
3960 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3962 if (
bits <= bitsRequired)
return;
3964 unsigned tensRemovable = (
bits - bitsRequired) * 59 / 196;
3965 if (!tensRemovable)
return;
3967 exp += tensRemovable;
3972 if (tensRemovable & 1)
3974 tensRemovable >>= 1;
3975 if (!tensRemovable)
break;
3979 significand = significand.
udiv(divisor);
3987 int &exp,
unsigned FormatPrecision) {
3988 unsigned N = buffer.
size();
3989 if (
N <= FormatPrecision)
return;
3992 unsigned FirstSignificant =
N - FormatPrecision;
3999 if (buffer[FirstSignificant - 1] <
'5') {
4000 while (FirstSignificant <
N && buffer[FirstSignificant] ==
'0')
4003 exp += FirstSignificant;
4004 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
4010 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
4011 if (buffer[
I] ==
'9') {
4020 if (FirstSignificant ==
N) {
4021 exp += FirstSignificant;
4027 exp += FirstSignificant;
4028 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
4033 unsigned FormatMaxPadding,
bool TruncateZero)
const {
4037 return append(Str,
"-Inf");
4039 return append(Str,
"+Inf");
4041 case fcNaN:
return append(Str,
"NaN");
4047 if (!FormatMaxPadding) {
4049 append(Str,
"0.0E+0");
4052 if (FormatPrecision > 1)
4053 Str.append(FormatPrecision - 1,
'0');
4054 append(Str,
"e+00");
4068 int exp = exponent - ((int) semantics->
precision - 1);
4075 if (!FormatPrecision) {
4083 FormatPrecision = 2 + semantics->
precision * 59 / 196;
4088 exp += trailingZeros;
4094 }
else if (exp > 0) {
4097 significand <<= exp;
4111 unsigned precision = semantics->
precision + (137 * texp + 136) / 59;
4115 significand = significand.
zext(precision);
4116 APInt five_to_the_i(precision, 5);
4118 if (texp & 1) significand *= five_to_the_i;
4122 five_to_the_i *= five_to_the_i;
4126 AdjustToPrecision(significand, exp, FormatPrecision);
4132 if (precision < 4) {
4135 significand = significand.
zext(precision);
4137 APInt ten(precision, 10);
4138 APInt digit(precision, 0);
4140 bool inTrail =
true;
4141 while (significand != 0) {
4149 if (inTrail && !d) exp++;
4156 assert(!buffer.
empty() &&
"no characters in buffer!");
4160 AdjustToPrecision(buffer, exp, FormatPrecision);
4162 unsigned NDigits = buffer.
size();
4165 bool FormatScientific;
4166 if (!FormatMaxPadding)
4167 FormatScientific =
true;
4173 FormatScientific = ((
unsigned) exp > FormatMaxPadding ||
4174 NDigits + (
unsigned) exp > FormatPrecision);
4177 int MSD = exp + (int) (NDigits - 1);
4180 FormatScientific =
false;
4184 FormatScientific = ((
unsigned) -MSD) > FormatMaxPadding;
4190 if (FormatScientific) {
4191 exp += (NDigits - 1);
4193 Str.push_back(buffer[NDigits-1]);
4195 if (NDigits == 1 && TruncateZero)
4198 for (
unsigned I = 1;
I != NDigits; ++
I)
4199 Str.push_back(buffer[NDigits-1-
I]);
4201 if (!TruncateZero && FormatPrecision > NDigits - 1)
4202 Str.append(FormatPrecision - NDigits + 1,
'0');
4204 Str.push_back(TruncateZero ?
'E' :
'e');
4206 Str.push_back(exp >= 0 ?
'+' :
'-');
4207 if (exp < 0) exp = -exp;
4210 expbuf.
push_back((
char) (
'0' + (exp % 10)));
4214 if (!TruncateZero && expbuf.
size() < 2)
4216 for (
unsigned I = 0,
E = expbuf.
size();
I !=
E; ++
I)
4217 Str.push_back(expbuf[
E-1-
I]);
4223 for (
unsigned I = 0;
I != NDigits; ++
I)
4224 Str.push_back(buffer[NDigits-1-
I]);
4233 int NWholeDigits = exp + (int) NDigits;
4236 if (NWholeDigits > 0) {
4238 Str.push_back(buffer[NDigits-
I-1]);
4241 unsigned NZeros = 1 + (
unsigned) -NWholeDigits;
4245 for (
unsigned Z = 1; Z != NZeros; ++Z)
4249 for (;
I != NDigits; ++
I)
4250 Str.push_back(buffer[NDigits-
I-1]);
4260 if (significandLSB() != semantics->
precision - 1)
4274 reciprocal.significandLSB() == reciprocal.semantics->
precision - 1);
4277 *inv =
APFloat(reciprocal, *semantics);
4360 bool WillCrossBinadeBoundary =
4361 exponent != semantics->
minExponent && isSignificandAllZeros();
4379 if (WillCrossBinadeBoundary) {
4395 bool WillCrossBinadeBoundary = !
isDenormal() && isSignificandAllOnes();
4397 if (WillCrossBinadeBoundary) {
4402 "We can not increment an exponent beyond the maxExponent allowed"
4403 " by the given floating point semantics.");
4406 incrementSignificand();
4441 exponent = exponentInf();
4448 exponent = exponentZero();
4463 if (
Arg.isInfinity())
4465 if (!
Arg.isDenormal())
4466 return Arg.exponent;
4469 int SignificandBits =
Arg.getSemantics().precision - 1;
4471 Normalized.exponent += SignificandBits;
4473 return Normalized.exponent - SignificandBits;
4477 auto MaxExp =
X.getSemantics().maxExponent;
4478 auto MinExp =
X.getSemantics().minExponent;
4486 int SignificandBits =
X.getSemantics().precision - 1;
4487 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
4490 X.exponent += std::clamp(Exp, -MaxIncrement - 1, MaxIncrement);
4513 return scalbn(Val, -Exp, RM);
4546 Floats(new
APFloat[2]{std::move(First), std::move(Second)}) {
4568 Floats[0] =
RHS.Floats[0];
4569 Floats[1] =
RHS.Floats[1];
4570 }
else if (
this != &
RHS) {
4588 Floats[0] = std::move(z);
4589 Floats[1].makeZero(
false);
4593 auto AComparedToC = a.compareAbsoluteValue(c);
4606 Floats[0] = std::move(z);
4607 Floats[1].makeZero(
false);
4616 Status |= Floats[1].subtract(z, RM);
4617 Status |= Floats[1].add(c, RM);
4618 Status |= Floats[1].add(zz, RM);
4622 Status |= Floats[1].subtract(z, RM);
4623 Status |= Floats[1].add(a, RM);
4624 Status |= Floats[1].add(zz, RM);
4642 Floats[0] = std::move(z);
4643 Floats[1].makeZero(
false);
4647 Status |= Floats[0].add(zz, RM);
4649 Floats[1].makeZero(
false);
4652 Floats[1] = std::move(z);
4653 Status |= Floats[1].subtract(Floats[0], RM);
4654 Status |= Floats[1].add(zz, RM);
4660 const DoubleAPFloat &RHS,
4680 LHS.isNegative() !=
RHS.isNegative()) {
4681 Out.makeNaN(
false, Out.isNegative(),
nullptr);
4702 return Out.addImpl(
A, AA,
C,
CC, RM);
4707 return addWithSpecial(*
this,
RHS, *
this, RM);
4720 const auto &
LHS = *
this;
4747 Out.makeNaN(
false,
false,
nullptr);
4759 "Special cases not handled exhaustively");
4766 if (!
T.isFiniteNonZero()) {
4768 Floats[1].makeZero(
false);
4794 Floats[1].makeZero(
false);
4853 Floats[0].changeSign();
4854 Floats[1].changeSign();
4859 auto Result = Floats[0].compareAbsoluteValue(
RHS.Floats[0]);
4862 Result = Floats[1].compareAbsoluteValue(
RHS.Floats[1]);
4864 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4865 auto RHSAgainst =
RHS.Floats[0].isNegative() ^
RHS.Floats[1].isNegative();
4866 if (Against && !RHSAgainst)
4868 if (!Against && RHSAgainst)
4870 if (!Against && !RHSAgainst)
4872 if (Against && RHSAgainst)
4879 return Floats[0].getCategory();
4885 Floats[0].makeInf(Neg);
4886 Floats[1].makeZero(
false);
4890 Floats[0].makeZero(Neg);
4891 Floats[1].makeZero(
false);
4904 Floats[0].makeSmallest(Neg);
4905 Floats[1].makeZero(
false);
4912 Floats[0].changeSign();
4913 Floats[1].makeZero(
false);
4917 Floats[0].makeNaN(SNaN, Neg, fill);
4918 Floats[1].makeZero(
false);
4922 auto Result = Floats[0].compare(
RHS.Floats[0]);
4925 return Floats[1].compare(
RHS.Floats[1]);
4930 return Floats[0].bitwiseIsEqual(
RHS.Floats[0]) &&
4931 Floats[1].bitwiseIsEqual(
RHS.Floats[1]);
4943 Floats[0].bitcastToAPInt().getRawData()[0],
4944 Floats[1].bitcastToAPInt().getRawData()[0],
4961 auto Ret = Tmp.
next(nextDown);
4968 unsigned int Width,
bool IsSigned,
4987 unsigned int InputSize,
4998 unsigned int InputSize,
5008 unsigned int HexDigits,
5018 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
5020 Floats[0] != Floats[0] + Floats[1]);
5050 return Floats[0].isInteger() && Floats[1].isInteger();
5054 unsigned FormatPrecision,
5055 unsigned FormatMaxPadding,
5056 bool TruncateZero)
const {
5059 .
toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
5086 Second =
scalbn(Second, -Exp, RM);
5092APFloat::Storage::Storage(IEEEFloat
F,
const fltSemantics &Semantics) {
5093 if (usesLayout<IEEEFloat>(Semantics)) {
5094 new (&IEEE) IEEEFloat(std::move(
F));
5097 if (usesLayout<DoubleAPFloat>(Semantics)) {
5100 DoubleAPFloat(Semantics,
APFloat(std::move(
F), S),