Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / ADT / APIntTest.cpp
blob3b909f8f7d14e2ed8226c373b7f42c31b8532195
1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/APInt.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/Alignment.h"
15 #include "gtest/gtest.h"
16 #include <array>
17 #include <optional>
19 using namespace llvm;
21 namespace {
23 TEST(APIntTest, ValueInit) {
24 APInt Zero = APInt();
25 EXPECT_TRUE(!Zero);
26 EXPECT_TRUE(!Zero.zext(64));
27 EXPECT_TRUE(!Zero.sext(64));
30 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
31 TEST(APIntTest, ShiftLeftByZero) {
32 APInt One = APInt::getZero(65) + 1;
33 APInt Shl = One.shl(0);
34 EXPECT_TRUE(Shl[0]);
35 EXPECT_FALSE(Shl[1]);
38 TEST(APIntTest, i64_ArithmeticRightShiftNegative) {
39 const APInt neg_one(64, static_cast<uint64_t>(-1), true);
40 EXPECT_EQ(neg_one, neg_one.ashr(7));
43 TEST(APIntTest, i128_NegativeCount) {
44 APInt Minus3(128, static_cast<uint64_t>(-3), true);
45 EXPECT_EQ(126u, Minus3.countl_one());
46 EXPECT_EQ(-3, Minus3.getSExtValue());
48 APInt Minus1(128, static_cast<uint64_t>(-1), true);
49 EXPECT_EQ(0u, Minus1.countl_zero());
50 EXPECT_EQ(128u, Minus1.countl_one());
51 EXPECT_EQ(128u, Minus1.getActiveBits());
52 EXPECT_EQ(0u, Minus1.countr_zero());
53 EXPECT_EQ(128u, Minus1.countr_one());
54 EXPECT_EQ(128u, Minus1.popcount());
55 EXPECT_EQ(-1, Minus1.getSExtValue());
58 TEST(APIntTest, i33_Count) {
59 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
60 EXPECT_EQ(0u, i33minus2.countl_zero());
61 EXPECT_EQ(32u, i33minus2.countl_one());
62 EXPECT_EQ(33u, i33minus2.getActiveBits());
63 EXPECT_EQ(1u, i33minus2.countr_zero());
64 EXPECT_EQ(32u, i33minus2.popcount());
65 EXPECT_EQ(-2, i33minus2.getSExtValue());
66 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
69 TEST(APIntTest, i61_Count) {
70 APInt i61(61, 1 << 15);
71 EXPECT_EQ(45u, i61.countl_zero());
72 EXPECT_EQ(0u, i61.countl_one());
73 EXPECT_EQ(16u, i61.getActiveBits());
74 EXPECT_EQ(15u, i61.countr_zero());
75 EXPECT_EQ(1u, i61.popcount());
76 EXPECT_EQ(static_cast<int64_t>(1 << 15), i61.getSExtValue());
77 EXPECT_EQ(static_cast<uint64_t>(1 << 15), i61.getZExtValue());
79 i61.setBits(8, 19);
80 EXPECT_EQ(42u, i61.countl_zero());
81 EXPECT_EQ(0u, i61.countl_one());
82 EXPECT_EQ(19u, i61.getActiveBits());
83 EXPECT_EQ(8u, i61.countr_zero());
84 EXPECT_EQ(11u, i61.popcount());
85 EXPECT_EQ(static_cast<int64_t>((1 << 19) - (1 << 8)), i61.getSExtValue());
86 EXPECT_EQ(static_cast<uint64_t>((1 << 19) - (1 << 8)), i61.getZExtValue());
89 TEST(APIntTest, i65_Count) {
90 APInt i65(65, 0, true);
91 EXPECT_EQ(65u, i65.countl_zero());
92 EXPECT_EQ(0u, i65.countl_one());
93 EXPECT_EQ(0u, i65.getActiveBits());
94 EXPECT_EQ(1u, i65.getActiveWords());
95 EXPECT_EQ(65u, i65.countr_zero());
96 EXPECT_EQ(0u, i65.popcount());
98 APInt i65minus(65, 0, true);
99 i65minus.setBit(64);
100 EXPECT_EQ(0u, i65minus.countl_zero());
101 EXPECT_EQ(1u, i65minus.countl_one());
102 EXPECT_EQ(65u, i65minus.getActiveBits());
103 EXPECT_EQ(64u, i65minus.countr_zero());
104 EXPECT_EQ(1u, i65minus.popcount());
107 TEST(APIntTest, i128_PositiveCount) {
108 APInt u128max = APInt::getAllOnes(128);
109 EXPECT_EQ(128u, u128max.countl_one());
110 EXPECT_EQ(0u, u128max.countl_zero());
111 EXPECT_EQ(128u, u128max.getActiveBits());
112 EXPECT_EQ(0u, u128max.countr_zero());
113 EXPECT_EQ(128u, u128max.countr_one());
114 EXPECT_EQ(128u, u128max.popcount());
116 APInt u64max(128, static_cast<uint64_t>(-1), false);
117 EXPECT_EQ(64u, u64max.countl_zero());
118 EXPECT_EQ(0u, u64max.countl_one());
119 EXPECT_EQ(64u, u64max.getActiveBits());
120 EXPECT_EQ(0u, u64max.countr_zero());
121 EXPECT_EQ(64u, u64max.countr_one());
122 EXPECT_EQ(64u, u64max.popcount());
123 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
125 APInt zero(128, 0, true);
126 EXPECT_EQ(128u, zero.countl_zero());
127 EXPECT_EQ(0u, zero.countl_one());
128 EXPECT_EQ(0u, zero.getActiveBits());
129 EXPECT_EQ(128u, zero.countr_zero());
130 EXPECT_EQ(0u, zero.countr_one());
131 EXPECT_EQ(0u, zero.popcount());
132 EXPECT_EQ(0u, zero.getSExtValue());
133 EXPECT_EQ(0u, zero.getZExtValue());
135 APInt one(128, 1, true);
136 EXPECT_EQ(127u, one.countl_zero());
137 EXPECT_EQ(0u, one.countl_one());
138 EXPECT_EQ(1u, one.getActiveBits());
139 EXPECT_EQ(0u, one.countr_zero());
140 EXPECT_EQ(1u, one.countr_one());
141 EXPECT_EQ(1u, one.popcount());
142 EXPECT_EQ(1, one.getSExtValue());
143 EXPECT_EQ(1u, one.getZExtValue());
145 APInt s128(128, 2, true);
146 EXPECT_EQ(126u, s128.countl_zero());
147 EXPECT_EQ(0u, s128.countl_one());
148 EXPECT_EQ(2u, s128.getActiveBits());
149 EXPECT_EQ(1u, s128.countr_zero());
150 EXPECT_EQ(0u, s128.countr_one());
151 EXPECT_EQ(1u, s128.popcount());
152 EXPECT_EQ(2, s128.getSExtValue());
153 EXPECT_EQ(2u, s128.getZExtValue());
155 // NOP Test
156 s128.setBits(42, 42);
157 EXPECT_EQ(126u, s128.countl_zero());
158 EXPECT_EQ(0u, s128.countl_one());
159 EXPECT_EQ(2u, s128.getActiveBits());
160 EXPECT_EQ(1u, s128.countr_zero());
161 EXPECT_EQ(0u, s128.countr_one());
162 EXPECT_EQ(1u, s128.popcount());
163 EXPECT_EQ(2, s128.getSExtValue());
164 EXPECT_EQ(2u, s128.getZExtValue());
166 s128.setBits(3, 32);
167 EXPECT_EQ(96u, s128.countl_zero());
168 EXPECT_EQ(0u, s128.countl_one());
169 EXPECT_EQ(32u, s128.getActiveBits());
170 EXPECT_EQ(33u, s128.getSignificantBits());
171 EXPECT_EQ(1u, s128.countr_zero());
172 EXPECT_EQ(0u, s128.countr_one());
173 EXPECT_EQ(30u, s128.popcount());
174 EXPECT_EQ(static_cast<uint32_t>((~0u << 3) | 2), s128.getZExtValue());
176 s128.setBits(62, 128);
177 EXPECT_EQ(0u, s128.countl_zero());
178 EXPECT_EQ(66u, s128.countl_one());
179 EXPECT_EQ(128u, s128.getActiveBits());
180 EXPECT_EQ(63u, s128.getSignificantBits());
181 EXPECT_EQ(1u, s128.countr_zero());
182 EXPECT_EQ(0u, s128.countr_one());
183 EXPECT_EQ(96u, s128.popcount());
184 EXPECT_EQ(static_cast<int64_t>((3ull << 62) |
185 static_cast<uint32_t>((~0u << 3) | 2)),
186 s128.getSExtValue());
189 TEST(APIntTest, i256) {
190 APInt s256(256, 15, true);
191 EXPECT_EQ(252u, s256.countl_zero());
192 EXPECT_EQ(0u, s256.countl_one());
193 EXPECT_EQ(4u, s256.getActiveBits());
194 EXPECT_EQ(0u, s256.countr_zero());
195 EXPECT_EQ(4u, s256.countr_one());
196 EXPECT_EQ(4u, s256.popcount());
197 EXPECT_EQ(15, s256.getSExtValue());
198 EXPECT_EQ(15u, s256.getZExtValue());
200 s256.setBits(62, 66);
201 EXPECT_EQ(190u, s256.countl_zero());
202 EXPECT_EQ(0u, s256.countl_one());
203 EXPECT_EQ(66u, s256.getActiveBits());
204 EXPECT_EQ(67u, s256.getSignificantBits());
205 EXPECT_EQ(0u, s256.countr_zero());
206 EXPECT_EQ(4u, s256.countr_one());
207 EXPECT_EQ(8u, s256.popcount());
209 s256.setBits(60, 256);
210 EXPECT_EQ(0u, s256.countl_zero());
211 EXPECT_EQ(196u, s256.countl_one());
212 EXPECT_EQ(256u, s256.getActiveBits());
213 EXPECT_EQ(61u, s256.getSignificantBits());
214 EXPECT_EQ(0u, s256.countr_zero());
215 EXPECT_EQ(4u, s256.countr_one());
216 EXPECT_EQ(200u, s256.popcount());
217 EXPECT_EQ(static_cast<int64_t>((~0ull << 60) | 15), s256.getSExtValue());
220 TEST(APIntTest, i1) {
221 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
222 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
223 const APInt zero(1, 0);
224 const APInt one(1, 1);
225 const APInt two(1, 2);
227 EXPECT_EQ(0, neg_two.getSExtValue());
228 EXPECT_EQ(-1, neg_one.getSExtValue());
229 EXPECT_EQ(1u, neg_one.getZExtValue());
230 EXPECT_EQ(0u, zero.getZExtValue());
231 EXPECT_EQ(-1, one.getSExtValue());
232 EXPECT_EQ(1u, one.getZExtValue());
233 EXPECT_EQ(0u, two.getZExtValue());
234 EXPECT_EQ(0, two.getSExtValue());
236 // Basic equalities for 1-bit values.
237 EXPECT_EQ(zero, two);
238 EXPECT_EQ(zero, neg_two);
239 EXPECT_EQ(one, neg_one);
240 EXPECT_EQ(two, neg_two);
242 // Min/max signed values.
243 EXPECT_TRUE(zero.isMaxSignedValue());
244 EXPECT_FALSE(one.isMaxSignedValue());
245 EXPECT_FALSE(zero.isMinSignedValue());
246 EXPECT_TRUE(one.isMinSignedValue());
248 // Additions.
249 EXPECT_EQ(two, one + one);
250 EXPECT_EQ(zero, neg_one + one);
251 EXPECT_EQ(neg_two, neg_one + neg_one);
253 // Subtractions.
254 EXPECT_EQ(neg_two, neg_one - one);
255 EXPECT_EQ(two, one - neg_one);
256 EXPECT_EQ(zero, one - one);
258 // And
259 EXPECT_EQ(zero, zero & zero);
260 EXPECT_EQ(zero, one & zero);
261 EXPECT_EQ(zero, zero & one);
262 EXPECT_EQ(one, one & one);
263 EXPECT_EQ(zero, zero & zero);
264 EXPECT_EQ(zero, neg_one & zero);
265 EXPECT_EQ(zero, zero & neg_one);
266 EXPECT_EQ(neg_one, neg_one & neg_one);
268 // Or
269 EXPECT_EQ(zero, zero | zero);
270 EXPECT_EQ(one, one | zero);
271 EXPECT_EQ(one, zero | one);
272 EXPECT_EQ(one, one | one);
273 EXPECT_EQ(zero, zero | zero);
274 EXPECT_EQ(neg_one, neg_one | zero);
275 EXPECT_EQ(neg_one, zero | neg_one);
276 EXPECT_EQ(neg_one, neg_one | neg_one);
278 // Xor
279 EXPECT_EQ(zero, zero ^ zero);
280 EXPECT_EQ(one, one ^ zero);
281 EXPECT_EQ(one, zero ^ one);
282 EXPECT_EQ(zero, one ^ one);
283 EXPECT_EQ(zero, zero ^ zero);
284 EXPECT_EQ(neg_one, neg_one ^ zero);
285 EXPECT_EQ(neg_one, zero ^ neg_one);
286 EXPECT_EQ(zero, neg_one ^ neg_one);
288 // Shifts.
289 EXPECT_EQ(zero, one << one);
290 EXPECT_EQ(one, one << zero);
291 EXPECT_EQ(zero, one.shl(1));
292 EXPECT_EQ(one, one.shl(0));
293 EXPECT_EQ(zero, one.lshr(1));
294 EXPECT_EQ(one, one.ashr(1));
296 // Rotates.
297 EXPECT_EQ(one, one.rotl(0));
298 EXPECT_EQ(one, one.rotl(1));
299 EXPECT_EQ(one, one.rotr(0));
300 EXPECT_EQ(one, one.rotr(1));
302 // Multiplies.
303 EXPECT_EQ(neg_one, neg_one * one);
304 EXPECT_EQ(neg_one, one * neg_one);
305 EXPECT_EQ(one, neg_one * neg_one);
306 EXPECT_EQ(one, one * one);
308 // Divides.
309 EXPECT_EQ(neg_one, one.sdiv(neg_one));
310 EXPECT_EQ(neg_one, neg_one.sdiv(one));
311 EXPECT_EQ(one, neg_one.sdiv(neg_one));
312 EXPECT_EQ(one, one.sdiv(one));
314 EXPECT_EQ(neg_one, one.udiv(neg_one));
315 EXPECT_EQ(neg_one, neg_one.udiv(one));
316 EXPECT_EQ(one, neg_one.udiv(neg_one));
317 EXPECT_EQ(one, one.udiv(one));
319 // Remainders.
320 EXPECT_EQ(zero, neg_one.srem(one));
321 EXPECT_EQ(zero, neg_one.urem(one));
322 EXPECT_EQ(zero, one.srem(neg_one));
324 // sdivrem
326 APInt q(8, 0);
327 APInt r(8, 0);
328 APInt one(8, 1);
329 APInt two(8, 2);
330 APInt nine(8, 9);
331 APInt four(8, 4);
333 EXPECT_EQ(nine.srem(two), one);
334 EXPECT_EQ(nine.srem(-two), one);
335 EXPECT_EQ((-nine).srem(two), -one);
336 EXPECT_EQ((-nine).srem(-two), -one);
338 APInt::sdivrem(nine, two, q, r);
339 EXPECT_EQ(four, q);
340 EXPECT_EQ(one, r);
341 APInt::sdivrem(-nine, two, q, r);
342 EXPECT_EQ(-four, q);
343 EXPECT_EQ(-one, r);
344 APInt::sdivrem(nine, -two, q, r);
345 EXPECT_EQ(-four, q);
346 EXPECT_EQ(one, r);
347 APInt::sdivrem(-nine, -two, q, r);
348 EXPECT_EQ(four, q);
349 EXPECT_EQ(-one, r);
353 TEST(APIntTest, compare) {
354 std::array<APInt, 5> testVals{{
355 APInt{16, 2},
356 APInt{16, 1},
357 APInt{16, 0},
358 APInt{16, (uint64_t)-1, true},
359 APInt{16, (uint64_t)-2, true},
362 for (auto &arg1 : testVals)
363 for (auto &arg2 : testVals) {
364 auto uv1 = arg1.getZExtValue();
365 auto uv2 = arg2.getZExtValue();
366 auto sv1 = arg1.getSExtValue();
367 auto sv2 = arg2.getSExtValue();
369 EXPECT_EQ(uv1 < uv2, arg1.ult(arg2));
370 EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2));
371 EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2));
372 EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2));
374 EXPECT_EQ(sv1 < sv2, arg1.slt(arg2));
375 EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2));
376 EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2));
377 EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2));
379 EXPECT_EQ(uv1 < uv2, arg1.ult(uv2));
380 EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2));
381 EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2));
382 EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2));
384 EXPECT_EQ(sv1 < sv2, arg1.slt(sv2));
385 EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2));
386 EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2));
387 EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2));
391 TEST(APIntTest, compareWithRawIntegers) {
392 EXPECT_TRUE(!APInt(8, 1).uge(256));
393 EXPECT_TRUE(!APInt(8, 1).ugt(256));
394 EXPECT_TRUE( APInt(8, 1).ule(256));
395 EXPECT_TRUE( APInt(8, 1).ult(256));
396 EXPECT_TRUE(!APInt(8, 1).sge(256));
397 EXPECT_TRUE(!APInt(8, 1).sgt(256));
398 EXPECT_TRUE( APInt(8, 1).sle(256));
399 EXPECT_TRUE( APInt(8, 1).slt(256));
400 EXPECT_TRUE(!(APInt(8, 0) == 256));
401 EXPECT_TRUE( APInt(8, 0) != 256);
402 EXPECT_TRUE(!(APInt(8, 1) == 256));
403 EXPECT_TRUE( APInt(8, 1) != 256);
405 auto uint64max = UINT64_MAX;
406 auto int64max = INT64_MAX;
407 auto int64min = INT64_MIN;
409 auto u64 = APInt{128, uint64max};
410 auto s64 = APInt{128, static_cast<uint64_t>(int64max), true};
411 auto big = u64 + 1;
413 EXPECT_TRUE( u64.uge(uint64max));
414 EXPECT_TRUE(!u64.ugt(uint64max));
415 EXPECT_TRUE( u64.ule(uint64max));
416 EXPECT_TRUE(!u64.ult(uint64max));
417 EXPECT_TRUE( u64.sge(int64max));
418 EXPECT_TRUE( u64.sgt(int64max));
419 EXPECT_TRUE(!u64.sle(int64max));
420 EXPECT_TRUE(!u64.slt(int64max));
421 EXPECT_TRUE( u64.sge(int64min));
422 EXPECT_TRUE( u64.sgt(int64min));
423 EXPECT_TRUE(!u64.sle(int64min));
424 EXPECT_TRUE(!u64.slt(int64min));
426 EXPECT_TRUE(u64 == uint64max);
427 EXPECT_TRUE(u64 != int64max);
428 EXPECT_TRUE(u64 != int64min);
430 EXPECT_TRUE(!s64.uge(uint64max));
431 EXPECT_TRUE(!s64.ugt(uint64max));
432 EXPECT_TRUE( s64.ule(uint64max));
433 EXPECT_TRUE( s64.ult(uint64max));
434 EXPECT_TRUE( s64.sge(int64max));
435 EXPECT_TRUE(!s64.sgt(int64max));
436 EXPECT_TRUE( s64.sle(int64max));
437 EXPECT_TRUE(!s64.slt(int64max));
438 EXPECT_TRUE( s64.sge(int64min));
439 EXPECT_TRUE( s64.sgt(int64min));
440 EXPECT_TRUE(!s64.sle(int64min));
441 EXPECT_TRUE(!s64.slt(int64min));
443 EXPECT_TRUE(s64 != uint64max);
444 EXPECT_TRUE(s64 == int64max);
445 EXPECT_TRUE(s64 != int64min);
447 EXPECT_TRUE( big.uge(uint64max));
448 EXPECT_TRUE( big.ugt(uint64max));
449 EXPECT_TRUE(!big.ule(uint64max));
450 EXPECT_TRUE(!big.ult(uint64max));
451 EXPECT_TRUE( big.sge(int64max));
452 EXPECT_TRUE( big.sgt(int64max));
453 EXPECT_TRUE(!big.sle(int64max));
454 EXPECT_TRUE(!big.slt(int64max));
455 EXPECT_TRUE( big.sge(int64min));
456 EXPECT_TRUE( big.sgt(int64min));
457 EXPECT_TRUE(!big.sle(int64min));
458 EXPECT_TRUE(!big.slt(int64min));
460 EXPECT_TRUE(big != uint64max);
461 EXPECT_TRUE(big != int64max);
462 EXPECT_TRUE(big != int64min);
465 TEST(APIntTest, compareWithInt64Min) {
466 int64_t edge = INT64_MIN;
467 int64_t edgeP1 = edge + 1;
468 int64_t edgeM1 = INT64_MAX;
469 auto a = APInt{64, static_cast<uint64_t>(edge), true};
471 EXPECT_TRUE(!a.slt(edge));
472 EXPECT_TRUE( a.sle(edge));
473 EXPECT_TRUE(!a.sgt(edge));
474 EXPECT_TRUE( a.sge(edge));
475 EXPECT_TRUE( a.slt(edgeP1));
476 EXPECT_TRUE( a.sle(edgeP1));
477 EXPECT_TRUE(!a.sgt(edgeP1));
478 EXPECT_TRUE(!a.sge(edgeP1));
479 EXPECT_TRUE( a.slt(edgeM1));
480 EXPECT_TRUE( a.sle(edgeM1));
481 EXPECT_TRUE(!a.sgt(edgeM1));
482 EXPECT_TRUE(!a.sge(edgeM1));
485 TEST(APIntTest, compareWithHalfInt64Max) {
486 uint64_t edge = 0x4000000000000000;
487 uint64_t edgeP1 = edge + 1;
488 uint64_t edgeM1 = edge - 1;
489 auto a = APInt{64, edge};
491 EXPECT_TRUE(!a.ult(edge));
492 EXPECT_TRUE( a.ule(edge));
493 EXPECT_TRUE(!a.ugt(edge));
494 EXPECT_TRUE( a.uge(edge));
495 EXPECT_TRUE( a.ult(edgeP1));
496 EXPECT_TRUE( a.ule(edgeP1));
497 EXPECT_TRUE(!a.ugt(edgeP1));
498 EXPECT_TRUE(!a.uge(edgeP1));
499 EXPECT_TRUE(!a.ult(edgeM1));
500 EXPECT_TRUE(!a.ule(edgeM1));
501 EXPECT_TRUE( a.ugt(edgeM1));
502 EXPECT_TRUE( a.uge(edgeM1));
504 EXPECT_TRUE(!a.slt(edge));
505 EXPECT_TRUE( a.sle(edge));
506 EXPECT_TRUE(!a.sgt(edge));
507 EXPECT_TRUE( a.sge(edge));
508 EXPECT_TRUE( a.slt(edgeP1));
509 EXPECT_TRUE( a.sle(edgeP1));
510 EXPECT_TRUE(!a.sgt(edgeP1));
511 EXPECT_TRUE(!a.sge(edgeP1));
512 EXPECT_TRUE(!a.slt(edgeM1));
513 EXPECT_TRUE(!a.sle(edgeM1));
514 EXPECT_TRUE( a.sgt(edgeM1));
515 EXPECT_TRUE( a.sge(edgeM1));
518 TEST(APIntTest, compareLargeIntegers) {
519 // Make sure all the combinations of signed comparisons work with big ints.
520 auto One = APInt{128, static_cast<uint64_t>(1), true};
521 auto Two = APInt{128, static_cast<uint64_t>(2), true};
522 auto MinusOne = APInt{128, static_cast<uint64_t>(-1), true};
523 auto MinusTwo = APInt{128, static_cast<uint64_t>(-2), true};
525 EXPECT_TRUE(!One.slt(One));
526 EXPECT_TRUE(!Two.slt(One));
527 EXPECT_TRUE(MinusOne.slt(One));
528 EXPECT_TRUE(MinusTwo.slt(One));
530 EXPECT_TRUE(One.slt(Two));
531 EXPECT_TRUE(!Two.slt(Two));
532 EXPECT_TRUE(MinusOne.slt(Two));
533 EXPECT_TRUE(MinusTwo.slt(Two));
535 EXPECT_TRUE(!One.slt(MinusOne));
536 EXPECT_TRUE(!Two.slt(MinusOne));
537 EXPECT_TRUE(!MinusOne.slt(MinusOne));
538 EXPECT_TRUE(MinusTwo.slt(MinusOne));
540 EXPECT_TRUE(!One.slt(MinusTwo));
541 EXPECT_TRUE(!Two.slt(MinusTwo));
542 EXPECT_TRUE(!MinusOne.slt(MinusTwo));
543 EXPECT_TRUE(!MinusTwo.slt(MinusTwo));
546 TEST(APIntTest, binaryOpsWithRawIntegers) {
547 // Single word check.
548 uint64_t E1 = 0x2CA7F46BF6569915ULL;
549 APInt A1(64, E1);
551 EXPECT_EQ(A1 & E1, E1);
552 EXPECT_EQ(A1 & 0, 0);
553 EXPECT_EQ(A1 & 1, 1);
554 EXPECT_EQ(A1 & 5, 5);
555 EXPECT_EQ(A1 & UINT64_MAX, E1);
557 EXPECT_EQ(A1 | E1, E1);
558 EXPECT_EQ(A1 | 0, E1);
559 EXPECT_EQ(A1 | 1, E1);
560 EXPECT_EQ(A1 | 2, E1 | 2);
561 EXPECT_EQ(A1 | UINT64_MAX, UINT64_MAX);
563 EXPECT_EQ(A1 ^ E1, 0);
564 EXPECT_EQ(A1 ^ 0, E1);
565 EXPECT_EQ(A1 ^ 1, E1 ^ 1);
566 EXPECT_EQ(A1 ^ 7, E1 ^ 7);
567 EXPECT_EQ(A1 ^ UINT64_MAX, ~E1);
569 // Multiword check.
570 uint64_t N = 0xEB6EB136591CBA21ULL;
571 APInt::WordType E2[4] = {
573 0x7B9358BD6A33F10AULL,
574 0x7E7FFA5EADD8846ULL,
575 0x305F341CA00B613DULL
577 APInt A2(APInt::APINT_BITS_PER_WORD*4, E2);
579 EXPECT_EQ(A2 & N, N);
580 EXPECT_EQ(A2 & 0, 0);
581 EXPECT_EQ(A2 & 1, 1);
582 EXPECT_EQ(A2 & 5, 1);
583 EXPECT_EQ(A2 & UINT64_MAX, N);
585 EXPECT_EQ(A2 | N, A2);
586 EXPECT_EQ(A2 | 0, A2);
587 EXPECT_EQ(A2 | 1, A2);
588 EXPECT_EQ(A2 | 2, A2 + 2);
589 EXPECT_EQ(A2 | UINT64_MAX, A2 - N + UINT64_MAX);
591 EXPECT_EQ(A2 ^ N, A2 - N);
592 EXPECT_EQ(A2 ^ 0, A2);
593 EXPECT_EQ(A2 ^ 1, A2 - 1);
594 EXPECT_EQ(A2 ^ 7, A2 + 5);
595 EXPECT_EQ(A2 ^ UINT64_MAX, A2 - N + ~N);
598 TEST(APIntTest, rvalue_arithmetic) {
599 // Test all combinations of lvalue/rvalue lhs/rhs of add/sub
601 // Lamdba to return an APInt by value, but also provide the raw value of the
602 // allocated data.
603 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
604 APInt V(129, HexString, 16);
605 RawData = V.getRawData();
606 return V;
609 APInt One(129, "1", 16);
610 APInt Two(129, "2", 16);
611 APInt Three(129, "3", 16);
612 APInt MinusOne = -One;
614 const uint64_t *RawDataL = nullptr;
615 const uint64_t *RawDataR = nullptr;
618 // 1 + 1 = 2
619 APInt AddLL = One + One;
620 EXPECT_EQ(AddLL, Two);
622 APInt AddLR = One + getRValue("1", RawDataR);
623 EXPECT_EQ(AddLR, Two);
624 EXPECT_EQ(AddLR.getRawData(), RawDataR);
626 APInt AddRL = getRValue("1", RawDataL) + One;
627 EXPECT_EQ(AddRL, Two);
628 EXPECT_EQ(AddRL.getRawData(), RawDataL);
630 APInt AddRR = getRValue("1", RawDataL) + getRValue("1", RawDataR);
631 EXPECT_EQ(AddRR, Two);
632 EXPECT_EQ(AddRR.getRawData(), RawDataR);
634 // LValue's and constants
635 APInt AddLK = One + 1;
636 EXPECT_EQ(AddLK, Two);
638 APInt AddKL = 1 + One;
639 EXPECT_EQ(AddKL, Two);
641 // RValue's and constants
642 APInt AddRK = getRValue("1", RawDataL) + 1;
643 EXPECT_EQ(AddRK, Two);
644 EXPECT_EQ(AddRK.getRawData(), RawDataL);
646 APInt AddKR = 1 + getRValue("1", RawDataR);
647 EXPECT_EQ(AddKR, Two);
648 EXPECT_EQ(AddKR.getRawData(), RawDataR);
652 // 0x0,FFFF...FFFF + 0x2 = 0x100...0001
653 APInt AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
654 APInt HighOneLowOne(129, "100000000000000000000000000000001", 16);
656 APInt AddLL = AllOnes + Two;
657 EXPECT_EQ(AddLL, HighOneLowOne);
659 APInt AddLR = AllOnes + getRValue("2", RawDataR);
660 EXPECT_EQ(AddLR, HighOneLowOne);
661 EXPECT_EQ(AddLR.getRawData(), RawDataR);
663 APInt AddRL = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) + Two;
664 EXPECT_EQ(AddRL, HighOneLowOne);
665 EXPECT_EQ(AddRL.getRawData(), RawDataL);
667 APInt AddRR = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) +
668 getRValue("2", RawDataR);
669 EXPECT_EQ(AddRR, HighOneLowOne);
670 EXPECT_EQ(AddRR.getRawData(), RawDataR);
672 // LValue's and constants
673 APInt AddLK = AllOnes + 2;
674 EXPECT_EQ(AddLK, HighOneLowOne);
676 APInt AddKL = 2 + AllOnes;
677 EXPECT_EQ(AddKL, HighOneLowOne);
679 // RValue's and constants
680 APInt AddRK = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) + 2;
681 EXPECT_EQ(AddRK, HighOneLowOne);
682 EXPECT_EQ(AddRK.getRawData(), RawDataL);
684 APInt AddKR = 2 + getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
685 EXPECT_EQ(AddKR, HighOneLowOne);
686 EXPECT_EQ(AddKR.getRawData(), RawDataR);
690 // 2 - 1 = 1
691 APInt SubLL = Two - One;
692 EXPECT_EQ(SubLL, One);
694 APInt SubLR = Two - getRValue("1", RawDataR);
695 EXPECT_EQ(SubLR, One);
696 EXPECT_EQ(SubLR.getRawData(), RawDataR);
698 APInt SubRL = getRValue("2", RawDataL) - One;
699 EXPECT_EQ(SubRL, One);
700 EXPECT_EQ(SubRL.getRawData(), RawDataL);
702 APInt SubRR = getRValue("2", RawDataL) - getRValue("1", RawDataR);
703 EXPECT_EQ(SubRR, One);
704 EXPECT_EQ(SubRR.getRawData(), RawDataR);
706 // LValue's and constants
707 APInt SubLK = Two - 1;
708 EXPECT_EQ(SubLK, One);
710 APInt SubKL = 2 - One;
711 EXPECT_EQ(SubKL, One);
713 // RValue's and constants
714 APInt SubRK = getRValue("2", RawDataL) - 1;
715 EXPECT_EQ(SubRK, One);
716 EXPECT_EQ(SubRK.getRawData(), RawDataL);
718 APInt SubKR = 2 - getRValue("1", RawDataR);
719 EXPECT_EQ(SubKR, One);
720 EXPECT_EQ(SubKR.getRawData(), RawDataR);
724 // 0x100...0001 - 0x0,FFFF...FFFF = 0x2
725 APInt AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
726 APInt HighOneLowOne(129, "100000000000000000000000000000001", 16);
728 APInt SubLL = HighOneLowOne - AllOnes;
729 EXPECT_EQ(SubLL, Two);
731 APInt SubLR = HighOneLowOne -
732 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
733 EXPECT_EQ(SubLR, Two);
734 EXPECT_EQ(SubLR.getRawData(), RawDataR);
736 APInt SubRL = getRValue("100000000000000000000000000000001", RawDataL) -
737 AllOnes;
738 EXPECT_EQ(SubRL, Two);
739 EXPECT_EQ(SubRL.getRawData(), RawDataL);
741 APInt SubRR = getRValue("100000000000000000000000000000001", RawDataL) -
742 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
743 EXPECT_EQ(SubRR, Two);
744 EXPECT_EQ(SubRR.getRawData(), RawDataR);
746 // LValue's and constants
747 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
748 APInt SubLK = HighOneLowOne - 2;
749 EXPECT_EQ(SubLK, AllOnes);
751 // 2 - (-1) = 3
752 APInt SubKL = 2 - MinusOne;
753 EXPECT_EQ(SubKL, Three);
755 // RValue's and constants
756 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
757 APInt SubRK = getRValue("100000000000000000000000000000001", RawDataL) - 2;
758 EXPECT_EQ(SubRK, AllOnes);
759 EXPECT_EQ(SubRK.getRawData(), RawDataL);
761 APInt SubKR = 2 - getRValue("1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
762 EXPECT_EQ(SubKR, Three);
763 EXPECT_EQ(SubKR.getRawData(), RawDataR);
767 TEST(APIntTest, rvalue_bitwise) {
768 // Test all combinations of lvalue/rvalue lhs/rhs of and/or/xor
770 // Lamdba to return an APInt by value, but also provide the raw value of the
771 // allocated data.
772 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
773 APInt V(129, HexString, 16);
774 RawData = V.getRawData();
775 return V;
778 APInt Ten(129, "A", 16);
779 APInt Twelve(129, "C", 16);
781 const uint64_t *RawDataL = nullptr;
782 const uint64_t *RawDataR = nullptr;
785 // 12 & 10 = 8
786 APInt AndLL = Ten & Twelve;
787 EXPECT_EQ(AndLL, 0x8);
789 APInt AndLR = Ten & getRValue("C", RawDataR);
790 EXPECT_EQ(AndLR, 0x8);
791 EXPECT_EQ(AndLR.getRawData(), RawDataR);
793 APInt AndRL = getRValue("A", RawDataL) & Twelve;
794 EXPECT_EQ(AndRL, 0x8);
795 EXPECT_EQ(AndRL.getRawData(), RawDataL);
797 APInt AndRR = getRValue("A", RawDataL) & getRValue("C", RawDataR);
798 EXPECT_EQ(AndRR, 0x8);
799 EXPECT_EQ(AndRR.getRawData(), RawDataR);
801 // LValue's and constants
802 APInt AndLK = Ten & 0xc;
803 EXPECT_EQ(AndLK, 0x8);
805 APInt AndKL = 0xa & Twelve;
806 EXPECT_EQ(AndKL, 0x8);
808 // RValue's and constants
809 APInt AndRK = getRValue("A", RawDataL) & 0xc;
810 EXPECT_EQ(AndRK, 0x8);
811 EXPECT_EQ(AndRK.getRawData(), RawDataL);
813 APInt AndKR = 0xa & getRValue("C", RawDataR);
814 EXPECT_EQ(AndKR, 0x8);
815 EXPECT_EQ(AndKR.getRawData(), RawDataR);
819 // 12 | 10 = 14
820 APInt OrLL = Ten | Twelve;
821 EXPECT_EQ(OrLL, 0xe);
823 APInt OrLR = Ten | getRValue("C", RawDataR);
824 EXPECT_EQ(OrLR, 0xe);
825 EXPECT_EQ(OrLR.getRawData(), RawDataR);
827 APInt OrRL = getRValue("A", RawDataL) | Twelve;
828 EXPECT_EQ(OrRL, 0xe);
829 EXPECT_EQ(OrRL.getRawData(), RawDataL);
831 APInt OrRR = getRValue("A", RawDataL) | getRValue("C", RawDataR);
832 EXPECT_EQ(OrRR, 0xe);
833 EXPECT_EQ(OrRR.getRawData(), RawDataR);
835 // LValue's and constants
836 APInt OrLK = Ten | 0xc;
837 EXPECT_EQ(OrLK, 0xe);
839 APInt OrKL = 0xa | Twelve;
840 EXPECT_EQ(OrKL, 0xe);
842 // RValue's and constants
843 APInt OrRK = getRValue("A", RawDataL) | 0xc;
844 EXPECT_EQ(OrRK, 0xe);
845 EXPECT_EQ(OrRK.getRawData(), RawDataL);
847 APInt OrKR = 0xa | getRValue("C", RawDataR);
848 EXPECT_EQ(OrKR, 0xe);
849 EXPECT_EQ(OrKR.getRawData(), RawDataR);
853 // 12 ^ 10 = 6
854 APInt XorLL = Ten ^ Twelve;
855 EXPECT_EQ(XorLL, 0x6);
857 APInt XorLR = Ten ^ getRValue("C", RawDataR);
858 EXPECT_EQ(XorLR, 0x6);
859 EXPECT_EQ(XorLR.getRawData(), RawDataR);
861 APInt XorRL = getRValue("A", RawDataL) ^ Twelve;
862 EXPECT_EQ(XorRL, 0x6);
863 EXPECT_EQ(XorRL.getRawData(), RawDataL);
865 APInt XorRR = getRValue("A", RawDataL) ^ getRValue("C", RawDataR);
866 EXPECT_EQ(XorRR, 0x6);
867 EXPECT_EQ(XorRR.getRawData(), RawDataR);
869 // LValue's and constants
870 APInt XorLK = Ten ^ 0xc;
871 EXPECT_EQ(XorLK, 0x6);
873 APInt XorKL = 0xa ^ Twelve;
874 EXPECT_EQ(XorKL, 0x6);
876 // RValue's and constants
877 APInt XorRK = getRValue("A", RawDataL) ^ 0xc;
878 EXPECT_EQ(XorRK, 0x6);
879 EXPECT_EQ(XorRK.getRawData(), RawDataL);
881 APInt XorKR = 0xa ^ getRValue("C", RawDataR);
882 EXPECT_EQ(XorKR, 0x6);
883 EXPECT_EQ(XorKR.getRawData(), RawDataR);
887 TEST(APIntTest, rvalue_invert) {
888 // Lamdba to return an APInt by value, but also provide the raw value of the
889 // allocated data.
890 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
891 APInt V(129, HexString, 16);
892 RawData = V.getRawData();
893 return V;
896 APInt One(129, 1);
897 APInt NegativeTwo(129, -2ULL, true);
899 const uint64_t *RawData = nullptr;
902 // ~1 = -2
903 APInt NegL = ~One;
904 EXPECT_EQ(NegL, NegativeTwo);
906 APInt NegR = ~getRValue("1", RawData);
907 EXPECT_EQ(NegR, NegativeTwo);
908 EXPECT_EQ(NegR.getRawData(), RawData);
912 // Tests different div/rem varaints using scheme (a * b + c) / a
913 void testDiv(APInt a, APInt b, APInt c) {
914 ASSERT_TRUE(a.uge(b)); // Must: a >= b
915 ASSERT_TRUE(a.ugt(c)); // Must: a > c
917 auto p = a * b + c;
919 auto q = p.udiv(a);
920 auto r = p.urem(a);
921 EXPECT_EQ(b, q);
922 EXPECT_EQ(c, r);
923 APInt::udivrem(p, a, q, r);
924 EXPECT_EQ(b, q);
925 EXPECT_EQ(c, r);
926 q = p.sdiv(a);
927 r = p.srem(a);
928 EXPECT_EQ(b, q);
929 EXPECT_EQ(c, r);
930 APInt::sdivrem(p, a, q, r);
931 EXPECT_EQ(b, q);
932 EXPECT_EQ(c, r);
934 if (b.ugt(c)) { // Test also symmetric case
935 q = p.udiv(b);
936 r = p.urem(b);
937 EXPECT_EQ(a, q);
938 EXPECT_EQ(c, r);
939 APInt::udivrem(p, b, q, r);
940 EXPECT_EQ(a, q);
941 EXPECT_EQ(c, r);
942 q = p.sdiv(b);
943 r = p.srem(b);
944 EXPECT_EQ(a, q);
945 EXPECT_EQ(c, r);
946 APInt::sdivrem(p, b, q, r);
947 EXPECT_EQ(a, q);
948 EXPECT_EQ(c, r);
952 TEST(APIntTest, divrem_big1) {
953 // Tests KnuthDiv rare step D6
954 testDiv({256, "1ffffffffffffffff", 16},
955 {256, "1ffffffffffffffff", 16},
956 {256, 0});
959 TEST(APIntTest, divrem_big2) {
960 // Tests KnuthDiv rare step D6
961 testDiv({1024, "112233ceff"
962 "cecece000000ffffffffffffffffffff"
963 "ffffffffffffffffffffffffffffffff"
964 "ffffffffffffffffffffffffffffffff"
965 "ffffffffffffffffffffffffffffff33", 16},
966 {1024, "111111ffffffffffffffff"
967 "ffffffffffffffffffffffffffffffff"
968 "fffffffffffffffffffffffffffffccf"
969 "ffffffffffffffffffffffffffffff00", 16},
970 {1024, 7919});
973 TEST(APIntTest, divrem_big3) {
974 // Tests KnuthDiv case without shift
975 testDiv({256, "80000001ffffffffffffffff", 16},
976 {256, "ffffffffffffff0000000", 16},
977 {256, 4219});
980 TEST(APIntTest, divrem_big4) {
981 // Tests heap allocation in divide() enfoced by huge numbers
982 testDiv(APInt{4096, 5}.shl(2001),
983 APInt{4096, 1}.shl(2000),
984 APInt{4096, 4219*13});
987 TEST(APIntTest, divrem_big5) {
988 // Tests one word divisor case of divide()
989 testDiv(APInt{1024, 19}.shl(811),
990 APInt{1024, 4356013}, // one word
991 APInt{1024, 1});
994 TEST(APIntTest, divrem_big6) {
995 // Tests some rare "borrow" cases in D4 step
996 testDiv(APInt{512, "ffffffffffffffff00000000000000000000000001", 16},
997 APInt{512, "10000000000000001000000000000001", 16},
998 APInt{512, "10000000000000000000000000000000", 16});
1001 TEST(APIntTest, divrem_big7) {
1002 // Yet another test for KnuthDiv rare step D6.
1003 testDiv({224, "800000008000000200000005", 16},
1004 {224, "fffffffd", 16},
1005 {224, "80000000800000010000000f", 16});
1008 void testDiv(APInt a, uint64_t b, APInt c) {
1009 auto p = a * b + c;
1011 APInt q;
1012 uint64_t r;
1013 // Unsigned division will only work if our original number wasn't negative.
1014 if (!a.isNegative()) {
1015 q = p.udiv(b);
1016 r = p.urem(b);
1017 EXPECT_EQ(a, q);
1018 EXPECT_EQ(c, r);
1019 APInt::udivrem(p, b, q, r);
1020 EXPECT_EQ(a, q);
1021 EXPECT_EQ(c, r);
1023 q = p.sdiv(b);
1024 r = p.srem(b);
1025 EXPECT_EQ(a, q);
1026 if (c.isNegative())
1027 EXPECT_EQ(-c, -r); // Need to negate so the uint64_t compare will work.
1028 else
1029 EXPECT_EQ(c, r);
1030 int64_t sr;
1031 APInt::sdivrem(p, b, q, sr);
1032 EXPECT_EQ(a, q);
1033 if (c.isNegative())
1034 EXPECT_EQ(-c, -sr); // Need to negate so the uint64_t compare will work.
1035 else
1036 EXPECT_EQ(c, sr);
1039 TEST(APIntTest, divremuint) {
1040 // Single word APInt
1041 testDiv(APInt{64, 9},
1043 APInt{64, 1});
1045 // Single word negative APInt
1046 testDiv(-APInt{64, 9},
1048 -APInt{64, 1});
1050 // Multiword dividend with only one significant word.
1051 testDiv(APInt{256, 9},
1053 APInt{256, 1});
1055 // Negative dividend.
1056 testDiv(-APInt{256, 9},
1058 -APInt{256, 1});
1060 // Multiword dividend
1061 testDiv(APInt{1024, 19}.shl(811),
1062 4356013, // one word
1063 APInt{1024, 1});
1066 TEST(APIntTest, divrem_simple) {
1067 // Test simple cases.
1068 APInt A(65, 2), B(65, 2);
1069 APInt Q, R;
1071 // X / X
1072 APInt::sdivrem(A, B, Q, R);
1073 EXPECT_EQ(Q, APInt(65, 1));
1074 EXPECT_EQ(R, APInt(65, 0));
1075 APInt::udivrem(A, B, Q, R);
1076 EXPECT_EQ(Q, APInt(65, 1));
1077 EXPECT_EQ(R, APInt(65, 0));
1079 // 0 / X
1080 APInt O(65, 0);
1081 APInt::sdivrem(O, B, Q, R);
1082 EXPECT_EQ(Q, APInt(65, 0));
1083 EXPECT_EQ(R, APInt(65, 0));
1084 APInt::udivrem(O, B, Q, R);
1085 EXPECT_EQ(Q, APInt(65, 0));
1086 EXPECT_EQ(R, APInt(65, 0));
1088 // X / 1
1089 APInt I(65, 1);
1090 APInt::sdivrem(A, I, Q, R);
1091 EXPECT_EQ(Q, A);
1092 EXPECT_EQ(R, APInt(65, 0));
1093 APInt::udivrem(A, I, Q, R);
1094 EXPECT_EQ(Q, A);
1095 EXPECT_EQ(R, APInt(65, 0));
1098 TEST(APIntTest, fromString) {
1099 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
1100 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
1101 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
1102 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
1103 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
1105 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
1106 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
1107 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
1108 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
1109 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
1111 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
1112 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
1113 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
1114 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
1115 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
1117 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
1118 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
1119 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
1120 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
1121 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
1122 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
1124 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
1125 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
1126 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
1127 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
1128 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
1129 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
1131 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
1132 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
1133 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
1134 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
1135 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
1136 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
1138 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
1139 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
1140 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
1141 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
1142 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
1143 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
1145 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
1146 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
1147 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
1148 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
1149 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
1150 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
1152 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
1153 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
1154 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
1155 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
1156 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
1157 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
1159 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
1160 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
1161 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
1162 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
1163 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
1164 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
1166 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
1167 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
1168 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
1169 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
1170 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
1171 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
1173 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
1174 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
1175 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
1176 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
1177 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
1178 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
1181 TEST(APIntTest, SaturatingMath) {
1182 APInt AP_10 = APInt(8, 10);
1183 APInt AP_42 = APInt(8, 42);
1184 APInt AP_100 = APInt(8, 100);
1185 APInt AP_200 = APInt(8, 200);
1187 EXPECT_EQ(APInt(8, 100), AP_100.truncUSat(8));
1188 EXPECT_EQ(APInt(7, 100), AP_100.truncUSat(7));
1189 EXPECT_EQ(APInt(6, 63), AP_100.truncUSat(6));
1190 EXPECT_EQ(APInt(5, 31), AP_100.truncUSat(5));
1192 EXPECT_EQ(APInt(8, 200), AP_200.truncUSat(8));
1193 EXPECT_EQ(APInt(7, 127), AP_200.truncUSat(7));
1194 EXPECT_EQ(APInt(6, 63), AP_200.truncUSat(6));
1195 EXPECT_EQ(APInt(5, 31), AP_200.truncUSat(5));
1197 EXPECT_EQ(APInt(8, 42), AP_42.truncSSat(8));
1198 EXPECT_EQ(APInt(7, 42), AP_42.truncSSat(7));
1199 EXPECT_EQ(APInt(6, 31), AP_42.truncSSat(6));
1200 EXPECT_EQ(APInt(5, 15), AP_42.truncSSat(5));
1202 EXPECT_EQ(APInt(8, -56), AP_200.truncSSat(8));
1203 EXPECT_EQ(APInt(7, -56), AP_200.truncSSat(7));
1204 EXPECT_EQ(APInt(6, -32), AP_200.truncSSat(6));
1205 EXPECT_EQ(APInt(5, -16), AP_200.truncSSat(5));
1207 EXPECT_EQ(APInt(8, 200), AP_100.uadd_sat(AP_100));
1208 EXPECT_EQ(APInt(8, 255), AP_100.uadd_sat(AP_200));
1209 EXPECT_EQ(APInt(8, 255), APInt(8, 255).uadd_sat(APInt(8, 255)));
1211 EXPECT_EQ(APInt(8, 110), AP_10.sadd_sat(AP_100));
1212 EXPECT_EQ(APInt(8, 127), AP_100.sadd_sat(AP_100));
1213 EXPECT_EQ(APInt(8, -128), (-AP_100).sadd_sat(-AP_100));
1214 EXPECT_EQ(APInt(8, -128), APInt(8, -128).sadd_sat(APInt(8, -128)));
1216 EXPECT_EQ(APInt(8, 90), AP_100.usub_sat(AP_10));
1217 EXPECT_EQ(APInt(8, 0), AP_100.usub_sat(AP_200));
1218 EXPECT_EQ(APInt(8, 0), APInt(8, 0).usub_sat(APInt(8, 255)));
1220 EXPECT_EQ(APInt(8, -90), AP_10.ssub_sat(AP_100));
1221 EXPECT_EQ(APInt(8, 127), AP_100.ssub_sat(-AP_100));
1222 EXPECT_EQ(APInt(8, -128), (-AP_100).ssub_sat(AP_100));
1223 EXPECT_EQ(APInt(8, -128), APInt(8, -128).ssub_sat(APInt(8, 127)));
1225 EXPECT_EQ(APInt(8, 250), APInt(8, 50).umul_sat(APInt(8, 5)));
1226 EXPECT_EQ(APInt(8, 255), APInt(8, 50).umul_sat(APInt(8, 6)));
1227 EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, 3)));
1228 EXPECT_EQ(APInt(8, 255), APInt(8, 3).umul_sat(APInt(8, -128)));
1229 EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, -128)));
1231 EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5)));
1232 EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6)));
1233 EXPECT_EQ(APInt(8, 127), APInt(8, 127).smul_sat(APInt(8, 127)));
1234 EXPECT_EQ(APInt(8, -125), APInt(8, -25).smul_sat(APInt(8, 5)));
1235 EXPECT_EQ(APInt(8, -125), APInt(8, 25).smul_sat(APInt(8, -5)));
1236 EXPECT_EQ(APInt(8, 125), APInt(8, -25).smul_sat(APInt(8, -5)));
1237 EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5)));
1238 EXPECT_EQ(APInt(8, -128), APInt(8, -25).smul_sat(APInt(8, 6)));
1239 EXPECT_EQ(APInt(8, -128), APInt(8, 25).smul_sat(APInt(8, -6)));
1240 EXPECT_EQ(APInt(8, 127), APInt(8, -25).smul_sat(APInt(8, -6)));
1241 EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6)));
1243 EXPECT_EQ(APInt(8, 128), APInt(8, 4).ushl_sat(APInt(8, 5)));
1244 EXPECT_EQ(APInt(8, 255), APInt(8, 4).ushl_sat(APInt(8, 6)));
1245 EXPECT_EQ(APInt(8, 128), APInt(8, 1).ushl_sat(APInt(8, 7)));
1246 EXPECT_EQ(APInt(8, 255), APInt(8, 1).ushl_sat(APInt(8, 8)));
1247 EXPECT_EQ(APInt(8, 255), APInt(8, -128).ushl_sat(APInt(8, 2)));
1248 EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, 2)));
1249 EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, -2)));
1251 EXPECT_EQ(APInt(8, 64), APInt(8, 4).sshl_sat(APInt(8, 4)));
1252 EXPECT_EQ(APInt(8, 127), APInt(8, 4).sshl_sat(APInt(8, 5)));
1253 EXPECT_EQ(APInt(8, 127), APInt(8, 1).sshl_sat(APInt(8, 8)));
1254 EXPECT_EQ(APInt(8, -64), APInt(8, -4).sshl_sat(APInt(8, 4)));
1255 EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 5)));
1256 EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 6)));
1257 EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 7)));
1258 EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 8)));
1261 TEST(APIntTest, FromArray) {
1262 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
1265 TEST(APIntTest, StringBitsNeeded2) {
1266 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
1267 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
1268 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
1269 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
1270 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
1272 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
1273 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
1274 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
1275 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
1276 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
1278 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
1279 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
1280 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
1281 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
1282 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
1285 TEST(APIntTest, StringBitsNeeded8) {
1286 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
1287 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
1288 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
1289 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
1290 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
1292 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
1293 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
1294 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
1295 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
1296 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
1298 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
1299 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
1300 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
1301 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
1302 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
1305 TEST(APIntTest, StringBitsNeeded10) {
1306 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
1307 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
1308 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
1309 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
1310 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
1311 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
1313 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
1314 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
1315 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
1316 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
1317 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
1319 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
1320 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
1321 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
1322 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
1323 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
1325 EXPECT_EQ(1U, APInt::getBitsNeeded("-1", 10));
1326 EXPECT_EQ(2U, APInt::getBitsNeeded("-2", 10));
1327 EXPECT_EQ(3U, APInt::getBitsNeeded("-4", 10));
1328 EXPECT_EQ(4U, APInt::getBitsNeeded("-8", 10));
1329 EXPECT_EQ(5U, APInt::getBitsNeeded("-16", 10));
1330 EXPECT_EQ(6U, APInt::getBitsNeeded("-23", 10));
1331 EXPECT_EQ(6U, APInt::getBitsNeeded("-32", 10));
1332 EXPECT_EQ(7U, APInt::getBitsNeeded("-64", 10));
1333 EXPECT_EQ(8U, APInt::getBitsNeeded("-127", 10));
1334 EXPECT_EQ(8U, APInt::getBitsNeeded("-128", 10));
1335 EXPECT_EQ(9U, APInt::getBitsNeeded("-255", 10));
1336 EXPECT_EQ(9U, APInt::getBitsNeeded("-256", 10));
1337 EXPECT_EQ(10U, APInt::getBitsNeeded("-512", 10));
1338 EXPECT_EQ(11U, APInt::getBitsNeeded("-1024", 10));
1339 EXPECT_EQ(12U, APInt::getBitsNeeded("-1025", 10));
1342 TEST(APIntTest, StringBitsNeeded16) {
1343 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
1344 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
1345 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
1346 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
1347 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
1349 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
1350 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
1351 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
1352 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
1353 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
1355 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
1356 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
1357 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
1358 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
1359 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
1362 TEST(APIntTest, toString) {
1363 SmallString<16> S;
1364 bool isSigned;
1366 APInt(8, 0).toString(S, 2, true, true);
1367 EXPECT_EQ(std::string(S), "0b0");
1368 S.clear();
1369 APInt(8, 0).toString(S, 8, true, true);
1370 EXPECT_EQ(std::string(S), "00");
1371 S.clear();
1372 APInt(8, 0).toString(S, 10, true, true);
1373 EXPECT_EQ(std::string(S), "0");
1374 S.clear();
1375 APInt(8, 0).toString(S, 16, true, true);
1376 EXPECT_EQ(std::string(S), "0x0");
1377 S.clear();
1378 APInt(8, 0).toString(S, 36, true, false);
1379 EXPECT_EQ(std::string(S), "0");
1380 S.clear();
1382 isSigned = false;
1383 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
1384 EXPECT_EQ(std::string(S), "0b11111111");
1385 S.clear();
1386 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
1387 EXPECT_EQ(std::string(S), "0377");
1388 S.clear();
1389 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
1390 EXPECT_EQ(std::string(S), "255");
1391 S.clear();
1392 APInt(8, 255, isSigned).toString(S, 16, isSigned, true, /*UpperCase=*/false);
1393 EXPECT_EQ(std::string(S), "0xff");
1394 S.clear();
1395 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
1396 EXPECT_EQ(std::string(S), "0xFF");
1397 S.clear();
1398 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
1399 EXPECT_EQ(std::string(S), "73");
1400 S.clear();
1402 isSigned = true;
1403 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
1404 EXPECT_EQ(std::string(S), "-0b1");
1405 S.clear();
1406 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
1407 EXPECT_EQ(std::string(S), "-01");
1408 S.clear();
1409 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
1410 EXPECT_EQ(std::string(S), "-1");
1411 S.clear();
1412 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
1413 EXPECT_EQ(std::string(S), "-0x1");
1414 S.clear();
1415 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
1416 EXPECT_EQ(std::string(S), "-1");
1417 S.clear();
1420 TEST(APIntTest, Log2) {
1421 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
1422 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
1423 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
1424 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
1425 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
1426 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
1427 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
1428 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
1429 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
1432 #ifdef GTEST_HAS_DEATH_TEST
1433 #ifndef NDEBUG
1434 TEST(APIntTest, StringDeath) {
1435 EXPECT_DEATH((void)APInt(32, "", 0), "Invalid string length");
1436 EXPECT_DEATH((void)APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
1437 EXPECT_DEATH((void)APInt(32, "", 10), "Invalid string length");
1438 EXPECT_DEATH((void)APInt(32, "-", 10), "String is only a sign, needs a value.");
1439 EXPECT_DEATH((void)APInt(1, "1234", 10), "Insufficient bit width");
1440 EXPECT_DEATH((void)APInt(32, "\0", 10), "Invalid string length");
1441 EXPECT_DEATH((void)APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
1442 EXPECT_DEATH((void)APInt(32, "1L", 10), "Invalid character in digit string");
1444 #endif
1445 #endif
1447 TEST(APIntTest, mul_clear) {
1448 APInt ValA(65, -1ULL);
1449 APInt ValB(65, 4);
1450 APInt ValC(65, 0);
1451 ValC = ValA * ValB;
1452 ValA *= ValB;
1453 SmallString<16> StrA, StrC;
1454 ValA.toString(StrA, 10, false);
1455 ValC.toString(StrC, 10, false);
1456 EXPECT_EQ(std::string(StrA), std::string(StrC));
1459 TEST(APIntTest, Rotate) {
1460 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
1461 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
1462 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
1463 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
1464 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
1466 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
1467 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
1468 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
1469 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
1470 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
1472 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1473 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1475 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1476 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1477 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(33, 33)));
1478 EXPECT_EQ(APInt(32, (1 << 8)), APInt(32, 1).rotl(APInt(32, 40)));
1479 EXPECT_EQ(APInt(32, (1 << 30)), APInt(32, 1).rotl(APInt(31, 30)));
1480 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotl(APInt(31, 31)));
1482 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(1, 0)));
1483 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(1, 1)));
1485 EXPECT_EQ(APInt(32, 16), APInt(32, 1).rotl(APInt(3, 4)));
1487 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(64, 64)));
1488 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(64, 65)));
1490 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 3)));
1491 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 10)));
1492 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(5, 10)));
1493 EXPECT_EQ(APInt(7, 6), APInt(7, 3).rotl(APInt(12, 120)));
1495 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
1496 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
1497 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
1498 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
1499 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
1501 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
1502 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
1503 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
1504 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
1505 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
1507 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1508 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1510 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1511 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1512 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(33, 33)));
1513 EXPECT_EQ(APInt(32, (1 << 24)), APInt(32, 1).rotr(APInt(32, 40)));
1515 EXPECT_EQ(APInt(32, (1 << 2)), APInt(32, 1).rotr(APInt(31, 30)));
1516 EXPECT_EQ(APInt(32, (1 << 1)), APInt(32, 1).rotr(APInt(31, 31)));
1518 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(1, 0)));
1519 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(1, 1)));
1521 EXPECT_EQ(APInt(32, (1 << 28)), APInt(32, 1).rotr(APInt(3, 4)));
1523 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(64, 64)));
1524 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(64, 65)));
1526 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 3)));
1527 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 10)));
1528 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(5, 10)));
1529 EXPECT_EQ(APInt(7, 65), APInt(7, 3).rotr(APInt(12, 120)));
1531 APInt Big(256, "00004000800000000000000000003fff8000000000000003", 16);
1532 APInt Rot(256, "3fff80000000000000030000000000000000000040008000", 16);
1533 EXPECT_EQ(Rot, Big.rotr(144));
1535 EXPECT_EQ(APInt(32, 8), APInt(32, 1).rotl(Big));
1536 EXPECT_EQ(APInt(32, (1 << 29)), APInt(32, 1).rotr(Big));
1539 TEST(APIntTest, Splat) {
1540 APInt ValA(8, 0x01);
1541 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
1542 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
1544 APInt ValB(3, 5);
1545 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
1546 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
1549 TEST(APIntTest, tcDecrement) {
1550 // Test single word decrement.
1552 // No out borrow.
1554 APInt::WordType singleWord = ~APInt::WordType(0) << (APInt::APINT_BITS_PER_WORD - 1);
1555 APInt::WordType carry = APInt::tcDecrement(&singleWord, 1);
1556 EXPECT_EQ(carry, APInt::WordType(0));
1557 EXPECT_EQ(singleWord, ~APInt::WordType(0) >> 1);
1560 // With out borrow.
1562 APInt::WordType singleWord = 0;
1563 APInt::WordType carry = APInt::tcDecrement(&singleWord, 1);
1564 EXPECT_EQ(carry, APInt::WordType(1));
1565 EXPECT_EQ(singleWord, ~APInt::WordType(0));
1568 // Test multiword decrement.
1570 // No across word borrow, no out borrow.
1572 APInt::WordType test[4] = {0x1, 0x1, 0x1, 0x1};
1573 APInt::WordType expected[4] = {0x0, 0x1, 0x1, 0x1};
1574 APInt::tcDecrement(test, 4);
1575 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1578 // 1 across word borrow, no out borrow.
1580 APInt::WordType test[4] = {0x0, 0xF, 0x1, 0x1};
1581 APInt::WordType expected[4] = {~APInt::WordType(0), 0xE, 0x1, 0x1};
1582 APInt::WordType carry = APInt::tcDecrement(test, 4);
1583 EXPECT_EQ(carry, APInt::WordType(0));
1584 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1587 // 2 across word borrow, no out borrow.
1589 APInt::WordType test[4] = {0x0, 0x0, 0xC, 0x1};
1590 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), 0xB, 0x1};
1591 APInt::WordType carry = APInt::tcDecrement(test, 4);
1592 EXPECT_EQ(carry, APInt::WordType(0));
1593 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1596 // 3 across word borrow, no out borrow.
1598 APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x1};
1599 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), 0x0};
1600 APInt::WordType carry = APInt::tcDecrement(test, 4);
1601 EXPECT_EQ(carry, APInt::WordType(0));
1602 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1605 // 3 across word borrow, with out borrow.
1607 APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x0};
1608 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0)};
1609 APInt::WordType carry = APInt::tcDecrement(test, 4);
1610 EXPECT_EQ(carry, APInt::WordType(1));
1611 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1615 TEST(APIntTest, arrayAccess) {
1616 // Single word check.
1617 uint64_t E1 = 0x2CA7F46BF6569915ULL;
1618 APInt A1(64, E1);
1619 for (unsigned i = 0, e = 64; i < e; ++i) {
1620 EXPECT_EQ(bool(E1 & (1ULL << i)),
1621 A1[i]);
1624 // Multiword check.
1625 APInt::WordType E2[4] = {
1626 0xEB6EB136591CBA21ULL,
1627 0x7B9358BD6A33F10AULL,
1628 0x7E7FFA5EADD8846ULL,
1629 0x305F341CA00B613DULL
1631 APInt A2(APInt::APINT_BITS_PER_WORD*4, E2);
1632 for (unsigned i = 0; i < 4; ++i) {
1633 for (unsigned j = 0; j < APInt::APINT_BITS_PER_WORD; ++j) {
1634 EXPECT_EQ(bool(E2[i] & (1ULL << j)),
1635 A2[i*APInt::APINT_BITS_PER_WORD + j]);
1640 TEST(APIntTest, LargeAPIntConstruction) {
1641 // Check that we can properly construct very large APInt. It is very
1642 // unlikely that people will ever do this, but it is a legal input,
1643 // so we should not crash on it.
1644 APInt A9(UINT32_MAX, 0);
1645 EXPECT_FALSE(A9.getBoolValue());
1648 TEST(APIntTest, nearestLogBase2) {
1649 // Single word check.
1651 // Test round up.
1652 uint64_t I1 = 0x1800001;
1653 APInt A1(64, I1);
1654 EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
1656 // Test round down.
1657 uint64_t I2 = 0x1000011;
1658 APInt A2(64, I2);
1659 EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
1661 // Test ties round up.
1662 uint64_t I3 = 0x1800000;
1663 APInt A3(64, I3);
1664 EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
1666 // Multiple word check.
1668 // Test round up.
1669 APInt::WordType I4[4] = {0x0, 0xF, 0x18, 0x0};
1670 APInt A4(APInt::APINT_BITS_PER_WORD*4, I4);
1671 EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
1673 // Test round down.
1674 APInt::WordType I5[4] = {0x0, 0xF, 0x10, 0x0};
1675 APInt A5(APInt::APINT_BITS_PER_WORD*4, I5);
1676 EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
1678 // Test ties round up.
1679 uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
1680 APInt A6(APInt::APINT_BITS_PER_WORD*4, I6);
1681 EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
1683 // Test BitWidth == 1 special cases.
1684 APInt A7(1, 1);
1685 EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
1686 APInt A8(1, 0);
1687 EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
1689 // Test the zero case when we have a bit width large enough such
1690 // that the bit width is larger than UINT32_MAX-1.
1691 APInt A9(UINT32_MAX, 0);
1692 EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
1695 TEST(APIntTest, IsSplat) {
1696 APInt A(32, 0x01010101);
1697 EXPECT_FALSE(A.isSplat(1));
1698 EXPECT_FALSE(A.isSplat(2));
1699 EXPECT_FALSE(A.isSplat(4));
1700 EXPECT_TRUE(A.isSplat(8));
1701 EXPECT_TRUE(A.isSplat(16));
1702 EXPECT_TRUE(A.isSplat(32));
1704 APInt B(24, 0xAAAAAA);
1705 EXPECT_FALSE(B.isSplat(1));
1706 EXPECT_TRUE(B.isSplat(2));
1707 EXPECT_TRUE(B.isSplat(4));
1708 EXPECT_TRUE(B.isSplat(8));
1709 EXPECT_TRUE(B.isSplat(24));
1711 APInt C(24, 0xABAAAB);
1712 EXPECT_FALSE(C.isSplat(1));
1713 EXPECT_FALSE(C.isSplat(2));
1714 EXPECT_FALSE(C.isSplat(4));
1715 EXPECT_FALSE(C.isSplat(8));
1716 EXPECT_TRUE(C.isSplat(24));
1718 APInt D(32, 0xABBAABBA);
1719 EXPECT_FALSE(D.isSplat(1));
1720 EXPECT_FALSE(D.isSplat(2));
1721 EXPECT_FALSE(D.isSplat(4));
1722 EXPECT_FALSE(D.isSplat(8));
1723 EXPECT_TRUE(D.isSplat(16));
1724 EXPECT_TRUE(D.isSplat(32));
1726 APInt E(32, 0);
1727 EXPECT_TRUE(E.isSplat(1));
1728 EXPECT_TRUE(E.isSplat(2));
1729 EXPECT_TRUE(E.isSplat(4));
1730 EXPECT_TRUE(E.isSplat(8));
1731 EXPECT_TRUE(E.isSplat(16));
1732 EXPECT_TRUE(E.isSplat(32));
1735 TEST(APIntTest, isMask) {
1736 EXPECT_FALSE(APInt(32, 0x01010101).isMask());
1737 EXPECT_FALSE(APInt(32, 0xf0000000).isMask());
1738 EXPECT_FALSE(APInt(32, 0xffff0000).isMask());
1739 EXPECT_FALSE(APInt(32, 0xff << 1).isMask());
1741 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1742 EXPECT_FALSE(APInt(N, 0).isMask());
1744 APInt One(N, 1);
1745 for (int I = 1; I <= N; ++I) {
1746 APInt MaskVal = One.shl(I) - 1;
1747 EXPECT_TRUE(MaskVal.isMask());
1748 EXPECT_TRUE(MaskVal.isMask(I));
1753 TEST(APIntTest, isShiftedMask) {
1754 EXPECT_FALSE(APInt(32, 0x01010101).isShiftedMask());
1755 EXPECT_TRUE(APInt(32, 0xf0000000).isShiftedMask());
1756 EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask());
1757 EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask());
1759 unsigned MaskIdx, MaskLen;
1760 EXPECT_FALSE(APInt(32, 0x01010101).isShiftedMask(MaskIdx, MaskLen));
1761 EXPECT_TRUE(APInt(32, 0xf0000000).isShiftedMask(MaskIdx, MaskLen));
1762 EXPECT_EQ(28, (int)MaskIdx);
1763 EXPECT_EQ(4, (int)MaskLen);
1764 EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask(MaskIdx, MaskLen));
1765 EXPECT_EQ(16, (int)MaskIdx);
1766 EXPECT_EQ(16, (int)MaskLen);
1767 EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask(MaskIdx, MaskLen));
1768 EXPECT_EQ(1, (int)MaskIdx);
1769 EXPECT_EQ(8, (int)MaskLen);
1771 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1772 EXPECT_FALSE(APInt(N, 0).isShiftedMask());
1773 EXPECT_FALSE(APInt(N, 0).isShiftedMask(MaskIdx, MaskLen));
1775 APInt One(N, 1);
1776 for (int I = 1; I < N; ++I) {
1777 APInt MaskVal = One.shl(I) - 1;
1778 EXPECT_TRUE(MaskVal.isShiftedMask());
1779 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1780 EXPECT_EQ(0, (int)MaskIdx);
1781 EXPECT_EQ(I, (int)MaskLen);
1783 for (int I = 1; I < N - 1; ++I) {
1784 APInt MaskVal = One.shl(I);
1785 EXPECT_TRUE(MaskVal.isShiftedMask());
1786 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1787 EXPECT_EQ(I, (int)MaskIdx);
1788 EXPECT_EQ(1, (int)MaskLen);
1790 for (int I = 1; I < N; ++I) {
1791 APInt MaskVal = APInt::getHighBitsSet(N, I);
1792 EXPECT_TRUE(MaskVal.isShiftedMask());
1793 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1794 EXPECT_EQ(N - I, (int)MaskIdx);
1795 EXPECT_EQ(I, (int)MaskLen);
1800 TEST(APIntTest, isOneBitSet) {
1801 EXPECT_FALSE(APInt(5, 0x00).isOneBitSet(0));
1802 EXPECT_FALSE(APInt(5, 0x02).isOneBitSet(0));
1803 EXPECT_FALSE(APInt(5, 0x03).isOneBitSet(0));
1804 EXPECT_TRUE(APInt(5, 0x02).isOneBitSet(1));
1805 EXPECT_TRUE(APInt(32, (unsigned)(0xffu << 31)).isOneBitSet(31));
1807 EXPECT_TRUE(APInt::getOneBitSet(255, 13).isOneBitSet(13));
1810 TEST(APIntTest, isPowerOf2) {
1811 EXPECT_FALSE(APInt(5, 0x00).isPowerOf2());
1812 EXPECT_FALSE(APInt(32, 0x11).isPowerOf2());
1813 EXPECT_TRUE(APInt(17, 0x01).isPowerOf2());
1814 EXPECT_TRUE(APInt(32, (unsigned)(0xffu << 31)).isPowerOf2());
1816 for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
1817 EXPECT_FALSE(APInt(N, 0).isPowerOf2());
1818 EXPECT_TRUE(APInt::getSignedMinValue(N).isPowerOf2());
1820 APInt One(N, 1);
1821 for (int I = 1; I < N - 1; ++I) {
1822 EXPECT_TRUE(APInt::getOneBitSet(N, I).isPowerOf2());
1824 APInt MaskVal = One.shl(I);
1825 EXPECT_TRUE(MaskVal.isPowerOf2());
1830 TEST(APIntTest, isNegatedPowerOf2) {
1831 EXPECT_FALSE(APInt(5, 0x00).isNegatedPowerOf2());
1832 EXPECT_TRUE(APInt(15, 0x7ffe).isNegatedPowerOf2());
1833 EXPECT_TRUE(APInt(16, 0xfffc).isNegatedPowerOf2());
1834 EXPECT_TRUE(APInt(32, 0xffffffff).isNegatedPowerOf2());
1836 for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
1837 EXPECT_FALSE(APInt(N, 0).isNegatedPowerOf2());
1838 EXPECT_TRUE(APInt::getAllOnes(N).isNegatedPowerOf2());
1839 EXPECT_TRUE(APInt::getSignedMinValue(N).isNegatedPowerOf2());
1840 EXPECT_TRUE((-APInt::getSignedMinValue(N)).isNegatedPowerOf2());
1842 APInt One(N, 1);
1843 for (int I = 1; I < N - 1; ++I) {
1844 EXPECT_FALSE(APInt::getOneBitSet(N, I).isNegatedPowerOf2());
1845 EXPECT_TRUE((-APInt::getOneBitSet(N, I)).isNegatedPowerOf2());
1847 APInt MaskVal = One.shl(I);
1848 EXPECT_TRUE((-MaskVal).isNegatedPowerOf2());
1850 APInt ShiftMaskVal = One.getHighBitsSet(N, I);
1851 EXPECT_TRUE(ShiftMaskVal.isNegatedPowerOf2());
1856 TEST(APIntTest, isAligned) {
1857 struct {
1858 uint64_t alignment;
1859 uint64_t offset;
1860 bool isAligned;
1861 } Tests[] = {
1862 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
1863 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
1864 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
1866 for (const auto &T : Tests)
1867 EXPECT_EQ(APInt(32, T.offset).isAligned(Align(T.alignment)), T.isAligned);
1868 // Tests for APInt that can't represent the alignment.
1869 // Here APInt(4, I) can represent values from 0 to 15.
1870 EXPECT_TRUE(APInt(4, 0).isAligned(Align(32))); // zero is always aligned.
1871 for (int I = 1; I < 16; ++I)
1872 EXPECT_FALSE(APInt(4, I).isAligned(Align(32)));
1875 // Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which
1876 // does self-move on some platforms.
1877 #ifdef EXPENSIVE_CHECKS
1878 #if defined(__clang__)
1879 // Disable the pragma warning from versions of Clang without -Wself-move
1880 #pragma clang diagnostic push
1881 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1882 // Disable the warning that triggers on exactly what is being tested.
1883 #pragma clang diagnostic push
1884 #pragma clang diagnostic ignored "-Wself-move"
1885 #endif
1886 TEST(APIntTest, SelfMoveAssignment) {
1887 APInt X(32, 0xdeadbeef);
1888 X = std::move(X);
1889 EXPECT_EQ(32u, X.getBitWidth());
1890 EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
1892 uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
1893 APInt Y(128, Bits);
1894 Y = std::move(Y);
1895 EXPECT_EQ(128u, Y.getBitWidth());
1896 EXPECT_EQ(~0ULL, Y.getLimitedValue());
1897 const uint64_t *Raw = Y.getRawData();
1898 EXPECT_EQ(2u, Y.getNumWords());
1899 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
1900 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
1902 #if defined(__clang__)
1903 #pragma clang diagnostic pop
1904 #pragma clang diagnostic pop
1905 #endif
1906 #endif // EXPENSIVE_CHECKS
1908 TEST(APIntTest, byteSwap) {
1909 EXPECT_EQ(0x00000000, APInt(16, 0x0000).byteSwap());
1910 EXPECT_EQ(0x0000010f, APInt(16, 0x0f01).byteSwap());
1911 EXPECT_EQ(0x00ff8000, APInt(24, 0x0080ff).byteSwap());
1912 EXPECT_EQ(0x117700ff, APInt(32, 0xff007711).byteSwap());
1913 EXPECT_EQ(0x228811aaffULL, APInt(40, 0xffaa118822ULL).byteSwap());
1914 EXPECT_EQ(0x050403020100ULL, APInt(48, 0x000102030405ULL).byteSwap());
1915 EXPECT_EQ(0xff050403020100ULL, APInt(56, 0x000102030405ffULL).byteSwap());
1916 EXPECT_EQ(0xff050403020100aaULL, APInt(64, 0xaa000102030405ffULL).byteSwap());
1918 for (unsigned N : {16, 24, 32, 48, 56, 64, 72, 80, 96, 112, 128, 248, 256,
1919 1024, 1032, 1040}) {
1920 for (unsigned I = 0; I < N; I += 8) {
1921 APInt X = APInt::getBitsSet(N, I, I + 8);
1922 APInt Y = APInt::getBitsSet(N, N - I - 8, N - I);
1923 EXPECT_EQ(Y, X.byteSwap());
1924 EXPECT_EQ(X, Y.byteSwap());
1929 TEST(APIntTest, reverseBits) {
1930 EXPECT_EQ(1, APInt(1, 1).reverseBits());
1931 EXPECT_EQ(0, APInt(1, 0).reverseBits());
1933 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1934 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1936 EXPECT_EQ(0xb, APInt(4, 0xd).reverseBits());
1937 EXPECT_EQ(0xd, APInt(4, 0xb).reverseBits());
1938 EXPECT_EQ(0xf, APInt(4, 0xf).reverseBits());
1940 EXPECT_EQ(0x30, APInt(7, 0x6).reverseBits());
1941 EXPECT_EQ(0x5a, APInt(7, 0x2d).reverseBits());
1943 EXPECT_EQ(0x0f, APInt(8, 0xf0).reverseBits());
1944 EXPECT_EQ(0xf0, APInt(8, 0x0f).reverseBits());
1946 EXPECT_EQ(0x0f0f, APInt(16, 0xf0f0).reverseBits());
1947 EXPECT_EQ(0xf0f0, APInt(16, 0x0f0f).reverseBits());
1949 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1950 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1952 EXPECT_EQ(0x402880a0 >> 1, APInt(31, 0x05011402).reverseBits());
1954 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1955 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1957 EXPECT_EQ(0x0f0f0f0f0f0f0f0f, APInt(64, 0xf0f0f0f0f0f0f0f0).reverseBits());
1958 EXPECT_EQ(0xf0f0f0f0f0f0f0f0, APInt(64, 0x0f0f0f0f0f0f0f0f).reverseBits());
1960 for (unsigned N : { 1, 8, 16, 24, 31, 32, 33,
1961 63, 64, 65, 127, 128, 257, 1024 }) {
1962 for (unsigned I = 0; I < N; ++I) {
1963 APInt X = APInt::getOneBitSet(N, I);
1964 APInt Y = APInt::getOneBitSet(N, N - (I + 1));
1965 EXPECT_EQ(Y, X.reverseBits());
1966 EXPECT_EQ(X, Y.reverseBits());
1971 TEST(APIntTest, insertBits) {
1972 APInt iSrc(31, 0x00123456);
1974 // Direct copy.
1975 APInt i31(31, 0x76543210ull);
1976 i31.insertBits(iSrc, 0);
1977 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
1979 // Single word src/dst insertion.
1980 APInt i63(63, 0x01234567FFFFFFFFull);
1981 i63.insertBits(iSrc, 4);
1982 EXPECT_EQ(static_cast<int64_t>(0x012345600123456Full), i63.getSExtValue());
1984 // Zero width insert is a noop.
1985 i31.insertBits(APInt::getZeroWidth(), 1);
1986 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
1988 // Insert single word src into one word of dst.
1989 APInt i120(120, UINT64_MAX, true);
1990 i120.insertBits(iSrc, 8);
1991 EXPECT_EQ(static_cast<int64_t>(0xFFFFFF80123456FFull), i120.getSExtValue());
1993 // Insert single word src into two words of dst.
1994 APInt i127(127, UINT64_MAX, true);
1995 i127.insertBits(iSrc, 48);
1996 EXPECT_EQ(i127.extractBits(64, 0).getZExtValue(), 0x3456FFFFFFFFFFFFull);
1997 EXPECT_EQ(i127.extractBits(63, 64).getZExtValue(), 0x7FFFFFFFFFFF8012ull);
1999 // Insert on word boundaries.
2000 APInt i128(128, 0);
2001 i128.insertBits(APInt(64, UINT64_MAX, true), 0);
2002 i128.insertBits(APInt(64, UINT64_MAX, true), 64);
2003 EXPECT_EQ(-1, i128.getSExtValue());
2005 APInt i256(256, UINT64_MAX, true);
2006 i256.insertBits(APInt(65, 0), 0);
2007 i256.insertBits(APInt(69, 0), 64);
2008 i256.insertBits(APInt(128, 0), 128);
2009 EXPECT_EQ(0u, i256.getSExtValue());
2011 APInt i257(257, 0);
2012 i257.insertBits(APInt(96, UINT64_MAX, true), 64);
2013 EXPECT_EQ(i257.extractBits(64, 0).getZExtValue(), 0x0000000000000000ull);
2014 EXPECT_EQ(i257.extractBits(64, 64).getZExtValue(), 0xFFFFFFFFFFFFFFFFull);
2015 EXPECT_EQ(i257.extractBits(64, 128).getZExtValue(), 0x00000000FFFFFFFFull);
2016 EXPECT_EQ(i257.extractBits(65, 192).getZExtValue(), 0x0000000000000000ull);
2018 // General insertion.
2019 APInt i260(260, UINT64_MAX, true);
2020 i260.insertBits(APInt(129, 1ull << 48), 15);
2021 EXPECT_EQ(i260.extractBits(64, 0).getZExtValue(), 0x8000000000007FFFull);
2022 EXPECT_EQ(i260.extractBits(64, 64).getZExtValue(), 0x0000000000000000ull);
2023 EXPECT_EQ(i260.extractBits(64, 128).getZExtValue(), 0xFFFFFFFFFFFF0000ull);
2024 EXPECT_EQ(i260.extractBits(64, 192).getZExtValue(), 0xFFFFFFFFFFFFFFFFull);
2025 EXPECT_EQ(i260.extractBits(4, 256).getZExtValue(), 0x000000000000000Full);
2028 TEST(APIntTest, insertBitsUInt64) {
2029 // Tests cloned from insertBits but adapted to the numBits <= 64 constraint
2030 uint64_t iSrc = 0x00123456;
2032 // Direct copy.
2033 APInt i31(31, 0x76543210ull);
2034 i31.insertBits(iSrc, 0, 31);
2035 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
2037 // Single word src/dst insertion.
2038 APInt i63(63, 0x01234567FFFFFFFFull);
2039 i63.insertBits(iSrc, 4, 31);
2040 EXPECT_EQ(static_cast<int64_t>(0x012345600123456Full), i63.getSExtValue());
2042 // Insert single word src into one word of dst.
2043 APInt i120(120, UINT64_MAX, true);
2044 i120.insertBits(iSrc, 8, 31);
2045 EXPECT_EQ(static_cast<int64_t>(0xFFFFFF80123456FFull), i120.getSExtValue());
2047 // Insert single word src into two words of dst.
2048 APInt i127(127, UINT64_MAX, true);
2049 i127.insertBits(iSrc, 48, 31);
2050 EXPECT_EQ(i127.extractBits(64, 0).getZExtValue(), 0x3456FFFFFFFFFFFFull);
2051 EXPECT_EQ(i127.extractBits(63, 64).getZExtValue(), 0x7FFFFFFFFFFF8012ull);
2053 // Insert on word boundaries.
2054 APInt i128(128, 0);
2055 i128.insertBits(UINT64_MAX, 0, 64);
2056 i128.insertBits(UINT64_MAX, 64, 64);
2057 EXPECT_EQ(-1, i128.getSExtValue());
2059 APInt i256(256, UINT64_MAX, true);
2060 i256.insertBits(0, 0, 64);
2061 i256.insertBits(0, 64, 1);
2062 i256.insertBits(0, 64, 64);
2063 i256.insertBits(0, 128, 5);
2064 i256.insertBits(0, 128, 64);
2065 i256.insertBits(0, 192, 64);
2066 EXPECT_EQ(0u, i256.getSExtValue());
2068 APInt i257(257, 0);
2069 i257.insertBits(APInt(96, UINT64_MAX, true), 64);
2070 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 0), 0x0000000000000000ull);
2071 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 64), 0xFFFFFFFFFFFFFFFFull);
2072 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 128), 0x00000000FFFFFFFFull);
2073 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 192), 0x0000000000000000ull);
2074 EXPECT_EQ(i257.extractBitsAsZExtValue(1, 256), 0x0000000000000000ull);
2076 // General insertion.
2077 APInt i260(260, UINT64_MAX, true);
2078 i260.insertBits(APInt(129, 1ull << 48), 15);
2079 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 0), 0x8000000000007FFFull);
2080 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 64), 0x0000000000000000ull);
2081 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 128), 0xFFFFFFFFFFFF0000ull);
2082 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 192), 0xFFFFFFFFFFFFFFFFull);
2083 EXPECT_EQ(i260.extractBitsAsZExtValue(4, 256), 0x000000000000000Full);
2086 TEST(APIntTest, extractBits) {
2087 APInt i32(32, 0x1234567);
2088 EXPECT_EQ(0x3456, i32.extractBits(16, 4));
2090 APInt i64(64, 0x01234567FFFFFFFFull);
2091 EXPECT_EQ(0xFFFFFFFF, i64.extractBits(32, 0));
2092 EXPECT_EQ(0xFFFFFFFF, i64.trunc(32));
2093 EXPECT_EQ(0x01234567, i64.extractBits(32, 32));
2094 EXPECT_EQ(0x01234567, i64.lshr(32).trunc(32));
2096 APInt i257(257, 0xFFFFFFFFFF0000FFull, true);
2097 EXPECT_EQ(0xFFu, i257.extractBits(16, 0));
2098 EXPECT_EQ(0xFFu, i257.lshr(0).trunc(16));
2099 EXPECT_EQ((0xFFu >> 1), i257.extractBits(16, 1));
2100 EXPECT_EQ((0xFFu >> 1), i257.lshr(1).trunc(16));
2101 EXPECT_EQ(-1, i257.extractBits(32, 64).getSExtValue());
2102 EXPECT_EQ(-1, i257.lshr(64).trunc(32).getSExtValue());
2103 EXPECT_EQ(-1, i257.extractBits(128, 128).getSExtValue());
2104 EXPECT_EQ(-1, i257.lshr(128).trunc(128).getSExtValue());
2105 EXPECT_EQ(-1, i257.extractBits(66, 191).getSExtValue());
2106 EXPECT_EQ(-1, i257.lshr(191).trunc(66).getSExtValue());
2107 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2108 i257.extractBits(128, 1).getSExtValue());
2109 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2110 i257.lshr(1).trunc(128).getSExtValue());
2111 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2112 i257.extractBits(129, 1).getSExtValue());
2113 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2114 i257.lshr(1).trunc(129).getSExtValue());
2116 EXPECT_EQ(APInt(48, 0),
2117 APInt(144, "281474976710655", 10).extractBits(48, 48));
2118 EXPECT_EQ(APInt(48, 0),
2119 APInt(144, "281474976710655", 10).lshr(48).trunc(48));
2120 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2121 APInt(144, "281474976710655", 10).extractBits(48, 0));
2122 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2123 APInt(144, "281474976710655", 10).lshr(0).trunc(48));
2124 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2125 APInt(144, "281474976710655", 10).extractBits(48, 1));
2126 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2127 APInt(144, "281474976710655", 10).lshr(1).trunc(48));
2130 TEST(APIntTest, extractBitsAsZExtValue) {
2131 // Tests based on extractBits
2132 APInt i32(32, 0x1234567);
2133 EXPECT_EQ(0x3456u, i32.extractBitsAsZExtValue(16, 4));
2135 APInt i257(257, 0xFFFFFFFFFF0000FFull, true);
2136 EXPECT_EQ(0xFFu, i257.extractBitsAsZExtValue(16, 0));
2137 EXPECT_EQ((0xFFu >> 1), i257.extractBitsAsZExtValue(16, 1));
2138 EXPECT_EQ(0xFFFFFFFFull, i257.extractBitsAsZExtValue(32, 64));
2139 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 128));
2140 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 192));
2141 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 191));
2142 EXPECT_EQ(0x3u, i257.extractBitsAsZExtValue(2, 255));
2143 EXPECT_EQ(0xFFFFFFFFFF80007Full, i257.extractBitsAsZExtValue(64, 1));
2144 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 65));
2145 EXPECT_EQ(0xFFFFFFFFFF80007Full, i257.extractBitsAsZExtValue(64, 1));
2146 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 65));
2147 EXPECT_EQ(0x1ull, i257.extractBitsAsZExtValue(1, 129));
2149 EXPECT_EQ(APInt(48, 0),
2150 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 48));
2151 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2152 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 0));
2153 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2154 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 1));
2157 TEST(APIntTest, getLowBitsSet) {
2158 APInt i128lo64 = APInt::getLowBitsSet(128, 64);
2159 EXPECT_EQ(0u, i128lo64.countl_one());
2160 EXPECT_EQ(64u, i128lo64.countl_zero());
2161 EXPECT_EQ(64u, i128lo64.getActiveBits());
2162 EXPECT_EQ(0u, i128lo64.countr_zero());
2163 EXPECT_EQ(64u, i128lo64.countr_one());
2164 EXPECT_EQ(64u, i128lo64.popcount());
2167 TEST(APIntTest, getBitsSet) {
2168 APInt i64hi1lo1 = APInt::getBitsSet(64, 1, 63);
2169 EXPECT_EQ(0u, i64hi1lo1.countl_one());
2170 EXPECT_EQ(1u, i64hi1lo1.countl_zero());
2171 EXPECT_EQ(63u, i64hi1lo1.getActiveBits());
2172 EXPECT_EQ(1u, i64hi1lo1.countr_zero());
2173 EXPECT_EQ(0u, i64hi1lo1.countr_one());
2174 EXPECT_EQ(62u, i64hi1lo1.popcount());
2176 APInt i127hi1lo1 = APInt::getBitsSet(127, 1, 126);
2177 EXPECT_EQ(0u, i127hi1lo1.countl_one());
2178 EXPECT_EQ(1u, i127hi1lo1.countl_zero());
2179 EXPECT_EQ(126u, i127hi1lo1.getActiveBits());
2180 EXPECT_EQ(1u, i127hi1lo1.countr_zero());
2181 EXPECT_EQ(0u, i127hi1lo1.countr_one());
2182 EXPECT_EQ(125u, i127hi1lo1.popcount());
2185 TEST(APIntTest, getBitsSetWithWrap) {
2186 APInt i64hi1lo1 = APInt::getBitsSetWithWrap(64, 1, 63);
2187 EXPECT_EQ(0u, i64hi1lo1.countl_one());
2188 EXPECT_EQ(1u, i64hi1lo1.countl_zero());
2189 EXPECT_EQ(63u, i64hi1lo1.getActiveBits());
2190 EXPECT_EQ(1u, i64hi1lo1.countr_zero());
2191 EXPECT_EQ(0u, i64hi1lo1.countr_one());
2192 EXPECT_EQ(62u, i64hi1lo1.popcount());
2194 APInt i127hi1lo1 = APInt::getBitsSetWithWrap(127, 1, 126);
2195 EXPECT_EQ(0u, i127hi1lo1.countl_one());
2196 EXPECT_EQ(1u, i127hi1lo1.countl_zero());
2197 EXPECT_EQ(126u, i127hi1lo1.getActiveBits());
2198 EXPECT_EQ(1u, i127hi1lo1.countr_zero());
2199 EXPECT_EQ(0u, i127hi1lo1.countr_one());
2200 EXPECT_EQ(125u, i127hi1lo1.popcount());
2202 APInt i64hi1lo1wrap = APInt::getBitsSetWithWrap(64, 63, 1);
2203 EXPECT_EQ(1u, i64hi1lo1wrap.countl_one());
2204 EXPECT_EQ(0u, i64hi1lo1wrap.countl_zero());
2205 EXPECT_EQ(64u, i64hi1lo1wrap.getActiveBits());
2206 EXPECT_EQ(0u, i64hi1lo1wrap.countr_zero());
2207 EXPECT_EQ(1u, i64hi1lo1wrap.countr_one());
2208 EXPECT_EQ(2u, i64hi1lo1wrap.popcount());
2210 APInt i127hi1lo1wrap = APInt::getBitsSetWithWrap(127, 126, 1);
2211 EXPECT_EQ(1u, i127hi1lo1wrap.countl_one());
2212 EXPECT_EQ(0u, i127hi1lo1wrap.countl_zero());
2213 EXPECT_EQ(127u, i127hi1lo1wrap.getActiveBits());
2214 EXPECT_EQ(0u, i127hi1lo1wrap.countr_zero());
2215 EXPECT_EQ(1u, i127hi1lo1wrap.countr_one());
2216 EXPECT_EQ(2u, i127hi1lo1wrap.popcount());
2218 APInt i32hiequallowrap = APInt::getBitsSetWithWrap(32, 10, 10);
2219 EXPECT_EQ(32u, i32hiequallowrap.countl_one());
2220 EXPECT_EQ(0u, i32hiequallowrap.countl_zero());
2221 EXPECT_EQ(32u, i32hiequallowrap.getActiveBits());
2222 EXPECT_EQ(0u, i32hiequallowrap.countr_zero());
2223 EXPECT_EQ(32u, i32hiequallowrap.countr_one());
2224 EXPECT_EQ(32u, i32hiequallowrap.popcount());
2227 TEST(APIntTest, getHighBitsSet) {
2228 APInt i64hi32 = APInt::getHighBitsSet(64, 32);
2229 EXPECT_EQ(32u, i64hi32.countl_one());
2230 EXPECT_EQ(0u, i64hi32.countl_zero());
2231 EXPECT_EQ(64u, i64hi32.getActiveBits());
2232 EXPECT_EQ(32u, i64hi32.countr_zero());
2233 EXPECT_EQ(0u, i64hi32.countr_one());
2234 EXPECT_EQ(32u, i64hi32.popcount());
2237 TEST(APIntTest, getBitsSetFrom) {
2238 APInt i64hi31 = APInt::getBitsSetFrom(64, 33);
2239 EXPECT_EQ(31u, i64hi31.countl_one());
2240 EXPECT_EQ(0u, i64hi31.countl_zero());
2241 EXPECT_EQ(64u, i64hi31.getActiveBits());
2242 EXPECT_EQ(33u, i64hi31.countr_zero());
2243 EXPECT_EQ(0u, i64hi31.countr_one());
2244 EXPECT_EQ(31u, i64hi31.popcount());
2247 TEST(APIntTest, setLowBits) {
2248 APInt i64lo32(64, 0);
2249 i64lo32.setLowBits(32);
2250 EXPECT_EQ(0u, i64lo32.countl_one());
2251 EXPECT_EQ(32u, i64lo32.countl_zero());
2252 EXPECT_EQ(32u, i64lo32.getActiveBits());
2253 EXPECT_EQ(0u, i64lo32.countr_zero());
2254 EXPECT_EQ(32u, i64lo32.countr_one());
2255 EXPECT_EQ(32u, i64lo32.popcount());
2257 APInt i128lo64(128, 0);
2258 i128lo64.setLowBits(64);
2259 EXPECT_EQ(0u, i128lo64.countl_one());
2260 EXPECT_EQ(64u, i128lo64.countl_zero());
2261 EXPECT_EQ(64u, i128lo64.getActiveBits());
2262 EXPECT_EQ(0u, i128lo64.countr_zero());
2263 EXPECT_EQ(64u, i128lo64.countr_one());
2264 EXPECT_EQ(64u, i128lo64.popcount());
2266 APInt i128lo24(128, 0);
2267 i128lo24.setLowBits(24);
2268 EXPECT_EQ(0u, i128lo24.countl_one());
2269 EXPECT_EQ(104u, i128lo24.countl_zero());
2270 EXPECT_EQ(24u, i128lo24.getActiveBits());
2271 EXPECT_EQ(0u, i128lo24.countr_zero());
2272 EXPECT_EQ(24u, i128lo24.countr_one());
2273 EXPECT_EQ(24u, i128lo24.popcount());
2275 APInt i128lo104(128, 0);
2276 i128lo104.setLowBits(104);
2277 EXPECT_EQ(0u, i128lo104.countl_one());
2278 EXPECT_EQ(24u, i128lo104.countl_zero());
2279 EXPECT_EQ(104u, i128lo104.getActiveBits());
2280 EXPECT_EQ(0u, i128lo104.countr_zero());
2281 EXPECT_EQ(104u, i128lo104.countr_one());
2282 EXPECT_EQ(104u, i128lo104.popcount());
2284 APInt i128lo0(128, 0);
2285 i128lo0.setLowBits(0);
2286 EXPECT_EQ(0u, i128lo0.countl_one());
2287 EXPECT_EQ(128u, i128lo0.countl_zero());
2288 EXPECT_EQ(0u, i128lo0.getActiveBits());
2289 EXPECT_EQ(128u, i128lo0.countr_zero());
2290 EXPECT_EQ(0u, i128lo0.countr_one());
2291 EXPECT_EQ(0u, i128lo0.popcount());
2293 APInt i80lo79(80, 0);
2294 i80lo79.setLowBits(79);
2295 EXPECT_EQ(0u, i80lo79.countl_one());
2296 EXPECT_EQ(1u, i80lo79.countl_zero());
2297 EXPECT_EQ(79u, i80lo79.getActiveBits());
2298 EXPECT_EQ(0u, i80lo79.countr_zero());
2299 EXPECT_EQ(79u, i80lo79.countr_one());
2300 EXPECT_EQ(79u, i80lo79.popcount());
2303 TEST(APIntTest, setHighBits) {
2304 APInt i64hi32(64, 0);
2305 i64hi32.setHighBits(32);
2306 EXPECT_EQ(32u, i64hi32.countl_one());
2307 EXPECT_EQ(0u, i64hi32.countl_zero());
2308 EXPECT_EQ(64u, i64hi32.getActiveBits());
2309 EXPECT_EQ(32u, i64hi32.countr_zero());
2310 EXPECT_EQ(0u, i64hi32.countr_one());
2311 EXPECT_EQ(32u, i64hi32.popcount());
2313 APInt i128hi64(128, 0);
2314 i128hi64.setHighBits(64);
2315 EXPECT_EQ(64u, i128hi64.countl_one());
2316 EXPECT_EQ(0u, i128hi64.countl_zero());
2317 EXPECT_EQ(128u, i128hi64.getActiveBits());
2318 EXPECT_EQ(64u, i128hi64.countr_zero());
2319 EXPECT_EQ(0u, i128hi64.countr_one());
2320 EXPECT_EQ(64u, i128hi64.popcount());
2322 APInt i128hi24(128, 0);
2323 i128hi24.setHighBits(24);
2324 EXPECT_EQ(24u, i128hi24.countl_one());
2325 EXPECT_EQ(0u, i128hi24.countl_zero());
2326 EXPECT_EQ(128u, i128hi24.getActiveBits());
2327 EXPECT_EQ(104u, i128hi24.countr_zero());
2328 EXPECT_EQ(0u, i128hi24.countr_one());
2329 EXPECT_EQ(24u, i128hi24.popcount());
2331 APInt i128hi104(128, 0);
2332 i128hi104.setHighBits(104);
2333 EXPECT_EQ(104u, i128hi104.countl_one());
2334 EXPECT_EQ(0u, i128hi104.countl_zero());
2335 EXPECT_EQ(128u, i128hi104.getActiveBits());
2336 EXPECT_EQ(24u, i128hi104.countr_zero());
2337 EXPECT_EQ(0u, i128hi104.countr_one());
2338 EXPECT_EQ(104u, i128hi104.popcount());
2340 APInt i128hi0(128, 0);
2341 i128hi0.setHighBits(0);
2342 EXPECT_EQ(0u, i128hi0.countl_one());
2343 EXPECT_EQ(128u, i128hi0.countl_zero());
2344 EXPECT_EQ(0u, i128hi0.getActiveBits());
2345 EXPECT_EQ(128u, i128hi0.countr_zero());
2346 EXPECT_EQ(0u, i128hi0.countr_one());
2347 EXPECT_EQ(0u, i128hi0.popcount());
2349 APInt i80hi1(80, 0);
2350 i80hi1.setHighBits(1);
2351 EXPECT_EQ(1u, i80hi1.countl_one());
2352 EXPECT_EQ(0u, i80hi1.countl_zero());
2353 EXPECT_EQ(80u, i80hi1.getActiveBits());
2354 EXPECT_EQ(79u, i80hi1.countr_zero());
2355 EXPECT_EQ(0u, i80hi1.countr_one());
2356 EXPECT_EQ(1u, i80hi1.popcount());
2358 APInt i32hi16(32, 0);
2359 i32hi16.setHighBits(16);
2360 EXPECT_EQ(16u, i32hi16.countl_one());
2361 EXPECT_EQ(0u, i32hi16.countl_zero());
2362 EXPECT_EQ(32u, i32hi16.getActiveBits());
2363 EXPECT_EQ(16u, i32hi16.countr_zero());
2364 EXPECT_EQ(0u, i32hi16.countr_one());
2365 EXPECT_EQ(16u, i32hi16.popcount());
2368 TEST(APIntTest, setBitsFrom) {
2369 APInt i64from63(64, 0);
2370 i64from63.setBitsFrom(63);
2371 EXPECT_EQ(1u, i64from63.countl_one());
2372 EXPECT_EQ(0u, i64from63.countl_zero());
2373 EXPECT_EQ(64u, i64from63.getActiveBits());
2374 EXPECT_EQ(63u, i64from63.countr_zero());
2375 EXPECT_EQ(0u, i64from63.countr_one());
2376 EXPECT_EQ(1u, i64from63.popcount());
2379 TEST(APIntTest, setAllBits) {
2380 APInt i32(32, 0);
2381 i32.setAllBits();
2382 EXPECT_EQ(32u, i32.countl_one());
2383 EXPECT_EQ(0u, i32.countl_zero());
2384 EXPECT_EQ(32u, i32.getActiveBits());
2385 EXPECT_EQ(0u, i32.countr_zero());
2386 EXPECT_EQ(32u, i32.countr_one());
2387 EXPECT_EQ(32u, i32.popcount());
2389 APInt i64(64, 0);
2390 i64.setAllBits();
2391 EXPECT_EQ(64u, i64.countl_one());
2392 EXPECT_EQ(0u, i64.countl_zero());
2393 EXPECT_EQ(64u, i64.getActiveBits());
2394 EXPECT_EQ(0u, i64.countr_zero());
2395 EXPECT_EQ(64u, i64.countr_one());
2396 EXPECT_EQ(64u, i64.popcount());
2398 APInt i96(96, 0);
2399 i96.setAllBits();
2400 EXPECT_EQ(96u, i96.countl_one());
2401 EXPECT_EQ(0u, i96.countl_zero());
2402 EXPECT_EQ(96u, i96.getActiveBits());
2403 EXPECT_EQ(0u, i96.countr_zero());
2404 EXPECT_EQ(96u, i96.countr_one());
2405 EXPECT_EQ(96u, i96.popcount());
2407 APInt i128(128, 0);
2408 i128.setAllBits();
2409 EXPECT_EQ(128u, i128.countl_one());
2410 EXPECT_EQ(0u, i128.countl_zero());
2411 EXPECT_EQ(128u, i128.getActiveBits());
2412 EXPECT_EQ(0u, i128.countr_zero());
2413 EXPECT_EQ(128u, i128.countr_one());
2414 EXPECT_EQ(128u, i128.popcount());
2417 TEST(APIntTest, getLoBits) {
2418 APInt i32(32, 0xfa);
2419 i32.setHighBits(1);
2420 EXPECT_EQ(0xa, i32.getLoBits(4));
2421 APInt i128(128, 0xfa);
2422 i128.setHighBits(1);
2423 EXPECT_EQ(0xa, i128.getLoBits(4));
2426 TEST(APIntTest, getHiBits) {
2427 APInt i32(32, 0xfa);
2428 i32.setHighBits(2);
2429 EXPECT_EQ(0xc, i32.getHiBits(4));
2430 APInt i128(128, 0xfa);
2431 i128.setHighBits(2);
2432 EXPECT_EQ(0xc, i128.getHiBits(4));
2435 TEST(APIntTest, clearLowBits) {
2436 APInt i64hi32 = APInt::getAllOnes(64);
2437 i64hi32.clearLowBits(32);
2438 EXPECT_EQ(32u, i64hi32.countl_one());
2439 EXPECT_EQ(0u, i64hi32.countl_zero());
2440 EXPECT_EQ(64u, i64hi32.getActiveBits());
2441 EXPECT_EQ(32u, i64hi32.countr_zero());
2442 EXPECT_EQ(0u, i64hi32.countr_one());
2443 EXPECT_EQ(32u, i64hi32.popcount());
2445 APInt i128hi64 = APInt::getAllOnes(128);
2446 i128hi64.clearLowBits(64);
2447 EXPECT_EQ(64u, i128hi64.countl_one());
2448 EXPECT_EQ(0u, i128hi64.countl_zero());
2449 EXPECT_EQ(128u, i128hi64.getActiveBits());
2450 EXPECT_EQ(64u, i128hi64.countr_zero());
2451 EXPECT_EQ(0u, i128hi64.countr_one());
2452 EXPECT_EQ(64u, i128hi64.popcount());
2454 APInt i128hi24 = APInt::getAllOnes(128);
2455 i128hi24.clearLowBits(104);
2456 EXPECT_EQ(24u, i128hi24.countl_one());
2457 EXPECT_EQ(0u, i128hi24.countl_zero());
2458 EXPECT_EQ(128u, i128hi24.getActiveBits());
2459 EXPECT_EQ(104u, i128hi24.countr_zero());
2460 EXPECT_EQ(0u, i128hi24.countr_one());
2461 EXPECT_EQ(24u, i128hi24.popcount());
2463 APInt i128hi104 = APInt::getAllOnes(128);
2464 i128hi104.clearLowBits(24);
2465 EXPECT_EQ(104u, i128hi104.countl_one());
2466 EXPECT_EQ(0u, i128hi104.countl_zero());
2467 EXPECT_EQ(128u, i128hi104.getActiveBits());
2468 EXPECT_EQ(24u, i128hi104.countr_zero());
2469 EXPECT_EQ(0u, i128hi104.countr_one());
2470 EXPECT_EQ(104u, i128hi104.popcount());
2472 APInt i128hi0 = APInt::getAllOnes(128);
2473 i128hi0.clearLowBits(128);
2474 EXPECT_EQ(0u, i128hi0.countl_one());
2475 EXPECT_EQ(128u, i128hi0.countl_zero());
2476 EXPECT_EQ(0u, i128hi0.getActiveBits());
2477 EXPECT_EQ(128u, i128hi0.countr_zero());
2478 EXPECT_EQ(0u, i128hi0.countr_one());
2479 EXPECT_EQ(0u, i128hi0.popcount());
2481 APInt i80hi1 = APInt::getAllOnes(80);
2482 i80hi1.clearLowBits(79);
2483 EXPECT_EQ(1u, i80hi1.countl_one());
2484 EXPECT_EQ(0u, i80hi1.countl_zero());
2485 EXPECT_EQ(80u, i80hi1.getActiveBits());
2486 EXPECT_EQ(79u, i80hi1.countr_zero());
2487 EXPECT_EQ(0u, i80hi1.countr_one());
2488 EXPECT_EQ(1u, i80hi1.popcount());
2490 APInt i32hi16 = APInt::getAllOnes(32);
2491 i32hi16.clearLowBits(16);
2492 EXPECT_EQ(16u, i32hi16.countl_one());
2493 EXPECT_EQ(0u, i32hi16.countl_zero());
2494 EXPECT_EQ(32u, i32hi16.getActiveBits());
2495 EXPECT_EQ(16u, i32hi16.countr_zero());
2496 EXPECT_EQ(0u, i32hi16.countr_one());
2497 EXPECT_EQ(16u, i32hi16.popcount());
2500 TEST(APIntTest, GCD) {
2501 using APIntOps::GreatestCommonDivisor;
2503 for (unsigned Bits : {1, 2, 32, 63, 64, 65}) {
2504 // Test some corner cases near zero.
2505 APInt Zero(Bits, 0), One(Bits, 1);
2506 EXPECT_EQ(GreatestCommonDivisor(Zero, Zero), Zero);
2507 EXPECT_EQ(GreatestCommonDivisor(Zero, One), One);
2508 EXPECT_EQ(GreatestCommonDivisor(One, Zero), One);
2509 EXPECT_EQ(GreatestCommonDivisor(One, One), One);
2511 if (Bits > 1) {
2512 APInt Two(Bits, 2);
2513 EXPECT_EQ(GreatestCommonDivisor(Zero, Two), Two);
2514 EXPECT_EQ(GreatestCommonDivisor(One, Two), One);
2515 EXPECT_EQ(GreatestCommonDivisor(Two, Two), Two);
2517 // Test some corner cases near the highest representable value.
2518 APInt Max(Bits, 0);
2519 Max.setAllBits();
2520 EXPECT_EQ(GreatestCommonDivisor(Zero, Max), Max);
2521 EXPECT_EQ(GreatestCommonDivisor(One, Max), One);
2522 EXPECT_EQ(GreatestCommonDivisor(Two, Max), One);
2523 EXPECT_EQ(GreatestCommonDivisor(Max, Max), Max);
2525 APInt MaxOver2 = Max.udiv(Two);
2526 EXPECT_EQ(GreatestCommonDivisor(MaxOver2, Max), One);
2527 // Max - 1 == Max / 2 * 2, because Max is odd.
2528 EXPECT_EQ(GreatestCommonDivisor(MaxOver2, Max - 1), MaxOver2);
2532 // Compute the 20th Mersenne prime.
2533 const unsigned BitWidth = 4450;
2534 APInt HugePrime = APInt::getLowBitsSet(BitWidth, 4423);
2536 // 9931 and 123456 are coprime.
2537 APInt A = HugePrime * APInt(BitWidth, 9931);
2538 APInt B = HugePrime * APInt(BitWidth, 123456);
2539 APInt C = GreatestCommonDivisor(A, B);
2540 EXPECT_EQ(C, HugePrime);
2543 TEST(APIntTest, LogicalRightShift) {
2544 APInt i256(APInt::getHighBitsSet(256, 2));
2546 i256.lshrInPlace(1);
2547 EXPECT_EQ(1U, i256.countl_zero());
2548 EXPECT_EQ(253U, i256.countr_zero());
2549 EXPECT_EQ(2U, i256.popcount());
2551 i256.lshrInPlace(62);
2552 EXPECT_EQ(63U, i256.countl_zero());
2553 EXPECT_EQ(191U, i256.countr_zero());
2554 EXPECT_EQ(2U, i256.popcount());
2556 i256.lshrInPlace(65);
2557 EXPECT_EQ(128U, i256.countl_zero());
2558 EXPECT_EQ(126U, i256.countr_zero());
2559 EXPECT_EQ(2U, i256.popcount());
2561 i256.lshrInPlace(64);
2562 EXPECT_EQ(192U, i256.countl_zero());
2563 EXPECT_EQ(62U, i256.countr_zero());
2564 EXPECT_EQ(2U, i256.popcount());
2566 i256.lshrInPlace(63);
2567 EXPECT_EQ(255U, i256.countl_zero());
2568 EXPECT_EQ(0U, i256.countr_zero());
2569 EXPECT_EQ(1U, i256.popcount());
2571 // Ensure we handle large shifts of multi-word.
2572 const APInt neg_one(128, static_cast<uint64_t>(-1), true);
2573 EXPECT_EQ(0, neg_one.lshr(128));
2576 TEST(APIntTest, ArithmeticRightShift) {
2577 APInt i72(APInt::getHighBitsSet(72, 1));
2578 i72.ashrInPlace(46);
2579 EXPECT_EQ(47U, i72.countl_one());
2580 EXPECT_EQ(25U, i72.countr_zero());
2581 EXPECT_EQ(47U, i72.popcount());
2583 i72 = APInt::getHighBitsSet(72, 1);
2584 i72.ashrInPlace(64);
2585 EXPECT_EQ(65U, i72.countl_one());
2586 EXPECT_EQ(7U, i72.countr_zero());
2587 EXPECT_EQ(65U, i72.popcount());
2589 APInt i128(APInt::getHighBitsSet(128, 1));
2590 i128.ashrInPlace(64);
2591 EXPECT_EQ(65U, i128.countl_one());
2592 EXPECT_EQ(63U, i128.countr_zero());
2593 EXPECT_EQ(65U, i128.popcount());
2595 // Ensure we handle large shifts of multi-word.
2596 const APInt signmin32(APInt::getSignedMinValue(32));
2597 EXPECT_TRUE(signmin32.ashr(32).isAllOnes());
2599 // Ensure we handle large shifts of multi-word.
2600 const APInt umax32(APInt::getSignedMaxValue(32));
2601 EXPECT_EQ(0, umax32.ashr(32));
2603 // Ensure we handle large shifts of multi-word.
2604 const APInt signmin128(APInt::getSignedMinValue(128));
2605 EXPECT_TRUE(signmin128.ashr(128).isAllOnes());
2607 // Ensure we handle large shifts of multi-word.
2608 const APInt umax128(APInt::getSignedMaxValue(128));
2609 EXPECT_EQ(0, umax128.ashr(128));
2612 TEST(APIntTest, LeftShift) {
2613 APInt i256(APInt::getLowBitsSet(256, 2));
2615 i256 <<= 1;
2616 EXPECT_EQ(253U, i256.countl_zero());
2617 EXPECT_EQ(1U, i256.countr_zero());
2618 EXPECT_EQ(2U, i256.popcount());
2620 i256 <<= 62;
2621 EXPECT_EQ(191U, i256.countl_zero());
2622 EXPECT_EQ(63U, i256.countr_zero());
2623 EXPECT_EQ(2U, i256.popcount());
2625 i256 <<= 65;
2626 EXPECT_EQ(126U, i256.countl_zero());
2627 EXPECT_EQ(128U, i256.countr_zero());
2628 EXPECT_EQ(2U, i256.popcount());
2630 i256 <<= 64;
2631 EXPECT_EQ(62U, i256.countl_zero());
2632 EXPECT_EQ(192U, i256.countr_zero());
2633 EXPECT_EQ(2U, i256.popcount());
2635 i256 <<= 63;
2636 EXPECT_EQ(0U, i256.countl_zero());
2637 EXPECT_EQ(255U, i256.countr_zero());
2638 EXPECT_EQ(1U, i256.popcount());
2640 // Ensure we handle large shifts of multi-word.
2641 const APInt neg_one(128, static_cast<uint64_t>(-1), true);
2642 EXPECT_EQ(0, neg_one.shl(128));
2645 TEST(APIntTest, isSubsetOf) {
2646 APInt i32_1(32, 1);
2647 APInt i32_2(32, 2);
2648 APInt i32_3(32, 3);
2649 EXPECT_FALSE(i32_3.isSubsetOf(i32_1));
2650 EXPECT_TRUE(i32_1.isSubsetOf(i32_3));
2651 EXPECT_FALSE(i32_2.isSubsetOf(i32_1));
2652 EXPECT_FALSE(i32_1.isSubsetOf(i32_2));
2653 EXPECT_TRUE(i32_3.isSubsetOf(i32_3));
2655 APInt i128_1(128, 1);
2656 APInt i128_2(128, 2);
2657 APInt i128_3(128, 3);
2658 EXPECT_FALSE(i128_3.isSubsetOf(i128_1));
2659 EXPECT_TRUE(i128_1.isSubsetOf(i128_3));
2660 EXPECT_FALSE(i128_2.isSubsetOf(i128_1));
2661 EXPECT_FALSE(i128_1.isSubsetOf(i128_2));
2662 EXPECT_TRUE(i128_3.isSubsetOf(i128_3));
2664 i128_1 <<= 64;
2665 i128_2 <<= 64;
2666 i128_3 <<= 64;
2667 EXPECT_FALSE(i128_3.isSubsetOf(i128_1));
2668 EXPECT_TRUE(i128_1.isSubsetOf(i128_3));
2669 EXPECT_FALSE(i128_2.isSubsetOf(i128_1));
2670 EXPECT_FALSE(i128_1.isSubsetOf(i128_2));
2671 EXPECT_TRUE(i128_3.isSubsetOf(i128_3));
2674 TEST(APIntTest, sext) {
2675 EXPECT_EQ(0, APInt(1, 0).sext(64));
2676 EXPECT_EQ(~uint64_t(0), APInt(1, 1).sext(64));
2678 APInt i32_max(APInt::getSignedMaxValue(32).sext(63));
2679 EXPECT_EQ(i32_max, i32_max.sext(63));
2680 EXPECT_EQ(32U, i32_max.countl_zero());
2681 EXPECT_EQ(0U, i32_max.countr_zero());
2682 EXPECT_EQ(31U, i32_max.popcount());
2684 APInt i32_min(APInt::getSignedMinValue(32).sext(63));
2685 EXPECT_EQ(i32_min, i32_min.sext(63));
2686 EXPECT_EQ(32U, i32_min.countl_one());
2687 EXPECT_EQ(31U, i32_min.countr_zero());
2688 EXPECT_EQ(32U, i32_min.popcount());
2690 APInt i32_neg1(APInt(32, ~uint64_t(0)).sext(63));
2691 EXPECT_EQ(i32_neg1, i32_neg1.sext(63));
2692 EXPECT_EQ(63U, i32_neg1.countl_one());
2693 EXPECT_EQ(0U, i32_neg1.countr_zero());
2694 EXPECT_EQ(63U, i32_neg1.popcount());
2697 TEST(APIntTest, trunc) {
2698 APInt val(32, 0xFFFFFFFF);
2699 EXPECT_EQ(0xFFFF, val.trunc(16));
2700 EXPECT_EQ(0xFFFFFFFF, val.trunc(32));
2703 TEST(APIntTest, concat) {
2704 APInt Int1(4, 0x1ULL);
2705 APInt Int3(4, 0x3ULL);
2707 EXPECT_EQ(0x31, Int3.concat(Int1));
2708 EXPECT_EQ(APInt(12, 0x313), Int3.concat(Int1).concat(Int3));
2709 EXPECT_EQ(APInt(16, 0x3313), Int3.concat(Int3).concat(Int1).concat(Int3));
2711 APInt I64(64, 0x3ULL);
2712 EXPECT_EQ(I64, I64.concat(I64).lshr(64).trunc(64));
2714 APInt I65(65, 0x3ULL);
2715 APInt I0 = APInt::getZeroWidth();
2716 EXPECT_EQ(I65, I65.concat(I0));
2717 EXPECT_EQ(I65, I0.concat(I65));
2720 TEST(APIntTest, multiply) {
2721 APInt i64(64, 1234);
2723 EXPECT_EQ(7006652, i64 * 5678);
2724 EXPECT_EQ(7006652, 5678 * i64);
2726 APInt i128 = APInt::getOneBitSet(128, 64);
2727 APInt i128_1234(128, 1234);
2728 i128_1234 <<= 64;
2729 EXPECT_EQ(i128_1234, i128 * 1234);
2730 EXPECT_EQ(i128_1234, 1234 * i128);
2732 APInt i96 = APInt::getOneBitSet(96, 64);
2733 i96 *= ~0ULL;
2734 EXPECT_EQ(32U, i96.countl_one());
2735 EXPECT_EQ(32U, i96.popcount());
2736 EXPECT_EQ(64U, i96.countr_zero());
2739 TEST(APIntTest, RoundingUDiv) {
2740 for (uint64_t Ai = 1; Ai <= 255; Ai++) {
2741 APInt A(8, Ai);
2742 APInt Zero(8, 0);
2743 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::UP));
2744 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::DOWN));
2745 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::TOWARD_ZERO));
2747 for (uint64_t Bi = 1; Bi <= 255; Bi++) {
2748 APInt B(8, Bi);
2750 APInt Quo = APIntOps::RoundingUDiv(A, B, APInt::Rounding::UP);
2751 auto Prod = Quo.zext(16) * B.zext(16);
2752 EXPECT_TRUE(Prod.uge(Ai));
2753 if (Prod.ugt(Ai)) {
2754 EXPECT_TRUE(((Quo - 1).zext(16) * B.zext(16)).ult(Ai));
2758 APInt Quo = A.udiv(B);
2759 EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::TOWARD_ZERO));
2760 EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::DOWN));
2766 TEST(APIntTest, RoundingSDiv) {
2767 for (int64_t Ai = -128; Ai <= 127; Ai++) {
2768 APInt A(8, Ai);
2770 if (Ai != 0) {
2771 APInt Zero(8, 0);
2772 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::UP));
2773 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::DOWN));
2774 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::TOWARD_ZERO));
2777 for (int64_t Bi = -128; Bi <= 127; Bi++) {
2778 if (Bi == 0)
2779 continue;
2781 APInt B(8, Bi);
2782 APInt QuoTowardZero = A.sdiv(B);
2784 APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::UP);
2785 if (A.srem(B).isZero()) {
2786 EXPECT_EQ(QuoTowardZero, Quo);
2787 } else if (A.isNegative() !=
2788 B.isNegative()) { // if the math quotient is negative.
2789 EXPECT_EQ(QuoTowardZero, Quo);
2790 } else {
2791 EXPECT_EQ(QuoTowardZero + 1, Quo);
2795 APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::DOWN);
2796 if (A.srem(B).isZero()) {
2797 EXPECT_EQ(QuoTowardZero, Quo);
2798 } else if (A.isNegative() !=
2799 B.isNegative()) { // if the math quotient is negative.
2800 EXPECT_EQ(QuoTowardZero - 1, Quo);
2801 } else {
2802 EXPECT_EQ(QuoTowardZero, Quo);
2805 EXPECT_EQ(QuoTowardZero,
2806 APIntOps::RoundingSDiv(A, B, APInt::Rounding::TOWARD_ZERO));
2811 TEST(APIntTest, umul_ov) {
2812 const std::pair<uint64_t, uint64_t> Overflows[] = {
2813 {0x8000000000000000, 2},
2814 {0x5555555555555556, 3},
2815 {4294967296, 4294967296},
2816 {4294967295, 4294967298},
2818 const std::pair<uint64_t, uint64_t> NonOverflows[] = {
2819 {0x7fffffffffffffff, 2},
2820 {0x5555555555555555, 3},
2821 {4294967295, 4294967297},
2824 bool Overflow;
2825 for (auto &X : Overflows) {
2826 APInt A(64, X.first);
2827 APInt B(64, X.second);
2828 (void)A.umul_ov(B, Overflow);
2829 EXPECT_TRUE(Overflow);
2831 for (auto &X : NonOverflows) {
2832 APInt A(64, X.first);
2833 APInt B(64, X.second);
2834 (void)A.umul_ov(B, Overflow);
2835 EXPECT_FALSE(Overflow);
2838 for (unsigned Bits = 1; Bits <= 5; ++Bits)
2839 for (unsigned A = 0; A != 1u << Bits; ++A)
2840 for (unsigned B = 0; B != 1u << Bits; ++B) {
2841 APInt N1 = APInt(Bits, A), N2 = APInt(Bits, B);
2842 APInt Narrow = N1.umul_ov(N2, Overflow);
2843 APInt Wide = N1.zext(2 * Bits) * N2.zext(2 * Bits);
2844 EXPECT_EQ(Wide.trunc(Bits), Narrow);
2845 EXPECT_EQ(Narrow.zext(2 * Bits) != Wide, Overflow);
2849 TEST(APIntTest, smul_ov) {
2850 for (unsigned Bits = 1; Bits <= 5; ++Bits)
2851 for (unsigned A = 0; A != 1u << Bits; ++A)
2852 for (unsigned B = 0; B != 1u << Bits; ++B) {
2853 bool Overflow;
2854 APInt N1 = APInt(Bits, A), N2 = APInt(Bits, B);
2855 APInt Narrow = N1.smul_ov(N2, Overflow);
2856 APInt Wide = N1.sext(2 * Bits) * N2.sext(2 * Bits);
2857 EXPECT_EQ(Wide.trunc(Bits), Narrow);
2858 EXPECT_EQ(Narrow.sext(2 * Bits) != Wide, Overflow);
2862 TEST(APIntTest, SolveQuadraticEquationWrap) {
2863 // Verify that "Solution" is the first non-negative integer that solves
2864 // Ax^2 + Bx + C = "0 or overflow", i.e. that it is a correct solution
2865 // as calculated by SolveQuadraticEquationWrap.
2866 auto Validate = [] (int A, int B, int C, unsigned Width, int Solution) {
2867 int Mask = (1 << Width) - 1;
2869 // Solution should be non-negative.
2870 EXPECT_GE(Solution, 0);
2872 auto OverflowBits = [] (int64_t V, unsigned W) {
2873 return V & -(1 << W);
2876 int64_t Over0 = OverflowBits(C, Width);
2878 auto IsZeroOrOverflow = [&] (int X) {
2879 int64_t ValueAtX = A*X*X + B*X + C;
2880 int64_t OverX = OverflowBits(ValueAtX, Width);
2881 return (ValueAtX & Mask) == 0 || OverX != Over0;
2884 auto EquationToString = [&] (const char *X_str) {
2885 return (Twine(A) + Twine(X_str) + Twine("^2 + ") + Twine(B) +
2886 Twine(X_str) + Twine(" + ") + Twine(C) + Twine(", bitwidth: ") +
2887 Twine(Width)).str();
2890 auto IsSolution = [&] (const char *X_str, int X) {
2891 if (IsZeroOrOverflow(X))
2892 return ::testing::AssertionSuccess()
2893 << X << " is a solution of " << EquationToString(X_str);
2894 return ::testing::AssertionFailure()
2895 << X << " is not an expected solution of "
2896 << EquationToString(X_str);
2899 auto IsNotSolution = [&] (const char *X_str, int X) {
2900 if (!IsZeroOrOverflow(X))
2901 return ::testing::AssertionSuccess()
2902 << X << " is not a solution of " << EquationToString(X_str);
2903 return ::testing::AssertionFailure()
2904 << X << " is an unexpected solution of "
2905 << EquationToString(X_str);
2908 // This is the important part: make sure that there is no solution that
2909 // is less than the calculated one.
2910 if (Solution > 0) {
2911 for (int X = 1; X < Solution-1; ++X)
2912 EXPECT_PRED_FORMAT1(IsNotSolution, X);
2915 // Verify that the calculated solution is indeed a solution.
2916 EXPECT_PRED_FORMAT1(IsSolution, Solution);
2919 // Generate all possible quadratic equations with Width-bit wide integer
2920 // coefficients, get the solution from SolveQuadraticEquationWrap, and
2921 // verify that the solution is correct.
2922 auto Iterate = [&] (unsigned Width) {
2923 assert(1 < Width && Width < 32);
2924 int Low = -(1 << (Width-1));
2925 int High = (1 << (Width-1));
2927 for (int A = Low; A != High; ++A) {
2928 if (A == 0)
2929 continue;
2930 for (int B = Low; B != High; ++B) {
2931 for (int C = Low; C != High; ++C) {
2932 std::optional<APInt> S = APIntOps::SolveQuadraticEquationWrap(
2933 APInt(Width, A), APInt(Width, B), APInt(Width, C), Width);
2934 if (S)
2935 Validate(A, B, C, Width, S->getSExtValue());
2941 // Test all widths in [2..6].
2942 for (unsigned i = 2; i <= 6; ++i)
2943 Iterate(i);
2946 TEST(APIntTest, MultiplicativeInverseExaustive) {
2947 for (unsigned BitWidth = 1; BitWidth <= 16; ++BitWidth) {
2948 for (unsigned Value = 0; Value < (1u << BitWidth); ++Value) {
2949 APInt V = APInt(BitWidth, Value);
2950 APInt MulInv =
2951 V.zext(BitWidth + 1)
2952 .multiplicativeInverse(APInt::getSignedMinValue(BitWidth + 1))
2953 .trunc(BitWidth);
2954 APInt One = V * MulInv;
2955 if (!V.isZero() && V.countr_zero() == 0) {
2956 // Multiplicative inverse exists for all odd numbers.
2957 EXPECT_TRUE(One.isOne());
2958 } else {
2959 // Multiplicative inverse does not exist for even numbers (and 0).
2960 EXPECT_TRUE(MulInv.isZero());
2966 TEST(APIntTest, GetMostSignificantDifferentBit) {
2967 EXPECT_EQ(APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 0)),
2968 std::nullopt);
2969 EXPECT_EQ(
2970 APIntOps::GetMostSignificantDifferentBit(APInt(8, 42), APInt(8, 42)),
2971 std::nullopt);
2972 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 1)),
2973 0u);
2974 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 2)),
2975 1u);
2976 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 3)),
2977 1u);
2978 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 0)),
2979 0u);
2980 EXPECT_EQ(APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 1)),
2981 std::nullopt);
2982 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 2)),
2983 1u);
2984 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 3)),
2985 1u);
2986 EXPECT_EQ(
2987 *APIntOps::GetMostSignificantDifferentBit(APInt(8, 42), APInt(8, 112)),
2988 6u);
2991 TEST(APIntTest, GetMostSignificantDifferentBitExaustive) {
2992 auto GetHighestDifferentBitBruteforce =
2993 [](const APInt &V0, const APInt &V1) -> std::optional<unsigned> {
2994 assert(V0.getBitWidth() == V1.getBitWidth() && "Must have same bitwidth");
2995 if (V0 == V1)
2996 return std::nullopt; // Bitwise identical.
2997 // There is a mismatch. Let's find the most significant different bit.
2998 for (int Bit = V0.getBitWidth() - 1; Bit >= 0; --Bit) {
2999 if (V0[Bit] == V1[Bit])
3000 continue;
3001 return Bit;
3003 llvm_unreachable("Must have found bit mismatch.");
3006 for (unsigned BitWidth = 1; BitWidth <= 8; ++BitWidth) {
3007 for (unsigned V0 = 0; V0 < (1u << BitWidth); ++V0) {
3008 for (unsigned V1 = 0; V1 < (1u << BitWidth); ++V1) {
3009 APInt A = APInt(BitWidth, V0);
3010 APInt B = APInt(BitWidth, V1);
3012 auto Bit = APIntOps::GetMostSignificantDifferentBit(A, B);
3013 EXPECT_EQ(Bit, GetHighestDifferentBitBruteforce(A, B));
3015 if (!Bit)
3016 EXPECT_EQ(A, B);
3017 else {
3018 EXPECT_NE(A, B);
3019 for (unsigned NumLowBits = 0; NumLowBits <= BitWidth; ++NumLowBits) {
3020 APInt Adash = A;
3021 Adash.clearLowBits(NumLowBits);
3022 APInt Bdash = B;
3023 Bdash.clearLowBits(NumLowBits);
3024 // Clearing only low bits up to and including *Bit is sufficient
3025 // to make values equal.
3026 if (NumLowBits >= 1 + *Bit)
3027 EXPECT_EQ(Adash, Bdash);
3028 else
3029 EXPECT_NE(Adash, Bdash);
3037 TEST(APIntTest, SignbitZeroChecks) {
3038 EXPECT_TRUE(APInt(8, -1).isNegative());
3039 EXPECT_FALSE(APInt(8, -1).isNonNegative());
3040 EXPECT_FALSE(APInt(8, -1).isStrictlyPositive());
3041 EXPECT_TRUE(APInt(8, -1).isNonPositive());
3043 EXPECT_FALSE(APInt(8, 0).isNegative());
3044 EXPECT_TRUE(APInt(8, 0).isNonNegative());
3045 EXPECT_FALSE(APInt(8, 0).isStrictlyPositive());
3046 EXPECT_TRUE(APInt(8, 0).isNonPositive());
3048 EXPECT_FALSE(APInt(8, 1).isNegative());
3049 EXPECT_TRUE(APInt(8, 1).isNonNegative());
3050 EXPECT_TRUE(APInt(8, 1).isStrictlyPositive());
3051 EXPECT_FALSE(APInt(8, 1).isNonPositive());
3054 TEST(APIntTest, ZeroWidth) {
3055 // Zero width Constructors.
3056 auto ZW = APInt::getZeroWidth();
3057 EXPECT_EQ(0U, ZW.getBitWidth());
3058 EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
3059 EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
3061 // Default constructor is single bit wide.
3062 EXPECT_EQ(1U, APInt().getBitWidth());
3064 // Copy ctor (move is down below).
3065 APInt ZW2(ZW);
3066 EXPECT_EQ(0U, ZW2.getBitWidth());
3067 // Assignment
3068 ZW = ZW2;
3069 EXPECT_EQ(0U, ZW.getBitWidth());
3071 // Methods like getLowBitsSet work with zero bits.
3072 EXPECT_EQ(0U, APInt::getLowBitsSet(0, 0).getBitWidth());
3073 EXPECT_EQ(0U, APInt::getSplat(0, ZW).getBitWidth());
3074 EXPECT_EQ(0U, APInt(4, 10).extractBits(0, 2).getBitWidth());
3075 EXPECT_EQ(0U, APInt(4, 10).extractBitsAsZExtValue(0, 2));
3077 // Logical operators.
3078 ZW |= ZW2;
3079 ZW &= ZW2;
3080 ZW ^= ZW2;
3081 ZW |= 42; // These ignore high bits of the literal.
3082 ZW &= 42;
3083 ZW ^= 42;
3084 EXPECT_EQ(1, ZW.isIntN(0));
3086 // Modulo Arithmetic. Divide/Rem aren't defined on division by zero, so they
3087 // aren't supported.
3088 ZW += ZW2;
3089 ZW -= ZW2;
3090 ZW *= ZW2;
3092 // Logical Shifts and rotates, the amount must be <= bitwidth.
3093 ZW <<= 0;
3094 ZW.lshrInPlace(0);
3095 (void)ZW.rotl(0);
3096 (void)ZW.rotr(0);
3098 // Comparisons.
3099 EXPECT_EQ(1, ZW == ZW);
3100 EXPECT_EQ(0, ZW != ZW);
3101 EXPECT_EQ(0, ZW.ult(ZW));
3103 // Mutations.
3104 ZW.setBitsWithWrap(0, 0);
3105 ZW.setBits(0, 0);
3106 ZW.clearAllBits();
3107 ZW.flipAllBits();
3109 // Leading, trailing, ctpop, etc
3110 EXPECT_EQ(0U, ZW.countl_zero());
3111 EXPECT_EQ(0U, ZW.countl_one());
3112 EXPECT_EQ(0U, ZW.popcount());
3113 EXPECT_EQ(0U, ZW.reverseBits().getBitWidth());
3114 EXPECT_EQ(0U, ZW.getHiBits(0).getBitWidth());
3115 EXPECT_EQ(0U, ZW.getLoBits(0).getBitWidth());
3116 EXPECT_EQ(0, ZW.zext(4));
3117 EXPECT_EQ(0U, APInt(4, 3).trunc(0).getBitWidth());
3118 EXPECT_TRUE(ZW.isAllOnes());
3120 // Zero extension.
3121 EXPECT_EQ(0U, ZW.getZExtValue());
3123 SmallString<42> STR;
3124 ZW.toStringUnsigned(STR);
3125 EXPECT_EQ("0", STR);
3127 // Move ctor (keep at the end of the method since moves are destructive).
3128 APInt MZW1(std::move(ZW));
3129 EXPECT_EQ(0U, MZW1.getBitWidth());
3130 // Move Assignment
3131 MZW1 = std::move(ZW2);
3132 EXPECT_EQ(0U, MZW1.getBitWidth());
3135 TEST(APIntTest, ScaleBitMask) {
3136 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x00), 8), APInt(8, 0x00));
3137 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x01), 8), APInt(8, 0x0F));
3138 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x02), 8), APInt(8, 0xF0));
3139 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x03), 8), APInt(8, 0xFF));
3141 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 4), APInt(4, 0x00));
3142 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xFF), 4), APInt(4, 0x0F));
3143 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xE4), 4), APInt(4, 0x0E));
3145 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 8), APInt(8, 0x00));
3147 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getZero(1024), 4096),
3148 APInt::getZero(4096));
3149 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getAllOnes(4096), 256),
3150 APInt::getAllOnes(256));
3151 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getOneBitSet(4096, 32), 256),
3152 APInt::getOneBitSet(256, 2));
3154 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x00), 8, true), APInt(8, 0x00));
3155 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x01), 8, true), APInt(8, 0x0F));
3156 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x02), 8, true), APInt(8, 0xF0));
3157 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x03), 8, true), APInt(8, 0xFF));
3159 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 4, true), APInt(4, 0x00));
3160 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xFF), 4, true), APInt(4, 0x0F));
3161 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xE4), 4, true), APInt(4, 0x08));
3164 TEST(APIntTest, DenseMap) {
3165 DenseMap<APInt, int> Map;
3166 APInt ZeroWidthInt(0, 0, false);
3167 Map.insert({ZeroWidthInt, 0});
3168 Map.find(ZeroWidthInt);
3171 TEST(APIntTest, TryExt) {
3172 APInt small(32, 42);
3173 APInt large(128, {0xffff, 0xffff});
3174 ASSERT_TRUE(small.tryZExtValue().has_value());
3175 ASSERT_TRUE(small.trySExtValue().has_value());
3176 ASSERT_FALSE(large.tryZExtValue().has_value());
3177 ASSERT_FALSE(large.trySExtValue().has_value());
3178 ASSERT_EQ(small.trySExtValue().value_or(41), 42);
3179 ASSERT_EQ(large.trySExtValue().value_or(41), 41);
3181 APInt negOne32(32, 0);
3182 negOne32.setAllBits();
3183 ASSERT_EQ(negOne32.trySExtValue().value_or(42), -1);
3184 APInt negOne64(64, 0);
3185 negOne64.setAllBits();
3186 ASSERT_EQ(negOne64.trySExtValue().value_or(42), -1);
3187 APInt negOne128(128, 0);
3188 negOne128.setAllBits();
3189 ASSERT_EQ(negOne128.trySExtValue().value_or(42), -1);
3190 ASSERT_EQ(42, APInt(128, -1).trySExtValue().value_or(42));
3193 } // end anonymous namespace