aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-01-29 06:43:41 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-01-29 06:43:41 +0000
commit4087e24f73d05d96ac2d259679751d054d3ddfbc (patch)
treef8687fd2865ff9a9f91a166200dbf344ce92399e
parent107dc76180733ecf04848a47299748f9755cdb62 (diff)
Evaluate ==,!= for complex types.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63280 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/AST/ExprConstant.cpp44
-rw-r--r--test/CodeGen/const-init.c31
2 files changed, 72 insertions, 3 deletions
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index eae3f64ac9..9798520507 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -729,6 +729,50 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
QualType LHSTy = E->getLHS()->getType();
QualType RHSTy = E->getRHS()->getType();
+
+ if (LHSTy->isAnyComplexType()) {
+ assert(RHSTy->isAnyComplexType() && "Invalid comparison");
+ APValue LHS, RHS;
+
+ if (!EvaluateComplex(E->getLHS(), LHS, Info))
+ return false;
+
+ if (!EvaluateComplex(E->getRHS(), RHS, Info))
+ return false;
+
+ if (LHS.isComplexFloat()) {
+ APFloat::cmpResult CR_r =
+ LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
+ APFloat::cmpResult CR_i =
+ LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
+
+ Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
+ if (E->getOpcode() == BinaryOperator::EQ)
+ Result = (CR_r == APFloat::cmpEqual &&
+ CR_i == APFloat::cmpEqual);
+ else if (E->getOpcode() == BinaryOperator::NE)
+ Result = ((CR_r == APFloat::cmpGreaterThan ||
+ CR_r == APFloat::cmpLessThan) &&
+ (CR_i == APFloat::cmpGreaterThan ||
+ CR_i == APFloat::cmpLessThan));
+ else
+ assert(0 && "Invalid complex compartison.");
+ Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
+ return true;
+ } else {
+ Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
+ if (E->getOpcode() == BinaryOperator::EQ)
+ Result = (LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
+ LHS.getComplexIntImag() == RHS.getComplexIntImag());
+ else if (E->getOpcode() == BinaryOperator::NE)
+ Result = (LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
+ LHS.getComplexIntImag() != RHS.getComplexIntImag());
+ else
+ assert(0 && "Invalid complex compartison.");
+ Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
+ return true;
+ }
+ }
if (LHSTy->isRealFloatingType() &&
RHSTy->isRealFloatingType()) {
diff --git a/test/CodeGen/const-init.c b/test/CodeGen/const-init.c
index 3aabcfe608..c98cfd4ca2 100644
--- a/test/CodeGen/const-init.c
+++ b/test/CodeGen/const-init.c
@@ -1,4 +1,4 @@
-// RUN: clang -verify -emit-llvm -o %t %s
+// RUN: clang -verify -emit-llvm -o %t %s &&
#include <stdint.h>
@@ -22,5 +22,30 @@ union s2 {
int g0 = (int)(&(((union s2 *) 0)->f0.f0) - 0);
-_Complex int g1 = 1 + 10i;
-_Complex double g2 = 1.0 + 10.0i;
+// RUN: grep '@g1x = global { double, double } { double 1.000000e+00, double 0.000000e+00 }' %t &&
+_Complex double g1x = 1.0f;
+// RUN: grep '@g1y = global { double, double } { double 0.000000e+00, double 1.000000e+00 }' %t &&
+_Complex double g1y = 1.0fi;
+// RUN: grep '@g1 = global { i8, i8 } { i8 1, i8 10 }' %t &&
+_Complex char g1 = (char) 1 + (char) 10 * 1i;
+// RUN: grep '@g2 = global { i32, i32 } { i32 1, i32 10 }' %t &&
+_Complex int g2 = 1 + 10i;
+// RUN: grep '@g3 = global { float, float } { float 1.000000e+00, float 1.000000e+01 }' %t &&
+_Complex float g3 = 1.0 + 10.0i;
+// RUN: grep '@g4 = global { double, double } { double 1.000000e+00, double 1.000000e+01 }' %t &&
+_Complex double g4 = 1.0 + 10.0i;
+// RUN: grep '@g5 = global { i32, i32 } zeroinitializer' %t &&
+_Complex int g5 = (2 + 3i) == (5 + 7i);
+// RUN: grep '@g6 = global { double, double } { double -1.100000e+01, double 2.900000e+01 }' %t &&
+_Complex double g6 = (2.0 + 3.0i) * (5.0 + 7.0i);
+// RUN: grep '@g7 = global i32 1' %t &&
+int g7 = (2 + 3i) * (5 + 7i) == (-11 + 29i);
+// RUN: grep '@g8 = global i32 1' %t &&
+int g8 = (2.0 + 3.0i) * (5.0 + 7.0i) == (-11.0 + 29.0i);
+// RUN: grep '@g9 = global i32 0' %t &&
+int g9 = (2 + 3i) * (5 + 7i) != (-11 + 29i);
+// RUN: grep '@g10 = global i32 0' %t &&
+int g10 = (2.0 + 3.0i) * (5.0 + 7.0i) != (-11.0 + 29.0i);
+
+
+// RUN: true