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))
236 static inline unsigned int
243 static inline unsigned int
257 unsigned int absExponent;
258 const unsigned int overlargeExponent = 24000;
262 if (
p ==
end || ((*
p ==
'-' || *
p ==
'+') && (
p + 1) ==
end)) {
266 isNegative = (*
p ==
'-');
267 if (*
p ==
'-' || *
p ==
'+') {
274 if (absExponent >= 10U)
275 return createError(
"Invalid character in exponent");
277 for (;
p !=
end; ++
p) {
282 return createError(
"Invalid character in exponent");
284 absExponent = absExponent * 10U + value;
285 if (absExponent >= overlargeExponent) {
286 absExponent = overlargeExponent;
292 return -(
int) absExponent;
294 return (
int) absExponent;
301 int exponentAdjustment) {
302 int unsignedExponent;
303 bool negative, overflow;
309 negative = *
p ==
'-';
310 if (*
p ==
'-' || *
p ==
'+') {
316 unsignedExponent = 0;
318 for (;
p !=
end; ++
p) {
323 return createError(
"Invalid character in exponent");
325 unsignedExponent = unsignedExponent * 10 + value;
326 if (unsignedExponent > 32767) {
332 if (exponentAdjustment > 32767 || exponentAdjustment < -32768)
336 exponent = unsignedExponent;
338 exponent = -exponent;
339 exponent += exponentAdjustment;
340 if (exponent > 32767 || exponent < -32768)
345 exponent = negative ? -32768: 32767;
355 while (
p !=
end && *
p ==
'0')
358 if (
p !=
end && *
p ==
'.') {
364 while (
p !=
end && *
p ==
'0')
397 return PtrOrErr.takeError();
400 D->firstSigDigit =
p;
402 D->normalizedExponent = 0;
404 for (;
p !=
end; ++
p) {
407 return createError(
"String contains multiple dots");
417 if (*
p !=
'e' && *
p !=
'E')
418 return createError(
"Invalid character in significand");
427 return ExpOrErr.takeError();
428 D->exponent = *ExpOrErr;
436 if (
p !=
D->firstSigDigit) {
442 while (
p !=
begin && *
p ==
'0');
443 while (
p !=
begin && *
p ==
'.');
448 D->normalizedExponent = (
D->exponent +
450 - (
dot >
D->firstSigDigit &&
dot <
p)));
462 unsigned int digitValue) {
463 unsigned int hexDigit;
469 else if (digitValue < 8 && digitValue > 0)
473 while (
p !=
end && (*
p ==
'0' || *
p ==
'.'))
477 return createError(
"Invalid trailing hexadecimal fraction!");
479 hexDigit = hexDigitValue(*
p);
493 unsigned int partCount,
522 return lost_fraction;
537 return moreSignificant;
548 HUerrBound(
bool inexactMultiply,
unsigned int HUerr1,
unsigned int HUerr2)
550 assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8));
552 if (HUerr1 + HUerr2 == 0)
553 return inexactMultiply * 2;
555 return inexactMultiply + 2 * (HUerr1 + HUerr2);
564 unsigned int count, partBits;
581 if (part - boundary <= boundary - part)
582 return part - boundary;
584 return boundary - part;
587 if (part == boundary) {
593 }
else if (part == boundary - 1) {
610 pow5s[0] = 78125 * 5;
612 unsigned int partsCount[16] = { 1 };
620 *p1 = firstEightPowers[power & 7];
626 for (
unsigned int n = 0; power; power >>= 1,
n++) {
633 pc = partsCount[
n - 1];
636 if (pow5[
pc - 1] == 0)
671 static const char NaNL[] =
"nan";
672 static const char NaNU[] =
"NAN";
679 const char *hexDigitChars)
687 dst[
count] = hexDigitChars[part & 0xf];
727 void IEEEFloat::initialize(
const fltSemantics *ourSemantics) {
730 semantics = ourSemantics;
736 void IEEEFloat::freeSignificand() {
738 delete [] significand.parts;
741 void IEEEFloat::assign(
const IEEEFloat &rhs) {
742 assert(semantics == rhs.semantics);
745 category = rhs.category;
746 exponent = rhs.exponent;
748 copySignificand(rhs);
751 void IEEEFloat::copySignificand(
const IEEEFloat &rhs) {
753 assert(rhs.partCount() >= partCount());
765 exponent = exponentNaN();
768 unsigned numParts = partCount();
778 unsigned bitsToPreserve = semantics->
precision - 1;
779 unsigned part = bitsToPreserve / 64;
780 bitsToPreserve %= 64;
781 significand[part] &= ((1ULL << bitsToPreserve) - 1);
782 for (part++; part != numParts; ++part)
783 significand[part] = 0;
786 unsigned QNaNBit = semantics->
precision - 2;
811 if (semantics != rhs.semantics) {
813 initialize(rhs.semantics);
824 semantics = rhs.semantics;
825 significand = rhs.significand;
826 exponent = rhs.exponent;
827 category = rhs.category;
845 significandMSB() == 0;
848 bool IEEEFloat::isSignificandAllOnes()
const {
853 for (
unsigned i = 0;
i < PartCount - 1;
i++)
858 const unsigned NumHighBits =
860 assert(NumHighBits <= integerPartWidth && NumHighBits > 0 &&
861 "Can not have more high bits to fill than integerPartWidth");
864 if (~(Parts[PartCount - 1] | HighBitFill))
870 bool IEEEFloat::isSignificandAllZeros()
const {
876 for (
unsigned i = 0;
i < PartCount - 1;
i++)
881 const unsigned NumHighBits =
884 "clear than integerPartWidth");
887 if (Parts[PartCount - 1] & HighBitMask)
897 && isSignificandAllOnes();
911 if (semantics != rhs.semantics ||
912 category != rhs.category ||
921 return std::equal(significandParts(), significandParts() + partCount(),
922 rhs.significandParts());
926 initialize(&ourSemantics);
931 significandParts()[0] = value;
936 initialize(&ourSemantics);
946 initialize(rhs.semantics);
956 unsigned int IEEEFloat::partCount()
const {
961 return const_cast<IEEEFloat *
>(
this)->significandParts();
966 return significand.parts;
968 return &significand.part;
971 void IEEEFloat::zeroSignificand() {
976 void IEEEFloat::incrementSignificand() {
990 parts = significandParts();
992 assert(semantics == rhs.semantics);
993 assert(exponent == rhs.exponent);
995 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
1001 integerPart borrow) {
1004 parts = significandParts();
1006 assert(semantics == rhs.semantics);
1007 assert(exponent == rhs.exponent);
1016 lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs,
1019 unsigned int partsCount, newPartsCount, precision;
1026 assert(semantics == rhs.semantics);
1034 if (newPartsCount > 4)
1037 fullSignificand = scratch;
1039 lhsSignificand = significandParts();
1040 partsCount = partCount();
1043 rhs.significandParts(), partsCount, partsCount);
1046 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1047 exponent += rhs.exponent;
1061 if (addend.isNonZero()) {
1065 Significand savedSignificand = significand;
1069 unsigned int extendedPrecision;
1072 extendedPrecision = 2 * precision + 1;
1073 if (omsb != extendedPrecision - 1) {
1074 assert(extendedPrecision > omsb);
1076 (extendedPrecision - 1) - omsb);
1077 exponent -= (extendedPrecision - 1) - omsb;
1081 extendedSemantics = *semantics;
1082 extendedSemantics.
precision = extendedPrecision;
1084 if (newPartsCount == 1)
1085 significand.part = fullSignificand[0];
1087 significand.parts = fullSignificand;
1088 semantics = &extendedSemantics;
1101 lost_fraction = extendedAddend.shiftSignificandRight(1);
1103 "Lost precision while shifting addend for fused-multiply-add.");
1105 lost_fraction = addOrSubtractSignificand(extendedAddend,
false);
1108 if (newPartsCount == 1)
1109 fullSignificand[0] = significand.part;
1110 significand = savedSignificand;
1111 semantics = savedSemantics;
1113 omsb =
APInt::tcMSB(fullSignificand, newPartsCount) + 1;
1120 exponent -= precision + 1;
1129 if (omsb > precision) {
1130 unsigned int bits, significantParts;
1133 bits = omsb - precision;
1142 if (newPartsCount > 4)
1143 delete [] fullSignificand;
1145 return lost_fraction;
1148 lostFraction IEEEFloat::multiplySignificand(
const IEEEFloat &rhs) {
1149 return multiplySignificand(rhs,
IEEEFloat(*semantics));
1153 lostFraction IEEEFloat::divideSignificand(
const IEEEFloat &rhs) {
1154 unsigned int bit,
i, partsCount;
1160 assert(semantics == rhs.semantics);
1162 lhsSignificand = significandParts();
1163 rhsSignificand = rhs.significandParts();
1164 partsCount = partCount();
1171 divisor = dividend + partsCount;
1174 for (
i = 0;
i < partsCount;
i++) {
1175 dividend[
i] = lhsSignificand[
i];
1176 divisor[
i] = rhsSignificand[
i];
1177 lhsSignificand[
i] = 0;
1180 exponent -= rhs.exponent;
1182 unsigned int precision = semantics->
precision;
1232 return lost_fraction;
1235 unsigned int IEEEFloat::significandMSB()
const {
1239 unsigned int IEEEFloat::significandLSB()
const {
1254 void IEEEFloat::shiftSignificandLeft(
unsigned int bits) {
1255 assert(bits < semantics->precision);
1258 unsigned int partsCount = partCount();
1271 assert(semantics == rhs.semantics);
1275 compare = exponent - rhs.exponent;
1334 bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode,
1336 unsigned int bit)
const {
1343 switch (rounding_mode) {
1381 omsb = significandMSB() + 1;
1387 exponentChange = omsb - semantics->
precision;
1391 if (exponent + exponentChange > semantics->
maxExponent)
1392 return handleOverflow(rounding_mode);
1396 if (exponent + exponentChange < semantics->minExponent)
1397 exponentChange = semantics->
minExponent - exponent;
1400 if (exponentChange < 0) {
1403 shiftSignificandLeft(-exponentChange);
1408 if (exponentChange > 0) {
1412 lf = shiftSignificandRight(exponentChange);
1417 if (omsb > (
unsigned) exponentChange)
1418 omsb -= exponentChange;
1438 if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) {
1442 incrementSignificand();
1443 omsb = significandMSB() + 1;
1446 if (omsb == (
unsigned) semantics->
precision + 1) {
1456 shiftSignificandRight(1);
1468 assert(omsb < semantics->precision);
1535 lostFraction IEEEFloat::addOrSubtractSignificand(
const IEEEFloat &rhs,
1543 subtract ^=
static_cast<bool>(sign ^ rhs.sign);
1546 bits = exponent - rhs.exponent;
1554 else if (
bits > 0) {
1555 lost_fraction = temp_rhs.shiftSignificandRight(
bits - 1);
1556 shiftSignificandLeft(1);
1558 lost_fraction = shiftSignificandRight(-
bits - 1);
1559 temp_rhs.shiftSignificandLeft(1);
1564 carry = temp_rhs.subtractSignificand
1566 copySignificand(temp_rhs);
1569 carry = subtractSignificand
1588 lost_fraction = temp_rhs.shiftSignificandRight(
bits);
1589 carry = addSignificand(temp_rhs);
1591 lost_fraction = shiftSignificandRight(-
bits);
1592 carry = addSignificand(rhs);
1600 return lost_fraction;
1777 roundingMode rounding_mode,
1781 fs = addOrSubtractSpecials(rhs,
subtract);
1787 lost_fraction = addOrSubtractSignificand(rhs,
subtract);
1788 fs = normalize(rounding_mode, lost_fraction);
1797 if (category ==
fcZero) {
1808 return addOrSubtract(rhs, rounding_mode,
false);
1814 return addOrSubtract(rhs, rounding_mode,
true);
1823 fs = multiplySpecials(rhs);
1827 fs = normalize(rounding_mode, lost_fraction);
1841 fs = divideSpecials(rhs);
1845 fs = normalize(rounding_mode, lost_fraction);
1856 unsigned int origSign = sign;
1859 fs = remainderSpecials(rhs);
1961 fs = modSpecials(rhs);
1962 unsigned int origSign = sign;
1986 sign ^= multiplicand.sign;
1995 lost_fraction = multiplySignificand(multiplicand, addend);
1996 fs = normalize(rounding_mode, lost_fraction);
2006 fs = multiplySpecials(multiplicand);
2016 fs = addOrSubtract(addend, rounding_mode,
false);
2089 MagicConstant.sign = sign;
2095 fs =
add(MagicConstant, rounding_mode);
2099 subtract(MagicConstant, rounding_mode);
2113 assert(semantics == rhs.semantics);
2145 if (sign == rhs.sign)
2160 if (sign != rhs.sign) {
2191 unsigned int newPartCount, oldPartCount;
2198 oldPartCount = partCount();
2201 bool X86SpecialNan =
false;
2204 (!(*significandParts() & 0x8000000000000000ULL) ||
2205 !(*significandParts() & 0x4000000000000000ULL))) {
2208 X86SpecialNan =
true;
2217 int exponentChange = significandMSB() + 1 - fromSemantics.
precision;
2218 if (exponent + exponentChange < toSemantics.
minExponent)
2219 exponentChange = toSemantics.
minExponent - exponent;
2220 if (exponentChange <
shift)
2221 exponentChange =
shift;
2222 if (exponentChange < 0) {
2223 shift -= exponentChange;
2224 exponent += exponentChange;
2233 if (newPartCount > oldPartCount) {
2241 significand.parts = newParts;
2242 }
else if (newPartCount == 1 && oldPartCount != 1) {
2246 newPart = significandParts()[0];
2248 significand.part = newPart;
2252 semantics = &toSemantics;
2261 *losesInfo = (fs !=
opOK);
2262 }
else if (category ==
fcNaN) {
2299 roundingMode rounding_mode,
bool *isExact)
const {
2302 unsigned int dstPartsCount, truncatedBits;
2311 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2313 if (category ==
fcZero) {
2320 src = significandParts();
2329 truncatedBits = semantics->
precision -1U - exponent;
2333 unsigned int bits = exponent + 1U;
2339 if (bits < semantics->precision) {
2355 if (truncatedBits) {
2359 roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
2379 if (omsb == width &&
2390 if (omsb >= width + !isSigned)
2412 unsigned int width,
bool isSigned,
2416 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode,
2420 unsigned int bits, dstPartsCount;
2423 assert(dstPartsCount <= parts.
size() &&
"Integer too big");
2425 if (category ==
fcNaN)
2430 bits = width - isSigned;
2433 if (sign && isSigned)
2444 const integerPart *src,
unsigned int srcCount, roundingMode rounding_mode) {
2445 unsigned int omsb, precision, dstCount;
2451 dst = significandParts();
2452 dstCount = partCount();
2457 if (precision <= omsb) {
2458 exponent = omsb - 1;
2463 exponent = precision - 1;
2468 return normalize(rounding_mode, lost_fraction);
2482 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2490 unsigned int srcCount,
bool isSigned,
2503 status = convertFromUnsignedParts(
copy, srcCount, rounding_mode);
2507 status = convertFromUnsignedParts(src, srcCount, rounding_mode);
2516 unsigned int width,
bool isSigned,
2527 return convertFromUnsignedParts(api.
getRawData(), partCount, rounding_mode);
2531 IEEEFloat::convertFromHexadecimalString(
StringRef s,
2532 roundingMode rounding_mode) {
2540 unsigned partsCount = partCount();
2542 bool computedTrailingFraction =
false;
2550 return PtrOrErr.takeError();
2559 return createError(
"String contains multiple dots");
2564 hex_value = hexDigitValue(*
p);
2565 if (hex_value == -1U)
2575 }
else if (!computedTrailingFraction) {
2578 return FractOrErr.takeError();
2579 lost_fraction = *FractOrErr;
2580 computedTrailingFraction =
true;
2586 return createError(
"Hex strings require an exponent");
2587 if (*
p !=
'p' && *
p !=
'P')
2588 return createError(
"Invalid character in significand");
2595 if (
p != firstSignificantDigit) {
2604 expAdjustment =
static_cast<int>(
dot - firstSignificantDigit);
2605 if (expAdjustment < 0)
2607 expAdjustment = expAdjustment * 4 - 1;
2617 return ExpOrErr.takeError();
2618 exponent = *ExpOrErr;
2621 return normalize(rounding_mode, lost_fraction);
2625 IEEEFloat::roundSignificandWithExponent(
const integerPart *decSigParts,
2626 unsigned sigPartCount,
int exp,
2627 roundingMode rounding_mode) {
2628 unsigned int parts, pow5PartCount;
2639 pow5PartCount =
powerOf5(pow5Parts, exp >= 0 ? exp: -exp);
2641 for (;; parts *= 2) {
2643 unsigned int excessPrecision, truncatedBits;
2647 truncatedBits = excessPrecision;
2650 decSig.makeZero(sign);
2653 sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount,
2655 powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount,
2658 decSig.exponent += exp;
2662 unsigned int powHUerr;
2666 calcLostFraction = decSig.multiplySignificand(pow5);
2667 powHUerr = powStatus !=
opOK;
2669 calcLostFraction = decSig.divideSignificand(pow5);
2672 excessPrecision += (semantics->
minExponent - decSig.exponent);
2673 truncatedBits = excessPrecision;
2674 if (excessPrecision > calcSemantics.
precision)
2675 excessPrecision = calcSemantics.
precision;
2684 (decSig.significandParts(), calcSemantics.
precision - 1) == 1);
2689 excessPrecision, isNearest);
2692 if (HUdistance >= HUerr) {
2693 APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(),
2694 calcSemantics.
precision - excessPrecision,
2699 exponent = (decSig.exponent + semantics->
precision
2700 - (calcSemantics.
precision - excessPrecision));
2704 return normalize(rounding_mode, calcLostFraction);
2710 IEEEFloat::convertFromDecimalString(
StringRef str, roundingMode rounding_mode) {
2749 }
else if (
D.normalizedExponent - 1 > INT_MAX / 42039) {
2750 fs = handleOverflow(rounding_mode);
2756 }
else if (
D.normalizedExponent - 1 < INT_MIN / 42039 ||
2757 (
D.normalizedExponent + 1) * 28738 <=
2765 }
else if ((
D.normalizedExponent - 1) * 42039
2768 fs = handleOverflow(rounding_mode);
2771 unsigned int partCount;
2777 partCount =
static_cast<unsigned int>(
D.lastSigDigit -
D.firstSigDigit) + 1;
2795 if (
p == str.
end()) {
2800 if (decValue >= 10U) {
2801 delete[] decSignificand;
2802 return createError(
"Invalid character in significand");
2805 val =
val * 10 + decValue;
2808 }
while (
p <=
D.lastSigDigit && multiplier <= (~ (
integerPart) 0 - 9) / 10);
2812 partCount, partCount + 1,
false);
2816 if (decSignificand[partCount])
2818 }
while (
p <=
D.lastSigDigit);
2821 fs = roundSignificandWithExponent(decSignificand, partCount,
2822 D.exponent, rounding_mode);
2824 delete [] decSignificand;
2830 bool IEEEFloat::convertFromStringSpecials(
StringRef str) {
2831 const size_t MIN_NAME_SIZE = 3;
2833 if (str.
size() < MIN_NAME_SIZE)
2841 bool IsNegative = str.
front() ==
'-';
2844 if (str.
size() < MIN_NAME_SIZE)
2854 bool IsSignaling = str.
front() ==
's' || str.
front() ==
'S';
2857 if (str.
size() < MIN_NAME_SIZE)
2866 makeNaN(IsSignaling, IsNegative);
2871 if (str.
front() ==
'(') {
2873 if (str.
size() <= 2 || str.
back() !=
')')
2880 unsigned Radix = 10;
2881 if (str[0] ==
'0') {
2882 if (str.
size() > 1 && tolower(str[1]) ==
'x') {
2892 makeNaN(IsSignaling, IsNegative, &Payload);
2906 if (convertFromStringSpecials(str))
2911 size_t slen = str.
size();
2912 sign = *
p ==
'-' ? 1 : 0;
2913 if (*
p ==
'-' || *
p ==
'+') {
2920 if (slen >= 2 &&
p[0] ==
'0' && (
p[1] ==
'x' ||
p[1] ==
'X')) {
2923 return convertFromHexadecimalString(
StringRef(
p + 2, slen - 2),
2927 return convertFromDecimalString(
StringRef(
p, slen), rounding_mode);
2971 dst +=
sizeof NaNU - 1;
2976 *dst++ = upperCase ?
'X':
'x';
2978 if (hexDigits > 1) {
2980 memset (dst,
'0', hexDigits - 1);
2981 dst += hexDigits - 1;
2983 *dst++ = upperCase ?
'P':
'p';
2988 dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode);
2994 return static_cast<unsigned int>(dst -
p);
3001 char *IEEEFloat::convertNormalToHexString(
char *dst,
unsigned int hexDigits,
3003 roundingMode rounding_mode)
const {
3004 unsigned int count, valueBits,
shift, partsCount, outputDigits;
3005 const char *hexDigitChars;
3011 *dst++ = upperCase ?
'X':
'x';
3016 significand = significandParts();
3017 partsCount = partCount();
3026 outputDigits = (valueBits - significandLSB () + 3) / 4;
3032 if (hexDigits < outputDigits) {
3038 bits = valueBits - hexDigits * 4;
3040 roundUp = roundAwayFromZero(rounding_mode, fraction,
bits);
3042 outputDigits = hexDigits;
3052 while (outputDigits &&
count) {
3056 if (--
count == partsCount)
3067 if (curDigits > outputDigits)
3068 curDigits = outputDigits;
3069 dst +=
partAsHex (dst, part, curDigits, hexDigitChars);
3070 outputDigits -= curDigits;
3079 *q = hexDigitChars[hexDigitValue (*q) + 1];
3080 }
while (*q ==
'0');
3084 memset (dst,
'0', outputDigits);
3085 dst += outputDigits;
3098 *dst++ = upperCase ?
'P':
'p';
3104 if (!
Arg.isFiniteNonZero())
3107 Arg.isNaN() ? (uint8_t)0 : (uint8_t)
Arg.sign,
3108 Arg.semantics->precision);
3112 Arg.semantics->precision,
Arg.exponent,
3114 Arg.significandParts(),
3115 Arg.significandParts() +
Arg.partCount()));
3127 APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt()
const {
3131 uint64_t myexponent, mysignificand;
3134 myexponent = exponent+16383;
3135 mysignificand = significandParts()[0];
3136 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
3138 }
else if (category==
fcZero) {
3142 myexponent = 0x7fff;
3143 mysignificand = 0x8000000000000000ULL;
3146 myexponent = 0x7fff;
3147 mysignificand = significandParts()[0];
3151 words[0] = mysignificand;
3152 words[1] = ((
uint64_t)(sign & 1) << 15) |
3153 (myexponent & 0x7fffLL);
3154 return APInt(80, words);
3157 APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt()
const {
3182 words[0] = *u.convertDoubleAPFloatToAPInt().getRawData();
3188 if (u.isFiniteNonZero() && losesInfo) {
3198 words[1] = *v.convertDoubleAPFloatToAPInt().getRawData();
3203 return APInt(128, words);
3206 APInt IEEEFloat::convertQuadrupleAPFloatToAPInt()
const {
3210 uint64_t myexponent, mysignificand, mysignificand2;
3213 myexponent = exponent+16383;
3214 mysignificand = significandParts()[0];
3215 mysignificand2 = significandParts()[1];
3216 if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL))
3218 }
else if (category==
fcZero) {
3220 mysignificand = mysignificand2 = 0;
3222 myexponent = 0x7fff;
3223 mysignificand = mysignificand2 = 0;
3226 myexponent = 0x7fff;
3227 mysignificand = significandParts()[0];
3228 mysignificand2 = significandParts()[1];
3232 words[0] = mysignificand;
3233 words[1] = ((
uint64_t)(sign & 1) << 63) |
3234 ((myexponent & 0x7fff) << 48) |
3235 (mysignificand2 & 0xffffffffffffLL);
3237 return APInt(128, words);
3240 APInt IEEEFloat::convertDoubleAPFloatToAPInt()
const {
3244 uint64_t myexponent, mysignificand;
3247 myexponent = exponent+1023;
3248 mysignificand = *significandParts();
3249 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
3251 }
else if (category==
fcZero) {
3260 mysignificand = *significandParts();
3264 ((myexponent & 0x7ff) << 52) |
3265 (mysignificand & 0xfffffffffffffLL))));
3268 APInt IEEEFloat::convertFloatAPFloatToAPInt()
const {
3272 uint32_t myexponent, mysignificand;
3275 myexponent = exponent+127;
3276 mysignificand = (
uint32_t)*significandParts();
3277 if (myexponent == 1 && !(mysignificand & 0x800000))
3279 }
else if (category==
fcZero) {
3288 mysignificand = (
uint32_t)*significandParts();
3291 return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
3292 (mysignificand & 0x7fffff)));
3295 APInt IEEEFloat::convertBFloatAPFloatToAPInt()
const {
3297 assert(partCount() == 1);
3299 uint32_t myexponent, mysignificand;
3302 myexponent = exponent + 127;
3303 mysignificand = (
uint32_t)*significandParts();
3304 if (myexponent == 1 && !(mysignificand & 0x80))
3306 }
else if (category ==
fcZero) {
3315 mysignificand = (
uint32_t)*significandParts();
3318 return APInt(16, (((sign & 1) << 15) | ((myexponent & 0xff) << 7) |
3319 (mysignificand & 0x7f)));
3322 APInt IEEEFloat::convertHalfAPFloatToAPInt()
const {
3326 uint32_t myexponent, mysignificand;
3329 myexponent = exponent+15;
3330 mysignificand = (
uint32_t)*significandParts();
3331 if (myexponent == 1 && !(mysignificand & 0x400))
3333 }
else if (category==
fcZero) {
3342 mysignificand = (
uint32_t)*significandParts();
3345 return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) |
3346 (mysignificand & 0x3ff)));
3355 return convertHalfAPFloatToAPInt();
3358 return convertBFloatAPFloatToAPInt();
3361 return convertFloatAPFloatToAPInt();
3364 return convertDoubleAPFloatToAPInt();
3367 return convertQuadrupleAPFloatToAPInt();
3370 return convertPPCDoubleDoubleAPFloatToAPInt();
3374 return convertF80LongDoubleAPFloatToAPInt();
3379 "Float semantics are not IEEEsingle");
3386 "Float semantics are not IEEEdouble");
3398 void IEEEFloat::initFromF80LongDoubleAPInt(
const APInt &api) {
3401 uint64_t myexponent = (i2 & 0x7fff);
3403 uint8_t myintegerbit = mysignificand >> 63;
3408 sign =
static_cast<unsigned int>(i2>>15);
3409 if (myexponent == 0 && mysignificand == 0) {
3411 }
else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
3413 }
else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) ||
3414 (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) {
3416 exponent = exponentNaN();
3417 significandParts()[0] = mysignificand;
3418 significandParts()[1] = 0;
3421 exponent = myexponent - 16383;
3422 significandParts()[0] = mysignificand;
3423 significandParts()[1] = 0;
3429 void IEEEFloat::initFromPPCDoubleDoubleAPInt(
const APInt &api) {
3436 initFromDoubleAPInt(
APInt(64,
i1));
3452 void IEEEFloat::initFromQuadrupleAPInt(
const APInt &api) {
3455 uint64_t myexponent = (i2 >> 48) & 0x7fff;
3457 uint64_t mysignificand2 = i2 & 0xffffffffffffLL;
3462 sign =
static_cast<unsigned int>(i2>>63);
3463 if (myexponent==0 &&
3464 (mysignificand==0 && mysignificand2==0)) {
3466 }
else if (myexponent==0x7fff &&
3467 (mysignificand==0 && mysignificand2==0)) {
3469 }
else if (myexponent==0x7fff &&
3470 (mysignificand!=0 || mysignificand2 !=0)) {
3472 exponent = exponentNaN();
3473 significandParts()[0] = mysignificand;
3474 significandParts()[1] = mysignificand2;
3477 exponent = myexponent - 16383;
3478 significandParts()[0] = mysignificand;
3479 significandParts()[1] = mysignificand2;
3483 significandParts()[1] |= 0x1000000000000LL;
3487 void IEEEFloat::initFromDoubleAPInt(
const APInt &api) {
3489 uint64_t myexponent = (
i >> 52) & 0x7ff;
3490 uint64_t mysignificand =
i & 0xfffffffffffffLL;
3495 sign =
static_cast<unsigned int>(
i>>63);
3496 if (myexponent==0 && mysignificand==0) {
3498 }
else if (myexponent==0x7ff && mysignificand==0) {
3500 }
else if (myexponent==0x7ff && mysignificand!=0) {
3502 exponent = exponentNaN();
3503 *significandParts() = mysignificand;
3506 exponent = myexponent - 1023;
3507 *significandParts() = mysignificand;
3511 *significandParts() |= 0x10000000000000LL;
3515 void IEEEFloat::initFromFloatAPInt(
const APInt &api) {
3524 if (myexponent==0 && mysignificand==0) {
3526 }
else if (myexponent==0xff && mysignificand==0) {
3528 }
else if (myexponent==0xff && mysignificand!=0) {
3530 exponent = exponentNaN();
3531 *significandParts() = mysignificand;
3534 exponent = myexponent - 127;
3535 *significandParts() = mysignificand;
3539 *significandParts() |= 0x800000;
3543 void IEEEFloat::initFromBFloatAPInt(
const APInt &api) {
3549 assert(partCount() == 1);
3552 if (myexponent == 0 && mysignificand == 0) {
3554 }
else if (myexponent == 0xff && mysignificand == 0) {
3556 }
else if (myexponent == 0xff && mysignificand != 0) {
3558 exponent = exponentNaN();
3559 *significandParts() = mysignificand;
3562 exponent = myexponent - 127;
3563 *significandParts() = mysignificand;
3564 if (myexponent == 0)
3567 *significandParts() |= 0x80;
3571 void IEEEFloat::initFromHalfAPInt(
const APInt &api) {
3580 if (myexponent==0 && mysignificand==0) {
3582 }
else if (myexponent==0x1f && mysignificand==0) {
3584 }
else if (myexponent==0x1f && mysignificand!=0) {
3586 exponent = exponentNaN();
3587 *significandParts() = mysignificand;
3590 exponent = myexponent - 15;
3591 *significandParts() = mysignificand;
3595 *significandParts() |= 0x400;
3606 return initFromHalfAPInt(api);
3608 return initFromBFloatAPInt(api);
3610 return initFromFloatAPInt(api);
3612 return initFromDoubleAPInt(api);
3614 return initFromF80LongDoubleAPInt(api);
3616 return initFromQuadrupleAPInt(api);
3618 return initFromPPCDoubleDoubleAPInt(api);
3636 unsigned PartCount = partCount();
3637 memset(significand, 0xFF,
sizeof(
integerPart)*(PartCount - 1));
3641 const unsigned NumUnusedHighBits =
3676 initFromAPInt(&Sem, API);
3689 Buffer.
append(Str.begin(), Str.end());
3694 void AdjustToPrecision(
APInt &significand,
3695 int &exp,
unsigned FormatPrecision) {
3699 unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59;
3704 if (!tensRemovable)
return;
3706 exp += tensRemovable;
3711 if (tensRemovable & 1)
3713 tensRemovable >>= 1;
3714 if (!tensRemovable)
break;
3718 significand = significand.
udiv(divisor);
3726 int &exp,
unsigned FormatPrecision) {
3727 unsigned N = buffer.size();
3728 if (
N <= FormatPrecision)
return;
3731 unsigned FirstSignificant =
N - FormatPrecision;
3738 if (buffer[FirstSignificant - 1] <
'5') {
3739 while (FirstSignificant <
N && buffer[FirstSignificant] ==
'0')
3742 exp += FirstSignificant;
3743 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3749 for (
unsigned I = FirstSignificant;
I !=
N; ++
I) {
3750 if (buffer[
I] ==
'9') {
3759 if (FirstSignificant ==
N) {
3760 exp += FirstSignificant;
3762 buffer.push_back(
'1');
3766 exp += FirstSignificant;
3767 buffer.
erase(&buffer[0], &buffer[FirstSignificant]);
3772 unsigned FormatMaxPadding,
bool TruncateZero)
const {
3776 return append(Str,
"-Inf");
3778 return append(Str,
"+Inf");
3786 if (!FormatMaxPadding) {
3791 if (FormatPrecision > 1)
3792 Str.append(FormatPrecision - 1,
'0');
3814 if (!FormatPrecision) {
3822 FormatPrecision = 2 + semantics->
precision * 59 / 196;
3827 exp += trailingZeros;
3833 }
else if (exp > 0) {
3836 significand <<= exp;
3850 unsigned precision = semantics->
precision + (137 * texp + 136) / 59;
3854 significand = significand.
zext(precision);
3855 APInt five_to_the_i(precision, 5);
3857 if (texp & 1) significand *= five_to_the_i;
3861 five_to_the_i *= five_to_the_i;
3865 AdjustToPrecision(significand, exp, FormatPrecision);
3871 APInt ten(precision, 10);
3872 APInt digit(precision, 0);
3874 bool inTrail =
true;
3875 while (significand != 0) {
3883 if (inTrail && !
d) exp++;
3885 buffer.push_back((
char) (
'0' +
d));
3890 assert(!buffer.empty() &&
"no characters in buffer!");
3894 AdjustToPrecision(buffer, exp, FormatPrecision);
3896 unsigned NDigits = buffer.size();
3899 bool FormatScientific;
3900 if (!FormatMaxPadding)
3901 FormatScientific =
true;
3907 FormatScientific = ((unsigned) exp > FormatMaxPadding ||
3908 NDigits + (
unsigned) exp > FormatPrecision);
3911 int MSD = exp + (
int) (NDigits - 1);
3914 FormatScientific =
false;
3918 FormatScientific = ((unsigned) -MSD) > FormatMaxPadding;
3924 if (FormatScientific) {
3925 exp += (NDigits - 1);
3927 Str.push_back(buffer[NDigits-1]);
3929 if (NDigits == 1 && TruncateZero)
3932 for (
unsigned I = 1;
I != NDigits; ++
I)
3933 Str.push_back(buffer[NDigits-1-
I]);
3935 if (!TruncateZero && FormatPrecision > NDigits - 1)
3936 Str.append(FormatPrecision - NDigits + 1,
'0');
3938 Str.push_back(TruncateZero ?
'E' :
'e');
3940 Str.push_back(exp >= 0 ?
'+' :
'-');
3941 if (exp < 0) exp = -exp;
3944 expbuf.push_back((
char) (
'0' + (exp % 10)));
3948 if (!TruncateZero && expbuf.size() < 2)
3949 expbuf.push_back(
'0');
3950 for (
unsigned I = 0,
E = expbuf.size();
I !=
E; ++
I)
3951 Str.push_back(expbuf[
E-1-
I]);
3957 for (
unsigned I = 0;
I != NDigits; ++
I)
3958 Str.push_back(buffer[NDigits-1-
I]);
3959 for (
unsigned I = 0;
I != (unsigned) exp; ++
I)
3967 int NWholeDigits = exp + (
int) NDigits;
3970 if (NWholeDigits > 0) {
3971 for (;
I != (unsigned) NWholeDigits; ++
I)
3972 Str.push_back(buffer[NDigits-
I-1]);
3975 unsigned NZeros = 1 + (unsigned) -NWholeDigits;
3979 for (
unsigned Z = 1; Z != NZeros; ++Z)
3983 for (;
I != NDigits; ++
I)
3984 Str.push_back(buffer[NDigits-
I-1]);
3994 if (significandLSB() != semantics->
precision - 1)
4008 reciprocal.significandLSB() == reciprocal.semantics->
precision - 1);
4011 *inv =
APFloat(reciprocal, *semantics);
4086 bool WillCrossBinadeBoundary =
4087 exponent != semantics->
minExponent && isSignificandAllZeros();
4105 if (WillCrossBinadeBoundary) {
4121 bool WillCrossBinadeBoundary = !
isDenormal() && isSignificandAllOnes();
4123 if (WillCrossBinadeBoundary) {
4128 "We can not increment an exponent beyond the maxExponent allowed"
4129 " by the given floating point semantics.");
4132 incrementSignificand();
4160 exponent = exponentInf();
4167 exponent = exponentZero();
4181 if (
Arg.isInfinity())
4183 if (!
Arg.isDenormal())
4184 return Arg.exponent;
4187 int SignificandBits =
Arg.getSemantics().precision - 1;
4189 Normalized.exponent += SignificandBits;
4191 return Normalized.exponent - SignificandBits;
4195 auto MaxExp =
X.getSemantics().maxExponent;
4196 auto MinExp =
X.getSemantics().minExponent;
4204 int SignificandBits =
X.getSemantics().precision - 1;
4205 int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1;
4286 Floats[0] =
RHS.Floats[0];
4287 Floats[1] =
RHS.Floats[1];
4288 }
else if (
this != &
RHS) {
4304 if (!
z.isFinite()) {
4305 if (!
z.isInfinity()) {
4307 Floats[1].makeZero(
false);
4311 auto AComparedToC =
a.compareAbsoluteValue(
c);
4323 if (!
z.isFinite()) {
4325 Floats[1].makeZero(
false);
4361 Floats[1].makeZero(
false);
4367 Floats[1].makeZero(
false);
4371 Status |= Floats[1].subtract(Floats[0],
RM);
4378 const DoubleAPFloat &
RHS,
4398 LHS.isNegative() !=
RHS.isNegative()) {
4399 Out.makeNaN(
false, Out.isNegative(),
nullptr);
4420 return Out.addImpl(A,
AA,
C, CC,
RM);
4425 return addWithSpecial(*
this,
RHS, *
this,
RM);
4438 const auto &
LHS = *
this;
4465 Out.makeNaN(
false,
false,
nullptr);
4477 "Special cases not handled exhaustively");
4480 APFloat A = Floats[0],
B = Floats[1],
C =
RHS.Floats[0],
D =
RHS.Floats[1];
4484 if (!
T.isFiniteNonZero()) {
4486 Floats[1].makeZero(
false);
4512 Floats[1].makeZero(
false);
4571 Floats[0].changeSign();
4572 Floats[1].changeSign();
4577 auto Result = Floats[0].compareAbsoluteValue(
RHS.Floats[0]);
4580 Result = Floats[1].compareAbsoluteValue(
RHS.Floats[1]);
4582 auto Against = Floats[0].isNegative() ^ Floats[1].isNegative();
4583 auto RHSAgainst =
RHS.Floats[0].isNegative() ^
RHS.Floats[1].isNegative();
4584 if (Against && !RHSAgainst)
4586 if (!Against && RHSAgainst)
4588 if (!Against && !RHSAgainst)
4590 if (Against && RHSAgainst)
4597 return Floats[0].getCategory();
4603 Floats[0].makeInf(Neg);
4604 Floats[1].makeZero(
false);
4608 Floats[0].makeZero(Neg);
4609 Floats[1].makeZero(
false);
4622 Floats[0].makeSmallest(Neg);
4623 Floats[1].makeZero(
false);
4630 Floats[0].changeSign();
4631 Floats[1].makeZero(
false);
4635 Floats[0].makeNaN(SNaN, Neg, fill);
4636 Floats[1].makeZero(
false);
4640 auto Result = Floats[0].compare(
RHS.Floats[0]);
4643 return Floats[1].compare(
RHS.Floats[1]);
4648 return Floats[0].bitwiseIsEqual(
RHS.Floats[0]) &&
4649 Floats[1].bitwiseIsEqual(
RHS.Floats[1]);
4661 Floats[0].bitcastToAPInt().getRawData()[0],
4662 Floats[1].bitcastToAPInt().getRawData()[0],
4679 auto Ret = Tmp.
next(nextDown);
4686 unsigned int Width,
bool IsSigned,
4690 .convertToInteger(Input,
Width, IsSigned,
RM, IsExact);
4705 unsigned int InputSize,
4716 unsigned int InputSize,
4726 unsigned int HexDigits,
4731 .convertToHexString(DST, HexDigits, UpperCase,
RM);
4736 (Floats[0].isDenormal() || Floats[1].isDenormal() ||
4738 Floats[0] != Floats[0] + Floats[1]);
4759 return Floats[0].isInteger() && Floats[1].isInteger();
4763 unsigned FormatPrecision,
4764 unsigned FormatMaxPadding,
4765 bool TruncateZero)
const {
4768 .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero);
4801 APFloat::Storage::Storage(IEEEFloat
F,
const fltSemantics &Semantics) {
4802 if (usesLayout<IEEEFloat>(Semantics)) {
4806 if (usesLayout<DoubleAPFloat>(Semantics)) {
4822 if (APFloat::usesLayout<detail::IEEEFloat>(
Arg.getSemantics()))
4824 if (APFloat::usesLayout<detail::DoubleAPFloat>(
Arg.getSemantics()))
4832 assert(StatusOrErr &&
"Invalid floating point representation");
4843 usesLayout<IEEEFloat>(ToSemantics))
4844 return U.IEEE.convert(ToSemantics,
RM, losesInfo);
4846 usesLayout<DoubleAPFloat>(ToSemantics)) {
4849 *
this =
APFloat(ToSemantics, U.IEEE.bitcastToAPInt());
4853 usesLayout<IEEEFloat>(ToSemantics)) {
4854 auto Ret = getIEEE().
convert(ToSemantics,
RM, losesInfo);
4868 OS << Buffer <<
"\n";
4871 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4885 bool *isExact)
const {
4889 rounding_mode, isExact);
4899 "Float semantics is not representable by IEEEdouble");
4912 "Float semantics is not representable by IEEEsingle");
4923 #undef APFLOAT_DISPATCH_ON_SEMANTICS