Update test to pass .ll to llvm-link and use Inputs.
[oota-llvm.git] / unittests / ADT / APIntTest.cpp
index c9b3bc51c135f45f53d845fd55be484ae5e95624..8198c719d6aa754e693499b038afcb2e606f9df6 100644 (file)
@@ -7,10 +7,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <ostream>
-#include "gtest/gtest.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/SmallString.h"
+#include "gtest/gtest.h"
+#include <ostream>
 
 using namespace llvm;
 
@@ -56,6 +56,14 @@ TEST(APIntTest, i33_Count) {
 #endif
 
 TEST(APIntTest, i65_Count) {
+  APInt i65(65, 0, true);
+  EXPECT_EQ(65u, i65.countLeadingZeros());
+  EXPECT_EQ(0u, i65.countLeadingOnes());
+  EXPECT_EQ(0u, i65.getActiveBits());
+  EXPECT_EQ(1u, i65.getActiveWords());
+  EXPECT_EQ(65u, i65.countTrailingZeros());
+  EXPECT_EQ(0u, i65.countPopulation());
+
   APInt i65minus(65, 0, true);
   i65minus.setBit(64);
   EXPECT_EQ(0u, i65minus.countLeadingZeros());
@@ -144,6 +152,12 @@ TEST(APIntTest, i1) {
   EXPECT_EQ(zero, one.lshr(1));
   EXPECT_EQ(zero, one.ashr(1));
 
+  // Rotates.
+  EXPECT_EQ(one, one.rotl(0));
+  EXPECT_EQ(one, one.rotl(1));
+  EXPECT_EQ(one, one.rotr(0));
+  EXPECT_EQ(one, one.rotr(1));
+
   // Multiplies.
   EXPECT_EQ(neg_one, neg_one * one);
   EXPECT_EQ(neg_one, one * neg_one);
@@ -165,6 +179,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) {
@@ -237,6 +279,20 @@ TEST(APIntTest, fromString) {
   EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
   EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
   EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
+
+  EXPECT_EQ(APInt(32,  0), APInt(32,  "0", 36));
+  EXPECT_EQ(APInt(32,  1), APInt(32,  "1", 36));
+  EXPECT_EQ(APInt(32, 35), APInt(32,  "Z", 36));
+  EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
+  EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
+  EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
+  
+  EXPECT_EQ(APInt(32,  uint64_t(-0LL)), APInt(32,  "-0", 36));
+  EXPECT_EQ(APInt(32,  uint64_t(-1LL)), APInt(32,  "-1", 36));
+  EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32,  "-Z", 36));
+  EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
+  EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
+  EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
 }
 
 TEST(APIntTest, FromArray) {
@@ -340,6 +396,9 @@ TEST(APIntTest, toString) {
   APInt(8, 0).toString(S, 16, true, true);
   EXPECT_EQ(S.str().str(), "0x0");
   S.clear();
+  APInt(8, 0).toString(S, 36, true, false);
+  EXPECT_EQ(S.str().str(), "0");
+  S.clear();
 
   isSigned = false;
   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
@@ -354,6 +413,9 @@ TEST(APIntTest, toString) {
   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
   EXPECT_EQ(S.str().str(), "0xFF");
   S.clear();
+  APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
+  EXPECT_EQ(S.str().str(), "73");
+  S.clear();
 
   isSigned = true;
   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
@@ -368,6 +430,9 @@ TEST(APIntTest, toString) {
   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
   EXPECT_EQ(S.str().str(), "-0x1");
   S.clear();
+  APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
+  EXPECT_EQ(S.str().str(), "-1");
+  S.clear();
 }
 
 TEST(APIntTest, Log2) {
@@ -407,7 +472,7 @@ TEST(APIntTest, magicu) {
 TEST(APIntTest, StringDeath) {
   EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
   EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
-  EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, or 16!");
+  EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
   EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
   EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
   EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
@@ -418,4 +483,216 @@ TEST(APIntTest, StringDeath) {
 #endif
 #endif
 
+TEST(APIntTest, mul_clear) {
+  APInt ValA(65, -1ULL);
+  APInt ValB(65, 4);
+  APInt ValC(65, 0);
+  ValC = ValA * ValB;
+  ValA *= ValB;
+  EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
+}
+
+TEST(APIntTest, Rotate) {
+  EXPECT_EQ(APInt(8, 1),  APInt(8, 1).rotl(0));
+  EXPECT_EQ(APInt(8, 2),  APInt(8, 1).rotl(1));
+  EXPECT_EQ(APInt(8, 4),  APInt(8, 1).rotl(2));
+  EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
+  EXPECT_EQ(APInt(8, 1),  APInt(8, 1).rotl(8));
+
+  EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
+  EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
+  EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
+  EXPECT_EQ(APInt(8, 1),  APInt(8, 16).rotl(4));
+  EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
+
+  EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
+  EXPECT_EQ(APInt(8, 8),  APInt(8, 16).rotr(1));
+  EXPECT_EQ(APInt(8, 4),  APInt(8, 16).rotr(2));
+  EXPECT_EQ(APInt(8, 1),  APInt(8, 16).rotr(4));
+  EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
+
+  EXPECT_EQ(APInt(8, 1),   APInt(8, 1).rotr(0));
+  EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
+  EXPECT_EQ(APInt(8, 64),  APInt(8, 1).rotr(2));
+  EXPECT_EQ(APInt(8, 16),  APInt(8, 1).rotr(4));
+  EXPECT_EQ(APInt(8, 1),   APInt(8, 1).rotr(8));
+
+  APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
+  APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
+  EXPECT_EQ(Rot, Big.rotr(144));
+}
+
+TEST(APIntTest, Splat) {
+  APInt ValA(8, 0x01);
+  EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
+  EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
+
+  APInt ValB(3, 5);
+  EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
+  EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
+}
+
+TEST(APIntTest, tcDecrement) {
+  // Test single word decrement.
+
+  // No out borrow.
+  {
+    integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1);
+    integerPart carry = APInt::tcDecrement(&singleWord, 1);
+    EXPECT_EQ(carry, integerPart(0));
+    EXPECT_EQ(singleWord, ~integerPart(0) >> 1);
+  }
+
+  // With out borrow.
+  {
+    integerPart singleWord = 0;
+    integerPart carry = APInt::tcDecrement(&singleWord, 1);
+    EXPECT_EQ(carry, integerPart(1));
+    EXPECT_EQ(singleWord, ~integerPart(0));
+  }
+
+  // Test multiword decrement.
+
+  // No across word borrow, no out borrow.
+  {
+    integerPart test[4] = {0x1, 0x1, 0x1, 0x1};
+    integerPart expected[4] = {0x0, 0x1, 0x1, 0x1};
+    APInt::tcDecrement(test, 4);
+    EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
+  }
+
+  // 1 across word borrow, no out borrow.
+  {
+    integerPart test[4] = {0x0, 0xF, 0x1, 0x1};
+    integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1};
+    integerPart carry = APInt::tcDecrement(test, 4);
+    EXPECT_EQ(carry, integerPart(0));
+    EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
+  }
+
+  // 2 across word borrow, no out borrow.
+  {
+    integerPart test[4] = {0x0, 0x0, 0xC, 0x1};
+    integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1};
+    integerPart carry = APInt::tcDecrement(test, 4);
+    EXPECT_EQ(carry, integerPart(0));
+    EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
+  }
+
+  // 3 across word borrow, no out borrow.
+  {
+    integerPart test[4] = {0x0, 0x0, 0x0, 0x1};
+    integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0};
+    integerPart carry = APInt::tcDecrement(test, 4);
+    EXPECT_EQ(carry, integerPart(0));
+    EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
+  }
+
+  // 3 across word borrow, with out borrow.
+  {
+    integerPart test[4] = {0x0, 0x0, 0x0, 0x0};
+    integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)};
+    integerPart carry = APInt::tcDecrement(test, 4);
+    EXPECT_EQ(carry, integerPart(1));
+    EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
+  }
+}
+
+TEST(APIntTest, arrayAccess) {
+  // Single word check.
+  uint64_t E1 = 0x2CA7F46BF6569915ULL;
+  APInt A1(64, E1);
+  for (unsigned i = 0, e = 64; i < e; ++i) {
+    EXPECT_EQ(bool(E1 & (1ULL << i)),
+              A1[i]);
+  }
+
+  // Multiword check.
+  integerPart E2[4] = {
+    0xEB6EB136591CBA21ULL,
+    0x7B9358BD6A33F10AULL,
+    0x7E7FFA5EADD8846ULL,
+    0x305F341CA00B613DULL
+  };
+  APInt A2(integerPartWidth*4, E2);
+  for (unsigned i = 0; i < 4; ++i) {
+    for (unsigned j = 0; j < integerPartWidth; ++j) {
+      EXPECT_EQ(bool(E2[i] & (1ULL << j)),
+                A2[i*integerPartWidth + j]);
+    }
+  }
+}
+
+TEST(APIntTest, LargeAPIntConstruction) {
+  // Check that we can properly construct very large APInt. It is very
+  // unlikely that people will ever do this, but it is a legal input,
+  // so we should not crash on it.
+  APInt A9(UINT32_MAX, 0);
+  EXPECT_FALSE(A9.getBoolValue());
+}
+
+TEST(APIntTest, nearestLogBase2) {
+  // Single word check.
+
+  // Test round up.
+  uint64_t I1 = 0x1800001;
+  APInt A1(64, I1);
+  EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
+
+  // Test round down.
+  uint64_t I2 = 0x1000011;
+  APInt A2(64, I2);
+  EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
+
+  // Test ties round up.
+  uint64_t I3 = 0x1800000;
+  APInt A3(64, I3);
+  EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
+
+  // Multiple word check.
+
+  // Test round up.
+  integerPart I4[4] = {0x0, 0xF, 0x18, 0x0};
+  APInt A4(integerPartWidth*4, I4);
+  EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
+
+  // Test round down.
+  integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
+  APInt A5(integerPartWidth*4, I5);
+  EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
+
+  // Test ties round up.
+  uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
+  APInt A6(integerPartWidth*4, I6);
+  EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
+
+  // Test BitWidth == 1 special cases.
+  APInt A7(1, 1);
+  EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
+  APInt A8(1, 0);
+  EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
+
+  // Test the zero case when we have a bit width large enough such
+  // that the bit width is larger than UINT32_MAX-1.
+  APInt A9(UINT32_MAX, 0);
+  EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
+}
+
+TEST(APIntTest, SelfMoveAssignment) {
+  APInt X(32, 0xdeadbeef);
+  X = std::move(X);
+  EXPECT_EQ(32u, X.getBitWidth());
+  EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
+
+  uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
+  APInt Y(128, Bits);
+  Y = std::move(Y);
+  EXPECT_EQ(128u, Y.getBitWidth());
+  EXPECT_EQ(~0ULL, Y.getLimitedValue());
+  const uint64_t *Raw = Y.getRawData();
+  EXPECT_EQ(2u, Y.getNumWords());
+  EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
+  EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
+}
+
 }