From: Serguei Katkov Date: Wed, 1 Nov 2017 07:56:55 +0000 (+0000) Subject: Fix APFloat mod sign X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f2c2851efe0505328d1f4074b313a2922bcd8f2c;p=platform%2Fupstream%2Fllvm.git 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 --- diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp index f835bd1..f7fb0ce 100644 --- a/llvm/lib/Support/APFloat.cpp +++ b/llvm/lib/Support/APFloat.cpp @@ -1743,6 +1743,7 @@ IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { opStatus fs; fs = modSpecials(rhs); + unsigned int origSign = sign; while (isFiniteNonZero() && rhs.isFiniteNonZero() && compareAbsoluteValue(rhs) != cmpLessThan) { @@ -1754,6 +1755,8 @@ IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { fs = subtract(V, rmNearestTiesToEven); assert(fs==opOK); } + if (isZero()) + sign = origSign; // fmod requires this return fs; } diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp index 280a086..84fb6fa 100644 --- a/llvm/unittests/ADT/APFloatTest.cpp +++ b/llvm/unittests/ADT/APFloatTest.cpp @@ -3289,6 +3289,20 @@ TEST(APFloatTest, mod) { EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp); EXPECT_TRUE(f1.isNaN()); } + { + APFloat f1(APFloat::IEEEdouble(), "-4.0"); + APFloat f2(APFloat::IEEEdouble(), "-2.0"); + APFloat expected(APFloat::IEEEdouble(), "-0.0"); + EXPECT_EQ(f1.mod(f2), APFloat::opOK); + EXPECT_TRUE(f1.bitwiseIsEqual(expected)); + } + { + APFloat f1(APFloat::IEEEdouble(), "-4.0"); + APFloat f2(APFloat::IEEEdouble(), "2.0"); + APFloat expected(APFloat::IEEEdouble(), "-0.0"); + EXPECT_EQ(f1.mod(f2), APFloat::opOK); + EXPECT_TRUE(f1.bitwiseIsEqual(expected)); + } } TEST(APFloatTest, PPCDoubleDoubleAddSpecial) {