//
//===----------------------------------------------------------------------===//
-#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;
#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());
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);
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) {
APInt(8, 0).toString(S, 16, true, true);
EXPECT_EQ(S.str().str(), "0x0");
S.clear();
- APInt(8, 0).toString(S, 36, true, true);
+ APInt(8, 0).toString(S, 36, true, false);
EXPECT_EQ(S.str().str(), "0");
S.clear();
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, true);
+ APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
EXPECT_EQ(S.str().str(), "73");
S.clear();
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, true);
+ APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
EXPECT_EQ(S.str().str(), "-1");
S.clear();
}
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);
+ }
+}
}