- if ((unsigned) -bits >= semantics->precision) {
- // Unrepresentably large.
- if (!sign && isSigned)
- APInt::tcSetLeastSignificantBits(parts, partsCount, width-1);
- else if (!sign && !isSigned)
- APInt::tcSetLeastSignificantBits(parts, partsCount, width);
- else if (sign && isSigned) {
- APInt::tcSetLeastSignificantBits(parts, partsCount, 1);
- APInt::tcShiftLeft(parts, partsCount, width-1);
- } else // sign && !isSigned
- APInt::tcSet(parts, 0, partsCount);
- return (opStatus)(opOverflow | opInexact);
+ /* We want the most significant (exponent + 1) bits; the rest are
+ truncated. */
+ unsigned int bits = exponent + 1U;
+
+ /* Hopelessly large in magnitude? */
+ if (bits > width)
+ return opInvalidOp;
+
+ if (bits < semantics->precision) {
+ /* We truncate (semantics->precision - bits) bits. */
+ truncatedBits = semantics->precision - bits;
+ APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits);
+ } else {
+ /* We want at least as many bits as are available. */
+ APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0);
+ APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision);
+ truncatedBits = 0;
+ }
+ }
+
+ /* Step 2: work out any lost fraction, and increment the absolute
+ value if we would round away from zero. */
+ if (truncatedBits) {
+ lost_fraction = lostFractionThroughTruncation(src, partCount(),
+ truncatedBits);
+ if (lost_fraction != lfExactlyZero
+ && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) {
+ if (APInt::tcIncrement(parts, dstPartsCount))
+ return opInvalidOp; /* Overflow. */