summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Support/APFloat.cpp8
-rw-r--r--test/Transforms/InstCombine/fdiv.ll2
-rw-r--r--unittests/ADT/APFloatTest.cpp6
3 files changed, 11 insertions, 5 deletions
diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp
index abe5575318..3a63258cd2 100644
--- a/lib/Support/APFloat.cpp
+++ b/lib/Support/APFloat.cpp
@@ -3583,6 +3583,14 @@ bool APFloat::getExactInverse(APFloat *inv) const {
if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK)
return false;
+ // Avoid multiplication with a denormal, it is not safe on all platforms and
+ // may be slower than a normal division.
+ if (reciprocal.significandMSB() + 1 < reciprocal.semantics->precision)
+ return false;
+
+ assert(reciprocal.category == fcNormal &&
+ reciprocal.significandLSB() == reciprocal.semantics->precision - 1);
+
if (inv)
*inv = reciprocal;
diff --git a/test/Transforms/InstCombine/fdiv.ll b/test/Transforms/InstCombine/fdiv.ll
index f4e0b48411..a2cce016e1 100644
--- a/test/Transforms/InstCombine/fdiv.ll
+++ b/test/Transforms/InstCombine/fdiv.ll
@@ -13,7 +13,7 @@ define float @test2(float %x) nounwind readnone ssp {
ret float %div
; CHECK: @test2
-; CHECK-NEXT: fmul float %x, 0x3800000000000000
+; CHECK-NEXT: fdiv float %x, 0x47E0000000000000
}
define float @test3(float %x) nounwind readnone ssp {
diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp
index dea4a65b88..5f05b86169 100644
--- a/unittests/ADT/APFloatTest.cpp
+++ b/unittests/ADT/APFloatTest.cpp
@@ -589,10 +589,8 @@ TEST(APFloatTest, exactInverse) {
EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
- // Large float
- EXPECT_TRUE(APFloat(1.7014118e38f).getExactInverse(&inv));
- EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(5.8774718e-39f)));
-
+ // Large float, inverse is a denormal.
+ EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(0));
// Zero
EXPECT_FALSE(APFloat(0.0).getExactInverse(0));
// Denormalized float