[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / ADT / APIntTest.cpp
blob0af294c609698bcfd8a5eca04d2894156d4de64a
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/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "gtest/gtest.h"
14 #include <array>
16 using namespace llvm;
18 namespace {
20 TEST(APIntTest, ValueInit) {
21 APInt Zero = APInt();
22 EXPECT_TRUE(!Zero);
23 EXPECT_TRUE(!Zero.zext(64));
24 EXPECT_TRUE(!Zero.sext(64));
27 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
28 TEST(APIntTest, ShiftLeftByZero) {
29 APInt One = APInt::getZero(65) + 1;
30 APInt Shl = One.shl(0);
31 EXPECT_TRUE(Shl[0]);
32 EXPECT_FALSE(Shl[1]);
35 TEST(APIntTest, i64_ArithmeticRightShiftNegative) {
36 const APInt neg_one(64, static_cast<uint64_t>(-1), true);
37 EXPECT_EQ(neg_one, neg_one.ashr(7));
40 TEST(APIntTest, i128_NegativeCount) {
41 APInt Minus3(128, static_cast<uint64_t>(-3), true);
42 EXPECT_EQ(126u, Minus3.countLeadingOnes());
43 EXPECT_EQ(-3, Minus3.getSExtValue());
45 APInt Minus1(128, static_cast<uint64_t>(-1), true);
46 EXPECT_EQ(0u, Minus1.countLeadingZeros());
47 EXPECT_EQ(128u, Minus1.countLeadingOnes());
48 EXPECT_EQ(128u, Minus1.getActiveBits());
49 EXPECT_EQ(0u, Minus1.countTrailingZeros());
50 EXPECT_EQ(128u, Minus1.countTrailingOnes());
51 EXPECT_EQ(128u, Minus1.countPopulation());
52 EXPECT_EQ(-1, Minus1.getSExtValue());
55 TEST(APIntTest, i33_Count) {
56 APInt i33minus2(33, static_cast<uint64_t>(-2), true);
57 EXPECT_EQ(0u, i33minus2.countLeadingZeros());
58 EXPECT_EQ(32u, i33minus2.countLeadingOnes());
59 EXPECT_EQ(33u, i33minus2.getActiveBits());
60 EXPECT_EQ(1u, i33minus2.countTrailingZeros());
61 EXPECT_EQ(32u, i33minus2.countPopulation());
62 EXPECT_EQ(-2, i33minus2.getSExtValue());
63 EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
66 TEST(APIntTest, i61_Count) {
67 APInt i61(61, 1 << 15);
68 EXPECT_EQ(45u, i61.countLeadingZeros());
69 EXPECT_EQ(0u, i61.countLeadingOnes());
70 EXPECT_EQ(16u, i61.getActiveBits());
71 EXPECT_EQ(15u, i61.countTrailingZeros());
72 EXPECT_EQ(1u, i61.countPopulation());
73 EXPECT_EQ(static_cast<int64_t>(1 << 15), i61.getSExtValue());
74 EXPECT_EQ(static_cast<uint64_t>(1 << 15), i61.getZExtValue());
76 i61.setBits(8, 19);
77 EXPECT_EQ(42u, i61.countLeadingZeros());
78 EXPECT_EQ(0u, i61.countLeadingOnes());
79 EXPECT_EQ(19u, i61.getActiveBits());
80 EXPECT_EQ(8u, i61.countTrailingZeros());
81 EXPECT_EQ(11u, i61.countPopulation());
82 EXPECT_EQ(static_cast<int64_t>((1 << 19) - (1 << 8)), i61.getSExtValue());
83 EXPECT_EQ(static_cast<uint64_t>((1 << 19) - (1 << 8)), i61.getZExtValue());
86 TEST(APIntTest, i65_Count) {
87 APInt i65(65, 0, true);
88 EXPECT_EQ(65u, i65.countLeadingZeros());
89 EXPECT_EQ(0u, i65.countLeadingOnes());
90 EXPECT_EQ(0u, i65.getActiveBits());
91 EXPECT_EQ(1u, i65.getActiveWords());
92 EXPECT_EQ(65u, i65.countTrailingZeros());
93 EXPECT_EQ(0u, i65.countPopulation());
95 APInt i65minus(65, 0, true);
96 i65minus.setBit(64);
97 EXPECT_EQ(0u, i65minus.countLeadingZeros());
98 EXPECT_EQ(1u, i65minus.countLeadingOnes());
99 EXPECT_EQ(65u, i65minus.getActiveBits());
100 EXPECT_EQ(64u, i65minus.countTrailingZeros());
101 EXPECT_EQ(1u, i65minus.countPopulation());
104 TEST(APIntTest, i128_PositiveCount) {
105 APInt u128max = APInt::getAllOnes(128);
106 EXPECT_EQ(128u, u128max.countLeadingOnes());
107 EXPECT_EQ(0u, u128max.countLeadingZeros());
108 EXPECT_EQ(128u, u128max.getActiveBits());
109 EXPECT_EQ(0u, u128max.countTrailingZeros());
110 EXPECT_EQ(128u, u128max.countTrailingOnes());
111 EXPECT_EQ(128u, u128max.countPopulation());
113 APInt u64max(128, static_cast<uint64_t>(-1), false);
114 EXPECT_EQ(64u, u64max.countLeadingZeros());
115 EXPECT_EQ(0u, u64max.countLeadingOnes());
116 EXPECT_EQ(64u, u64max.getActiveBits());
117 EXPECT_EQ(0u, u64max.countTrailingZeros());
118 EXPECT_EQ(64u, u64max.countTrailingOnes());
119 EXPECT_EQ(64u, u64max.countPopulation());
120 EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
122 APInt zero(128, 0, true);
123 EXPECT_EQ(128u, zero.countLeadingZeros());
124 EXPECT_EQ(0u, zero.countLeadingOnes());
125 EXPECT_EQ(0u, zero.getActiveBits());
126 EXPECT_EQ(128u, zero.countTrailingZeros());
127 EXPECT_EQ(0u, zero.countTrailingOnes());
128 EXPECT_EQ(0u, zero.countPopulation());
129 EXPECT_EQ(0u, zero.getSExtValue());
130 EXPECT_EQ(0u, zero.getZExtValue());
132 APInt one(128, 1, true);
133 EXPECT_EQ(127u, one.countLeadingZeros());
134 EXPECT_EQ(0u, one.countLeadingOnes());
135 EXPECT_EQ(1u, one.getActiveBits());
136 EXPECT_EQ(0u, one.countTrailingZeros());
137 EXPECT_EQ(1u, one.countTrailingOnes());
138 EXPECT_EQ(1u, one.countPopulation());
139 EXPECT_EQ(1, one.getSExtValue());
140 EXPECT_EQ(1u, one.getZExtValue());
142 APInt s128(128, 2, true);
143 EXPECT_EQ(126u, s128.countLeadingZeros());
144 EXPECT_EQ(0u, s128.countLeadingOnes());
145 EXPECT_EQ(2u, s128.getActiveBits());
146 EXPECT_EQ(1u, s128.countTrailingZeros());
147 EXPECT_EQ(0u, s128.countTrailingOnes());
148 EXPECT_EQ(1u, s128.countPopulation());
149 EXPECT_EQ(2, s128.getSExtValue());
150 EXPECT_EQ(2u, s128.getZExtValue());
152 // NOP Test
153 s128.setBits(42, 42);
154 EXPECT_EQ(126u, s128.countLeadingZeros());
155 EXPECT_EQ(0u, s128.countLeadingOnes());
156 EXPECT_EQ(2u, s128.getActiveBits());
157 EXPECT_EQ(1u, s128.countTrailingZeros());
158 EXPECT_EQ(0u, s128.countTrailingOnes());
159 EXPECT_EQ(1u, s128.countPopulation());
160 EXPECT_EQ(2, s128.getSExtValue());
161 EXPECT_EQ(2u, s128.getZExtValue());
163 s128.setBits(3, 32);
164 EXPECT_EQ(96u, s128.countLeadingZeros());
165 EXPECT_EQ(0u, s128.countLeadingOnes());
166 EXPECT_EQ(32u, s128.getActiveBits());
167 EXPECT_EQ(33u, s128.getMinSignedBits());
168 EXPECT_EQ(1u, s128.countTrailingZeros());
169 EXPECT_EQ(0u, s128.countTrailingOnes());
170 EXPECT_EQ(30u, s128.countPopulation());
171 EXPECT_EQ(static_cast<uint32_t>((~0u << 3) | 2), s128.getZExtValue());
173 s128.setBits(62, 128);
174 EXPECT_EQ(0u, s128.countLeadingZeros());
175 EXPECT_EQ(66u, s128.countLeadingOnes());
176 EXPECT_EQ(128u, s128.getActiveBits());
177 EXPECT_EQ(63u, s128.getMinSignedBits());
178 EXPECT_EQ(1u, s128.countTrailingZeros());
179 EXPECT_EQ(0u, s128.countTrailingOnes());
180 EXPECT_EQ(96u, s128.countPopulation());
181 EXPECT_EQ(static_cast<int64_t>((3ull << 62) |
182 static_cast<uint32_t>((~0u << 3) | 2)),
183 s128.getSExtValue());
186 TEST(APIntTest, i256) {
187 APInt s256(256, 15, true);
188 EXPECT_EQ(252u, s256.countLeadingZeros());
189 EXPECT_EQ(0u, s256.countLeadingOnes());
190 EXPECT_EQ(4u, s256.getActiveBits());
191 EXPECT_EQ(0u, s256.countTrailingZeros());
192 EXPECT_EQ(4u, s256.countTrailingOnes());
193 EXPECT_EQ(4u, s256.countPopulation());
194 EXPECT_EQ(15, s256.getSExtValue());
195 EXPECT_EQ(15u, s256.getZExtValue());
197 s256.setBits(62, 66);
198 EXPECT_EQ(190u, s256.countLeadingZeros());
199 EXPECT_EQ(0u, s256.countLeadingOnes());
200 EXPECT_EQ(66u, s256.getActiveBits());
201 EXPECT_EQ(67u, s256.getMinSignedBits());
202 EXPECT_EQ(0u, s256.countTrailingZeros());
203 EXPECT_EQ(4u, s256.countTrailingOnes());
204 EXPECT_EQ(8u, s256.countPopulation());
206 s256.setBits(60, 256);
207 EXPECT_EQ(0u, s256.countLeadingZeros());
208 EXPECT_EQ(196u, s256.countLeadingOnes());
209 EXPECT_EQ(256u, s256.getActiveBits());
210 EXPECT_EQ(61u, s256.getMinSignedBits());
211 EXPECT_EQ(0u, s256.countTrailingZeros());
212 EXPECT_EQ(4u, s256.countTrailingOnes());
213 EXPECT_EQ(200u, s256.countPopulation());
214 EXPECT_EQ(static_cast<int64_t>((~0ull << 60) | 15), s256.getSExtValue());
217 TEST(APIntTest, i1) {
218 const APInt neg_two(1, static_cast<uint64_t>(-2), true);
219 const APInt neg_one(1, static_cast<uint64_t>(-1), true);
220 const APInt zero(1, 0);
221 const APInt one(1, 1);
222 const APInt two(1, 2);
224 EXPECT_EQ(0, neg_two.getSExtValue());
225 EXPECT_EQ(-1, neg_one.getSExtValue());
226 EXPECT_EQ(1u, neg_one.getZExtValue());
227 EXPECT_EQ(0u, zero.getZExtValue());
228 EXPECT_EQ(-1, one.getSExtValue());
229 EXPECT_EQ(1u, one.getZExtValue());
230 EXPECT_EQ(0u, two.getZExtValue());
231 EXPECT_EQ(0, two.getSExtValue());
233 // Basic equalities for 1-bit values.
234 EXPECT_EQ(zero, two);
235 EXPECT_EQ(zero, neg_two);
236 EXPECT_EQ(one, neg_one);
237 EXPECT_EQ(two, neg_two);
239 // Min/max signed values.
240 EXPECT_TRUE(zero.isMaxSignedValue());
241 EXPECT_FALSE(one.isMaxSignedValue());
242 EXPECT_FALSE(zero.isMinSignedValue());
243 EXPECT_TRUE(one.isMinSignedValue());
245 // Additions.
246 EXPECT_EQ(two, one + one);
247 EXPECT_EQ(zero, neg_one + one);
248 EXPECT_EQ(neg_two, neg_one + neg_one);
250 // Subtractions.
251 EXPECT_EQ(neg_two, neg_one - one);
252 EXPECT_EQ(two, one - neg_one);
253 EXPECT_EQ(zero, one - one);
255 // And
256 EXPECT_EQ(zero, zero & zero);
257 EXPECT_EQ(zero, one & zero);
258 EXPECT_EQ(zero, zero & one);
259 EXPECT_EQ(one, one & one);
260 EXPECT_EQ(zero, zero & zero);
261 EXPECT_EQ(zero, neg_one & zero);
262 EXPECT_EQ(zero, zero & neg_one);
263 EXPECT_EQ(neg_one, neg_one & neg_one);
265 // Or
266 EXPECT_EQ(zero, zero | zero);
267 EXPECT_EQ(one, one | zero);
268 EXPECT_EQ(one, zero | one);
269 EXPECT_EQ(one, one | one);
270 EXPECT_EQ(zero, zero | zero);
271 EXPECT_EQ(neg_one, neg_one | zero);
272 EXPECT_EQ(neg_one, zero | neg_one);
273 EXPECT_EQ(neg_one, neg_one | neg_one);
275 // Xor
276 EXPECT_EQ(zero, zero ^ zero);
277 EXPECT_EQ(one, one ^ zero);
278 EXPECT_EQ(one, zero ^ one);
279 EXPECT_EQ(zero, one ^ one);
280 EXPECT_EQ(zero, zero ^ zero);
281 EXPECT_EQ(neg_one, neg_one ^ zero);
282 EXPECT_EQ(neg_one, zero ^ neg_one);
283 EXPECT_EQ(zero, neg_one ^ neg_one);
285 // Shifts.
286 EXPECT_EQ(zero, one << one);
287 EXPECT_EQ(one, one << zero);
288 EXPECT_EQ(zero, one.shl(1));
289 EXPECT_EQ(one, one.shl(0));
290 EXPECT_EQ(zero, one.lshr(1));
291 EXPECT_EQ(one, one.ashr(1));
293 // Rotates.
294 EXPECT_EQ(one, one.rotl(0));
295 EXPECT_EQ(one, one.rotl(1));
296 EXPECT_EQ(one, one.rotr(0));
297 EXPECT_EQ(one, one.rotr(1));
299 // Multiplies.
300 EXPECT_EQ(neg_one, neg_one * one);
301 EXPECT_EQ(neg_one, one * neg_one);
302 EXPECT_EQ(one, neg_one * neg_one);
303 EXPECT_EQ(one, one * one);
305 // Divides.
306 EXPECT_EQ(neg_one, one.sdiv(neg_one));
307 EXPECT_EQ(neg_one, neg_one.sdiv(one));
308 EXPECT_EQ(one, neg_one.sdiv(neg_one));
309 EXPECT_EQ(one, one.sdiv(one));
311 EXPECT_EQ(neg_one, one.udiv(neg_one));
312 EXPECT_EQ(neg_one, neg_one.udiv(one));
313 EXPECT_EQ(one, neg_one.udiv(neg_one));
314 EXPECT_EQ(one, one.udiv(one));
316 // Remainders.
317 EXPECT_EQ(zero, neg_one.srem(one));
318 EXPECT_EQ(zero, neg_one.urem(one));
319 EXPECT_EQ(zero, one.srem(neg_one));
321 // sdivrem
323 APInt q(8, 0);
324 APInt r(8, 0);
325 APInt one(8, 1);
326 APInt two(8, 2);
327 APInt nine(8, 9);
328 APInt four(8, 4);
330 EXPECT_EQ(nine.srem(two), one);
331 EXPECT_EQ(nine.srem(-two), one);
332 EXPECT_EQ((-nine).srem(two), -one);
333 EXPECT_EQ((-nine).srem(-two), -one);
335 APInt::sdivrem(nine, two, q, r);
336 EXPECT_EQ(four, q);
337 EXPECT_EQ(one, r);
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);
350 TEST(APIntTest, compare) {
351 std::array<APInt, 5> testVals{{
352 APInt{16, 2},
353 APInt{16, 1},
354 APInt{16, 0},
355 APInt{16, (uint64_t)-1, true},
356 APInt{16, (uint64_t)-2, true},
359 for (auto &arg1 : testVals)
360 for (auto &arg2 : testVals) {
361 auto uv1 = arg1.getZExtValue();
362 auto uv2 = arg2.getZExtValue();
363 auto sv1 = arg1.getSExtValue();
364 auto sv2 = arg2.getSExtValue();
366 EXPECT_EQ(uv1 < uv2, arg1.ult(arg2));
367 EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2));
368 EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2));
369 EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2));
371 EXPECT_EQ(sv1 < sv2, arg1.slt(arg2));
372 EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2));
373 EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2));
374 EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2));
376 EXPECT_EQ(uv1 < uv2, arg1.ult(uv2));
377 EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2));
378 EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2));
379 EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2));
381 EXPECT_EQ(sv1 < sv2, arg1.slt(sv2));
382 EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2));
383 EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2));
384 EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2));
388 TEST(APIntTest, compareWithRawIntegers) {
389 EXPECT_TRUE(!APInt(8, 1).uge(256));
390 EXPECT_TRUE(!APInt(8, 1).ugt(256));
391 EXPECT_TRUE( APInt(8, 1).ule(256));
392 EXPECT_TRUE( APInt(8, 1).ult(256));
393 EXPECT_TRUE(!APInt(8, 1).sge(256));
394 EXPECT_TRUE(!APInt(8, 1).sgt(256));
395 EXPECT_TRUE( APInt(8, 1).sle(256));
396 EXPECT_TRUE( APInt(8, 1).slt(256));
397 EXPECT_TRUE(!(APInt(8, 0) == 256));
398 EXPECT_TRUE( APInt(8, 0) != 256);
399 EXPECT_TRUE(!(APInt(8, 1) == 256));
400 EXPECT_TRUE( APInt(8, 1) != 256);
402 auto uint64max = UINT64_MAX;
403 auto int64max = INT64_MAX;
404 auto int64min = INT64_MIN;
406 auto u64 = APInt{128, uint64max};
407 auto s64 = APInt{128, static_cast<uint64_t>(int64max), true};
408 auto big = u64 + 1;
410 EXPECT_TRUE( u64.uge(uint64max));
411 EXPECT_TRUE(!u64.ugt(uint64max));
412 EXPECT_TRUE( u64.ule(uint64max));
413 EXPECT_TRUE(!u64.ult(uint64max));
414 EXPECT_TRUE( u64.sge(int64max));
415 EXPECT_TRUE( u64.sgt(int64max));
416 EXPECT_TRUE(!u64.sle(int64max));
417 EXPECT_TRUE(!u64.slt(int64max));
418 EXPECT_TRUE( u64.sge(int64min));
419 EXPECT_TRUE( u64.sgt(int64min));
420 EXPECT_TRUE(!u64.sle(int64min));
421 EXPECT_TRUE(!u64.slt(int64min));
423 EXPECT_TRUE(u64 == uint64max);
424 EXPECT_TRUE(u64 != int64max);
425 EXPECT_TRUE(u64 != int64min);
427 EXPECT_TRUE(!s64.uge(uint64max));
428 EXPECT_TRUE(!s64.ugt(uint64max));
429 EXPECT_TRUE( s64.ule(uint64max));
430 EXPECT_TRUE( s64.ult(uint64max));
431 EXPECT_TRUE( s64.sge(int64max));
432 EXPECT_TRUE(!s64.sgt(int64max));
433 EXPECT_TRUE( s64.sle(int64max));
434 EXPECT_TRUE(!s64.slt(int64max));
435 EXPECT_TRUE( s64.sge(int64min));
436 EXPECT_TRUE( s64.sgt(int64min));
437 EXPECT_TRUE(!s64.sle(int64min));
438 EXPECT_TRUE(!s64.slt(int64min));
440 EXPECT_TRUE(s64 != uint64max);
441 EXPECT_TRUE(s64 == int64max);
442 EXPECT_TRUE(s64 != int64min);
444 EXPECT_TRUE( big.uge(uint64max));
445 EXPECT_TRUE( big.ugt(uint64max));
446 EXPECT_TRUE(!big.ule(uint64max));
447 EXPECT_TRUE(!big.ult(uint64max));
448 EXPECT_TRUE( big.sge(int64max));
449 EXPECT_TRUE( big.sgt(int64max));
450 EXPECT_TRUE(!big.sle(int64max));
451 EXPECT_TRUE(!big.slt(int64max));
452 EXPECT_TRUE( big.sge(int64min));
453 EXPECT_TRUE( big.sgt(int64min));
454 EXPECT_TRUE(!big.sle(int64min));
455 EXPECT_TRUE(!big.slt(int64min));
457 EXPECT_TRUE(big != uint64max);
458 EXPECT_TRUE(big != int64max);
459 EXPECT_TRUE(big != int64min);
462 TEST(APIntTest, compareWithInt64Min) {
463 int64_t edge = INT64_MIN;
464 int64_t edgeP1 = edge + 1;
465 int64_t edgeM1 = INT64_MAX;
466 auto a = APInt{64, static_cast<uint64_t>(edge), true};
468 EXPECT_TRUE(!a.slt(edge));
469 EXPECT_TRUE( a.sle(edge));
470 EXPECT_TRUE(!a.sgt(edge));
471 EXPECT_TRUE( a.sge(edge));
472 EXPECT_TRUE( a.slt(edgeP1));
473 EXPECT_TRUE( a.sle(edgeP1));
474 EXPECT_TRUE(!a.sgt(edgeP1));
475 EXPECT_TRUE(!a.sge(edgeP1));
476 EXPECT_TRUE( a.slt(edgeM1));
477 EXPECT_TRUE( a.sle(edgeM1));
478 EXPECT_TRUE(!a.sgt(edgeM1));
479 EXPECT_TRUE(!a.sge(edgeM1));
482 TEST(APIntTest, compareWithHalfInt64Max) {
483 uint64_t edge = 0x4000000000000000;
484 uint64_t edgeP1 = edge + 1;
485 uint64_t edgeM1 = edge - 1;
486 auto a = APInt{64, edge};
488 EXPECT_TRUE(!a.ult(edge));
489 EXPECT_TRUE( a.ule(edge));
490 EXPECT_TRUE(!a.ugt(edge));
491 EXPECT_TRUE( a.uge(edge));
492 EXPECT_TRUE( a.ult(edgeP1));
493 EXPECT_TRUE( a.ule(edgeP1));
494 EXPECT_TRUE(!a.ugt(edgeP1));
495 EXPECT_TRUE(!a.uge(edgeP1));
496 EXPECT_TRUE(!a.ult(edgeM1));
497 EXPECT_TRUE(!a.ule(edgeM1));
498 EXPECT_TRUE( a.ugt(edgeM1));
499 EXPECT_TRUE( a.uge(edgeM1));
501 EXPECT_TRUE(!a.slt(edge));
502 EXPECT_TRUE( a.sle(edge));
503 EXPECT_TRUE(!a.sgt(edge));
504 EXPECT_TRUE( a.sge(edge));
505 EXPECT_TRUE( a.slt(edgeP1));
506 EXPECT_TRUE( a.sle(edgeP1));
507 EXPECT_TRUE(!a.sgt(edgeP1));
508 EXPECT_TRUE(!a.sge(edgeP1));
509 EXPECT_TRUE(!a.slt(edgeM1));
510 EXPECT_TRUE(!a.sle(edgeM1));
511 EXPECT_TRUE( a.sgt(edgeM1));
512 EXPECT_TRUE( a.sge(edgeM1));
515 TEST(APIntTest, compareLargeIntegers) {
516 // Make sure all the combinations of signed comparisons work with big ints.
517 auto One = APInt{128, static_cast<uint64_t>(1), true};
518 auto Two = APInt{128, static_cast<uint64_t>(2), true};
519 auto MinusOne = APInt{128, static_cast<uint64_t>(-1), true};
520 auto MinusTwo = APInt{128, static_cast<uint64_t>(-2), true};
522 EXPECT_TRUE(!One.slt(One));
523 EXPECT_TRUE(!Two.slt(One));
524 EXPECT_TRUE(MinusOne.slt(One));
525 EXPECT_TRUE(MinusTwo.slt(One));
527 EXPECT_TRUE(One.slt(Two));
528 EXPECT_TRUE(!Two.slt(Two));
529 EXPECT_TRUE(MinusOne.slt(Two));
530 EXPECT_TRUE(MinusTwo.slt(Two));
532 EXPECT_TRUE(!One.slt(MinusOne));
533 EXPECT_TRUE(!Two.slt(MinusOne));
534 EXPECT_TRUE(!MinusOne.slt(MinusOne));
535 EXPECT_TRUE(MinusTwo.slt(MinusOne));
537 EXPECT_TRUE(!One.slt(MinusTwo));
538 EXPECT_TRUE(!Two.slt(MinusTwo));
539 EXPECT_TRUE(!MinusOne.slt(MinusTwo));
540 EXPECT_TRUE(!MinusTwo.slt(MinusTwo));
543 TEST(APIntTest, binaryOpsWithRawIntegers) {
544 // Single word check.
545 uint64_t E1 = 0x2CA7F46BF6569915ULL;
546 APInt A1(64, E1);
548 EXPECT_EQ(A1 & E1, E1);
549 EXPECT_EQ(A1 & 0, 0);
550 EXPECT_EQ(A1 & 1, 1);
551 EXPECT_EQ(A1 & 5, 5);
552 EXPECT_EQ(A1 & UINT64_MAX, E1);
554 EXPECT_EQ(A1 | E1, E1);
555 EXPECT_EQ(A1 | 0, E1);
556 EXPECT_EQ(A1 | 1, E1);
557 EXPECT_EQ(A1 | 2, E1 | 2);
558 EXPECT_EQ(A1 | UINT64_MAX, UINT64_MAX);
560 EXPECT_EQ(A1 ^ E1, 0);
561 EXPECT_EQ(A1 ^ 0, E1);
562 EXPECT_EQ(A1 ^ 1, E1 ^ 1);
563 EXPECT_EQ(A1 ^ 7, E1 ^ 7);
564 EXPECT_EQ(A1 ^ UINT64_MAX, ~E1);
566 // Multiword check.
567 uint64_t N = 0xEB6EB136591CBA21ULL;
568 APInt::WordType E2[4] = {
570 0x7B9358BD6A33F10AULL,
571 0x7E7FFA5EADD8846ULL,
572 0x305F341CA00B613DULL
574 APInt A2(APInt::APINT_BITS_PER_WORD*4, E2);
576 EXPECT_EQ(A2 & N, N);
577 EXPECT_EQ(A2 & 0, 0);
578 EXPECT_EQ(A2 & 1, 1);
579 EXPECT_EQ(A2 & 5, 1);
580 EXPECT_EQ(A2 & UINT64_MAX, N);
582 EXPECT_EQ(A2 | N, A2);
583 EXPECT_EQ(A2 | 0, A2);
584 EXPECT_EQ(A2 | 1, A2);
585 EXPECT_EQ(A2 | 2, A2 + 2);
586 EXPECT_EQ(A2 | UINT64_MAX, A2 - N + UINT64_MAX);
588 EXPECT_EQ(A2 ^ N, A2 - N);
589 EXPECT_EQ(A2 ^ 0, A2);
590 EXPECT_EQ(A2 ^ 1, A2 - 1);
591 EXPECT_EQ(A2 ^ 7, A2 + 5);
592 EXPECT_EQ(A2 ^ UINT64_MAX, A2 - N + ~N);
595 TEST(APIntTest, rvalue_arithmetic) {
596 // Test all combinations of lvalue/rvalue lhs/rhs of add/sub
598 // Lamdba to return an APInt by value, but also provide the raw value of the
599 // allocated data.
600 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
601 APInt V(129, HexString, 16);
602 RawData = V.getRawData();
603 return V;
606 APInt One(129, "1", 16);
607 APInt Two(129, "2", 16);
608 APInt Three(129, "3", 16);
609 APInt MinusOne = -One;
611 const uint64_t *RawDataL = nullptr;
612 const uint64_t *RawDataR = nullptr;
615 // 1 + 1 = 2
616 APInt AddLL = One + One;
617 EXPECT_EQ(AddLL, Two);
619 APInt AddLR = One + getRValue("1", RawDataR);
620 EXPECT_EQ(AddLR, Two);
621 EXPECT_EQ(AddLR.getRawData(), RawDataR);
623 APInt AddRL = getRValue("1", RawDataL) + One;
624 EXPECT_EQ(AddRL, Two);
625 EXPECT_EQ(AddRL.getRawData(), RawDataL);
627 APInt AddRR = getRValue("1", RawDataL) + getRValue("1", RawDataR);
628 EXPECT_EQ(AddRR, Two);
629 EXPECT_EQ(AddRR.getRawData(), RawDataR);
631 // LValue's and constants
632 APInt AddLK = One + 1;
633 EXPECT_EQ(AddLK, Two);
635 APInt AddKL = 1 + One;
636 EXPECT_EQ(AddKL, Two);
638 // RValue's and constants
639 APInt AddRK = getRValue("1", RawDataL) + 1;
640 EXPECT_EQ(AddRK, Two);
641 EXPECT_EQ(AddRK.getRawData(), RawDataL);
643 APInt AddKR = 1 + getRValue("1", RawDataR);
644 EXPECT_EQ(AddKR, Two);
645 EXPECT_EQ(AddKR.getRawData(), RawDataR);
649 // 0x0,FFFF...FFFF + 0x2 = 0x100...0001
650 APInt AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
651 APInt HighOneLowOne(129, "100000000000000000000000000000001", 16);
653 APInt AddLL = AllOnes + Two;
654 EXPECT_EQ(AddLL, HighOneLowOne);
656 APInt AddLR = AllOnes + getRValue("2", RawDataR);
657 EXPECT_EQ(AddLR, HighOneLowOne);
658 EXPECT_EQ(AddLR.getRawData(), RawDataR);
660 APInt AddRL = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) + Two;
661 EXPECT_EQ(AddRL, HighOneLowOne);
662 EXPECT_EQ(AddRL.getRawData(), RawDataL);
664 APInt AddRR = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) +
665 getRValue("2", RawDataR);
666 EXPECT_EQ(AddRR, HighOneLowOne);
667 EXPECT_EQ(AddRR.getRawData(), RawDataR);
669 // LValue's and constants
670 APInt AddLK = AllOnes + 2;
671 EXPECT_EQ(AddLK, HighOneLowOne);
673 APInt AddKL = 2 + AllOnes;
674 EXPECT_EQ(AddKL, HighOneLowOne);
676 // RValue's and constants
677 APInt AddRK = getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataL) + 2;
678 EXPECT_EQ(AddRK, HighOneLowOne);
679 EXPECT_EQ(AddRK.getRawData(), RawDataL);
681 APInt AddKR = 2 + getRValue("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
682 EXPECT_EQ(AddKR, HighOneLowOne);
683 EXPECT_EQ(AddKR.getRawData(), RawDataR);
687 // 2 - 1 = 1
688 APInt SubLL = Two - One;
689 EXPECT_EQ(SubLL, One);
691 APInt SubLR = Two - getRValue("1", RawDataR);
692 EXPECT_EQ(SubLR, One);
693 EXPECT_EQ(SubLR.getRawData(), RawDataR);
695 APInt SubRL = getRValue("2", RawDataL) - One;
696 EXPECT_EQ(SubRL, One);
697 EXPECT_EQ(SubRL.getRawData(), RawDataL);
699 APInt SubRR = getRValue("2", RawDataL) - getRValue("1", RawDataR);
700 EXPECT_EQ(SubRR, One);
701 EXPECT_EQ(SubRR.getRawData(), RawDataR);
703 // LValue's and constants
704 APInt SubLK = Two - 1;
705 EXPECT_EQ(SubLK, One);
707 APInt SubKL = 2 - One;
708 EXPECT_EQ(SubKL, One);
710 // RValue's and constants
711 APInt SubRK = getRValue("2", RawDataL) - 1;
712 EXPECT_EQ(SubRK, One);
713 EXPECT_EQ(SubRK.getRawData(), RawDataL);
715 APInt SubKR = 2 - getRValue("1", RawDataR);
716 EXPECT_EQ(SubKR, One);
717 EXPECT_EQ(SubKR.getRawData(), RawDataR);
721 // 0x100...0001 - 0x0,FFFF...FFFF = 0x2
722 APInt AllOnes(129, "0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
723 APInt HighOneLowOne(129, "100000000000000000000000000000001", 16);
725 APInt SubLL = HighOneLowOne - AllOnes;
726 EXPECT_EQ(SubLL, Two);
728 APInt SubLR = HighOneLowOne -
729 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
730 EXPECT_EQ(SubLR, Two);
731 EXPECT_EQ(SubLR.getRawData(), RawDataR);
733 APInt SubRL = getRValue("100000000000000000000000000000001", RawDataL) -
734 AllOnes;
735 EXPECT_EQ(SubRL, Two);
736 EXPECT_EQ(SubRL.getRawData(), RawDataL);
738 APInt SubRR = getRValue("100000000000000000000000000000001", RawDataL) -
739 getRValue("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
740 EXPECT_EQ(SubRR, Two);
741 EXPECT_EQ(SubRR.getRawData(), RawDataR);
743 // LValue's and constants
744 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
745 APInt SubLK = HighOneLowOne - 2;
746 EXPECT_EQ(SubLK, AllOnes);
748 // 2 - (-1) = 3
749 APInt SubKL = 2 - MinusOne;
750 EXPECT_EQ(SubKL, Three);
752 // RValue's and constants
753 // 0x100...0001 - 0x2 = 0x0,FFFF...FFFF
754 APInt SubRK = getRValue("100000000000000000000000000000001", RawDataL) - 2;
755 EXPECT_EQ(SubRK, AllOnes);
756 EXPECT_EQ(SubRK.getRawData(), RawDataL);
758 APInt SubKR = 2 - getRValue("1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", RawDataR);
759 EXPECT_EQ(SubKR, Three);
760 EXPECT_EQ(SubKR.getRawData(), RawDataR);
764 TEST(APIntTest, rvalue_bitwise) {
765 // Test all combinations of lvalue/rvalue lhs/rhs of and/or/xor
767 // Lamdba to return an APInt by value, but also provide the raw value of the
768 // allocated data.
769 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
770 APInt V(129, HexString, 16);
771 RawData = V.getRawData();
772 return V;
775 APInt Ten(129, "A", 16);
776 APInt Twelve(129, "C", 16);
778 const uint64_t *RawDataL = nullptr;
779 const uint64_t *RawDataR = nullptr;
782 // 12 & 10 = 8
783 APInt AndLL = Ten & Twelve;
784 EXPECT_EQ(AndLL, 0x8);
786 APInt AndLR = Ten & getRValue("C", RawDataR);
787 EXPECT_EQ(AndLR, 0x8);
788 EXPECT_EQ(AndLR.getRawData(), RawDataR);
790 APInt AndRL = getRValue("A", RawDataL) & Twelve;
791 EXPECT_EQ(AndRL, 0x8);
792 EXPECT_EQ(AndRL.getRawData(), RawDataL);
794 APInt AndRR = getRValue("A", RawDataL) & getRValue("C", RawDataR);
795 EXPECT_EQ(AndRR, 0x8);
796 EXPECT_EQ(AndRR.getRawData(), RawDataR);
798 // LValue's and constants
799 APInt AndLK = Ten & 0xc;
800 EXPECT_EQ(AndLK, 0x8);
802 APInt AndKL = 0xa & Twelve;
803 EXPECT_EQ(AndKL, 0x8);
805 // RValue's and constants
806 APInt AndRK = getRValue("A", RawDataL) & 0xc;
807 EXPECT_EQ(AndRK, 0x8);
808 EXPECT_EQ(AndRK.getRawData(), RawDataL);
810 APInt AndKR = 0xa & getRValue("C", RawDataR);
811 EXPECT_EQ(AndKR, 0x8);
812 EXPECT_EQ(AndKR.getRawData(), RawDataR);
816 // 12 | 10 = 14
817 APInt OrLL = Ten | Twelve;
818 EXPECT_EQ(OrLL, 0xe);
820 APInt OrLR = Ten | getRValue("C", RawDataR);
821 EXPECT_EQ(OrLR, 0xe);
822 EXPECT_EQ(OrLR.getRawData(), RawDataR);
824 APInt OrRL = getRValue("A", RawDataL) | Twelve;
825 EXPECT_EQ(OrRL, 0xe);
826 EXPECT_EQ(OrRL.getRawData(), RawDataL);
828 APInt OrRR = getRValue("A", RawDataL) | getRValue("C", RawDataR);
829 EXPECT_EQ(OrRR, 0xe);
830 EXPECT_EQ(OrRR.getRawData(), RawDataR);
832 // LValue's and constants
833 APInt OrLK = Ten | 0xc;
834 EXPECT_EQ(OrLK, 0xe);
836 APInt OrKL = 0xa | Twelve;
837 EXPECT_EQ(OrKL, 0xe);
839 // RValue's and constants
840 APInt OrRK = getRValue("A", RawDataL) | 0xc;
841 EXPECT_EQ(OrRK, 0xe);
842 EXPECT_EQ(OrRK.getRawData(), RawDataL);
844 APInt OrKR = 0xa | getRValue("C", RawDataR);
845 EXPECT_EQ(OrKR, 0xe);
846 EXPECT_EQ(OrKR.getRawData(), RawDataR);
850 // 12 ^ 10 = 6
851 APInt XorLL = Ten ^ Twelve;
852 EXPECT_EQ(XorLL, 0x6);
854 APInt XorLR = Ten ^ getRValue("C", RawDataR);
855 EXPECT_EQ(XorLR, 0x6);
856 EXPECT_EQ(XorLR.getRawData(), RawDataR);
858 APInt XorRL = getRValue("A", RawDataL) ^ Twelve;
859 EXPECT_EQ(XorRL, 0x6);
860 EXPECT_EQ(XorRL.getRawData(), RawDataL);
862 APInt XorRR = getRValue("A", RawDataL) ^ getRValue("C", RawDataR);
863 EXPECT_EQ(XorRR, 0x6);
864 EXPECT_EQ(XorRR.getRawData(), RawDataR);
866 // LValue's and constants
867 APInt XorLK = Ten ^ 0xc;
868 EXPECT_EQ(XorLK, 0x6);
870 APInt XorKL = 0xa ^ Twelve;
871 EXPECT_EQ(XorKL, 0x6);
873 // RValue's and constants
874 APInt XorRK = getRValue("A", RawDataL) ^ 0xc;
875 EXPECT_EQ(XorRK, 0x6);
876 EXPECT_EQ(XorRK.getRawData(), RawDataL);
878 APInt XorKR = 0xa ^ getRValue("C", RawDataR);
879 EXPECT_EQ(XorKR, 0x6);
880 EXPECT_EQ(XorKR.getRawData(), RawDataR);
884 TEST(APIntTest, rvalue_invert) {
885 // Lamdba to return an APInt by value, but also provide the raw value of the
886 // allocated data.
887 auto getRValue = [](const char *HexString, uint64_t const *&RawData) {
888 APInt V(129, HexString, 16);
889 RawData = V.getRawData();
890 return V;
893 APInt One(129, 1);
894 APInt NegativeTwo(129, -2ULL, true);
896 const uint64_t *RawData = nullptr;
899 // ~1 = -2
900 APInt NegL = ~One;
901 EXPECT_EQ(NegL, NegativeTwo);
903 APInt NegR = ~getRValue("1", RawData);
904 EXPECT_EQ(NegR, NegativeTwo);
905 EXPECT_EQ(NegR.getRawData(), RawData);
909 // Tests different div/rem varaints using scheme (a * b + c) / a
910 void testDiv(APInt a, APInt b, APInt c) {
911 ASSERT_TRUE(a.uge(b)); // Must: a >= b
912 ASSERT_TRUE(a.ugt(c)); // Must: a > c
914 auto p = a * b + c;
916 auto q = p.udiv(a);
917 auto r = p.urem(a);
918 EXPECT_EQ(b, q);
919 EXPECT_EQ(c, r);
920 APInt::udivrem(p, a, q, r);
921 EXPECT_EQ(b, q);
922 EXPECT_EQ(c, r);
923 q = p.sdiv(a);
924 r = p.srem(a);
925 EXPECT_EQ(b, q);
926 EXPECT_EQ(c, r);
927 APInt::sdivrem(p, a, q, r);
928 EXPECT_EQ(b, q);
929 EXPECT_EQ(c, r);
931 if (b.ugt(c)) { // Test also symmetric case
932 q = p.udiv(b);
933 r = p.urem(b);
934 EXPECT_EQ(a, q);
935 EXPECT_EQ(c, r);
936 APInt::udivrem(p, b, q, r);
937 EXPECT_EQ(a, q);
938 EXPECT_EQ(c, r);
939 q = p.sdiv(b);
940 r = p.srem(b);
941 EXPECT_EQ(a, q);
942 EXPECT_EQ(c, r);
943 APInt::sdivrem(p, b, q, r);
944 EXPECT_EQ(a, q);
945 EXPECT_EQ(c, r);
949 TEST(APIntTest, divrem_big1) {
950 // Tests KnuthDiv rare step D6
951 testDiv({256, "1ffffffffffffffff", 16},
952 {256, "1ffffffffffffffff", 16},
953 {256, 0});
956 TEST(APIntTest, divrem_big2) {
957 // Tests KnuthDiv rare step D6
958 testDiv({1024, "112233ceff"
959 "cecece000000ffffffffffffffffffff"
960 "ffffffffffffffffffffffffffffffff"
961 "ffffffffffffffffffffffffffffffff"
962 "ffffffffffffffffffffffffffffff33", 16},
963 {1024, "111111ffffffffffffffff"
964 "ffffffffffffffffffffffffffffffff"
965 "fffffffffffffffffffffffffffffccf"
966 "ffffffffffffffffffffffffffffff00", 16},
967 {1024, 7919});
970 TEST(APIntTest, divrem_big3) {
971 // Tests KnuthDiv case without shift
972 testDiv({256, "80000001ffffffffffffffff", 16},
973 {256, "ffffffffffffff0000000", 16},
974 {256, 4219});
977 TEST(APIntTest, divrem_big4) {
978 // Tests heap allocation in divide() enfoced by huge numbers
979 testDiv(APInt{4096, 5}.shl(2001),
980 APInt{4096, 1}.shl(2000),
981 APInt{4096, 4219*13});
984 TEST(APIntTest, divrem_big5) {
985 // Tests one word divisor case of divide()
986 testDiv(APInt{1024, 19}.shl(811),
987 APInt{1024, 4356013}, // one word
988 APInt{1024, 1});
991 TEST(APIntTest, divrem_big6) {
992 // Tests some rare "borrow" cases in D4 step
993 testDiv(APInt{512, "ffffffffffffffff00000000000000000000000001", 16},
994 APInt{512, "10000000000000001000000000000001", 16},
995 APInt{512, "10000000000000000000000000000000", 16});
998 TEST(APIntTest, divrem_big7) {
999 // Yet another test for KnuthDiv rare step D6.
1000 testDiv({224, "800000008000000200000005", 16},
1001 {224, "fffffffd", 16},
1002 {224, "80000000800000010000000f", 16});
1005 void testDiv(APInt a, uint64_t b, APInt c) {
1006 auto p = a * b + c;
1008 APInt q;
1009 uint64_t r;
1010 // Unsigned division will only work if our original number wasn't negative.
1011 if (!a.isNegative()) {
1012 q = p.udiv(b);
1013 r = p.urem(b);
1014 EXPECT_EQ(a, q);
1015 EXPECT_EQ(c, r);
1016 APInt::udivrem(p, b, q, r);
1017 EXPECT_EQ(a, q);
1018 EXPECT_EQ(c, r);
1020 q = p.sdiv(b);
1021 r = p.srem(b);
1022 EXPECT_EQ(a, q);
1023 if (c.isNegative())
1024 EXPECT_EQ(-c, -r); // Need to negate so the uint64_t compare will work.
1025 else
1026 EXPECT_EQ(c, r);
1027 int64_t sr;
1028 APInt::sdivrem(p, b, q, sr);
1029 EXPECT_EQ(a, q);
1030 if (c.isNegative())
1031 EXPECT_EQ(-c, -sr); // Need to negate so the uint64_t compare will work.
1032 else
1033 EXPECT_EQ(c, sr);
1036 TEST(APIntTest, divremuint) {
1037 // Single word APInt
1038 testDiv(APInt{64, 9},
1040 APInt{64, 1});
1042 // Single word negative APInt
1043 testDiv(-APInt{64, 9},
1045 -APInt{64, 1});
1047 // Multiword dividend with only one significant word.
1048 testDiv(APInt{256, 9},
1050 APInt{256, 1});
1052 // Negative dividend.
1053 testDiv(-APInt{256, 9},
1055 -APInt{256, 1});
1057 // Multiword dividend
1058 testDiv(APInt{1024, 19}.shl(811),
1059 4356013, // one word
1060 APInt{1024, 1});
1063 TEST(APIntTest, divrem_simple) {
1064 // Test simple cases.
1065 APInt A(65, 2), B(65, 2);
1066 APInt Q, R;
1068 // X / X
1069 APInt::sdivrem(A, B, Q, R);
1070 EXPECT_EQ(Q, APInt(65, 1));
1071 EXPECT_EQ(R, APInt(65, 0));
1072 APInt::udivrem(A, B, Q, R);
1073 EXPECT_EQ(Q, APInt(65, 1));
1074 EXPECT_EQ(R, APInt(65, 0));
1076 // 0 / X
1077 APInt O(65, 0);
1078 APInt::sdivrem(O, B, Q, R);
1079 EXPECT_EQ(Q, APInt(65, 0));
1080 EXPECT_EQ(R, APInt(65, 0));
1081 APInt::udivrem(O, B, Q, R);
1082 EXPECT_EQ(Q, APInt(65, 0));
1083 EXPECT_EQ(R, APInt(65, 0));
1085 // X / 1
1086 APInt I(65, 1);
1087 APInt::sdivrem(A, I, Q, R);
1088 EXPECT_EQ(Q, A);
1089 EXPECT_EQ(R, APInt(65, 0));
1090 APInt::udivrem(A, I, Q, R);
1091 EXPECT_EQ(Q, A);
1092 EXPECT_EQ(R, APInt(65, 0));
1095 TEST(APIntTest, fromString) {
1096 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2));
1097 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2));
1098 EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2));
1099 EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2));
1100 EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
1102 EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2));
1103 EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2));
1104 EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2));
1105 EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2));
1106 EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
1108 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2));
1109 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2));
1110 EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2));
1111 EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2));
1112 EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
1114 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8));
1115 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8));
1116 EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8));
1117 EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8));
1118 EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8));
1119 EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8));
1121 EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8));
1122 EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8));
1123 EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8));
1124 EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8));
1125 EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8));
1126 EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8));
1128 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8));
1129 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8));
1130 EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8));
1131 EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8));
1132 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8));
1133 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8));
1135 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10));
1136 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10));
1137 EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10));
1138 EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
1139 EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
1140 EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
1142 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10));
1143 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10));
1144 EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10));
1145 EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
1146 EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
1147 EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
1149 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16));
1150 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16));
1151 EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16));
1152 EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
1153 EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
1154 EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
1156 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16));
1157 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16));
1158 EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16));
1159 EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
1160 EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
1161 EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
1163 EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36));
1164 EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36));
1165 EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36));
1166 EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
1167 EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
1168 EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
1170 EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36));
1171 EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36));
1172 EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36));
1173 EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
1174 EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
1175 EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
1178 TEST(APIntTest, SaturatingMath) {
1179 APInt AP_10 = APInt(8, 10);
1180 APInt AP_42 = APInt(8, 42);
1181 APInt AP_100 = APInt(8, 100);
1182 APInt AP_200 = APInt(8, 200);
1184 EXPECT_EQ(APInt(8, 100), AP_100.truncUSat(8));
1185 EXPECT_EQ(APInt(7, 100), AP_100.truncUSat(7));
1186 EXPECT_EQ(APInt(6, 63), AP_100.truncUSat(6));
1187 EXPECT_EQ(APInt(5, 31), AP_100.truncUSat(5));
1189 EXPECT_EQ(APInt(8, 200), AP_200.truncUSat(8));
1190 EXPECT_EQ(APInt(7, 127), AP_200.truncUSat(7));
1191 EXPECT_EQ(APInt(6, 63), AP_200.truncUSat(6));
1192 EXPECT_EQ(APInt(5, 31), AP_200.truncUSat(5));
1194 EXPECT_EQ(APInt(8, 42), AP_42.truncSSat(8));
1195 EXPECT_EQ(APInt(7, 42), AP_42.truncSSat(7));
1196 EXPECT_EQ(APInt(6, 31), AP_42.truncSSat(6));
1197 EXPECT_EQ(APInt(5, 15), AP_42.truncSSat(5));
1199 EXPECT_EQ(APInt(8, -56), AP_200.truncSSat(8));
1200 EXPECT_EQ(APInt(7, -56), AP_200.truncSSat(7));
1201 EXPECT_EQ(APInt(6, -32), AP_200.truncSSat(6));
1202 EXPECT_EQ(APInt(5, -16), AP_200.truncSSat(5));
1204 EXPECT_EQ(APInt(8, 200), AP_100.uadd_sat(AP_100));
1205 EXPECT_EQ(APInt(8, 255), AP_100.uadd_sat(AP_200));
1206 EXPECT_EQ(APInt(8, 255), APInt(8, 255).uadd_sat(APInt(8, 255)));
1208 EXPECT_EQ(APInt(8, 110), AP_10.sadd_sat(AP_100));
1209 EXPECT_EQ(APInt(8, 127), AP_100.sadd_sat(AP_100));
1210 EXPECT_EQ(APInt(8, -128), (-AP_100).sadd_sat(-AP_100));
1211 EXPECT_EQ(APInt(8, -128), APInt(8, -128).sadd_sat(APInt(8, -128)));
1213 EXPECT_EQ(APInt(8, 90), AP_100.usub_sat(AP_10));
1214 EXPECT_EQ(APInt(8, 0), AP_100.usub_sat(AP_200));
1215 EXPECT_EQ(APInt(8, 0), APInt(8, 0).usub_sat(APInt(8, 255)));
1217 EXPECT_EQ(APInt(8, -90), AP_10.ssub_sat(AP_100));
1218 EXPECT_EQ(APInt(8, 127), AP_100.ssub_sat(-AP_100));
1219 EXPECT_EQ(APInt(8, -128), (-AP_100).ssub_sat(AP_100));
1220 EXPECT_EQ(APInt(8, -128), APInt(8, -128).ssub_sat(APInt(8, 127)));
1222 EXPECT_EQ(APInt(8, 250), APInt(8, 50).umul_sat(APInt(8, 5)));
1223 EXPECT_EQ(APInt(8, 255), APInt(8, 50).umul_sat(APInt(8, 6)));
1224 EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, 3)));
1225 EXPECT_EQ(APInt(8, 255), APInt(8, 3).umul_sat(APInt(8, -128)));
1226 EXPECT_EQ(APInt(8, 255), APInt(8, -128).umul_sat(APInt(8, -128)));
1228 EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5)));
1229 EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6)));
1230 EXPECT_EQ(APInt(8, 127), APInt(8, 127).smul_sat(APInt(8, 127)));
1231 EXPECT_EQ(APInt(8, -125), APInt(8, -25).smul_sat(APInt(8, 5)));
1232 EXPECT_EQ(APInt(8, -125), APInt(8, 25).smul_sat(APInt(8, -5)));
1233 EXPECT_EQ(APInt(8, 125), APInt(8, -25).smul_sat(APInt(8, -5)));
1234 EXPECT_EQ(APInt(8, 125), APInt(8, 25).smul_sat(APInt(8, 5)));
1235 EXPECT_EQ(APInt(8, -128), APInt(8, -25).smul_sat(APInt(8, 6)));
1236 EXPECT_EQ(APInt(8, -128), APInt(8, 25).smul_sat(APInt(8, -6)));
1237 EXPECT_EQ(APInt(8, 127), APInt(8, -25).smul_sat(APInt(8, -6)));
1238 EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6)));
1240 EXPECT_EQ(APInt(8, 128), APInt(8, 4).ushl_sat(APInt(8, 5)));
1241 EXPECT_EQ(APInt(8, 255), APInt(8, 4).ushl_sat(APInt(8, 6)));
1242 EXPECT_EQ(APInt(8, 128), APInt(8, 1).ushl_sat(APInt(8, 7)));
1243 EXPECT_EQ(APInt(8, 255), APInt(8, 1).ushl_sat(APInt(8, 8)));
1244 EXPECT_EQ(APInt(8, 255), APInt(8, -128).ushl_sat(APInt(8, 2)));
1245 EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, 2)));
1246 EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, -2)));
1248 EXPECT_EQ(APInt(8, 64), APInt(8, 4).sshl_sat(APInt(8, 4)));
1249 EXPECT_EQ(APInt(8, 127), APInt(8, 4).sshl_sat(APInt(8, 5)));
1250 EXPECT_EQ(APInt(8, 127), APInt(8, 1).sshl_sat(APInt(8, 8)));
1251 EXPECT_EQ(APInt(8, -64), APInt(8, -4).sshl_sat(APInt(8, 4)));
1252 EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 5)));
1253 EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 6)));
1254 EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 7)));
1255 EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 8)));
1258 TEST(APIntTest, FromArray) {
1259 EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
1262 TEST(APIntTest, StringBitsNeeded2) {
1263 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2));
1264 EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2));
1265 EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
1266 EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
1267 EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
1269 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2));
1270 EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2));
1271 EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
1272 EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
1273 EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
1275 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2));
1276 EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2));
1277 EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
1278 EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
1279 EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
1282 TEST(APIntTest, StringBitsNeeded8) {
1283 EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
1284 EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
1285 EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
1286 EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
1287 EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
1289 EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
1290 EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
1291 EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
1292 EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
1293 EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
1295 EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
1296 EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
1297 EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
1298 EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
1299 EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
1302 TEST(APIntTest, StringBitsNeeded10) {
1303 EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
1304 EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
1305 EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
1306 EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
1307 EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
1308 EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
1310 EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
1311 EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
1312 EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
1313 EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
1314 EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
1316 EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
1317 EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
1318 EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
1319 EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
1320 EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
1322 EXPECT_EQ(1U, APInt::getBitsNeeded("-1", 10));
1323 EXPECT_EQ(2U, APInt::getBitsNeeded("-2", 10));
1324 EXPECT_EQ(3U, APInt::getBitsNeeded("-4", 10));
1325 EXPECT_EQ(4U, APInt::getBitsNeeded("-8", 10));
1326 EXPECT_EQ(5U, APInt::getBitsNeeded("-16", 10));
1327 EXPECT_EQ(6U, APInt::getBitsNeeded("-23", 10));
1328 EXPECT_EQ(6U, APInt::getBitsNeeded("-32", 10));
1329 EXPECT_EQ(7U, APInt::getBitsNeeded("-64", 10));
1330 EXPECT_EQ(8U, APInt::getBitsNeeded("-127", 10));
1331 EXPECT_EQ(8U, APInt::getBitsNeeded("-128", 10));
1332 EXPECT_EQ(9U, APInt::getBitsNeeded("-255", 10));
1333 EXPECT_EQ(9U, APInt::getBitsNeeded("-256", 10));
1334 EXPECT_EQ(10U, APInt::getBitsNeeded("-512", 10));
1335 EXPECT_EQ(11U, APInt::getBitsNeeded("-1024", 10));
1336 EXPECT_EQ(12U, APInt::getBitsNeeded("-1025", 10));
1339 TEST(APIntTest, StringBitsNeeded16) {
1340 EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
1341 EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
1342 EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
1343 EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
1344 EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
1346 EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
1347 EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
1348 EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
1349 EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
1350 EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
1352 EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
1353 EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
1354 EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
1355 EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
1356 EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
1359 TEST(APIntTest, toString) {
1360 SmallString<16> S;
1361 bool isSigned;
1363 APInt(8, 0).toString(S, 2, true, true);
1364 EXPECT_EQ(std::string(S), "0b0");
1365 S.clear();
1366 APInt(8, 0).toString(S, 8, true, true);
1367 EXPECT_EQ(std::string(S), "00");
1368 S.clear();
1369 APInt(8, 0).toString(S, 10, true, true);
1370 EXPECT_EQ(std::string(S), "0");
1371 S.clear();
1372 APInt(8, 0).toString(S, 16, true, true);
1373 EXPECT_EQ(std::string(S), "0x0");
1374 S.clear();
1375 APInt(8, 0).toString(S, 36, true, false);
1376 EXPECT_EQ(std::string(S), "0");
1377 S.clear();
1379 isSigned = false;
1380 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
1381 EXPECT_EQ(std::string(S), "0b11111111");
1382 S.clear();
1383 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
1384 EXPECT_EQ(std::string(S), "0377");
1385 S.clear();
1386 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
1387 EXPECT_EQ(std::string(S), "255");
1388 S.clear();
1389 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
1390 EXPECT_EQ(std::string(S), "0xFF");
1391 S.clear();
1392 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
1393 EXPECT_EQ(std::string(S), "73");
1394 S.clear();
1396 isSigned = true;
1397 APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
1398 EXPECT_EQ(std::string(S), "-0b1");
1399 S.clear();
1400 APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
1401 EXPECT_EQ(std::string(S), "-01");
1402 S.clear();
1403 APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
1404 EXPECT_EQ(std::string(S), "-1");
1405 S.clear();
1406 APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
1407 EXPECT_EQ(std::string(S), "-0x1");
1408 S.clear();
1409 APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
1410 EXPECT_EQ(std::string(S), "-1");
1411 S.clear();
1414 TEST(APIntTest, Log2) {
1415 EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
1416 EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
1417 EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
1418 EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
1419 EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
1420 EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
1421 EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
1422 EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
1423 EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
1426 #ifdef GTEST_HAS_DEATH_TEST
1427 #ifndef NDEBUG
1428 TEST(APIntTest, StringDeath) {
1429 EXPECT_DEATH((void)APInt(32, "", 0), "Invalid string length");
1430 EXPECT_DEATH((void)APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
1431 EXPECT_DEATH((void)APInt(32, "", 10), "Invalid string length");
1432 EXPECT_DEATH((void)APInt(32, "-", 10), "String is only a sign, needs a value.");
1433 EXPECT_DEATH((void)APInt(1, "1234", 10), "Insufficient bit width");
1434 EXPECT_DEATH((void)APInt(32, "\0", 10), "Invalid string length");
1435 EXPECT_DEATH((void)APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
1436 EXPECT_DEATH((void)APInt(32, "1L", 10), "Invalid character in digit string");
1438 #endif
1439 #endif
1441 TEST(APIntTest, mul_clear) {
1442 APInt ValA(65, -1ULL);
1443 APInt ValB(65, 4);
1444 APInt ValC(65, 0);
1445 ValC = ValA * ValB;
1446 ValA *= ValB;
1447 SmallString<16> StrA, StrC;
1448 ValA.toString(StrA, 10, false);
1449 ValC.toString(StrC, 10, false);
1450 EXPECT_EQ(std::string(StrA), std::string(StrC));
1453 TEST(APIntTest, Rotate) {
1454 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0));
1455 EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1));
1456 EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2));
1457 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
1458 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8));
1460 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
1461 EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
1462 EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
1463 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4));
1464 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
1466 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1467 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1469 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33));
1470 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(32, 33)));
1471 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(33, 33)));
1472 EXPECT_EQ(APInt(32, (1 << 8)), APInt(32, 1).rotl(APInt(32, 40)));
1473 EXPECT_EQ(APInt(32, (1 << 30)), APInt(32, 1).rotl(APInt(31, 30)));
1474 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotl(APInt(31, 31)));
1476 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(1, 0)));
1477 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(1, 1)));
1479 EXPECT_EQ(APInt(32, 16), APInt(32, 1).rotl(APInt(3, 4)));
1481 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotl(APInt(64, 64)));
1482 EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(APInt(64, 65)));
1484 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 3)));
1485 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(7, 10)));
1486 EXPECT_EQ(APInt(7, 24), APInt(7, 3).rotl(APInt(5, 10)));
1487 EXPECT_EQ(APInt(7, 6), APInt(7, 3).rotl(APInt(12, 120)));
1489 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
1490 EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1));
1491 EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2));
1492 EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4));
1493 EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
1495 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0));
1496 EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
1497 EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2));
1498 EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4));
1499 EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8));
1501 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1502 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1504 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33));
1505 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33)));
1506 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(33, 33)));
1507 EXPECT_EQ(APInt(32, (1 << 24)), APInt(32, 1).rotr(APInt(32, 40)));
1509 EXPECT_EQ(APInt(32, (1 << 2)), APInt(32, 1).rotr(APInt(31, 30)));
1510 EXPECT_EQ(APInt(32, (1 << 1)), APInt(32, 1).rotr(APInt(31, 31)));
1512 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(1, 0)));
1513 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(1, 1)));
1515 EXPECT_EQ(APInt(32, (1 << 28)), APInt(32, 1).rotr(APInt(3, 4)));
1517 EXPECT_EQ(APInt(32, 1), APInt(32, 1).rotr(APInt(64, 64)));
1518 EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(64, 65)));
1520 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 3)));
1521 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(7, 10)));
1522 EXPECT_EQ(APInt(7, 48), APInt(7, 3).rotr(APInt(5, 10)));
1523 EXPECT_EQ(APInt(7, 65), APInt(7, 3).rotr(APInt(12, 120)));
1525 APInt Big(256, "00004000800000000000000000003fff8000000000000003", 16);
1526 APInt Rot(256, "3fff80000000000000030000000000000000000040008000", 16);
1527 EXPECT_EQ(Rot, Big.rotr(144));
1529 EXPECT_EQ(APInt(32, 8), APInt(32, 1).rotl(Big));
1530 EXPECT_EQ(APInt(32, (1 << 29)), APInt(32, 1).rotr(Big));
1533 TEST(APIntTest, Splat) {
1534 APInt ValA(8, 0x01);
1535 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
1536 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
1538 APInt ValB(3, 5);
1539 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
1540 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
1543 TEST(APIntTest, tcDecrement) {
1544 // Test single word decrement.
1546 // No out borrow.
1548 APInt::WordType singleWord = ~APInt::WordType(0) << (APInt::APINT_BITS_PER_WORD - 1);
1549 APInt::WordType carry = APInt::tcDecrement(&singleWord, 1);
1550 EXPECT_EQ(carry, APInt::WordType(0));
1551 EXPECT_EQ(singleWord, ~APInt::WordType(0) >> 1);
1554 // With out borrow.
1556 APInt::WordType singleWord = 0;
1557 APInt::WordType carry = APInt::tcDecrement(&singleWord, 1);
1558 EXPECT_EQ(carry, APInt::WordType(1));
1559 EXPECT_EQ(singleWord, ~APInt::WordType(0));
1562 // Test multiword decrement.
1564 // No across word borrow, no out borrow.
1566 APInt::WordType test[4] = {0x1, 0x1, 0x1, 0x1};
1567 APInt::WordType expected[4] = {0x0, 0x1, 0x1, 0x1};
1568 APInt::tcDecrement(test, 4);
1569 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1572 // 1 across word borrow, no out borrow.
1574 APInt::WordType test[4] = {0x0, 0xF, 0x1, 0x1};
1575 APInt::WordType expected[4] = {~APInt::WordType(0), 0xE, 0x1, 0x1};
1576 APInt::WordType carry = APInt::tcDecrement(test, 4);
1577 EXPECT_EQ(carry, APInt::WordType(0));
1578 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1581 // 2 across word borrow, no out borrow.
1583 APInt::WordType test[4] = {0x0, 0x0, 0xC, 0x1};
1584 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), 0xB, 0x1};
1585 APInt::WordType carry = APInt::tcDecrement(test, 4);
1586 EXPECT_EQ(carry, APInt::WordType(0));
1587 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1590 // 3 across word borrow, no out borrow.
1592 APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x1};
1593 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), 0x0};
1594 APInt::WordType carry = APInt::tcDecrement(test, 4);
1595 EXPECT_EQ(carry, APInt::WordType(0));
1596 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1599 // 3 across word borrow, with out borrow.
1601 APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x0};
1602 APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0)};
1603 APInt::WordType carry = APInt::tcDecrement(test, 4);
1604 EXPECT_EQ(carry, APInt::WordType(1));
1605 EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
1609 TEST(APIntTest, arrayAccess) {
1610 // Single word check.
1611 uint64_t E1 = 0x2CA7F46BF6569915ULL;
1612 APInt A1(64, E1);
1613 for (unsigned i = 0, e = 64; i < e; ++i) {
1614 EXPECT_EQ(bool(E1 & (1ULL << i)),
1615 A1[i]);
1618 // Multiword check.
1619 APInt::WordType E2[4] = {
1620 0xEB6EB136591CBA21ULL,
1621 0x7B9358BD6A33F10AULL,
1622 0x7E7FFA5EADD8846ULL,
1623 0x305F341CA00B613DULL
1625 APInt A2(APInt::APINT_BITS_PER_WORD*4, E2);
1626 for (unsigned i = 0; i < 4; ++i) {
1627 for (unsigned j = 0; j < APInt::APINT_BITS_PER_WORD; ++j) {
1628 EXPECT_EQ(bool(E2[i] & (1ULL << j)),
1629 A2[i*APInt::APINT_BITS_PER_WORD + j]);
1634 TEST(APIntTest, LargeAPIntConstruction) {
1635 // Check that we can properly construct very large APInt. It is very
1636 // unlikely that people will ever do this, but it is a legal input,
1637 // so we should not crash on it.
1638 APInt A9(UINT32_MAX, 0);
1639 EXPECT_FALSE(A9.getBoolValue());
1642 TEST(APIntTest, nearestLogBase2) {
1643 // Single word check.
1645 // Test round up.
1646 uint64_t I1 = 0x1800001;
1647 APInt A1(64, I1);
1648 EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2());
1650 // Test round down.
1651 uint64_t I2 = 0x1000011;
1652 APInt A2(64, I2);
1653 EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2());
1655 // Test ties round up.
1656 uint64_t I3 = 0x1800000;
1657 APInt A3(64, I3);
1658 EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2());
1660 // Multiple word check.
1662 // Test round up.
1663 APInt::WordType I4[4] = {0x0, 0xF, 0x18, 0x0};
1664 APInt A4(APInt::APINT_BITS_PER_WORD*4, I4);
1665 EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2());
1667 // Test round down.
1668 APInt::WordType I5[4] = {0x0, 0xF, 0x10, 0x0};
1669 APInt A5(APInt::APINT_BITS_PER_WORD*4, I5);
1670 EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2());
1672 // Test ties round up.
1673 uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18};
1674 APInt A6(APInt::APINT_BITS_PER_WORD*4, I6);
1675 EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2());
1677 // Test BitWidth == 1 special cases.
1678 APInt A7(1, 1);
1679 EXPECT_EQ(A7.nearestLogBase2(), 0ULL);
1680 APInt A8(1, 0);
1681 EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX);
1683 // Test the zero case when we have a bit width large enough such
1684 // that the bit width is larger than UINT32_MAX-1.
1685 APInt A9(UINT32_MAX, 0);
1686 EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX);
1689 TEST(APIntTest, IsSplat) {
1690 APInt A(32, 0x01010101);
1691 EXPECT_FALSE(A.isSplat(1));
1692 EXPECT_FALSE(A.isSplat(2));
1693 EXPECT_FALSE(A.isSplat(4));
1694 EXPECT_TRUE(A.isSplat(8));
1695 EXPECT_TRUE(A.isSplat(16));
1696 EXPECT_TRUE(A.isSplat(32));
1698 APInt B(24, 0xAAAAAA);
1699 EXPECT_FALSE(B.isSplat(1));
1700 EXPECT_TRUE(B.isSplat(2));
1701 EXPECT_TRUE(B.isSplat(4));
1702 EXPECT_TRUE(B.isSplat(8));
1703 EXPECT_TRUE(B.isSplat(24));
1705 APInt C(24, 0xABAAAB);
1706 EXPECT_FALSE(C.isSplat(1));
1707 EXPECT_FALSE(C.isSplat(2));
1708 EXPECT_FALSE(C.isSplat(4));
1709 EXPECT_FALSE(C.isSplat(8));
1710 EXPECT_TRUE(C.isSplat(24));
1712 APInt D(32, 0xABBAABBA);
1713 EXPECT_FALSE(D.isSplat(1));
1714 EXPECT_FALSE(D.isSplat(2));
1715 EXPECT_FALSE(D.isSplat(4));
1716 EXPECT_FALSE(D.isSplat(8));
1717 EXPECT_TRUE(D.isSplat(16));
1718 EXPECT_TRUE(D.isSplat(32));
1720 APInt E(32, 0);
1721 EXPECT_TRUE(E.isSplat(1));
1722 EXPECT_TRUE(E.isSplat(2));
1723 EXPECT_TRUE(E.isSplat(4));
1724 EXPECT_TRUE(E.isSplat(8));
1725 EXPECT_TRUE(E.isSplat(16));
1726 EXPECT_TRUE(E.isSplat(32));
1729 TEST(APIntTest, isMask) {
1730 EXPECT_FALSE(APInt(32, 0x01010101).isMask());
1731 EXPECT_FALSE(APInt(32, 0xf0000000).isMask());
1732 EXPECT_FALSE(APInt(32, 0xffff0000).isMask());
1733 EXPECT_FALSE(APInt(32, 0xff << 1).isMask());
1735 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1736 EXPECT_FALSE(APInt(N, 0).isMask());
1738 APInt One(N, 1);
1739 for (int I = 1; I <= N; ++I) {
1740 APInt MaskVal = One.shl(I) - 1;
1741 EXPECT_TRUE(MaskVal.isMask());
1742 EXPECT_TRUE(MaskVal.isMask(I));
1747 TEST(APIntTest, isShiftedMask) {
1748 EXPECT_FALSE(APInt(32, 0x01010101).isShiftedMask());
1749 EXPECT_TRUE(APInt(32, 0xf0000000).isShiftedMask());
1750 EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask());
1751 EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask());
1753 unsigned MaskIdx, MaskLen;
1754 EXPECT_FALSE(APInt(32, 0x01010101).isShiftedMask(MaskIdx, MaskLen));
1755 EXPECT_TRUE(APInt(32, 0xf0000000).isShiftedMask(MaskIdx, MaskLen));
1756 EXPECT_EQ(28, (int)MaskIdx);
1757 EXPECT_EQ(4, (int)MaskLen);
1758 EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask(MaskIdx, MaskLen));
1759 EXPECT_EQ(16, (int)MaskIdx);
1760 EXPECT_EQ(16, (int)MaskLen);
1761 EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask(MaskIdx, MaskLen));
1762 EXPECT_EQ(1, (int)MaskIdx);
1763 EXPECT_EQ(8, (int)MaskLen);
1765 for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) {
1766 EXPECT_FALSE(APInt(N, 0).isShiftedMask());
1767 EXPECT_FALSE(APInt(N, 0).isShiftedMask(MaskIdx, MaskLen));
1769 APInt One(N, 1);
1770 for (int I = 1; I < N; ++I) {
1771 APInt MaskVal = One.shl(I) - 1;
1772 EXPECT_TRUE(MaskVal.isShiftedMask());
1773 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1774 EXPECT_EQ(0, (int)MaskIdx);
1775 EXPECT_EQ(I, (int)MaskLen);
1777 for (int I = 1; I < N - 1; ++I) {
1778 APInt MaskVal = One.shl(I);
1779 EXPECT_TRUE(MaskVal.isShiftedMask());
1780 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1781 EXPECT_EQ(I, (int)MaskIdx);
1782 EXPECT_EQ(1, (int)MaskLen);
1784 for (int I = 1; I < N; ++I) {
1785 APInt MaskVal = APInt::getHighBitsSet(N, I);
1786 EXPECT_TRUE(MaskVal.isShiftedMask());
1787 EXPECT_TRUE(MaskVal.isShiftedMask(MaskIdx, MaskLen));
1788 EXPECT_EQ(N - I, (int)MaskIdx);
1789 EXPECT_EQ(I, (int)MaskLen);
1794 TEST(APIntTest, isPowerOf2) {
1795 EXPECT_FALSE(APInt(5, 0x00).isPowerOf2());
1796 EXPECT_FALSE(APInt(32, 0x11).isPowerOf2());
1797 EXPECT_TRUE(APInt(17, 0x01).isPowerOf2());
1798 EXPECT_TRUE(APInt(32, (unsigned)(0xffu << 31)).isPowerOf2());
1800 for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
1801 EXPECT_FALSE(APInt(N, 0).isPowerOf2());
1802 EXPECT_TRUE(APInt::getSignedMinValue(N).isPowerOf2());
1804 APInt One(N, 1);
1805 for (int I = 1; I < N - 1; ++I) {
1806 EXPECT_TRUE(APInt::getOneBitSet(N, I).isPowerOf2());
1808 APInt MaskVal = One.shl(I);
1809 EXPECT_TRUE(MaskVal.isPowerOf2());
1814 TEST(APIntTest, isNegatedPowerOf2) {
1815 EXPECT_FALSE(APInt(5, 0x00).isNegatedPowerOf2());
1816 EXPECT_TRUE(APInt(15, 0x7ffe).isNegatedPowerOf2());
1817 EXPECT_TRUE(APInt(16, 0xfffc).isNegatedPowerOf2());
1818 EXPECT_TRUE(APInt(32, 0xffffffff).isNegatedPowerOf2());
1820 for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
1821 EXPECT_FALSE(APInt(N, 0).isNegatedPowerOf2());
1822 EXPECT_TRUE(APInt::getAllOnes(N).isNegatedPowerOf2());
1823 EXPECT_TRUE(APInt::getSignedMinValue(N).isNegatedPowerOf2());
1824 EXPECT_TRUE((-APInt::getSignedMinValue(N)).isNegatedPowerOf2());
1826 APInt One(N, 1);
1827 for (int I = 1; I < N - 1; ++I) {
1828 EXPECT_FALSE(APInt::getOneBitSet(N, I).isNegatedPowerOf2());
1829 EXPECT_TRUE((-APInt::getOneBitSet(N, I)).isNegatedPowerOf2());
1831 APInt MaskVal = One.shl(I);
1832 EXPECT_TRUE((-MaskVal).isNegatedPowerOf2());
1834 APInt ShiftMaskVal = One.getHighBitsSet(N, I);
1835 EXPECT_TRUE(ShiftMaskVal.isNegatedPowerOf2());
1840 // Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which
1841 // does self-move on some platforms.
1842 #ifdef EXPENSIVE_CHECKS
1843 #if defined(__clang__)
1844 // Disable the pragma warning from versions of Clang without -Wself-move
1845 #pragma clang diagnostic push
1846 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1847 // Disable the warning that triggers on exactly what is being tested.
1848 #pragma clang diagnostic push
1849 #pragma clang diagnostic ignored "-Wself-move"
1850 #endif
1851 TEST(APIntTest, SelfMoveAssignment) {
1852 APInt X(32, 0xdeadbeef);
1853 X = std::move(X);
1854 EXPECT_EQ(32u, X.getBitWidth());
1855 EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue());
1857 uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL};
1858 APInt Y(128, Bits);
1859 Y = std::move(Y);
1860 EXPECT_EQ(128u, Y.getBitWidth());
1861 EXPECT_EQ(~0ULL, Y.getLimitedValue());
1862 const uint64_t *Raw = Y.getRawData();
1863 EXPECT_EQ(2u, Y.getNumWords());
1864 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]);
1865 EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]);
1867 #if defined(__clang__)
1868 #pragma clang diagnostic pop
1869 #pragma clang diagnostic pop
1870 #endif
1871 #endif // EXPENSIVE_CHECKS
1873 TEST(APIntTest, byteSwap) {
1874 EXPECT_EQ(0x00000000, APInt(16, 0x0000).byteSwap());
1875 EXPECT_EQ(0x0000010f, APInt(16, 0x0f01).byteSwap());
1876 EXPECT_EQ(0x00ff8000, APInt(24, 0x0080ff).byteSwap());
1877 EXPECT_EQ(0x117700ff, APInt(32, 0xff007711).byteSwap());
1878 EXPECT_EQ(0x228811aaffULL, APInt(40, 0xffaa118822ULL).byteSwap());
1879 EXPECT_EQ(0x050403020100ULL, APInt(48, 0x000102030405ULL).byteSwap());
1880 EXPECT_EQ(0xff050403020100ULL, APInt(56, 0x000102030405ffULL).byteSwap());
1881 EXPECT_EQ(0xff050403020100aaULL, APInt(64, 0xaa000102030405ffULL).byteSwap());
1883 for (unsigned N : {16, 24, 32, 48, 56, 64, 72, 80, 96, 112, 128, 248, 256,
1884 1024, 1032, 1040}) {
1885 for (unsigned I = 0; I < N; I += 8) {
1886 APInt X = APInt::getBitsSet(N, I, I + 8);
1887 APInt Y = APInt::getBitsSet(N, N - I - 8, N - I);
1888 EXPECT_EQ(Y, X.byteSwap());
1889 EXPECT_EQ(X, Y.byteSwap());
1894 TEST(APIntTest, reverseBits) {
1895 EXPECT_EQ(1, APInt(1, 1).reverseBits());
1896 EXPECT_EQ(0, APInt(1, 0).reverseBits());
1898 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1899 EXPECT_EQ(3, APInt(2, 3).reverseBits());
1901 EXPECT_EQ(0xb, APInt(4, 0xd).reverseBits());
1902 EXPECT_EQ(0xd, APInt(4, 0xb).reverseBits());
1903 EXPECT_EQ(0xf, APInt(4, 0xf).reverseBits());
1905 EXPECT_EQ(0x30, APInt(7, 0x6).reverseBits());
1906 EXPECT_EQ(0x5a, APInt(7, 0x2d).reverseBits());
1908 EXPECT_EQ(0x0f, APInt(8, 0xf0).reverseBits());
1909 EXPECT_EQ(0xf0, APInt(8, 0x0f).reverseBits());
1911 EXPECT_EQ(0x0f0f, APInt(16, 0xf0f0).reverseBits());
1912 EXPECT_EQ(0xf0f0, APInt(16, 0x0f0f).reverseBits());
1914 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1915 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1917 EXPECT_EQ(0x402880a0 >> 1, APInt(31, 0x05011402).reverseBits());
1919 EXPECT_EQ(0x0f0f0f0f, APInt(32, 0xf0f0f0f0).reverseBits());
1920 EXPECT_EQ(0xf0f0f0f0, APInt(32, 0x0f0f0f0f).reverseBits());
1922 EXPECT_EQ(0x0f0f0f0f0f0f0f0f, APInt(64, 0xf0f0f0f0f0f0f0f0).reverseBits());
1923 EXPECT_EQ(0xf0f0f0f0f0f0f0f0, APInt(64, 0x0f0f0f0f0f0f0f0f).reverseBits());
1925 for (unsigned N : { 1, 8, 16, 24, 31, 32, 33,
1926 63, 64, 65, 127, 128, 257, 1024 }) {
1927 for (unsigned I = 0; I < N; ++I) {
1928 APInt X = APInt::getOneBitSet(N, I);
1929 APInt Y = APInt::getOneBitSet(N, N - (I + 1));
1930 EXPECT_EQ(Y, X.reverseBits());
1931 EXPECT_EQ(X, Y.reverseBits());
1936 TEST(APIntTest, insertBits) {
1937 APInt iSrc(31, 0x00123456);
1939 // Direct copy.
1940 APInt i31(31, 0x76543210ull);
1941 i31.insertBits(iSrc, 0);
1942 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
1944 // Single word src/dst insertion.
1945 APInt i63(63, 0x01234567FFFFFFFFull);
1946 i63.insertBits(iSrc, 4);
1947 EXPECT_EQ(static_cast<int64_t>(0x012345600123456Full), i63.getSExtValue());
1949 // Zero width insert is a noop.
1950 i31.insertBits(APInt::getZeroWidth(), 1);
1951 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
1953 // Insert single word src into one word of dst.
1954 APInt i120(120, UINT64_MAX, true);
1955 i120.insertBits(iSrc, 8);
1956 EXPECT_EQ(static_cast<int64_t>(0xFFFFFF80123456FFull), i120.getSExtValue());
1958 // Insert single word src into two words of dst.
1959 APInt i127(127, UINT64_MAX, true);
1960 i127.insertBits(iSrc, 48);
1961 EXPECT_EQ(i127.extractBits(64, 0).getZExtValue(), 0x3456FFFFFFFFFFFFull);
1962 EXPECT_EQ(i127.extractBits(63, 64).getZExtValue(), 0x7FFFFFFFFFFF8012ull);
1964 // Insert on word boundaries.
1965 APInt i128(128, 0);
1966 i128.insertBits(APInt(64, UINT64_MAX, true), 0);
1967 i128.insertBits(APInt(64, UINT64_MAX, true), 64);
1968 EXPECT_EQ(-1, i128.getSExtValue());
1970 APInt i256(256, UINT64_MAX, true);
1971 i256.insertBits(APInt(65, 0), 0);
1972 i256.insertBits(APInt(69, 0), 64);
1973 i256.insertBits(APInt(128, 0), 128);
1974 EXPECT_EQ(0u, i256.getSExtValue());
1976 APInt i257(257, 0);
1977 i257.insertBits(APInt(96, UINT64_MAX, true), 64);
1978 EXPECT_EQ(i257.extractBits(64, 0).getZExtValue(), 0x0000000000000000ull);
1979 EXPECT_EQ(i257.extractBits(64, 64).getZExtValue(), 0xFFFFFFFFFFFFFFFFull);
1980 EXPECT_EQ(i257.extractBits(64, 128).getZExtValue(), 0x00000000FFFFFFFFull);
1981 EXPECT_EQ(i257.extractBits(65, 192).getZExtValue(), 0x0000000000000000ull);
1983 // General insertion.
1984 APInt i260(260, UINT64_MAX, true);
1985 i260.insertBits(APInt(129, 1ull << 48), 15);
1986 EXPECT_EQ(i260.extractBits(64, 0).getZExtValue(), 0x8000000000007FFFull);
1987 EXPECT_EQ(i260.extractBits(64, 64).getZExtValue(), 0x0000000000000000ull);
1988 EXPECT_EQ(i260.extractBits(64, 128).getZExtValue(), 0xFFFFFFFFFFFF0000ull);
1989 EXPECT_EQ(i260.extractBits(64, 192).getZExtValue(), 0xFFFFFFFFFFFFFFFFull);
1990 EXPECT_EQ(i260.extractBits(4, 256).getZExtValue(), 0x000000000000000Full);
1993 TEST(APIntTest, insertBitsUInt64) {
1994 // Tests cloned from insertBits but adapted to the numBits <= 64 constraint
1995 uint64_t iSrc = 0x00123456;
1997 // Direct copy.
1998 APInt i31(31, 0x76543210ull);
1999 i31.insertBits(iSrc, 0, 31);
2000 EXPECT_EQ(static_cast<int64_t>(0x00123456ull), i31.getSExtValue());
2002 // Single word src/dst insertion.
2003 APInt i63(63, 0x01234567FFFFFFFFull);
2004 i63.insertBits(iSrc, 4, 31);
2005 EXPECT_EQ(static_cast<int64_t>(0x012345600123456Full), i63.getSExtValue());
2007 // Insert single word src into one word of dst.
2008 APInt i120(120, UINT64_MAX, true);
2009 i120.insertBits(iSrc, 8, 31);
2010 EXPECT_EQ(static_cast<int64_t>(0xFFFFFF80123456FFull), i120.getSExtValue());
2012 // Insert single word src into two words of dst.
2013 APInt i127(127, UINT64_MAX, true);
2014 i127.insertBits(iSrc, 48, 31);
2015 EXPECT_EQ(i127.extractBits(64, 0).getZExtValue(), 0x3456FFFFFFFFFFFFull);
2016 EXPECT_EQ(i127.extractBits(63, 64).getZExtValue(), 0x7FFFFFFFFFFF8012ull);
2018 // Insert on word boundaries.
2019 APInt i128(128, 0);
2020 i128.insertBits(UINT64_MAX, 0, 64);
2021 i128.insertBits(UINT64_MAX, 64, 64);
2022 EXPECT_EQ(-1, i128.getSExtValue());
2024 APInt i256(256, UINT64_MAX, true);
2025 i256.insertBits(0, 0, 64);
2026 i256.insertBits(0, 64, 1);
2027 i256.insertBits(0, 64, 64);
2028 i256.insertBits(0, 128, 5);
2029 i256.insertBits(0, 128, 64);
2030 i256.insertBits(0, 192, 64);
2031 EXPECT_EQ(0u, i256.getSExtValue());
2033 APInt i257(257, 0);
2034 i257.insertBits(APInt(96, UINT64_MAX, true), 64);
2035 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 0), 0x0000000000000000ull);
2036 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 64), 0xFFFFFFFFFFFFFFFFull);
2037 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 128), 0x00000000FFFFFFFFull);
2038 EXPECT_EQ(i257.extractBitsAsZExtValue(64, 192), 0x0000000000000000ull);
2039 EXPECT_EQ(i257.extractBitsAsZExtValue(1, 256), 0x0000000000000000ull);
2041 // General insertion.
2042 APInt i260(260, UINT64_MAX, true);
2043 i260.insertBits(APInt(129, 1ull << 48), 15);
2044 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 0), 0x8000000000007FFFull);
2045 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 64), 0x0000000000000000ull);
2046 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 128), 0xFFFFFFFFFFFF0000ull);
2047 EXPECT_EQ(i260.extractBitsAsZExtValue(64, 192), 0xFFFFFFFFFFFFFFFFull);
2048 EXPECT_EQ(i260.extractBitsAsZExtValue(4, 256), 0x000000000000000Full);
2051 TEST(APIntTest, extractBits) {
2052 APInt i32(32, 0x1234567);
2053 EXPECT_EQ(0x3456, i32.extractBits(16, 4));
2055 APInt i64(64, 0x01234567FFFFFFFFull);
2056 EXPECT_EQ(0xFFFFFFFF, i64.extractBits(32, 0));
2057 EXPECT_EQ(0xFFFFFFFF, i64.trunc(32));
2058 EXPECT_EQ(0x01234567, i64.extractBits(32, 32));
2059 EXPECT_EQ(0x01234567, i64.lshr(32).trunc(32));
2061 APInt i257(257, 0xFFFFFFFFFF0000FFull, true);
2062 EXPECT_EQ(0xFFu, i257.extractBits(16, 0));
2063 EXPECT_EQ(0xFFu, i257.lshr(0).trunc(16));
2064 EXPECT_EQ((0xFFu >> 1), i257.extractBits(16, 1));
2065 EXPECT_EQ((0xFFu >> 1), i257.lshr(1).trunc(16));
2066 EXPECT_EQ(-1, i257.extractBits(32, 64).getSExtValue());
2067 EXPECT_EQ(-1, i257.lshr(64).trunc(32).getSExtValue());
2068 EXPECT_EQ(-1, i257.extractBits(128, 128).getSExtValue());
2069 EXPECT_EQ(-1, i257.lshr(128).trunc(128).getSExtValue());
2070 EXPECT_EQ(-1, i257.extractBits(66, 191).getSExtValue());
2071 EXPECT_EQ(-1, i257.lshr(191).trunc(66).getSExtValue());
2072 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2073 i257.extractBits(128, 1).getSExtValue());
2074 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2075 i257.lshr(1).trunc(128).getSExtValue());
2076 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2077 i257.extractBits(129, 1).getSExtValue());
2078 EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFFF80007Full),
2079 i257.lshr(1).trunc(129).getSExtValue());
2081 EXPECT_EQ(APInt(48, 0),
2082 APInt(144, "281474976710655", 10).extractBits(48, 48));
2083 EXPECT_EQ(APInt(48, 0),
2084 APInt(144, "281474976710655", 10).lshr(48).trunc(48));
2085 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2086 APInt(144, "281474976710655", 10).extractBits(48, 0));
2087 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2088 APInt(144, "281474976710655", 10).lshr(0).trunc(48));
2089 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2090 APInt(144, "281474976710655", 10).extractBits(48, 1));
2091 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2092 APInt(144, "281474976710655", 10).lshr(1).trunc(48));
2095 TEST(APIntTest, extractBitsAsZExtValue) {
2096 // Tests based on extractBits
2097 APInt i32(32, 0x1234567);
2098 EXPECT_EQ(0x3456u, i32.extractBitsAsZExtValue(16, 4));
2100 APInt i257(257, 0xFFFFFFFFFF0000FFull, true);
2101 EXPECT_EQ(0xFFu, i257.extractBitsAsZExtValue(16, 0));
2102 EXPECT_EQ((0xFFu >> 1), i257.extractBitsAsZExtValue(16, 1));
2103 EXPECT_EQ(0xFFFFFFFFull, i257.extractBitsAsZExtValue(32, 64));
2104 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 128));
2105 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 192));
2106 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 191));
2107 EXPECT_EQ(0x3u, i257.extractBitsAsZExtValue(2, 255));
2108 EXPECT_EQ(0xFFFFFFFFFF80007Full, i257.extractBitsAsZExtValue(64, 1));
2109 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 65));
2110 EXPECT_EQ(0xFFFFFFFFFF80007Full, i257.extractBitsAsZExtValue(64, 1));
2111 EXPECT_EQ(0xFFFFFFFFFFFFFFFFull, i257.extractBitsAsZExtValue(64, 65));
2112 EXPECT_EQ(0x1ull, i257.extractBitsAsZExtValue(1, 129));
2114 EXPECT_EQ(APInt(48, 0),
2115 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 48));
2116 EXPECT_EQ(APInt(48, 0x0000ffffffffffffull),
2117 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 0));
2118 EXPECT_EQ(APInt(48, 0x00007fffffffffffull),
2119 APInt(144, "281474976710655", 10).extractBitsAsZExtValue(48, 1));
2122 TEST(APIntTest, getLowBitsSet) {
2123 APInt i128lo64 = APInt::getLowBitsSet(128, 64);
2124 EXPECT_EQ(0u, i128lo64.countLeadingOnes());
2125 EXPECT_EQ(64u, i128lo64.countLeadingZeros());
2126 EXPECT_EQ(64u, i128lo64.getActiveBits());
2127 EXPECT_EQ(0u, i128lo64.countTrailingZeros());
2128 EXPECT_EQ(64u, i128lo64.countTrailingOnes());
2129 EXPECT_EQ(64u, i128lo64.countPopulation());
2132 TEST(APIntTest, getBitsSet) {
2133 APInt i64hi1lo1 = APInt::getBitsSet(64, 1, 63);
2134 EXPECT_EQ(0u, i64hi1lo1.countLeadingOnes());
2135 EXPECT_EQ(1u, i64hi1lo1.countLeadingZeros());
2136 EXPECT_EQ(63u, i64hi1lo1.getActiveBits());
2137 EXPECT_EQ(1u, i64hi1lo1.countTrailingZeros());
2138 EXPECT_EQ(0u, i64hi1lo1.countTrailingOnes());
2139 EXPECT_EQ(62u, i64hi1lo1.countPopulation());
2141 APInt i127hi1lo1 = APInt::getBitsSet(127, 1, 126);
2142 EXPECT_EQ(0u, i127hi1lo1.countLeadingOnes());
2143 EXPECT_EQ(1u, i127hi1lo1.countLeadingZeros());
2144 EXPECT_EQ(126u, i127hi1lo1.getActiveBits());
2145 EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros());
2146 EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes());
2147 EXPECT_EQ(125u, i127hi1lo1.countPopulation());
2150 TEST(APIntTest, getBitsSetWithWrap) {
2151 APInt i64hi1lo1 = APInt::getBitsSetWithWrap(64, 1, 63);
2152 EXPECT_EQ(0u, i64hi1lo1.countLeadingOnes());
2153 EXPECT_EQ(1u, i64hi1lo1.countLeadingZeros());
2154 EXPECT_EQ(63u, i64hi1lo1.getActiveBits());
2155 EXPECT_EQ(1u, i64hi1lo1.countTrailingZeros());
2156 EXPECT_EQ(0u, i64hi1lo1.countTrailingOnes());
2157 EXPECT_EQ(62u, i64hi1lo1.countPopulation());
2159 APInt i127hi1lo1 = APInt::getBitsSetWithWrap(127, 1, 126);
2160 EXPECT_EQ(0u, i127hi1lo1.countLeadingOnes());
2161 EXPECT_EQ(1u, i127hi1lo1.countLeadingZeros());
2162 EXPECT_EQ(126u, i127hi1lo1.getActiveBits());
2163 EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros());
2164 EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes());
2165 EXPECT_EQ(125u, i127hi1lo1.countPopulation());
2167 APInt i64hi1lo1wrap = APInt::getBitsSetWithWrap(64, 63, 1);
2168 EXPECT_EQ(1u, i64hi1lo1wrap.countLeadingOnes());
2169 EXPECT_EQ(0u, i64hi1lo1wrap.countLeadingZeros());
2170 EXPECT_EQ(64u, i64hi1lo1wrap.getActiveBits());
2171 EXPECT_EQ(0u, i64hi1lo1wrap.countTrailingZeros());
2172 EXPECT_EQ(1u, i64hi1lo1wrap.countTrailingOnes());
2173 EXPECT_EQ(2u, i64hi1lo1wrap.countPopulation());
2175 APInt i127hi1lo1wrap = APInt::getBitsSetWithWrap(127, 126, 1);
2176 EXPECT_EQ(1u, i127hi1lo1wrap.countLeadingOnes());
2177 EXPECT_EQ(0u, i127hi1lo1wrap.countLeadingZeros());
2178 EXPECT_EQ(127u, i127hi1lo1wrap.getActiveBits());
2179 EXPECT_EQ(0u, i127hi1lo1wrap.countTrailingZeros());
2180 EXPECT_EQ(1u, i127hi1lo1wrap.countTrailingOnes());
2181 EXPECT_EQ(2u, i127hi1lo1wrap.countPopulation());
2183 APInt i32hiequallowrap = APInt::getBitsSetWithWrap(32, 10, 10);
2184 EXPECT_EQ(32u, i32hiequallowrap.countLeadingOnes());
2185 EXPECT_EQ(0u, i32hiequallowrap.countLeadingZeros());
2186 EXPECT_EQ(32u, i32hiequallowrap.getActiveBits());
2187 EXPECT_EQ(0u, i32hiequallowrap.countTrailingZeros());
2188 EXPECT_EQ(32u, i32hiequallowrap.countTrailingOnes());
2189 EXPECT_EQ(32u, i32hiequallowrap.countPopulation());
2192 TEST(APIntTest, getHighBitsSet) {
2193 APInt i64hi32 = APInt::getHighBitsSet(64, 32);
2194 EXPECT_EQ(32u, i64hi32.countLeadingOnes());
2195 EXPECT_EQ(0u, i64hi32.countLeadingZeros());
2196 EXPECT_EQ(64u, i64hi32.getActiveBits());
2197 EXPECT_EQ(32u, i64hi32.countTrailingZeros());
2198 EXPECT_EQ(0u, i64hi32.countTrailingOnes());
2199 EXPECT_EQ(32u, i64hi32.countPopulation());
2202 TEST(APIntTest, getBitsSetFrom) {
2203 APInt i64hi31 = APInt::getBitsSetFrom(64, 33);
2204 EXPECT_EQ(31u, i64hi31.countLeadingOnes());
2205 EXPECT_EQ(0u, i64hi31.countLeadingZeros());
2206 EXPECT_EQ(64u, i64hi31.getActiveBits());
2207 EXPECT_EQ(33u, i64hi31.countTrailingZeros());
2208 EXPECT_EQ(0u, i64hi31.countTrailingOnes());
2209 EXPECT_EQ(31u, i64hi31.countPopulation());
2212 TEST(APIntTest, setLowBits) {
2213 APInt i64lo32(64, 0);
2214 i64lo32.setLowBits(32);
2215 EXPECT_EQ(0u, i64lo32.countLeadingOnes());
2216 EXPECT_EQ(32u, i64lo32.countLeadingZeros());
2217 EXPECT_EQ(32u, i64lo32.getActiveBits());
2218 EXPECT_EQ(0u, i64lo32.countTrailingZeros());
2219 EXPECT_EQ(32u, i64lo32.countTrailingOnes());
2220 EXPECT_EQ(32u, i64lo32.countPopulation());
2222 APInt i128lo64(128, 0);
2223 i128lo64.setLowBits(64);
2224 EXPECT_EQ(0u, i128lo64.countLeadingOnes());
2225 EXPECT_EQ(64u, i128lo64.countLeadingZeros());
2226 EXPECT_EQ(64u, i128lo64.getActiveBits());
2227 EXPECT_EQ(0u, i128lo64.countTrailingZeros());
2228 EXPECT_EQ(64u, i128lo64.countTrailingOnes());
2229 EXPECT_EQ(64u, i128lo64.countPopulation());
2231 APInt i128lo24(128, 0);
2232 i128lo24.setLowBits(24);
2233 EXPECT_EQ(0u, i128lo24.countLeadingOnes());
2234 EXPECT_EQ(104u, i128lo24.countLeadingZeros());
2235 EXPECT_EQ(24u, i128lo24.getActiveBits());
2236 EXPECT_EQ(0u, i128lo24.countTrailingZeros());
2237 EXPECT_EQ(24u, i128lo24.countTrailingOnes());
2238 EXPECT_EQ(24u, i128lo24.countPopulation());
2240 APInt i128lo104(128, 0);
2241 i128lo104.setLowBits(104);
2242 EXPECT_EQ(0u, i128lo104.countLeadingOnes());
2243 EXPECT_EQ(24u, i128lo104.countLeadingZeros());
2244 EXPECT_EQ(104u, i128lo104.getActiveBits());
2245 EXPECT_EQ(0u, i128lo104.countTrailingZeros());
2246 EXPECT_EQ(104u, i128lo104.countTrailingOnes());
2247 EXPECT_EQ(104u, i128lo104.countPopulation());
2249 APInt i128lo0(128, 0);
2250 i128lo0.setLowBits(0);
2251 EXPECT_EQ(0u, i128lo0.countLeadingOnes());
2252 EXPECT_EQ(128u, i128lo0.countLeadingZeros());
2253 EXPECT_EQ(0u, i128lo0.getActiveBits());
2254 EXPECT_EQ(128u, i128lo0.countTrailingZeros());
2255 EXPECT_EQ(0u, i128lo0.countTrailingOnes());
2256 EXPECT_EQ(0u, i128lo0.countPopulation());
2258 APInt i80lo79(80, 0);
2259 i80lo79.setLowBits(79);
2260 EXPECT_EQ(0u, i80lo79.countLeadingOnes());
2261 EXPECT_EQ(1u, i80lo79.countLeadingZeros());
2262 EXPECT_EQ(79u, i80lo79.getActiveBits());
2263 EXPECT_EQ(0u, i80lo79.countTrailingZeros());
2264 EXPECT_EQ(79u, i80lo79.countTrailingOnes());
2265 EXPECT_EQ(79u, i80lo79.countPopulation());
2268 TEST(APIntTest, setHighBits) {
2269 APInt i64hi32(64, 0);
2270 i64hi32.setHighBits(32);
2271 EXPECT_EQ(32u, i64hi32.countLeadingOnes());
2272 EXPECT_EQ(0u, i64hi32.countLeadingZeros());
2273 EXPECT_EQ(64u, i64hi32.getActiveBits());
2274 EXPECT_EQ(32u, i64hi32.countTrailingZeros());
2275 EXPECT_EQ(0u, i64hi32.countTrailingOnes());
2276 EXPECT_EQ(32u, i64hi32.countPopulation());
2278 APInt i128hi64(128, 0);
2279 i128hi64.setHighBits(64);
2280 EXPECT_EQ(64u, i128hi64.countLeadingOnes());
2281 EXPECT_EQ(0u, i128hi64.countLeadingZeros());
2282 EXPECT_EQ(128u, i128hi64.getActiveBits());
2283 EXPECT_EQ(64u, i128hi64.countTrailingZeros());
2284 EXPECT_EQ(0u, i128hi64.countTrailingOnes());
2285 EXPECT_EQ(64u, i128hi64.countPopulation());
2287 APInt i128hi24(128, 0);
2288 i128hi24.setHighBits(24);
2289 EXPECT_EQ(24u, i128hi24.countLeadingOnes());
2290 EXPECT_EQ(0u, i128hi24.countLeadingZeros());
2291 EXPECT_EQ(128u, i128hi24.getActiveBits());
2292 EXPECT_EQ(104u, i128hi24.countTrailingZeros());
2293 EXPECT_EQ(0u, i128hi24.countTrailingOnes());
2294 EXPECT_EQ(24u, i128hi24.countPopulation());
2296 APInt i128hi104(128, 0);
2297 i128hi104.setHighBits(104);
2298 EXPECT_EQ(104u, i128hi104.countLeadingOnes());
2299 EXPECT_EQ(0u, i128hi104.countLeadingZeros());
2300 EXPECT_EQ(128u, i128hi104.getActiveBits());
2301 EXPECT_EQ(24u, i128hi104.countTrailingZeros());
2302 EXPECT_EQ(0u, i128hi104.countTrailingOnes());
2303 EXPECT_EQ(104u, i128hi104.countPopulation());
2305 APInt i128hi0(128, 0);
2306 i128hi0.setHighBits(0);
2307 EXPECT_EQ(0u, i128hi0.countLeadingOnes());
2308 EXPECT_EQ(128u, i128hi0.countLeadingZeros());
2309 EXPECT_EQ(0u, i128hi0.getActiveBits());
2310 EXPECT_EQ(128u, i128hi0.countTrailingZeros());
2311 EXPECT_EQ(0u, i128hi0.countTrailingOnes());
2312 EXPECT_EQ(0u, i128hi0.countPopulation());
2314 APInt i80hi1(80, 0);
2315 i80hi1.setHighBits(1);
2316 EXPECT_EQ(1u, i80hi1.countLeadingOnes());
2317 EXPECT_EQ(0u, i80hi1.countLeadingZeros());
2318 EXPECT_EQ(80u, i80hi1.getActiveBits());
2319 EXPECT_EQ(79u, i80hi1.countTrailingZeros());
2320 EXPECT_EQ(0u, i80hi1.countTrailingOnes());
2321 EXPECT_EQ(1u, i80hi1.countPopulation());
2323 APInt i32hi16(32, 0);
2324 i32hi16.setHighBits(16);
2325 EXPECT_EQ(16u, i32hi16.countLeadingOnes());
2326 EXPECT_EQ(0u, i32hi16.countLeadingZeros());
2327 EXPECT_EQ(32u, i32hi16.getActiveBits());
2328 EXPECT_EQ(16u, i32hi16.countTrailingZeros());
2329 EXPECT_EQ(0u, i32hi16.countTrailingOnes());
2330 EXPECT_EQ(16u, i32hi16.countPopulation());
2333 TEST(APIntTest, setBitsFrom) {
2334 APInt i64from63(64, 0);
2335 i64from63.setBitsFrom(63);
2336 EXPECT_EQ(1u, i64from63.countLeadingOnes());
2337 EXPECT_EQ(0u, i64from63.countLeadingZeros());
2338 EXPECT_EQ(64u, i64from63.getActiveBits());
2339 EXPECT_EQ(63u, i64from63.countTrailingZeros());
2340 EXPECT_EQ(0u, i64from63.countTrailingOnes());
2341 EXPECT_EQ(1u, i64from63.countPopulation());
2344 TEST(APIntTest, setAllBits) {
2345 APInt i32(32, 0);
2346 i32.setAllBits();
2347 EXPECT_EQ(32u, i32.countLeadingOnes());
2348 EXPECT_EQ(0u, i32.countLeadingZeros());
2349 EXPECT_EQ(32u, i32.getActiveBits());
2350 EXPECT_EQ(0u, i32.countTrailingZeros());
2351 EXPECT_EQ(32u, i32.countTrailingOnes());
2352 EXPECT_EQ(32u, i32.countPopulation());
2354 APInt i64(64, 0);
2355 i64.setAllBits();
2356 EXPECT_EQ(64u, i64.countLeadingOnes());
2357 EXPECT_EQ(0u, i64.countLeadingZeros());
2358 EXPECT_EQ(64u, i64.getActiveBits());
2359 EXPECT_EQ(0u, i64.countTrailingZeros());
2360 EXPECT_EQ(64u, i64.countTrailingOnes());
2361 EXPECT_EQ(64u, i64.countPopulation());
2363 APInt i96(96, 0);
2364 i96.setAllBits();
2365 EXPECT_EQ(96u, i96.countLeadingOnes());
2366 EXPECT_EQ(0u, i96.countLeadingZeros());
2367 EXPECT_EQ(96u, i96.getActiveBits());
2368 EXPECT_EQ(0u, i96.countTrailingZeros());
2369 EXPECT_EQ(96u, i96.countTrailingOnes());
2370 EXPECT_EQ(96u, i96.countPopulation());
2372 APInt i128(128, 0);
2373 i128.setAllBits();
2374 EXPECT_EQ(128u, i128.countLeadingOnes());
2375 EXPECT_EQ(0u, i128.countLeadingZeros());
2376 EXPECT_EQ(128u, i128.getActiveBits());
2377 EXPECT_EQ(0u, i128.countTrailingZeros());
2378 EXPECT_EQ(128u, i128.countTrailingOnes());
2379 EXPECT_EQ(128u, i128.countPopulation());
2382 TEST(APIntTest, getLoBits) {
2383 APInt i32(32, 0xfa);
2384 i32.setHighBits(1);
2385 EXPECT_EQ(0xa, i32.getLoBits(4));
2386 APInt i128(128, 0xfa);
2387 i128.setHighBits(1);
2388 EXPECT_EQ(0xa, i128.getLoBits(4));
2391 TEST(APIntTest, getHiBits) {
2392 APInt i32(32, 0xfa);
2393 i32.setHighBits(2);
2394 EXPECT_EQ(0xc, i32.getHiBits(4));
2395 APInt i128(128, 0xfa);
2396 i128.setHighBits(2);
2397 EXPECT_EQ(0xc, i128.getHiBits(4));
2400 TEST(APIntTest, clearLowBits) {
2401 APInt i64hi32 = APInt::getAllOnes(64);
2402 i64hi32.clearLowBits(32);
2403 EXPECT_EQ(32u, i64hi32.countLeadingOnes());
2404 EXPECT_EQ(0u, i64hi32.countLeadingZeros());
2405 EXPECT_EQ(64u, i64hi32.getActiveBits());
2406 EXPECT_EQ(32u, i64hi32.countTrailingZeros());
2407 EXPECT_EQ(0u, i64hi32.countTrailingOnes());
2408 EXPECT_EQ(32u, i64hi32.countPopulation());
2410 APInt i128hi64 = APInt::getAllOnes(128);
2411 i128hi64.clearLowBits(64);
2412 EXPECT_EQ(64u, i128hi64.countLeadingOnes());
2413 EXPECT_EQ(0u, i128hi64.countLeadingZeros());
2414 EXPECT_EQ(128u, i128hi64.getActiveBits());
2415 EXPECT_EQ(64u, i128hi64.countTrailingZeros());
2416 EXPECT_EQ(0u, i128hi64.countTrailingOnes());
2417 EXPECT_EQ(64u, i128hi64.countPopulation());
2419 APInt i128hi24 = APInt::getAllOnes(128);
2420 i128hi24.clearLowBits(104);
2421 EXPECT_EQ(24u, i128hi24.countLeadingOnes());
2422 EXPECT_EQ(0u, i128hi24.countLeadingZeros());
2423 EXPECT_EQ(128u, i128hi24.getActiveBits());
2424 EXPECT_EQ(104u, i128hi24.countTrailingZeros());
2425 EXPECT_EQ(0u, i128hi24.countTrailingOnes());
2426 EXPECT_EQ(24u, i128hi24.countPopulation());
2428 APInt i128hi104 = APInt::getAllOnes(128);
2429 i128hi104.clearLowBits(24);
2430 EXPECT_EQ(104u, i128hi104.countLeadingOnes());
2431 EXPECT_EQ(0u, i128hi104.countLeadingZeros());
2432 EXPECT_EQ(128u, i128hi104.getActiveBits());
2433 EXPECT_EQ(24u, i128hi104.countTrailingZeros());
2434 EXPECT_EQ(0u, i128hi104.countTrailingOnes());
2435 EXPECT_EQ(104u, i128hi104.countPopulation());
2437 APInt i128hi0 = APInt::getAllOnes(128);
2438 i128hi0.clearLowBits(128);
2439 EXPECT_EQ(0u, i128hi0.countLeadingOnes());
2440 EXPECT_EQ(128u, i128hi0.countLeadingZeros());
2441 EXPECT_EQ(0u, i128hi0.getActiveBits());
2442 EXPECT_EQ(128u, i128hi0.countTrailingZeros());
2443 EXPECT_EQ(0u, i128hi0.countTrailingOnes());
2444 EXPECT_EQ(0u, i128hi0.countPopulation());
2446 APInt i80hi1 = APInt::getAllOnes(80);
2447 i80hi1.clearLowBits(79);
2448 EXPECT_EQ(1u, i80hi1.countLeadingOnes());
2449 EXPECT_EQ(0u, i80hi1.countLeadingZeros());
2450 EXPECT_EQ(80u, i80hi1.getActiveBits());
2451 EXPECT_EQ(79u, i80hi1.countTrailingZeros());
2452 EXPECT_EQ(0u, i80hi1.countTrailingOnes());
2453 EXPECT_EQ(1u, i80hi1.countPopulation());
2455 APInt i32hi16 = APInt::getAllOnes(32);
2456 i32hi16.clearLowBits(16);
2457 EXPECT_EQ(16u, i32hi16.countLeadingOnes());
2458 EXPECT_EQ(0u, i32hi16.countLeadingZeros());
2459 EXPECT_EQ(32u, i32hi16.getActiveBits());
2460 EXPECT_EQ(16u, i32hi16.countTrailingZeros());
2461 EXPECT_EQ(0u, i32hi16.countTrailingOnes());
2462 EXPECT_EQ(16u, i32hi16.countPopulation());
2465 TEST(APIntTest, GCD) {
2466 using APIntOps::GreatestCommonDivisor;
2468 for (unsigned Bits : {1, 2, 32, 63, 64, 65}) {
2469 // Test some corner cases near zero.
2470 APInt Zero(Bits, 0), One(Bits, 1);
2471 EXPECT_EQ(GreatestCommonDivisor(Zero, Zero), Zero);
2472 EXPECT_EQ(GreatestCommonDivisor(Zero, One), One);
2473 EXPECT_EQ(GreatestCommonDivisor(One, Zero), One);
2474 EXPECT_EQ(GreatestCommonDivisor(One, One), One);
2476 if (Bits > 1) {
2477 APInt Two(Bits, 2);
2478 EXPECT_EQ(GreatestCommonDivisor(Zero, Two), Two);
2479 EXPECT_EQ(GreatestCommonDivisor(One, Two), One);
2480 EXPECT_EQ(GreatestCommonDivisor(Two, Two), Two);
2482 // Test some corner cases near the highest representable value.
2483 APInt Max(Bits, 0);
2484 Max.setAllBits();
2485 EXPECT_EQ(GreatestCommonDivisor(Zero, Max), Max);
2486 EXPECT_EQ(GreatestCommonDivisor(One, Max), One);
2487 EXPECT_EQ(GreatestCommonDivisor(Two, Max), One);
2488 EXPECT_EQ(GreatestCommonDivisor(Max, Max), Max);
2490 APInt MaxOver2 = Max.udiv(Two);
2491 EXPECT_EQ(GreatestCommonDivisor(MaxOver2, Max), One);
2492 // Max - 1 == Max / 2 * 2, because Max is odd.
2493 EXPECT_EQ(GreatestCommonDivisor(MaxOver2, Max - 1), MaxOver2);
2497 // Compute the 20th Mersenne prime.
2498 const unsigned BitWidth = 4450;
2499 APInt HugePrime = APInt::getLowBitsSet(BitWidth, 4423);
2501 // 9931 and 123456 are coprime.
2502 APInt A = HugePrime * APInt(BitWidth, 9931);
2503 APInt B = HugePrime * APInt(BitWidth, 123456);
2504 APInt C = GreatestCommonDivisor(A, B);
2505 EXPECT_EQ(C, HugePrime);
2508 TEST(APIntTest, LogicalRightShift) {
2509 APInt i256(APInt::getHighBitsSet(256, 2));
2511 i256.lshrInPlace(1);
2512 EXPECT_EQ(1U, i256.countLeadingZeros());
2513 EXPECT_EQ(253U, i256.countTrailingZeros());
2514 EXPECT_EQ(2U, i256.countPopulation());
2516 i256.lshrInPlace(62);
2517 EXPECT_EQ(63U, i256.countLeadingZeros());
2518 EXPECT_EQ(191U, i256.countTrailingZeros());
2519 EXPECT_EQ(2U, i256.countPopulation());
2521 i256.lshrInPlace(65);
2522 EXPECT_EQ(128U, i256.countLeadingZeros());
2523 EXPECT_EQ(126U, i256.countTrailingZeros());
2524 EXPECT_EQ(2U, i256.countPopulation());
2526 i256.lshrInPlace(64);
2527 EXPECT_EQ(192U, i256.countLeadingZeros());
2528 EXPECT_EQ(62U, i256.countTrailingZeros());
2529 EXPECT_EQ(2U, i256.countPopulation());
2531 i256.lshrInPlace(63);
2532 EXPECT_EQ(255U, i256.countLeadingZeros());
2533 EXPECT_EQ(0U, i256.countTrailingZeros());
2534 EXPECT_EQ(1U, i256.countPopulation());
2536 // Ensure we handle large shifts of multi-word.
2537 const APInt neg_one(128, static_cast<uint64_t>(-1), true);
2538 EXPECT_EQ(0, neg_one.lshr(128));
2541 TEST(APIntTest, ArithmeticRightShift) {
2542 APInt i72(APInt::getHighBitsSet(72, 1));
2543 i72.ashrInPlace(46);
2544 EXPECT_EQ(47U, i72.countLeadingOnes());
2545 EXPECT_EQ(25U, i72.countTrailingZeros());
2546 EXPECT_EQ(47U, i72.countPopulation());
2548 i72 = APInt::getHighBitsSet(72, 1);
2549 i72.ashrInPlace(64);
2550 EXPECT_EQ(65U, i72.countLeadingOnes());
2551 EXPECT_EQ(7U, i72.countTrailingZeros());
2552 EXPECT_EQ(65U, i72.countPopulation());
2554 APInt i128(APInt::getHighBitsSet(128, 1));
2555 i128.ashrInPlace(64);
2556 EXPECT_EQ(65U, i128.countLeadingOnes());
2557 EXPECT_EQ(63U, i128.countTrailingZeros());
2558 EXPECT_EQ(65U, i128.countPopulation());
2560 // Ensure we handle large shifts of multi-word.
2561 const APInt signmin32(APInt::getSignedMinValue(32));
2562 EXPECT_TRUE(signmin32.ashr(32).isAllOnes());
2564 // Ensure we handle large shifts of multi-word.
2565 const APInt umax32(APInt::getSignedMaxValue(32));
2566 EXPECT_EQ(0, umax32.ashr(32));
2568 // Ensure we handle large shifts of multi-word.
2569 const APInt signmin128(APInt::getSignedMinValue(128));
2570 EXPECT_TRUE(signmin128.ashr(128).isAllOnes());
2572 // Ensure we handle large shifts of multi-word.
2573 const APInt umax128(APInt::getSignedMaxValue(128));
2574 EXPECT_EQ(0, umax128.ashr(128));
2577 TEST(APIntTest, LeftShift) {
2578 APInt i256(APInt::getLowBitsSet(256, 2));
2580 i256 <<= 1;
2581 EXPECT_EQ(253U, i256.countLeadingZeros());
2582 EXPECT_EQ(1U, i256.countTrailingZeros());
2583 EXPECT_EQ(2U, i256.countPopulation());
2585 i256 <<= 62;
2586 EXPECT_EQ(191U, i256.countLeadingZeros());
2587 EXPECT_EQ(63U, i256.countTrailingZeros());
2588 EXPECT_EQ(2U, i256.countPopulation());
2590 i256 <<= 65;
2591 EXPECT_EQ(126U, i256.countLeadingZeros());
2592 EXPECT_EQ(128U, i256.countTrailingZeros());
2593 EXPECT_EQ(2U, i256.countPopulation());
2595 i256 <<= 64;
2596 EXPECT_EQ(62U, i256.countLeadingZeros());
2597 EXPECT_EQ(192U, i256.countTrailingZeros());
2598 EXPECT_EQ(2U, i256.countPopulation());
2600 i256 <<= 63;
2601 EXPECT_EQ(0U, i256.countLeadingZeros());
2602 EXPECT_EQ(255U, i256.countTrailingZeros());
2603 EXPECT_EQ(1U, i256.countPopulation());
2605 // Ensure we handle large shifts of multi-word.
2606 const APInt neg_one(128, static_cast<uint64_t>(-1), true);
2607 EXPECT_EQ(0, neg_one.shl(128));
2610 TEST(APIntTest, isSubsetOf) {
2611 APInt i32_1(32, 1);
2612 APInt i32_2(32, 2);
2613 APInt i32_3(32, 3);
2614 EXPECT_FALSE(i32_3.isSubsetOf(i32_1));
2615 EXPECT_TRUE(i32_1.isSubsetOf(i32_3));
2616 EXPECT_FALSE(i32_2.isSubsetOf(i32_1));
2617 EXPECT_FALSE(i32_1.isSubsetOf(i32_2));
2618 EXPECT_TRUE(i32_3.isSubsetOf(i32_3));
2620 APInt i128_1(128, 1);
2621 APInt i128_2(128, 2);
2622 APInt i128_3(128, 3);
2623 EXPECT_FALSE(i128_3.isSubsetOf(i128_1));
2624 EXPECT_TRUE(i128_1.isSubsetOf(i128_3));
2625 EXPECT_FALSE(i128_2.isSubsetOf(i128_1));
2626 EXPECT_FALSE(i128_1.isSubsetOf(i128_2));
2627 EXPECT_TRUE(i128_3.isSubsetOf(i128_3));
2629 i128_1 <<= 64;
2630 i128_2 <<= 64;
2631 i128_3 <<= 64;
2632 EXPECT_FALSE(i128_3.isSubsetOf(i128_1));
2633 EXPECT_TRUE(i128_1.isSubsetOf(i128_3));
2634 EXPECT_FALSE(i128_2.isSubsetOf(i128_1));
2635 EXPECT_FALSE(i128_1.isSubsetOf(i128_2));
2636 EXPECT_TRUE(i128_3.isSubsetOf(i128_3));
2639 TEST(APIntTest, sext) {
2640 EXPECT_EQ(0, APInt(1, 0).sext(64));
2641 EXPECT_EQ(~uint64_t(0), APInt(1, 1).sext(64));
2643 APInt i32_max(APInt::getSignedMaxValue(32).sext(63));
2644 EXPECT_EQ(i32_max, i32_max.sext(63));
2645 EXPECT_EQ(32U, i32_max.countLeadingZeros());
2646 EXPECT_EQ(0U, i32_max.countTrailingZeros());
2647 EXPECT_EQ(31U, i32_max.countPopulation());
2649 APInt i32_min(APInt::getSignedMinValue(32).sext(63));
2650 EXPECT_EQ(i32_min, i32_min.sext(63));
2651 EXPECT_EQ(32U, i32_min.countLeadingOnes());
2652 EXPECT_EQ(31U, i32_min.countTrailingZeros());
2653 EXPECT_EQ(32U, i32_min.countPopulation());
2655 APInt i32_neg1(APInt(32, ~uint64_t(0)).sext(63));
2656 EXPECT_EQ(i32_neg1, i32_neg1.sext(63));
2657 EXPECT_EQ(63U, i32_neg1.countLeadingOnes());
2658 EXPECT_EQ(0U, i32_neg1.countTrailingZeros());
2659 EXPECT_EQ(63U, i32_neg1.countPopulation());
2662 TEST(APIntTest, trunc) {
2663 APInt val(32, 0xFFFFFFFF);
2664 EXPECT_EQ(0xFFFF, val.trunc(16));
2665 EXPECT_EQ(0xFFFFFFFF, val.trunc(32));
2668 TEST(APIntTest, concat) {
2669 APInt Int1(4, 0x1ULL);
2670 APInt Int3(4, 0x3ULL);
2672 EXPECT_EQ(0x31, Int3.concat(Int1));
2673 EXPECT_EQ(APInt(12, 0x313), Int3.concat(Int1).concat(Int3));
2674 EXPECT_EQ(APInt(16, 0x3313), Int3.concat(Int3).concat(Int1).concat(Int3));
2676 APInt I64(64, 0x3ULL);
2677 EXPECT_EQ(I64, I64.concat(I64).lshr(64).trunc(64));
2679 APInt I65(65, 0x3ULL);
2680 APInt I0 = APInt::getZeroWidth();
2681 EXPECT_EQ(I65, I65.concat(I0));
2682 EXPECT_EQ(I65, I0.concat(I65));
2685 TEST(APIntTest, multiply) {
2686 APInt i64(64, 1234);
2688 EXPECT_EQ(7006652, i64 * 5678);
2689 EXPECT_EQ(7006652, 5678 * i64);
2691 APInt i128 = APInt::getOneBitSet(128, 64);
2692 APInt i128_1234(128, 1234);
2693 i128_1234 <<= 64;
2694 EXPECT_EQ(i128_1234, i128 * 1234);
2695 EXPECT_EQ(i128_1234, 1234 * i128);
2697 APInt i96 = APInt::getOneBitSet(96, 64);
2698 i96 *= ~0ULL;
2699 EXPECT_EQ(32U, i96.countLeadingOnes());
2700 EXPECT_EQ(32U, i96.countPopulation());
2701 EXPECT_EQ(64U, i96.countTrailingZeros());
2704 TEST(APIntTest, RoundingUDiv) {
2705 for (uint64_t Ai = 1; Ai <= 255; Ai++) {
2706 APInt A(8, Ai);
2707 APInt Zero(8, 0);
2708 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::UP));
2709 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::DOWN));
2710 EXPECT_EQ(0, APIntOps::RoundingUDiv(Zero, A, APInt::Rounding::TOWARD_ZERO));
2712 for (uint64_t Bi = 1; Bi <= 255; Bi++) {
2713 APInt B(8, Bi);
2715 APInt Quo = APIntOps::RoundingUDiv(A, B, APInt::Rounding::UP);
2716 auto Prod = Quo.zext(16) * B.zext(16);
2717 EXPECT_TRUE(Prod.uge(Ai));
2718 if (Prod.ugt(Ai)) {
2719 EXPECT_TRUE(((Quo - 1).zext(16) * B.zext(16)).ult(Ai));
2723 APInt Quo = A.udiv(B);
2724 EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::TOWARD_ZERO));
2725 EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::DOWN));
2731 TEST(APIntTest, RoundingSDiv) {
2732 for (int64_t Ai = -128; Ai <= 127; Ai++) {
2733 APInt A(8, Ai);
2735 if (Ai != 0) {
2736 APInt Zero(8, 0);
2737 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::UP));
2738 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::DOWN));
2739 EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::TOWARD_ZERO));
2742 for (int64_t Bi = -128; Bi <= 127; Bi++) {
2743 if (Bi == 0)
2744 continue;
2746 APInt B(8, Bi);
2747 APInt QuoTowardZero = A.sdiv(B);
2749 APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::UP);
2750 if (A.srem(B).isNullValue()) {
2751 EXPECT_EQ(QuoTowardZero, Quo);
2752 } else if (A.isNegative() !=
2753 B.isNegative()) { // if the math quotient is negative.
2754 EXPECT_EQ(QuoTowardZero, Quo);
2755 } else {
2756 EXPECT_EQ(QuoTowardZero + 1, Quo);
2760 APInt Quo = APIntOps::RoundingSDiv(A, B, APInt::Rounding::DOWN);
2761 if (A.srem(B).isNullValue()) {
2762 EXPECT_EQ(QuoTowardZero, Quo);
2763 } else if (A.isNegative() !=
2764 B.isNegative()) { // if the math quotient is negative.
2765 EXPECT_EQ(QuoTowardZero - 1, Quo);
2766 } else {
2767 EXPECT_EQ(QuoTowardZero, Quo);
2770 EXPECT_EQ(QuoTowardZero,
2771 APIntOps::RoundingSDiv(A, B, APInt::Rounding::TOWARD_ZERO));
2776 TEST(APIntTest, umul_ov) {
2777 const std::pair<uint64_t, uint64_t> Overflows[] = {
2778 {0x8000000000000000, 2},
2779 {0x5555555555555556, 3},
2780 {4294967296, 4294967296},
2781 {4294967295, 4294967298},
2783 const std::pair<uint64_t, uint64_t> NonOverflows[] = {
2784 {0x7fffffffffffffff, 2},
2785 {0x5555555555555555, 3},
2786 {4294967295, 4294967297},
2789 bool Overflow;
2790 for (auto &X : Overflows) {
2791 APInt A(64, X.first);
2792 APInt B(64, X.second);
2793 (void)A.umul_ov(B, Overflow);
2794 EXPECT_TRUE(Overflow);
2796 for (auto &X : NonOverflows) {
2797 APInt A(64, X.first);
2798 APInt B(64, X.second);
2799 (void)A.umul_ov(B, Overflow);
2800 EXPECT_FALSE(Overflow);
2803 for (unsigned Bits = 1; Bits <= 5; ++Bits)
2804 for (unsigned A = 0; A != 1u << Bits; ++A)
2805 for (unsigned B = 0; B != 1u << Bits; ++B) {
2806 APInt N1 = APInt(Bits, A), N2 = APInt(Bits, B);
2807 APInt Narrow = N1.umul_ov(N2, Overflow);
2808 APInt Wide = N1.zext(2 * Bits) * N2.zext(2 * Bits);
2809 EXPECT_EQ(Wide.trunc(Bits), Narrow);
2810 EXPECT_EQ(Narrow.zext(2 * Bits) != Wide, Overflow);
2814 TEST(APIntTest, smul_ov) {
2815 for (unsigned Bits = 1; Bits <= 5; ++Bits)
2816 for (unsigned A = 0; A != 1u << Bits; ++A)
2817 for (unsigned B = 0; B != 1u << Bits; ++B) {
2818 bool Overflow;
2819 APInt N1 = APInt(Bits, A), N2 = APInt(Bits, B);
2820 APInt Narrow = N1.smul_ov(N2, Overflow);
2821 APInt Wide = N1.sext(2 * Bits) * N2.sext(2 * Bits);
2822 EXPECT_EQ(Wide.trunc(Bits), Narrow);
2823 EXPECT_EQ(Narrow.sext(2 * Bits) != Wide, Overflow);
2827 TEST(APIntTest, SolveQuadraticEquationWrap) {
2828 // Verify that "Solution" is the first non-negative integer that solves
2829 // Ax^2 + Bx + C = "0 or overflow", i.e. that it is a correct solution
2830 // as calculated by SolveQuadraticEquationWrap.
2831 auto Validate = [] (int A, int B, int C, unsigned Width, int Solution) {
2832 int Mask = (1 << Width) - 1;
2834 // Solution should be non-negative.
2835 EXPECT_GE(Solution, 0);
2837 auto OverflowBits = [] (int64_t V, unsigned W) {
2838 return V & -(1 << W);
2841 int64_t Over0 = OverflowBits(C, Width);
2843 auto IsZeroOrOverflow = [&] (int X) {
2844 int64_t ValueAtX = A*X*X + B*X + C;
2845 int64_t OverX = OverflowBits(ValueAtX, Width);
2846 return (ValueAtX & Mask) == 0 || OverX != Over0;
2849 auto EquationToString = [&] (const char *X_str) {
2850 return (Twine(A) + Twine(X_str) + Twine("^2 + ") + Twine(B) +
2851 Twine(X_str) + Twine(" + ") + Twine(C) + Twine(", bitwidth: ") +
2852 Twine(Width)).str();
2855 auto IsSolution = [&] (const char *X_str, int X) {
2856 if (IsZeroOrOverflow(X))
2857 return ::testing::AssertionSuccess()
2858 << X << " is a solution of " << EquationToString(X_str);
2859 return ::testing::AssertionFailure()
2860 << X << " is not an expected solution of "
2861 << EquationToString(X_str);
2864 auto IsNotSolution = [&] (const char *X_str, int X) {
2865 if (!IsZeroOrOverflow(X))
2866 return ::testing::AssertionSuccess()
2867 << X << " is not a solution of " << EquationToString(X_str);
2868 return ::testing::AssertionFailure()
2869 << X << " is an unexpected solution of "
2870 << EquationToString(X_str);
2873 // This is the important part: make sure that there is no solution that
2874 // is less than the calculated one.
2875 if (Solution > 0) {
2876 for (int X = 1; X < Solution-1; ++X)
2877 EXPECT_PRED_FORMAT1(IsNotSolution, X);
2880 // Verify that the calculated solution is indeed a solution.
2881 EXPECT_PRED_FORMAT1(IsSolution, Solution);
2884 // Generate all possible quadratic equations with Width-bit wide integer
2885 // coefficients, get the solution from SolveQuadraticEquationWrap, and
2886 // verify that the solution is correct.
2887 auto Iterate = [&] (unsigned Width) {
2888 assert(1 < Width && Width < 32);
2889 int Low = -(1 << (Width-1));
2890 int High = (1 << (Width-1));
2892 for (int A = Low; A != High; ++A) {
2893 if (A == 0)
2894 continue;
2895 for (int B = Low; B != High; ++B) {
2896 for (int C = Low; C != High; ++C) {
2897 Optional<APInt> S = APIntOps::SolveQuadraticEquationWrap(
2898 APInt(Width, A), APInt(Width, B),
2899 APInt(Width, C), Width);
2900 if (S)
2901 Validate(A, B, C, Width, S->getSExtValue());
2907 // Test all widths in [2..6].
2908 for (unsigned i = 2; i <= 6; ++i)
2909 Iterate(i);
2912 TEST(APIntTest, MultiplicativeInverseExaustive) {
2913 for (unsigned BitWidth = 1; BitWidth <= 16; ++BitWidth) {
2914 for (unsigned Value = 0; Value < (1u << BitWidth); ++Value) {
2915 APInt V = APInt(BitWidth, Value);
2916 APInt MulInv =
2917 V.zext(BitWidth + 1)
2918 .multiplicativeInverse(APInt::getSignedMinValue(BitWidth + 1))
2919 .trunc(BitWidth);
2920 APInt One = V * MulInv;
2921 if (!V.isNullValue() && V.countTrailingZeros() == 0) {
2922 // Multiplicative inverse exists for all odd numbers.
2923 EXPECT_TRUE(One.isOneValue());
2924 } else {
2925 // Multiplicative inverse does not exist for even numbers (and 0).
2926 EXPECT_TRUE(MulInv.isNullValue());
2932 TEST(APIntTest, GetMostSignificantDifferentBit) {
2933 EXPECT_EQ(APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 0)),
2934 llvm::None);
2935 EXPECT_EQ(
2936 APIntOps::GetMostSignificantDifferentBit(APInt(8, 42), APInt(8, 42)),
2937 llvm::None);
2938 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 1)),
2939 0u);
2940 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 2)),
2941 1u);
2942 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 0), APInt(8, 3)),
2943 1u);
2944 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 0)),
2945 0u);
2946 EXPECT_EQ(APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 1)),
2947 llvm::None);
2948 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 2)),
2949 1u);
2950 EXPECT_EQ(*APIntOps::GetMostSignificantDifferentBit(APInt(8, 1), APInt(8, 3)),
2951 1u);
2952 EXPECT_EQ(
2953 *APIntOps::GetMostSignificantDifferentBit(APInt(8, 42), APInt(8, 112)),
2954 6u);
2957 TEST(APIntTest, GetMostSignificantDifferentBitExaustive) {
2958 auto GetHighestDifferentBitBruteforce =
2959 [](const APInt &V0, const APInt &V1) -> llvm::Optional<unsigned> {
2960 assert(V0.getBitWidth() == V1.getBitWidth() && "Must have same bitwidth");
2961 if (V0 == V1)
2962 return llvm::None; // Bitwise identical.
2963 // There is a mismatch. Let's find the most significant different bit.
2964 for (int Bit = V0.getBitWidth() - 1; Bit >= 0; --Bit) {
2965 if (V0[Bit] == V1[Bit])
2966 continue;
2967 return Bit;
2969 llvm_unreachable("Must have found bit mismatch.");
2972 for (unsigned BitWidth = 1; BitWidth <= 8; ++BitWidth) {
2973 for (unsigned V0 = 0; V0 < (1u << BitWidth); ++V0) {
2974 for (unsigned V1 = 0; V1 < (1u << BitWidth); ++V1) {
2975 APInt A = APInt(BitWidth, V0);
2976 APInt B = APInt(BitWidth, V1);
2978 auto Bit = APIntOps::GetMostSignificantDifferentBit(A, B);
2979 EXPECT_EQ(Bit, GetHighestDifferentBitBruteforce(A, B));
2981 if (!Bit)
2982 EXPECT_EQ(A, B);
2983 else {
2984 EXPECT_NE(A, B);
2985 for (unsigned NumLowBits = 0; NumLowBits <= BitWidth; ++NumLowBits) {
2986 APInt Adash = A;
2987 Adash.clearLowBits(NumLowBits);
2988 APInt Bdash = B;
2989 Bdash.clearLowBits(NumLowBits);
2990 // Clearing only low bits up to and including *Bit is sufficient
2991 // to make values equal.
2992 if (NumLowBits >= 1 + *Bit)
2993 EXPECT_EQ(Adash, Bdash);
2994 else
2995 EXPECT_NE(Adash, Bdash);
3003 TEST(APIntTest, SignbitZeroChecks) {
3004 EXPECT_TRUE(APInt(8, -1).isNegative());
3005 EXPECT_FALSE(APInt(8, -1).isNonNegative());
3006 EXPECT_FALSE(APInt(8, -1).isStrictlyPositive());
3007 EXPECT_TRUE(APInt(8, -1).isNonPositive());
3009 EXPECT_FALSE(APInt(8, 0).isNegative());
3010 EXPECT_TRUE(APInt(8, 0).isNonNegative());
3011 EXPECT_FALSE(APInt(8, 0).isStrictlyPositive());
3012 EXPECT_TRUE(APInt(8, 0).isNonPositive());
3014 EXPECT_FALSE(APInt(8, 1).isNegative());
3015 EXPECT_TRUE(APInt(8, 1).isNonNegative());
3016 EXPECT_TRUE(APInt(8, 1).isStrictlyPositive());
3017 EXPECT_FALSE(APInt(8, 1).isNonPositive());
3020 TEST(APIntTest, ZeroWidth) {
3021 // Zero width Constructors.
3022 auto ZW = APInt::getZeroWidth();
3023 EXPECT_EQ(0U, ZW.getBitWidth());
3024 EXPECT_EQ(0U, APInt(0, ArrayRef<uint64_t>({0, 1, 2})).getBitWidth());
3025 EXPECT_EQ(0U, APInt(0, "0", 10).getBitWidth());
3027 // Default constructor is single bit wide.
3028 EXPECT_EQ(1U, APInt().getBitWidth());
3030 // Copy ctor (move is down below).
3031 APInt ZW2(ZW);
3032 EXPECT_EQ(0U, ZW2.getBitWidth());
3033 // Assignment
3034 ZW = ZW2;
3035 EXPECT_EQ(0U, ZW.getBitWidth());
3037 // Methods like getLowBitsSet work with zero bits.
3038 EXPECT_EQ(0U, APInt::getLowBitsSet(0, 0).getBitWidth());
3039 EXPECT_EQ(0U, APInt::getSplat(0, ZW).getBitWidth());
3040 EXPECT_EQ(0U, APInt(4, 10).extractBits(0, 2).getBitWidth());
3042 // Logical operators.
3043 ZW |= ZW2;
3044 ZW &= ZW2;
3045 ZW ^= ZW2;
3046 ZW |= 42; // These ignore high bits of the literal.
3047 ZW &= 42;
3048 ZW ^= 42;
3049 EXPECT_EQ(1, ZW.isIntN(0));
3051 // Modulo Arithmetic. Divide/Rem aren't defined on division by zero, so they
3052 // aren't supported.
3053 ZW += ZW2;
3054 ZW -= ZW2;
3055 ZW *= ZW2;
3057 // Logical Shifts and rotates, the amount must be <= bitwidth.
3058 ZW <<= 0;
3059 ZW.lshrInPlace(0);
3060 (void)ZW.rotl(0);
3061 (void)ZW.rotr(0);
3063 // Comparisons.
3064 EXPECT_EQ(1, ZW == ZW);
3065 EXPECT_EQ(0, ZW != ZW);
3066 EXPECT_EQ(0, ZW.ult(ZW));
3068 // Mutations.
3069 ZW.setBitsWithWrap(0, 0);
3070 ZW.setBits(0, 0);
3071 ZW.clearAllBits();
3072 ZW.flipAllBits();
3074 // Leading, trailing, ctpop, etc
3075 EXPECT_EQ(0U, ZW.countLeadingZeros());
3076 EXPECT_EQ(0U, ZW.countLeadingOnes());
3077 EXPECT_EQ(0U, ZW.countPopulation());
3078 EXPECT_EQ(0U, ZW.reverseBits().getBitWidth());
3079 EXPECT_EQ(0U, ZW.getHiBits(0).getBitWidth());
3080 EXPECT_EQ(0U, ZW.getLoBits(0).getBitWidth());
3081 EXPECT_EQ(0, ZW.zext(4));
3082 EXPECT_EQ(0U, APInt(4, 3).trunc(0).getBitWidth());
3083 EXPECT_TRUE(ZW.isAllOnes());
3085 // Zero extension.
3086 EXPECT_EQ(0U, ZW.getZExtValue());
3088 SmallString<42> STR;
3089 ZW.toStringUnsigned(STR);
3090 EXPECT_EQ("0", STR);
3092 // Move ctor (keep at the end of the method since moves are destructive).
3093 APInt MZW1(std::move(ZW));
3094 EXPECT_EQ(0U, MZW1.getBitWidth());
3095 // Move Assignment
3096 MZW1 = std::move(ZW2);
3097 EXPECT_EQ(0U, MZW1.getBitWidth());
3100 TEST(APIntTest, ScaleBitMask) {
3101 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x00), 8), APInt(8, 0x00));
3102 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x01), 8), APInt(8, 0x0F));
3103 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x02), 8), APInt(8, 0xF0));
3104 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x03), 8), APInt(8, 0xFF));
3106 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 4), APInt(4, 0x00));
3107 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xFF), 4), APInt(4, 0x0F));
3108 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xE4), 4), APInt(4, 0x0E));
3110 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 8), APInt(8, 0x00));
3112 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getNullValue(1024), 4096),
3113 APInt::getNullValue(4096));
3114 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getAllOnes(4096), 256),
3115 APInt::getAllOnes(256));
3116 EXPECT_EQ(APIntOps::ScaleBitMask(APInt::getOneBitSet(4096, 32), 256),
3117 APInt::getOneBitSet(256, 2));
3119 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x00), 8, true), APInt(8, 0x00));
3120 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x01), 8, true), APInt(8, 0x0F));
3121 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x02), 8, true), APInt(8, 0xF0));
3122 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(2, 0x03), 8, true), APInt(8, 0xFF));
3124 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0x00), 4, true), APInt(4, 0x00));
3125 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xFF), 4, true), APInt(4, 0x0F));
3126 EXPECT_EQ(APIntOps::ScaleBitMask(APInt(8, 0xE4), 4, true), APInt(4, 0x08));
3129 } // end anonymous namespace