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/SmallString.h"
12 #include "gtest/gtest.h"
13 #include <ostream>
14
15 using namespace llvm;
16
17 namespace {
18
19 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
TEST(APIntTest,ShiftLeftByZero)20 TEST(APIntTest, ShiftLeftByZero) {
21 APInt One = APInt::getNullValue(65) + 1;
22 APInt Shl = One.shl(0);
23 EXPECT_TRUE(Shl[0]);
24 EXPECT_FALSE(Shl[1]);
25 }
26
TEST(APIntTest,i128_NegativeCount)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());
31
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());
40 }
41
42 // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it.
43 #if defined(__llvm__) || !defined(__FreeBSD__)
44
TEST(APIntTest,i33_Count)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());
54 }
55
56 #endif
57
TEST(APIntTest,i65_Count)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());
66
67 APInt i65minus(65, 0, true);
68 i65minus.setBit(64);
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());
74 }
75
TEST(APIntTest,i128_PositiveCount)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());
84
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());
93
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());
103
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());
113 }
114
TEST(APIntTest,i1)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);
121
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());
130
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);
136
137 // Additions.
138 EXPECT_EQ(two, one + one);
139 EXPECT_EQ(zero, neg_one + one);
140 EXPECT_EQ(neg_two, neg_one + neg_one);
141
142 // Subtractions.
143 EXPECT_EQ(neg_two, neg_one - one);
144 EXPECT_EQ(two, one - neg_one);
145 EXPECT_EQ(zero, one - one);
146
147 // Shifts.
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));
154
155 // Rotates.
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));
160
161 // Multiplies.
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);
166
167 // Divides.
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));
172
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));
177
178 // Remainders.
179 EXPECT_EQ(zero, neg_one.srem(one));
180 EXPECT_EQ(zero, neg_one.urem(one));
181 EXPECT_EQ(zero, one.srem(neg_one));
182
183 // sdivrem
184 {
185 APInt q(8, 0);
186 APInt r(8, 0);
187 APInt one(8, 1);
188 APInt two(8, 2);
189 APInt nine(8, 9);
190 APInt four(8, 4);
191
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);
196
197 APInt::sdivrem(nine, two, q, r);
198 EXPECT_EQ(four, q);
199 EXPECT_EQ(one, r);
200 APInt::sdivrem(-nine, two, q, r);
201 EXPECT_EQ(-four, q);
202 EXPECT_EQ(-one, r);
203 APInt::sdivrem(nine, -two, q, r);
204 EXPECT_EQ(-four, q);
205 EXPECT_EQ(one, r);
206 APInt::sdivrem(-nine, -two, q, r);
207 EXPECT_EQ(four, q);
208 EXPECT_EQ(-one, r);
209 }
210 }
211
TEST(APIntTest,divrem_big1)212 TEST(APIntTest, divrem_big1) {
213 // Tests KnuthDiv rare step D6
214 APInt a{256, "1ffffffffffffffff", 16};
215 APInt b{256, "1ffffffffffffffff", 16};
216 APInt c{256, 0};
217
218 auto p = a * b + c;
219 auto q = p.udiv(a);
220 auto r = p.urem(a);
221 EXPECT_EQ(q, b);
222 EXPECT_EQ(r, c);
223 APInt::udivrem(p, a, q, r);
224 EXPECT_EQ(q, b);
225 EXPECT_EQ(r, c);
226 q = p.udiv(b);
227 r = p.urem(b);
228 EXPECT_EQ(q, a);
229 EXPECT_EQ(r, c);
230 APInt::udivrem(p, b, q, r);
231 EXPECT_EQ(q, a);
232 EXPECT_EQ(r, c);
233 q = p.sdiv(a);
234 r = p.srem(a);
235 EXPECT_EQ(q, b);
236 EXPECT_EQ(r, c);
237 APInt::sdivrem(p, a, q, r);
238 EXPECT_EQ(q, b);
239 EXPECT_EQ(r, c);
240 q = p.sdiv(b);
241 r = p.srem(b);
242 EXPECT_EQ(q, a);
243 EXPECT_EQ(r, c);
244 APInt::sdivrem(p, b, q, r);
245 EXPECT_EQ(q, a);
246 EXPECT_EQ(r, c);
247 }
248
TEST(APIntTest,divrem_big2)249 TEST(APIntTest, divrem_big2) {
250 // Tests KnuthDiv rare step D6
251 APInt a{1024, "111111ffffffffffffffff"
252 "ffffffffffffffffffffffffffffffff"
253 "fffffffffffffffffffffffffffffccf"
254 "ffffffffffffffffffffffffffffff00", 16};
255 APInt b{1024, "112233ceff"
256 "cecece000000ffffffffffffffffffff"
257 "ffffffffffffffffffffffffffffffff"
258 "ffffffffffffffffffffffffffffffff"
259 "ffffffffffffffffffffffffffffff33", 16};
260 APInt c{1024, 7919};
261
262 auto p = a * b + c;
263 auto q = p.udiv(a);
264 auto r = p.urem(a);
265 EXPECT_EQ(q, b);
266 EXPECT_EQ(r, c);
267 APInt::udivrem(p, a, q, r);
268 EXPECT_EQ(q, b);
269 EXPECT_EQ(r, c);
270 q = p.udiv(b);
271 r = p.urem(b);
272 EXPECT_EQ(q, a);
273 EXPECT_EQ(r, c);
274 APInt::udivrem(p, b, q, r);
275 EXPECT_EQ(q, a);
276 EXPECT_EQ(r, c);
277 q = p.sdiv(a);
278 r = p.srem(a);
279 EXPECT_EQ(q, b);
280 EXPECT_EQ(r, c);
281 APInt::sdivrem(p, a, q, r);
282 EXPECT_EQ(q, b);
283 EXPECT_EQ(r, c);
284 q = p.sdiv(b);
285 r = p.srem(b);
286 EXPECT_EQ(q, a);
287 EXPECT_EQ(r, c);
288 APInt::sdivrem(p, b, q, r);
289 EXPECT_EQ(q, a);
290 EXPECT_EQ(r, c);
291 }
292
TEST(APIntTest,divrem_big3)293 TEST(APIntTest, divrem_big3) {
294 // Tests KnuthDiv case without shift
295 APInt a{256, "ffffffffffffff0000000", 16};
296 APInt b{256, "80000001ffffffffffffffff", 16};
297 APInt c{256, 4219};
298
299 auto p = a * b + c;
300 auto q = p.udiv(a);
301 auto r = p.urem(a);
302 EXPECT_EQ(q, b);
303 EXPECT_EQ(r, c);
304 APInt::udivrem(p, a, q, r);
305 EXPECT_EQ(q, b);
306 EXPECT_EQ(r, c);
307 q = p.udiv(b);
308 r = p.urem(b);
309 EXPECT_EQ(q, a);
310 EXPECT_EQ(r, c);
311 APInt::udivrem(p, b, q, r);
312 EXPECT_EQ(q, a);
313 EXPECT_EQ(r, c);
314 q = p.sdiv(a);
315 r = p.srem(a);
316 EXPECT_EQ(q, b);
317 EXPECT_EQ(r, c);
318 APInt::sdivrem(p, a, q, r);
319 EXPECT_EQ(q, b);
320 EXPECT_EQ(r, c);
321 q = p.sdiv(b);
322 r = p.srem(b);
323 EXPECT_EQ(q, a);
324 EXPECT_EQ(r, c);
325 APInt::sdivrem(p, b, q, r);
326 EXPECT_EQ(q, a);
327 EXPECT_EQ(r, c);
328 }
329
TEST(APIntTest,divrem_big4)330 TEST(APIntTest, divrem_big4) {
331 // Tests heap allocation in divide() enfoced by huge numbers
332 auto a = APInt{4096, 1}.shl(2000);
333 auto b = APInt{4096, 5}.shl(2001);
334 auto c = APInt{4096, 4219*13};
335
336 auto p = a * b + c;
337 auto q = p.udiv(a);
338 auto r = p.urem(a);
339 EXPECT_EQ(q, b);
340 EXPECT_EQ(r, c);
341 q = APInt{1024, 0}; // test non-single word APInt conversion in divide()
342 r = APInt{1024, 0};
343 APInt::udivrem(p, a, q, r);
344 EXPECT_EQ(q, b);
345 EXPECT_EQ(r, c);
346 q = p.udiv(b);
347 r = p.urem(b);
348 EXPECT_EQ(q, a);
349 EXPECT_EQ(r, c);
350 q = APInt{1024, 0};
351 r = APInt{1024, 0};
352 APInt::udivrem(p, b, q, r);
353 EXPECT_EQ(q, a);
354 EXPECT_EQ(r, c);
355 q = p.sdiv(a);
356 r = p.srem(a);
357 EXPECT_EQ(q, b);
358 EXPECT_EQ(r, c);
359 q = APInt{1024, 0};
360 r = APInt{1024, 0};
361 APInt::sdivrem(p, a, q, r);
362 EXPECT_EQ(q, b);
363 EXPECT_EQ(r, c);
364 q = p.sdiv(b);
365 r = p.srem(b);
366 EXPECT_EQ(q, a);
367 EXPECT_EQ(r, c);
368 q = APInt{1024, 0};
369 r = APInt{1024, 0};
370 APInt::sdivrem(p, b, q, r);
371 EXPECT_EQ(q, a);
372 EXPECT_EQ(r, c);
373 }
374
TEST(APIntTest,divrem_big5)375 TEST(APIntTest, divrem_big5) {
376 // Tests one word divisor case of divide()
377 auto a = APInt{1024, 19}.shl(811);
378 auto b = APInt{1024, 4356013}; // one word
379 auto c = APInt{1024, 1};
380
381 auto p = a * b + c;
382 auto q = p.udiv(a);
383 auto r = p.urem(a);
384 EXPECT_EQ(q, b);
385 EXPECT_EQ(r, c);
386 APInt::udivrem(p, a, q, r);
387 EXPECT_EQ(q, b);
388 EXPECT_EQ(r, c);
389 q = p.udiv(b);
390 r = p.urem(b);
391 EXPECT_EQ(q, a);
392 EXPECT_EQ(r, c);
393 APInt::udivrem(p, b, q, r);
394 EXPECT_EQ(q, a);
395 EXPECT_EQ(r, c);
396 q = p.sdiv(a);
397 r = p.srem(a);
398 EXPECT_EQ(q, b);
399 EXPECT_EQ(r, c);
400 APInt::sdivrem(p, a, q, r);
401 EXPECT_EQ(q, b);
402 EXPECT_EQ(r, c);
403 q = p.sdiv(b);
404 r = p.srem(b);
405 EXPECT_EQ(q, a);
406 EXPECT_EQ(r, c);
407 APInt::sdivrem(p, b, q, r);
408 EXPECT_EQ(q, a);
409 EXPECT_EQ(r, c);
410 }
411
TEST(APIntTest,fromString)412 TEST(APIntTest, fromString) {
413 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
414 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
415 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
416 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
417 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
418
419 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
420 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
421 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
422 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
423 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
424
425 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
426 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
427 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
428 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
429 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
430
431
432 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
433 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
434 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
435 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
436 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
437 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
438
439 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
440 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
441 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
442 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
443 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
444 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
445
446 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
447 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
448 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
449 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
450 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
451 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
452
453
454 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
455 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
456 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
457 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
458 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
459 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
460
461 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
462 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
463 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
464 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
465 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
466 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
467
468
469 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
470 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
471 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
472 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
473 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
474 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
475
476 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
477 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
478 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
479 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
480 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
481 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
482
483 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
484 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
485 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
486 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
487 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
488 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
489
490 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
491 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
492 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
493 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
494 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
495 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
496 }
497
TEST(APIntTest,FromArray)498 TEST(APIntTest, FromArray) {
499 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
500 }
501
TEST(APIntTest,StringBitsNeeded2)502 TEST(APIntTest, StringBitsNeeded2) {
503 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
504 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
505 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
506 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
507 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
508
509 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
510 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
511 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
512 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
513 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
514
515 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
516 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
517 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
518 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
519 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
520 }
521
TEST(APIntTest,StringBitsNeeded8)522 TEST(APIntTest, StringBitsNeeded8) {
523 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
524 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
525 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
526 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
527 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
528
529 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
530 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
531 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
532 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
533 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
534
535 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
536 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
537 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
538 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
539 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
540 }
541
TEST(APIntTest,StringBitsNeeded10)542 TEST(APIntTest, StringBitsNeeded10) {
543 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
544 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
545 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
546 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
547 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
548 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
549
550 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
551 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
552 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
553 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
554 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
555
556 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
557 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
558 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
559 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
560 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
561 }
562
TEST(APIntTest,StringBitsNeeded16)563 TEST(APIntTest, StringBitsNeeded16) {
564 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
565 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
566 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
567 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
568 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
569
570 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
571 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
572 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
573 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
574 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
575
576 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
577 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
578 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
579 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
580 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
581 }
582
TEST(APIntTest,toString)583 TEST(APIntTest, toString) {
584 SmallString<16> S;
585 bool isSigned;
586
587 APInt(8, 0).toString(S, 2, true, true);
588 EXPECT_EQ(S.str().str(), "0b0");
589 S.clear();
590 APInt(8, 0).toString(S, 8, true, true);
591 EXPECT_EQ(S.str().str(), "00");
592 S.clear();
593 APInt(8, 0).toString(S, 10, true, true);
594 EXPECT_EQ(S.str().str(), "0");
595 S.clear();
596 APInt(8, 0).toString(S, 16, true, true);
597 EXPECT_EQ(S.str().str(), "0x0");
598 S.clear();
599 APInt(8, 0).toString(S, 36, true, false);
600 EXPECT_EQ(S.str().str(), "0");
601 S.clear();
602
603 isSigned = false;
604 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
605 EXPECT_EQ(S.str().str(), "0b11111111");
606 S.clear();
607 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
608 EXPECT_EQ(S.str().str(), "0377");
609 S.clear();
610 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
611 EXPECT_EQ(S.str().str(), "255");
612 S.clear();
613 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
614 EXPECT_EQ(S.str().str(), "0xFF");
615 S.clear();
616 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
617 EXPECT_EQ(S.str().str(), "73");
618 S.clear();
619
620 isSigned = true;
621 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
622 EXPECT_EQ(S.str().str(), "-0b1");
623 S.clear();
624 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
625 EXPECT_EQ(S.str().str(), "-01");
626 S.clear();
627 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
628 EXPECT_EQ(S.str().str(), "-1");
629 S.clear();
630 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
631 EXPECT_EQ(S.str().str(), "-0x1");
632 S.clear();
633 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
634 EXPECT_EQ(S.str().str(), "-1");
635 S.clear();
636 }
637
TEST(APIntTest,Log2)638 TEST(APIntTest, Log2) {
639 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
640 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
641 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
642 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
643 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
644 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
645 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
646 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
647 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
648 }
649
TEST(APIntTest,magic)650 TEST(APIntTest, magic) {
651 EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
652 EXPECT_EQ(APInt(32, 3).magic().s, 0U);
653 EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
654 EXPECT_EQ(APInt(32, 5).magic().s, 1U);
655 EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
656 EXPECT_EQ(APInt(32, 7).magic().s, 2U);
657 }
658
TEST(APIntTest,magicu)659 TEST(APIntTest, magicu) {
660 EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
661 EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
662 EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
663 EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
664 EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
665 EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
666 EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
667 EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
668 }
669
670 #ifdef GTEST_HAS_DEATH_TEST
671 #ifndef NDEBUG
TEST(APIntTest,StringDeath)672 TEST(APIntTest, StringDeath) {
673 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
674 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
675 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
676 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
677 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
678 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
679 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
680 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
681 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
682 }
683 #endif
684 #endif
685
TEST(APIntTest,mul_clear)686 TEST(APIntTest, mul_clear) {
687 APInt ValA(65, -1ULL);
688 APInt ValB(65, 4);
689 APInt ValC(65, 0);
690 ValC = ValA * ValB;
691 ValA *= ValB;
692 EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
693 }
694
TEST(APIntTest,Rotate)695 TEST(APIntTest, Rotate) {
696 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
697 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
698 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
699 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
700 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
701
702 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
703 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
704 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
705 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
706 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
707
708 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
709 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
710 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
711 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
712 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
713
714 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
715 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
716 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
717 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
718 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
719
720 APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
721 APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
722 EXPECT_EQ(Rot, Big.rotr(144));
723 }
724
TEST(APIntTest,Splat)725 TEST(APIntTest, Splat) {
726 APInt ValA(8, 0x01);
727 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
728 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
729
730 APInt ValB(3, 5);
731 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
732 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
733 }
734
TEST(APIntTest,tcDecrement)735 TEST(APIntTest, tcDecrement) {
736 // Test single word decrement.
737
738 // No out borrow.
739 {
740 integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1);
741 integerPart carry = APInt::tcDecrement(&singleWord, 1);
742 EXPECT_EQ(carry, integerPart(0));
743 EXPECT_EQ(singleWord, ~integerPart(0) >> 1);
744 }
745
746 // With out borrow.
747 {
748 integerPart singleWord = 0;
749 integerPart carry = APInt::tcDecrement(&singleWord, 1);
750 EXPECT_EQ(carry, integerPart(1));
751 EXPECT_EQ(singleWord, ~integerPart(0));
752 }
753
754 // Test multiword decrement.
755
756 // No across word borrow, no out borrow.
757 {
758 integerPart test[4] = {0x1, 0x1, 0x1, 0x1};
759 integerPart expected[4] = {0x0, 0x1, 0x1, 0x1};
760 APInt::tcDecrement(test, 4);
761 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
762 }
763
764 // 1 across word borrow, no out borrow.
765 {
766 integerPart test[4] = {0x0, 0xF, 0x1, 0x1};
767 integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1};
768 integerPart carry = APInt::tcDecrement(test, 4);
769 EXPECT_EQ(carry, integerPart(0));
770 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
771 }
772
773 // 2 across word borrow, no out borrow.
774 {
775 integerPart test[4] = {0x0, 0x0, 0xC, 0x1};
776 integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1};
777 integerPart carry = APInt::tcDecrement(test, 4);
778 EXPECT_EQ(carry, integerPart(0));
779 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
780 }
781
782 // 3 across word borrow, no out borrow.
783 {
784 integerPart test[4] = {0x0, 0x0, 0x0, 0x1};
785 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0};
786 integerPart carry = APInt::tcDecrement(test, 4);
787 EXPECT_EQ(carry, integerPart(0));
788 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
789 }
790
791 // 3 across word borrow, with out borrow.
792 {
793 integerPart test[4] = {0x0, 0x0, 0x0, 0x0};
794 integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)};
795 integerPart carry = APInt::tcDecrement(test, 4);
796 EXPECT_EQ(carry, integerPart(1));
797 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
798 }
799 }
800
TEST(APIntTest,arrayAccess)801 TEST(APIntTest, arrayAccess) {
802 // Single word check.
803 uint64_t E1 = 0x2CA7F46BF6569915ULL;
804 APInt A1(64, E1);
805 for (unsigned i = 0, e = 64; i < e; ++i) {
806 EXPECT_EQ(bool(E1 & (1ULL << i)),
807 A1[i]);
808 }
809
810 // Multiword check.
811 integerPart E2[4] = {
812 0xEB6EB136591CBA21ULL,
813 0x7B9358BD6A33F10AULL,
814 0x7E7FFA5EADD8846ULL,
815 0x305F341CA00B613DULL
816 };
817 APInt A2(integerPartWidth*4, E2);
818 for (unsigned i = 0; i < 4; ++i) {
819 for (unsigned j = 0; j < integerPartWidth; ++j) {
820 EXPECT_EQ(bool(E2[i] & (1ULL << j)),
821 A2[i*integerPartWidth + j]);
822 }
823 }
824 }
825
TEST(APIntTest,LargeAPIntConstruction)826 TEST(APIntTest, LargeAPIntConstruction) {
827 // Check that we can properly construct very large APInt. It is very
828 // unlikely that people will ever do this, but it is a legal input,
829 // so we should not crash on it.
830 APInt A9(UINT32_MAX, 0);
831 EXPECT_FALSE(A9.getBoolValue());
832 }
833
TEST(APIntTest,nearestLogBase2)834 TEST(APIntTest, nearestLogBase2) {
835 // Single word check.
836
837 // Test round up.
838 uint64_t I1 = 0x1800001;
839 APInt A1(64, I1);
840 EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
841
842 // Test round down.
843 uint64_t I2 = 0x1000011;
844 APInt A2(64, I2);
845 EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
846
847 // Test ties round up.
848 uint64_t I3 = 0x1800000;
849 APInt A3(64, I3);
850 EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
851
852 // Multiple word check.
853
854 // Test round up.
855 integerPart I4[4] = {0x0, 0xF, 0x18, 0x0};
856 APInt A4(integerPartWidth*4, I4);
857 EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
858
859 // Test round down.
860 integerPart I5[4] = {0x0, 0xF, 0x10, 0x0};
861 APInt A5(integerPartWidth*4, I5);
862 EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
863
864 // Test ties round up.
865 uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
866 APInt A6(integerPartWidth*4, I6);
867 EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
868
869 // Test BitWidth == 1 special cases.
870 APInt A7(1, 1);
871 EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
872 APInt A8(1, 0);
873 EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
874
875 // Test the zero case when we have a bit width large enough such
876 // that the bit width is larger than UINT32_MAX-1.
877 APInt A9(UINT32_MAX, 0);
878 EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
879 }
880
TEST(APIntTest,IsSplat)881 TEST(APIntTest, IsSplat) {
882 APInt A(32, 0x01010101);
883 EXPECT_FALSE(A.isSplat(1));
884 EXPECT_FALSE(A.isSplat(2));
885 EXPECT_FALSE(A.isSplat(4));
886 EXPECT_TRUE(A.isSplat(8));
887 EXPECT_TRUE(A.isSplat(16));
888 EXPECT_TRUE(A.isSplat(32));
889
890 APInt B(24, 0xAAAAAA);
891 EXPECT_FALSE(B.isSplat(1));
892 EXPECT_TRUE(B.isSplat(2));
893 EXPECT_TRUE(B.isSplat(4));
894 EXPECT_TRUE(B.isSplat(8));
895 EXPECT_TRUE(B.isSplat(24));
896
897 APInt C(24, 0xABAAAB);
898 EXPECT_FALSE(C.isSplat(1));
899 EXPECT_FALSE(C.isSplat(2));
900 EXPECT_FALSE(C.isSplat(4));
901 EXPECT_FALSE(C.isSplat(8));
902 EXPECT_TRUE(C.isSplat(24));
903
904 APInt D(32, 0xABBAABBA);
905 EXPECT_FALSE(D.isSplat(1));
906 EXPECT_FALSE(D.isSplat(2));
907 EXPECT_FALSE(D.isSplat(4));
908 EXPECT_FALSE(D.isSplat(8));
909 EXPECT_TRUE(D.isSplat(16));
910 EXPECT_TRUE(D.isSplat(32));
911
912 APInt E(32, 0);
913 EXPECT_TRUE(E.isSplat(1));
914 EXPECT_TRUE(E.isSplat(2));
915 EXPECT_TRUE(E.isSplat(4));
916 EXPECT_TRUE(E.isSplat(8));
917 EXPECT_TRUE(E.isSplat(16));
918 EXPECT_TRUE(E.isSplat(32));
919 }
920
921 #if defined(__clang__)
922 // Disable the pragma warning from versions of Clang without -Wself-move
923 #pragma clang diagnostic push
924 #pragma clang diagnostic ignored "-Wunknown-pragmas"
925 // Disable the warning that triggers on exactly what is being tested.
926 #pragma clang diagnostic push
927 #pragma clang diagnostic ignored "-Wself-move"
928 #endif
TEST(APIntTest,SelfMoveAssignment)929 TEST(APIntTest, SelfMoveAssignment) {
930 APInt X(32, 0xdeadbeef);
931 X = std::move(X);
932 EXPECT_EQ(32u, X.getBitWidth());
933 EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
934
935 uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
936 APInt Y(128, Bits);
937 Y = std::move(Y);
938 EXPECT_EQ(128u, Y.getBitWidth());
939 EXPECT_EQ(~0ULL, Y.getLimitedValue());
940 const uint64_t *Raw = Y.getRawData();
941 EXPECT_EQ(2u, Y.getNumWords());
942 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
943 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
944 }
945 #if defined(__clang__)
946 #pragma clang diagnostic pop
947 #pragma clang diagnostic pop
948 #endif
949 }
950