Merge branch 'master' into systemz
[llvm/systemz.git] / unittests / ADT / APIntTest.cpp
blob648faf13ad81a2cbe1ea09695a77e84dcad3f6fa
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 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
21 TEST(APIntTest, ShiftLeftByZero) {
22 APInt One = APInt::getNullValue(65) + 1;
23 APInt Shl = One.shl(0);
24 EXPECT_EQ(true, Shl[0]);
25 EXPECT_EQ(false, Shl[1]);
28 TEST(APIntTest, i128_NegativeCount) {
29 APInt Minus3(128, static_cast<uint64_t>(-3), true);
30 EXPECT_EQ(126u, Minus3.countLeadingOnes());
31 EXPECT_EQ(-3, Minus3.getSExtValue());
33 APInt Minus1(128, static_cast<uint64_t>(-1), true);
34 EXPECT_EQ(0u, Minus1.countLeadingZeros());
35 EXPECT_EQ(128u, Minus1.countLeadingOnes());
36 EXPECT_EQ(128u, Minus1.getActiveBits());
37 EXPECT_EQ(0u, Minus1.countTrailingZeros());
38 EXPECT_EQ(128u, Minus1.countTrailingOnes());
39 EXPECT_EQ(128u, Minus1.countPopulation());
40 EXPECT_EQ(-1, Minus1.getSExtValue());
43 TEST(APIntTest, i33_Count) {
44 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
45 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
46 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
47 EXPECT_EQ(33u, i33minus2.getActiveBits());
48 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
49 EXPECT_EQ(32u, i33minus2.countPopulation());
50 EXPECT_EQ(-2, i33minus2.getSExtValue());
51 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
54 TEST(APIntTest, i65_Count) {
55 APInt i65minus(65, 0, true);
56 i65minus.set(64);
57 EXPECT_EQ(0u, i65minus.countLeadingZeros());
58 EXPECT_EQ(1u, i65minus.countLeadingOnes());
59 EXPECT_EQ(65u, i65minus.getActiveBits());
60 EXPECT_EQ(64u, i65minus.countTrailingZeros());
61 EXPECT_EQ(1u, i65minus.countPopulation());
64 TEST(APIntTest, i128_PositiveCount) {
65 APInt u128max = APInt::getAllOnesValue(128);
66 EXPECT_EQ(128u, u128max.countLeadingOnes());
67 EXPECT_EQ(0u, u128max.countLeadingZeros());
68 EXPECT_EQ(128u, u128max.getActiveBits());
69 EXPECT_EQ(0u, u128max.countTrailingZeros());
70 EXPECT_EQ(128u, u128max.countTrailingOnes());
71 EXPECT_EQ(128u, u128max.countPopulation());
73 APInt u64max(128, static_cast<uint64_t>(-1), false);
74 EXPECT_EQ(64u, u64max.countLeadingZeros());
75 EXPECT_EQ(0u, u64max.countLeadingOnes());
76 EXPECT_EQ(64u, u64max.getActiveBits());
77 EXPECT_EQ(0u, u64max.countTrailingZeros());
78 EXPECT_EQ(64u, u64max.countTrailingOnes());
79 EXPECT_EQ(64u, u64max.countPopulation());
80 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
82 APInt zero(128, 0, true);
83 EXPECT_EQ(128u, zero.countLeadingZeros());
84 EXPECT_EQ(0u, zero.countLeadingOnes());
85 EXPECT_EQ(0u, zero.getActiveBits());
86 EXPECT_EQ(128u, zero.countTrailingZeros());
87 EXPECT_EQ(0u, zero.countTrailingOnes());
88 EXPECT_EQ(0u, zero.countPopulation());
89 EXPECT_EQ(0u, zero.getSExtValue());
90 EXPECT_EQ(0u, zero.getZExtValue());
92 APInt one(128, 1, true);
93 EXPECT_EQ(127u, one.countLeadingZeros());
94 EXPECT_EQ(0u, one.countLeadingOnes());
95 EXPECT_EQ(1u, one.getActiveBits());
96 EXPECT_EQ(0u, one.countTrailingZeros());
97 EXPECT_EQ(1u, one.countTrailingOnes());
98 EXPECT_EQ(1u, one.countPopulation());
99 EXPECT_EQ(1, one.getSExtValue());
100 EXPECT_EQ(1u, one.getZExtValue());
103 TEST(APIntTest, i1) {
104 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
105 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
106 const APInt zero(1, 0);
107 const APInt one(1, 1);
108 const APInt two(1, 2);
110 EXPECT_EQ(0, neg_two.getSExtValue());
111 EXPECT_EQ(-1, neg_one.getSExtValue());
112 EXPECT_EQ(1u, neg_one.getZExtValue());
113 EXPECT_EQ(0u, zero.getZExtValue());
114 EXPECT_EQ(-1, one.getSExtValue());
115 EXPECT_EQ(1u, one.getZExtValue());
116 EXPECT_EQ(0u, two.getZExtValue());
117 EXPECT_EQ(0, two.getSExtValue());
119 // Basic equalities for 1-bit values.
120 EXPECT_EQ(zero, two);
121 EXPECT_EQ(zero, neg_two);
122 EXPECT_EQ(one, neg_one);
123 EXPECT_EQ(two, neg_two);
125 // Additions.
126 EXPECT_EQ(two, one + one);
127 EXPECT_EQ(zero, neg_one + one);
128 EXPECT_EQ(neg_two, neg_one + neg_one);
130 // Subtractions.
131 EXPECT_EQ(neg_two, neg_one - one);
132 EXPECT_EQ(two, one - neg_one);
133 EXPECT_EQ(zero, one - one);
135 // Shifts.
136 EXPECT_EQ(zero, one << one);
137 EXPECT_EQ(one, one << zero);
138 EXPECT_EQ(zero, one.shl(1));
139 EXPECT_EQ(one, one.shl(0));
140 EXPECT_EQ(zero, one.lshr(1));
141 EXPECT_EQ(zero, one.ashr(1));
143 // Multiplies.
144 EXPECT_EQ(neg_one, neg_one * one);
145 EXPECT_EQ(neg_one, one * neg_one);
146 EXPECT_EQ(one, neg_one * neg_one);
147 EXPECT_EQ(one, one * one);
149 // Divides.
150 EXPECT_EQ(neg_one, one.sdiv(neg_one));
151 EXPECT_EQ(neg_one, neg_one.sdiv(one));
152 EXPECT_EQ(one, neg_one.sdiv(neg_one));
153 EXPECT_EQ(one, one.sdiv(one));
155 EXPECT_EQ(neg_one, one.udiv(neg_one));
156 EXPECT_EQ(neg_one, neg_one.udiv(one));
157 EXPECT_EQ(one, neg_one.udiv(neg_one));
158 EXPECT_EQ(one, one.udiv(one));
160 // Remainders.
161 EXPECT_EQ(zero, neg_one.srem(one));
162 EXPECT_EQ(zero, neg_one.urem(one));
163 EXPECT_EQ(zero, one.srem(neg_one));
166 TEST(APIntTest, fromString) {
167 EXPECT_EQ(APInt(1, 0), APInt(1, "0", 1, 10));
168 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 10));
169 EXPECT_EQ(APInt(1, 1), APInt(1, "-1", 2, 10));
170 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 2));
171 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 8));
172 EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 16));