[llvm-commits] [polly] r150673 - /polly/trunk/lib/CodeGeneration.cpp

Tobias Grosser grosser at fim.uni-passau.de
Thu Feb 16 01:56:14 PST 2012


Author: grosser
Date: Thu Feb 16 03:56:14 2012
New Revision: 150673

URL: http://llvm.org/viewvc/llvm-project?rev=150673&view=rev
Log:
CodeGeneration: Implement ceild/floord exactly as CLooG does

Modified:
    polly/trunk/lib/CodeGeneration.cpp

Modified: polly/trunk/lib/CodeGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGeneration.cpp?rev=150673&r1=150672&r2=150673&view=diff
==============================================================================
--- polly/trunk/lib/CodeGeneration.cpp (original)
+++ polly/trunk/lib/CodeGeneration.cpp Thu Feb 16 03:56:14 2012
@@ -802,25 +802,43 @@
     return Builder.CreateSRem(LHS, RHS);
   case clast_bin_fdiv:
     {
-      // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
+      // floord(N,D) ((N<0) ? -((-N+D-1)/D) : N/D)
+      Value *N = LHS;
+      Value *D = RHS;
+
       Value *One = ConstantInt::get(Ty, 1);
       Value *Zero = ConstantInt::get(Ty, 0);
-      Value *Sum1 = Builder.CreateSub(LHS, RHS);
-      Value *Sum2 = Builder.CreateAdd(Sum1, One);
-      Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
-      Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
-      return Builder.CreateSDiv(Dividend, RHS);
+
+      Value *NegativeCase = Builder.CreateNeg(N);
+      NegativeCase = Builder.CreateAdd(NegativeCase, D);
+      NegativeCase = Builder.CreateSub(NegativeCase, One);
+      NegativeCase = Builder.CreateSDiv(NegativeCase, NegativeCase);
+
+      Value *PositiveCase = Builder.CreateSDiv(N, D);
+
+      Value *IsNegative = Builder.CreateICmpSLT(N, Zero);
+      return Builder.CreateSelect(IsNegative, NegativeCase, PositiveCase);
+
     }
   case clast_bin_cdiv:
     {
-      // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d
-      Value *One = ConstantInt::get(Ty, 1);
+      // ceild(N,D)  ( (N<0) ? -((-N)/D) : (N+D-1)/D)
+      Value *N = LHS;
+      Value *D = RHS;
+
       Value *Zero = ConstantInt::get(Ty, 0);
-      Value *Sum1 = Builder.CreateAdd(LHS, RHS);
-      Value *Sum2 = Builder.CreateSub(Sum1, One);
-      Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
-      Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2);
-      return Builder.CreateSDiv(Dividend, RHS);
+      Value *MinusOne = ConstantInt::getSigned(Ty, -1);
+
+      Value *NegativeCase = Builder.CreateNeg(N);
+      NegativeCase = Builder.CreateSDiv(NegativeCase, D);
+      NegativeCase = Builder.CreateNeg(NegativeCase);
+
+      Value *PositiveCase = Builder.CreateAdd(N, D);
+      PositiveCase = Builder.CreateAdd(PositiveCase, MinusOne);
+      PositiveCase = Builder.CreateSDiv(PositiveCase, D);
+
+      Value *IsNegative = Builder.CreateICmpSLT(N, Zero);
+      return Builder.CreateSelect(IsNegative, NegativeCase, PositiveCase);
     }
   case clast_bin_div:
     return Builder.CreateSDiv(LHS, RHS);





More information about the llvm-commits mailing list