AsmPrinter: Simplify emitting FP elements in sequential data. NFC
authorJustin Bogner <mail@justinbogner.com>
Thu, 3 Dec 2015 23:28:35 +0000 (23:28 +0000)
committerJustin Bogner <mail@justinbogner.com>
Thu, 3 Dec 2015 23:28:35 +0000 (23:28 +0000)
Use APFloat APIs here Rather than manually type-punning through
unions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254664 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/AsmPrinter.cpp

index 9ffd830a9f58f1b2b96ccc331583bb9f55b9c705..2cfea650872a5cb8f75532caa1c593540d1610e9 100644 (file)
@@ -1945,33 +1945,22 @@ static void emitGlobalConstantDataSequential(const DataLayout &DL,
       AP.OutStreamer->EmitIntValue(CDS->getElementAsInteger(i),
                                    ElementByteSize);
     }
-  } else if (ElementByteSize == 4) {
-    // FP Constants are printed as integer constants to avoid losing
-    // precision.
-    assert(CDS->getElementType()->isFloatTy());
-    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
-      union {
-        float F;
-        uint32_t I;
-      };
-
-      F = CDS->getElementAsFloat(i);
-      if (AP.isVerbose())
-        AP.OutStreamer->GetCommentOS() << "float " << F << '\n';
-      AP.OutStreamer->EmitIntValue(I, 4);
-    }
   } else {
-    assert(CDS->getElementType()->isDoubleTy());
-    for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
-      union {
-        double F;
-        uint64_t I;
-      };
-
-      F = CDS->getElementAsDouble(i);
-      if (AP.isVerbose())
-        AP.OutStreamer->GetCommentOS() << "double " << F << '\n';
-      AP.OutStreamer->EmitIntValue(I, 8);
+    // FP Constants are printed as integer constants to avoid losing precision.
+    for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
+      APFloat Num = CDS->getElementAsAPFloat(I);
+      if (AP.isVerbose()) {
+        if (ElementByteSize == 4)
+          AP.OutStreamer->GetCommentOS() << "float " << Num.convertToFloat()
+                                         << '\n';
+        else if (ElementByteSize == 8)
+          AP.OutStreamer->GetCommentOS() << "double " << Num.convertToDouble()
+                                         << '\n';
+        else
+          llvm_unreachable("Unexpected float width");
+      }
+      AP.OutStreamer->EmitIntValue(Num.bitcastToAPInt().getLimitedValue(),
+                                   ElementByteSize);
     }
   }