Skip to content

Commit f2c2851

Browse files
author
Serguei Katkov
committed
Fix APFloat mod sign
fmod specification requires the sign of the remainder is the same as numerator in case remainder is zero. Reviewers: gottesmm, scanon, arsenm, davide, craig.topper Reviewed By: scanon Subscribers: wdng, llvm-commits Differential Revision: https://reviews.llvm.org/D39225 llvm-svn: 317081
1 parent 82f0c42 commit f2c2851

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

llvm/lib/Support/APFloat.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,7 @@ IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) {
17431743
IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) {
17441744
opStatus fs;
17451745
fs = modSpecials(rhs);
1746+
unsigned int origSign = sign;
17461747

17471748
while (isFiniteNonZero() && rhs.isFiniteNonZero() &&
17481749
compareAbsoluteValue(rhs) != cmpLessThan) {
@@ -1754,6 +1755,8 @@ IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) {
17541755
fs = subtract(V, rmNearestTiesToEven);
17551756
assert(fs==opOK);
17561757
}
1758+
if (isZero())
1759+
sign = origSign; // fmod requires this
17571760
return fs;
17581761
}
17591762

llvm/unittests/ADT/APFloatTest.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,20 @@ TEST(APFloatTest, mod) {
32893289
EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
32903290
EXPECT_TRUE(f1.isNaN());
32913291
}
3292+
{
3293+
APFloat f1(APFloat::IEEEdouble(), "-4.0");
3294+
APFloat f2(APFloat::IEEEdouble(), "-2.0");
3295+
APFloat expected(APFloat::IEEEdouble(), "-0.0");
3296+
EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3297+
EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3298+
}
3299+
{
3300+
APFloat f1(APFloat::IEEEdouble(), "-4.0");
3301+
APFloat f2(APFloat::IEEEdouble(), "2.0");
3302+
APFloat expected(APFloat::IEEEdouble(), "-0.0");
3303+
EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3304+
EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3305+
}
32923306
}
32933307

32943308
TEST(APFloatTest, PPCDoubleDoubleAddSpecial) {

0 commit comments

Comments
 (0)