Fix for PR4051. When 2address pass delete an instruction, update kill info when neces...
[llvm/msp430.git] / unittests / ADT / APIntTest.cpp
blob8ff7d1d8099dd409b130ed2b32f73850fc0d8224
1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include <ostream>
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/SmallString.h"
16 using namespace llvm;
18 namespace {
20 // Make the Google Test failure output equivalent to APInt::dump()
21 std::ostream& operator<<(std::ostream &OS, const llvm::APInt& I) {
22 llvm::raw_os_ostream raw_os(OS);
24 SmallString<40> S, U;
25 I.toStringUnsigned(U);
26 I.toStringSigned(S);
27 raw_os << "APInt(" << I.getBitWidth()<< "b, "
28 << U.c_str() << "u " << S.c_str() << "s)";
29 raw_os.flush();
30 return OS;
33 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
34 TEST(APIntTest, ShiftLeftByZero) {
35 APInt One = APInt::getNullValue(65) + 1;
36 APInt Shl = One.shl(0);
37 EXPECT_EQ(true, Shl[0]);
38 EXPECT_EQ(false, Shl[1]);
41 TEST(APIntTest, i128_NegativeCount) {
42 APInt Minus3(128, static_cast<uint64_t>(-3), true);
43 EXPECT_EQ(126u, Minus3.countLeadingOnes());
44 EXPECT_EQ(-3, Minus3.getSExtValue());
46 APInt Minus1(128, static_cast<uint64_t>(-1), true);
47 EXPECT_EQ(0u, Minus1.countLeadingZeros());
48 EXPECT_EQ(128u, Minus1.countLeadingOnes());
49 EXPECT_EQ(128u, Minus1.getActiveBits());
50 EXPECT_EQ(0u, Minus1.countTrailingZeros());
51 EXPECT_EQ(128u, Minus1.countTrailingOnes());
52 EXPECT_EQ(128u, Minus1.countPopulation());
53 EXPECT_EQ(-1, Minus1.getSExtValue());
56 TEST(APIntTest, i33_Count) {
57 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
58 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
59 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
60 EXPECT_EQ(33u, i33minus2.getActiveBits());
61 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
62 EXPECT_EQ(32u, i33minus2.countPopulation());
63 EXPECT_EQ(-2, i33minus2.getSExtValue());
64 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
67 TEST(APIntTest, i65_Count) {
68 APInt i65minus(65, 0, true);
69 i65minus.set(64);
70 EXPECT_EQ(0u, i65minus.countLeadingZeros());
71 EXPECT_EQ(1u, i65minus.countLeadingOnes());
72 EXPECT_EQ(65u, i65minus.getActiveBits());
73 EXPECT_EQ(64u, i65minus.countTrailingZeros());
74 EXPECT_EQ(1u, i65minus.countPopulation());
77 TEST(APIntTest, i128_PositiveCount) {
78 APInt u128max = APInt::getAllOnesValue(128);
79 EXPECT_EQ(128u, u128max.countLeadingOnes());
80 EXPECT_EQ(0u, u128max.countLeadingZeros());
81 EXPECT_EQ(128u, u128max.getActiveBits());
82 EXPECT_EQ(0u, u128max.countTrailingZeros());
83 EXPECT_EQ(128u, u128max.countTrailingOnes());
84 EXPECT_EQ(128u, u128max.countPopulation());
86 APInt u64max(128, static_cast<uint64_t>(-1), false);
87 EXPECT_EQ(64u, u64max.countLeadingZeros());
88 EXPECT_EQ(0u, u64max.countLeadingOnes());
89 EXPECT_EQ(64u, u64max.getActiveBits());
90 EXPECT_EQ(0u, u64max.countTrailingZeros());
91 EXPECT_EQ(64u, u64max.countTrailingOnes());
92 EXPECT_EQ(64u, u64max.countPopulation());
93 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
95 APInt zero(128, 0, true);
96 EXPECT_EQ(128u, zero.countLeadingZeros());
97 EXPECT_EQ(0u, zero.countLeadingOnes());
98 EXPECT_EQ(0u, zero.getActiveBits());
99 EXPECT_EQ(128u, zero.countTrailingZeros());
100 EXPECT_EQ(0u, zero.countTrailingOnes());
101 EXPECT_EQ(0u, zero.countPopulation());
102 EXPECT_EQ(0u, zero.getSExtValue());
103 EXPECT_EQ(0u, zero.getZExtValue());
105 APInt one(128, 1, true);
106 EXPECT_EQ(127u, one.countLeadingZeros());
107 EXPECT_EQ(0u, one.countLeadingOnes());
108 EXPECT_EQ(1u, one.getActiveBits());
109 EXPECT_EQ(0u, one.countTrailingZeros());
110 EXPECT_EQ(1u, one.countTrailingOnes());
111 EXPECT_EQ(1u, one.countPopulation());
112 EXPECT_EQ(1, one.getSExtValue());
113 EXPECT_EQ(1u, one.getZExtValue());
116 TEST(APIntTest, i1) {
117 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
118 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
119 const APInt zero(1, 0);
120 const APInt one(1, 1);
121 const APInt two(1, 2);
123 EXPECT_EQ(0, neg_two.getSExtValue());
124 EXPECT_EQ(-1, neg_one.getSExtValue());
125 EXPECT_EQ(1u, neg_one.getZExtValue());
126 EXPECT_EQ(0u, zero.getZExtValue());
127 EXPECT_EQ(-1, one.getSExtValue());
128 EXPECT_EQ(1u, one.getZExtValue());
129 EXPECT_EQ(0u, two.getZExtValue());
130 EXPECT_EQ(0, two.getSExtValue());
132 // Basic equalities for 1-bit values.
133 EXPECT_EQ(zero, two);
134 EXPECT_EQ(zero, neg_two);
135 EXPECT_EQ(one, neg_one);
136 EXPECT_EQ(two, neg_two);
138 // Additions.
139 EXPECT_EQ(two, one + one);
140 EXPECT_EQ(zero, neg_one + one);
141 EXPECT_EQ(neg_two, neg_one + neg_one);
143 // Subtractions.
144 EXPECT_EQ(neg_two, neg_one - one);
145 EXPECT_EQ(two, one - neg_one);
146 EXPECT_EQ(zero, one - one);
148 // Shifts.
149 EXPECT_EQ(zero, one << one);
150 EXPECT_EQ(one, one << zero);
151 EXPECT_EQ(zero, one.shl(1));
152 EXPECT_EQ(one, one.shl(0));
153 EXPECT_EQ(zero, one.lshr(1));
154 EXPECT_EQ(zero, one.ashr(1));
156 // Multiplies.
157 EXPECT_EQ(neg_one, neg_one * one);
158 EXPECT_EQ(neg_one, one * neg_one);
159 EXPECT_EQ(one, neg_one * neg_one);
160 EXPECT_EQ(one, one * one);
162 // Divides.
163 EXPECT_EQ(neg_one, one.sdiv(neg_one));
164 EXPECT_EQ(neg_one, neg_one.sdiv(one));
165 EXPECT_EQ(one, neg_one.sdiv(neg_one));
166 EXPECT_EQ(one, one.sdiv(one));
168 EXPECT_EQ(neg_one, one.udiv(neg_one));
169 EXPECT_EQ(neg_one, neg_one.udiv(one));
170 EXPECT_EQ(one, neg_one.udiv(neg_one));
171 EXPECT_EQ(one, one.udiv(one));
173 // Remainders.
174 EXPECT_EQ(zero, neg_one.srem(one));
175 EXPECT_EQ(zero, neg_one.urem(one));
176 EXPECT_EQ(zero, one.srem(neg_one));
179 TEST(APIntTest, fromString) {
180 EXPECT_EQ(APInt(1, 0), APInt(1, "0", 1, 10));
181 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 10));
182 EXPECT_EQ(APInt(1, 1), APInt(1, "-1", 2, 10));
183 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 2));
184 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 8));
185 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 16));