llvm.org GIT mirror llvm / ee7ae38
When converting to integer, do bit manipulations in the destination memory rather than in a copy of the APFloat. This avoids problems when the destination is wider than our significand and is cleaner. Also provide deterministic values in all cases where conversion fails, namely zero for NaNs and the minimal or maximal value respectively for underflow or overflow. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43626 91177308-0d34-0410-b5e6-96231b3b80d8 Neil Booth 13 years ago
2 changed file(s) with 125 addition(s) and 87 deletion(s). Raw diff Collapse all Expand all
274274 cmpResult compareAbsoluteValue(const APFloat &) const;
275275 opStatus handleOverflow(roundingMode);
276276 bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const;
277 opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool,
278 roundingMode) const;
277279 opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
278280 roundingMode);
279281 opStatus convertFromHexadecimalString(const char *, roundingMode);
17321732
17331733 /* Convert a floating point number to an integer according to the
17341734 rounding mode. If the rounded integer value is out of range this
1735 returns an invalid operation exception. If the rounded value is in
1735 returns an invalid operation exception and the contents of the
1736 destination parts are unspecified. If the rounded value is in
17361737 range but the floating point number is not the exact integer, the C
17371738 standard doesn't require an inexact exception to be raised. IEEE
17381739 854 does require it so we do that.
17391740
17401741 Note that for conversions to integer type the C standard requires
17411742 round-to-zero to always be used. */
1743 APFloat::opStatus
1744 APFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width,
1745 bool isSigned,
1746 roundingMode rounding_mode) const
1747 {
1748 lostFraction lost_fraction;
1749 const integerPart *src;
1750 unsigned int dstPartsCount, truncatedBits;
1751
1752 /* Handle the three special cases first. */
1753 if(category == fcInfinity || category == fcNaN)
1754 return opInvalidOp;
1755
1756 dstPartsCount = partCountForBits(width);
1757
1758 if(category == fcZero) {
1759 APInt::tcSet(parts, 0, dstPartsCount);
1760 return opOK;
1761 }
1762
1763 src = significandParts();
1764
1765 /* Step 1: place our absolute value, with any fraction truncated, in
1766 the destination. */
1767 if (exponent < 0) {
1768 /* Our absolute value is less than one; truncate everything. */
1769 APInt::tcSet(parts, 0, dstPartsCount);
1770 truncatedBits = semantics->precision;
1771 } else {
1772 /* We want the most significant (exponent + 1) bits; the rest are
1773 truncated. */
1774 unsigned int bits = exponent + 1U;
1775
1776 /* Hopelessly large in magnitude? */
1777 if (bits > width)
1778 return opInvalidOp;
1779
1780 if (bits < semantics->precision) {
1781 /* We truncate (semantics->precision - bits) bits. */
1782 truncatedBits = semantics->precision - bits;
1783 APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
1784 } else {
1785 /* We want at least as many bits as are available. */
1786 APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
1787 APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
1788 truncatedBits = 0;
1789 }
1790 }
1791
1792 /* Step 2: work out any lost fraction, and increment the absolute
1793 value if we would round away from zero. */
1794 if (truncatedBits) {
1795 lost_fraction = lostFractionThroughTruncation(src, partCount(),
1796 truncatedBits);
1797 if (lost_fraction != lfExactlyZero
1798 && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
1799 if (APInt::tcIncrement(parts, dstPartsCount))
1800 return opInvalidOp; /* Overflow. */
1801 }
1802 } else {
1803 lost_fraction = lfExactlyZero;
1804 }
1805
1806 /* Step 3: check if we fit in the destination. */
1807 unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1;
1808
1809 if (sign) {
1810 if (!isSigned) {
1811 /* Negative numbers cannot be represented as unsigned. */
1812 if (omsb != 0)
1813 return opInvalidOp;
1814 } else {
1815 /* It takes omsb bits to represent the unsigned integer value.
1816 We lose a bit for the sign, but care is needed as the
1817 maximally negative integer is a special case. */
1818 if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb)
1819 return opInvalidOp;
1820
1821 /* This case can happen because of rounding. */
1822 if (omsb > width)
1823 return opInvalidOp;
1824 }
1825
1826 APInt::tcNegate (parts, dstPartsCount);
1827 } else {
1828 if (omsb >= width + !isSigned)
1829 return opInvalidOp;
1830 }
1831
1832 if (lost_fraction == lfExactlyZero)
1833 return opOK;
1834 else
1835 return opInexact;
1836 }
1837
1838 /* Same as convertToSignExtendedInteger, except we provide
1839 deterministic values in case of an invalid operation exception,
1840 namely zero for NaNs and the minimal or maximal value respectively
1841 for underflow or overflow. */
17421842 APFloat::opStatus
17431843 APFloat::convertToInteger(integerPart *parts, unsigned int width,
17441844 bool isSigned,
17451845 roundingMode rounding_mode) const
17461846 {
1747 lostFraction lost_fraction;
1748 unsigned int msb, partsCount;
1749 int bits;
1750
1751 assertArithmeticOK(*semantics);
1752 partsCount = partCountForBits(width);
1753
1754 /* Handle the three special cases first. We produce
1755 a deterministic result even for the Invalid cases. */
1756 if (category == fcNaN) {
1757 // Neither sign nor isSigned affects this.
1758 APInt::tcSet(parts, 0, partsCount);
1759 return opInvalidOp;
1760 }
1761 if (category == fcInfinity) {
1762 if (!sign && isSigned)
1763 APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
1764 else if (!sign && !isSigned)
1765 APInt::tcSetLeastSignificantBits(parts, partsCount, width);
1766 else if (sign && isSigned) {
1767 APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
1768 APInt::tcShiftLeft(parts, partsCount, width-1);
1769 } else // sign && !isSigned
1770 APInt::tcSet(parts, 0, partsCount);
1771 return opInvalidOp;
1772 }
1773 if (category == fcZero) {
1774 APInt::tcSet(parts, 0, partsCount);
1775 return opOK;
1776 }
1777
1778 /* Shift the bit pattern so the fraction is lost. */
1779 APFloat tmp(*this);
1780
1781 bits = (int) semantics->precision - 1 - exponent;
1782
1783 if(bits > 0) {
1784 lost_fraction = tmp.shiftSignificandRight(bits);
1785 } else {
1786 if ((unsigned) -bits >= semantics->precision) {
1787 // Unrepresentably large.
1788 if (!sign && isSigned)
1789 APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
1790 else if (!sign && !isSigned)
1791 APInt::tcSetLeastSignificantBits(parts, partsCount, width);
1792 else if (sign && isSigned) {
1793 APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
1794 APInt::tcShiftLeft(parts, partsCount, width-1);
1795 } else // sign && !isSigned
1796 APInt::tcSet(parts, 0, partsCount);
1797 return (opStatus)(opOverflow | opInexact);
1798 }
1799 tmp.shiftSignificandLeft(-bits);
1800 lost_fraction = lfExactlyZero;
1801 }
1802
1803 if(lost_fraction != lfExactlyZero
1804 && tmp.roundAwayFromZero(rounding_mode, lost_fraction, 0))
1805 tmp.incrementSignificand();
1806
1807 msb = tmp.significandMSB();
1808
1809 /* Negative numbers cannot be represented as unsigned. */
1810 if(!isSigned && tmp.sign && msb != -1U)
1811 return opInvalidOp;
1812
1813 /* It takes exponent + 1 bits to represent the truncated floating
1814 point number without its sign. We lose a bit for the sign, but
1815 the maximally negative integer is a special case. */
1816 if(msb + 1 > width) /* !! Not same as msb >= width !! */
1817 return opInvalidOp;
1818
1819 if(isSigned && msb + 1 == width
1820 && (!tmp.sign || tmp.significandLSB() != msb))
1821 return opInvalidOp;
1822
1823 APInt::tcAssign(parts, tmp.significandParts(), partsCount);
1824
1825 if(tmp.sign)
1826 APInt::tcNegate(parts, partsCount);
1827
1828 if(lost_fraction == lfExactlyZero)
1829 return opOK;
1830 else
1831 return opInexact;
1847 opStatus fs;
1848
1849 fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode);
1850
1851 if (fs == opInvalidOp) {
1852 unsigned int bits, dstPartsCount;
1853
1854 dstPartsCount = partCountForBits(width);
1855
1856 if (category == fcNaN)
1857 bits = 0;
1858 else if (sign)
1859 bits = isSigned;
1860 else
1861 bits = width - isSigned;
1862
1863 APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits);
1864 if (sign && isSigned)
1865 APInt::tcShiftLeft(parts, dstPartsCount, width - 1);
1866 }
1867
1868 return fs;
18321869 }
18331870
18341871 /* Convert an unsigned integer SRC to a floating point number,
21612198 partCount++;
21622199 } while (p <= D.lastSigDigit);
21632200
2164 category = fcNormal;
21652201 fs = roundSignificandWithExponent(decSignificand, partCount,
21662202 D.exponent, rounding_mode);
21672203