[llvm] r254664 - AsmPrinter: Simplify emitting FP elements in sequential data. NFC

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 3 15:28:36 PST 2015


Author: bogner
Date: Thu Dec  3 17:28:35 2015
New Revision: 254664

URL: http://llvm.org/viewvc/llvm-project?rev=254664&view=rev
Log:
AsmPrinter: Simplify emitting FP elements in sequential data. NFC

Use APFloat APIs here Rather than manually type-punning through
unions.

Modified:
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=254664&r1=254663&r2=254664&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Thu Dec  3 17:28:35 2015
@@ -1945,33 +1945,22 @@ static void emitGlobalConstantDataSequen
       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);
     }
   }
 




More information about the llvm-commits mailing list