1 //===-- APFloat.cpp - Implement APFloat class -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Neil Booth and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a class to represent arbitrary precision floating
11 // point values and provide a variety of arithmetic operations on them.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/Support/MathExtras.h"
21 #define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
23 /* Assumed in hexadecimal significand parsing. */
24 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
28 /* Represents floating point arithmetic semantics. */
30 /* The largest E such that 2^E is representable; this matches the
31 definition of IEEE 754. */
32 exponent_t maxExponent;
34 /* The smallest E such that 2^E is a normalized number; this
35 matches the definition of IEEE 754. */
36 exponent_t minExponent;
38 /* Number of bits in the significand. This includes the integer
40 unsigned char precision;
42 /* If the target format has an implicit integer bit. */
43 bool implicitIntegerBit;
46 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
47 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
48 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
49 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, false };
50 const fltSemantics APFloat::Bogus = { 0, 0, 0, false };
53 /* Put a bunch of private, handy routines in an anonymous namespace. */
57 partCountForBits(unsigned int bits)
59 return ((bits) + integerPartWidth - 1) / integerPartWidth;
63 digitValue(unsigned int c)
75 hexDigitValue (unsigned int c)
94 /* This is ugly and needs cleaning up, but I don't immediately see
95 how whilst remaining safe. */
97 totalExponent(const char *p, int exponentAdjustment)
99 integerPart unsignedExponent;
100 bool negative, overflow;
103 /* Move past the exponent letter and sign to the digits. */
105 negative = *p == '-';
106 if(*p == '-' || *p == '+')
109 unsignedExponent = 0;
114 value = digitValue(*p);
119 unsignedExponent = unsignedExponent * 10 + value;
120 if(unsignedExponent > 65535)
124 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
128 exponent = unsignedExponent;
130 exponent = -exponent;
131 exponent += exponentAdjustment;
132 if(exponent > 65535 || exponent < -65536)
137 exponent = negative ? -65536: 65535;
143 skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
158 /* Return the trailing fraction of a hexadecimal number.
159 DIGITVALUE is the first hex digit of the fraction, P points to
162 trailingHexadecimalFraction(const char *p, unsigned int digitValue)
164 unsigned int hexDigit;
166 /* If the first trailing digit isn't 0 or 8 we can work out the
167 fraction immediately. */
169 return lfMoreThanHalf;
170 else if(digitValue < 8 && digitValue > 0)
171 return lfLessThanHalf;
173 /* Otherwise we need to find the first non-zero digit. */
177 hexDigit = hexDigitValue(*p);
179 /* If we ran off the end it is exactly zero or one-half, otherwise
182 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
184 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
187 /* Return the fraction lost were a bignum truncated. */
189 lostFractionThroughTruncation(integerPart *parts,
190 unsigned int partCount,
195 lsb = APInt::tcLSB(parts, partCount);
197 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
199 return lfExactlyZero;
201 return lfExactlyHalf;
202 if(bits <= partCount * integerPartWidth
203 && APInt::tcExtractBit(parts, bits - 1))
204 return lfMoreThanHalf;
206 return lfLessThanHalf;
209 /* Shift DST right BITS bits noting lost fraction. */
211 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
213 lostFraction lost_fraction;
215 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
217 APInt::tcShiftRight(dst, parts, bits);
219 return lost_fraction;
225 APFloat::initialize(const fltSemantics *ourSemantics)
229 semantics = ourSemantics;
232 significand.parts = new integerPart[count];
236 APFloat::freeSignificand()
239 delete [] significand.parts;
243 APFloat::assign(const APFloat &rhs)
245 assert(semantics == rhs.semantics);
248 category = rhs.category;
249 exponent = rhs.exponent;
250 if(category == fcNormal || category == fcNaN)
251 copySignificand(rhs);
255 APFloat::copySignificand(const APFloat &rhs)
257 assert(category == fcNormal || category == fcNaN);
258 assert(rhs.partCount() >= partCount());
260 APInt::tcAssign(significandParts(), rhs.significandParts(),
265 APFloat::operator=(const APFloat &rhs)
268 if(semantics != rhs.semantics) {
270 initialize(rhs.semantics);
279 APFloat::bitwiseIsEqual(const APFloat &rhs) const {
282 if (semantics != rhs.semantics ||
283 category != rhs.category ||
286 if (category==fcZero || category==fcInfinity)
288 else if (category==fcNormal && exponent!=rhs.exponent)
292 const integerPart* p=significandParts();
293 const integerPart* q=rhs.significandParts();
294 for (; i>0; i--, p++, q++) {
302 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
304 initialize(&ourSemantics);
307 exponent = ourSemantics.precision - 1;
308 significandParts()[0] = value;
309 normalize(rmNearestTiesToEven, lfExactlyZero);
312 APFloat::APFloat(const fltSemantics &ourSemantics,
313 fltCategory ourCategory, bool negative)
315 initialize(&ourSemantics);
316 category = ourCategory;
318 if(category == fcNormal)
322 APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
324 initialize(&ourSemantics);
325 convertFromString(text, rmNearestTiesToEven);
328 APFloat::APFloat(const APFloat &rhs)
330 initialize(rhs.semantics);
340 APFloat::partCount() const
342 return partCountForBits(semantics->precision + 1);
346 APFloat::semanticsPrecision(const fltSemantics &semantics)
348 return semantics.precision;
352 APFloat::significandParts() const
354 return const_cast<APFloat *>(this)->significandParts();
358 APFloat::significandParts()
360 assert(category == fcNormal || category == fcNaN);
363 return significand.parts;
365 return &significand.part;
368 /* Combine the effect of two lost fractions. */
370 APFloat::combineLostFractions(lostFraction moreSignificant,
371 lostFraction lessSignificant)
373 if(lessSignificant != lfExactlyZero) {
374 if(moreSignificant == lfExactlyZero)
375 moreSignificant = lfLessThanHalf;
376 else if(moreSignificant == lfExactlyHalf)
377 moreSignificant = lfMoreThanHalf;
380 return moreSignificant;
384 APFloat::zeroSignificand()
387 APInt::tcSet(significandParts(), 0, partCount());
390 /* Increment an fcNormal floating point number's significand. */
392 APFloat::incrementSignificand()
396 carry = APInt::tcIncrement(significandParts(), partCount());
398 /* Our callers should never cause us to overflow. */
402 /* Add the significand of the RHS. Returns the carry flag. */
404 APFloat::addSignificand(const APFloat &rhs)
408 parts = significandParts();
410 assert(semantics == rhs.semantics);
411 assert(exponent == rhs.exponent);
413 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
416 /* Subtract the significand of the RHS with a borrow flag. Returns
419 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
423 parts = significandParts();
425 assert(semantics == rhs.semantics);
426 assert(exponent == rhs.exponent);
428 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
432 /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
433 on to the full-precision result of the multiplication. Returns the
436 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
438 unsigned int omsb; // One, not zero, based MSB.
439 unsigned int partsCount, newPartsCount, precision;
440 integerPart *lhsSignificand;
441 integerPart scratch[4];
442 integerPart *fullSignificand;
443 lostFraction lost_fraction;
445 assert(semantics == rhs.semantics);
447 precision = semantics->precision;
448 newPartsCount = partCountForBits(precision * 2);
450 if(newPartsCount > 4)
451 fullSignificand = new integerPart[newPartsCount];
453 fullSignificand = scratch;
455 lhsSignificand = significandParts();
456 partsCount = partCount();
458 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
459 rhs.significandParts(), partsCount);
461 lost_fraction = lfExactlyZero;
462 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
463 exponent += rhs.exponent;
466 Significand savedSignificand = significand;
467 const fltSemantics *savedSemantics = semantics;
468 fltSemantics extendedSemantics;
470 unsigned int extendedPrecision;
472 /* Normalize our MSB. */
473 extendedPrecision = precision + precision - 1;
474 if(omsb != extendedPrecision)
476 APInt::tcShiftLeft(fullSignificand, newPartsCount,
477 extendedPrecision - omsb);
478 exponent -= extendedPrecision - omsb;
481 /* Create new semantics. */
482 extendedSemantics = *semantics;
483 extendedSemantics.precision = extendedPrecision;
485 if(newPartsCount == 1)
486 significand.part = fullSignificand[0];
488 significand.parts = fullSignificand;
489 semantics = &extendedSemantics;
491 APFloat extendedAddend(*addend);
492 status = extendedAddend.convert(extendedSemantics, rmTowardZero);
493 assert(status == opOK);
494 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
496 /* Restore our state. */
497 if(newPartsCount == 1)
498 fullSignificand[0] = significand.part;
499 significand = savedSignificand;
500 semantics = savedSemantics;
502 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
505 exponent -= (precision - 1);
507 if(omsb > precision) {
508 unsigned int bits, significantParts;
511 bits = omsb - precision;
512 significantParts = partCountForBits(omsb);
513 lf = shiftRight(fullSignificand, significantParts, bits);
514 lost_fraction = combineLostFractions(lf, lost_fraction);
518 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
520 if(newPartsCount > 4)
521 delete [] fullSignificand;
523 return lost_fraction;
526 /* Multiply the significands of LHS and RHS to DST. */
528 APFloat::divideSignificand(const APFloat &rhs)
530 unsigned int bit, i, partsCount;
531 const integerPart *rhsSignificand;
532 integerPart *lhsSignificand, *dividend, *divisor;
533 integerPart scratch[4];
534 lostFraction lost_fraction;
536 assert(semantics == rhs.semantics);
538 lhsSignificand = significandParts();
539 rhsSignificand = rhs.significandParts();
540 partsCount = partCount();
543 dividend = new integerPart[partsCount * 2];
547 divisor = dividend + partsCount;
549 /* Copy the dividend and divisor as they will be modified in-place. */
550 for(i = 0; i < partsCount; i++) {
551 dividend[i] = lhsSignificand[i];
552 divisor[i] = rhsSignificand[i];
553 lhsSignificand[i] = 0;
556 exponent -= rhs.exponent;
558 unsigned int precision = semantics->precision;
560 /* Normalize the divisor. */
561 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
564 APInt::tcShiftLeft(divisor, partsCount, bit);
567 /* Normalize the dividend. */
568 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
571 APInt::tcShiftLeft(dividend, partsCount, bit);
574 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
576 APInt::tcShiftLeft(dividend, partsCount, 1);
577 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
581 for(bit = precision; bit; bit -= 1) {
582 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
583 APInt::tcSubtract(dividend, divisor, 0, partsCount);
584 APInt::tcSetBit(lhsSignificand, bit - 1);
587 APInt::tcShiftLeft(dividend, partsCount, 1);
590 /* Figure out the lost fraction. */
591 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
594 lost_fraction = lfMoreThanHalf;
596 lost_fraction = lfExactlyHalf;
597 else if(APInt::tcIsZero(dividend, partsCount))
598 lost_fraction = lfExactlyZero;
600 lost_fraction = lfLessThanHalf;
605 return lost_fraction;
609 APFloat::significandMSB() const
611 return APInt::tcMSB(significandParts(), partCount());
615 APFloat::significandLSB() const
617 return APInt::tcLSB(significandParts(), partCount());
620 /* Note that a zero result is NOT normalized to fcZero. */
622 APFloat::shiftSignificandRight(unsigned int bits)
624 /* Our exponent should not overflow. */
625 assert((exponent_t) (exponent + bits) >= exponent);
629 return shiftRight(significandParts(), partCount(), bits);
632 /* Shift the significand left BITS bits, subtract BITS from its exponent. */
634 APFloat::shiftSignificandLeft(unsigned int bits)
636 assert(bits < semantics->precision);
639 unsigned int partsCount = partCount();
641 APInt::tcShiftLeft(significandParts(), partsCount, bits);
644 assert(!APInt::tcIsZero(significandParts(), partsCount));
649 APFloat::compareAbsoluteValue(const APFloat &rhs) const
653 assert(semantics == rhs.semantics);
654 assert(category == fcNormal);
655 assert(rhs.category == fcNormal);
657 compare = exponent - rhs.exponent;
659 /* If exponents are equal, do an unsigned bignum comparison of the
662 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
666 return cmpGreaterThan;
673 /* Handle overflow. Sign is preserved. We either become infinity or
674 the largest finite number. */
676 APFloat::handleOverflow(roundingMode rounding_mode)
679 if(rounding_mode == rmNearestTiesToEven
680 || rounding_mode == rmNearestTiesToAway
681 || (rounding_mode == rmTowardPositive && !sign)
682 || (rounding_mode == rmTowardNegative && sign))
684 category = fcInfinity;
685 return (opStatus) (opOverflow | opInexact);
688 /* Otherwise we become the largest finite number. */
690 exponent = semantics->maxExponent;
691 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
692 semantics->precision);
697 /* This routine must work for fcZero of both signs, and fcNormal
700 APFloat::roundAwayFromZero(roundingMode rounding_mode,
701 lostFraction lost_fraction)
703 /* NaNs and infinities should not have lost fractions. */
704 assert(category == fcNormal || category == fcZero);
706 /* Our caller has already handled this case. */
707 assert(lost_fraction != lfExactlyZero);
709 switch(rounding_mode) {
713 case rmNearestTiesToAway:
714 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
716 case rmNearestTiesToEven:
717 if(lost_fraction == lfMoreThanHalf)
720 /* Our zeroes don't have a significand to test. */
721 if(lost_fraction == lfExactlyHalf && category != fcZero)
722 return significandParts()[0] & 1;
729 case rmTowardPositive:
730 return sign == false;
732 case rmTowardNegative:
738 APFloat::normalize(roundingMode rounding_mode,
739 lostFraction lost_fraction)
741 unsigned int omsb; /* One, not zero, based MSB. */
744 if(category != fcNormal)
747 /* Before rounding normalize the exponent of fcNormal numbers. */
748 omsb = significandMSB() + 1;
751 /* OMSB is numbered from 1. We want to place it in the integer
752 bit numbered PRECISON if possible, with a compensating change in
754 exponentChange = omsb - semantics->precision;
756 /* If the resulting exponent is too high, overflow according to
757 the rounding mode. */
758 if(exponent + exponentChange > semantics->maxExponent)
759 return handleOverflow(rounding_mode);
761 /* Subnormal numbers have exponent minExponent, and their MSB
762 is forced based on that. */
763 if(exponent + exponentChange < semantics->minExponent)
764 exponentChange = semantics->minExponent - exponent;
766 /* Shifting left is easy as we don't lose precision. */
767 if(exponentChange < 0) {
768 assert(lost_fraction == lfExactlyZero);
770 shiftSignificandLeft(-exponentChange);
775 if(exponentChange > 0) {
778 /* Shift right and capture any new lost fraction. */
779 lf = shiftSignificandRight(exponentChange);
781 lost_fraction = combineLostFractions(lf, lost_fraction);
783 /* Keep OMSB up-to-date. */
784 if(omsb > (unsigned) exponentChange)
785 omsb -= (unsigned) exponentChange;
791 /* Now round the number according to rounding_mode given the lost
794 /* As specified in IEEE 754, since we do not trap we do not report
795 underflow for exact results. */
796 if(lost_fraction == lfExactlyZero) {
797 /* Canonicalize zeroes. */
804 /* Increment the significand if we're rounding away from zero. */
805 if(roundAwayFromZero(rounding_mode, lost_fraction)) {
807 exponent = semantics->minExponent;
809 incrementSignificand();
810 omsb = significandMSB() + 1;
812 /* Did the significand increment overflow? */
813 if(omsb == (unsigned) semantics->precision + 1) {
814 /* Renormalize by incrementing the exponent and shifting our
815 significand right one. However if we already have the
816 maximum exponent we overflow to infinity. */
817 if(exponent == semantics->maxExponent) {
818 category = fcInfinity;
820 return (opStatus) (opOverflow | opInexact);
823 shiftSignificandRight(1);
829 /* The normal case - we were and are not denormal, and any
830 significand increment above didn't overflow. */
831 if(omsb == semantics->precision)
834 /* We have a non-zero denormal. */
835 assert(omsb < semantics->precision);
836 assert(exponent == semantics->minExponent);
838 /* Canonicalize zeroes. */
842 /* The fcZero case is a denormal that underflowed to zero. */
843 return (opStatus) (opUnderflow | opInexact);
847 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
849 switch(convolve(category, rhs.category)) {
853 case convolve(fcNaN, fcZero):
854 case convolve(fcNaN, fcNormal):
855 case convolve(fcNaN, fcInfinity):
856 case convolve(fcNaN, fcNaN):
857 case convolve(fcNormal, fcZero):
858 case convolve(fcInfinity, fcNormal):
859 case convolve(fcInfinity, fcZero):
862 case convolve(fcZero, fcNaN):
863 case convolve(fcNormal, fcNaN):
864 case convolve(fcInfinity, fcNaN):
866 copySignificand(rhs);
869 case convolve(fcNormal, fcInfinity):
870 case convolve(fcZero, fcInfinity):
871 category = fcInfinity;
872 sign = rhs.sign ^ subtract;
875 case convolve(fcZero, fcNormal):
877 sign = rhs.sign ^ subtract;
880 case convolve(fcZero, fcZero):
881 /* Sign depends on rounding mode; handled by caller. */
884 case convolve(fcInfinity, fcInfinity):
885 /* Differently signed infinities can only be validly
887 if(sign ^ rhs.sign != subtract) {
889 // Arbitrary but deterministic value for significand
890 APInt::tcSet(significandParts(), ~0U, partCount());
896 case convolve(fcNormal, fcNormal):
901 /* Add or subtract two normal numbers. */
903 APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
906 lostFraction lost_fraction;
909 /* Determine if the operation on the absolute values is effectively
910 an addition or subtraction. */
911 subtract ^= (sign ^ rhs.sign);
913 /* Are we bigger exponent-wise than the RHS? */
914 bits = exponent - rhs.exponent;
916 /* Subtraction is more subtle than one might naively expect. */
918 APFloat temp_rhs(rhs);
922 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
923 lost_fraction = lfExactlyZero;
924 } else if (bits > 0) {
925 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
926 shiftSignificandLeft(1);
929 lost_fraction = shiftSignificandRight(-bits - 1);
930 temp_rhs.shiftSignificandLeft(1);
935 carry = temp_rhs.subtractSignificand
936 (*this, lost_fraction != lfExactlyZero);
937 copySignificand(temp_rhs);
940 carry = subtractSignificand
941 (temp_rhs, lost_fraction != lfExactlyZero);
944 /* Invert the lost fraction - it was on the RHS and
946 if(lost_fraction == lfLessThanHalf)
947 lost_fraction = lfMoreThanHalf;
948 else if(lost_fraction == lfMoreThanHalf)
949 lost_fraction = lfLessThanHalf;
951 /* The code above is intended to ensure that no borrow is
956 APFloat temp_rhs(rhs);
958 lost_fraction = temp_rhs.shiftSignificandRight(bits);
959 carry = addSignificand(temp_rhs);
961 lost_fraction = shiftSignificandRight(-bits);
962 carry = addSignificand(rhs);
965 /* We have a guard bit; generating a carry cannot happen. */
969 return lost_fraction;
973 APFloat::multiplySpecials(const APFloat &rhs)
975 switch(convolve(category, rhs.category)) {
979 case convolve(fcNaN, fcZero):
980 case convolve(fcNaN, fcNormal):
981 case convolve(fcNaN, fcInfinity):
982 case convolve(fcNaN, fcNaN):
985 case convolve(fcZero, fcNaN):
986 case convolve(fcNormal, fcNaN):
987 case convolve(fcInfinity, fcNaN):
989 copySignificand(rhs);
992 case convolve(fcNormal, fcInfinity):
993 case convolve(fcInfinity, fcNormal):
994 case convolve(fcInfinity, fcInfinity):
995 category = fcInfinity;
998 case convolve(fcZero, fcNormal):
999 case convolve(fcNormal, fcZero):
1000 case convolve(fcZero, fcZero):
1004 case convolve(fcZero, fcInfinity):
1005 case convolve(fcInfinity, fcZero):
1007 // Arbitrary but deterministic value for significand
1008 APInt::tcSet(significandParts(), ~0U, partCount());
1011 case convolve(fcNormal, fcNormal):
1017 APFloat::divideSpecials(const APFloat &rhs)
1019 switch(convolve(category, rhs.category)) {
1023 case convolve(fcNaN, fcZero):
1024 case convolve(fcNaN, fcNormal):
1025 case convolve(fcNaN, fcInfinity):
1026 case convolve(fcNaN, fcNaN):
1027 case convolve(fcInfinity, fcZero):
1028 case convolve(fcInfinity, fcNormal):
1029 case convolve(fcZero, fcInfinity):
1030 case convolve(fcZero, fcNormal):
1033 case convolve(fcZero, fcNaN):
1034 case convolve(fcNormal, fcNaN):
1035 case convolve(fcInfinity, fcNaN):
1037 copySignificand(rhs);
1040 case convolve(fcNormal, fcInfinity):
1044 case convolve(fcNormal, fcZero):
1045 category = fcInfinity;
1048 case convolve(fcInfinity, fcInfinity):
1049 case convolve(fcZero, fcZero):
1051 // Arbitrary but deterministic value for significand
1052 APInt::tcSet(significandParts(), ~0U, partCount());
1055 case convolve(fcNormal, fcNormal):
1062 APFloat::changeSign()
1064 /* Look mummy, this one's easy. */
1069 APFloat::clearSign()
1071 /* So is this one. */
1076 APFloat::copySign(const APFloat &rhs)
1082 /* Normalized addition or subtraction. */
1084 APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
1089 fs = addOrSubtractSpecials(rhs, subtract);
1091 /* This return code means it was not a simple case. */
1092 if(fs == opDivByZero) {
1093 lostFraction lost_fraction;
1095 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1096 fs = normalize(rounding_mode, lost_fraction);
1098 /* Can only be zero if we lost no fraction. */
1099 assert(category != fcZero || lost_fraction == lfExactlyZero);
1102 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1103 positive zero unless rounding to minus infinity, except that
1104 adding two like-signed zeroes gives that zero. */
1105 if(category == fcZero) {
1106 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1107 sign = (rounding_mode == rmTowardNegative);
1113 /* Normalized addition. */
1115 APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1117 return addOrSubtract(rhs, rounding_mode, false);
1120 /* Normalized subtraction. */
1122 APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1124 return addOrSubtract(rhs, rounding_mode, true);
1127 /* Normalized multiply. */
1129 APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1134 fs = multiplySpecials(rhs);
1136 if(category == fcNormal) {
1137 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1138 fs = normalize(rounding_mode, lost_fraction);
1139 if(lost_fraction != lfExactlyZero)
1140 fs = (opStatus) (fs | opInexact);
1146 /* Normalized divide. */
1148 APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1153 fs = divideSpecials(rhs);
1155 if(category == fcNormal) {
1156 lostFraction lost_fraction = divideSignificand(rhs);
1157 fs = normalize(rounding_mode, lost_fraction);
1158 if(lost_fraction != lfExactlyZero)
1159 fs = (opStatus) (fs | opInexact);
1165 /* Normalized remainder. */
1167 APFloat::mod(const APFloat &rhs, roundingMode rounding_mode)
1171 unsigned int origSign = sign;
1172 fs = V.divide(rhs, rmNearestTiesToEven);
1173 if (fs == opDivByZero)
1176 int parts = partCount();
1177 integerPart *x = new integerPart[parts];
1178 fs = V.convertToInteger(x, parts * integerPartWidth, true,
1179 rmNearestTiesToEven);
1180 if (fs==opInvalidOp)
1183 fs = V.convertFromInteger(x, parts * integerPartWidth, true,
1184 rmNearestTiesToEven);
1185 assert(fs==opOK); // should always work
1187 fs = V.multiply(rhs, rounding_mode);
1188 assert(fs==opOK || fs==opInexact); // should not overflow or underflow
1190 fs = subtract(V, rounding_mode);
1191 assert(fs==opOK || fs==opInexact); // likewise
1194 sign = origSign; // IEEE754 requires this
1199 /* Normalized fused-multiply-add. */
1201 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
1202 const APFloat &addend,
1203 roundingMode rounding_mode)
1207 /* Post-multiplication sign, before addition. */
1208 sign ^= multiplicand.sign;
1210 /* If and only if all arguments are normal do we need to do an
1211 extended-precision calculation. */
1212 if(category == fcNormal
1213 && multiplicand.category == fcNormal
1214 && addend.category == fcNormal) {
1215 lostFraction lost_fraction;
1217 lost_fraction = multiplySignificand(multiplicand, &addend);
1218 fs = normalize(rounding_mode, lost_fraction);
1219 if(lost_fraction != lfExactlyZero)
1220 fs = (opStatus) (fs | opInexact);
1222 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1223 positive zero unless rounding to minus infinity, except that
1224 adding two like-signed zeroes gives that zero. */
1225 if(category == fcZero && sign != addend.sign)
1226 sign = (rounding_mode == rmTowardNegative);
1228 fs = multiplySpecials(multiplicand);
1230 /* FS can only be opOK or opInvalidOp. There is no more work
1231 to do in the latter case. The IEEE-754R standard says it is
1232 implementation-defined in this case whether, if ADDEND is a
1233 quiet NaN, we raise invalid op; this implementation does so.
1235 If we need to do the addition we can do so with normal
1238 fs = addOrSubtract(addend, rounding_mode, false);
1244 /* Comparison requires normalized numbers. */
1246 APFloat::compare(const APFloat &rhs) const
1250 assert(semantics == rhs.semantics);
1252 switch(convolve(category, rhs.category)) {
1256 case convolve(fcNaN, fcZero):
1257 case convolve(fcNaN, fcNormal):
1258 case convolve(fcNaN, fcInfinity):
1259 case convolve(fcNaN, fcNaN):
1260 case convolve(fcZero, fcNaN):
1261 case convolve(fcNormal, fcNaN):
1262 case convolve(fcInfinity, fcNaN):
1263 return cmpUnordered;
1265 case convolve(fcInfinity, fcNormal):
1266 case convolve(fcInfinity, fcZero):
1267 case convolve(fcNormal, fcZero):
1271 return cmpGreaterThan;
1273 case convolve(fcNormal, fcInfinity):
1274 case convolve(fcZero, fcInfinity):
1275 case convolve(fcZero, fcNormal):
1277 return cmpGreaterThan;
1281 case convolve(fcInfinity, fcInfinity):
1282 if(sign == rhs.sign)
1287 return cmpGreaterThan;
1289 case convolve(fcZero, fcZero):
1292 case convolve(fcNormal, fcNormal):
1296 /* Two normal numbers. Do they have the same sign? */
1297 if(sign != rhs.sign) {
1299 result = cmpLessThan;
1301 result = cmpGreaterThan;
1303 /* Compare absolute values; invert result if negative. */
1304 result = compareAbsoluteValue(rhs);
1307 if(result == cmpLessThan)
1308 result = cmpGreaterThan;
1309 else if(result == cmpGreaterThan)
1310 result = cmpLessThan;
1318 APFloat::convert(const fltSemantics &toSemantics,
1319 roundingMode rounding_mode)
1321 lostFraction lostFraction;
1322 unsigned int newPartCount, oldPartCount;
1325 lostFraction = lfExactlyZero;
1326 newPartCount = partCountForBits(toSemantics.precision + 1);
1327 oldPartCount = partCount();
1329 /* Handle storage complications. If our new form is wider,
1330 re-allocate our bit pattern into wider storage. If it is
1331 narrower, we ignore the excess parts, but if narrowing to a
1332 single part we need to free the old storage. */
1333 if (newPartCount > oldPartCount) {
1334 integerPart *newParts;
1336 newParts = new integerPart[newPartCount];
1337 APInt::tcSet(newParts, 0, newPartCount);
1338 APInt::tcAssign(newParts, significandParts(), oldPartCount);
1340 significand.parts = newParts;
1341 } else if (newPartCount < oldPartCount) {
1342 /* Capture any lost fraction through truncation of parts so we get
1343 correct rounding whilst normalizing. */
1344 lostFraction = lostFractionThroughTruncation
1345 (significandParts(), oldPartCount, toSemantics.precision);
1346 if (newPartCount == 1)
1348 integerPart newPart = significandParts()[0];
1350 significand.part = newPart;
1354 if(category == fcNormal) {
1355 /* Re-interpret our bit-pattern. */
1356 exponent += toSemantics.precision - semantics->precision;
1357 semantics = &toSemantics;
1358 fs = normalize(rounding_mode, lostFraction);
1360 semantics = &toSemantics;
1367 /* Convert a floating point number to an integer according to the
1368 rounding mode. If the rounded integer value is out of range this
1369 returns an invalid operation exception. If the rounded value is in
1370 range but the floating point number is not the exact integer, the C
1371 standard doesn't require an inexact exception to be raised. IEEE
1372 854 does require it so we do that.
1374 Note that for conversions to integer type the C standard requires
1375 round-to-zero to always be used. */
1377 APFloat::convertToInteger(integerPart *parts, unsigned int width,
1379 roundingMode rounding_mode) const
1381 lostFraction lost_fraction;
1382 unsigned int msb, partsCount;
1385 /* Handle the three special cases first. */
1386 if(category == fcInfinity || category == fcNaN)
1389 partsCount = partCountForBits(width);
1391 if(category == fcZero) {
1392 APInt::tcSet(parts, 0, partsCount);
1396 /* Shift the bit pattern so the fraction is lost. */
1399 bits = (int) semantics->precision - 1 - exponent;
1402 lost_fraction = tmp.shiftSignificandRight(bits);
1404 tmp.shiftSignificandLeft(-bits);
1405 lost_fraction = lfExactlyZero;
1408 if(lost_fraction != lfExactlyZero
1409 && tmp.roundAwayFromZero(rounding_mode, lost_fraction))
1410 tmp.incrementSignificand();
1412 msb = tmp.significandMSB();
1414 /* Negative numbers cannot be represented as unsigned. */
1415 if(!isSigned && tmp.sign && msb != -1U)
1418 /* It takes exponent + 1 bits to represent the truncated floating
1419 point number without its sign. We lose a bit for the sign, but
1420 the maximally negative integer is a special case. */
1421 if(msb + 1 > width) /* !! Not same as msb >= width !! */
1424 if(isSigned && msb + 1 == width
1425 && (!tmp.sign || tmp.significandLSB() != msb))
1428 APInt::tcAssign(parts, tmp.significandParts(), partsCount);
1431 APInt::tcNegate(parts, partsCount);
1433 if(lost_fraction == lfExactlyZero)
1440 APFloat::convertFromUnsignedInteger(integerPart *parts,
1441 unsigned int partCount,
1442 roundingMode rounding_mode)
1444 unsigned int msb, precision;
1445 lostFraction lost_fraction;
1447 msb = APInt::tcMSB(parts, partCount) + 1;
1448 precision = semantics->precision;
1450 category = fcNormal;
1451 exponent = precision - 1;
1453 if(msb > precision) {
1454 exponent += (msb - precision);
1455 lost_fraction = shiftRight(parts, partCount, msb - precision);
1458 lost_fraction = lfExactlyZero;
1460 /* Copy the bit image. */
1462 APInt::tcAssign(significandParts(), parts, partCountForBits(msb));
1464 return normalize(rounding_mode, lost_fraction);
1468 APFloat::convertFromInteger(const integerPart *parts, unsigned int width,
1469 bool isSigned, roundingMode rounding_mode)
1471 unsigned int partCount = partCountForBits(width);
1473 APInt api = APInt(width, partCount, parts);
1474 integerPart *copy = new integerPart[partCount];
1478 if (APInt::tcExtractBit(parts, width - 1)) {
1480 if (width < partCount * integerPartWidth)
1481 api = api.sext(partCount * integerPartWidth);
1483 else if (width < partCount * integerPartWidth)
1484 api = api.zext(partCount * integerPartWidth);
1486 if (width < partCount * integerPartWidth)
1487 api = api.zext(partCount * integerPartWidth);
1490 APInt::tcAssign(copy, api.getRawData(), partCount);
1491 status = convertFromUnsignedInteger(copy, partCount, rounding_mode);
1496 APFloat::convertFromHexadecimalString(const char *p,
1497 roundingMode rounding_mode)
1499 lostFraction lost_fraction;
1500 integerPart *significand;
1501 unsigned int bitPos, partsCount;
1502 const char *dot, *firstSignificantDigit;
1506 category = fcNormal;
1508 significand = significandParts();
1509 partsCount = partCount();
1510 bitPos = partsCount * integerPartWidth;
1512 /* Skip leading zeroes and any(hexa)decimal point. */
1513 p = skipLeadingZeroesAndAnyDot(p, &dot);
1514 firstSignificantDigit = p;
1517 integerPart hex_value;
1524 hex_value = hexDigitValue(*p);
1525 if(hex_value == -1U) {
1526 lost_fraction = lfExactlyZero;
1532 /* Store the number whilst 4-bit nibbles remain. */
1535 hex_value <<= bitPos % integerPartWidth;
1536 significand[bitPos / integerPartWidth] |= hex_value;
1538 lost_fraction = trailingHexadecimalFraction(p, hex_value);
1539 while(hexDigitValue(*p) != -1U)
1545 /* Hex floats require an exponent but not a hexadecimal point. */
1546 assert(*p == 'p' || *p == 'P');
1548 /* Ignore the exponent if we are zero. */
1549 if(p != firstSignificantDigit) {
1552 /* Implicit hexadecimal point? */
1556 /* Calculate the exponent adjustment implicit in the number of
1557 significant digits. */
1558 expAdjustment = dot - firstSignificantDigit;
1559 if(expAdjustment < 0)
1561 expAdjustment = expAdjustment * 4 - 1;
1563 /* Adjust for writing the significand starting at the most
1564 significant nibble. */
1565 expAdjustment += semantics->precision;
1566 expAdjustment -= partsCount * integerPartWidth;
1568 /* Adjust for the given exponent. */
1569 exponent = totalExponent(p, expAdjustment);
1572 return normalize(rounding_mode, lost_fraction);
1576 APFloat::convertFromString(const char *p, roundingMode rounding_mode) {
1577 /* Handle a leading minus sign. */
1583 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
1584 return convertFromHexadecimalString(p + 2, rounding_mode);
1586 assert(0 && "Decimal to binary conversions not yet implemented");
1590 // For good performance it is desirable for different APFloats
1591 // to produce different integers.
1593 APFloat::getHashValue() const {
1594 if (category==fcZero) return sign<<8 | semantics->precision ;
1595 else if (category==fcInfinity) return sign<<9 | semantics->precision;
1596 else if (category==fcNaN) return 1<<10 | semantics->precision;
1598 uint32_t hash = sign<<11 | semantics->precision | exponent<<12;
1599 const integerPart* p = significandParts();
1600 for (int i=partCount(); i>0; i--, p++)
1601 hash ^= ((uint32_t)*p) ^ (*p)>>32;
1606 // Conversion from APFloat to/from host float/double. It may eventually be
1607 // possible to eliminate these and have everybody deal with APFloats, but that
1608 // will take a while. This approach will not easily extend to long double.
1609 // Current implementation requires integerPartWidth==64, which is correct at
1610 // the moment but could be made more general.
1612 // Denormals have exponent minExponent in APFloat, but minExponent-1 in
1613 // the actual IEEE respresentations. We compensate for that here.
1616 APFloat::convertF80LongDoubleAPFloatToAPInt() const {
1617 assert(semantics == (const llvm::fltSemantics* const)&x87DoubleExtended);
1618 assert (partCount()==2);
1620 uint64_t myexponent, mysignificand;
1622 if (category==fcNormal) {
1623 myexponent = exponent+16383; //bias
1624 mysignificand = significandParts()[0];
1625 if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL))
1626 myexponent = 0; // denormal
1627 } else if (category==fcZero) {
1630 } else if (category==fcInfinity) {
1631 myexponent = 0x7fff;
1632 mysignificand = 0x8000000000000000ULL;
1633 } else if (category==fcNaN) {
1634 myexponent = 0x7fff;
1635 mysignificand = significandParts()[0];
1640 words[0] = (((uint64_t)sign & 1) << 63) |
1641 ((myexponent & 0x7fff) << 48) |
1642 ((mysignificand >>16) & 0xffffffffffffLL);
1643 words[1] = mysignificand & 0xffff;
1644 APInt api(80, 2, words);
1649 APFloat::convertDoubleAPFloatToAPInt() const {
1650 assert(semantics == (const llvm::fltSemantics*)&IEEEdouble);
1651 assert (partCount()==1);
1653 uint64_t myexponent, mysignificand;
1655 if (category==fcNormal) {
1656 myexponent = exponent+1023; //bias
1657 mysignificand = *significandParts();
1658 if (myexponent==1 && !(mysignificand & 0x10000000000000LL))
1659 myexponent = 0; // denormal
1660 } else if (category==fcZero) {
1663 } else if (category==fcInfinity) {
1666 } else if (category==fcNaN) {
1668 mysignificand = *significandParts();
1672 APInt api(64, (((((uint64_t)sign & 1) << 63) |
1673 ((myexponent & 0x7ff) << 52) |
1674 (mysignificand & 0xfffffffffffffLL))));
1679 APFloat::convertFloatAPFloatToAPInt() const {
1680 assert(semantics == (const llvm::fltSemantics*)&IEEEsingle);
1681 assert (partCount()==1);
1683 uint32_t myexponent, mysignificand;
1685 if (category==fcNormal) {
1686 myexponent = exponent+127; //bias
1687 mysignificand = *significandParts();
1688 if (myexponent == 1 && !(mysignificand & 0x400000))
1689 myexponent = 0; // denormal
1690 } else if (category==fcZero) {
1693 } else if (category==fcInfinity) {
1696 } else if (category==fcNaN) {
1698 mysignificand = *significandParts();
1702 APInt api(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) |
1703 (mysignificand & 0x7fffff)));
1708 APFloat::convertToAPInt() const {
1709 if (semantics == (const llvm::fltSemantics* const)&IEEEsingle)
1710 return convertFloatAPFloatToAPInt();
1711 else if (semantics == (const llvm::fltSemantics* const)&IEEEdouble)
1712 return convertDoubleAPFloatToAPInt();
1713 else if (semantics == (const llvm::fltSemantics* const)&x87DoubleExtended)
1714 return convertF80LongDoubleAPFloatToAPInt();
1720 APFloat::convertToFloat() const {
1721 assert(semantics == (const llvm::fltSemantics* const)&IEEEsingle);
1722 APInt api = convertToAPInt();
1723 return api.bitsToFloat();
1727 APFloat::convertToDouble() const {
1728 assert(semantics == (const llvm::fltSemantics* const)&IEEEdouble);
1729 APInt api = convertToAPInt();
1730 return api.bitsToDouble();
1733 /// Integer bit is explicit in this format. Current Intel book does not
1734 /// define meaning of:
1735 /// exponent = all 1's, integer bit not set.
1736 /// exponent = 0, integer bit set. (formerly "psuedodenormals")
1737 /// exponent!=0 nor all 1's, integer bit not set. (formerly "unnormals")
1739 APFloat::initFromF80LongDoubleAPInt(const APInt &api) {
1740 assert(api.getBitWidth()==80);
1741 uint64_t i1 = api.getRawData()[0];
1742 uint64_t i2 = api.getRawData()[1];
1743 uint64_t myexponent = (i1 >> 48) & 0x7fff;
1744 uint64_t mysignificand = ((i1 << 16) & 0xffffffffffff0000ULL) |
1747 initialize(&APFloat::x87DoubleExtended);
1748 assert(partCount()==2);
1751 if (myexponent==0 && mysignificand==0) {
1752 // exponent, significand meaningless
1754 } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {
1755 // exponent, significand meaningless
1756 category = fcInfinity;
1757 } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) {
1758 // exponent meaningless
1760 significandParts()[0] = mysignificand;
1761 significandParts()[1] = 0;
1763 category = fcNormal;
1764 exponent = myexponent - 16383;
1765 significandParts()[0] = mysignificand;
1766 significandParts()[1] = 0;
1767 if (myexponent==0) // denormal
1773 APFloat::initFromDoubleAPInt(const APInt &api) {
1774 assert(api.getBitWidth()==64);
1775 uint64_t i = *api.getRawData();
1776 uint64_t myexponent = (i >> 52) & 0x7ff;
1777 uint64_t mysignificand = i & 0xfffffffffffffLL;
1779 initialize(&APFloat::IEEEdouble);
1780 assert(partCount()==1);
1783 if (myexponent==0 && mysignificand==0) {
1784 // exponent, significand meaningless
1786 } else if (myexponent==0x7ff && mysignificand==0) {
1787 // exponent, significand meaningless
1788 category = fcInfinity;
1789 } else if (myexponent==0x7ff && mysignificand!=0) {
1790 // exponent meaningless
1792 *significandParts() = mysignificand;
1794 category = fcNormal;
1795 exponent = myexponent - 1023;
1796 *significandParts() = mysignificand;
1797 if (myexponent==0) // denormal
1800 *significandParts() |= 0x10000000000000LL; // integer bit
1805 APFloat::initFromFloatAPInt(const APInt & api) {
1806 assert(api.getBitWidth()==32);
1807 uint32_t i = (uint32_t)*api.getRawData();
1808 uint32_t myexponent = (i >> 23) & 0xff;
1809 uint32_t mysignificand = i & 0x7fffff;
1811 initialize(&APFloat::IEEEsingle);
1812 assert(partCount()==1);
1815 if (myexponent==0 && mysignificand==0) {
1816 // exponent, significand meaningless
1818 } else if (myexponent==0xff && mysignificand==0) {
1819 // exponent, significand meaningless
1820 category = fcInfinity;
1821 } else if (myexponent==0xff && (mysignificand & 0x400000)) {
1822 // sign, exponent, significand meaningless
1824 *significandParts() = mysignificand;
1826 category = fcNormal;
1827 exponent = myexponent - 127; //bias
1828 *significandParts() = mysignificand;
1829 if (myexponent==0) // denormal
1832 *significandParts() |= 0x800000; // integer bit
1836 /// Treat api as containing the bits of a floating point number. Currently
1837 /// we infer the floating point type from the size of the APInt. FIXME: This
1838 /// breaks when we get to PPC128 and IEEE128 (but both cannot exist in the
1839 /// same compile...)
1841 APFloat::initFromAPInt(const APInt& api) {
1842 if (api.getBitWidth() == 32)
1843 return initFromFloatAPInt(api);
1844 else if (api.getBitWidth()==64)
1845 return initFromDoubleAPInt(api);
1846 else if (api.getBitWidth()==80)
1847 return initFromF80LongDoubleAPInt(api);
1852 APFloat::APFloat(const APInt& api) {
1856 APFloat::APFloat(float f) {
1857 APInt api = APInt(32, 0);
1858 initFromAPInt(api.floatToBits(f));
1861 APFloat::APFloat(double d) {
1862 APInt api = APInt(64, 0);
1863 initFromAPInt(api.doubleToBits(d));