Culling out use of unions for converting FP to bits and vice versa.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22838 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index b29187b..fc87afd 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -32,6 +32,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
@@ -585,14 +586,10 @@
const unsigned long SignalNaN = 0x7ff4UL;
// We need to grab the first part of the FP #
- union {
- double d;
- uint64_t ll;
- } DHex;
char Buffer[100];
- DHex.d = FPC->getValue();
- sprintf(Buffer, "0x%llx", (unsigned long long)DHex.ll);
+ uint64_t ll = DoubleToBits(FPC->getValue());
+ sprintf(Buffer, "0x%llx", (unsigned long long)ll);
std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16);
@@ -953,16 +950,6 @@
/// Output all floating point constants that cannot be printed accurately...
void CWriter::printFloatingPointConstants(Function &F) {
- union {
- double D;
- uint64_t U;
- } DBLUnion;
-
- union {
- float F;
- unsigned U;
- } FLTUnion;
-
// Scan the module for floating point constants. If any FP constant is used
// in the function, we want to redirect it here so that we do not depend on
// the precision of the printed form, unless the printed form preserves
@@ -979,14 +966,12 @@
FPConstantMap[FPC] = FPCounter; // Number the FP constants
if (FPC->getType() == Type::DoubleTy) {
- DBLUnion.D = Val;
Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
- << " = 0x" << std::hex << DBLUnion.U << std::dec
+ << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
<< "ULL; /* " << Val << " */\n";
} else if (FPC->getType() == Type::FloatTy) {
- FLTUnion.F = Val;
Out << "static const ConstantFloatTy FPConstant" << FPCounter++
- << " = 0x" << std::hex << FLTUnion.U << std::dec
+ << " = 0x" << std::hex << FloatToBits(Val) << std::dec
<< "U; /* " << Val << " */\n";
} else
assert(0 && "Unknown float type!");
diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp
index b29187b..fc87afd 100644
--- a/lib/Target/CBackend/Writer.cpp
+++ b/lib/Target/CBackend/Writer.cpp
@@ -32,6 +32,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
@@ -585,14 +586,10 @@
const unsigned long SignalNaN = 0x7ff4UL;
// We need to grab the first part of the FP #
- union {
- double d;
- uint64_t ll;
- } DHex;
char Buffer[100];
- DHex.d = FPC->getValue();
- sprintf(Buffer, "0x%llx", (unsigned long long)DHex.ll);
+ uint64_t ll = DoubleToBits(FPC->getValue());
+ sprintf(Buffer, "0x%llx", (unsigned long long)ll);
std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16);
@@ -953,16 +950,6 @@
/// Output all floating point constants that cannot be printed accurately...
void CWriter::printFloatingPointConstants(Function &F) {
- union {
- double D;
- uint64_t U;
- } DBLUnion;
-
- union {
- float F;
- unsigned U;
- } FLTUnion;
-
// Scan the module for floating point constants. If any FP constant is used
// in the function, we want to redirect it here so that we do not depend on
// the precision of the printed form, unless the printed form preserves
@@ -979,14 +966,12 @@
FPConstantMap[FPC] = FPCounter; // Number the FP constants
if (FPC->getType() == Type::DoubleTy) {
- DBLUnion.D = Val;
Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
- << " = 0x" << std::hex << DBLUnion.U << std::dec
+ << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
<< "ULL; /* " << Val << " */\n";
} else if (FPC->getType() == Type::FloatTy) {
- FLTUnion.F = Val;
Out << "static const ConstantFloatTy FPConstant" << FPCounter++
- << " = 0x" << std::hex << FLTUnion.U << std::dec
+ << " = 0x" << std::hex << FloatToBits(Val) << std::dec
<< "U; /* " << Val << " */\n";
} else
assert(0 && "Unknown float type!");
diff --git a/lib/Target/Sparc/SparcAsmPrinter.cpp b/lib/Target/Sparc/SparcAsmPrinter.cpp
index c8852cc..4e908a1 100644
--- a/lib/Target/Sparc/SparcAsmPrinter.cpp
+++ b/lib/Target/Sparc/SparcAsmPrinter.cpp
@@ -247,22 +247,12 @@
switch (CFP->getType()->getTypeID()) {
default: assert(0 && "Unknown floating point type!");
case Type::FloatTyID: {
- union FU { // Abide by C TBAA rules
- float FVal;
- unsigned UVal;
- } U;
- U.FVal = Val;
- O << ".long\t" << U.UVal << "\t! float " << Val << "\n";
+ O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
return;
}
case Type::DoubleTyID: {
- union DU { // Abide by C TBAA rules
- double FVal;
- uint64_t UVal;
- } U;
- U.FVal = Val;
- O << ".word\t0x" << std::hex << (U.UVal >> 32) << std::dec << "\t! double " << Val << "\n";
- O << ".word\t0x" << std::hex << (U.UVal & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
+ O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
+ O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
return;
}
}
diff --git a/lib/Target/SparcV8/SparcV8AsmPrinter.cpp b/lib/Target/SparcV8/SparcV8AsmPrinter.cpp
index c8852cc..4e908a1 100644
--- a/lib/Target/SparcV8/SparcV8AsmPrinter.cpp
+++ b/lib/Target/SparcV8/SparcV8AsmPrinter.cpp
@@ -247,22 +247,12 @@
switch (CFP->getType()->getTypeID()) {
default: assert(0 && "Unknown floating point type!");
case Type::FloatTyID: {
- union FU { // Abide by C TBAA rules
- float FVal;
- unsigned UVal;
- } U;
- U.FVal = Val;
- O << ".long\t" << U.UVal << "\t! float " << Val << "\n";
+ O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
return;
}
case Type::DoubleTyID: {
- union DU { // Abide by C TBAA rules
- double FVal;
- uint64_t UVal;
- } U;
- U.FVal = Val;
- O << ".word\t0x" << std::hex << (U.UVal >> 32) << std::dec << "\t! double " << Val << "\n";
- O << ".word\t0x" << std::hex << (U.UVal & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
+ O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
+ O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
return;
}
}