1d330f0d369cdeab6a67a1e11783f5a0f0b4b2c8
[oota-llvm.git] / unittests / ADT / APIntTest.cpp
1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/APInt.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "gtest/gtest.h"
13 #include <ostream>
14
15 using namespace llvm;
16
17 namespace {
18
19 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
20 TEST(APIntTest, ShiftLeftByZero) {
21   APInt One = APInt::getNullValue(65) + 1;
22   APInt Shl = One.shl(0);
23   EXPECT_TRUE(Shl[0]);
24   EXPECT_FALSE(Shl[1]);
25 }
26
27 TEST(APIntTest, i128_NegativeCount) {
28   APInt Minus3(128, static_cast<uint64_t>(-3), true);
29   EXPECT_EQ(126u, Minus3.countLeadingOnes());
30   EXPECT_EQ(-3, Minus3.getSExtValue());
31
32   APInt Minus1(128, static_cast<uint64_t>(-1), true);
33   EXPECT_EQ(0u, Minus1.countLeadingZeros());
34   EXPECT_EQ(128u, Minus1.countLeadingOnes());
35   EXPECT_EQ(128u, Minus1.getActiveBits());
36   EXPECT_EQ(0u, Minus1.countTrailingZeros());
37   EXPECT_EQ(128u, Minus1.countTrailingOnes());
38   EXPECT_EQ(128u, Minus1.countPopulation());
39   EXPECT_EQ(-1, Minus1.getSExtValue());
40 }
41
42 // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it.
43 #if defined(__llvm__) || !defined(__FreeBSD__)
44
45 TEST(APIntTest, i33_Count) {
46   APInt i33minus2(33, static_cast<uint64_t>(-2), true);
47   EXPECT_EQ(0u, i33minus2.countLeadingZeros());
48   EXPECT_EQ(32u, i33minus2.countLeadingOnes());
49   EXPECT_EQ(33u, i33minus2.getActiveBits());
50   EXPECT_EQ(1u, i33minus2.countTrailingZeros());
51   EXPECT_EQ(32u, i33minus2.countPopulation());
52   EXPECT_EQ(-2, i33minus2.getSExtValue());
53   EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue());
54 }
55
56 #endif
57
58 TEST(APIntTest, i65_Count) {
59   APInt i65(65, 0, true);
60   EXPECT_EQ(65u, i65.countLeadingZeros());
61   EXPECT_EQ(0u, i65.countLeadingOnes());
62   EXPECT_EQ(0u, i65.getActiveBits());
63   EXPECT_EQ(1u, i65.getActiveWords());
64   EXPECT_EQ(65u, i65.countTrailingZeros());
65   EXPECT_EQ(0u, i65.countPopulation());
66
67   APInt i65minus(65, 0, true);
68   i65minus.setBit(64);
69   EXPECT_EQ(0u, i65minus.countLeadingZeros());
70   EXPECT_EQ(1u, i65minus.countLeadingOnes());
71   EXPECT_EQ(65u, i65minus.getActiveBits());
72   EXPECT_EQ(64u, i65minus.countTrailingZeros());
73   EXPECT_EQ(1u, i65minus.countPopulation());
74 }
75
76 TEST(APIntTest, i128_PositiveCount) {
77   APInt u128max = APInt::getAllOnesValue(128);
78   EXPECT_EQ(128u, u128max.countLeadingOnes());
79   EXPECT_EQ(0u, u128max.countLeadingZeros());
80   EXPECT_EQ(128u, u128max.getActiveBits());
81   EXPECT_EQ(0u, u128max.countTrailingZeros());
82   EXPECT_EQ(128u, u128max.countTrailingOnes());
83   EXPECT_EQ(128u, u128max.countPopulation());
84
85   APInt u64max(128, static_cast<uint64_t>(-1), false);
86   EXPECT_EQ(64u, u64max.countLeadingZeros());
87   EXPECT_EQ(0u, u64max.countLeadingOnes());
88   EXPECT_EQ(64u, u64max.getActiveBits());
89   EXPECT_EQ(0u, u64max.countTrailingZeros());
90   EXPECT_EQ(64u, u64max.countTrailingOnes());
91   EXPECT_EQ(64u, u64max.countPopulation());
92   EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue());
93
94   APInt zero(128, 0, true);
95   EXPECT_EQ(128u, zero.countLeadingZeros());
96   EXPECT_EQ(0u, zero.countLeadingOnes());
97   EXPECT_EQ(0u, zero.getActiveBits());
98   EXPECT_EQ(128u, zero.countTrailingZeros());
99   EXPECT_EQ(0u, zero.countTrailingOnes());
100   EXPECT_EQ(0u, zero.countPopulation());
101   EXPECT_EQ(0u, zero.getSExtValue());
102   EXPECT_EQ(0u, zero.getZExtValue());
103
104   APInt one(128, 1, true);
105   EXPECT_EQ(127u, one.countLeadingZeros());
106   EXPECT_EQ(0u, one.countLeadingOnes());
107   EXPECT_EQ(1u, one.getActiveBits());
108   EXPECT_EQ(0u, one.countTrailingZeros());
109   EXPECT_EQ(1u, one.countTrailingOnes());
110   EXPECT_EQ(1u, one.countPopulation());
111   EXPECT_EQ(1, one.getSExtValue());
112   EXPECT_EQ(1u, one.getZExtValue());
113 }
114
115 TEST(APIntTest, i1) {
116   const APInt neg_two(1, static_cast<uint64_t>(-2), true);
117   const APInt neg_one(1, static_cast<uint64_t>(-1), true);
118   const APInt zero(1, 0);
119   const APInt one(1, 1);
120   const APInt two(1, 2);
121
122   EXPECT_EQ(0, neg_two.getSExtValue());
123   EXPECT_EQ(-1, neg_one.getSExtValue());
124   EXPECT_EQ(1u, neg_one.getZExtValue());
125   EXPECT_EQ(0u, zero.getZExtValue());
126   EXPECT_EQ(-1, one.getSExtValue());
127   EXPECT_EQ(1u, one.getZExtValue());
128   EXPECT_EQ(0u, two.getZExtValue());
129   EXPECT_EQ(0, two.getSExtValue());
130
131   // Basic equalities for 1-bit values.
132   EXPECT_EQ(zero, two);
133   EXPECT_EQ(zero, neg_two);
134   EXPECT_EQ(one, neg_one);
135   EXPECT_EQ(two, neg_two);
136
137   // Additions.
138   EXPECT_EQ(two, one + one);
139   EXPECT_EQ(zero, neg_one + one);
140   EXPECT_EQ(neg_two, neg_one + neg_one);
141
142   // Subtractions.
143   EXPECT_EQ(neg_two, neg_one - one);
144   EXPECT_EQ(two, one - neg_one);
145   EXPECT_EQ(zero, one - one);
146
147   // Shifts.
148   EXPECT_EQ(zero, one << one);
149   EXPECT_EQ(one, one << zero);
150   EXPECT_EQ(zero, one.shl(1));
151   EXPECT_EQ(one, one.shl(0));
152   EXPECT_EQ(zero, one.lshr(1));
153   EXPECT_EQ(zero, one.ashr(1));
154
155   // Rotates.
156   EXPECT_EQ(one, one.rotl(0));
157   EXPECT_EQ(one, one.rotl(1));
158   EXPECT_EQ(one, one.rotr(0));
159   EXPECT_EQ(one, one.rotr(1));
160
161   // Multiplies.
162   EXPECT_EQ(neg_one, neg_one * one);
163   EXPECT_EQ(neg_one, one * neg_one);
164   EXPECT_EQ(one, neg_one * neg_one);
165   EXPECT_EQ(one, one * one);
166
167   // Divides.
168   EXPECT_EQ(neg_one, one.sdiv(neg_one));
169   EXPECT_EQ(neg_one, neg_one.sdiv(one));
170   EXPECT_EQ(one, neg_one.sdiv(neg_one));
171   EXPECT_EQ(one, one.sdiv(one));
172
173   EXPECT_EQ(neg_one, one.udiv(neg_one));
174   EXPECT_EQ(neg_one, neg_one.udiv(one));
175   EXPECT_EQ(one, neg_one.udiv(neg_one));
176   EXPECT_EQ(one, one.udiv(one));
177
178   // Remainders.
179   EXPECT_EQ(zero, neg_one.srem(one));
180   EXPECT_EQ(zero, neg_one.urem(one));
181   EXPECT_EQ(zero, one.srem(neg_one));
182
183   // sdivrem
184   {
185   APInt q(8, 0);
186   APInt r(8, 0);
187   APInt one(8, 1);
188   APInt two(8, 2);
189   APInt nine(8, 9);
190   APInt four(8, 4);
191
192   EXPECT_EQ(nine.srem(two), one);
193   EXPECT_EQ(nine.srem(-two), one);
194   EXPECT_EQ((-nine).srem(two), -one);
195   EXPECT_EQ((-nine).srem(-two), -one);
196
197   APInt::sdivrem(nine, two, q, r);
198   EXPECT_EQ(four, q);
199   EXPECT_EQ(one, r);
200   APInt::sdivrem(-nine, two, q, r);
201   EXPECT_EQ(-four, q);
202   EXPECT_EQ(-one, r);
203   APInt::sdivrem(nine, -two, q, r);
204   EXPECT_EQ(-four, q);
205   EXPECT_EQ(one, r);
206   APInt::sdivrem(-nine, -two, q, r);
207   EXPECT_EQ(four, q);
208   EXPECT_EQ(-one, r);
209   }
210 }
211
212 TEST(APIntTest, fromString) {
213   EXPECT_EQ(APInt(32, 0), APInt(32,   "0", 2));
214   EXPECT_EQ(APInt(32, 1), APInt(32,   "1", 2));
215   EXPECT_EQ(APInt(32, 2), APInt(32,  "10", 2));
216   EXPECT_EQ(APInt(32, 3), APInt(32,  "11", 2));
217   EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2));
218
219   EXPECT_EQ(APInt(32, 0), APInt(32,   "+0", 2));
220   EXPECT_EQ(APInt(32, 1), APInt(32,   "+1", 2));
221   EXPECT_EQ(APInt(32, 2), APInt(32,  "+10", 2));
222   EXPECT_EQ(APInt(32, 3), APInt(32,  "+11", 2));
223   EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2));
224
225   EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32,   "-0", 2));
226   EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32,   "-1", 2));
227   EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32,  "-10", 2));
228   EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32,  "-11", 2));
229   EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2));
230
231
232   EXPECT_EQ(APInt(32,  0), APInt(32,  "0",  8));
233   EXPECT_EQ(APInt(32,  1), APInt(32,  "1",  8));
234   EXPECT_EQ(APInt(32,  7), APInt(32,  "7",  8));
235   EXPECT_EQ(APInt(32,  8), APInt(32,  "10", 8));
236   EXPECT_EQ(APInt(32, 15), APInt(32,  "17", 8));
237   EXPECT_EQ(APInt(32, 16), APInt(32,  "20", 8));
238
239   EXPECT_EQ(APInt(32,  +0), APInt(32,  "+0",  8));
240   EXPECT_EQ(APInt(32,  +1), APInt(32,  "+1",  8));
241   EXPECT_EQ(APInt(32,  +7), APInt(32,  "+7",  8));
242   EXPECT_EQ(APInt(32,  +8), APInt(32,  "+10", 8));
243   EXPECT_EQ(APInt(32, +15), APInt(32,  "+17", 8));
244   EXPECT_EQ(APInt(32, +16), APInt(32,  "+20", 8));
245
246   EXPECT_EQ(APInt(32,  uint64_t(-0LL)), APInt(32,  "-0",  8));
247   EXPECT_EQ(APInt(32,  uint64_t(-1LL)), APInt(32,  "-1",  8));
248   EXPECT_EQ(APInt(32,  uint64_t(-7LL)), APInt(32,  "-7",  8));
249   EXPECT_EQ(APInt(32,  uint64_t(-8LL)), APInt(32,  "-10", 8));
250   EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32,  "-17", 8));
251   EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32,  "-20", 8));
252
253
254   EXPECT_EQ(APInt(32,  0), APInt(32,  "0", 10));
255   EXPECT_EQ(APInt(32,  1), APInt(32,  "1", 10));
256   EXPECT_EQ(APInt(32,  9), APInt(32,  "9", 10));
257   EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10));
258   EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10));
259   EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10));
260
261   EXPECT_EQ(APInt(32,  uint64_t(-0LL)), APInt(32,  "-0", 10));
262   EXPECT_EQ(APInt(32,  uint64_t(-1LL)), APInt(32,  "-1", 10));
263   EXPECT_EQ(APInt(32,  uint64_t(-9LL)), APInt(32,  "-9", 10));
264   EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10));
265   EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10));
266   EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10));
267
268
269   EXPECT_EQ(APInt(32,  0), APInt(32,  "0", 16));
270   EXPECT_EQ(APInt(32,  1), APInt(32,  "1", 16));
271   EXPECT_EQ(APInt(32, 15), APInt(32,  "F", 16));
272   EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16));
273   EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16));
274   EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16));
275
276   EXPECT_EQ(APInt(32,  uint64_t(-0LL)), APInt(32,  "-0", 16));
277   EXPECT_EQ(APInt(32,  uint64_t(-1LL)), APInt(32,  "-1", 16));
278   EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32,  "-F", 16));
279   EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16));
280   EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16));
281   EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16));
282
283   EXPECT_EQ(APInt(32,  0), APInt(32,  "0", 36));
284   EXPECT_EQ(APInt(32,  1), APInt(32,  "1", 36));
285   EXPECT_EQ(APInt(32, 35), APInt(32,  "Z", 36));
286   EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36));
287   EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36));
288   EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36));
289   
290   EXPECT_EQ(APInt(32,  uint64_t(-0LL)), APInt(32,  "-0", 36));
291   EXPECT_EQ(APInt(32,  uint64_t(-1LL)), APInt(32,  "-1", 36));
292   EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32,  "-Z", 36));
293   EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36));
294   EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36));
295   EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36));
296 }
297
298 TEST(APIntTest, FromArray) {
299   EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1)));
300 }
301
302 TEST(APIntTest, StringBitsNeeded2) {
303   EXPECT_EQ(1U, APInt::getBitsNeeded(  "0", 2));
304   EXPECT_EQ(1U, APInt::getBitsNeeded(  "1", 2));
305   EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2));
306   EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2));
307   EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2));
308
309   EXPECT_EQ(1U, APInt::getBitsNeeded(  "+0", 2));
310   EXPECT_EQ(1U, APInt::getBitsNeeded(  "+1", 2));
311   EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2));
312   EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2));
313   EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2));
314
315   EXPECT_EQ(2U, APInt::getBitsNeeded(  "-0", 2));
316   EXPECT_EQ(2U, APInt::getBitsNeeded(  "-1", 2));
317   EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2));
318   EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2));
319   EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2));
320 }
321
322 TEST(APIntTest, StringBitsNeeded8) {
323   EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8));
324   EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8));
325   EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8));
326   EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8));
327   EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8));
328
329   EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8));
330   EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8));
331   EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8));
332   EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8));
333   EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8));
334
335   EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8));
336   EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8));
337   EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8));
338   EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8));
339   EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8));
340 }
341
342 TEST(APIntTest, StringBitsNeeded10) {
343   EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10));
344   EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10));
345   EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10));
346   EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10));
347   EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10));
348   EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10));
349
350   EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10));
351   EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10));
352   EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10));
353   EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10));
354   EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10));
355
356   EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10));
357   EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10));
358   EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10));
359   EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10));
360   EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10));
361 }
362
363 TEST(APIntTest, StringBitsNeeded16) {
364   EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16));
365   EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16));
366   EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16));
367   EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16));
368   EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16));
369
370   EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16));
371   EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16));
372   EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16));
373   EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16));
374   EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16));
375
376   EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16));
377   EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16));
378   EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16));
379   EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16));
380   EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16));
381 }
382
383 TEST(APIntTest, toString) {
384   SmallString<16> S;
385   bool isSigned;
386
387   APInt(8, 0).toString(S, 2, true, true);
388   EXPECT_EQ(S.str().str(), "0b0");
389   S.clear();
390   APInt(8, 0).toString(S, 8, true, true);
391   EXPECT_EQ(S.str().str(), "00");
392   S.clear();
393   APInt(8, 0).toString(S, 10, true, true);
394   EXPECT_EQ(S.str().str(), "0");
395   S.clear();
396   APInt(8, 0).toString(S, 16, true, true);
397   EXPECT_EQ(S.str().str(), "0x0");
398   S.clear();
399   APInt(8, 0).toString(S, 36, true, false);
400   EXPECT_EQ(S.str().str(), "0");
401   S.clear();
402
403   isSigned = false;
404   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
405   EXPECT_EQ(S.str().str(), "0b11111111");
406   S.clear();
407   APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
408   EXPECT_EQ(S.str().str(), "0377");
409   S.clear();
410   APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
411   EXPECT_EQ(S.str().str(), "255");
412   S.clear();
413   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
414   EXPECT_EQ(S.str().str(), "0xFF");
415   S.clear();
416   APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
417   EXPECT_EQ(S.str().str(), "73");
418   S.clear();
419
420   isSigned = true;
421   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
422   EXPECT_EQ(S.str().str(), "-0b1");
423   S.clear();
424   APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
425   EXPECT_EQ(S.str().str(), "-01");
426   S.clear();
427   APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
428   EXPECT_EQ(S.str().str(), "-1");
429   S.clear();
430   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
431   EXPECT_EQ(S.str().str(), "-0x1");
432   S.clear();
433   APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
434   EXPECT_EQ(S.str().str(), "-1");
435   S.clear();
436 }
437
438 TEST(APIntTest, Log2) {
439   EXPECT_EQ(APInt(15, 7).logBase2(), 2U);
440   EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U);
441   EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1);
442   EXPECT_EQ(APInt(15, 8).logBase2(), 3U);
443   EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U);
444   EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3);
445   EXPECT_EQ(APInt(15, 9).logBase2(), 3U);
446   EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U);
447   EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
448 }
449
450 TEST(APIntTest, magic) {
451   EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
452   EXPECT_EQ(APInt(32, 3).magic().s, 0U);
453   EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
454   EXPECT_EQ(APInt(32, 5).magic().s, 1U);
455   EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
456   EXPECT_EQ(APInt(32, 7).magic().s, 2U);
457 }
458
459 TEST(APIntTest, magicu) {
460   EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
461   EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
462   EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
463   EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
464   EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
465   EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
466   EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16));
467   EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U);
468 }
469
470 #ifdef GTEST_HAS_DEATH_TEST
471 #ifndef NDEBUG
472 TEST(APIntTest, StringDeath) {
473   EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small");
474   EXPECT_DEATH(APInt(32, "", 0), "Invalid string length");
475   EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!");
476   EXPECT_DEATH(APInt(32, "", 10), "Invalid string length");
477   EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value.");
478   EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width");
479   EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length");
480   EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string");
481   EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string");
482 }
483 #endif
484 #endif
485
486 TEST(APIntTest, mul_clear) {
487   APInt ValA(65, -1ULL);
488   APInt ValB(65, 4);
489   APInt ValC(65, 0);
490   ValC = ValA * ValB;
491   ValA *= ValB;
492   EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false));
493 }
494
495 TEST(APIntTest, Rotate) {
496   EXPECT_EQ(APInt(8, 1),  APInt(8, 1).rotl(0));
497   EXPECT_EQ(APInt(8, 2),  APInt(8, 1).rotl(1));
498   EXPECT_EQ(APInt(8, 4),  APInt(8, 1).rotl(2));
499   EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4));
500   EXPECT_EQ(APInt(8, 1),  APInt(8, 1).rotl(8));
501
502   EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0));
503   EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1));
504   EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2));
505   EXPECT_EQ(APInt(8, 1),  APInt(8, 16).rotl(4));
506   EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8));
507
508   EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0));
509   EXPECT_EQ(APInt(8, 8),  APInt(8, 16).rotr(1));
510   EXPECT_EQ(APInt(8, 4),  APInt(8, 16).rotr(2));
511   EXPECT_EQ(APInt(8, 1),  APInt(8, 16).rotr(4));
512   EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8));
513
514   EXPECT_EQ(APInt(8, 1),   APInt(8, 1).rotr(0));
515   EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1));
516   EXPECT_EQ(APInt(8, 64),  APInt(8, 1).rotr(2));
517   EXPECT_EQ(APInt(8, 16),  APInt(8, 1).rotr(4));
518   EXPECT_EQ(APInt(8, 1),   APInt(8, 1).rotr(8));
519
520   APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16);
521   APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16);
522   EXPECT_EQ(Rot, Big.rotr(144));
523 }
524
525 TEST(APIntTest, Splat) {
526   APInt ValA(8, 0x01);
527   EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
528   EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
529
530   APInt ValB(3, 5);
531   EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
532   EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
533 }
534
535 TEST(APIntTest, tcDecrement) {
536   // Test single word decrement.
537
538   // No out borrow.
539   {
540     integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1);
541     integerPart carry = APInt::tcDecrement(&singleWord, 1);
542     EXPECT_EQ(carry, integerPart(0));
543     EXPECT_EQ(singleWord, ~integerPart(0) >> 1);
544   }
545
546   // With out borrow.
547   {
548     integerPart singleWord = 0;
549     integerPart carry = APInt::tcDecrement(&singleWord, 1);
550     EXPECT_EQ(carry, integerPart(1));
551     EXPECT_EQ(singleWord, ~integerPart(0));
552   }
553
554   // Test multiword decrement.
555
556   // No across word borrow, no out borrow.
557   {
558     integerPart test[4] = {0x1, 0x1, 0x1, 0x1};
559     integerPart expected[4] = {0x0, 0x1, 0x1, 0x1};
560     APInt::tcDecrement(test, 4);
561     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
562   }
563
564   // 1 across word borrow, no out borrow.
565   {
566     integerPart test[4] = {0x0, 0xF, 0x1, 0x1};
567     integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1};
568     integerPart carry = APInt::tcDecrement(test, 4);
569     EXPECT_EQ(carry, integerPart(0));
570     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
571   }
572
573   // 2 across word borrow, no out borrow.
574   {
575     integerPart test[4] = {0x0, 0x0, 0xC, 0x1};
576     integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1};
577     integerPart carry = APInt::tcDecrement(test, 4);
578     EXPECT_EQ(carry, integerPart(0));
579     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
580   }
581
582   // 3 across word borrow, no out borrow.
583   {
584     integerPart test[4] = {0x0, 0x0, 0x0, 0x1};
585     integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0};
586     integerPart carry = APInt::tcDecrement(test, 4);
587     EXPECT_EQ(carry, integerPart(0));
588     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
589   }
590
591   // 3 across word borrow, with out borrow.
592   {
593     integerPart test[4] = {0x0, 0x0, 0x0, 0x0};
594     integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)};
595     integerPart carry = APInt::tcDecrement(test, 4);
596     EXPECT_EQ(carry, integerPart(1));
597     EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0);
598   }
599 }
600
601 TEST(APIntTest, extractBit) {
602   // Single word check.
603   uint64_t E1 = 0x2CA7F46BF6569915ULL;
604   APInt A1(64, E1);
605   for (unsigned i = 0, e = 64; i < e; ++i) {    
606     EXPECT_EQ(bool(E1 & (1ULL << i)),
607               A1.extractBit(i));
608   }
609
610   // Multiword check.
611   integerPart E2[4] = {
612     0xeb6eb136591cba21ULL,
613     0x7b9358bd6a33f10aULL,
614     0x7e7ffa5eadd8846ULL,
615     0x305f341ca00b613dULL
616   };
617   APInt A2(integerPartWidth*4, ArrayRef<integerPart>(E2, 4));
618   for (unsigned i = 0; i < 4; ++i) {
619     for (unsigned j = 0; j < integerPartWidth; ++j) {
620       EXPECT_EQ(bool(E2[i] & (1ULL << j)),
621                 A2.extractBit(i*integerPartWidth + j));
622     }
623   }
624 }
625
626 }