Fix APFloat::getLargest so that it actually returns the correct value. Found by...
authorEli Friedman <eli.friedman@gmail.com>
Wed, 12 Oct 2011 21:51:36 +0000 (21:51 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 12 Oct 2011 21:51:36 +0000 (21:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141816 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APFloat.cpp
unittests/ADT/APFloatTest.cpp

index 5307829ed83b2208eb19006debb701c5a3a2c45e..95df8617fb5c4ead5a87c1ad4c73df0c0427c343 100644 (file)
@@ -3243,8 +3243,9 @@ APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
     significand[i] = ~((integerPart) 0);
 
   // ...and then clear the top bits for internal consistency.
-  significand[N-1] &=
-    (((integerPart) 1) << ((Sem.precision % integerPartWidth) - 1)) - 1;
+  if (Sem.precision % integerPartWidth != 0)
+    significand[N-1] &=
+      (((integerPart) 1) << (Sem.precision % integerPartWidth)) - 1;
 
   return Val;
 }
index 3c40a13d3677b4d282228f5fe35aef94b768ff8a..e70c5f7af2b3a7df3c7c955a065c37ba7611978b 100644 (file)
@@ -648,4 +648,9 @@ TEST(APFloatTest, exactInverse) {
   EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(0));
 }
 
+TEST(APFloatTest, getLargest) {
+  EXPECT_EQ(3.40282347e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
+  EXPECT_EQ(1.7976931348623157e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
+}
+
 }