aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/ADT/APInt.h5
-rw-r--r--unittests/ADT/APIntTest.cpp28
2 files changed, 31 insertions, 2 deletions
diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h
index 4101989976..eb7192cb95 100644
--- a/include/llvm/ADT/APInt.h
+++ b/include/llvm/ADT/APInt.h
@@ -817,9 +817,10 @@ public:
if (LHS.isNegative()) {
if (RHS.isNegative())
APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
- else
+ else {
APInt::udivrem(-LHS, RHS, Quotient, Remainder);
- Quotient = -Quotient;
+ Quotient = -Quotient;
+ }
Remainder = -Remainder;
} else if (RHS.isNegative()) {
APInt::udivrem(LHS, -RHS, Quotient, Remainder);
diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp
index 89b8aa94e4..49d7e703de 100644
--- a/unittests/ADT/APIntTest.cpp
+++ b/unittests/ADT/APIntTest.cpp
@@ -171,6 +171,34 @@ TEST(APIntTest, i1) {
EXPECT_EQ(zero, neg_one.srem(one));
EXPECT_EQ(zero, neg_one.urem(one));
EXPECT_EQ(zero, one.srem(neg_one));
+
+ // sdivrem
+ {
+ APInt q(8, 0);
+ APInt r(8, 0);
+ APInt one(8, 1);
+ APInt two(8, 2);
+ APInt nine(8, 9);
+ APInt four(8, 4);
+
+ EXPECT_EQ(nine.srem(two), one);
+ EXPECT_EQ(nine.srem(-two), one);
+ EXPECT_EQ((-nine).srem(two), -one);
+ EXPECT_EQ((-nine).srem(-two), -one);
+
+ APInt::sdivrem(nine, two, q, r);
+ EXPECT_EQ(four, q);
+ EXPECT_EQ(one, r);
+ APInt::sdivrem(-nine, two, q, r);
+ EXPECT_EQ(-four, q);
+ EXPECT_EQ(-one, r);
+ APInt::sdivrem(nine, -two, q, r);
+ EXPECT_EQ(-four, q);
+ EXPECT_EQ(one, r);
+ APInt::sdivrem(-nine, -two, q, r);
+ EXPECT_EQ(four, q);
+ EXPECT_EQ(-one, r);
+ }
}
TEST(APIntTest, fromString) {