Next round of APFloat changes.

Use APFloat in UpgradeParser and AsmParser.
Change all references to ConstantFP to use the
APFloat interface rather than double.  Remove
the ConstantFP double interfaces.
Use APFloat functions for constant folding arithmetic
and comparisons.
(There are still way too many places APFloat is
just a wrapper around host float/double, but we're
getting there.)


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41747 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dale Johannesen
2007-09-06 18:13:44 +00:00
parent 325be7c608
commit 43421b3dd7
32 changed files with 540 additions and 437 deletions

View File

@@ -604,17 +604,19 @@ void CWriter::printConstantVector(ConstantVector *CP) {
// only deal in IEEE FP).
//
static bool isFPCSafeToPrint(const ConstantFP *CFP) {
APFloat APF = APFloat(CFP->getValueAPF()); // copy
if (CFP->getType()==Type::FloatTy)
APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
char Buffer[100];
sprintf(Buffer, "%a", CFP->getValue());
sprintf(Buffer, "%a", APF.convertToDouble());
if (!strncmp(Buffer, "0x", 2) ||
!strncmp(Buffer, "-0x", 3) ||
!strncmp(Buffer, "+0x", 3))
return atof(Buffer) == CFP->getValue();
return APF.bitwiseIsEqual(APFloat(atof(Buffer)));
return false;
#else
std::string StrVal = ftostr(CFP->getValue());
std::string StrVal = ftostr(APF);
while (StrVal[0] == ' ')
StrVal.erase(StrVal.begin());
@@ -625,7 +627,7 @@ static bool isFPCSafeToPrint(const ConstantFP *CFP) {
((StrVal[0] == '-' || StrVal[0] == '+') &&
(StrVal[1] >= '0' && StrVal[1] <= '9')))
// Reparse stringized version!
return atof(StrVal.c_str()) == CFP->getValue();
return APF.bitwiseIsEqual(APFloat(atof(StrVal.c_str())));
return false;
#endif
}
@@ -882,9 +884,13 @@ void CWriter::printConstant(Constant *CPV) {
Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
<< "*)&FPConstant" << I->second << ')';
} else {
if (IsNAN(FPC->getValue())) {
double V = FPC->getType() == Type::FloatTy ?
FPC->getValueAPF().convertToFloat() :
FPC->getValueAPF().convertToDouble();
if (IsNAN(V)) {
// The value is NaN
// FIXME the actual NaN bits should be emitted.
// The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
// it's 0x7ff4.
const unsigned long QuietNaN = 0x7ff8UL;
@@ -893,7 +899,7 @@ void CWriter::printConstant(Constant *CPV) {
// We need to grab the first part of the FP #
char Buffer[100];
uint64_t ll = DoubleToBits(FPC->getValue());
uint64_t ll = DoubleToBits(V);
sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
std::string Num(&Buffer[0], &Buffer[6]);
@@ -905,9 +911,9 @@ void CWriter::printConstant(Constant *CPV) {
else
Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
<< Buffer << "\") /*nan*/ ";
} else if (IsInf(FPC->getValue())) {
} else if (IsInf(V)) {
// The value is Inf
if (FPC->getValue() < 0) Out << '-';
if (V < 0) Out << '-';
Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "")
<< " /*inf*/ ";
} else {
@@ -915,12 +921,12 @@ void CWriter::printConstant(Constant *CPV) {
#if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
// Print out the constant as a floating point number.
char Buffer[100];
sprintf(Buffer, "%a", FPC->getValue());
sprintf(Buffer, "%a", V);
Num = Buffer;
#else
Num = ftostr(FPC->getValue());
Num = ftostr(FPC->getValueAPF());
#endif
Out << Num;
Out << Num;
}
}
break;
@@ -1715,15 +1721,15 @@ void CWriter::printFloatingPointConstants(Function &F) {
if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
!FPConstantMap.count(FPC)) {
double Val = FPC->getValue();
FPConstantMap[FPC] = FPCounter; // Number the FP constants
if (FPC->getType() == Type::DoubleTy) {
double Val = FPC->getValueAPF().convertToDouble();
Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << DoubleToBits(Val) << std::dec
<< "ULL; /* " << Val << " */\n";
} else if (FPC->getType() == Type::FloatTy) {
float Val = FPC->getValueAPF().convertToFloat();
Out << "static const ConstantFloatTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << FloatToBits(Val) << std::dec
<< "U; /* " << Val << " */\n";

View File

@@ -428,10 +428,10 @@ void MSILWriter::printConstLoad(const Constant* C) {
uint64_t X;
unsigned Size;
if (FP->getType()->getTypeID()==Type::FloatTyID) {
X = FloatToBits(FP->getValue());
X = FloatToBits(FP->getValueAPF().convertToFloat());
Size = 4;
} else {
X = DoubleToBits(FP->getValue());
X = DoubleToBits(FP->getValueAPF().convertToDouble());
Size = 8;
}
Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')';
@@ -1472,9 +1472,11 @@ void MSILWriter::printStaticConstant(const Constant* C, uint64_t& Offset) {
TySize = TD->getTypeSize(Ty);
const ConstantFP* FP = cast<ConstantFP>(C);
if (Ty->getTypeID() == Type::FloatTyID)
Out << "int32 (" << FloatToBits(FP->getValue()) << ')';
Out << "int32 (" <<
FloatToBits(FP->getValueAPF().convertToFloat()) << ')';
else
Out << "int64 (" << DoubleToBits(FP->getValue()) << ')';
Out << "int64 (" <<
DoubleToBits(FP->getValueAPF().convertToDouble()) << ')';
break;
}
case Type::ArrayTyID:

View File

@@ -3412,11 +3412,11 @@ SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
const Type *OpNTy = MVT::getTypeForValueType(EltVT);
std::vector<Constant*> CV;
if (EltVT == MVT::f64) {
Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)));
Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToDouble(~(1ULL << 63))));
CV.push_back(C);
CV.push_back(C);
} else {
Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)));
Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToFloat(~(1U << 31))));
CV.push_back(C);
CV.push_back(C);
CV.push_back(C);
@@ -3440,11 +3440,11 @@ SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
const Type *OpNTy = MVT::getTypeForValueType(EltVT);
std::vector<Constant*> CV;
if (EltVT == MVT::f64) {
Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63));
Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToDouble(1ULL << 63)));
CV.push_back(C);
CV.push_back(C);
} else {
Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31));
Constant *C = ConstantFP::get(OpNTy, APFloat(BitsToFloat(1U << 31)));
CV.push_back(C);
CV.push_back(C);
CV.push_back(C);
@@ -3475,18 +3475,19 @@ SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
SrcVT = VT;
SrcTy = MVT::getTypeForValueType(SrcVT);
}
// First get the sign bit of second operand.
std::vector<Constant*> CV;
if (SrcVT == MVT::f64) {
CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToDouble(1ULL << 63))));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0)));
} else {
CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToFloat(1U << 31))));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
}
Constant *C = ConstantVector::get(CV);
SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
@@ -3508,13 +3509,13 @@ SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
// Clear first operand sign bit.
CV.clear();
if (VT == MVT::f64) {
CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToDouble(~(1ULL << 63)))));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0)));
} else {
CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, 0.0));
CV.push_back(ConstantFP::get(SrcTy, APFloat(BitsToFloat(~(1U << 31)))));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
CV.push_back(ConstantFP::get(SrcTy, APFloat(0.0f)));
}
C = ConstantVector::get(CV);
CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);