aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/APFloatTest.cpp
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2011-03-30 15:42:27 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2011-03-30 15:42:27 +0000
commit03fd67260905170cb1dabe09089c2afc51af4510 (patch)
treeafdc2b203e1876b83e944acd1a799595dad8196f /llvm/unittests/ADT/APFloatTest.cpp
parent2298dee87acd27d4c729641aba154278c71a1845 (diff)
downloadllvm-03fd67260905170cb1dabe09089c2afc51af4510.zip
llvm-03fd67260905170cb1dabe09089c2afc51af4510.tar.gz
llvm-03fd67260905170cb1dabe09089c2afc51af4510.tar.bz2
Add APFloat::getExactInverse.
The idea is, that if an ieee 754 float is divided by a power of two, we can turn the division into a cheaper multiplication. This function sees if we can get an exact multiplicative inverse for a divisor and returns it if possible. This is the hard part of PR9587. I tested many inputs against llvm-gcc's frotend implementation of this optimization and didn't find any difference. However, floating point is the land of weird edge cases, so any review would be appreciated. llvm-svn: 128545
Diffstat (limited to 'llvm/unittests/ADT/APFloatTest.cpp')
-rw-r--r--llvm/unittests/ADT/APFloatTest.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 964b04d..dea4a65 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -576,4 +576,27 @@ TEST(APFloatTest, StringHexadecimalExponentDeath) {
#endif
#endif
+TEST(APFloatTest, exactInverse) {
+ APFloat inv(0.0f);
+
+ // Trivial operation.
+ EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
+ EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
+ EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
+ EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
+
+ // FLT_MIN
+ 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)));
+
+ // Zero
+ EXPECT_FALSE(APFloat(0.0).getExactInverse(0));
+ // Denormalized float
+ EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(0));
+}
+
}