A couple of minor code cleanups.
authorChris Lattner <sabre@nondot.org>
Sun, 5 Oct 2003 00:40:51 +0000 (00:40 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 5 Oct 2003 00:40:51 +0000 (00:40 +0000)
Print literal doubles using ftostr instead of <<, because it yields higher precision numbers.

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

lib/Target/CBackend/CBackend.cpp
lib/Target/CBackend/Writer.cpp

index 16d55feb47d2680a5a4f2b53ff3a7254d9746b11..72479ee34d7709fa80b61fce56ed52a06283a762 100644 (file)
@@ -152,7 +152,7 @@ namespace {
 
 // A pointer type should not use parens around *'s alone, e.g., (**)
 inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
-  return (NameSoFar.find_last_not_of('*') != std::string::npos);
+  return NameSoFar.find_last_not_of('*') != std::string::npos;
 }
 
 // Pass the Type* and the variable name and this prints out the variable
@@ -327,25 +327,23 @@ void CWriter::printConstantArray(ConstantArray *CPA) {
   }
 }
 
-/// FPCSafeToPrint - Returns true if we may assume that CFP may be
-/// written out textually as a double (rather than as a reference to a
-/// stack-allocated variable). We decide this by converting CFP to a
-/// string and back into a double, and then checking whether the
-/// conversion results in a bit-equal double to the original value of
-/// CFP. This depends on us and the target C compiler agreeing on the
-/// conversion process (which is pretty likely since we only deal in
-/// IEEE FP.) This is adapted from similar code in
-/// lib/VMCore/AsmWriter.cpp:WriteConstantInt().
-static bool FPCSafeToPrint (const ConstantFP *CFP) {
+// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
+// textually as a double (rather than as a reference to a stack-allocated
+// variable). We decide this by converting CFP to a string and back into a
+// double, and then checking whether the conversion results in a bit-equal
+// double to the original value of CFP. This depends on us and the target C
+// compiler agreeing on the conversion process (which is pretty likely since we
+// only deal in IEEE FP).
+//
+static bool isFPCSafeToPrint(const ConstantFP *CFP) {
   std::string StrVal = ftostr(CFP->getValue());
-  // Check to make sure that the stringized number is not some string like
-  // "Inf" or NaN, that atof will accept, but the lexer will not.  Check that
-  // the string matches the "[-+]?[0-9]" regex.
+  // Check to make sure that the stringized number is not some string like "Inf"
+  // or NaN.  Check that the string matches the "[-+]?[0-9]" regex.
   if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
       ((StrVal[0] == '-' || StrVal[0] == '+') &&
        (StrVal[1] >= '0' && StrVal[1] <= '9')))
     // Reparse stringized version!
-    return (atof(StrVal.c_str()) == CFP->getValue());
+    return atof(StrVal.c_str()) == CFP->getValue();
   return false;
 }
 
@@ -439,11 +437,8 @@ void CWriter::printConstant(Constant *CPV) {
       Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
           << "*)&FloatConstant" << I->second << ")";
     } else {
-      if (FPCSafeToPrint (FPC)) {
-        Out << ftostr (FPC->getValue ());
-      } else {
-        Out << FPC->getValue(); // Who knows? Give it our best shot...
-      }
+      // Print out the constant as a floating point number.
+      Out << ftostr(FPC->getValue());
     }
     break;
   }
@@ -860,7 +855,7 @@ void CWriter::printFunction(Function *F) {
   unsigned FPCounter = 0;
   for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
-      if ((!FPCSafeToPrint(FPC)) // Do not put in FPConstantMap if safe.
+      if ((!isFPCSafeToPrint(FPC)) // Do not put in FPConstantMap if safe.
          && (FPConstantMap.find(FPC) == FPConstantMap.end())) {
         double Val = FPC->getValue();
         
index 16d55feb47d2680a5a4f2b53ff3a7254d9746b11..72479ee34d7709fa80b61fce56ed52a06283a762 100644 (file)
@@ -152,7 +152,7 @@ namespace {
 
 // A pointer type should not use parens around *'s alone, e.g., (**)
 inline bool ptrTypeNameNeedsParens(const std::string &NameSoFar) {
-  return (NameSoFar.find_last_not_of('*') != std::string::npos);
+  return NameSoFar.find_last_not_of('*') != std::string::npos;
 }
 
 // Pass the Type* and the variable name and this prints out the variable
@@ -327,25 +327,23 @@ void CWriter::printConstantArray(ConstantArray *CPA) {
   }
 }
 
-/// FPCSafeToPrint - Returns true if we may assume that CFP may be
-/// written out textually as a double (rather than as a reference to a
-/// stack-allocated variable). We decide this by converting CFP to a
-/// string and back into a double, and then checking whether the
-/// conversion results in a bit-equal double to the original value of
-/// CFP. This depends on us and the target C compiler agreeing on the
-/// conversion process (which is pretty likely since we only deal in
-/// IEEE FP.) This is adapted from similar code in
-/// lib/VMCore/AsmWriter.cpp:WriteConstantInt().
-static bool FPCSafeToPrint (const ConstantFP *CFP) {
+// isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
+// textually as a double (rather than as a reference to a stack-allocated
+// variable). We decide this by converting CFP to a string and back into a
+// double, and then checking whether the conversion results in a bit-equal
+// double to the original value of CFP. This depends on us and the target C
+// compiler agreeing on the conversion process (which is pretty likely since we
+// only deal in IEEE FP).
+//
+static bool isFPCSafeToPrint(const ConstantFP *CFP) {
   std::string StrVal = ftostr(CFP->getValue());
-  // Check to make sure that the stringized number is not some string like
-  // "Inf" or NaN, that atof will accept, but the lexer will not.  Check that
-  // the string matches the "[-+]?[0-9]" regex.
+  // Check to make sure that the stringized number is not some string like "Inf"
+  // or NaN.  Check that the string matches the "[-+]?[0-9]" regex.
   if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
       ((StrVal[0] == '-' || StrVal[0] == '+') &&
        (StrVal[1] >= '0' && StrVal[1] <= '9')))
     // Reparse stringized version!
-    return (atof(StrVal.c_str()) == CFP->getValue());
+    return atof(StrVal.c_str()) == CFP->getValue();
   return false;
 }
 
@@ -439,11 +437,8 @@ void CWriter::printConstant(Constant *CPV) {
       Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
           << "*)&FloatConstant" << I->second << ")";
     } else {
-      if (FPCSafeToPrint (FPC)) {
-        Out << ftostr (FPC->getValue ());
-      } else {
-        Out << FPC->getValue(); // Who knows? Give it our best shot...
-      }
+      // Print out the constant as a floating point number.
+      Out << ftostr(FPC->getValue());
     }
     break;
   }
@@ -860,7 +855,7 @@ void CWriter::printFunction(Function *F) {
   unsigned FPCounter = 0;
   for (constant_iterator I = constant_begin(F), E = constant_end(F); I != E;++I)
     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
-      if ((!FPCSafeToPrint(FPC)) // Do not put in FPConstantMap if safe.
+      if ((!isFPCSafeToPrint(FPC)) // Do not put in FPConstantMap if safe.
          && (FPConstantMap.find(FPC) == FPConstantMap.end())) {
         double Val = FPC->getValue();