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 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/APInt.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "gtest/gtest.h"
14 #include <array>
15
16 using namespace llvm;
17
18 namespace {
19
TEST(APIntTest,ValueInit)20 TEST(APIntTest, ValueInit) {
21 APInt Zero = APInt();
22 EXPECT_TRUE(!Zero);
23 EXPECT_TRUE(!Zero.zext(64));
24 EXPECT_TRUE(!Zero.sext(64));
25 }
26
27 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
TEST(APIntTest,ShiftLeftByZero)28 TEST(APIntTest, ShiftLeftByZero) {
29 APInt One = APInt::getNullValue(65) + 1;
30 APInt Shl = One.shl(0);
31 EXPECT_TRUE(Shl[0]);
32 EXPECT_FALSE(Shl[1]);
33 }
34
TEST(APIntTest,i128_NegativeCount)35 TEST(APIntTest, i128_NegativeCount) {
36 APInt Minus3(128, static_cast<uint64_t>(-3), true);
37 EXPECT_EQ(126u, Minus3.countLeadingOnes());
38 EXPECT_EQ(-3, Minus3.getSExtValue());
39
40 APInt Minus1(128, static_cast<uint64_t>(-1), true);
41 EXPECT_EQ(0u, Minus1.countLeadingZeros());
42 EXPECT_EQ(128u, Minus1.countLeadingOnes());
43 EXPECT_EQ(128u, Minus1.getActiveBits());
44 EXPECT_EQ(0u, Minus1.countTrailingZeros());
45 EXPECT_EQ(128u, Minus1.countTrailingOnes());
46 EXPECT_EQ(128u, Minus1.countPopulation());
47 EXPECT_EQ(-1, Minus1.getSExtValue());
48 }
49
50 // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it.
51 #if defined(__llvm__) || !defined(__FreeBSD__)
52
TEST(APIntTest,i33_Count)53 TEST(APIntTest, i33_Count) {
54 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
55 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
56 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
57 EXPECT_EQ(33u, i33minus2.getActiveBits());
58 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
59 EXPECT_EQ(32u, i33minus2.countPopulation());
60 EXPECT_EQ(-2, i33minus2.getSExtValue());
61 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
62 }
63
64 #endif
65
TEST(APIntTest,i65_Count)66 TEST(APIntTest, i65_Count) {
67 APInt i65(65, 0, true);
68 EXPECT_EQ(65u, i65.countLeadingZeros());
69 EXPECT_EQ(0u, i65.countLeadingOnes());
70 EXPECT_EQ(0u, i65.getActiveBits());
71 EXPECT_EQ(1u, i65.getActiveWords());
72 EXPECT_EQ(65u, i65.countTrailingZeros());
73 EXPECT_EQ(0u, i65.countPopulation());
74
75 APInt i65minus(65, 0, true);
76 i65minus.setBit(64);
77 EXPECT_EQ(0u, i65minus.countLeadingZeros());
78 EXPECT_EQ(1u, i65minus.countLeadingOnes());
79 EXPECT_EQ(65u, i65minus.getActiveBits());
80 EXPECT_EQ(64u, i65minus.countTrailingZeros());
81 EXPECT_EQ(1u, i65minus.countPopulation());
82 }
83
TEST(APIntTest,i128_PositiveCount)84 TEST(APIntTest, i128_PositiveCount) {
85 APInt u128max = APInt::getAllOnesValue(128);
86 EXPECT_EQ(128u, u128max.countLeadingOnes());
87 EXPECT_EQ(0u, u128max.countLeadingZeros());
88 EXPECT_EQ(128u, u128max.getActiveBits());
89 EXPECT_EQ(0u, u128max.countTrailingZeros());
90 EXPECT_EQ(128u, u128max.countTrailingOnes());
91 EXPECT_EQ(128u, u128max.countPopulation());
92
93 APInt u64max(128, static_cast<uint64_t>(-1), false);
94 EXPECT_EQ(64u, u64max.countLeadingZeros());
95 EXPECT_EQ(0u, u64max.countLeadingOnes());
96 EXPECT_EQ(64u, u64max.getActiveBits());
97 EXPECT_EQ(0u, u64max.countTrailingZeros());
98 EXPECT_EQ(64u, u64max.countTrailingOnes());
99 EXPECT_EQ(64u, u64max.countPopulation());
100 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
101
102 APInt zero(128, 0, true);
103 EXPECT_EQ(128u, zero.countLeadingZeros());
104 EXPECT_EQ(0u, zero.countLeadingOnes());
105 EXPECT_EQ(0u, zero.getActiveBits());
106 EXPECT_EQ(128u, zero.countTrailingZeros());
107 EXPECT_EQ(0u, zero.countTrailingOnes());
108 EXPECT_EQ(0u, zero.countPopulation());
109 EXPECT_EQ(0u, zero.getSExtValue());
110 EXPECT_EQ(0u, zero.getZExtValue());
111
112 APInt one(128, 1, true);
113 EXPECT_EQ(127u, one.countLeadingZeros());
114 EXPECT_EQ(0u, one.countLeadingOnes());
115 EXPECT_EQ(1u, one.getActiveBits());
116 EXPECT_EQ(0u, one.countTrailingZeros());
117 EXPECT_EQ(1u, one.countTrailingOnes());
118 EXPECT_EQ(1u, one.countPopulation());
119 EXPECT_EQ(1, one.getSExtValue());
120 EXPECT_EQ(1u, one.getZExtValue());
121 }
122
TEST(APIntTest,i1)123 TEST(APIntTest, i1) {
124 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
125 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
126 const APInt zero(1, 0);
127 const APInt one(1, 1);
128 const APInt two(1, 2);
129
130 EXPECT_EQ(0, neg_two.getSExtValue());
131 EXPECT_EQ(-1, neg_one.getSExtValue());
132 EXPECT_EQ(1u, neg_one.getZExtValue());
133 EXPECT_EQ(0u, zero.getZExtValue());
134 EXPECT_EQ(-1, one.getSExtValue());
135 EXPECT_EQ(1u, one.getZExtValue());
136 EXPECT_EQ(0u, two.getZExtValue());
137 EXPECT_EQ(0, two.getSExtValue());
138
139 // Basic equalities for 1-bit values.
140 EXPECT_EQ(zero, two);
141 EXPECT_EQ(zero, neg_two);
142 EXPECT_EQ(one, neg_one);
143 EXPECT_EQ(two, neg_two);
144
145 // Min/max signed values.
146 EXPECT_TRUE(zero.isMaxSignedValue());
147 EXPECT_FALSE(one.isMaxSignedValue());
148 EXPECT_FALSE(zero.isMinSignedValue());
149 EXPECT_TRUE(one.isMinSignedValue());
150
151 // Additions.
152 EXPECT_EQ(two, one + one);
153 EXPECT_EQ(zero, neg_one + one);
154 EXPECT_EQ(neg_two, neg_one + neg_one);
155
156 // Subtractions.
157 EXPECT_EQ(neg_two, neg_one - one);
158 EXPECT_EQ(two, one - neg_one);
159 EXPECT_EQ(zero, one - one);
160
161 // Shifts.
162 EXPECT_EQ(zero, one << one);
163 EXPECT_EQ(one, one << zero);
164 EXPECT_EQ(zero, one.shl(1));
165 EXPECT_EQ(one, one.shl(0));
166 EXPECT_EQ(zero, one.lshr(1));
167 EXPECT_EQ(zero, one.ashr(1));
168
169 // Rotates.
170 EXPECT_EQ(one, one.rotl(0));
171 EXPECT_EQ(one, one.rotl(1));
172 EXPECT_EQ(one, one.rotr(0));
173 EXPECT_EQ(one, one.rotr(1));
174
175 // Multiplies.
176 EXPECT_EQ(neg_one, neg_one * one);
177 EXPECT_EQ(neg_one, one * neg_one);
178 EXPECT_EQ(one, neg_one * neg_one);
179 EXPECT_EQ(one, one * one);
180
181 // Divides.
182 EXPECT_EQ(neg_one, one.sdiv(neg_one));
183 EXPECT_EQ(neg_one, neg_one.sdiv(one));
184 EXPECT_EQ(one, neg_one.sdiv(neg_one));
185 EXPECT_EQ(one, one.sdiv(one));
186
187 EXPECT_EQ(neg_one, one.udiv(neg_one));
188 EXPECT_EQ(neg_one, neg_one.udiv(one));
189 EXPECT_EQ(one, neg_one.udiv(neg_one));
190 EXPECT_EQ(one, one.udiv(one));
191
192 // Remainders.
193 EXPECT_EQ(zero, neg_one.srem(one));
194 EXPECT_EQ(zero, neg_one.urem(one));
195 EXPECT_EQ(zero, one.srem(neg_one));
196
197 // sdivrem
198 {
199 APInt q(8, 0);
200 APInt r(8, 0);
201 APInt one(8, 1);
202 APInt two(8, 2);
203 APInt nine(8, 9);
204 APInt four(8, 4);
205
206 EXPECT_EQ(nine.srem(two), one);
207 EXPECT_EQ(nine.srem(-two), one);
208 EXPECT_EQ((-nine).srem(two), -one);
209 EXPECT_EQ((-nine).srem(-two), -one);
210
211 APInt::sdivrem(nine, two, q, r);
212 EXPECT_EQ(four, q);
213 EXPECT_EQ(one, r);
214 APInt::sdivrem(-nine, two, q, r);
215 EXPECT_EQ(-four, q);
216 EXPECT_EQ(-one, r);
217 APInt::sdivrem(nine, -two, q, r);
218 EXPECT_EQ(-four, q);
219 EXPECT_EQ(one, r);
220 APInt::sdivrem(-nine, -two, q, r);
221 EXPECT_EQ(four, q);
222 EXPECT_EQ(-one, r);
223 }
224 }
225
TEST(APIntTest,compare)226 TEST(APIntTest, compare) {
227 std::array<APInt, 5> testVals{{
228 APInt{16, 2},
229 APInt{16, 1},
230 APInt{16, 0},
231 APInt{16, (uint64_t)-1, true},
232 APInt{16, (uint64_t)-2, true},
233 }};
234
235 for (auto &arg1 : testVals)
236 for (auto &arg2 : testVals) {
237 auto uv1 = arg1.getZExtValue();
238 auto uv2 = arg2.getZExtValue();
239 auto sv1 = arg1.getSExtValue();
240 auto sv2 = arg2.getSExtValue();
241
242 EXPECT_EQ(uv1 < uv2, arg1.ult(arg2));
243 EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2));
244 EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2));
245 EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2));
246
247 EXPECT_EQ(sv1 < sv2, arg1.slt(arg2));
248 EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2));
249 EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2));
250 EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2));
251
252 EXPECT_EQ(uv1 < uv2, arg1.ult(uv2));
253 EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2));
254 EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2));
255 EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2));
256
257 EXPECT_EQ(sv1 < sv2, arg1.slt(sv2));
258 EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2));
259 EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2));
260 EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2));
261 }
262 }
263
TEST(APIntTest,compareWithRawIntegers)264 TEST(APIntTest, compareWithRawIntegers) {
265 EXPECT_TRUE(!APInt(8, 1).uge(256));
266 EXPECT_TRUE(!APInt(8, 1).ugt(256));
267 EXPECT_TRUE( APInt(8, 1).ule(256));
268 EXPECT_TRUE( APInt(8, 1).ult(256));
269 EXPECT_TRUE(!APInt(8, 1).sge(256));
270 EXPECT_TRUE(!APInt(8, 1).sgt(256));
271 EXPECT_TRUE( APInt(8, 1).sle(256));
272 EXPECT_TRUE( APInt(8, 1).slt(256));
273 EXPECT_TRUE(!(APInt(8, 0) == 256));
274 EXPECT_TRUE( APInt(8, 0) != 256);
275 EXPECT_TRUE(!(APInt(8, 1) == 256));
276 EXPECT_TRUE( APInt(8, 1) != 256);
277
278 auto uint64max = UINT64_MAX;
279 auto int64max = INT64_MAX;
280 auto int64min = INT64_MIN;
281
282 auto u64 = APInt{128, uint64max};
283 auto s64 = APInt{128, static_cast<uint64_t>(int64max), true};
284 auto big = u64 + 1;
285
286 EXPECT_TRUE( u64.uge(uint64max));
287 EXPECT_TRUE(!u64.ugt(uint64max));
288 EXPECT_TRUE( u64.ule(uint64max));
289 EXPECT_TRUE(!u64.ult(uint64max));
290 EXPECT_TRUE( u64.sge(int64max));
291 EXPECT_TRUE( u64.sgt(int64max));
292 EXPECT_TRUE(!u64.sle(int64max));
293 EXPECT_TRUE(!u64.slt(int64max));
294 EXPECT_TRUE( u64.sge(int64min));
295 EXPECT_TRUE( u64.sgt(int64min));
296 EXPECT_TRUE(!u64.sle(int64min));
297 EXPECT_TRUE(!u64.slt(int64min));
298
299 EXPECT_TRUE(u64 == uint64max);
300 EXPECT_TRUE(u64 != int64max);
301 EXPECT_TRUE(u64 != int64min);
302
303 EXPECT_TRUE(!s64.uge(uint64max));
304 EXPECT_TRUE(!s64.ugt(uint64max));
305 EXPECT_TRUE( s64.ule(uint64max));
306 EXPECT_TRUE( s64.ult(uint64max));
307 EXPECT_TRUE( s64.sge(int64max));
308 EXPECT_TRUE(!s64.sgt(int64max));
309 EXPECT_TRUE( s64.sle(int64max));
310 EXPECT_TRUE(!s64.slt(int64max));
311 EXPECT_TRUE( s64.sge(int64min));
312 EXPECT_TRUE( s64.sgt(int64min));
313 EXPECT_TRUE(!s64.sle(int64min));
314 EXPECT_TRUE(!s64.slt(int64min));
315
316 EXPECT_TRUE(s64 != uint64max);
317 EXPECT_TRUE(s64 == int64max);
318 EXPECT_TRUE(s64 != int64min);
319
320 EXPECT_TRUE( big.uge(uint64max));
321 EXPECT_TRUE( big.ugt(uint64max));
322 EXPECT_TRUE(!big.ule(uint64max));
323 EXPECT_TRUE(!big.ult(uint64max));
324 EXPECT_TRUE( big.sge(int64max));
325 EXPECT_TRUE( big.sgt(int64max));
326 EXPECT_TRUE(!big.sle(int64max));
327 EXPECT_TRUE(!big.slt(int64max));
328 EXPECT_TRUE( big.sge(int64min));
329 EXPECT_TRUE( big.sgt(int64min));
330 EXPECT_TRUE(!big.sle(int64min));
331 EXPECT_TRUE(!big.slt(int64min));
332
333 EXPECT_TRUE(big != uint64max);
334 EXPECT_TRUE(big != int64max);
335 EXPECT_TRUE(big != int64min);
336 }
337
TEST(APIntTest,compareWithInt64Min)338 TEST(APIntTest, compareWithInt64Min) {
339 int64_t edge = INT64_MIN;
340 int64_t edgeP1 = edge + 1;
341 int64_t edgeM1 = INT64_MAX;
342 auto a = APInt{64, static_cast<uint64_t>(edge), true};
343
344 EXPECT_TRUE(!a.slt(edge));
345 EXPECT_TRUE( a.sle(edge));
346 EXPECT_TRUE(!a.sgt(edge));
347 EXPECT_TRUE( a.sge(edge));
348 EXPECT_TRUE( a.slt(edgeP1));
349 EXPECT_TRUE( a.sle(edgeP1));
350 EXPECT_TRUE(!a.sgt(edgeP1));
351 EXPECT_TRUE(!a.sge(edgeP1));
352 EXPECT_TRUE( a.slt(edgeM1));
353 EXPECT_TRUE( a.sle(edgeM1));
354 EXPECT_TRUE(!a.sgt(edgeM1));
355 EXPECT_TRUE(!a.sge(edgeM1));
356 }
357
TEST(APIntTest,compareWithHalfInt64Max)358 TEST(APIntTest, compareWithHalfInt64Max) {
359 uint64_t edge = 0x4000000000000000;
360 uint64_t edgeP1 = edge + 1;
361 uint64_t edgeM1 = edge - 1;
362 auto a = APInt{64, edge};
363
364 EXPECT_TRUE(!a.ult(edge));
365 EXPECT_TRUE( a.ule(edge));
366 EXPECT_TRUE(!a.ugt(edge));
367 EXPECT_TRUE( a.uge(edge));
368 EXPECT_TRUE( a.ult(edgeP1));
369 EXPECT_TRUE( a.ule(edgeP1));
370 EXPECT_TRUE(!a.ugt(edgeP1));
371 EXPECT_TRUE(!a.uge(edgeP1));
372 EXPECT_TRUE(!a.ult(edgeM1));
373 EXPECT_TRUE(!a.ule(edgeM1));
374 EXPECT_TRUE( a.ugt(edgeM1));
375 EXPECT_TRUE( a.uge(edgeM1));
376
377 EXPECT_TRUE(!a.slt(edge));
378 EXPECT_TRUE( a.sle(edge));
379 EXPECT_TRUE(!a.sgt(edge));
380 EXPECT_TRUE( a.sge(edge));
381 EXPECT_TRUE( a.slt(edgeP1));
382 EXPECT_TRUE( a.sle(edgeP1));
383 EXPECT_TRUE(!a.sgt(edgeP1));
384 EXPECT_TRUE(!a.sge(edgeP1));
385 EXPECT_TRUE(!a.slt(edgeM1));
386 EXPECT_TRUE(!a.sle(edgeM1));
387 EXPECT_TRUE( a.sgt(edgeM1));
388 EXPECT_TRUE( a.sge(edgeM1));
389 }
390
TEST(APIntTest,compareLargeIntegers)391 TEST(APIntTest, compareLargeIntegers) {
392 // Make sure all the combinations of signed comparisons work with big ints.
393 auto One = APInt{128, static_cast<uint64_t>(1), true};
394 auto Two = APInt{128, static_cast<uint64_t>(2), true};
395 auto MinusOne = APInt{128, static_cast<uint64_t>(-1), true};
396 auto MinusTwo = APInt{128, static_cast<uint64_t>(-2), true};
397
398 EXPECT_TRUE(!One.slt(One));
399 EXPECT_TRUE(!Two.slt(One));
400 EXPECT_TRUE(MinusOne.slt(One));
401 EXPECT_TRUE(MinusTwo.slt(One));
402
403 EXPECT_TRUE(One.slt(Two));
404 EXPECT_TRUE(!Two.slt(Two));
405 EXPECT_TRUE(MinusOne.slt(Two));
406 EXPECT_TRUE(MinusTwo.slt(Two));
407
408 EXPECT_TRUE(!One.slt(MinusOne));
409 EXPECT_TRUE(!Two.slt(MinusOne));
410 EXPECT_TRUE(!MinusOne.slt(MinusOne));
411 EXPECT_TRUE(MinusTwo.slt(MinusOne));
412
413 EXPECT_TRUE(!One.slt(MinusTwo));
414 EXPECT_TRUE(!Two.slt(MinusTwo));
415 EXPECT_TRUE(!MinusOne.slt(MinusTwo));
416 EXPECT_TRUE(!MinusTwo.slt(MinusTwo));
417 }
418
419
420 // Tests different div/rem varaints using scheme (a * b + c) / a
testDiv(APInt a,APInt b,APInt c)421 void testDiv(APInt a, APInt b, APInt c) {
422 ASSERT_TRUE(a.uge(b)); // Must: a >= b
423 ASSERT_TRUE(a.ugt(c)); // Must: a > c
424
425 auto p = a * b + c;
426
427 auto q = p.udiv(a);
428 auto r = p.urem(a);
429 EXPECT_EQ(b, q);
430 EXPECT_EQ(c, r);
431 APInt::udivrem(p, a, q, r);
432 EXPECT_EQ(b, q);
433 EXPECT_EQ(c, r);
434 q = p.sdiv(a);
435 r = p.srem(a);
436 EXPECT_EQ(b, q);
437 EXPECT_EQ(c, r);
438 APInt::sdivrem(p, a, q, r);
439 EXPECT_EQ(b, q);
440 EXPECT_EQ(c, r);
441
442 if (b.ugt(c)) { // Test also symmetric case
443 q = p.udiv(b);
444 r = p.urem(b);
445 EXPECT_EQ(a, q);
446 EXPECT_EQ(c, r);
447 APInt::udivrem(p, b, q, r);
448 EXPECT_EQ(a, q);
449 EXPECT_EQ(c, r);
450 q = p.sdiv(b);
451 r = p.srem(b);
452 EXPECT_EQ(a, q);
453 EXPECT_EQ(c, r);
454 APInt::sdivrem(p, b, q, r);
455 EXPECT_EQ(a, q);
456 EXPECT_EQ(c, r);
457 }
458 }
459
TEST(APIntTest,divrem_big1)460 TEST(APIntTest, divrem_big1) {
461 // Tests KnuthDiv rare step D6
462 testDiv({256, "1ffffffffffffffff", 16},
463 {256, "1ffffffffffffffff", 16},
464 {256, 0});
465 }
466
TEST(APIntTest,divrem_big2)467 TEST(APIntTest, divrem_big2) {
468 // Tests KnuthDiv rare step D6
469 testDiv({1024, "112233ceff"
470 "cecece000000ffffffffffffffffffff"
471 "ffffffffffffffffffffffffffffffff"
472 "ffffffffffffffffffffffffffffffff"
473 "ffffffffffffffffffffffffffffff33", 16},
474 {1024, "111111ffffffffffffffff"
475 "ffffffffffffffffffffffffffffffff"
476 "fffffffffffffffffffffffffffffccf"
477 "ffffffffffffffffffffffffffffff00", 16},
478 {1024, 7919});
479 }
480
TEST(APIntTest,divrem_big3)481 TEST(APIntTest, divrem_big3) {
482 // Tests KnuthDiv case without shift
483 testDiv({256, "80000001ffffffffffffffff", 16},
484 {256, "ffffffffffffff0000000", 16},
485 {256, 4219});
486 }
487
TEST(APIntTest,divrem_big4)488 TEST(APIntTest, divrem_big4) {
489 // Tests heap allocation in divide() enfoced by huge numbers
490 testDiv(APInt{4096, 5}.shl(2001),
491 APInt{4096, 1}.shl(2000),
492 APInt{4096, 4219*13});
493 }
494
TEST(APIntTest,divrem_big5)495 TEST(APIntTest, divrem_big5) {
496 // Tests one word divisor case of divide()
497 testDiv(APInt{1024, 19}.shl(811),
498 APInt{1024, 4356013}, // one word
499 APInt{1024, 1});
500 }
501
TEST(APIntTest,divrem_big6)502 TEST(APIntTest, divrem_big6) {
503 // Tests some rare "borrow" cases in D4 step
504 testDiv(APInt{512, "ffffffffffffffff00000000000000000000000001", 16},
505 APInt{512, "10000000000000001000000000000001", 16},
506 APInt{512, "10000000000000000000000000000000", 16});
507 }
508
TEST(APIntTest,divrem_big7)509 TEST(APIntTest, divrem_big7) {
510 // Yet another test for KnuthDiv rare step D6.
511 testDiv({224, "800000008000000200000005", 16},
512 {224, "fffffffd", 16},
513 {224, "80000000800000010000000f", 16});
514 }
515
TEST(APIntTest,fromString)516 TEST(APIntTest, fromString) {
517 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
518 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
519 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
520 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
521 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
522
523 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
524 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
525 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
526 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
527 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
528
529 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
530 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
531 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
532 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
533 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
534
535
536 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
537 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
538 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
539 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
540 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
541 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
542
543 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
544 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
545 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
546 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
547 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
548 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
549
550 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
551 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
552 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
553 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
554 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
555 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
556
557
558 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
559 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
560 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
561 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
562 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
563 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
564
565 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
566 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
567 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
568 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
569 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
570 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
571
572
573 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
574 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
575 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
576 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
577 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
578 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
579
580 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
581 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
582 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
583 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
584 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
585 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
586
587 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
588 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
589 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
590 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
591 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
592 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
593
594 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
595 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
596 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
597 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
598 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
599 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
600 }
601
TEST(APIntTest,FromArray)602 TEST(APIntTest, FromArray) {
603 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
604 }
605
TEST(APIntTest,StringBitsNeeded2)606 TEST(APIntTest, StringBitsNeeded2) {
607 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
608 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
609 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
610 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
611 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
612
613 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
614 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
615 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
616 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
617 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
618
619 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
620 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
621 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
622 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
623 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
624 }
625
TEST(APIntTest,StringBitsNeeded8)626 TEST(APIntTest, StringBitsNeeded8) {
627 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
628 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
629 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
630 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
631 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
632
633 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
634 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
635 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
636 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
637 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
638
639 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
640 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
641 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
642 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
643 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
644 }
645
TEST(APIntTest,StringBitsNeeded10)646 TEST(APIntTest, StringBitsNeeded10) {
647 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
648 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
649 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
650 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
651 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
652 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
653
654 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
655 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
656 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
657 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
658 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
659
660 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
661 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
662 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
663 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
664 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
665 }
666
TEST(APIntTest,StringBitsNeeded16)667 TEST(APIntTest, StringBitsNeeded16) {
668 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
669 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
670 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
671 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
672 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
673
674 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
675 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
676 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
677 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
678 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
679
680 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
681 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
682 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
683 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
684 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
685 }
686
TEST(APIntTest,toString)687 TEST(APIntTest, toString) {
688 SmallString<16> S;
689 bool isSigned;
690
691 APInt(8, 0).toString(S, 2, true, true);
692 EXPECT_EQ(S.str().str(), "0b0");
693 S.clear();
694 APInt(8, 0).toString(S, 8, true, true);
695 EXPECT_EQ(S.str().str(), "00");
696 S.clear();
697 APInt(8, 0).toString(S, 10, true, true);
698 EXPECT_EQ(S.str().str(), "0");
699 S.clear();
700 APInt(8, 0).toString(S, 16, true, true);
701 EXPECT_EQ(S.str().str(), "0x0");
702 S.clear();
703 APInt(8, 0).toString(S, 36, true, false);
704 EXPECT_EQ(S.str().str(), "0");
705 S.clear();
706
707 isSigned = false;
708 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
709 EXPECT_EQ(S.str().str(), "0b11111111");
710 S.clear();
711 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
712 EXPECT_EQ(S.str().str(), "0377");
713 S.clear();
714 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
715 EXPECT_EQ(S.str().str(), "255");
716 S.clear();
717 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
718 EXPECT_EQ(S.str().str(), "0xFF");
719 S.clear();
720 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
721 EXPECT_EQ(S.str().str(), "73");
722 S.clear();
723
724 isSigned = true;
725 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
726 EXPECT_EQ(S.str().str(), "-0b1");
727 S.clear();
728 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
729 EXPECT_EQ(S.str().str(), "-01");
730 S.clear();
731 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
732 EXPECT_EQ(S.str().str(), "-1");
733 S.clear();
734 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
735 EXPECT_EQ(S.str().str(), "-0x1");
736 S.clear();
737 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
738 EXPECT_EQ(S.str().str(), "-1");
739 S.clear();
740 }
741
TEST(APIntTest,Log2)742 TEST(APIntTest, Log2) {
743 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
744 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
745 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
746 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
747 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
748 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
749 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
750 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
751 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
752 }
753
TEST(APIntTest,magic)754 TEST(APIntTest, magic) {
755 EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
756 EXPECT_EQ(APInt(32, 3).magic().s, 0U);
757 EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
758 EXPECT_EQ(APInt(32, 5).magic().s, 1U);
759 EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
760 EXPECT_EQ(APInt(32, 7).magic().s, 2U);
761 }
762
TEST(APIntTest,magicu)763 TEST(APIntTest, magicu) {
764 EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
765 EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
766 EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
767 EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
768 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
769 EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
770 EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
771 EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
772 }
773
774 #ifdef GTEST_HAS_DEATH_TEST
775 #ifndef NDEBUG
TEST(APIntTest,StringDeath)776 TEST(APIntTest, StringDeath) {
777 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
778 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
779 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
780 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
781 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
782 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
783 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
784 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
785 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
786 }
787 #endif
788 #endif
789
TEST(APIntTest,mul_clear)790 TEST(APIntTest, mul_clear) {
791 APInt ValA(65, -1ULL);
792 APInt ValB(65, 4);
793 APInt ValC(65, 0);
794 ValC = ValA * ValB;
795 ValA *= ValB;
796 EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
797 }
798
TEST(APIntTest,Rotate)799 TEST(APIntTest, Rotate) {
800 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
801 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
802 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
803 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
804 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
805
806 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
807 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
808 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
809 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
810 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
811
812 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
813 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
814 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
815 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
816 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
817
818 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
819 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
820 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
821 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
822 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
823
824 APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
825 APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
826 EXPECT_EQ(Rot, Big.rotr(144));
827 }
828
TEST(APIntTest,Splat)829 TEST(APIntTest, Splat) {
830 APInt ValA(8, 0x01);
831 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
832 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
833
834 APInt ValB(3, 5);
835 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
836 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
837 }
838
TEST(APIntTest,tcDecrement)839 TEST(APIntTest, tcDecrement) {
840 // Test single word decrement.
841
842 // No out borrow.
843 {
844 integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1);
845 integerPart carry = APInt::tcDecrement(&singleWord, 1);
846 EXPECT_EQ(carry, integerPart(0));
847 EXPECT_EQ(singleWord, ~integerPart(0) >> 1);
848 }
849
850 // With out borrow.
851 {
852 integerPart singleWord = 0;
853 integerPart carry = APInt::tcDecrement(&singleWord, 1);
854 EXPECT_EQ(carry, integerPart(1));
855 EXPECT_EQ(singleWord, ~integerPart(0));
856 }
857
858 // Test multiword decrement.
859
860 // No across word borrow, no out borrow.
861 {
862 integerPart test[4] = {0x1, 0x1, 0x1, 0x1};
863 integerPart expected[4] = {0x0, 0x1, 0x1, 0x1};
864 APInt::tcDecrement(test, 4);
865 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
866 }
867
868 // 1 across word borrow, no out borrow.
869 {
870 integerPart test[4] = {0x0, 0xF, 0x1, 0x1};
871 integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1};
872 integerPart carry = APInt::tcDecrement(test, 4);
873 EXPECT_EQ(carry, integerPart(0));
874 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
875 }
876
877 // 2 across word borrow, no out borrow.
878 {
879 integerPart test[4] = {0x0, 0x0, 0xC, 0x1};
880 integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1};
881 integerPart carry = APInt::tcDecrement(test, 4);
882 EXPECT_EQ(carry, integerPart(0));
883 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
884 }
885
886 // 3 across word borrow, no out borrow.
887 {
888 integerPart test[4] = {0x0, 0x0, 0x0, 0x1};
889 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0};
890 integerPart carry = APInt::tcDecrement(test, 4);
891 EXPECT_EQ(carry, integerPart(0));
892 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
893 }
894
895 // 3 across word borrow, with out borrow.
896 {
897 integerPart test[4] = {0x0, 0x0, 0x0, 0x0};
898 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)};
899 integerPart carry = APInt::tcDecrement(test, 4);
900 EXPECT_EQ(carry, integerPart(1));
901 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
902 }
903 }
904
TEST(APIntTest,arrayAccess)905 TEST(APIntTest, arrayAccess) {
906 // Single word check.
907 uint64_t E1 = 0x2CA7F46BF6569915ULL;
908 APInt A1(64, E1);
909 for (unsigned i = 0, e = 64; i < e; ++i) {
910 EXPECT_EQ(bool(E1 & (1ULL << i)),
911 A1[i]);
912 }
913
914 // Multiword check.
915 integerPart E2[4] = {
916 0xEB6EB136591CBA21ULL,
917 0x7B9358BD6A33F10AULL,
918 0x7E7FFA5EADD8846ULL,
919 0x305F341CA00B613DULL
920 };
921 APInt A2(integerPartWidth*4, E2);
922 for (unsigned i = 0; i < 4; ++i) {
923 for (unsigned j = 0; j < integerPartWidth; ++j) {
924 EXPECT_EQ(bool(E2[i] & (1ULL << j)),
925 A2[i*integerPartWidth + j]);
926 }
927 }
928 }
929
TEST(APIntTest,LargeAPIntConstruction)930 TEST(APIntTest, LargeAPIntConstruction) {
931 // Check that we can properly construct very large APInt. It is very
932 // unlikely that people will ever do this, but it is a legal input,
933 // so we should not crash on it.
934 APInt A9(UINT32_MAX, 0);
935 EXPECT_FALSE(A9.getBoolValue());
936 }
937
TEST(APIntTest,nearestLogBase2)938 TEST(APIntTest, nearestLogBase2) {
939 // Single word check.
940
941 // Test round up.
942 uint64_t I1 = 0x1800001;
943 APInt A1(64, I1);
944 EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
945
946 // Test round down.
947 uint64_t I2 = 0x1000011;
948 APInt A2(64, I2);
949 EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
950
951 // Test ties round up.
952 uint64_t I3 = 0x1800000;
953 APInt A3(64, I3);
954 EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
955
956 // Multiple word check.
957
958 // Test round up.
959 integerPart I4[4] = {0x0, 0xF, 0x18, 0x0};
960 APInt A4(integerPartWidth*4, I4);
961 EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
962
963 // Test round down.
964 integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
965 APInt A5(integerPartWidth*4, I5);
966 EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
967
968 // Test ties round up.
969 uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
970 APInt A6(integerPartWidth*4, I6);
971 EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
972
973 // Test BitWidth == 1 special cases.
974 APInt A7(1, 1);
975 EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
976 APInt A8(1, 0);
977 EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
978
979 // Test the zero case when we have a bit width large enough such
980 // that the bit width is larger than UINT32_MAX-1.
981 APInt A9(UINT32_MAX, 0);
982 EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
983 }
984
TEST(APIntTest,IsSplat)985 TEST(APIntTest, IsSplat) {
986 APInt A(32, 0x01010101);
987 EXPECT_FALSE(A.isSplat(1));
988 EXPECT_FALSE(A.isSplat(2));
989 EXPECT_FALSE(A.isSplat(4));
990 EXPECT_TRUE(A.isSplat(8));
991 EXPECT_TRUE(A.isSplat(16));
992 EXPECT_TRUE(A.isSplat(32));
993
994 APInt B(24, 0xAAAAAA);
995 EXPECT_FALSE(B.isSplat(1));
996 EXPECT_TRUE(B.isSplat(2));
997 EXPECT_TRUE(B.isSplat(4));
998 EXPECT_TRUE(B.isSplat(8));
999 EXPECT_TRUE(B.isSplat(24));
1000
1001 APInt C(24, 0xABAAAB);
1002 EXPECT_FALSE(C.isSplat(1));
1003 EXPECT_FALSE(C.isSplat(2));
1004 EXPECT_FALSE(C.isSplat(4));
1005 EXPECT_FALSE(C.isSplat(8));
1006 EXPECT_TRUE(C.isSplat(24));
1007
1008 APInt D(32, 0xABBAABBA);
1009 EXPECT_FALSE(D.isSplat(1));
1010 EXPECT_FALSE(D.isSplat(2));
1011 EXPECT_FALSE(D.isSplat(4));
1012 EXPECT_FALSE(D.isSplat(8));
1013 EXPECT_TRUE(D.isSplat(16));
1014 EXPECT_TRUE(D.isSplat(32));
1015
1016 APInt E(32, 0);
1017 EXPECT_TRUE(E.isSplat(1));
1018 EXPECT_TRUE(E.isSplat(2));
1019 EXPECT_TRUE(E.isSplat(4));
1020 EXPECT_TRUE(E.isSplat(8));
1021 EXPECT_TRUE(E.isSplat(16));
1022 EXPECT_TRUE(E.isSplat(32));
1023 }
1024
TEST(APIntTest,isMask)1025 TEST(APIntTest, isMask) {
1026 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0x01010101)));
1027 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xf0000000)));
1028 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xffff0000)));
1029 EXPECT_FALSE(APIntOps::isMask(APInt(32, 0xff << 1)));
1030
1031 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1032 EXPECT_FALSE(APIntOps::isMask(APInt(N, 0)));
1033
1034 APInt One(N, 1);
1035 for (int I = 1; I <= N; ++I) {
1036 APInt MaskVal = One.shl(I) - 1;
1037 EXPECT_TRUE(APIntOps::isMask(MaskVal));
1038 }
1039 }
1040 }
1041
1042 #if defined(__clang__)
1043 // Disable the pragma warning from versions of Clang without -Wself-move
1044 #pragma clang diagnostic push
1045 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1046 // Disable the warning that triggers on exactly what is being tested.
1047 #pragma clang diagnostic push
1048 #pragma clang diagnostic ignored "-Wself-move"
1049 #endif
TEST(APIntTest,SelfMoveAssignment)1050 TEST(APIntTest, SelfMoveAssignment) {
1051 APInt X(32, 0xdeadbeef);
1052 X = std::move(X);
1053 EXPECT_EQ(32u, X.getBitWidth());
1054 EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
1055
1056 uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
1057 APInt Y(128, Bits);
1058 Y = std::move(Y);
1059 EXPECT_EQ(128u, Y.getBitWidth());
1060 EXPECT_EQ(~0ULL, Y.getLimitedValue());
1061 const uint64_t *Raw = Y.getRawData();
1062 EXPECT_EQ(2u, Y.getNumWords());
1063 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
1064 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
1065 }
1066 #if defined(__clang__)
1067 #pragma clang diagnostic pop
1068 #pragma clang diagnostic pop
1069 #endif
1070 }
1071
TEST(APIntTest,reverseBits)1072 TEST(APIntTest, reverseBits) {
1073 EXPECT_EQ(1, APInt(1, 1).reverseBits());
1074 EXPECT_EQ(0, APInt(1, 0).reverseBits());
1075
1076 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1077 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1078
1079 EXPECT_EQ(0xb, APInt(4, 0xd).reverseBits());
1080 EXPECT_EQ(0xd, APInt(4, 0xb).reverseBits());
1081 EXPECT_EQ(0xf, APInt(4, 0xf).reverseBits());
1082
1083 EXPECT_EQ(0x30, APInt(7, 0x6).reverseBits());
1084 EXPECT_EQ(0x5a, APInt(7, 0x2d).reverseBits());
1085
1086 EXPECT_EQ(0x0f, APInt(8, 0xf0).reverseBits());
1087 EXPECT_EQ(0xf0, APInt(8, 0x0f).reverseBits());
1088
1089 EXPECT_EQ(0x0f0f, APInt(16, 0xf0f0).reverseBits());
1090 EXPECT_EQ(0xf0f0, APInt(16, 0x0f0f).reverseBits());
1091
1092 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1093 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1094
1095 EXPECT_EQ(0x402880a0 >> 1, APInt(31, 0x05011402).reverseBits());
1096
1097 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1098 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1099
1100 EXPECT_EQ(0x0f0f0f0f0f0f0f0f, APInt(64, 0xf0f0f0f0f0f0f0f0).reverseBits());
1101 EXPECT_EQ(0xf0f0f0f0f0f0f0f0, APInt(64, 0x0f0f0f0f0f0f0f0f).reverseBits());
1102
1103 for (unsigned N : { 1, 8, 16, 24, 31, 32, 33,
1104 63, 64, 65, 127, 128, 257, 1024 }) {
1105 for (unsigned I = 0; I < N; ++I) {
1106 APInt X = APInt::getOneBitSet(N, I);
1107 APInt Y = APInt::getOneBitSet(N, N - (I + 1));
1108 EXPECT_EQ(Y, X.reverseBits());
1109 EXPECT_EQ(X, Y.reverseBits());
1110 }
1111 }
1112 }
1113