Index: llvm/lib/Support/APFloat.cpp =================================================================== --- llvm/lib/Support/APFloat.cpp +++ llvm/lib/Support/APFloat.cpp @@ -843,10 +843,41 @@ // us to test for binade boundaries. const integerPart *Parts = significandParts(); const unsigned PartCount = partCount(); - for (unsigned i = 0; i < PartCount - 1; i++) + + // Check the integerParts excluding the last two + for (unsigned i = 0; i + 2 < PartCount; i++) if (~Parts[i]) return false; + if (PartCount > 1) { + + // Because significandParts includes a guard bit, + // in the case that the size of the significand + // is a multiple of integerPartWidth, the integral bit + // (which we must ignore) will be in the second-to-last + // integerPart + if (semantics->precision % integerPartWidth == 0) { + + integerPart IntegralBitPart = Parts[PartCount - 2]; + + // Set the integral bit to 1 to ignore it + const integerPart IntegralBitFill = ~integerPart(0) + << (integerPartWidth - 1); + + IntegralBitPart |= IntegralBitFill; + + // Since the last integerPart only contains + // the guard bit in this case, we do not have + // to check it + return !(~IntegralBitPart); + } + + // Otherwise, check all the bits in the + // second-to-last integerPart + if (~Parts[PartCount - 2]) + return false; + } + // Set the unused high bits to all ones when we compare. const unsigned NumHighBits = PartCount*integerPartWidth - semantics->precision + 1; @@ -866,10 +897,40 @@ const integerPart *Parts = significandParts(); const unsigned PartCount = partCount(); - for (unsigned i = 0; i < PartCount - 1; i++) + // Check the integerParts excluding the last two + for (unsigned i = 0; i + 2 < PartCount; i++) if (Parts[i]) return false; + if (PartCount > 1) { + + // Because significandParts includes a guard bit, + // in the case that the size of the significand + // is a multiple of integerPartWidth, the integral bit + // (which we must ignore) will be in the second-to-last + // integerPart + if (semantics->precision % integerPartWidth == 0) { + + integerPart IntegralBitPart = Parts[PartCount - 2]; + + // Set the integral bit to 0 to ignore it + const integerPart IntegralBitMask = ~integerPart(0) + << (integerPartWidth - 1); + + IntegralBitPart &= ~IntegralBitMask; + + // Since the last integerPart only contains + // the guard bit in this case, we do not have + // to check it + return !IntegralBitPart; + } + + // Otherwise, check all the bits + // in the second-to-last integerPart + if (Parts[PartCount - 2]) + return false; + } + const unsigned NumHighBits = PartCount*integerPartWidth - semantics->precision + 1; assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " Index: llvm/unittests/ADT/APFloatTest.cpp =================================================================== --- llvm/unittests/ADT/APFloatTest.cpp +++ llvm/unittests/ADT/APFloatTest.cpp @@ -4696,4 +4696,15 @@ EXPECT_EQ(0x3fe8000000000000ull, Result.bitcastToAPInt().getRawData()[0]); EXPECT_EQ(0x3c98000000000000ull, Result.bitcastToAPInt().getRawData()[1]); } + +TEST(APFloatTest, x87Largest) { + APFloat MaxX87Val = APFloat::getLargest(APFloat::x87DoubleExtended()); + EXPECT_TRUE(MaxX87Val.isLargest()); +} + +TEST(APFloatTest, x87Next) { + APFloat f(APFloat::x87DoubleExtended(), "-1.0"); + f.next(false); + EXPECT_TRUE(ilogb(f) == -1); +} }