[cfe-commits] r59408 - /cfe/trunk/lib/AST/ExprConstant.cpp

Anders Carlsson andersca at mac.com
Sat Nov 15 23:17:21 PST 2008


Author: andersca
Date: Sun Nov 16 01:17:21 2008
New Revision: 59408

URL: http://llvm.org/viewvc/llvm-project?rev=59408&view=rev
Log:
Add the ability to evaluate comparison operators with floating point numbers as operands.

Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=59408&r1=59407&r2=59408&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sun Nov 16 01:17:21 2008
@@ -529,8 +529,51 @@
     return false;
   }
 
-  if (!E->getLHS()->getType()->isIntegralType() ||
-      !E->getRHS()->getType()->isIntegralType()) {
+  QualType LHSTy = E->getLHS()->getType();
+  QualType RHSTy = E->getRHS()->getType();
+  
+  if (LHSTy->isRealFloatingType() &&
+      RHSTy->isRealFloatingType()) {
+    APFloat RHS(0.0), LHS(0.0);
+    
+    if (!EvaluateFloat(E->getRHS(), RHS, Info))
+      return false;
+    
+    if (!EvaluateFloat(E->getLHS(), LHS, Info))
+      return false;
+    
+    APFloat::cmpResult CR = LHS.compare(RHS);
+    
+    switch (E->getOpcode()) {
+    default:
+      assert(0 && "Invalid binary operator!");
+    case BinaryOperator::LT:
+      Result = CR == APFloat::cmpLessThan;
+      break;
+    case BinaryOperator::GT:
+      Result = CR == APFloat::cmpGreaterThan;
+      break;
+    case BinaryOperator::LE:
+      Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
+      break;
+    case BinaryOperator::GE:
+      Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
+      break;
+    case BinaryOperator::EQ:
+      Result = CR == APFloat::cmpEqual;
+      break;
+    case BinaryOperator::NE:
+      Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
+      break;
+    }
+    
+    Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
+    Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
+    return true;
+  }
+  
+  if (!LHSTy->isIntegralType() ||
+      !RHSTy->isIntegralType()) {
     // We can't continue from here for non-integral types, and they
     // could potentially confuse the following operations.
     // FIXME: Deal with EQ and friends.





More information about the cfe-commits mailing list