[PATCH] D41868: APFloat/x87: Fix string conversion for "unnormal" values (pr35860)

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 9 08:45:40 PST 2018


labath created this revision.
labath added reviewers: skatkov, scanon, gottesmm.

Unnormal values are a feature of some very old x87 processors. We handle
them correctly for the most part -- the only exception was an unnormal
value whose significand happened to be zero. In this case the APFloat
was still initialized as normal number (category = fcNormal), but a
subsequent toString operation would assert because the math would
produce nonsensical values for the zero significand.

This commit fixes this by initializing the APFloat to fcZero for these
numbers.

The issue was discovered because LLDB would crash when trying to print
some "long double" values.


https://reviews.llvm.org/D41868

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


Index: unittests/ADT/APFloatTest.cpp
===================================================================
--- unittests/ADT/APFloatTest.cpp
+++ unittests/ADT/APFloatTest.cpp
@@ -983,6 +983,13 @@
   ASSERT_EQ("8.73183400000000010e+02", convertToString(873.1834, 0, 0, false));
   ASSERT_EQ("1.79769313486231570e+308",
             convertToString(1.7976931348623157E+308, 0, 0, false));
+
+  {
+    SmallString<64> Str;
+    APFloat UnnormalZero(APFloat::x87DoubleExtended(), APInt(80, {0, 1}));
+    UnnormalZero.toString(Str);
+    ASSERT_EQ("0", Str);
+  }
 }
 
 TEST(APFloatTest, toInteger) {
Index: lib/Support/APFloat.cpp
===================================================================
--- lib/Support/APFloat.cpp
+++ lib/Support/APFloat.cpp
@@ -3045,7 +3045,9 @@
   assert(partCount()==2);
 
   sign = static_cast<unsigned int>(i2>>15);
-  if (myexponent==0 && mysignificand==0) {
+  if (myexponent != 0x7fff && mysignificand == 0) {
+    // if myexponent==0 this is a true zero
+    // if myexponent!=0 this is an unnormal value that happens to be zero
     // exponent, significand meaningless
     category = fcZero;
   } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41868.129096.patch
Type: text/x-patch
Size: 1205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180109/ac261fe8/attachment.bin>


More information about the llvm-commits mailing list