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/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "gtest/gtest.h"
21 TEST(APIntTest
, ValueInit
) {
24 EXPECT_TRUE(!Zero
.zext(64));
25 EXPECT_TRUE(!Zero
.sext(64));
28 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
29 TEST(APIntTest
, ShiftLeftByZero
) {
30 APInt One
= APInt::getNullValue(65) + 1;
31 APInt Shl
= One
.shl(0);
36 TEST(APIntTest
, i64_ArithmeticRightShiftNegative
) {
37 const APInt
neg_one(64, static_cast<uint64_t>(-1), true);
38 EXPECT_EQ(neg_one
, neg_one
.ashr(7));
41 TEST(APIntTest
, i128_NegativeCount
) {
42 APInt
Minus3(128, static_cast<uint64_t>(-3), true);
43 EXPECT_EQ(126u, Minus3
.countLeadingOnes());
44 EXPECT_EQ(-3, Minus3
.getSExtValue());
46 APInt
Minus1(128, static_cast<uint64_t>(-1), true);
47 EXPECT_EQ(0u, Minus1
.countLeadingZeros());
48 EXPECT_EQ(128u, Minus1
.countLeadingOnes());
49 EXPECT_EQ(128u, Minus1
.getActiveBits());
50 EXPECT_EQ(0u, Minus1
.countTrailingZeros());
51 EXPECT_EQ(128u, Minus1
.countTrailingOnes());
52 EXPECT_EQ(128u, Minus1
.countPopulation());
53 EXPECT_EQ(-1, Minus1
.getSExtValue());
56 TEST(APIntTest
, i33_Count
) {
57 APInt
i33minus2(33, static_cast<uint64_t>(-2), true);
58 EXPECT_EQ(0u, i33minus2
.countLeadingZeros());
59 EXPECT_EQ(32u, i33minus2
.countLeadingOnes());
60 EXPECT_EQ(33u, i33minus2
.getActiveBits());
61 EXPECT_EQ(1u, i33minus2
.countTrailingZeros());
62 EXPECT_EQ(32u, i33minus2
.countPopulation());
63 EXPECT_EQ(-2, i33minus2
.getSExtValue());
64 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2
.getZExtValue());
67 TEST(APIntTest
, i61_Count
) {
68 APInt
i61(61, 1 << 15);
69 EXPECT_EQ(45u, i61
.countLeadingZeros());
70 EXPECT_EQ(0u, i61
.countLeadingOnes());
71 EXPECT_EQ(16u, i61
.getActiveBits());
72 EXPECT_EQ(15u, i61
.countTrailingZeros());
73 EXPECT_EQ(1u, i61
.countPopulation());
74 EXPECT_EQ(static_cast<int64_t>(1 << 15), i61
.getSExtValue());
75 EXPECT_EQ(static_cast<uint64_t>(1 << 15), i61
.getZExtValue());
78 EXPECT_EQ(42u, i61
.countLeadingZeros());
79 EXPECT_EQ(0u, i61
.countLeadingOnes());
80 EXPECT_EQ(19u, i61
.getActiveBits());
81 EXPECT_EQ(8u, i61
.countTrailingZeros());
82 EXPECT_EQ(11u, i61
.countPopulation());
83 EXPECT_EQ(static_cast<int64_t>((1 << 19) - (1 << 8)), i61
.getSExtValue());
84 EXPECT_EQ(static_cast<uint64_t>((1 << 19) - (1 << 8)), i61
.getZExtValue());
87 TEST(APIntTest
, i65_Count
) {
88 APInt
i65(65, 0, true);
89 EXPECT_EQ(65u, i65
.countLeadingZeros());
90 EXPECT_EQ(0u, i65
.countLeadingOnes());
91 EXPECT_EQ(0u, i65
.getActiveBits());
92 EXPECT_EQ(1u, i65
.getActiveWords());
93 EXPECT_EQ(65u, i65
.countTrailingZeros());
94 EXPECT_EQ(0u, i65
.countPopulation());
96 APInt
i65minus(65, 0, true);
98 EXPECT_EQ(0u, i65minus
.countLeadingZeros());
99 EXPECT_EQ(1u, i65minus
.countLeadingOnes());
100 EXPECT_EQ(65u, i65minus
.getActiveBits());
101 EXPECT_EQ(64u, i65minus
.countTrailingZeros());
102 EXPECT_EQ(1u, i65minus
.countPopulation());
105 TEST(APIntTest
, i128_PositiveCount
) {
106 APInt u128max
= APInt::getAllOnesValue(128);
107 EXPECT_EQ(128u, u128max
.countLeadingOnes());
108 EXPECT_EQ(0u, u128max
.countLeadingZeros());
109 EXPECT_EQ(128u, u128max
.getActiveBits());
110 EXPECT_EQ(0u, u128max
.countTrailingZeros());
111 EXPECT_EQ(128u, u128max
.countTrailingOnes());
112 EXPECT_EQ(128u, u128max
.countPopulation());
114 APInt
u64max(128, static_cast<uint64_t>(-1), false);
115 EXPECT_EQ(64u, u64max
.countLeadingZeros());
116 EXPECT_EQ(0u, u64max
.countLeadingOnes());
117 EXPECT_EQ(64u, u64max
.getActiveBits());
118 EXPECT_EQ(0u, u64max
.countTrailingZeros());
119 EXPECT_EQ(64u, u64max
.countTrailingOnes());
120 EXPECT_EQ(64u, u64max
.countPopulation());
121 EXPECT_EQ((uint64_t)~0ull, u64max
.getZExtValue());
123 APInt
zero(128, 0, true);
124 EXPECT_EQ(128u, zero
.countLeadingZeros());
125 EXPECT_EQ(0u, zero
.countLeadingOnes());
126 EXPECT_EQ(0u, zero
.getActiveBits());
127 EXPECT_EQ(128u, zero
.countTrailingZeros());
128 EXPECT_EQ(0u, zero
.countTrailingOnes());
129 EXPECT_EQ(0u, zero
.countPopulation());
130 EXPECT_EQ(0u, zero
.getSExtValue());
131 EXPECT_EQ(0u, zero
.getZExtValue());
133 APInt
one(128, 1, true);
134 EXPECT_EQ(127u, one
.countLeadingZeros());
135 EXPECT_EQ(0u, one
.countLeadingOnes());
136 EXPECT_EQ(1u, one
.getActiveBits());
137 EXPECT_EQ(0u, one
.countTrailingZeros());
138 EXPECT_EQ(1u, one
.countTrailingOnes());
139 EXPECT_EQ(1u, one
.countPopulation());
140 EXPECT_EQ(1, one
.getSExtValue());
141 EXPECT_EQ(1u, one
.getZExtValue());
143 APInt
s128(128, 2, true);
144 EXPECT_EQ(126u, s128
.countLeadingZeros());
145 EXPECT_EQ(0u, s128
.countLeadingOnes());
146 EXPECT_EQ(2u, s128
.getActiveBits());
147 EXPECT_EQ(1u, s128
.countTrailingZeros());
148 EXPECT_EQ(0u, s128
.countTrailingOnes());
149 EXPECT_EQ(1u, s128
.countPopulation());
150 EXPECT_EQ(2, s128
.getSExtValue());
151 EXPECT_EQ(2u, s128
.getZExtValue());
154 s128
.setBits(42, 42);
155 EXPECT_EQ(126u, s128
.countLeadingZeros());
156 EXPECT_EQ(0u, s128
.countLeadingOnes());
157 EXPECT_EQ(2u, s128
.getActiveBits());
158 EXPECT_EQ(1u, s128
.countTrailingZeros());
159 EXPECT_EQ(0u, s128
.countTrailingOnes());
160 EXPECT_EQ(1u, s128
.countPopulation());
161 EXPECT_EQ(2, s128
.getSExtValue());
162 EXPECT_EQ(2u, s128
.getZExtValue());
165 EXPECT_EQ(96u, s128
.countLeadingZeros());
166 EXPECT_EQ(0u, s128
.countLeadingOnes());
167 EXPECT_EQ(32u, s128
.getActiveBits());
168 EXPECT_EQ(33u, s128
.getMinSignedBits());
169 EXPECT_EQ(1u, s128
.countTrailingZeros());
170 EXPECT_EQ(0u, s128
.countTrailingOnes());
171 EXPECT_EQ(30u, s128
.countPopulation());
172 EXPECT_EQ(static_cast<uint32_t>((~0u << 3) | 2), s128
.getZExtValue());
174 s128
.setBits(62, 128);
175 EXPECT_EQ(0u, s128
.countLeadingZeros());
176 EXPECT_EQ(66u, s128
.countLeadingOnes());
177 EXPECT_EQ(128u, s128
.getActiveBits());
178 EXPECT_EQ(63u, s128
.getMinSignedBits());
179 EXPECT_EQ(1u, s128
.countTrailingZeros());
180 EXPECT_EQ(0u, s128
.countTrailingOnes());
181 EXPECT_EQ(96u, s128
.countPopulation());
182 EXPECT_EQ(static_cast<int64_t>((3ull << 62) |
183 static_cast<uint32_t>((~0u << 3) | 2)),
184 s128
.getSExtValue());
187 TEST(APIntTest
, i256
) {
188 APInt
s256(256, 15, true);
189 EXPECT_EQ(252u, s256
.countLeadingZeros());
190 EXPECT_EQ(0u, s256
.countLeadingOnes());
191 EXPECT_EQ(4u, s256
.getActiveBits());
192 EXPECT_EQ(0u, s256
.countTrailingZeros());
193 EXPECT_EQ(4u, s256
.countTrailingOnes());
194 EXPECT_EQ(4u, s256
.countPopulation());
195 EXPECT_EQ(15, s256
.getSExtValue());
196 EXPECT_EQ(15u, s256
.getZExtValue());
198 s256
.setBits(62, 66);
199 EXPECT_EQ(190u, s256
.countLeadingZeros());
200 EXPECT_EQ(0u, s256
.countLeadingOnes());
201 EXPECT_EQ(66u, s256
.getActiveBits());
202 EXPECT_EQ(67u, s256
.getMinSignedBits());
203 EXPECT_EQ(0u, s256
.countTrailingZeros());
204 EXPECT_EQ(4u, s256
.countTrailingOnes());
205 EXPECT_EQ(8u, s256
.countPopulation());
207 s256
.setBits(60, 256);
208 EXPECT_EQ(0u, s256
.countLeadingZeros());
209 EXPECT_EQ(196u, s256
.countLeadingOnes());
210 EXPECT_EQ(256u, s256
.getActiveBits());
211 EXPECT_EQ(61u, s256
.getMinSignedBits());
212 EXPECT_EQ(0u, s256
.countTrailingZeros());
213 EXPECT_EQ(4u, s256
.countTrailingOnes());
214 EXPECT_EQ(200u, s256
.countPopulation());
215 EXPECT_EQ(static_cast<int64_t>((~0ull << 60) | 15), s256
.getSExtValue());
218 TEST(APIntTest
, i1
) {
219 const APInt
neg_two(1, static_cast<uint64_t>(-2), true);
220 const APInt
neg_one(1, static_cast<uint64_t>(-1), true);
221 const APInt
zero(1, 0);
222 const APInt
one(1, 1);
223 const APInt
two(1, 2);
225 EXPECT_EQ(0, neg_two
.getSExtValue());
226 EXPECT_EQ(-1, neg_one
.getSExtValue());
227 EXPECT_EQ(1u, neg_one
.getZExtValue());
228 EXPECT_EQ(0u, zero
.getZExtValue());
229 EXPECT_EQ(-1, one
.getSExtValue());
230 EXPECT_EQ(1u, one
.getZExtValue());
231 EXPECT_EQ(0u, two
.getZExtValue());
232 EXPECT_EQ(0, two
.getSExtValue());
234 // Basic equalities for 1-bit values.
235 EXPECT_EQ(zero
, two
);
236 EXPECT_EQ(zero
, neg_two
);
237 EXPECT_EQ(one
, neg_one
);
238 EXPECT_EQ(two
, neg_two
);
240 // Min/max signed values.
241 EXPECT_TRUE(zero
.isMaxSignedValue());
242 EXPECT_FALSE(one
.isMaxSignedValue());
243 EXPECT_FALSE(zero
.isMinSignedValue());
244 EXPECT_TRUE(one
.isMinSignedValue());
247 EXPECT_EQ(two
, one
+ one
);
248 EXPECT_EQ(zero
, neg_one
+ one
);
249 EXPECT_EQ(neg_two
, neg_one
+ neg_one
);
252 EXPECT_EQ(neg_two
, neg_one
- one
);
253 EXPECT_EQ(two
, one
- neg_one
);
254 EXPECT_EQ(zero
, one
- one
);
257 EXPECT_EQ(zero
, zero
& zero
);
258 EXPECT_EQ(zero
, one
& zero
);
259 EXPECT_EQ(zero
, zero
& one
);
260 EXPECT_EQ(one
, one
& one
);
261 EXPECT_EQ(zero
, zero
& zero
);
262 EXPECT_EQ(zero
, neg_one
& zero
);
263 EXPECT_EQ(zero
, zero
& neg_one
);
264 EXPECT_EQ(neg_one
, neg_one
& neg_one
);
267 EXPECT_EQ(zero
, zero
| zero
);
268 EXPECT_EQ(one
, one
| zero
);
269 EXPECT_EQ(one
, zero
| one
);
270 EXPECT_EQ(one
, one
| one
);
271 EXPECT_EQ(zero
, zero
| zero
);
272 EXPECT_EQ(neg_one
, neg_one
| zero
);
273 EXPECT_EQ(neg_one
, zero
| neg_one
);
274 EXPECT_EQ(neg_one
, neg_one
| neg_one
);
277 EXPECT_EQ(zero
, zero
^ zero
);
278 EXPECT_EQ(one
, one
^ zero
);
279 EXPECT_EQ(one
, zero
^ one
);
280 EXPECT_EQ(zero
, one
^ one
);
281 EXPECT_EQ(zero
, zero
^ zero
);
282 EXPECT_EQ(neg_one
, neg_one
^ zero
);
283 EXPECT_EQ(neg_one
, zero
^ neg_one
);
284 EXPECT_EQ(zero
, neg_one
^ neg_one
);
287 EXPECT_EQ(zero
, one
<< one
);
288 EXPECT_EQ(one
, one
<< zero
);
289 EXPECT_EQ(zero
, one
.shl(1));
290 EXPECT_EQ(one
, one
.shl(0));
291 EXPECT_EQ(zero
, one
.lshr(1));
292 EXPECT_EQ(one
, one
.ashr(1));
295 EXPECT_EQ(one
, one
.rotl(0));
296 EXPECT_EQ(one
, one
.rotl(1));
297 EXPECT_EQ(one
, one
.rotr(0));
298 EXPECT_EQ(one
, one
.rotr(1));
301 EXPECT_EQ(neg_one
, neg_one
* one
);
302 EXPECT_EQ(neg_one
, one
* neg_one
);
303 EXPECT_EQ(one
, neg_one
* neg_one
);
304 EXPECT_EQ(one
, one
* one
);
307 EXPECT_EQ(neg_one
, one
.sdiv(neg_one
));
308 EXPECT_EQ(neg_one
, neg_one
.sdiv(one
));
309 EXPECT_EQ(one
, neg_one
.sdiv(neg_one
));
310 EXPECT_EQ(one
, one
.sdiv(one
));
312 EXPECT_EQ(neg_one
, one
.udiv(neg_one
));
313 EXPECT_EQ(neg_one
, neg_one
.udiv(one
));
314 EXPECT_EQ(one
, neg_one
.udiv(neg_one
));
315 EXPECT_EQ(one
, one
.udiv(one
));
318 EXPECT_EQ(zero
, neg_one
.srem(one
));
319 EXPECT_EQ(zero
, neg_one
.urem(one
));
320 EXPECT_EQ(zero
, one
.srem(neg_one
));
331 EXPECT_EQ(nine
.srem(two
), one
);
332 EXPECT_EQ(nine
.srem(-two
), one
);
333 EXPECT_EQ((-nine
).srem(two
), -one
);
334 EXPECT_EQ((-nine
).srem(-two
), -one
);
336 APInt::sdivrem(nine
, two
, q
, r
);
339 APInt::sdivrem(-nine
, two
, q
, r
);
342 APInt::sdivrem(nine
, -two
, q
, r
);
345 APInt::sdivrem(-nine
, -two
, q
, r
);
351 TEST(APIntTest
, compare
) {
352 std::array
<APInt
, 5> testVals
{{
356 APInt
{16, (uint64_t)-1, true},
357 APInt
{16, (uint64_t)-2, true},
360 for (auto &arg1
: testVals
)
361 for (auto &arg2
: testVals
) {
362 auto uv1
= arg1
.getZExtValue();
363 auto uv2
= arg2
.getZExtValue();
364 auto sv1
= arg1
.getSExtValue();
365 auto sv2
= arg2
.getSExtValue();
367 EXPECT_EQ(uv1
< uv2
, arg1
.ult(arg2
));
368 EXPECT_EQ(uv1
<= uv2
, arg1
.ule(arg2
));
369 EXPECT_EQ(uv1
> uv2
, arg1
.ugt(arg2
));
370 EXPECT_EQ(uv1
>= uv2
, arg1
.uge(arg2
));
372 EXPECT_EQ(sv1
< sv2
, arg1
.slt(arg2
));
373 EXPECT_EQ(sv1
<= sv2
, arg1
.sle(arg2
));
374 EXPECT_EQ(sv1
> sv2
, arg1
.sgt(arg2
));
375 EXPECT_EQ(sv1
>= sv2
, arg1
.sge(arg2
));
377 EXPECT_EQ(uv1
< uv2
, arg1
.ult(uv2
));
378 EXPECT_EQ(uv1
<= uv2
, arg1
.ule(uv2
));
379 EXPECT_EQ(uv1
> uv2
, arg1
.ugt(uv2
));
380 EXPECT_EQ(uv1
>= uv2
, arg1
.uge(uv2
));
382 EXPECT_EQ(sv1
< sv2
, arg1
.slt(sv2
));
383 EXPECT_EQ(sv1
<= sv2
, arg1
.sle(sv2
));
384 EXPECT_EQ(sv1
> sv2
, arg1
.sgt(sv2
));
385 EXPECT_EQ(sv1
>= sv2
, arg1
.sge(sv2
));
389 TEST(APIntTest
, compareWithRawIntegers
) {
390 EXPECT_TRUE(!APInt(8, 1).uge(256));
391 EXPECT_TRUE(!APInt(8, 1).ugt(256));
392 EXPECT_TRUE( APInt(8, 1).ule(256));
393 EXPECT_TRUE( APInt(8, 1).ult(256));
394 EXPECT_TRUE(!APInt(8, 1).sge(256));
395 EXPECT_TRUE(!APInt(8, 1).sgt(256));
396 EXPECT_TRUE( APInt(8, 1).sle(256));
397 EXPECT_TRUE( APInt(8, 1).slt(256));
398 EXPECT_TRUE(!(APInt(8, 0) == 256));
399 EXPECT_TRUE( APInt(8, 0) != 256);
400 EXPECT_TRUE(!(APInt(8, 1) == 256));
401 EXPECT_TRUE( APInt(8, 1) != 256);
403 auto uint64max
= UINT64_MAX
;
404 auto int64max
= INT64_MAX
;
405 auto int64min
= INT64_MIN
;
407 auto u64
= APInt
{128, uint64max
};
408 auto s64
= APInt
{128, static_cast<uint64_t>(int64max
), true};
411 EXPECT_TRUE( u64
.uge(uint64max
));
412 EXPECT_TRUE(!u64
.ugt(uint64max
));
413 EXPECT_TRUE( u64
.ule(uint64max
));
414 EXPECT_TRUE(!u64
.ult(uint64max
));
415 EXPECT_TRUE( u64
.sge(int64max
));
416 EXPECT_TRUE( u64
.sgt(int64max
));
417 EXPECT_TRUE(!u64
.sle(int64max
));
418 EXPECT_TRUE(!u64
.slt(int64max
));
419 EXPECT_TRUE( u64
.sge(int64min
));
420 EXPECT_TRUE( u64
.sgt(int64min
));
421 EXPECT_TRUE(!u64
.sle(int64min
));
422 EXPECT_TRUE(!u64
.slt(int64min
));
424 EXPECT_TRUE(u64
== uint64max
);
425 EXPECT_TRUE(u64
!= int64max
);
426 EXPECT_TRUE(u64
!= int64min
);
428 EXPECT_TRUE(!s64
.uge(uint64max
));
429 EXPECT_TRUE(!s64
.ugt(uint64max
));
430 EXPECT_TRUE( s64
.ule(uint64max
));
431 EXPECT_TRUE( s64
.ult(uint64max
));
432 EXPECT_TRUE( s64
.sge(int64max
));
433 EXPECT_TRUE(!s64
.sgt(int64max
));
434 EXPECT_TRUE( s64
.sle(int64max
));
435 EXPECT_TRUE(!s64
.slt(int64max
));
436 EXPECT_TRUE( s64
.sge(int64min
));
437 EXPECT_TRUE( s64
.sgt(int64min
));
438 EXPECT_TRUE(!s64
.sle(int64min
));
439 EXPECT_TRUE(!s64
.slt(int64min
));
441 EXPECT_TRUE(s64
!= uint64max
);
442 EXPECT_TRUE(s64
== int64max
);
443 EXPECT_TRUE(s64
!= int64min
);
445 EXPECT_TRUE( big
.uge(uint64max
));
446 EXPECT_TRUE( big
.ugt(uint64max
));
447 EXPECT_TRUE(!big
.ule(uint64max
));
448 EXPECT_TRUE(!big
.ult(uint64max
));
449 EXPECT_TRUE( big
.sge(int64max
));
450 EXPECT_TRUE( big
.sgt(int64max
));
451 EXPECT_TRUE(!big
.sle(int64max
));
452 EXPECT_TRUE(!big
.slt(int64max
));
453 EXPECT_TRUE( big
.sge(int64min
));
454 EXPECT_TRUE( big
.sgt(int64min
));
455 EXPECT_TRUE(!big
.sle(int64min
));
456 EXPECT_TRUE(!big
.slt(int64min
));
458 EXPECT_TRUE(big
!= uint64max
);
459 EXPECT_TRUE(big
!= int64max
);
460 EXPECT_TRUE(big
!= int64min
);
463 TEST(APIntTest
, compareWithInt64Min
) {
464 int64_t edge
= INT64_MIN
;
465 int64_t edgeP1
= edge
+ 1;
466 int64_t edgeM1
= INT64_MAX
;
467 auto a
= APInt
{64, static_cast<uint64_t>(edge
), true};
469 EXPECT_TRUE(!a
.slt(edge
));
470 EXPECT_TRUE( a
.sle(edge
));
471 EXPECT_TRUE(!a
.sgt(edge
));
472 EXPECT_TRUE( a
.sge(edge
));
473 EXPECT_TRUE( a
.slt(edgeP1
));
474 EXPECT_TRUE( a
.sle(edgeP1
));
475 EXPECT_TRUE(!a
.sgt(edgeP1
));
476 EXPECT_TRUE(!a
.sge(edgeP1
));
477 EXPECT_TRUE( a
.slt(edgeM1
));
478 EXPECT_TRUE( a
.sle(edgeM1
));
479 EXPECT_TRUE(!a
.sgt(edgeM1
));
480 EXPECT_TRUE(!a
.sge(edgeM1
));
483 TEST(APIntTest
, compareWithHalfInt64Max
) {
484 uint64_t edge
= 0x4000000000000000;
485 uint64_t edgeP1
= edge
+ 1;
486 uint64_t edgeM1
= edge
- 1;
487 auto a
= APInt
{64, edge
};
489 EXPECT_TRUE(!a
.ult(edge
));
490 EXPECT_TRUE( a
.ule(edge
));
491 EXPECT_TRUE(!a
.ugt(edge
));
492 EXPECT_TRUE( a
.uge(edge
));
493 EXPECT_TRUE( a
.ult(edgeP1
));
494 EXPECT_TRUE( a
.ule(edgeP1
));
495 EXPECT_TRUE(!a
.ugt(edgeP1
));
496 EXPECT_TRUE(!a
.uge(edgeP1
));
497 EXPECT_TRUE(!a
.ult(edgeM1
));
498 EXPECT_TRUE(!a
.ule(edgeM1
));
499 EXPECT_TRUE( a
.ugt(edgeM1
));
500 EXPECT_TRUE( a
.uge(edgeM1
));
502 EXPECT_TRUE(!a
.slt(edge
));
503 EXPECT_TRUE( a
.sle(edge
));
504 EXPECT_TRUE(!a
.sgt(edge
));
505 EXPECT_TRUE( a
.sge(edge
));
506 EXPECT_TRUE( a
.slt(edgeP1
));
507 EXPECT_TRUE( a
.sle(edgeP1
));
508 EXPECT_TRUE(!a
.sgt(edgeP1
));
509 EXPECT_TRUE(!a
.sge(edgeP1
));
510 EXPECT_TRUE(!a
.slt(edgeM1
));
511 EXPECT_TRUE(!a
.sle(edgeM1
));
512 EXPECT_TRUE( a
.sgt(edgeM1
));
513 EXPECT_TRUE( a
.sge(edgeM1
));
516 TEST(APIntTest
, compareLargeIntegers
) {
517 // Make sure all the combinations of signed comparisons work with big ints.
518 auto One
= APInt
{128, static_cast<uint64_t>(1), true};
519 auto Two
= APInt
{128, static_cast<uint64_t>(2), true};
520 auto MinusOne
= APInt
{128, static_cast<uint64_t>(-1), true};
521 auto MinusTwo
= APInt
{128, static_cast<uint64_t>(-2), true};
523 EXPECT_TRUE(!One
.slt(One
));
524 EXPECT_TRUE(!Two
.slt(One
));
525 EXPECT_TRUE(MinusOne
.slt(One
));
526 EXPECT_TRUE(MinusTwo
.slt(One
));
528 EXPECT_TRUE(One
.slt(Two
));
529 EXPECT_TRUE(!Two
.slt(Two
));
530 EXPECT_TRUE(MinusOne
.slt(Two
));
531 EXPECT_TRUE(MinusTwo
.slt(Two
));
533 EXPECT_TRUE(!One
.slt(MinusOne
));
534 EXPECT_TRUE(!Two
.slt(MinusOne
));
535 EXPECT_TRUE(!MinusOne
.slt(MinusOne
));
536 EXPECT_TRUE(MinusTwo
.slt(MinusOne
));
538 EXPECT_TRUE(!One
.slt(MinusTwo
));
539 EXPECT_TRUE(!Two
.slt(MinusTwo
));
540 EXPECT_TRUE(!MinusOne
.slt(MinusTwo
));
541 EXPECT_TRUE(!MinusTwo
.slt(MinusTwo
));
544 TEST(APIntTest
, binaryOpsWithRawIntegers
) {
545 // Single word check.
546 uint64_t E1
= 0x2CA7F46BF6569915ULL
;
549 EXPECT_EQ(A1
& E1
, E1
);
550 EXPECT_EQ(A1
& 0, 0);
551 EXPECT_EQ(A1
& 1, 1);
552 EXPECT_EQ(A1
& 5, 5);
553 EXPECT_EQ(A1
& UINT64_MAX
, E1
);
555 EXPECT_EQ(A1
| E1
, E1
);
556 EXPECT_EQ(A1
| 0, E1
);
557 EXPECT_EQ(A1
| 1, E1
);
558 EXPECT_EQ(A1
| 2, E1
| 2);
559 EXPECT_EQ(A1
| UINT64_MAX
, UINT64_MAX
);
561 EXPECT_EQ(A1
^ E1
, 0);
562 EXPECT_EQ(A1
^ 0, E1
);
563 EXPECT_EQ(A1
^ 1, E1
^ 1);
564 EXPECT_EQ(A1
^ 7, E1
^ 7);
565 EXPECT_EQ(A1
^ UINT64_MAX
, ~E1
);
568 uint64_t N
= 0xEB6EB136591CBA21ULL
;
569 APInt::WordType E2
[4] = {
571 0x7B9358BD6A33F10AULL
,
572 0x7E7FFA5EADD8846ULL
,
573 0x305F341CA00B613DULL
575 APInt
A2(APInt::APINT_BITS_PER_WORD
*4, E2
);
577 EXPECT_EQ(A2
& N
, N
);
578 EXPECT_EQ(A2
& 0, 0);
579 EXPECT_EQ(A2
& 1, 1);
580 EXPECT_EQ(A2
& 5, 1);
581 EXPECT_EQ(A2
& UINT64_MAX
, N
);
583 EXPECT_EQ(A2
| N
, A2
);
584 EXPECT_EQ(A2
| 0, A2
);
585 EXPECT_EQ(A2
| 1, A2
);
586 EXPECT_EQ(A2
| 2, A2
+ 2);
587 EXPECT_EQ(A2
| UINT64_MAX
, A2
- N
+ UINT64_MAX
);
589 EXPECT_EQ(A2
^ N
, A2
- N
);
590 EXPECT_EQ(A2
^ 0, A2
);
591 EXPECT_EQ(A2
^ 1, A2
- 1);
592 EXPECT_EQ(A2
^ 7, A2
+ 5);
593 EXPECT_EQ(A2
^ UINT64_MAX
, A2
- N
+ ~N
);
596 TEST(APIntTest
, rvalue_arithmetic
) {
597 // Test all combinations of lvalue/rvalue lhs/rhs of add/sub
599 // Lamdba to return an APInt by value, but also provide the raw value of the
601 auto getRValue
= [](const char *HexString
, uint64_t const *&RawData
) {
602 APInt
V(129, HexString
, 16);
603 RawData
= V
.getRawData();
607 APInt
One(129, "1", 16);
608 APInt
Two(129, "2", 16);
609 APInt
Three(129, "3", 16);
610 APInt MinusOne
= -One
;
612 const uint64_t *RawDataL
= nullptr;
613 const uint64_t *RawDataR
= nullptr;
617 APInt AddLL
= One
+ One
;
618 EXPECT_EQ(AddLL
, Two
);
620 APInt AddLR
= One
+ getRValue("1", RawDataR
);
621 EXPECT_EQ(AddLR
, Two
);
622 EXPECT_EQ(AddLR
.getRawData(), RawDataR
);
624 APInt AddRL
= getRValue("1", RawDataL
) + One
;
625 EXPECT_EQ(AddRL
, Two
);
626 EXPECT_EQ(AddRL
.getRawData(), RawDataL
);
628 APInt AddRR
= getRValue("1", RawDataL
) + getRValue("1", RawDataR
);
629 EXPECT_EQ(AddRR
, Two
);
630 EXPECT_EQ(AddRR
.getRawData(), RawDataR
);
632 // LValue's and constants
633 APInt AddLK
= One
+ 1;
634 EXPECT_EQ(AddLK
, Two
);
636 APInt AddKL
= 1 + One
;
637 EXPECT_EQ(AddKL
, Two
);
639 // RValue's and constants
640 APInt AddRK
= getRValue("1", RawDataL
) + 1;
641 EXPECT_EQ(AddRK
, Two
);
642 EXPECT_EQ(AddRK
.getRawData(), RawDataL
);
644 APInt AddKR
= 1 + getRValue("1", RawDataR
);
645 EXPECT_EQ(AddKR
, Two
);
646 EXPECT_EQ(AddKR
.getRawData(), RawDataR
);
650 // 0x0,FFFF...FFFF + 0x2 = 0x100...0001
651 APInt
AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
652 APInt
HighOneLowOne(129, "100000000000000000000000000000001", 16);
654 APInt AddLL
= AllOnes
+ Two
;
655 EXPECT_EQ(AddLL
, HighOneLowOne
);
657 APInt AddLR
= AllOnes
+ getRValue("2", RawDataR
);
658 EXPECT_EQ(AddLR
, HighOneLowOne
);
659 EXPECT_EQ(AddLR
.getRawData(), RawDataR
);
661 APInt AddRL
= getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL
) + Two
;
662 EXPECT_EQ(AddRL
, HighOneLowOne
);
663 EXPECT_EQ(AddRL
.getRawData(), RawDataL
);
665 APInt AddRR
= getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL
) +
666 getRValue("2", RawDataR
);
667 EXPECT_EQ(AddRR
, HighOneLowOne
);
668 EXPECT_EQ(AddRR
.getRawData(), RawDataR
);
670 // LValue's and constants
671 APInt AddLK
= AllOnes
+ 2;
672 EXPECT_EQ(AddLK
, HighOneLowOne
);
674 APInt AddKL
= 2 + AllOnes
;
675 EXPECT_EQ(AddKL
, HighOneLowOne
);
677 // RValue's and constants
678 APInt AddRK
= getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL
) + 2;
679 EXPECT_EQ(AddRK
, HighOneLowOne
);
680 EXPECT_EQ(AddRK
.getRawData(), RawDataL
);
682 APInt AddKR
= 2 + getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR
);
683 EXPECT_EQ(AddKR
, HighOneLowOne
);
684 EXPECT_EQ(AddKR
.getRawData(), RawDataR
);
689 APInt SubLL
= Two
- One
;
690 EXPECT_EQ(SubLL
, One
);
692 APInt SubLR
= Two
- getRValue("1", RawDataR
);
693 EXPECT_EQ(SubLR
, One
);
694 EXPECT_EQ(SubLR
.getRawData(), RawDataR
);
696 APInt SubRL
= getRValue("2", RawDataL
) - One
;
697 EXPECT_EQ(SubRL
, One
);
698 EXPECT_EQ(SubRL
.getRawData(), RawDataL
);
700 APInt SubRR
= getRValue("2", RawDataL
) - getRValue("1", RawDataR
);
701 EXPECT_EQ(SubRR
, One
);
702 EXPECT_EQ(SubRR
.getRawData(), RawDataR
);
704 // LValue's and constants
705 APInt SubLK
= Two
- 1;
706 EXPECT_EQ(SubLK
, One
);
708 APInt SubKL
= 2 - One
;
709 EXPECT_EQ(SubKL
, One
);
711 // RValue's and constants
712 APInt SubRK
= getRValue("2", RawDataL
) - 1;
713 EXPECT_EQ(SubRK
, One
);
714 EXPECT_EQ(SubRK
.getRawData(), RawDataL
);
716 APInt SubKR
= 2 - getRValue("1", RawDataR
);
717 EXPECT_EQ(SubKR
, One
);
718 EXPECT_EQ(SubKR
.getRawData(), RawDataR
);
722 // 0x100...0001 - 0x0,FFFF...FFFF = 0x2
723 APInt
AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
724 APInt
HighOneLowOne(129, "100000000000000000000000000000001", 16);
726 APInt SubLL
= HighOneLowOne
- AllOnes
;
727 EXPECT_EQ(SubLL
, Two
);
729 APInt SubLR
= HighOneLowOne
-
730 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR
);
731 EXPECT_EQ(SubLR
, Two
);
732 EXPECT_EQ(SubLR
.getRawData(), RawDataR
);
734 APInt SubRL
= getRValue("100000000000000000000000000000001", RawDataL
) -
736 EXPECT_EQ(SubRL
, Two
);
737 EXPECT_EQ(SubRL
.getRawData(), RawDataL
);
739 APInt SubRR
= getRValue("100000000000000000000000000000001", RawDataL
) -
740 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR
);
741 EXPECT_EQ(SubRR
, Two
);
742 EXPECT_EQ(SubRR
.getRawData(), RawDataR
);
744 // LValue's and constants
745 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
746 APInt SubLK
= HighOneLowOne
- 2;
747 EXPECT_EQ(SubLK
, AllOnes
);
750 APInt SubKL
= 2 - MinusOne
;
751 EXPECT_EQ(SubKL
, Three
);
753 // RValue's and constants
754 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
755 APInt SubRK
= getRValue("100000000000000000000000000000001", RawDataL
) - 2;
756 EXPECT_EQ(SubRK
, AllOnes
);
757 EXPECT_EQ(SubRK
.getRawData(), RawDataL
);
759 APInt SubKR
= 2 - getRValue("1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR
);
760 EXPECT_EQ(SubKR
, Three
);
761 EXPECT_EQ(SubKR
.getRawData(), RawDataR
);
765 TEST(APIntTest
, rvalue_bitwise
) {
766 // Test all combinations of lvalue/rvalue lhs/rhs of and/or/xor
768 // Lamdba to return an APInt by value, but also provide the raw value of the
770 auto getRValue
= [](const char *HexString
, uint64_t const *&RawData
) {
771 APInt
V(129, HexString
, 16);
772 RawData
= V
.getRawData();
776 APInt
Ten(129, "A", 16);
777 APInt
Twelve(129, "C", 16);
779 const uint64_t *RawDataL
= nullptr;
780 const uint64_t *RawDataR
= nullptr;
784 APInt AndLL
= Ten
& Twelve
;
785 EXPECT_EQ(AndLL
, 0x8);
787 APInt AndLR
= Ten
& getRValue("C", RawDataR
);
788 EXPECT_EQ(AndLR
, 0x8);
789 EXPECT_EQ(AndLR
.getRawData(), RawDataR
);
791 APInt AndRL
= getRValue("A", RawDataL
) & Twelve
;
792 EXPECT_EQ(AndRL
, 0x8);
793 EXPECT_EQ(AndRL
.getRawData(), RawDataL
);
795 APInt AndRR
= getRValue("A", RawDataL
) & getRValue("C", RawDataR
);
796 EXPECT_EQ(AndRR
, 0x8);
797 EXPECT_EQ(AndRR
.getRawData(), RawDataR
);
799 // LValue's and constants
800 APInt AndLK
= Ten
& 0xc;
801 EXPECT_EQ(AndLK
, 0x8);
803 APInt AndKL
= 0xa & Twelve
;
804 EXPECT_EQ(AndKL
, 0x8);
806 // RValue's and constants
807 APInt AndRK
= getRValue("A", RawDataL
) & 0xc;
808 EXPECT_EQ(AndRK
, 0x8);
809 EXPECT_EQ(AndRK
.getRawData(), RawDataL
);
811 APInt AndKR
= 0xa & getRValue("C", RawDataR
);
812 EXPECT_EQ(AndKR
, 0x8);
813 EXPECT_EQ(AndKR
.getRawData(), RawDataR
);
818 APInt OrLL
= Ten
| Twelve
;
819 EXPECT_EQ(OrLL
, 0xe);
821 APInt OrLR
= Ten
| getRValue("C", RawDataR
);
822 EXPECT_EQ(OrLR
, 0xe);
823 EXPECT_EQ(OrLR
.getRawData(), RawDataR
);
825 APInt OrRL
= getRValue("A", RawDataL
) | Twelve
;
826 EXPECT_EQ(OrRL
, 0xe);
827 EXPECT_EQ(OrRL
.getRawData(), RawDataL
);
829 APInt OrRR
= getRValue("A", RawDataL
) | getRValue("C", RawDataR
);
830 EXPECT_EQ(OrRR
, 0xe);
831 EXPECT_EQ(OrRR
.getRawData(), RawDataR
);
833 // LValue's and constants
834 APInt OrLK
= Ten
| 0xc;
835 EXPECT_EQ(OrLK
, 0xe);
837 APInt OrKL
= 0xa | Twelve
;
838 EXPECT_EQ(OrKL
, 0xe);
840 // RValue's and constants
841 APInt OrRK
= getRValue("A", RawDataL
) | 0xc;
842 EXPECT_EQ(OrRK
, 0xe);
843 EXPECT_EQ(OrRK
.getRawData(), RawDataL
);
845 APInt OrKR
= 0xa | getRValue("C", RawDataR
);
846 EXPECT_EQ(OrKR
, 0xe);
847 EXPECT_EQ(OrKR
.getRawData(), RawDataR
);
852 APInt XorLL
= Ten
^ Twelve
;
853 EXPECT_EQ(XorLL
, 0x6);
855 APInt XorLR
= Ten
^ getRValue("C", RawDataR
);
856 EXPECT_EQ(XorLR
, 0x6);
857 EXPECT_EQ(XorLR
.getRawData(), RawDataR
);
859 APInt XorRL
= getRValue("A", RawDataL
) ^ Twelve
;
860 EXPECT_EQ(XorRL
, 0x6);
861 EXPECT_EQ(XorRL
.getRawData(), RawDataL
);
863 APInt XorRR
= getRValue("A", RawDataL
) ^ getRValue("C", RawDataR
);
864 EXPECT_EQ(XorRR
, 0x6);
865 EXPECT_EQ(XorRR
.getRawData(), RawDataR
);
867 // LValue's and constants
868 APInt XorLK
= Ten
^ 0xc;
869 EXPECT_EQ(XorLK
, 0x6);
871 APInt XorKL
= 0xa ^ Twelve
;
872 EXPECT_EQ(XorKL
, 0x6);
874 // RValue's and constants
875 APInt XorRK
= getRValue("A", RawDataL
) ^ 0xc;
876 EXPECT_EQ(XorRK
, 0x6);
877 EXPECT_EQ(XorRK
.getRawData(), RawDataL
);
879 APInt XorKR
= 0xa ^ getRValue("C", RawDataR
);
880 EXPECT_EQ(XorKR
, 0x6);
881 EXPECT_EQ(XorKR
.getRawData(), RawDataR
);
885 TEST(APIntTest
, rvalue_invert
) {
886 // Lamdba to return an APInt by value, but also provide the raw value of the
888 auto getRValue
= [](const char *HexString
, uint64_t const *&RawData
) {
889 APInt
V(129, HexString
, 16);
890 RawData
= V
.getRawData();
895 APInt
NegativeTwo(129, -2ULL, true);
897 const uint64_t *RawData
= nullptr;
902 EXPECT_EQ(NegL
, NegativeTwo
);
904 APInt NegR
= ~getRValue("1", RawData
);
905 EXPECT_EQ(NegR
, NegativeTwo
);
906 EXPECT_EQ(NegR
.getRawData(), RawData
);
910 // Tests different div/rem varaints using scheme (a * b + c) / a
911 void testDiv(APInt a
, APInt b
, APInt c
) {
912 ASSERT_TRUE(a
.uge(b
)); // Must: a >= b
913 ASSERT_TRUE(a
.ugt(c
)); // Must: a > c
921 APInt::udivrem(p
, a
, q
, r
);
928 APInt::sdivrem(p
, a
, q
, r
);
932 if (b
.ugt(c
)) { // Test also symmetric case
937 APInt::udivrem(p
, b
, q
, r
);
944 APInt::sdivrem(p
, b
, q
, r
);
950 TEST(APIntTest
, divrem_big1
) {
951 // Tests KnuthDiv rare step D6
952 testDiv({256, "1ffffffffffffffff", 16},
953 {256, "1ffffffffffffffff", 16},
957 TEST(APIntTest
, divrem_big2
) {
958 // Tests KnuthDiv rare step D6
959 testDiv({1024, "112233ceff"
960 "cecece000000ffffffffffffffffffff"
961 "ffffffffffffffffffffffffffffffff"
962 "ffffffffffffffffffffffffffffffff"
963 "ffffffffffffffffffffffffffffff33", 16},
964 {1024, "111111ffffffffffffffff"
965 "ffffffffffffffffffffffffffffffff"
966 "fffffffffffffffffffffffffffffccf"
967 "ffffffffffffffffffffffffffffff00", 16},
971 TEST(APIntTest
, divrem_big3
) {
972 // Tests KnuthDiv case without shift
973 testDiv({256, "80000001ffffffffffffffff", 16},
974 {256, "ffffffffffffff0000000", 16},
978 TEST(APIntTest
, divrem_big4
) {
979 // Tests heap allocation in divide() enfoced by huge numbers
980 testDiv(APInt
{4096, 5}.shl(2001),
981 APInt
{4096, 1}.shl(2000),
982 APInt
{4096, 4219*13});
985 TEST(APIntTest
, divrem_big5
) {
986 // Tests one word divisor case of divide()
987 testDiv(APInt
{1024, 19}.shl(811),
988 APInt
{1024, 4356013}, // one word
992 TEST(APIntTest
, divrem_big6
) {
993 // Tests some rare "borrow" cases in D4 step
994 testDiv(APInt
{512, "ffffffffffffffff00000000000000000000000001", 16},
995 APInt
{512, "10000000000000001000000000000001", 16},
996 APInt
{512, "10000000000000000000000000000000", 16});
999 TEST(APIntTest
, divrem_big7
) {
1000 // Yet another test for KnuthDiv rare step D6.
1001 testDiv({224, "800000008000000200000005", 16},
1002 {224, "fffffffd", 16},
1003 {224, "80000000800000010000000f", 16});
1006 void testDiv(APInt a
, uint64_t b
, APInt c
) {
1011 // Unsigned division will only work if our original number wasn't negative.
1012 if (!a
.isNegative()) {
1017 APInt::udivrem(p
, b
, q
, r
);
1025 EXPECT_EQ(-c
, -r
); // Need to negate so the uint64_t compare will work.
1029 APInt::sdivrem(p
, b
, q
, sr
);
1032 EXPECT_EQ(-c
, -sr
); // Need to negate so the uint64_t compare will work.
1037 TEST(APIntTest
, divremuint
) {
1038 // Single word APInt
1039 testDiv(APInt
{64, 9},
1043 // Single word negative APInt
1044 testDiv(-APInt
{64, 9},
1048 // Multiword dividend with only one significant word.
1049 testDiv(APInt
{256, 9},
1053 // Negative dividend.
1054 testDiv(-APInt
{256, 9},
1058 // Multiword dividend
1059 testDiv(APInt
{1024, 19}.shl(811),
1060 4356013, // one word
1064 TEST(APIntTest
, divrem_simple
) {
1065 // Test simple cases.
1066 APInt
A(65, 2), B(65, 2);
1070 APInt::sdivrem(A
, B
, Q
, R
);
1071 EXPECT_EQ(Q
, APInt(65, 1));
1072 EXPECT_EQ(R
, APInt(65, 0));
1073 APInt::udivrem(A
, B
, Q
, R
);
1074 EXPECT_EQ(Q
, APInt(65, 1));
1075 EXPECT_EQ(R
, APInt(65, 0));
1079 APInt::sdivrem(O
, B
, Q
, R
);
1080 EXPECT_EQ(Q
, APInt(65, 0));
1081 EXPECT_EQ(R
, APInt(65, 0));
1082 APInt::udivrem(O
, B
, Q
, R
);
1083 EXPECT_EQ(Q
, APInt(65, 0));
1084 EXPECT_EQ(R
, APInt(65, 0));
1088 APInt::sdivrem(A
, I
, Q
, R
);
1090 EXPECT_EQ(R
, APInt(65, 0));
1091 APInt::udivrem(A
, I
, Q
, R
);
1093 EXPECT_EQ(R
, APInt(65, 0));
1096 TEST(APIntTest
, fromString
) {
1097 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
1098 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
1099 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
1100 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
1101 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
1103 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
1104 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
1105 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
1106 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
1107 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
1109 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
1110 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
1111 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
1112 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
1113 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
1115 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
1116 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
1117 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
1118 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
1119 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
1120 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
1122 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
1123 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
1124 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
1125 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
1126 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
1127 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
1129 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
1130 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
1131 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
1132 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
1133 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
1134 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
1136 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
1137 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
1138 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
1139 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
1140 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
1141 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
1143 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
1144 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
1145 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
1146 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
1147 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
1148 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
1150 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
1151 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
1152 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
1153 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
1154 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
1155 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
1157 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
1158 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
1159 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
1160 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
1161 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
1162 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
1164 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
1165 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
1166 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
1167 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
1168 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
1169 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
1171 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
1172 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
1173 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
1174 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
1175 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
1176 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
1179 TEST(APIntTest
, FromArray
) {
1180 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef
<uint64_t>(1)));
1183 TEST(APIntTest
, StringBitsNeeded2
) {
1184 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
1185 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
1186 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
1187 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
1188 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
1190 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
1191 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
1192 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
1193 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
1194 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
1196 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
1197 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
1198 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
1199 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
1200 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
1203 TEST(APIntTest
, StringBitsNeeded8
) {
1204 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
1205 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
1206 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
1207 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
1208 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
1210 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
1211 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
1212 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
1213 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
1214 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
1216 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
1217 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
1218 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
1219 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
1220 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
1223 TEST(APIntTest
, StringBitsNeeded10
) {
1224 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
1225 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
1226 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
1227 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
1228 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
1229 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
1231 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
1232 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
1233 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
1234 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
1235 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
1237 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
1238 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
1239 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
1240 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
1241 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
1244 TEST(APIntTest
, StringBitsNeeded16
) {
1245 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
1246 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
1247 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
1248 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
1249 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
1251 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
1252 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
1253 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
1254 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
1255 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
1257 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
1258 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
1259 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
1260 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
1261 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
1264 TEST(APIntTest
, toString
) {
1268 APInt(8, 0).toString(S
, 2, true, true);
1269 EXPECT_EQ(S
.str().str(), "0b0");
1271 APInt(8, 0).toString(S
, 8, true, true);
1272 EXPECT_EQ(S
.str().str(), "00");
1274 APInt(8, 0).toString(S
, 10, true, true);
1275 EXPECT_EQ(S
.str().str(), "0");
1277 APInt(8, 0).toString(S
, 16, true, true);
1278 EXPECT_EQ(S
.str().str(), "0x0");
1280 APInt(8, 0).toString(S
, 36, true, false);
1281 EXPECT_EQ(S
.str().str(), "0");
1285 APInt(8, 255, isSigned
).toString(S
, 2, isSigned
, true);
1286 EXPECT_EQ(S
.str().str(), "0b11111111");
1288 APInt(8, 255, isSigned
).toString(S
, 8, isSigned
, true);
1289 EXPECT_EQ(S
.str().str(), "0377");
1291 APInt(8, 255, isSigned
).toString(S
, 10, isSigned
, true);
1292 EXPECT_EQ(S
.str().str(), "255");
1294 APInt(8, 255, isSigned
).toString(S
, 16, isSigned
, true);
1295 EXPECT_EQ(S
.str().str(), "0xFF");
1297 APInt(8, 255, isSigned
).toString(S
, 36, isSigned
, false);
1298 EXPECT_EQ(S
.str().str(), "73");
1302 APInt(8, 255, isSigned
).toString(S
, 2, isSigned
, true);
1303 EXPECT_EQ(S
.str().str(), "-0b1");
1305 APInt(8, 255, isSigned
).toString(S
, 8, isSigned
, true);
1306 EXPECT_EQ(S
.str().str(), "-01");
1308 APInt(8, 255, isSigned
).toString(S
, 10, isSigned
, true);
1309 EXPECT_EQ(S
.str().str(), "-1");
1311 APInt(8, 255, isSigned
).toString(S
, 16, isSigned
, true);
1312 EXPECT_EQ(S
.str().str(), "-0x1");
1314 APInt(8, 255, isSigned
).toString(S
, 36, isSigned
, false);
1315 EXPECT_EQ(S
.str().str(), "-1");
1319 TEST(APIntTest
, Log2
) {
1320 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
1321 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
1322 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
1323 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
1324 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
1325 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
1326 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
1327 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
1328 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
1331 TEST(APIntTest
, magic
) {
1332 EXPECT_EQ(APInt(32, 3).magic().m
, APInt(32, "55555556", 16));
1333 EXPECT_EQ(APInt(32, 3).magic().s
, 0U);
1334 EXPECT_EQ(APInt(32, 5).magic().m
, APInt(32, "66666667", 16));
1335 EXPECT_EQ(APInt(32, 5).magic().s
, 1U);
1336 EXPECT_EQ(APInt(32, 7).magic().m
, APInt(32, "92492493", 16));
1337 EXPECT_EQ(APInt(32, 7).magic().s
, 2U);
1340 TEST(APIntTest
, magicu
) {
1341 EXPECT_EQ(APInt(32, 3).magicu().m
, APInt(32, "AAAAAAAB", 16));
1342 EXPECT_EQ(APInt(32, 3).magicu().s
, 1U);
1343 EXPECT_EQ(APInt(32, 5).magicu().m
, APInt(32, "CCCCCCCD", 16));
1344 EXPECT_EQ(APInt(32, 5).magicu().s
, 2U);
1345 EXPECT_EQ(APInt(32, 7).magicu().m
, APInt(32, "24924925", 16));
1346 EXPECT_EQ(APInt(32, 7).magicu().s
, 3U);
1347 EXPECT_EQ(APInt(64, 25).magicu(1).m
, APInt(64, "A3D70A3D70A3D70B", 16));
1348 EXPECT_EQ(APInt(64, 25).magicu(1).s
, 4U);
1351 #ifdef GTEST_HAS_DEATH_TEST
1353 TEST(APIntTest
, StringDeath
) {
1354 EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
1355 EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
1356 EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
1357 EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
1358 EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
1359 EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
1360 EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
1361 EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
1362 EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
1367 TEST(APIntTest
, mul_clear
) {
1368 APInt
ValA(65, -1ULL);
1373 EXPECT_EQ(ValA
.toString(10, false), ValC
.toString(10, false));
1376 TEST(APIntTest
, Rotate
) {
1377 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
1378 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
1379 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
1380 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
1381 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
1383 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
1384 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
1385 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
1386 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
1387 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
1389 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1390 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1392 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1393 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1394 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(33, 33)));
1395 EXPECT_EQ(APInt(32, (1 << 8)), APInt(32, 1).rotl(APInt(32, 40)));
1396 EXPECT_EQ(APInt(32, (1 << 30)), APInt(32, 1).rotl(APInt(31, 30)));
1397 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotl(APInt(31, 31)));
1399 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(1, 0)));
1400 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(1, 1)));
1402 EXPECT_EQ(APInt(32, 16), APInt(32, 1).rotl(APInt(3, 4)));
1404 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(64, 64)));
1405 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(64, 65)));
1407 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 3)));
1408 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 10)));
1409 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(5, 10)));
1410 EXPECT_EQ(APInt(7, 6), APInt(7, 3).rotl(APInt(12, 120)));
1412 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
1413 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
1414 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
1415 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
1416 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
1418 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
1419 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
1420 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
1421 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
1422 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
1424 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1425 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1427 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1428 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1429 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(33, 33)));
1430 EXPECT_EQ(APInt(32, (1 << 24)), APInt(32, 1).rotr(APInt(32, 40)));
1432 EXPECT_EQ(APInt(32, (1 << 2)), APInt(32, 1).rotr(APInt(31, 30)));
1433 EXPECT_EQ(APInt(32, (1 << 1)), APInt(32, 1).rotr(APInt(31, 31)));
1435 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(1, 0)));
1436 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(1, 1)));
1438 EXPECT_EQ(APInt(32, (1 << 28)), APInt(32, 1).rotr(APInt(3, 4)));
1440 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(64, 64)));
1441 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(64, 65)));
1443 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 3)));
1444 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 10)));
1445 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(5, 10)));
1446 EXPECT_EQ(APInt(7, 65), APInt(7, 3).rotr(APInt(12, 120)));
1448 APInt
Big(256, "00004000800000000000000000003fff8000000000000003", 16);
1449 APInt
Rot(256, "3fff80000000000000030000000000000000000040008000", 16);
1450 EXPECT_EQ(Rot
, Big
.rotr(144));
1452 EXPECT_EQ(APInt(32, 8), APInt(32, 1).rotl(Big
));
1453 EXPECT_EQ(APInt(32, (1 << 29)), APInt(32, 1).rotr(Big
));
1456 TEST(APIntTest
, Splat
) {
1457 APInt
ValA(8, 0x01);
1458 EXPECT_EQ(ValA
, APInt::getSplat(8, ValA
));
1459 EXPECT_EQ(APInt(64, 0x0101010101010101ULL
), APInt::getSplat(64, ValA
));
1462 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB
));
1463 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB
));
1466 TEST(APIntTest
, tcDecrement
) {
1467 // Test single word decrement.
1471 APInt::WordType singleWord
= ~APInt::WordType(0) << (APInt::APINT_BITS_PER_WORD
- 1);
1472 APInt::WordType carry
= APInt::tcDecrement(&singleWord
, 1);
1473 EXPECT_EQ(carry
, APInt::WordType(0));
1474 EXPECT_EQ(singleWord
, ~APInt::WordType(0) >> 1);
1479 APInt::WordType singleWord
= 0;
1480 APInt::WordType carry
= APInt::tcDecrement(&singleWord
, 1);
1481 EXPECT_EQ(carry
, APInt::WordType(1));
1482 EXPECT_EQ(singleWord
, ~APInt::WordType(0));
1485 // Test multiword decrement.
1487 // No across word borrow, no out borrow.
1489 APInt::WordType test
[4] = {0x1, 0x1, 0x1, 0x1};
1490 APInt::WordType expected
[4] = {0x0, 0x1, 0x1, 0x1};
1491 APInt::tcDecrement(test
, 4);
1492 EXPECT_EQ(APInt::tcCompare(test
, expected
, 4), 0);
1495 // 1 across word borrow, no out borrow.
1497 APInt::WordType test
[4] = {0x0, 0xF, 0x1, 0x1};
1498 APInt::WordType expected
[4] = {~APInt::WordType(0), 0xE, 0x1, 0x1};
1499 APInt::WordType carry
= APInt::tcDecrement(test
, 4);
1500 EXPECT_EQ(carry
, APInt::WordType(0));
1501 EXPECT_EQ(APInt::tcCompare(test
, expected
, 4), 0);
1504 // 2 across word borrow, no out borrow.
1506 APInt::WordType test
[4] = {0x0, 0x0, 0xC, 0x1};
1507 APInt::WordType expected
[4] = {~APInt::WordType(0), ~APInt::WordType(0), 0xB, 0x1};
1508 APInt::WordType carry
= APInt::tcDecrement(test
, 4);
1509 EXPECT_EQ(carry
, APInt::WordType(0));
1510 EXPECT_EQ(APInt::tcCompare(test
, expected
, 4), 0);
1513 // 3 across word borrow, no out borrow.
1515 APInt::WordType test
[4] = {0x0, 0x0, 0x0, 0x1};
1516 APInt::WordType expected
[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), 0x0};
1517 APInt::WordType carry
= APInt::tcDecrement(test
, 4);
1518 EXPECT_EQ(carry
, APInt::WordType(0));
1519 EXPECT_EQ(APInt::tcCompare(test
, expected
, 4), 0);
1522 // 3 across word borrow, with out borrow.
1524 APInt::WordType test
[4] = {0x0, 0x0, 0x0, 0x0};
1525 APInt::WordType expected
[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0)};
1526 APInt::WordType carry
= APInt::tcDecrement(test
, 4);
1527 EXPECT_EQ(carry
, APInt::WordType(1));
1528 EXPECT_EQ(APInt::tcCompare(test
, expected
, 4), 0);
1532 TEST(APIntTest
, arrayAccess
) {
1533 // Single word check.
1534 uint64_t E1
= 0x2CA7F46BF6569915ULL
;
1536 for (unsigned i
= 0, e
= 64; i
< e
; ++i
) {
1537 EXPECT_EQ(bool(E1
& (1ULL << i
)),
1542 APInt::WordType E2
[4] = {
1543 0xEB6EB136591CBA21ULL
,
1544 0x7B9358BD6A33F10AULL
,
1545 0x7E7FFA5EADD8846ULL
,
1546 0x305F341CA00B613DULL
1548 APInt
A2(APInt::APINT_BITS_PER_WORD
*4, E2
);
1549 for (unsigned i
= 0; i
< 4; ++i
) {
1550 for (unsigned j
= 0; j
< APInt::APINT_BITS_PER_WORD
; ++j
) {
1551 EXPECT_EQ(bool(E2
[i
] & (1ULL << j
)),
1552 A2
[i
*APInt::APINT_BITS_PER_WORD
+ j
]);
1557 TEST(APIntTest
, LargeAPIntConstruction
) {
1558 // Check that we can properly construct very large APInt. It is very
1559 // unlikely that people will ever do this, but it is a legal input,
1560 // so we should not crash on it.
1561 APInt
A9(UINT32_MAX
, 0);
1562 EXPECT_FALSE(A9
.getBoolValue());
1565 TEST(APIntTest
, nearestLogBase2
) {
1566 // Single word check.
1569 uint64_t I1
= 0x1800001;
1571 EXPECT_EQ(A1
.nearestLogBase2(), A1
.ceilLogBase2());
1574 uint64_t I2
= 0x1000011;
1576 EXPECT_EQ(A2
.nearestLogBase2(), A2
.logBase2());
1578 // Test ties round up.
1579 uint64_t I3
= 0x1800000;
1581 EXPECT_EQ(A3
.nearestLogBase2(), A3
.ceilLogBase2());
1583 // Multiple word check.
1586 APInt::WordType I4
[4] = {0x0, 0xF, 0x18, 0x0};
1587 APInt
A4(APInt::APINT_BITS_PER_WORD
*4, I4
);
1588 EXPECT_EQ(A4
.nearestLogBase2(), A4
.ceilLogBase2());
1591 APInt::WordType I5
[4] = {0x0, 0xF, 0x10, 0x0};
1592 APInt
A5(APInt::APINT_BITS_PER_WORD
*4, I5
);
1593 EXPECT_EQ(A5
.nearestLogBase2(), A5
.logBase2());
1595 // Test ties round up.
1596 uint64_t I6
[4] = {0x0, 0x0, 0x0, 0x18};
1597 APInt
A6(APInt::APINT_BITS_PER_WORD
*4, I6
);
1598 EXPECT_EQ(A6
.nearestLogBase2(), A6
.ceilLogBase2());
1600 // Test BitWidth == 1 special cases.
1602 EXPECT_EQ(A7
.nearestLogBase2(), 0ULL);
1604 EXPECT_EQ(A8
.nearestLogBase2(), UINT32_MAX
);
1606 // Test the zero case when we have a bit width large enough such
1607 // that the bit width is larger than UINT32_MAX-1.
1608 APInt
A9(UINT32_MAX
, 0);
1609 EXPECT_EQ(A9
.nearestLogBase2(), UINT32_MAX
);
1612 TEST(APIntTest
, IsSplat
) {
1613 APInt
A(32, 0x01010101);
1614 EXPECT_FALSE(A
.isSplat(1));
1615 EXPECT_FALSE(A
.isSplat(2));
1616 EXPECT_FALSE(A
.isSplat(4));
1617 EXPECT_TRUE(A
.isSplat(8));
1618 EXPECT_TRUE(A
.isSplat(16));
1619 EXPECT_TRUE(A
.isSplat(32));
1621 APInt
B(24, 0xAAAAAA);
1622 EXPECT_FALSE(B
.isSplat(1));
1623 EXPECT_TRUE(B
.isSplat(2));
1624 EXPECT_TRUE(B
.isSplat(4));
1625 EXPECT_TRUE(B
.isSplat(8));
1626 EXPECT_TRUE(B
.isSplat(24));
1628 APInt
C(24, 0xABAAAB);
1629 EXPECT_FALSE(C
.isSplat(1));
1630 EXPECT_FALSE(C
.isSplat(2));
1631 EXPECT_FALSE(C
.isSplat(4));
1632 EXPECT_FALSE(C
.isSplat(8));
1633 EXPECT_TRUE(C
.isSplat(24));
1635 APInt
D(32, 0xABBAABBA);
1636 EXPECT_FALSE(D
.isSplat(1));
1637 EXPECT_FALSE(D
.isSplat(2));
1638 EXPECT_FALSE(D
.isSplat(4));
1639 EXPECT_FALSE(D
.isSplat(8));
1640 EXPECT_TRUE(D
.isSplat(16));
1641 EXPECT_TRUE(D
.isSplat(32));
1644 EXPECT_TRUE(E
.isSplat(1));
1645 EXPECT_TRUE(E
.isSplat(2));
1646 EXPECT_TRUE(E
.isSplat(4));
1647 EXPECT_TRUE(E
.isSplat(8));
1648 EXPECT_TRUE(E
.isSplat(16));
1649 EXPECT_TRUE(E
.isSplat(32));
1652 TEST(APIntTest
, isMask
) {
1653 EXPECT_FALSE(APInt(32, 0x01010101).isMask());
1654 EXPECT_FALSE(APInt(32, 0xf0000000).isMask());
1655 EXPECT_FALSE(APInt(32, 0xffff0000).isMask());
1656 EXPECT_FALSE(APInt(32, 0xff << 1).isMask());
1658 for (int N
: { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1659 EXPECT_FALSE(APInt(N
, 0).isMask());
1662 for (int I
= 1; I
<= N
; ++I
) {
1663 APInt MaskVal
= One
.shl(I
) - 1;
1664 EXPECT_TRUE(MaskVal
.isMask());
1665 EXPECT_TRUE(MaskVal
.isMask(I
));
1670 TEST(APIntTest
, isShiftedMask
) {
1671 EXPECT_FALSE(APInt(32, 0x01010101).isShiftedMask());
1672 EXPECT_TRUE(APInt(32, 0xf0000000).isShiftedMask());
1673 EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask());
1674 EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask());
1676 for (int N
: { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1677 EXPECT_FALSE(APInt(N
, 0).isShiftedMask());
1680 for (int I
= 1; I
< N
; ++I
) {
1681 APInt MaskVal
= One
.shl(I
) - 1;
1682 EXPECT_TRUE(MaskVal
.isShiftedMask());
1684 for (int I
= 1; I
< N
- 1; ++I
) {
1685 APInt MaskVal
= One
.shl(I
);
1686 EXPECT_TRUE(MaskVal
.isShiftedMask());
1688 for (int I
= 1; I
< N
; ++I
) {
1689 APInt MaskVal
= APInt::getHighBitsSet(N
, I
);
1690 EXPECT_TRUE(MaskVal
.isShiftedMask());
1695 // Test that self-move works, but only when we're using MSVC.
1696 #if defined(_MSC_VER)
1697 #if defined(__clang__)
1698 // Disable the pragma warning from versions of Clang without -Wself-move
1699 #pragma clang diagnostic push
1700 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1701 // Disable the warning that triggers on exactly what is being tested.
1702 #pragma clang diagnostic push
1703 #pragma clang diagnostic ignored "-Wself-move"
1705 TEST(APIntTest
, SelfMoveAssignment
) {
1706 APInt
X(32, 0xdeadbeef);
1708 EXPECT_EQ(32u, X
.getBitWidth());
1709 EXPECT_EQ(0xdeadbeefULL
, X
.getLimitedValue());
1711 uint64_t Bits
[] = {0xdeadbeefdeadbeefULL
, 0xdeadbeefdeadbeefULL
};
1714 EXPECT_EQ(128u, Y
.getBitWidth());
1715 EXPECT_EQ(~0ULL, Y
.getLimitedValue());
1716 const uint64_t *Raw
= Y
.getRawData();
1717 EXPECT_EQ(2u, Y
.getNumWords());
1718 EXPECT_EQ(0xdeadbeefdeadbeefULL
, Raw
[0]);
1719 EXPECT_EQ(0xdeadbeefdeadbeefULL
, Raw
[1]);
1721 #if defined(__clang__)
1722 #pragma clang diagnostic pop
1723 #pragma clang diagnostic pop
1727 TEST(APIntTest
, reverseBits
) {
1728 EXPECT_EQ(1, APInt(1, 1).reverseBits());
1729 EXPECT_EQ(0, APInt(1, 0).reverseBits());
1731 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1732 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1734 EXPECT_EQ(0xb, APInt(4, 0xd).reverseBits());
1735 EXPECT_EQ(0xd, APInt(4, 0xb).reverseBits());
1736 EXPECT_EQ(0xf, APInt(4, 0xf).reverseBits());
1738 EXPECT_EQ(0x30, APInt(7, 0x6).reverseBits());
1739 EXPECT_EQ(0x5a, APInt(7, 0x2d).reverseBits());
1741 EXPECT_EQ(0x0f, APInt(8, 0xf0).reverseBits());
1742 EXPECT_EQ(0xf0, APInt(8, 0x0f).reverseBits());
1744 EXPECT_EQ(0x0f0f, APInt(16, 0xf0f0).reverseBits());
1745 EXPECT_EQ(0xf0f0, APInt(16, 0x0f0f).reverseBits());
1747 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1748 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1750 EXPECT_EQ(0x402880a0 >> 1, APInt(31, 0x05011402).reverseBits());
1752 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1753 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1755 EXPECT_EQ(0x0f0f0f0f0f0f0f0f, APInt(64, 0xf0f0f0f0f0f0f0f0).reverseBits());
1756 EXPECT_EQ(0xf0f0f0f0f0f0f0f0, APInt(64, 0x0f0f0f0f0f0f0f0f).reverseBits());
1758 for (unsigned N
: { 1, 8, 16, 24, 31, 32, 33,
1759 63, 64, 65, 127, 128, 257, 1024 }) {
1760 for (unsigned I
= 0; I
< N
; ++I
) {
1761 APInt X
= APInt::getOneBitSet(N
, I
);
1762 APInt Y
= APInt::getOneBitSet(N
, N
- (I
+ 1));
1763 EXPECT_EQ(Y
, X
.reverseBits());
1764 EXPECT_EQ(X
, Y
.reverseBits());
1769 TEST(APIntTest
, insertBits
) {
1770 APInt
iSrc(31, 0x00123456);
1773 APInt
i31(31, 0x76543210ull
);
1774 i31
.insertBits(iSrc
, 0);
1775 EXPECT_EQ(static_cast<int64_t>(0x00123456ull
), i31
.getSExtValue());
1777 // Single word src/dst insertion.
1778 APInt
i63(63, 0x01234567FFFFFFFFull
);
1779 i63
.insertBits(iSrc
, 4);
1780 EXPECT_EQ(static_cast<int64_t>(0x012345600123456Full
), i63
.getSExtValue());
1782 // Insert single word src into one word of dst.
1783 APInt
i120(120, UINT64_MAX
, true);
1784 i120
.insertBits(iSrc
, 8);
1785 EXPECT_EQ(static_cast<int64_t>(0xFFFFFF80123456FFull
), i120
.getSExtValue());
1787 // Insert single word src into two words of dst.
1788 APInt
i127(127, UINT64_MAX
, true);
1789 i127
.insertBits(iSrc
, 48);
1790 EXPECT_EQ(i127
.extractBits(64, 0).getZExtValue(), 0x3456FFFFFFFFFFFFull
);
1791 EXPECT_EQ(i127
.extractBits(63, 64).getZExtValue(), 0x7FFFFFFFFFFF8012ull
);
1793 // Insert on word boundaries.
1795 i128
.insertBits(APInt(64, UINT64_MAX
, true), 0);
1796 i128
.insertBits(APInt(64, UINT64_MAX
, true), 64);
1797 EXPECT_EQ(-1, i128
.getSExtValue());
1799 APInt
i256(256, UINT64_MAX
, true);
1800 i256
.insertBits(APInt(65, 0), 0);
1801 i256
.insertBits(APInt(69, 0), 64);
1802 i256
.insertBits(APInt(128, 0), 128);
1803 EXPECT_EQ(0u, i256
.getSExtValue());
1806 i257
.insertBits(APInt(96, UINT64_MAX
, true), 64);
1807 EXPECT_EQ(i257
.extractBits(64, 0).getZExtValue(), 0x0000000000000000ull
);
1808 EXPECT_EQ(i257
.extractBits(64, 64).getZExtValue(), 0xFFFFFFFFFFFFFFFFull
);
1809 EXPECT_EQ(i257
.extractBits(64, 128).getZExtValue(), 0x00000000FFFFFFFFull
);
1810 EXPECT_EQ(i257
.extractBits(65, 192).getZExtValue(), 0x0000000000000000ull
);
1812 // General insertion.
1813 APInt
i260(260, UINT64_MAX
, true);
1814 i260
.insertBits(APInt(129, 1ull << 48), 15);
1815 EXPECT_EQ(i260
.extractBits(64, 0).getZExtValue(), 0x8000000000007FFFull
);
1816 EXPECT_EQ(i260
.extractBits(64, 64).getZExtValue(), 0x0000000000000000ull
);
1817 EXPECT_EQ(i260
.extractBits(64, 128).getZExtValue(), 0xFFFFFFFFFFFF0000ull
);
1818 EXPECT_EQ(i260
.extractBits(64, 192).getZExtValue(), 0xFFFFFFFFFFFFFFFFull
);
1819 EXPECT_EQ(i260
.extractBits(4, 256).getZExtValue(), 0x000000000000000Full
);
1822 TEST(APIntTest
, extractBits
) {
1823 APInt
i32(32, 0x1234567);
1824 EXPECT_EQ(0x3456, i32
.extractBits(16, 4));
1826 APInt
i257(257, 0xFFFFFFFFFF0000FFull
, true);
1827 EXPECT_EQ(0xFFu
, i257
.extractBits(16, 0));
1828 EXPECT_EQ((0xFFu
>> 1), i257
.extractBits(16, 1));
1829 EXPECT_EQ(-1, i257
.extractBits(32, 64).getSExtValue());
1830 EXPECT_EQ(-1, i257
.extractBits(128, 128).getSExtValue());
1831 EXPECT_EQ(-1, i257
.extractBits(66, 191).getSExtValue());
1832 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full
),
1833 i257
.extractBits(128, 1).getSExtValue());
1834 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full
),
1835 i257
.extractBits(129, 1).getSExtValue());
1837 EXPECT_EQ(APInt(48, 0),
1838 APInt(144, "281474976710655", 10).extractBits(48, 48));
1839 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull
),
1840 APInt(144, "281474976710655", 10).extractBits(48, 0));
1841 EXPECT_EQ(APInt(48, 0x00007fffffffffffull
),
1842 APInt(144, "281474976710655", 10).extractBits(48, 1));
1845 TEST(APIntTest
, getLowBitsSet
) {
1846 APInt i128lo64
= APInt::getLowBitsSet(128, 64);
1847 EXPECT_EQ(0u, i128lo64
.countLeadingOnes());
1848 EXPECT_EQ(64u, i128lo64
.countLeadingZeros());
1849 EXPECT_EQ(64u, i128lo64
.getActiveBits());
1850 EXPECT_EQ(0u, i128lo64
.countTrailingZeros());
1851 EXPECT_EQ(64u, i128lo64
.countTrailingOnes());
1852 EXPECT_EQ(64u, i128lo64
.countPopulation());
1855 TEST(APIntTest
, getBitsSet
) {
1856 APInt i64hi1lo1
= APInt::getBitsSet(64, 1, 63);
1857 EXPECT_EQ(0u, i64hi1lo1
.countLeadingOnes());
1858 EXPECT_EQ(1u, i64hi1lo1
.countLeadingZeros());
1859 EXPECT_EQ(63u, i64hi1lo1
.getActiveBits());
1860 EXPECT_EQ(1u, i64hi1lo1
.countTrailingZeros());
1861 EXPECT_EQ(0u, i64hi1lo1
.countTrailingOnes());
1862 EXPECT_EQ(62u, i64hi1lo1
.countPopulation());
1864 APInt i127hi1lo1
= APInt::getBitsSet(127, 1, 126);
1865 EXPECT_EQ(0u, i127hi1lo1
.countLeadingOnes());
1866 EXPECT_EQ(1u, i127hi1lo1
.countLeadingZeros());
1867 EXPECT_EQ(126u, i127hi1lo1
.getActiveBits());
1868 EXPECT_EQ(1u, i127hi1lo1
.countTrailingZeros());
1869 EXPECT_EQ(0u, i127hi1lo1
.countTrailingOnes());
1870 EXPECT_EQ(125u, i127hi1lo1
.countPopulation());
1873 TEST(APIntTest
, getHighBitsSet
) {
1874 APInt i64hi32
= APInt::getHighBitsSet(64, 32);
1875 EXPECT_EQ(32u, i64hi32
.countLeadingOnes());
1876 EXPECT_EQ(0u, i64hi32
.countLeadingZeros());
1877 EXPECT_EQ(64u, i64hi32
.getActiveBits());
1878 EXPECT_EQ(32u, i64hi32
.countTrailingZeros());
1879 EXPECT_EQ(0u, i64hi32
.countTrailingOnes());
1880 EXPECT_EQ(32u, i64hi32
.countPopulation());
1883 TEST(APIntTest
, getBitsSetFrom
) {
1884 APInt i64hi31
= APInt::getBitsSetFrom(64, 33);
1885 EXPECT_EQ(31u, i64hi31
.countLeadingOnes());
1886 EXPECT_EQ(0u, i64hi31
.countLeadingZeros());
1887 EXPECT_EQ(64u, i64hi31
.getActiveBits());
1888 EXPECT_EQ(33u, i64hi31
.countTrailingZeros());
1889 EXPECT_EQ(0u, i64hi31
.countTrailingOnes());
1890 EXPECT_EQ(31u, i64hi31
.countPopulation());
1893 TEST(APIntTest
, setLowBits
) {
1894 APInt
i64lo32(64, 0);
1895 i64lo32
.setLowBits(32);
1896 EXPECT_EQ(0u, i64lo32
.countLeadingOnes());
1897 EXPECT_EQ(32u, i64lo32
.countLeadingZeros());
1898 EXPECT_EQ(32u, i64lo32
.getActiveBits());
1899 EXPECT_EQ(0u, i64lo32
.countTrailingZeros());
1900 EXPECT_EQ(32u, i64lo32
.countTrailingOnes());
1901 EXPECT_EQ(32u, i64lo32
.countPopulation());
1903 APInt
i128lo64(128, 0);
1904 i128lo64
.setLowBits(64);
1905 EXPECT_EQ(0u, i128lo64
.countLeadingOnes());
1906 EXPECT_EQ(64u, i128lo64
.countLeadingZeros());
1907 EXPECT_EQ(64u, i128lo64
.getActiveBits());
1908 EXPECT_EQ(0u, i128lo64
.countTrailingZeros());
1909 EXPECT_EQ(64u, i128lo64
.countTrailingOnes());
1910 EXPECT_EQ(64u, i128lo64
.countPopulation());
1912 APInt
i128lo24(128, 0);
1913 i128lo24
.setLowBits(24);
1914 EXPECT_EQ(0u, i128lo24
.countLeadingOnes());
1915 EXPECT_EQ(104u, i128lo24
.countLeadingZeros());
1916 EXPECT_EQ(24u, i128lo24
.getActiveBits());
1917 EXPECT_EQ(0u, i128lo24
.countTrailingZeros());
1918 EXPECT_EQ(24u, i128lo24
.countTrailingOnes());
1919 EXPECT_EQ(24u, i128lo24
.countPopulation());
1921 APInt
i128lo104(128, 0);
1922 i128lo104
.setLowBits(104);
1923 EXPECT_EQ(0u, i128lo104
.countLeadingOnes());
1924 EXPECT_EQ(24u, i128lo104
.countLeadingZeros());
1925 EXPECT_EQ(104u, i128lo104
.getActiveBits());
1926 EXPECT_EQ(0u, i128lo104
.countTrailingZeros());
1927 EXPECT_EQ(104u, i128lo104
.countTrailingOnes());
1928 EXPECT_EQ(104u, i128lo104
.countPopulation());
1930 APInt
i128lo0(128, 0);
1931 i128lo0
.setLowBits(0);
1932 EXPECT_EQ(0u, i128lo0
.countLeadingOnes());
1933 EXPECT_EQ(128u, i128lo0
.countLeadingZeros());
1934 EXPECT_EQ(0u, i128lo0
.getActiveBits());
1935 EXPECT_EQ(128u, i128lo0
.countTrailingZeros());
1936 EXPECT_EQ(0u, i128lo0
.countTrailingOnes());
1937 EXPECT_EQ(0u, i128lo0
.countPopulation());
1939 APInt
i80lo79(80, 0);
1940 i80lo79
.setLowBits(79);
1941 EXPECT_EQ(0u, i80lo79
.countLeadingOnes());
1942 EXPECT_EQ(1u, i80lo79
.countLeadingZeros());
1943 EXPECT_EQ(79u, i80lo79
.getActiveBits());
1944 EXPECT_EQ(0u, i80lo79
.countTrailingZeros());
1945 EXPECT_EQ(79u, i80lo79
.countTrailingOnes());
1946 EXPECT_EQ(79u, i80lo79
.countPopulation());
1949 TEST(APIntTest
, setHighBits
) {
1950 APInt
i64hi32(64, 0);
1951 i64hi32
.setHighBits(32);
1952 EXPECT_EQ(32u, i64hi32
.countLeadingOnes());
1953 EXPECT_EQ(0u, i64hi32
.countLeadingZeros());
1954 EXPECT_EQ(64u, i64hi32
.getActiveBits());
1955 EXPECT_EQ(32u, i64hi32
.countTrailingZeros());
1956 EXPECT_EQ(0u, i64hi32
.countTrailingOnes());
1957 EXPECT_EQ(32u, i64hi32
.countPopulation());
1959 APInt
i128hi64(128, 0);
1960 i128hi64
.setHighBits(64);
1961 EXPECT_EQ(64u, i128hi64
.countLeadingOnes());
1962 EXPECT_EQ(0u, i128hi64
.countLeadingZeros());
1963 EXPECT_EQ(128u, i128hi64
.getActiveBits());
1964 EXPECT_EQ(64u, i128hi64
.countTrailingZeros());
1965 EXPECT_EQ(0u, i128hi64
.countTrailingOnes());
1966 EXPECT_EQ(64u, i128hi64
.countPopulation());
1968 APInt
i128hi24(128, 0);
1969 i128hi24
.setHighBits(24);
1970 EXPECT_EQ(24u, i128hi24
.countLeadingOnes());
1971 EXPECT_EQ(0u, i128hi24
.countLeadingZeros());
1972 EXPECT_EQ(128u, i128hi24
.getActiveBits());
1973 EXPECT_EQ(104u, i128hi24
.countTrailingZeros());
1974 EXPECT_EQ(0u, i128hi24
.countTrailingOnes());
1975 EXPECT_EQ(24u, i128hi24
.countPopulation());
1977 APInt
i128hi104(128, 0);
1978 i128hi104
.setHighBits(104);
1979 EXPECT_EQ(104u, i128hi104
.countLeadingOnes());
1980 EXPECT_EQ(0u, i128hi104
.countLeadingZeros());
1981 EXPECT_EQ(128u, i128hi104
.getActiveBits());
1982 EXPECT_EQ(24u, i128hi104
.countTrailingZeros());
1983 EXPECT_EQ(0u, i128hi104
.countTrailingOnes());
1984 EXPECT_EQ(104u, i128hi104
.countPopulation());
1986 APInt
i128hi0(128, 0);
1987 i128hi0
.setHighBits(0);
1988 EXPECT_EQ(0u, i128hi0
.countLeadingOnes());
1989 EXPECT_EQ(128u, i128hi0
.countLeadingZeros());
1990 EXPECT_EQ(0u, i128hi0
.getActiveBits());
1991 EXPECT_EQ(128u, i128hi0
.countTrailingZeros());
1992 EXPECT_EQ(0u, i128hi0
.countTrailingOnes());
1993 EXPECT_EQ(0u, i128hi0
.countPopulation());
1995 APInt
i80hi1(80, 0);
1996 i80hi1
.setHighBits(1);
1997 EXPECT_EQ(1u, i80hi1
.countLeadingOnes());
1998 EXPECT_EQ(0u, i80hi1
.countLeadingZeros());
1999 EXPECT_EQ(80u, i80hi1
.getActiveBits());
2000 EXPECT_EQ(79u, i80hi1
.countTrailingZeros());
2001 EXPECT_EQ(0u, i80hi1
.countTrailingOnes());
2002 EXPECT_EQ(1u, i80hi1
.countPopulation());
2004 APInt
i32hi16(32, 0);
2005 i32hi16
.setHighBits(16);
2006 EXPECT_EQ(16u, i32hi16
.countLeadingOnes());
2007 EXPECT_EQ(0u, i32hi16
.countLeadingZeros());
2008 EXPECT_EQ(32u, i32hi16
.getActiveBits());
2009 EXPECT_EQ(16u, i32hi16
.countTrailingZeros());
2010 EXPECT_EQ(0u, i32hi16
.countTrailingOnes());
2011 EXPECT_EQ(16u, i32hi16
.countPopulation());
2014 TEST(APIntTest
, setBitsFrom
) {
2015 APInt
i64from63(64, 0);
2016 i64from63
.setBitsFrom(63);
2017 EXPECT_EQ(1u, i64from63
.countLeadingOnes());
2018 EXPECT_EQ(0u, i64from63
.countLeadingZeros());
2019 EXPECT_EQ(64u, i64from63
.getActiveBits());
2020 EXPECT_EQ(63u, i64from63
.countTrailingZeros());
2021 EXPECT_EQ(0u, i64from63
.countTrailingOnes());
2022 EXPECT_EQ(1u, i64from63
.countPopulation());
2025 TEST(APIntTest
, setAllBits
) {
2028 EXPECT_EQ(32u, i32
.countLeadingOnes());
2029 EXPECT_EQ(0u, i32
.countLeadingZeros());
2030 EXPECT_EQ(32u, i32
.getActiveBits());
2031 EXPECT_EQ(0u, i32
.countTrailingZeros());
2032 EXPECT_EQ(32u, i32
.countTrailingOnes());
2033 EXPECT_EQ(32u, i32
.countPopulation());
2037 EXPECT_EQ(64u, i64
.countLeadingOnes());
2038 EXPECT_EQ(0u, i64
.countLeadingZeros());
2039 EXPECT_EQ(64u, i64
.getActiveBits());
2040 EXPECT_EQ(0u, i64
.countTrailingZeros());
2041 EXPECT_EQ(64u, i64
.countTrailingOnes());
2042 EXPECT_EQ(64u, i64
.countPopulation());
2046 EXPECT_EQ(96u, i96
.countLeadingOnes());
2047 EXPECT_EQ(0u, i96
.countLeadingZeros());
2048 EXPECT_EQ(96u, i96
.getActiveBits());
2049 EXPECT_EQ(0u, i96
.countTrailingZeros());
2050 EXPECT_EQ(96u, i96
.countTrailingOnes());
2051 EXPECT_EQ(96u, i96
.countPopulation());
2055 EXPECT_EQ(128u, i128
.countLeadingOnes());
2056 EXPECT_EQ(0u, i128
.countLeadingZeros());
2057 EXPECT_EQ(128u, i128
.getActiveBits());
2058 EXPECT_EQ(0u, i128
.countTrailingZeros());
2059 EXPECT_EQ(128u, i128
.countTrailingOnes());
2060 EXPECT_EQ(128u, i128
.countPopulation());
2063 TEST(APIntTest
, getLoBits
) {
2064 APInt
i32(32, 0xfa);
2066 EXPECT_EQ(0xa, i32
.getLoBits(4));
2067 APInt
i128(128, 0xfa);
2068 i128
.setHighBits(1);
2069 EXPECT_EQ(0xa, i128
.getLoBits(4));
2072 TEST(APIntTest
, getHiBits
) {
2073 APInt
i32(32, 0xfa);
2075 EXPECT_EQ(0xc, i32
.getHiBits(4));
2076 APInt
i128(128, 0xfa);
2077 i128
.setHighBits(2);
2078 EXPECT_EQ(0xc, i128
.getHiBits(4));
2081 TEST(APIntTest
, GCD
) {
2082 using APIntOps::GreatestCommonDivisor
;
2084 for (unsigned Bits
: {1, 2, 32, 63, 64, 65}) {
2085 // Test some corner cases near zero.
2086 APInt
Zero(Bits
, 0), One(Bits
, 1);
2087 EXPECT_EQ(GreatestCommonDivisor(Zero
, Zero
), Zero
);
2088 EXPECT_EQ(GreatestCommonDivisor(Zero
, One
), One
);
2089 EXPECT_EQ(GreatestCommonDivisor(One
, Zero
), One
);
2090 EXPECT_EQ(GreatestCommonDivisor(One
, One
), One
);
2094 EXPECT_EQ(GreatestCommonDivisor(Zero
, Two
), Two
);
2095 EXPECT_EQ(GreatestCommonDivisor(One
, Two
), One
);
2096 EXPECT_EQ(GreatestCommonDivisor(Two
, Two
), Two
);
2098 // Test some corner cases near the highest representable value.
2101 EXPECT_EQ(GreatestCommonDivisor(Zero
, Max
), Max
);
2102 EXPECT_EQ(GreatestCommonDivisor(One
, Max
), One
);
2103 EXPECT_EQ(GreatestCommonDivisor(Two
, Max
), One
);
2104 EXPECT_EQ(GreatestCommonDivisor(Max
, Max
), Max
);
2106 APInt MaxOver2
= Max
.udiv(Two
);
2107 EXPECT_EQ(GreatestCommonDivisor(MaxOver2
, Max
), One
);
2108 // Max - 1 == Max / 2 * 2, because Max is odd.
2109 EXPECT_EQ(GreatestCommonDivisor(MaxOver2
, Max
- 1), MaxOver2
);
2113 // Compute the 20th Mersenne prime.
2114 const unsigned BitWidth
= 4450;
2115 APInt HugePrime
= APInt::getLowBitsSet(BitWidth
, 4423);
2117 // 9931 and 123456 are coprime.
2118 APInt A
= HugePrime
* APInt(BitWidth
, 9931);
2119 APInt B
= HugePrime
* APInt(BitWidth
, 123456);
2120 APInt C
= GreatestCommonDivisor(A
, B
);
2121 EXPECT_EQ(C
, HugePrime
);
2124 TEST(APIntTest
, LogicalRightShift
) {
2125 APInt
i256(APInt::getHighBitsSet(256, 2));
2127 i256
.lshrInPlace(1);
2128 EXPECT_EQ(1U, i256
.countLeadingZeros());
2129 EXPECT_EQ(253U, i256
.countTrailingZeros());
2130 EXPECT_EQ(2U, i256
.countPopulation());
2132 i256
.lshrInPlace(62);
2133 EXPECT_EQ(63U, i256
.countLeadingZeros());
2134 EXPECT_EQ(191U, i256
.countTrailingZeros());
2135 EXPECT_EQ(2U, i256
.countPopulation());
2137 i256
.lshrInPlace(65);
2138 EXPECT_EQ(128U, i256
.countLeadingZeros());
2139 EXPECT_EQ(126U, i256
.countTrailingZeros());
2140 EXPECT_EQ(2U, i256
.countPopulation());
2142 i256
.lshrInPlace(64);
2143 EXPECT_EQ(192U, i256
.countLeadingZeros());
2144 EXPECT_EQ(62U, i256
.countTrailingZeros());
2145 EXPECT_EQ(2U, i256
.countPopulation());
2147 i256
.lshrInPlace(63);
2148 EXPECT_EQ(255U, i256
.countLeadingZeros());
2149 EXPECT_EQ(0U, i256
.countTrailingZeros());
2150 EXPECT_EQ(1U, i256
.countPopulation());
2152 // Ensure we handle large shifts of multi-word.
2153 const APInt
neg_one(128, static_cast<uint64_t>(-1), true);
2154 EXPECT_EQ(0, neg_one
.lshr(128));
2157 TEST(APIntTest
, ArithmeticRightShift
) {
2158 APInt
i72(APInt::getHighBitsSet(72, 1));
2159 i72
.ashrInPlace(46);
2160 EXPECT_EQ(47U, i72
.countLeadingOnes());
2161 EXPECT_EQ(25U, i72
.countTrailingZeros());
2162 EXPECT_EQ(47U, i72
.countPopulation());
2164 i72
= APInt::getHighBitsSet(72, 1);
2165 i72
.ashrInPlace(64);
2166 EXPECT_EQ(65U, i72
.countLeadingOnes());
2167 EXPECT_EQ(7U, i72
.countTrailingZeros());
2168 EXPECT_EQ(65U, i72
.countPopulation());
2170 APInt
i128(APInt::getHighBitsSet(128, 1));
2171 i128
.ashrInPlace(64);
2172 EXPECT_EQ(65U, i128
.countLeadingOnes());
2173 EXPECT_EQ(63U, i128
.countTrailingZeros());
2174 EXPECT_EQ(65U, i128
.countPopulation());
2176 // Ensure we handle large shifts of multi-word.
2177 const APInt
signmin32(APInt::getSignedMinValue(32));
2178 EXPECT_TRUE(signmin32
.ashr(32).isAllOnesValue());
2180 // Ensure we handle large shifts of multi-word.
2181 const APInt
umax32(APInt::getSignedMaxValue(32));
2182 EXPECT_EQ(0, umax32
.ashr(32));
2184 // Ensure we handle large shifts of multi-word.
2185 const APInt
signmin128(APInt::getSignedMinValue(128));
2186 EXPECT_TRUE(signmin128
.ashr(128).isAllOnesValue());
2188 // Ensure we handle large shifts of multi-word.
2189 const APInt
umax128(APInt::getSignedMaxValue(128));
2190 EXPECT_EQ(0, umax128
.ashr(128));
2193 TEST(APIntTest
, LeftShift
) {
2194 APInt
i256(APInt::getLowBitsSet(256, 2));
2197 EXPECT_EQ(253U, i256
.countLeadingZeros());
2198 EXPECT_EQ(1U, i256
.countTrailingZeros());
2199 EXPECT_EQ(2U, i256
.countPopulation());
2202 EXPECT_EQ(191U, i256
.countLeadingZeros());
2203 EXPECT_EQ(63U, i256
.countTrailingZeros());
2204 EXPECT_EQ(2U, i256
.countPopulation());
2207 EXPECT_EQ(126U, i256
.countLeadingZeros());
2208 EXPECT_EQ(128U, i256
.countTrailingZeros());
2209 EXPECT_EQ(2U, i256
.countPopulation());
2212 EXPECT_EQ(62U, i256
.countLeadingZeros());
2213 EXPECT_EQ(192U, i256
.countTrailingZeros());
2214 EXPECT_EQ(2U, i256
.countPopulation());
2217 EXPECT_EQ(0U, i256
.countLeadingZeros());
2218 EXPECT_EQ(255U, i256
.countTrailingZeros());
2219 EXPECT_EQ(1U, i256
.countPopulation());
2221 // Ensure we handle large shifts of multi-word.
2222 const APInt
neg_one(128, static_cast<uint64_t>(-1), true);
2223 EXPECT_EQ(0, neg_one
.shl(128));
2226 TEST(APIntTest
, isSubsetOf
) {
2230 EXPECT_FALSE(i32_3
.isSubsetOf(i32_1
));
2231 EXPECT_TRUE(i32_1
.isSubsetOf(i32_3
));
2232 EXPECT_FALSE(i32_2
.isSubsetOf(i32_1
));
2233 EXPECT_FALSE(i32_1
.isSubsetOf(i32_2
));
2234 EXPECT_TRUE(i32_3
.isSubsetOf(i32_3
));
2236 APInt
i128_1(128, 1);
2237 APInt
i128_2(128, 2);
2238 APInt
i128_3(128, 3);
2239 EXPECT_FALSE(i128_3
.isSubsetOf(i128_1
));
2240 EXPECT_TRUE(i128_1
.isSubsetOf(i128_3
));
2241 EXPECT_FALSE(i128_2
.isSubsetOf(i128_1
));
2242 EXPECT_FALSE(i128_1
.isSubsetOf(i128_2
));
2243 EXPECT_TRUE(i128_3
.isSubsetOf(i128_3
));
2248 EXPECT_FALSE(i128_3
.isSubsetOf(i128_1
));
2249 EXPECT_TRUE(i128_1
.isSubsetOf(i128_3
));
2250 EXPECT_FALSE(i128_2
.isSubsetOf(i128_1
));
2251 EXPECT_FALSE(i128_1
.isSubsetOf(i128_2
));
2252 EXPECT_TRUE(i128_3
.isSubsetOf(i128_3
));
2255 TEST(APIntTest
, sext
) {
2256 EXPECT_EQ(0, APInt(1, 0).sext(64));
2257 EXPECT_EQ(~uint64_t(0), APInt(1, 1).sext(64));
2259 APInt
i32_max(APInt::getSignedMaxValue(32).sext(63));
2260 EXPECT_EQ(32U, i32_max
.countLeadingZeros());
2261 EXPECT_EQ(0U, i32_max
.countTrailingZeros());
2262 EXPECT_EQ(31U, i32_max
.countPopulation());
2264 APInt
i32_min(APInt::getSignedMinValue(32).sext(63));
2265 EXPECT_EQ(32U, i32_min
.countLeadingOnes());
2266 EXPECT_EQ(31U, i32_min
.countTrailingZeros());
2267 EXPECT_EQ(32U, i32_min
.countPopulation());
2269 APInt
i32_neg1(APInt(32, ~uint64_t(0)).sext(63));
2270 EXPECT_EQ(63U, i32_neg1
.countLeadingOnes());
2271 EXPECT_EQ(0U, i32_neg1
.countTrailingZeros());
2272 EXPECT_EQ(63U, i32_neg1
.countPopulation());
2275 TEST(APIntTest
, multiply
) {
2276 APInt
i64(64, 1234);
2278 EXPECT_EQ(7006652, i64
* 5678);
2279 EXPECT_EQ(7006652, 5678 * i64
);
2281 APInt i128
= APInt::getOneBitSet(128, 64);
2282 APInt
i128_1234(128, 1234);
2284 EXPECT_EQ(i128_1234
, i128
* 1234);
2285 EXPECT_EQ(i128_1234
, 1234 * i128
);
2287 APInt i96
= APInt::getOneBitSet(96, 64);
2289 EXPECT_EQ(32U, i96
.countLeadingOnes());
2290 EXPECT_EQ(32U, i96
.countPopulation());
2291 EXPECT_EQ(64U, i96
.countTrailingZeros());
2294 TEST(APIntTest
, RoundingUDiv
) {
2295 for (uint64_t Ai
= 1; Ai
<= 255; Ai
++) {
2298 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero
, A
, APInt::Rounding::UP
));
2299 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero
, A
, APInt::Rounding::DOWN
));
2300 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero
, A
, APInt::Rounding::TOWARD_ZERO
));
2302 for (uint64_t Bi
= 1; Bi
<= 255; Bi
++) {
2305 APInt Quo
= APIntOps::RoundingUDiv(A
, B
, APInt::Rounding::UP
);
2306 auto Prod
= Quo
.zext(16) * B
.zext(16);
2307 EXPECT_TRUE(Prod
.uge(Ai
));
2309 EXPECT_TRUE(((Quo
- 1).zext(16) * B
.zext(16)).ult(Ai
));
2313 APInt Quo
= A
.udiv(B
);
2314 EXPECT_EQ(Quo
, APIntOps::RoundingUDiv(A
, B
, APInt::Rounding::TOWARD_ZERO
));
2315 EXPECT_EQ(Quo
, APIntOps::RoundingUDiv(A
, B
, APInt::Rounding::DOWN
));
2321 TEST(APIntTest
, RoundingSDiv
) {
2322 for (int64_t Ai
= -128; Ai
<= 127; Ai
++) {
2327 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero
, A
, APInt::Rounding::UP
));
2328 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero
, A
, APInt::Rounding::DOWN
));
2329 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero
, A
, APInt::Rounding::TOWARD_ZERO
));
2332 for (uint64_t Bi
= -128; Bi
<= 127; Bi
++) {
2338 APInt Quo
= APIntOps::RoundingSDiv(A
, B
, APInt::Rounding::UP
);
2339 auto Prod
= Quo
.sext(16) * B
.sext(16);
2340 EXPECT_TRUE(Prod
.uge(A
));
2342 EXPECT_TRUE(((Quo
- 1).sext(16) * B
.sext(16)).ult(A
));
2346 APInt Quo
= APIntOps::RoundingSDiv(A
, B
, APInt::Rounding::DOWN
);
2347 auto Prod
= Quo
.sext(16) * B
.sext(16);
2348 EXPECT_TRUE(Prod
.ule(A
));
2350 EXPECT_TRUE(((Quo
+ 1).sext(16) * B
.sext(16)).ugt(A
));
2354 APInt Quo
= A
.sdiv(B
);
2355 EXPECT_EQ(Quo
, APIntOps::RoundingSDiv(A
, B
, APInt::Rounding::TOWARD_ZERO
));
2361 TEST(APIntTest
, SolveQuadraticEquationWrap
) {
2362 // Verify that "Solution" is the first non-negative integer that solves
2363 // Ax^2 + Bx + C = "0 or overflow", i.e. that it is a correct solution
2364 // as calculated by SolveQuadraticEquationWrap.
2365 auto Validate
= [] (int A
, int B
, int C
, unsigned Width
, int Solution
) {
2366 int Mask
= (1 << Width
) - 1;
2368 // Solution should be non-negative.
2369 EXPECT_GE(Solution
, 0);
2371 auto OverflowBits
= [] (int64_t V
, unsigned W
) {
2372 return V
& -(1 << W
);
2375 int64_t Over0
= OverflowBits(C
, Width
);
2377 auto IsZeroOrOverflow
= [&] (int X
) {
2378 int64_t ValueAtX
= A
*X
*X
+ B
*X
+ C
;
2379 int64_t OverX
= OverflowBits(ValueAtX
, Width
);
2380 return (ValueAtX
& Mask
) == 0 || OverX
!= Over0
;
2383 auto EquationToString
= [&] (const char *X_str
) {
2384 return (Twine(A
) + Twine(X_str
) + Twine("^2 + ") + Twine(B
) +
2385 Twine(X_str
) + Twine(" + ") + Twine(C
) + Twine(", bitwidth: ") +
2386 Twine(Width
)).str();
2389 auto IsSolution
= [&] (const char *X_str
, int X
) {
2390 if (IsZeroOrOverflow(X
))
2391 return ::testing::AssertionSuccess()
2392 << X
<< " is a solution of " << EquationToString(X_str
);
2393 return ::testing::AssertionFailure()
2394 << X
<< " is not an expected solution of "
2395 << EquationToString(X_str
);
2398 auto IsNotSolution
= [&] (const char *X_str
, int X
) {
2399 if (!IsZeroOrOverflow(X
))
2400 return ::testing::AssertionSuccess()
2401 << X
<< " is not a solution of " << EquationToString(X_str
);
2402 return ::testing::AssertionFailure()
2403 << X
<< " is an unexpected solution of "
2404 << EquationToString(X_str
);
2407 // This is the important part: make sure that there is no solution that
2408 // is less than the calculated one.
2410 for (int X
= 1; X
< Solution
-1; ++X
)
2411 EXPECT_PRED_FORMAT1(IsNotSolution
, X
);
2414 // Verify that the calculated solution is indeed a solution.
2415 EXPECT_PRED_FORMAT1(IsSolution
, Solution
);
2418 // Generate all possible quadratic equations with Width-bit wide integer
2419 // coefficients, get the solution from SolveQuadraticEquationWrap, and
2420 // verify that the solution is correct.
2421 auto Iterate
= [&] (unsigned Width
) {
2422 assert(1 < Width
&& Width
< 32);
2423 int Low
= -(1 << (Width
-1));
2424 int High
= (1 << (Width
-1));
2426 for (int A
= Low
; A
!= High
; ++A
) {
2429 for (int B
= Low
; B
!= High
; ++B
) {
2430 for (int C
= Low
; C
!= High
; ++C
) {
2431 Optional
<APInt
> S
= APIntOps::SolveQuadraticEquationWrap(
2432 APInt(Width
, A
), APInt(Width
, B
),
2433 APInt(Width
, C
), Width
);
2435 Validate(A
, B
, C
, Width
, S
->getSExtValue());
2441 // Test all widths in [2..6].
2442 for (unsigned i
= 2; i
<= 6; ++i
)
2446 } // end anonymous namespace