Constant fold int-to-long-double conversions;
authorDale Johannesen <dalej@apple.com>
Sun, 30 Sep 2007 18:19:03 +0000 (18:19 +0000)
committerDale Johannesen <dalej@apple.com>
Sun, 30 Sep 2007 18:19:03 +0000 (18:19 +0000)
use APFloat for int-to-float/double; use
round-to-nearest for these (implementation-defined,
seems to match gcc).

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

lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
lib/CodeGen/SelectionDAG/SelectionDAG.cpp
lib/ExecutionEngine/ExecutionEngine.cpp
lib/VMCore/ConstantFold.cpp

index 2fc8b0a7b9d1fffaa845cfd3e6c9721b1dc8e344..7f2b6e7630a1033d4b54b44dbe84789184b4fb4f 100644 (file)
@@ -3220,7 +3220,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
           APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
           uint64_t x = 1ULL << ShiftAmt;
           (void)apf.convertFromInteger(&x, MVT::getSizeInBits(NVT), false, 
-                                       APFloat::rmTowardZero);
+                                       APFloat::rmNearestTiesToEven);
           Tmp2 = DAG.getConstantFP(apf, VT);
           Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
                             Node->getOperand(0), Tmp2, ISD::SETLT);
index 042868d7bb1ff8390112bb2056b3fd4e8a365415..e286eb0ec5bb94d6c25f19bc15c3d2bcbd0f6b85 100644 (file)
@@ -1598,7 +1598,7 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
       (void)apf.convertFromInteger(&Val, 
                                MVT::getSizeInBits(Operand.getValueType()), 
                                Opcode==ISD::SINT_TO_FP,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
       return getConstantFP(apf, VT);
     }
     case ISD::BIT_CONVERT:
index c72663e0d4d436414bfbc9aff8dd12ed22a5865f..61be35097b9a995e6974b7dfdba59c8c016aeb2b 100644 (file)
@@ -398,7 +398,7 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
         APFloat apf = APFloat(APInt(80, 2, zero));
         (void)apf.convertFromInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), false,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -414,7 +414,7 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
         APFloat apf = APFloat(APInt(80, 2, zero));
         (void)apf.convertFromInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), true,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
index 73ca47a9aa56c565f3e69b5e19d2c698d15fcb10..72077db378077fc577d3a3bac088a290dd8e82a6 100644 (file)
@@ -209,25 +209,17 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
       return ConstantInt::get(DestTy, 0);
     return 0;                   // Other pointer types cannot be casted
   case Instruction::UIToFP:
-    if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
-      double d = CI->getValue().roundToDouble();
-      if (DestTy==Type::FloatTy) 
-        return ConstantFP::get(DestTy, APFloat((float)d));
-      else if (DestTy==Type::DoubleTy)
-        return ConstantFP::get(DestTy, APFloat(d));
-      else
-        return 0;     // FIXME do this for long double
-    }
-    return 0;
   case Instruction::SIToFP:
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
-      double d = CI->getValue().signedRoundToDouble();
-      if (DestTy==Type::FloatTy)
-        return ConstantFP::get(DestTy, APFloat((float)d));
-      else if (DestTy==Type::DoubleTy)
-        return ConstantFP::get(DestTy, APFloat(d));
-      else
-        return 0;     // FIXME do this for long double
+      APInt api = CI->getValue();
+      const uint64_t zero[] = {0, 0};
+      uint32_t BitWidth = cast<IntegerType>(SrcTy)->getBitWidth();
+      APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
+                                  2, zero));
+      (void)apf.convertFromInteger(api.getRawData(), BitWidth, 
+                                   opc==Instruction::SIToFP,
+                                   APFloat::rmNearestTiesToEven);
+      return ConstantFP::get(DestTy, apf);
     }
     return 0;
   case Instruction::ZExt: