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"
20 #define convolve(lhs, rhs) ((lhs) * 4 + (rhs))
22 /* Assumed in hexadecimal significand parsing. */
23 COMPILE_TIME_ASSERT(integerPartWidth % 4 == 0);
27 /* Represents floating point arithmetic semantics. */
29 /* The largest E such that 2^E is representable; this matches the
30 definition of IEEE 754. */
31 exponent_t maxExponent;
33 /* The smallest E such that 2^E is a normalized number; this
34 matches the definition of IEEE 754. */
35 exponent_t minExponent;
37 /* Number of bits in the significand. This includes the integer
39 unsigned char precision;
41 /* If the target format has an implicit integer bit. */
42 bool implicitIntegerBit;
45 const fltSemantics APFloat::IEEEsingle = { 127, -126, 24, true };
46 const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53, true };
47 const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113, true };
48 const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64, false };
51 /* Put a bunch of private, handy routines in an anonymous namespace. */
55 partCountForBits(unsigned int bits)
57 return ((bits) + integerPartWidth - 1) / integerPartWidth;
61 digitValue(unsigned int c)
73 hexDigitValue (unsigned int c)
92 /* This is ugly and needs cleaning up, but I don't immediately see
93 how whilst remaining safe. */
95 totalExponent(const char *p, int exponentAdjustment)
97 integerPart unsignedExponent;
98 bool negative, overflow;
101 /* Move past the exponent letter and sign to the digits. */
103 negative = *p == '-';
104 if(*p == '-' || *p == '+')
107 unsignedExponent = 0;
112 value = digitValue(*p);
117 unsignedExponent = unsignedExponent * 10 + value;
118 if(unsignedExponent > 65535)
122 if(exponentAdjustment > 65535 || exponentAdjustment < -65536)
126 exponent = unsignedExponent;
128 exponent = -exponent;
129 exponent += exponentAdjustment;
130 if(exponent > 65535 || exponent < -65536)
135 exponent = negative ? -65536: 65535;
141 skipLeadingZeroesAndAnyDot(const char *p, const char **dot)
156 /* Return the trailing fraction of a hexadecimal number.
157 DIGITVALUE is the first hex digit of the fraction, P points to
160 trailingHexadecimalFraction(const char *p, unsigned int digitValue)
162 unsigned int hexDigit;
164 /* If the first trailing digit isn't 0 or 8 we can work out the
165 fraction immediately. */
167 return lfMoreThanHalf;
168 else if(digitValue < 8 && digitValue > 0)
169 return lfLessThanHalf;
171 /* Otherwise we need to find the first non-zero digit. */
175 hexDigit = hexDigitValue(*p);
177 /* If we ran off the end it is exactly zero or one-half, otherwise
180 return digitValue == 0 ? lfExactlyZero: lfExactlyHalf;
182 return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf;
185 /* Return the fraction lost were a bignum truncated. */
187 lostFractionThroughTruncation(integerPart *parts,
188 unsigned int partCount,
193 lsb = APInt::tcLSB(parts, partCount);
195 /* Note this is guaranteed true if bits == 0, or LSB == -1U. */
197 return lfExactlyZero;
199 return lfExactlyHalf;
200 if(bits <= partCount * integerPartWidth
201 && APInt::tcExtractBit(parts, bits - 1))
202 return lfMoreThanHalf;
204 return lfLessThanHalf;
207 /* Shift DST right BITS bits noting lost fraction. */
209 shiftRight(integerPart *dst, unsigned int parts, unsigned int bits)
211 lostFraction lost_fraction;
213 lost_fraction = lostFractionThroughTruncation(dst, parts, bits);
215 APInt::tcShiftRight(dst, parts, bits);
217 return lost_fraction;
223 APFloat::initialize(const fltSemantics *ourSemantics)
227 semantics = ourSemantics;
230 significand.parts = new integerPart[count];
234 APFloat::freeSignificand()
237 delete [] significand.parts;
241 APFloat::assign(const APFloat &rhs)
243 assert(semantics == rhs.semantics);
246 category = rhs.category;
247 exponent = rhs.exponent;
248 if(category == fcNormal)
249 copySignificand(rhs);
253 APFloat::copySignificand(const APFloat &rhs)
255 assert(category == fcNormal);
256 assert(rhs.partCount() >= partCount());
258 APInt::tcAssign(significandParts(), rhs.significandParts(),
263 APFloat::operator=(const APFloat &rhs)
266 if(semantics != rhs.semantics) {
268 initialize(rhs.semantics);
276 APFloat::APFloat(const fltSemantics &ourSemantics, integerPart value)
278 initialize(&ourSemantics);
281 exponent = ourSemantics.precision - 1;
282 significandParts()[0] = value;
283 normalize(rmNearestTiesToEven, lfExactlyZero);
286 APFloat::APFloat(const fltSemantics &ourSemantics,
287 fltCategory ourCategory, bool negative)
289 initialize(&ourSemantics);
290 category = ourCategory;
292 if(category == fcNormal)
296 APFloat::APFloat(const fltSemantics &ourSemantics, const char *text)
298 initialize(&ourSemantics);
299 convertFromString(text, rmNearestTiesToEven);
302 APFloat::APFloat(const APFloat &rhs)
304 initialize(rhs.semantics);
314 APFloat::partCount() const
316 return partCountForBits(semantics->precision + 1);
320 APFloat::semanticsPrecision(const fltSemantics &semantics)
322 return semantics.precision;
326 APFloat::significandParts() const
328 return const_cast<APFloat *>(this)->significandParts();
332 APFloat::significandParts()
334 assert(category == fcNormal);
337 return significand.parts;
339 return &significand.part;
342 /* Combine the effect of two lost fractions. */
344 APFloat::combineLostFractions(lostFraction moreSignificant,
345 lostFraction lessSignificant)
347 if(lessSignificant != lfExactlyZero) {
348 if(moreSignificant == lfExactlyZero)
349 moreSignificant = lfLessThanHalf;
350 else if(moreSignificant == lfExactlyHalf)
351 moreSignificant = lfMoreThanHalf;
354 return moreSignificant;
358 APFloat::zeroSignificand()
361 APInt::tcSet(significandParts(), 0, partCount());
364 /* Increment an fcNormal floating point number's significand. */
366 APFloat::incrementSignificand()
370 carry = APInt::tcIncrement(significandParts(), partCount());
372 /* Our callers should never cause us to overflow. */
376 /* Add the significand of the RHS. Returns the carry flag. */
378 APFloat::addSignificand(const APFloat &rhs)
382 parts = significandParts();
384 assert(semantics == rhs.semantics);
385 assert(exponent == rhs.exponent);
387 return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount());
390 /* Subtract the significand of the RHS with a borrow flag. Returns
393 APFloat::subtractSignificand(const APFloat &rhs, integerPart borrow)
397 parts = significandParts();
399 assert(semantics == rhs.semantics);
400 assert(exponent == rhs.exponent);
402 return APInt::tcSubtract(parts, rhs.significandParts(), borrow,
406 /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it
407 on to the full-precision result of the multiplication. Returns the
410 APFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend)
412 unsigned int omsb; // One, not zero, based MSB.
413 unsigned int partsCount, newPartsCount, precision;
414 integerPart *lhsSignificand;
415 integerPart scratch[4];
416 integerPart *fullSignificand;
417 lostFraction lost_fraction;
419 assert(semantics == rhs.semantics);
421 precision = semantics->precision;
422 newPartsCount = partCountForBits(precision * 2);
424 if(newPartsCount > 4)
425 fullSignificand = new integerPart[newPartsCount];
427 fullSignificand = scratch;
429 lhsSignificand = significandParts();
430 partsCount = partCount();
432 APInt::tcFullMultiply(fullSignificand, lhsSignificand,
433 rhs.significandParts(), partsCount);
435 lost_fraction = lfExactlyZero;
436 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
437 exponent += rhs.exponent;
440 Significand savedSignificand = significand;
441 const fltSemantics *savedSemantics = semantics;
442 fltSemantics extendedSemantics;
444 unsigned int extendedPrecision;
446 /* Normalize our MSB. */
447 extendedPrecision = precision + precision - 1;
448 if(omsb != extendedPrecision)
450 APInt::tcShiftLeft(fullSignificand, newPartsCount,
451 extendedPrecision - omsb);
452 exponent -= extendedPrecision - omsb;
455 /* Create new semantics. */
456 extendedSemantics = *semantics;
457 extendedSemantics.precision = extendedPrecision;
459 if(newPartsCount == 1)
460 significand.part = fullSignificand[0];
462 significand.parts = fullSignificand;
463 semantics = &extendedSemantics;
465 APFloat extendedAddend(*addend);
466 status = extendedAddend.convert(extendedSemantics, rmTowardZero);
467 assert(status == opOK);
468 lost_fraction = addOrSubtractSignificand(extendedAddend, false);
470 /* Restore our state. */
471 if(newPartsCount == 1)
472 fullSignificand[0] = significand.part;
473 significand = savedSignificand;
474 semantics = savedSemantics;
476 omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1;
479 exponent -= (precision - 1);
481 if(omsb > precision) {
482 unsigned int bits, significantParts;
485 bits = omsb - precision;
486 significantParts = partCountForBits(omsb);
487 lf = shiftRight(fullSignificand, significantParts, bits);
488 lost_fraction = combineLostFractions(lf, lost_fraction);
492 APInt::tcAssign(lhsSignificand, fullSignificand, partsCount);
494 if(newPartsCount > 4)
495 delete [] fullSignificand;
497 return lost_fraction;
500 /* Multiply the significands of LHS and RHS to DST. */
502 APFloat::divideSignificand(const APFloat &rhs)
504 unsigned int bit, i, partsCount;
505 const integerPart *rhsSignificand;
506 integerPart *lhsSignificand, *dividend, *divisor;
507 integerPart scratch[4];
508 lostFraction lost_fraction;
510 assert(semantics == rhs.semantics);
512 lhsSignificand = significandParts();
513 rhsSignificand = rhs.significandParts();
514 partsCount = partCount();
517 dividend = new integerPart[partsCount * 2];
521 divisor = dividend + partsCount;
523 /* Copy the dividend and divisor as they will be modified in-place. */
524 for(i = 0; i < partsCount; i++) {
525 dividend[i] = lhsSignificand[i];
526 divisor[i] = rhsSignificand[i];
527 lhsSignificand[i] = 0;
530 exponent -= rhs.exponent;
532 unsigned int precision = semantics->precision;
534 /* Normalize the divisor. */
535 bit = precision - APInt::tcMSB(divisor, partsCount) - 1;
538 APInt::tcShiftLeft(divisor, partsCount, bit);
541 /* Normalize the dividend. */
542 bit = precision - APInt::tcMSB(dividend, partsCount) - 1;
545 APInt::tcShiftLeft(dividend, partsCount, bit);
548 if(APInt::tcCompare(dividend, divisor, partsCount) < 0) {
550 APInt::tcShiftLeft(dividend, partsCount, 1);
551 assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0);
555 for(bit = precision; bit; bit -= 1) {
556 if(APInt::tcCompare(dividend, divisor, partsCount) >= 0) {
557 APInt::tcSubtract(dividend, divisor, 0, partsCount);
558 APInt::tcSetBit(lhsSignificand, bit - 1);
561 APInt::tcShiftLeft(dividend, partsCount, 1);
564 /* Figure out the lost fraction. */
565 int cmp = APInt::tcCompare(dividend, divisor, partsCount);
568 lost_fraction = lfMoreThanHalf;
570 lost_fraction = lfExactlyHalf;
571 else if(APInt::tcIsZero(dividend, partsCount))
572 lost_fraction = lfExactlyZero;
574 lost_fraction = lfLessThanHalf;
579 return lost_fraction;
583 APFloat::significandMSB() const
585 return APInt::tcMSB(significandParts(), partCount());
589 APFloat::significandLSB() const
591 return APInt::tcLSB(significandParts(), partCount());
594 /* Note that a zero result is NOT normalized to fcZero. */
596 APFloat::shiftSignificandRight(unsigned int bits)
598 /* Our exponent should not overflow. */
599 assert((exponent_t) (exponent + bits) >= exponent);
603 return shiftRight(significandParts(), partCount(), bits);
606 /* Shift the significand left BITS bits, subtract BITS from its exponent. */
608 APFloat::shiftSignificandLeft(unsigned int bits)
610 assert(bits < semantics->precision);
613 unsigned int partsCount = partCount();
615 APInt::tcShiftLeft(significandParts(), partsCount, bits);
618 assert(!APInt::tcIsZero(significandParts(), partsCount));
623 APFloat::compareAbsoluteValue(const APFloat &rhs) const
627 assert(semantics == rhs.semantics);
628 assert(category == fcNormal);
629 assert(rhs.category == fcNormal);
631 compare = exponent - rhs.exponent;
633 /* If exponents are equal, do an unsigned bignum comparison of the
636 compare = APInt::tcCompare(significandParts(), rhs.significandParts(),
640 return cmpGreaterThan;
647 /* Handle overflow. Sign is preserved. We either become infinity or
648 the largest finite number. */
650 APFloat::handleOverflow(roundingMode rounding_mode)
653 if(rounding_mode == rmNearestTiesToEven
654 || rounding_mode == rmNearestTiesToAway
655 || (rounding_mode == rmTowardPositive && !sign)
656 || (rounding_mode == rmTowardNegative && sign))
658 category = fcInfinity;
659 return (opStatus) (opOverflow | opInexact);
662 /* Otherwise we become the largest finite number. */
664 exponent = semantics->maxExponent;
665 APInt::tcSetLeastSignificantBits(significandParts(), partCount(),
666 semantics->precision);
671 /* This routine must work for fcZero of both signs, and fcNormal
674 APFloat::roundAwayFromZero(roundingMode rounding_mode,
675 lostFraction lost_fraction)
677 /* QNaNs and infinities should not have lost fractions. */
678 assert(category == fcNormal || category == fcZero);
680 /* Our caller has already handled this case. */
681 assert(lost_fraction != lfExactlyZero);
683 switch(rounding_mode) {
687 case rmNearestTiesToAway:
688 return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf;
690 case rmNearestTiesToEven:
691 if(lost_fraction == lfMoreThanHalf)
694 /* Our zeroes don't have a significand to test. */
695 if(lost_fraction == lfExactlyHalf && category != fcZero)
696 return significandParts()[0] & 1;
703 case rmTowardPositive:
704 return sign == false;
706 case rmTowardNegative:
712 APFloat::normalize(roundingMode rounding_mode,
713 lostFraction lost_fraction)
715 unsigned int omsb; /* One, not zero, based MSB. */
718 if(category != fcNormal)
721 /* Before rounding normalize the exponent of fcNormal numbers. */
722 omsb = significandMSB() + 1;
725 /* OMSB is numbered from 1. We want to place it in the integer
726 bit numbered PRECISON if possible, with a compensating change in
728 exponentChange = omsb - semantics->precision;
730 /* If the resulting exponent is too high, overflow according to
731 the rounding mode. */
732 if(exponent + exponentChange > semantics->maxExponent)
733 return handleOverflow(rounding_mode);
735 /* Subnormal numbers have exponent minExponent, and their MSB
736 is forced based on that. */
737 if(exponent + exponentChange < semantics->minExponent)
738 exponentChange = semantics->minExponent - exponent;
740 /* Shifting left is easy as we don't lose precision. */
741 if(exponentChange < 0) {
742 assert(lost_fraction == lfExactlyZero);
744 shiftSignificandLeft(-exponentChange);
749 if(exponentChange > 0) {
752 /* Shift right and capture any new lost fraction. */
753 lf = shiftSignificandRight(exponentChange);
755 lost_fraction = combineLostFractions(lf, lost_fraction);
757 /* Keep OMSB up-to-date. */
758 if(omsb > (unsigned) exponentChange)
759 omsb -= (unsigned) exponentChange;
765 /* Now round the number according to rounding_mode given the lost
768 /* As specified in IEEE 754, since we do not trap we do not report
769 underflow for exact results. */
770 if(lost_fraction == lfExactlyZero) {
771 /* Canonicalize zeroes. */
778 /* Increment the significand if we're rounding away from zero. */
779 if(roundAwayFromZero(rounding_mode, lost_fraction)) {
781 exponent = semantics->minExponent;
783 incrementSignificand();
784 omsb = significandMSB() + 1;
786 /* Did the significand increment overflow? */
787 if(omsb == (unsigned) semantics->precision + 1) {
788 /* Renormalize by incrementing the exponent and shifting our
789 significand right one. However if we already have the
790 maximum exponent we overflow to infinity. */
791 if(exponent == semantics->maxExponent) {
792 category = fcInfinity;
794 return (opStatus) (opOverflow | opInexact);
797 shiftSignificandRight(1);
803 /* The normal case - we were and are not denormal, and any
804 significand increment above didn't overflow. */
805 if(omsb == semantics->precision)
808 /* We have a non-zero denormal. */
809 assert(omsb < semantics->precision);
810 assert(exponent == semantics->minExponent);
812 /* Canonicalize zeroes. */
816 /* The fcZero case is a denormal that underflowed to zero. */
817 return (opStatus) (opUnderflow | opInexact);
821 APFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract)
823 switch(convolve(category, rhs.category)) {
827 case convolve(fcQNaN, fcZero):
828 case convolve(fcQNaN, fcNormal):
829 case convolve(fcQNaN, fcInfinity):
830 case convolve(fcQNaN, fcQNaN):
831 case convolve(fcNormal, fcZero):
832 case convolve(fcInfinity, fcNormal):
833 case convolve(fcInfinity, fcZero):
836 case convolve(fcZero, fcQNaN):
837 case convolve(fcNormal, fcQNaN):
838 case convolve(fcInfinity, fcQNaN):
842 case convolve(fcNormal, fcInfinity):
843 case convolve(fcZero, fcInfinity):
844 category = fcInfinity;
845 sign = rhs.sign ^ subtract;
848 case convolve(fcZero, fcNormal):
850 sign = rhs.sign ^ subtract;
853 case convolve(fcZero, fcZero):
854 /* Sign depends on rounding mode; handled by caller. */
857 case convolve(fcInfinity, fcInfinity):
858 /* Differently signed infinities can only be validly
860 if(sign ^ rhs.sign != subtract) {
867 case convolve(fcNormal, fcNormal):
872 /* Add or subtract two normal numbers. */
874 APFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract)
877 lostFraction lost_fraction;
880 /* Determine if the operation on the absolute values is effectively
881 an addition or subtraction. */
882 subtract ^= (sign ^ rhs.sign);
884 /* Are we bigger exponent-wise than the RHS? */
885 bits = exponent - rhs.exponent;
887 /* Subtraction is more subtle than one might naively expect. */
889 APFloat temp_rhs(rhs);
893 reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
894 lost_fraction = lfExactlyZero;
895 } else if(bits > 0) {
896 lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
897 shiftSignificandLeft(1);
899 } else if(bits < 0) {
900 lost_fraction = shiftSignificandRight(-bits - 1);
901 temp_rhs.shiftSignificandLeft(1);
906 carry = temp_rhs.subtractSignificand
907 (*this, lost_fraction != lfExactlyZero);
908 copySignificand(temp_rhs);
911 carry = subtractSignificand
912 (temp_rhs, lost_fraction != lfExactlyZero);
915 /* Invert the lost fraction - it was on the RHS and
917 if(lost_fraction == lfLessThanHalf)
918 lost_fraction = lfMoreThanHalf;
919 else if(lost_fraction == lfMoreThanHalf)
920 lost_fraction = lfLessThanHalf;
922 /* The code above is intended to ensure that no borrow is
927 APFloat temp_rhs(rhs);
929 lost_fraction = temp_rhs.shiftSignificandRight(bits);
930 carry = addSignificand(temp_rhs);
932 lost_fraction = shiftSignificandRight(-bits);
933 carry = addSignificand(rhs);
936 /* We have a guard bit; generating a carry cannot happen. */
940 return lost_fraction;
944 APFloat::multiplySpecials(const APFloat &rhs)
946 switch(convolve(category, rhs.category)) {
950 case convolve(fcQNaN, fcZero):
951 case convolve(fcQNaN, fcNormal):
952 case convolve(fcQNaN, fcInfinity):
953 case convolve(fcQNaN, fcQNaN):
954 case convolve(fcZero, fcQNaN):
955 case convolve(fcNormal, fcQNaN):
956 case convolve(fcInfinity, fcQNaN):
960 case convolve(fcNormal, fcInfinity):
961 case convolve(fcInfinity, fcNormal):
962 case convolve(fcInfinity, fcInfinity):
963 category = fcInfinity;
966 case convolve(fcZero, fcNormal):
967 case convolve(fcNormal, fcZero):
968 case convolve(fcZero, fcZero):
972 case convolve(fcZero, fcInfinity):
973 case convolve(fcInfinity, fcZero):
977 case convolve(fcNormal, fcNormal):
983 APFloat::divideSpecials(const APFloat &rhs)
985 switch(convolve(category, rhs.category)) {
989 case convolve(fcQNaN, fcZero):
990 case convolve(fcQNaN, fcNormal):
991 case convolve(fcQNaN, fcInfinity):
992 case convolve(fcQNaN, fcQNaN):
993 case convolve(fcInfinity, fcZero):
994 case convolve(fcInfinity, fcNormal):
995 case convolve(fcZero, fcInfinity):
996 case convolve(fcZero, fcNormal):
999 case convolve(fcZero, fcQNaN):
1000 case convolve(fcNormal, fcQNaN):
1001 case convolve(fcInfinity, fcQNaN):
1005 case convolve(fcNormal, fcInfinity):
1009 case convolve(fcNormal, fcZero):
1010 category = fcInfinity;
1013 case convolve(fcInfinity, fcInfinity):
1014 case convolve(fcZero, fcZero):
1018 case convolve(fcNormal, fcNormal):
1025 APFloat::changeSign()
1027 /* Look mummy, this one's easy. */
1031 /* Normalized addition or subtraction. */
1033 APFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode,
1038 fs = addOrSubtractSpecials(rhs, subtract);
1040 /* This return code means it was not a simple case. */
1041 if(fs == opDivByZero) {
1042 lostFraction lost_fraction;
1044 lost_fraction = addOrSubtractSignificand(rhs, subtract);
1045 fs = normalize(rounding_mode, lost_fraction);
1047 /* Can only be zero if we lost no fraction. */
1048 assert(category != fcZero || lost_fraction == lfExactlyZero);
1051 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1052 positive zero unless rounding to minus infinity, except that
1053 adding two like-signed zeroes gives that zero. */
1054 if(category == fcZero) {
1055 if(rhs.category != fcZero || (sign == rhs.sign) == subtract)
1056 sign = (rounding_mode == rmTowardNegative);
1062 /* Normalized addition. */
1064 APFloat::add(const APFloat &rhs, roundingMode rounding_mode)
1066 return addOrSubtract(rhs, rounding_mode, false);
1069 /* Normalized subtraction. */
1071 APFloat::subtract(const APFloat &rhs, roundingMode rounding_mode)
1073 return addOrSubtract(rhs, rounding_mode, true);
1076 /* Normalized multiply. */
1078 APFloat::multiply(const APFloat &rhs, roundingMode rounding_mode)
1083 fs = multiplySpecials(rhs);
1085 if(category == fcNormal) {
1086 lostFraction lost_fraction = multiplySignificand(rhs, 0);
1087 fs = normalize(rounding_mode, lost_fraction);
1088 if(lost_fraction != lfExactlyZero)
1089 fs = (opStatus) (fs | opInexact);
1095 /* Normalized divide. */
1097 APFloat::divide(const APFloat &rhs, roundingMode rounding_mode)
1102 fs = divideSpecials(rhs);
1104 if(category == fcNormal) {
1105 lostFraction lost_fraction = divideSignificand(rhs);
1106 fs = normalize(rounding_mode, lost_fraction);
1107 if(lost_fraction != lfExactlyZero)
1108 fs = (opStatus) (fs | opInexact);
1114 /* Normalized fused-multiply-add. */
1116 APFloat::fusedMultiplyAdd(const APFloat &multiplicand,
1117 const APFloat &addend,
1118 roundingMode rounding_mode)
1122 /* Post-multiplication sign, before addition. */
1123 sign ^= multiplicand.sign;
1125 /* If and only if all arguments are normal do we need to do an
1126 extended-precision calculation. */
1127 if(category == fcNormal
1128 && multiplicand.category == fcNormal
1129 && addend.category == fcNormal) {
1130 lostFraction lost_fraction;
1132 lost_fraction = multiplySignificand(multiplicand, &addend);
1133 fs = normalize(rounding_mode, lost_fraction);
1134 if(lost_fraction != lfExactlyZero)
1135 fs = (opStatus) (fs | opInexact);
1137 /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a
1138 positive zero unless rounding to minus infinity, except that
1139 adding two like-signed zeroes gives that zero. */
1140 if(category == fcZero && sign != addend.sign)
1141 sign = (rounding_mode == rmTowardNegative);
1143 fs = multiplySpecials(multiplicand);
1145 /* FS can only be opOK or opInvalidOp. There is no more work
1146 to do in the latter case. The IEEE-754R standard says it is
1147 implementation-defined in this case whether, if ADDEND is a
1148 quiet QNaN, we raise invalid op; this implementation does so.
1150 If we need to do the addition we can do so with normal
1153 fs = addOrSubtract(addend, rounding_mode, false);
1159 /* Comparison requires normalized numbers. */
1161 APFloat::compare(const APFloat &rhs) const
1165 assert(semantics == rhs.semantics);
1167 switch(convolve(category, rhs.category)) {
1171 case convolve(fcQNaN, fcZero):
1172 case convolve(fcQNaN, fcNormal):
1173 case convolve(fcQNaN, fcInfinity):
1174 case convolve(fcQNaN, fcQNaN):
1175 case convolve(fcZero, fcQNaN):
1176 case convolve(fcNormal, fcQNaN):
1177 case convolve(fcInfinity, fcQNaN):
1178 return cmpUnordered;
1180 case convolve(fcInfinity, fcNormal):
1181 case convolve(fcInfinity, fcZero):
1182 case convolve(fcNormal, fcZero):
1186 return cmpGreaterThan;
1188 case convolve(fcNormal, fcInfinity):
1189 case convolve(fcZero, fcInfinity):
1190 case convolve(fcZero, fcNormal):
1192 return cmpGreaterThan;
1196 case convolve(fcInfinity, fcInfinity):
1197 if(sign == rhs.sign)
1202 return cmpGreaterThan;
1204 case convolve(fcZero, fcZero):
1207 case convolve(fcNormal, fcNormal):
1211 /* Two normal numbers. Do they have the same sign? */
1212 if(sign != rhs.sign) {
1214 result = cmpLessThan;
1216 result = cmpGreaterThan;
1218 /* Compare absolute values; invert result if negative. */
1219 result = compareAbsoluteValue(rhs);
1222 if(result == cmpLessThan)
1223 result = cmpGreaterThan;
1224 else if(result == cmpGreaterThan)
1225 result = cmpLessThan;
1233 APFloat::convert(const fltSemantics &toSemantics,
1234 roundingMode rounding_mode)
1236 unsigned int newPartCount;
1239 newPartCount = partCountForBits(toSemantics.precision + 1);
1241 /* If our new form is wider, re-allocate our bit pattern into wider
1243 if(newPartCount > partCount()) {
1244 integerPart *newParts;
1246 newParts = new integerPart[newPartCount];
1247 APInt::tcSet(newParts, 0, newPartCount);
1248 APInt::tcAssign(newParts, significandParts(), partCount());
1250 significand.parts = newParts;
1253 if(category == fcNormal) {
1254 /* Re-interpret our bit-pattern. */
1255 exponent += toSemantics.precision - semantics->precision;
1256 semantics = &toSemantics;
1257 fs = normalize(rounding_mode, lfExactlyZero);
1259 semantics = &toSemantics;
1266 /* Convert a floating point number to an integer according to the
1267 rounding mode. If the rounded integer value is out of range this
1268 returns an invalid operation exception. If the rounded value is in
1269 range but the floating point number is not the exact integer, the C
1270 standard doesn't require an inexact exception to be raised. IEEE
1271 854 does require it so we do that.
1273 Note that for conversions to integer type the C standard requires
1274 round-to-zero to always be used. */
1276 APFloat::convertToInteger(integerPart *parts, unsigned int width,
1278 roundingMode rounding_mode) const
1280 lostFraction lost_fraction;
1281 unsigned int msb, partsCount;
1284 /* Handle the three special cases first. */
1285 if(category == fcInfinity || category == fcQNaN)
1288 partsCount = partCountForBits(width);
1290 if(category == fcZero) {
1291 APInt::tcSet(parts, 0, partsCount);
1295 /* Shift the bit pattern so the fraction is lost. */
1298 bits = (int) semantics->precision - 1 - exponent;
1301 lost_fraction = tmp.shiftSignificandRight(bits);
1303 tmp.shiftSignificandLeft(-bits);
1304 lost_fraction = lfExactlyZero;
1307 if(lost_fraction != lfExactlyZero
1308 && tmp.roundAwayFromZero(rounding_mode, lost_fraction))
1309 tmp.incrementSignificand();
1311 msb = tmp.significandMSB();
1313 /* Negative numbers cannot be represented as unsigned. */
1314 if(!isSigned && tmp.sign && msb != -1U)
1317 /* It takes exponent + 1 bits to represent the truncated floating
1318 point number without its sign. We lose a bit for the sign, but
1319 the maximally negative integer is a special case. */
1320 if(msb + 1 > width) /* !! Not same as msb >= width !! */
1323 if(isSigned && msb + 1 == width
1324 && (!tmp.sign || tmp.significandLSB() != msb))
1327 APInt::tcAssign(parts, tmp.significandParts(), partsCount);
1330 APInt::tcNegate(parts, partsCount);
1332 if(lost_fraction == lfExactlyZero)
1339 APFloat::convertFromUnsignedInteger(integerPart *parts,
1340 unsigned int partCount,
1341 roundingMode rounding_mode)
1343 unsigned int msb, precision;
1344 lostFraction lost_fraction;
1346 msb = APInt::tcMSB(parts, partCount) + 1;
1347 precision = semantics->precision;
1349 category = fcNormal;
1350 exponent = precision - 1;
1352 if(msb > precision) {
1353 exponent += (msb - precision);
1354 lost_fraction = shiftRight(parts, partCount, msb - precision);
1357 lost_fraction = lfExactlyZero;
1359 /* Copy the bit image. */
1361 APInt::tcAssign(significandParts(), parts, partCountForBits(msb));
1363 return normalize(rounding_mode, lost_fraction);
1367 APFloat::convertFromInteger(const integerPart *parts,
1368 unsigned int partCount, bool isSigned,
1369 roundingMode rounding_mode)
1375 copy = new integerPart[partCount];
1376 APInt::tcAssign(copy, parts, partCount);
1378 width = partCount * integerPartWidth;
1381 if(isSigned && APInt::tcExtractBit(parts, width - 1)) {
1383 APInt::tcNegate(copy, partCount);
1386 status = convertFromUnsignedInteger(copy, partCount, rounding_mode);
1393 APFloat::convertFromHexadecimalString(const char *p,
1394 roundingMode rounding_mode)
1396 lostFraction lost_fraction;
1397 integerPart *significand;
1398 unsigned int bitPos, partsCount;
1399 const char *dot, *firstSignificantDigit;
1403 category = fcNormal;
1405 significand = significandParts();
1406 partsCount = partCount();
1407 bitPos = partsCount * integerPartWidth;
1409 /* Skip leading zeroes and any(hexa)decimal point. */
1410 p = skipLeadingZeroesAndAnyDot(p, &dot);
1411 firstSignificantDigit = p;
1414 integerPart hex_value;
1421 hex_value = hexDigitValue(*p);
1422 if(hex_value == -1U) {
1423 lost_fraction = lfExactlyZero;
1429 /* Store the number whilst 4-bit nibbles remain. */
1432 hex_value <<= bitPos % integerPartWidth;
1433 significand[bitPos / integerPartWidth] |= hex_value;
1435 lost_fraction = trailingHexadecimalFraction(p, hex_value);
1436 while(hexDigitValue(*p) != -1U)
1442 /* Hex floats require an exponent but not a hexadecimal point. */
1443 assert(*p == 'p' || *p == 'P');
1445 /* Ignore the exponent if we are zero. */
1446 if(p != firstSignificantDigit) {
1449 /* Implicit hexadecimal point? */
1453 /* Calculate the exponent adjustment implicit in the number of
1454 significant digits. */
1455 expAdjustment = dot - firstSignificantDigit;
1456 if(expAdjustment < 0)
1458 expAdjustment = expAdjustment * 4 - 1;
1460 /* Adjust for writing the significand starting at the most
1461 significant nibble. */
1462 expAdjustment += semantics->precision;
1463 expAdjustment -= partsCount * integerPartWidth;
1465 /* Adjust for the given exponent. */
1466 exponent = totalExponent(p, expAdjustment);
1469 return normalize(rounding_mode, lost_fraction);
1473 APFloat::convertFromString(const char *p, roundingMode rounding_mode)
1475 /* Handle a leading minus sign. */
1481 if(p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
1482 return convertFromHexadecimalString(p + 2, rounding_mode);
1485 assert(0 && "Decimal to binary conversions not yet imlemented");