2009-02-09 12:31:40 +00:00
|
|
|
//===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
|
2009-01-19 18:08:33 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/APInt.h"
|
2009-03-24 21:36:09 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-04 10:23:08 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <ostream>
|
2009-01-19 18:08:33 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
|
|
|
|
TEST(APIntTest, ShiftLeftByZero) {
|
|
|
|
APInt One = APInt::getNullValue(65) + 1;
|
|
|
|
APInt Shl = One.shl(0);
|
2010-07-13 17:28:05 +00:00
|
|
|
EXPECT_TRUE(Shl[0]);
|
|
|
|
EXPECT_FALSE(Shl[1]);
|
2009-01-27 18:06:03 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
TEST(APIntTest, i128_NegativeCount) {
|
2009-04-08 16:17:23 +00:00
|
|
|
APInt Minus3(128, static_cast<uint64_t>(-3), true);
|
2009-01-27 18:06:03 +00:00
|
|
|
EXPECT_EQ(126u, Minus3.countLeadingOnes());
|
|
|
|
EXPECT_EQ(-3, Minus3.getSExtValue());
|
|
|
|
|
2009-04-08 16:17:23 +00:00
|
|
|
APInt Minus1(128, static_cast<uint64_t>(-1), true);
|
2009-01-27 18:06:03 +00:00
|
|
|
EXPECT_EQ(0u, Minus1.countLeadingZeros());
|
|
|
|
EXPECT_EQ(128u, Minus1.countLeadingOnes());
|
|
|
|
EXPECT_EQ(128u, Minus1.getActiveBits());
|
|
|
|
EXPECT_EQ(0u, Minus1.countTrailingZeros());
|
|
|
|
EXPECT_EQ(128u, Minus1.countTrailingOnes());
|
|
|
|
EXPECT_EQ(128u, Minus1.countPopulation());
|
|
|
|
EXPECT_EQ(-1, Minus1.getSExtValue());
|
|
|
|
}
|
|
|
|
|
2010-09-14 00:51:58 +00:00
|
|
|
// XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it.
|
|
|
|
#if defined(__llvm__) || !defined(__FreeBSD__)
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
TEST(APIntTest, i33_Count) {
|
2009-04-08 16:17:23 +00:00
|
|
|
APInt i33minus2(33, static_cast<uint64_t>(-2), true);
|
2009-01-27 18:06:03 +00:00
|
|
|
EXPECT_EQ(0u, i33minus2.countLeadingZeros());
|
|
|
|
EXPECT_EQ(32u, i33minus2.countLeadingOnes());
|
|
|
|
EXPECT_EQ(33u, i33minus2.getActiveBits());
|
|
|
|
EXPECT_EQ(1u, i33minus2.countTrailingZeros());
|
|
|
|
EXPECT_EQ(32u, i33minus2.countPopulation());
|
|
|
|
EXPECT_EQ(-2, i33minus2.getSExtValue());
|
|
|
|
EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
|
|
|
|
}
|
|
|
|
|
2010-09-14 00:51:58 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
TEST(APIntTest, i65_Count) {
|
2013-02-07 18:36:50 +00:00
|
|
|
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());
|
|
|
|
|
2009-01-27 18:06:03 +00:00
|
|
|
APInt i65minus(65, 0, true);
|
2010-12-01 08:53:58 +00:00
|
|
|
i65minus.setBit(64);
|
2009-01-27 18:06:03 +00:00
|
|
|
EXPECT_EQ(0u, i65minus.countLeadingZeros());
|
|
|
|
EXPECT_EQ(1u, i65minus.countLeadingOnes());
|
|
|
|
EXPECT_EQ(65u, i65minus.getActiveBits());
|
|
|
|
EXPECT_EQ(64u, i65minus.countTrailingZeros());
|
|
|
|
EXPECT_EQ(1u, i65minus.countPopulation());
|
|
|
|
}
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
TEST(APIntTest, i128_PositiveCount) {
|
2009-01-27 18:06:03 +00:00
|
|
|
APInt u128max = APInt::getAllOnesValue(128);
|
|
|
|
EXPECT_EQ(128u, u128max.countLeadingOnes());
|
|
|
|
EXPECT_EQ(0u, u128max.countLeadingZeros());
|
|
|
|
EXPECT_EQ(128u, u128max.getActiveBits());
|
|
|
|
EXPECT_EQ(0u, u128max.countTrailingZeros());
|
|
|
|
EXPECT_EQ(128u, u128max.countTrailingOnes());
|
|
|
|
EXPECT_EQ(128u, u128max.countPopulation());
|
|
|
|
|
2009-04-08 16:17:23 +00:00
|
|
|
APInt u64max(128, static_cast<uint64_t>(-1), false);
|
2009-01-27 18:06:03 +00:00
|
|
|
EXPECT_EQ(64u, u64max.countLeadingZeros());
|
|
|
|
EXPECT_EQ(0u, u64max.countLeadingOnes());
|
|
|
|
EXPECT_EQ(64u, u64max.getActiveBits());
|
|
|
|
EXPECT_EQ(0u, u64max.countTrailingZeros());
|
|
|
|
EXPECT_EQ(64u, u64max.countTrailingOnes());
|
|
|
|
EXPECT_EQ(64u, u64max.countPopulation());
|
|
|
|
EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
|
|
|
|
|
|
|
|
APInt zero(128, 0, true);
|
|
|
|
EXPECT_EQ(128u, zero.countLeadingZeros());
|
|
|
|
EXPECT_EQ(0u, zero.countLeadingOnes());
|
|
|
|
EXPECT_EQ(0u, zero.getActiveBits());
|
|
|
|
EXPECT_EQ(128u, zero.countTrailingZeros());
|
|
|
|
EXPECT_EQ(0u, zero.countTrailingOnes());
|
|
|
|
EXPECT_EQ(0u, zero.countPopulation());
|
|
|
|
EXPECT_EQ(0u, zero.getSExtValue());
|
|
|
|
EXPECT_EQ(0u, zero.getZExtValue());
|
|
|
|
|
|
|
|
APInt one(128, 1, true);
|
|
|
|
EXPECT_EQ(127u, one.countLeadingZeros());
|
|
|
|
EXPECT_EQ(0u, one.countLeadingOnes());
|
|
|
|
EXPECT_EQ(1u, one.getActiveBits());
|
|
|
|
EXPECT_EQ(0u, one.countTrailingZeros());
|
|
|
|
EXPECT_EQ(1u, one.countTrailingOnes());
|
|
|
|
EXPECT_EQ(1u, one.countPopulation());
|
|
|
|
EXPECT_EQ(1, one.getSExtValue());
|
|
|
|
EXPECT_EQ(1u, one.getZExtValue());
|
2009-01-19 18:08:33 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
TEST(APIntTest, i1) {
|
2009-04-08 16:17:23 +00:00
|
|
|
const APInt neg_two(1, static_cast<uint64_t>(-2), true);
|
|
|
|
const APInt neg_one(1, static_cast<uint64_t>(-1), true);
|
2009-03-24 21:36:09 +00:00
|
|
|
const APInt zero(1, 0);
|
|
|
|
const APInt one(1, 1);
|
|
|
|
const APInt two(1, 2);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, neg_two.getSExtValue());
|
|
|
|
EXPECT_EQ(-1, neg_one.getSExtValue());
|
|
|
|
EXPECT_EQ(1u, neg_one.getZExtValue());
|
|
|
|
EXPECT_EQ(0u, zero.getZExtValue());
|
|
|
|
EXPECT_EQ(-1, one.getSExtValue());
|
|
|
|
EXPECT_EQ(1u, one.getZExtValue());
|
|
|
|
EXPECT_EQ(0u, two.getZExtValue());
|
|
|
|
EXPECT_EQ(0, two.getSExtValue());
|
|
|
|
|
|
|
|
// Basic equalities for 1-bit values.
|
|
|
|
EXPECT_EQ(zero, two);
|
|
|
|
EXPECT_EQ(zero, neg_two);
|
|
|
|
EXPECT_EQ(one, neg_one);
|
|
|
|
EXPECT_EQ(two, neg_two);
|
|
|
|
|
|
|
|
// Additions.
|
|
|
|
EXPECT_EQ(two, one + one);
|
|
|
|
EXPECT_EQ(zero, neg_one + one);
|
|
|
|
EXPECT_EQ(neg_two, neg_one + neg_one);
|
|
|
|
|
|
|
|
// Subtractions.
|
|
|
|
EXPECT_EQ(neg_two, neg_one - one);
|
|
|
|
EXPECT_EQ(two, one - neg_one);
|
|
|
|
EXPECT_EQ(zero, one - one);
|
|
|
|
|
|
|
|
// Shifts.
|
|
|
|
EXPECT_EQ(zero, one << one);
|
|
|
|
EXPECT_EQ(one, one << zero);
|
|
|
|
EXPECT_EQ(zero, one.shl(1));
|
|
|
|
EXPECT_EQ(one, one.shl(0));
|
|
|
|
EXPECT_EQ(zero, one.lshr(1));
|
|
|
|
EXPECT_EQ(zero, one.ashr(1));
|
|
|
|
|
2011-12-22 22:11:19 +00:00
|
|
|
// 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));
|
|
|
|
|
2009-03-24 21:36:09 +00:00
|
|
|
// Multiplies.
|
|
|
|
EXPECT_EQ(neg_one, neg_one * one);
|
|
|
|
EXPECT_EQ(neg_one, one * neg_one);
|
|
|
|
EXPECT_EQ(one, neg_one * neg_one);
|
|
|
|
EXPECT_EQ(one, one * one);
|
|
|
|
|
|
|
|
// Divides.
|
|
|
|
EXPECT_EQ(neg_one, one.sdiv(neg_one));
|
|
|
|
EXPECT_EQ(neg_one, neg_one.sdiv(one));
|
|
|
|
EXPECT_EQ(one, neg_one.sdiv(neg_one));
|
|
|
|
EXPECT_EQ(one, one.sdiv(one));
|
|
|
|
|
|
|
|
EXPECT_EQ(neg_one, one.udiv(neg_one));
|
|
|
|
EXPECT_EQ(neg_one, neg_one.udiv(one));
|
|
|
|
EXPECT_EQ(one, neg_one.udiv(neg_one));
|
|
|
|
EXPECT_EQ(one, one.udiv(one));
|
|
|
|
|
|
|
|
// Remainders.
|
|
|
|
EXPECT_EQ(zero, neg_one.srem(one));
|
|
|
|
EXPECT_EQ(zero, neg_one.urem(one));
|
|
|
|
EXPECT_EQ(zero, one.srem(neg_one));
|
2012-05-22 01:09:48 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2009-03-24 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 19:45:19 +00:00
|
|
|
TEST(APIntTest, divrem_big1) {
|
|
|
|
// Tests KnuthDiv rare step D6
|
|
|
|
APInt a{256, "1ffffffffffffffff", 16};
|
|
|
|
APInt b{256, "1ffffffffffffffff", 16};
|
|
|
|
APInt c{256, 0};
|
|
|
|
|
|
|
|
auto p = a * b + c;
|
|
|
|
auto q = p.udiv(a);
|
|
|
|
auto r = p.urem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.udiv(b);
|
|
|
|
r = p.urem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(a);
|
|
|
|
r = p.srem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(b);
|
|
|
|
r = p.srem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, divrem_big2) {
|
|
|
|
// Tests KnuthDiv rare step D6
|
|
|
|
APInt a{1024, "111111ffffffffffffffff"
|
|
|
|
"ffffffffffffffffffffffffffffffff"
|
|
|
|
"fffffffffffffffffffffffffffffccf"
|
|
|
|
"ffffffffffffffffffffffffffffff00", 16};
|
|
|
|
APInt b{1024, "112233ceff"
|
|
|
|
"cecece000000ffffffffffffffffffff"
|
|
|
|
"ffffffffffffffffffffffffffffffff"
|
|
|
|
"ffffffffffffffffffffffffffffffff"
|
|
|
|
"ffffffffffffffffffffffffffffff33", 16};
|
|
|
|
APInt c{1024, 7919};
|
|
|
|
|
|
|
|
auto p = a * b + c;
|
|
|
|
auto q = p.udiv(a);
|
|
|
|
auto r = p.urem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.udiv(b);
|
|
|
|
r = p.urem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(a);
|
|
|
|
r = p.srem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(b);
|
|
|
|
r = p.srem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, divrem_big3) {
|
|
|
|
// Tests KnuthDiv case without shift
|
|
|
|
APInt a{256, "ffffffffffffff0000000", 16};
|
|
|
|
APInt b{256, "80000001ffffffffffffffff", 16};
|
|
|
|
APInt c{256, 4219};
|
|
|
|
|
|
|
|
auto p = a * b + c;
|
|
|
|
auto q = p.udiv(a);
|
|
|
|
auto r = p.urem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.udiv(b);
|
|
|
|
r = p.urem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(a);
|
|
|
|
r = p.srem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(b);
|
|
|
|
r = p.srem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, divrem_big4) {
|
|
|
|
// Tests heap allocation in divide() enfoced by huge numbers
|
|
|
|
auto a = APInt{4096, 1}.shl(2000);
|
|
|
|
auto b = APInt{4096, 5}.shl(2001);
|
|
|
|
auto c = APInt{4096, 4219*13};
|
|
|
|
|
|
|
|
auto p = a * b + c;
|
|
|
|
auto q = p.udiv(a);
|
|
|
|
auto r = p.urem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = APInt{1024, 0}; // test non-single word APInt conversion in divide()
|
|
|
|
r = APInt{1024, 0};
|
|
|
|
APInt::udivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.udiv(b);
|
|
|
|
r = p.urem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = APInt{1024, 0};
|
|
|
|
r = APInt{1024, 0};
|
|
|
|
APInt::udivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(a);
|
|
|
|
r = p.srem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = APInt{1024, 0};
|
|
|
|
r = APInt{1024, 0};
|
|
|
|
APInt::sdivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(b);
|
|
|
|
r = p.srem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = APInt{1024, 0};
|
|
|
|
r = APInt{1024, 0};
|
|
|
|
APInt::sdivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, divrem_big5) {
|
|
|
|
// Tests one word divisor case of divide()
|
|
|
|
auto a = APInt{1024, 19}.shl(811);
|
|
|
|
auto b = APInt{1024, 4356013}; // one word
|
|
|
|
auto c = APInt{1024, 1};
|
|
|
|
|
|
|
|
auto p = a * b + c;
|
|
|
|
auto q = p.udiv(a);
|
|
|
|
auto r = p.urem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.udiv(b);
|
|
|
|
r = p.urem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::udivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(a);
|
|
|
|
r = p.srem(a);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, a, q, r);
|
|
|
|
EXPECT_EQ(q, b);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
q = p.sdiv(b);
|
|
|
|
r = p.srem(b);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
APInt::sdivrem(p, b, q, r);
|
|
|
|
EXPECT_EQ(q, a);
|
|
|
|
EXPECT_EQ(r, c);
|
|
|
|
}
|
|
|
|
|
2009-04-25 18:34:04 +00:00
|
|
|
TEST(APIntTest, fromString) {
|
2009-08-21 03:15:14 +00:00
|
|
|
EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
|
|
|
|
|
|
|
|
EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
|
|
|
|
EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
|
|
|
|
|
2009-09-18 17:48:05 +00:00
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
|
2009-08-21 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
|
|
|
|
EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
|
|
|
|
EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
|
|
|
|
EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
|
|
|
|
EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
|
|
|
|
EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
|
|
|
|
|
|
|
|
EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
|
|
|
|
EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
|
|
|
|
EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
|
|
|
|
EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
|
|
|
|
EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
|
|
|
|
EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
|
|
|
|
|
2009-09-17 17:46:53 +00:00
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
|
2009-08-21 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
|
|
|
|
EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
|
|
|
|
EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
|
|
|
|
EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
|
|
|
|
EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
|
|
|
|
EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
|
|
|
|
|
2009-09-17 17:46:53 +00:00
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
|
2009-08-21 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
|
|
|
|
|
2009-09-17 17:46:53 +00:00
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
|
|
|
|
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));
|
2011-09-14 15:54:46 +00:00
|
|
|
|
|
|
|
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));
|
2009-04-25 18:34:04 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 21:45:40 +00:00
|
|
|
TEST(APIntTest, FromArray) {
|
|
|
|
EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
|
|
|
|
}
|
|
|
|
|
2009-08-21 03:15:28 +00:00
|
|
|
TEST(APIntTest, StringBitsNeeded2) {
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
|
|
|
|
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
|
|
|
|
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, StringBitsNeeded8) {
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
|
|
|
|
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
|
|
|
|
EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
|
|
|
|
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
|
|
|
|
EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
|
|
|
|
EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
|
|
|
|
EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, StringBitsNeeded10) {
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
|
|
|
|
|
|
|
|
EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
|
|
|
|
|
|
|
|
EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
|
|
|
|
EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, StringBitsNeeded16) {
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
|
|
|
|
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
|
|
|
|
EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
|
|
|
|
EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
|
|
|
|
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
|
|
|
|
EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
|
|
|
|
EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
|
|
|
|
EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
|
|
|
|
EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
|
|
|
|
}
|
|
|
|
|
2011-06-15 23:36:34 +00:00
|
|
|
TEST(APIntTest, toString) {
|
|
|
|
SmallString<16> S;
|
|
|
|
bool isSigned;
|
|
|
|
|
|
|
|
APInt(8, 0).toString(S, 2, true, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0b0");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 0).toString(S, 8, true, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "00");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 0).toString(S, 10, true, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 0).toString(S, 16, true, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0x0");
|
|
|
|
S.clear();
|
2011-12-16 20:36:31 +00:00
|
|
|
APInt(8, 0).toString(S, 36, true, false);
|
2011-09-14 15:54:46 +00:00
|
|
|
EXPECT_EQ(S.str().str(), "0");
|
|
|
|
S.clear();
|
2011-06-15 23:36:34 +00:00
|
|
|
|
|
|
|
isSigned = false;
|
|
|
|
APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0b11111111");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0377");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "255");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "0xFF");
|
|
|
|
S.clear();
|
2011-12-16 20:36:31 +00:00
|
|
|
APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
|
2011-09-14 15:54:46 +00:00
|
|
|
EXPECT_EQ(S.str().str(), "73");
|
|
|
|
S.clear();
|
2011-06-15 23:36:34 +00:00
|
|
|
|
|
|
|
isSigned = true;
|
|
|
|
APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "-0b1");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "-01");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "-1");
|
|
|
|
S.clear();
|
|
|
|
APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
|
|
|
|
EXPECT_EQ(S.str().str(), "-0x1");
|
|
|
|
S.clear();
|
2011-12-16 20:36:31 +00:00
|
|
|
APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
|
2011-09-14 15:54:46 +00:00
|
|
|
EXPECT_EQ(S.str().str(), "-1");
|
|
|
|
S.clear();
|
2011-06-15 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2009-10-13 01:49:02 +00:00
|
|
|
TEST(APIntTest, Log2) {
|
2009-10-13 09:23:11 +00:00
|
|
|
EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
|
|
|
|
EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
|
2009-10-13 01:49:02 +00:00
|
|
|
EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
|
2009-10-13 09:23:11 +00:00
|
|
|
EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
|
|
|
|
EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
|
2009-10-13 01:49:02 +00:00
|
|
|
EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
|
2009-10-13 09:23:11 +00:00
|
|
|
EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
|
|
|
|
EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
|
2009-10-13 01:49:02 +00:00
|
|
|
EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
|
|
|
|
}
|
2009-08-21 03:15:28 +00:00
|
|
|
|
2011-02-21 00:22:02 +00:00
|
|
|
TEST(APIntTest, magic) {
|
|
|
|
EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 3).magic().s, 0U);
|
|
|
|
EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 5).magic().s, 1U);
|
|
|
|
EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 7).magic().s, 2U);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APIntTest, magicu) {
|
|
|
|
EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
|
|
|
|
EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
|
|
|
|
EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
|
|
|
|
EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
|
2011-03-17 20:39:06 +00:00
|
|
|
EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
|
|
|
|
EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
|
2011-02-21 00:22:02 +00:00
|
|
|
}
|
|
|
|
|
2009-08-17 00:55:33 +00:00
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
2010-03-17 01:18:45 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-16 23:36:01 +00:00
|
|
|
TEST(APIntTest, StringDeath) {
|
2009-08-21 03:15:14 +00:00
|
|
|
EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
|
|
|
|
EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
|
2011-09-14 15:54:46 +00:00
|
|
|
EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
|
2009-08-16 23:36:01 +00:00
|
|
|
EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
|
2009-08-21 06:35:41 +00:00
|
|
|
EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
|
2009-08-16 23:36:01 +00:00
|
|
|
EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
|
|
|
|
EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
|
|
|
|
EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
|
|
|
|
EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
|
|
|
|
}
|
2009-08-17 00:55:33 +00:00
|
|
|
#endif
|
2010-03-17 01:18:45 +00:00
|
|
|
#endif
|
2009-08-16 23:36:01 +00:00
|
|
|
|
2011-10-07 23:40:49 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:11:19 +00:00
|
|
|
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));
|
2012-02-07 16:27:39 +00:00
|
|
|
|
|
|
|
APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
|
|
|
|
APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
|
|
|
|
EXPECT_EQ(Rot, Big.rotr(144));
|
2011-12-22 22:11:19 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 13:00:06 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:50:20 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 20:47:34 +00:00
|
|
|
|
2013-12-13 22:00:19 +00:00
|
|
|
TEST(APIntTest, arrayAccess) {
|
2013-12-13 20:47:34 +00:00
|
|
|
// Single word check.
|
|
|
|
uint64_t E1 = 0x2CA7F46BF6569915ULL;
|
|
|
|
APInt A1(64, E1);
|
2014-01-31 21:45:51 +00:00
|
|
|
for (unsigned i = 0, e = 64; i < e; ++i) {
|
2013-12-13 20:47:34 +00:00
|
|
|
EXPECT_EQ(bool(E1 & (1ULL << i)),
|
2013-12-13 22:00:19 +00:00
|
|
|
A1[i]);
|
2013-12-13 20:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiword check.
|
|
|
|
integerPart E2[4] = {
|
2013-12-13 20:47:37 +00:00
|
|
|
0xEB6EB136591CBA21ULL,
|
|
|
|
0x7B9358BD6A33F10AULL,
|
|
|
|
0x7E7FFA5EADD8846ULL,
|
|
|
|
0x305F341CA00B613DULL
|
2013-12-13 20:47:34 +00:00
|
|
|
};
|
2014-08-27 05:25:25 +00:00
|
|
|
APInt A2(integerPartWidth*4, E2);
|
2013-12-13 20:47:34 +00:00
|
|
|
for (unsigned i = 0; i < 4; ++i) {
|
|
|
|
for (unsigned j = 0; j < integerPartWidth; ++j) {
|
|
|
|
EXPECT_EQ(bool(E2[i] & (1ULL << j)),
|
2013-12-13 22:00:19 +00:00
|
|
|
A2[i*integerPartWidth + j]);
|
2013-12-13 20:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-19 20:33:38 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:47:37 +00:00
|
|
|
TEST(APIntTest, nearestLogBase2) {
|
2014-01-31 21:45:51 +00:00
|
|
|
// Single word check.
|
2013-12-13 20:47:37 +00:00
|
|
|
|
|
|
|
// 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};
|
2014-08-27 05:25:25 +00:00
|
|
|
APInt A4(integerPartWidth*4, I4);
|
2013-12-13 20:47:37 +00:00
|
|
|
EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
|
|
|
|
|
|
|
|
// Test round down.
|
|
|
|
integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
|
2014-08-27 05:25:25 +00:00
|
|
|
APInt A5(integerPartWidth*4, I5);
|
2013-12-13 20:47:37 +00:00
|
|
|
EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
|
|
|
|
|
|
|
|
// Test ties round up.
|
|
|
|
uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
|
2014-08-27 05:25:25 +00:00
|
|
|
APInt A6(integerPartWidth*4, I6);
|
2013-12-13 20:47:37 +00:00
|
|
|
EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
|
2014-01-19 20:33:48 +00:00
|
|
|
|
|
|
|
// 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);
|
2013-12-13 20:47:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 16:49:59 +00:00
|
|
|
TEST(APIntTest, IsSplat) {
|
|
|
|
APInt A(32, 0x01010101);
|
|
|
|
EXPECT_FALSE(A.isSplat(1));
|
|
|
|
EXPECT_FALSE(A.isSplat(2));
|
|
|
|
EXPECT_FALSE(A.isSplat(4));
|
|
|
|
EXPECT_TRUE(A.isSplat(8));
|
|
|
|
EXPECT_TRUE(A.isSplat(16));
|
|
|
|
EXPECT_TRUE(A.isSplat(32));
|
|
|
|
|
|
|
|
APInt B(24, 0xAAAAAA);
|
|
|
|
EXPECT_FALSE(B.isSplat(1));
|
|
|
|
EXPECT_TRUE(B.isSplat(2));
|
|
|
|
EXPECT_TRUE(B.isSplat(4));
|
|
|
|
EXPECT_TRUE(B.isSplat(8));
|
|
|
|
EXPECT_TRUE(B.isSplat(24));
|
|
|
|
|
|
|
|
APInt C(24, 0xABAAAB);
|
|
|
|
EXPECT_FALSE(C.isSplat(1));
|
|
|
|
EXPECT_FALSE(C.isSplat(2));
|
|
|
|
EXPECT_FALSE(C.isSplat(4));
|
|
|
|
EXPECT_FALSE(C.isSplat(8));
|
|
|
|
EXPECT_TRUE(C.isSplat(24));
|
|
|
|
|
|
|
|
APInt D(32, 0xABBAABBA);
|
|
|
|
EXPECT_FALSE(D.isSplat(1));
|
|
|
|
EXPECT_FALSE(D.isSplat(2));
|
|
|
|
EXPECT_FALSE(D.isSplat(4));
|
|
|
|
EXPECT_FALSE(D.isSplat(8));
|
|
|
|
EXPECT_TRUE(D.isSplat(16));
|
|
|
|
EXPECT_TRUE(D.isSplat(32));
|
|
|
|
|
|
|
|
APInt E(32, 0);
|
|
|
|
EXPECT_TRUE(E.isSplat(1));
|
|
|
|
EXPECT_TRUE(E.isSplat(2));
|
|
|
|
EXPECT_TRUE(E.isSplat(4));
|
|
|
|
EXPECT_TRUE(E.isSplat(8));
|
|
|
|
EXPECT_TRUE(E.isSplat(16));
|
|
|
|
EXPECT_TRUE(E.isSplat(32));
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:30:07 +00:00
|
|
|
#if defined(__clang__)
|
2015-01-14 01:50:12 +00:00
|
|
|
// Disable the pragma warning from versions of Clang without -Wself-move
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
2015-01-13 02:10:33 +00:00
|
|
|
// Disable the warning that triggers on exactly what is being tested.
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wself-move"
|
2015-01-13 14:30:07 +00:00
|
|
|
#endif
|
APInt: Make self-move-assignment a no-op to fix stage3 clang-cl
It's not clear what the semantics of a self-move should be. The
consensus appears to be that a self-move should leave the object in a
moved-from state, which is what our existing move assignment operator
does.
However, the MSVC 2013 STL will perform self-moves in some cases. In
particular, when doing a std::stable_sort of an already sorted APSInt
vector of an appropriate size, one of the merge steps will self-move
half of the elements.
We don't notice this when building with MSVC, because MSVC will not
synthesize the move assignment operator for APSInt. Presumably MSVC
does this because APInt, the base class, has user-declared special
members that implicitly delete move special members. Instead, MSVC
selects the copy-assign operator, which defends against self-assignment.
Clang, on the other hand, selects the move-assign operator, and we get
garbage APInts.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215478 91177308-0d34-0410-b5e6-96231b3b80d8
2014-08-12 22:01:39 +00:00
|
|
|
TEST(APIntTest, SelfMoveAssignment) {
|
|
|
|
APInt X(32, 0xdeadbeef);
|
|
|
|
X = std::move(X);
|
2014-08-12 23:23:05 +00:00
|
|
|
EXPECT_EQ(32u, X.getBitWidth());
|
APInt: Make self-move-assignment a no-op to fix stage3 clang-cl
It's not clear what the semantics of a self-move should be. The
consensus appears to be that a self-move should leave the object in a
moved-from state, which is what our existing move assignment operator
does.
However, the MSVC 2013 STL will perform self-moves in some cases. In
particular, when doing a std::stable_sort of an already sorted APSInt
vector of an appropriate size, one of the merge steps will self-move
half of the elements.
We don't notice this when building with MSVC, because MSVC will not
synthesize the move assignment operator for APSInt. Presumably MSVC
does this because APInt, the base class, has user-declared special
members that implicitly delete move special members. Instead, MSVC
selects the copy-assign operator, which defends against self-assignment.
Clang, on the other hand, selects the move-assign operator, and we get
garbage APInts.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215478 91177308-0d34-0410-b5e6-96231b3b80d8
2014-08-12 22:01:39 +00:00
|
|
|
EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
|
|
|
|
|
|
|
|
uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
|
|
|
|
APInt Y(128, Bits);
|
|
|
|
Y = std::move(Y);
|
2014-08-12 23:23:05 +00:00
|
|
|
EXPECT_EQ(128u, Y.getBitWidth());
|
APInt: Make self-move-assignment a no-op to fix stage3 clang-cl
It's not clear what the semantics of a self-move should be. The
consensus appears to be that a self-move should leave the object in a
moved-from state, which is what our existing move assignment operator
does.
However, the MSVC 2013 STL will perform self-moves in some cases. In
particular, when doing a std::stable_sort of an already sorted APSInt
vector of an appropriate size, one of the merge steps will self-move
half of the elements.
We don't notice this when building with MSVC, because MSVC will not
synthesize the move assignment operator for APSInt. Presumably MSVC
does this because APInt, the base class, has user-declared special
members that implicitly delete move special members. Instead, MSVC
selects the copy-assign operator, which defends against self-assignment.
Clang, on the other hand, selects the move-assign operator, and we get
garbage APInts.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215478 91177308-0d34-0410-b5e6-96231b3b80d8
2014-08-12 22:01:39 +00:00
|
|
|
EXPECT_EQ(~0ULL, Y.getLimitedValue());
|
|
|
|
const uint64_t *Raw = Y.getRawData();
|
2014-08-12 23:23:05 +00:00
|
|
|
EXPECT_EQ(2u, Y.getNumWords());
|
APInt: Make self-move-assignment a no-op to fix stage3 clang-cl
It's not clear what the semantics of a self-move should be. The
consensus appears to be that a self-move should leave the object in a
moved-from state, which is what our existing move assignment operator
does.
However, the MSVC 2013 STL will perform self-moves in some cases. In
particular, when doing a std::stable_sort of an already sorted APSInt
vector of an appropriate size, one of the merge steps will self-move
half of the elements.
We don't notice this when building with MSVC, because MSVC will not
synthesize the move assignment operator for APSInt. Presumably MSVC
does this because APInt, the base class, has user-declared special
members that implicitly delete move special members. Instead, MSVC
selects the copy-assign operator, which defends against self-assignment.
Clang, on the other hand, selects the move-assign operator, and we get
garbage APInts.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215478 91177308-0d34-0410-b5e6-96231b3b80d8
2014-08-12 22:01:39 +00:00
|
|
|
EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
|
|
|
|
EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
|
|
|
|
}
|
2015-01-13 14:30:07 +00:00
|
|
|
#if defined(__clang__)
|
2015-01-13 02:10:33 +00:00
|
|
|
#pragma clang diagnostic pop
|
2015-01-14 01:50:12 +00:00
|
|
|
#pragma clang diagnostic pop
|
2015-01-13 14:30:07 +00:00
|
|
|
#endif
|
2009-01-19 18:08:33 +00:00
|
|
|
}
|