From dd76acac3f005e3ecc9a5eac8b18a1472bdaf400 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 5 Oct 2003 00:40:51 +0000 Subject: [PATCH] A couple of minor code cleanups. 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 | 37 ++++++++++++++------------------ lib/Target/CBackend/Writer.cpp | 37 ++++++++++++++------------------ 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 16d55feb47d..72479ee34d7 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -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(*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(); diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index 16d55feb47d..72479ee34d7 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -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(*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(); -- 2.34.1