1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/APInt.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "gtest/gtest.h"
19 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
20 TEST(APIntTest, ShiftLeftByZero) {
21 APInt One = APInt::getNullValue(65) + 1;
22 APInt Shl = One.shl(0);
27 TEST(APIntTest, i128_NegativeCount) {
28 APInt Minus3(128, static_cast<uint64_t>(-3), true);
29 EXPECT_EQ(126u, Minus3.countLeadingOnes());
30 EXPECT_EQ(-3, Minus3.getSExtValue());
32 APInt Minus1(128, static_cast<uint64_t>(-1), true);
33 EXPECT_EQ(0u, Minus1.countLeadingZeros());
34 EXPECT_EQ(128u, Minus1.countLeadingOnes());
35 EXPECT_EQ(128u, Minus1.getActiveBits());
36 EXPECT_EQ(0u, Minus1.countTrailingZeros());
37 EXPECT_EQ(128u, Minus1.countTrailingOnes());
38 EXPECT_EQ(128u, Minus1.countPopulation());
39 EXPECT_EQ(-1, Minus1.getSExtValue());
42 // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it.
43 #if defined(__llvm__) || !defined(__FreeBSD__)
45 TEST(APIntTest, i33_Count) {
46 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
47 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
48 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
49 EXPECT_EQ(33u, i33minus2.getActiveBits());
50 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
51 EXPECT_EQ(32u, i33minus2.countPopulation());
52 EXPECT_EQ(-2, i33minus2.getSExtValue());
53 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
58 TEST(APIntTest, i65_Count) {
59 APInt i65(65, 0, true);
60 EXPECT_EQ(65u, i65.countLeadingZeros());
61 EXPECT_EQ(0u, i65.countLeadingOnes());
62 EXPECT_EQ(0u, i65.getActiveBits());
63 EXPECT_EQ(1u, i65.getActiveWords());
64 EXPECT_EQ(65u, i65.countTrailingZeros());
65 EXPECT_EQ(0u, i65.countPopulation());
67 APInt i65minus(65, 0, true);
69 EXPECT_EQ(0u, i65minus.countLeadingZeros());
70 EXPECT_EQ(1u, i65minus.countLeadingOnes());
71 EXPECT_EQ(65u, i65minus.getActiveBits());
72 EXPECT_EQ(64u, i65minus.countTrailingZeros());
73 EXPECT_EQ(1u, i65minus.countPopulation());
76 TEST(APIntTest, i128_PositiveCount) {
77 APInt u128max = APInt::getAllOnesValue(128);
78 EXPECT_EQ(128u, u128max.countLeadingOnes());
79 EXPECT_EQ(0u, u128max.countLeadingZeros());
80 EXPECT_EQ(128u, u128max.getActiveBits());
81 EXPECT_EQ(0u, u128max.countTrailingZeros());
82 EXPECT_EQ(128u, u128max.countTrailingOnes());
83 EXPECT_EQ(128u, u128max.countPopulation());
85 APInt u64max(128, static_cast<uint64_t>(-1), false);
86 EXPECT_EQ(64u, u64max.countLeadingZeros());
87 EXPECT_EQ(0u, u64max.countLeadingOnes());
88 EXPECT_EQ(64u, u64max.getActiveBits());
89 EXPECT_EQ(0u, u64max.countTrailingZeros());
90 EXPECT_EQ(64u, u64max.countTrailingOnes());
91 EXPECT_EQ(64u, u64max.countPopulation());
92 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
94 APInt zero(128, 0, true);
95 EXPECT_EQ(128u, zero.countLeadingZeros());
96 EXPECT_EQ(0u, zero.countLeadingOnes());
97 EXPECT_EQ(0u, zero.getActiveBits());
98 EXPECT_EQ(128u, zero.countTrailingZeros());
99 EXPECT_EQ(0u, zero.countTrailingOnes());
100 EXPECT_EQ(0u, zero.countPopulation());
101 EXPECT_EQ(0u, zero.getSExtValue());
102 EXPECT_EQ(0u, zero.getZExtValue());
104 APInt one(128, 1, true);
105 EXPECT_EQ(127u, one.countLeadingZeros());
106 EXPECT_EQ(0u, one.countLeadingOnes());
107 EXPECT_EQ(1u, one.getActiveBits());
108 EXPECT_EQ(0u, one.countTrailingZeros());
109 EXPECT_EQ(1u, one.countTrailingOnes());
110 EXPECT_EQ(1u, one.countPopulation());
111 EXPECT_EQ(1, one.getSExtValue());
112 EXPECT_EQ(1u, one.getZExtValue());
115 TEST(APIntTest, i1) {
116 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
117 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
118 const APInt zero(1, 0);
119 const APInt one(1, 1);
120 const APInt two(1, 2);
122 EXPECT_EQ(0, neg_two.getSExtValue());
123 EXPECT_EQ(-1, neg_one.getSExtValue());
124 EXPECT_EQ(1u, neg_one.getZExtValue());
125 EXPECT_EQ(0u, zero.getZExtValue());
126 EXPECT_EQ(-1, one.getSExtValue());
127 EXPECT_EQ(1u, one.getZExtValue());
128 EXPECT_EQ(0u, two.getZExtValue());
129 EXPECT_EQ(0, two.getSExtValue());
131 // Basic equalities for 1-bit values.
132 EXPECT_EQ(zero, two);
133 EXPECT_EQ(zero, neg_two);
134 EXPECT_EQ(one, neg_one);
135 EXPECT_EQ(two, neg_two);
138 EXPECT_EQ(two, one + one);
139 EXPECT_EQ(zero, neg_one + one);
140 EXPECT_EQ(neg_two, neg_one + neg_one);
143 EXPECT_EQ(neg_two, neg_one - one);
144 EXPECT_EQ(two, one - neg_one);
145 EXPECT_EQ(zero, one - one);
148 EXPECT_EQ(zero, one << one);
149 EXPECT_EQ(one, one << zero);
150 EXPECT_EQ(zero, one.shl(1));
151 EXPECT_EQ(one, one.shl(0));
152 EXPECT_EQ(zero, one.lshr(1));
153 EXPECT_EQ(zero, one.ashr(1));
156 EXPECT_EQ(one, one.rotl(0));
157 EXPECT_EQ(one, one.rotl(1));
158 EXPECT_EQ(one, one.rotr(0));
159 EXPECT_EQ(one, one.rotr(1));
162 EXPECT_EQ(neg_one, neg_one * one);
163 EXPECT_EQ(neg_one, one * neg_one);
164 EXPECT_EQ(one, neg_one * neg_one);
165 EXPECT_EQ(one, one * one);
168 EXPECT_EQ(neg_one, one.sdiv(neg_one));
169 EXPECT_EQ(neg_one, neg_one.sdiv(one));
170 EXPECT_EQ(one, neg_one.sdiv(neg_one));
171 EXPECT_EQ(one, one.sdiv(one));
173 EXPECT_EQ(neg_one, one.udiv(neg_one));
174 EXPECT_EQ(neg_one, neg_one.udiv(one));
175 EXPECT_EQ(one, neg_one.udiv(neg_one));
176 EXPECT_EQ(one, one.udiv(one));
179 EXPECT_EQ(zero, neg_one.srem(one));
180 EXPECT_EQ(zero, neg_one.urem(one));
181 EXPECT_EQ(zero, one.srem(neg_one));
192 EXPECT_EQ(nine.srem(two), one);
193 EXPECT_EQ(nine.srem(-two), one);
194 EXPECT_EQ((-nine).srem(two), -one);
195 EXPECT_EQ((-nine).srem(-two), -one);
197 APInt::sdivrem(nine, two, q, r);
200 APInt::sdivrem(-nine, two, q, r);
203 APInt::sdivrem(nine, -two, q, r);
206 APInt::sdivrem(-nine, -two, q, r);
212 TEST(APIntTest, fromString) {
213 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
214 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
215 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
216 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
217 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
219 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
220 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
221 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
222 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
223 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
225 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
226 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
227 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
228 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
229 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
232 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
233 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
234 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
235 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
236 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
237 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
239 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
240 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
241 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
242 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
243 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
244 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
246 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
247 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
248 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
249 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
250 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
251 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
254 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
255 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
256 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
257 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
258 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
259 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
261 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
262 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
263 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
264 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
265 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
266 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
269 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
270 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
271 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
272 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
273 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
274 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
276 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
277 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
278 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
279 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
280 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
281 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
283 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
284 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
285 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
286 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
287 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
288 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
290 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
291 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
292 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
293 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
294 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
295 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
298 TEST(APIntTest, FromArray) {
299 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
302 TEST(APIntTest, StringBitsNeeded2) {
303 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
304 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
305 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
306 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
307 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
309 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
310 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
311 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
312 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
313 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
315 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
316 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
317 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
318 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
319 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
322 TEST(APIntTest, StringBitsNeeded8) {
323 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
324 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
325 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
326 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
327 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
329 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
330 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
331 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
332 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
333 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
335 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
336 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
337 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
338 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
339 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
342 TEST(APIntTest, StringBitsNeeded10) {
343 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
344 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
345 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
346 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
347 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
348 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
350 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
351 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
352 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
353 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
354 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
356 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
357 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
358 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
359 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
360 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
363 TEST(APIntTest, StringBitsNeeded16) {
364 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
365 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
366 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
367 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
368 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
370 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
371 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
372 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
373 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
374 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
376 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
377 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
378 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
379 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
380 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
383 TEST(APIntTest, toString) {
387 APInt(8, 0).toString(S, 2, true, true);
388 EXPECT_EQ(S.str().str(), "0b0");
390 APInt(8, 0).toString(S, 8, true, true);
391 EXPECT_EQ(S.str().str(), "00");
393 APInt(8, 0).toString(S, 10, true, true);
394 EXPECT_EQ(S.str().str(), "0");
396 APInt(8, 0).toString(S, 16, true, true);
397 EXPECT_EQ(S.str().str(), "0x0");
399 APInt(8, 0).toString(S, 36, true, false);
400 EXPECT_EQ(S.str().str(), "0");
404 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
405 EXPECT_EQ(S.str().str(), "0b11111111");
407 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
408 EXPECT_EQ(S.str().str(), "0377");
410 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
411 EXPECT_EQ(S.str().str(), "255");
413 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
414 EXPECT_EQ(S.str().str(), "0xFF");
416 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
417 EXPECT_EQ(S.str().str(), "73");
421 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
422 EXPECT_EQ(S.str().str(), "-0b1");
424 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
425 EXPECT_EQ(S.str().str(), "-01");
427 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
428 EXPECT_EQ(S.str().str(), "-1");
430 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
431 EXPECT_EQ(S.str().str(), "-0x1");
433 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
434 EXPECT_EQ(S.str().str(), "-1");
438 TEST(APIntTest, Log2) {
439 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
440 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
441 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
442 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
443 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
444 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
445 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
446 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
447 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
450 TEST(APIntTest, magic) {
451 EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
452 EXPECT_EQ(APInt(32, 3).magic().s, 0U);
453 EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
454 EXPECT_EQ(APInt(32, 5).magic().s, 1U);
455 EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
456 EXPECT_EQ(APInt(32, 7).magic().s, 2U);
459 TEST(APIntTest, magicu) {
460 EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
461 EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
462 EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
463 EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
464 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
465 EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
466 EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
467 EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
470 #ifdef GTEST_HAS_DEATH_TEST
472 TEST(APIntTest, StringDeath) {
473 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
474 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
475 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
476 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
477 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
478 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
479 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
480 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
481 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
486 TEST(APIntTest, mul_clear) {
487 APInt ValA(65, -1ULL);
492 EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
495 TEST(APIntTest, Rotate) {
496 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
497 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
498 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
499 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
500 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
502 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
503 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
504 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
505 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
506 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
508 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
509 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
510 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
511 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
512 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
514 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
515 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
516 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
517 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
518 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
520 APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
521 APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
522 EXPECT_EQ(Rot, Big.rotr(144));
525 TEST(APIntTest, Splat) {
527 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
528 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
531 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
532 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
535 TEST(APIntTest, tcDecrement) {
536 // Test single word decrement.
540 integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1);
541 integerPart carry = APInt::tcDecrement(&singleWord, 1);
542 EXPECT_EQ(carry, integerPart(0));
543 EXPECT_EQ(singleWord, ~integerPart(0) >> 1);
548 integerPart singleWord = 0;
549 integerPart carry = APInt::tcDecrement(&singleWord, 1);
550 EXPECT_EQ(carry, integerPart(1));
551 EXPECT_EQ(singleWord, ~integerPart(0));
554 // Test multiword decrement.
556 // No across word borrow, no out borrow.
558 integerPart test[4] = {0x1, 0x1, 0x1, 0x1};
559 integerPart expected[4] = {0x0, 0x1, 0x1, 0x1};
560 APInt::tcDecrement(test, 4);
561 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
564 // 1 across word borrow, no out borrow.
566 integerPart test[4] = {0x0, 0xF, 0x1, 0x1};
567 integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1};
568 integerPart carry = APInt::tcDecrement(test, 4);
569 EXPECT_EQ(carry, integerPart(0));
570 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
573 // 2 across word borrow, no out borrow.
575 integerPart test[4] = {0x0, 0x0, 0xC, 0x1};
576 integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1};
577 integerPart carry = APInt::tcDecrement(test, 4);
578 EXPECT_EQ(carry, integerPart(0));
579 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
582 // 3 across word borrow, no out borrow.
584 integerPart test[4] = {0x0, 0x0, 0x0, 0x1};
585 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0};
586 integerPart carry = APInt::tcDecrement(test, 4);
587 EXPECT_EQ(carry, integerPart(0));
588 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
591 // 3 across word borrow, with out borrow.
593 integerPart test[4] = {0x0, 0x0, 0x0, 0x0};
594 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)};
595 integerPart carry = APInt::tcDecrement(test, 4);
596 EXPECT_EQ(carry, integerPart(1));
597 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
601 TEST(APIntTest, arrayAccess) {
602 // Single word check.
603 uint64_t E1 = 0x2CA7F46BF6569915ULL;
605 for (unsigned i = 0, e = 64; i < e; ++i) {
606 EXPECT_EQ(bool(E1 & (1ULL << i)),
611 integerPart E2[4] = {
612 0xEB6EB136591CBA21ULL,
613 0x7B9358BD6A33F10AULL,
614 0x7E7FFA5EADD8846ULL,
615 0x305F341CA00B613DULL
617 APInt A2(integerPartWidth*4, E2);
618 for (unsigned i = 0; i < 4; ++i) {
619 for (unsigned j = 0; j < integerPartWidth; ++j) {
620 EXPECT_EQ(bool(E2[i] & (1ULL << j)),
621 A2[i*integerPartWidth + j]);
626 TEST(APIntTest, LargeAPIntConstruction) {
627 // Check that we can properly construct very large APInt. It is very
628 // unlikely that people will ever do this, but it is a legal input,
629 // so we should not crash on it.
630 APInt A9(UINT32_MAX, 0);
631 EXPECT_FALSE(A9.getBoolValue());
634 TEST(APIntTest, nearestLogBase2) {
635 // Single word check.
638 uint64_t I1 = 0x1800001;
640 EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
643 uint64_t I2 = 0x1000011;
645 EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
647 // Test ties round up.
648 uint64_t I3 = 0x1800000;
650 EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
652 // Multiple word check.
655 integerPart I4[4] = {0x0, 0xF, 0x18, 0x0};
656 APInt A4(integerPartWidth*4, I4);
657 EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
660 integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
661 APInt A5(integerPartWidth*4, I5);
662 EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
664 // Test ties round up.
665 uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
666 APInt A6(integerPartWidth*4, I6);
667 EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
669 // Test BitWidth == 1 special cases.
671 EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
673 EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
675 // Test the zero case when we have a bit width large enough such
676 // that the bit width is larger than UINT32_MAX-1.
677 APInt A9(UINT32_MAX, 0);
678 EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
681 #if defined(__clang__)
682 // Disable the warning that triggers on exactly what is being tested.
683 #pragma clang diagnostic push
684 #pragma clang diagnostic ignored "-Wself-move"
686 TEST(APIntTest, SelfMoveAssignment) {
687 APInt X(32, 0xdeadbeef);
689 EXPECT_EQ(32u, X.getBitWidth());
690 EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
692 uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
695 EXPECT_EQ(128u, Y.getBitWidth());
696 EXPECT_EQ(~0ULL, Y.getLimitedValue());
697 const uint64_t *Raw = Y.getRawData();
698 EXPECT_EQ(2u, Y.getNumWords());
699 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
700 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
702 #if defined(__clang__)
703 #pragma clang diagnostic pop