[cfe-commits] r39841 - in /cfe/trunk: CodeGen/CGExpr.cpp test/CodeGen/complex.c

Gabor Greif ggreif at gmail.com
Fri Jul 13 16:33:19 PDT 2007


Author: ggreif
Date: Fri Jul 13 18:33:18 2007
New Revision: 39841

URL: http://llvm.org/viewvc/llvm-project?rev=39841&view=rev
Log:
implement _Complex * == and !=

Modified:
    cfe/trunk/CodeGen/CGExpr.cpp
    cfe/trunk/test/CodeGen/complex.c

Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=39841&r1=39840&r2=39841&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Fri Jul 13 18:33:18 2007
@@ -1070,7 +1070,23 @@
   if (LHS.isScalar())
     return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
   
-  assert(0 && "FIXME: This doesn't handle complex operands yet");
+  // Otherwise, this must be a complex number.
+  llvm::Value *LHSR, *LHSI, *RHSR, *RHSI;
+  
+  EmitLoadOfComplex(LHS, LHSR, LHSI);
+  EmitLoadOfComplex(RHS, RHSR, RHSI);
+  
+  llvm::Value *ResRl = Builder.CreateMul(LHSR, RHSR, "mul.rl");
+  llvm::Value *ResRr = Builder.CreateMul(LHSI, RHSI, "mul.rr");
+  llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
+
+  llvm::Value *ResIl = Builder.CreateMul(LHSI, RHSR, "mul.il");
+  llvm::Value *ResIr = Builder.CreateMul(LHSR, RHSI, "mul.ir");
+  llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
+  
+  llvm::Value *Res = CreateTempAlloca(ConvertType(ResTy));
+  EmitStoreOfComplex(ResR, ResI, Res);
+  return RValue::getAggregate(Res);
 }
 
 RValue CodeGenFunction::EmitDiv(RValue LHS, RValue RHS, QualType ResTy) {
@@ -1231,9 +1247,23 @@
     }
   } else {
     // Struct/union/complex
-    assert(0 && "Aggregate comparisons not implemented yet!");
+    llvm::Value *LHSR, *LHSI, *RHSR, *RHSI, *ResultR, *ResultI;
+    EmitLoadOfComplex(LHS, LHSR, LHSI);
+    EmitLoadOfComplex(RHS, RHSR, RHSI);
+
+    ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
+				 LHSR, RHSR, "cmp.r");
+    ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
+				 LHSI, RHSI, "cmp.i");
+    if (BinaryOperator::EQ == E->getOpcode()) {
+      Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
+    } else if (BinaryOperator::NE == E->getOpcode()) {
+      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
+    } else {
+      assert(0 && "Complex comparison other than == or != ?");
+    }
   }
-  
+
   // ZExt result to int.
   return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
 }

Modified: cfe/trunk/test/CodeGen/complex.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/complex.c?rev=39841&r1=39840&r2=39841&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/complex.c (original)
+++ cfe/trunk/test/CodeGen/complex.c Fri Jul 13 18:33:18 2007
@@ -6,5 +6,5 @@
   double _Complex a = 5;
   double _Complex b = 42;
 
-  return a * b == 123;
+  return a * b == b * a;
 }





More information about the cfe-commits mailing list