Bill Wendling | fb3e5ca | 2009-02-09 12:31:40 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===// |
Nick Lewycky | 1999ff1 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 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 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 10 | #include <ostream> |
Nick Lewycky | 1999ff1 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | #include "llvm/ADT/APInt.h" |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
Nick Lewycky | 1999ff1 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 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); |
Chandler Carruth | 48b17fa | 2010-07-13 17:28:05 +0000 | [diff] [blame] | 23 | EXPECT_TRUE(Shl[0]); |
| 24 | EXPECT_FALSE(Shl[1]); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 27 | TEST(APIntTest, i128_NegativeCount) { |
Misha Brukman | e3bc46e | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 28 | APInt Minus3(128, static_cast<uint64_t>(-3), true); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 29 | EXPECT_EQ(126u, Minus3.countLeadingOnes()); |
| 30 | EXPECT_EQ(-3, Minus3.getSExtValue()); |
| 31 | |
Misha Brukman | e3bc46e | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 32 | APInt Minus1(128, static_cast<uint64_t>(-1), true); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | bc9c36b | 2010-09-14 00:51:58 +0000 | [diff] [blame] | 42 | // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it. |
| 43 | #if defined(__llvm__) || !defined(__FreeBSD__) |
| 44 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 45 | TEST(APIntTest, i33_Count) { |
Misha Brukman | e3bc46e | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 46 | APInt i33minus2(33, static_cast<uint64_t>(-2), true); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | bc9c36b | 2010-09-14 00:51:58 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 58 | TEST(APIntTest, i65_Count) { |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 59 | APInt i65minus(65, 0, true); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 60 | i65minus.setBit(64); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 61 | EXPECT_EQ(0u, i65minus.countLeadingZeros()); |
| 62 | EXPECT_EQ(1u, i65minus.countLeadingOnes()); |
| 63 | EXPECT_EQ(65u, i65minus.getActiveBits()); |
| 64 | EXPECT_EQ(64u, i65minus.countTrailingZeros()); |
| 65 | EXPECT_EQ(1u, i65minus.countPopulation()); |
| 66 | } |
| 67 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 68 | TEST(APIntTest, i128_PositiveCount) { |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 69 | APInt u128max = APInt::getAllOnesValue(128); |
| 70 | EXPECT_EQ(128u, u128max.countLeadingOnes()); |
| 71 | EXPECT_EQ(0u, u128max.countLeadingZeros()); |
| 72 | EXPECT_EQ(128u, u128max.getActiveBits()); |
| 73 | EXPECT_EQ(0u, u128max.countTrailingZeros()); |
| 74 | EXPECT_EQ(128u, u128max.countTrailingOnes()); |
| 75 | EXPECT_EQ(128u, u128max.countPopulation()); |
| 76 | |
Misha Brukman | e3bc46e | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 77 | APInt u64max(128, static_cast<uint64_t>(-1), false); |
Torok Edwin | 2d0f1c5 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 78 | EXPECT_EQ(64u, u64max.countLeadingZeros()); |
| 79 | EXPECT_EQ(0u, u64max.countLeadingOnes()); |
| 80 | EXPECT_EQ(64u, u64max.getActiveBits()); |
| 81 | EXPECT_EQ(0u, u64max.countTrailingZeros()); |
| 82 | EXPECT_EQ(64u, u64max.countTrailingOnes()); |
| 83 | EXPECT_EQ(64u, u64max.countPopulation()); |
| 84 | EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue()); |
| 85 | |
| 86 | APInt zero(128, 0, true); |
| 87 | EXPECT_EQ(128u, zero.countLeadingZeros()); |
| 88 | EXPECT_EQ(0u, zero.countLeadingOnes()); |
| 89 | EXPECT_EQ(0u, zero.getActiveBits()); |
| 90 | EXPECT_EQ(128u, zero.countTrailingZeros()); |
| 91 | EXPECT_EQ(0u, zero.countTrailingOnes()); |
| 92 | EXPECT_EQ(0u, zero.countPopulation()); |
| 93 | EXPECT_EQ(0u, zero.getSExtValue()); |
| 94 | EXPECT_EQ(0u, zero.getZExtValue()); |
| 95 | |
| 96 | APInt one(128, 1, true); |
| 97 | EXPECT_EQ(127u, one.countLeadingZeros()); |
| 98 | EXPECT_EQ(0u, one.countLeadingOnes()); |
| 99 | EXPECT_EQ(1u, one.getActiveBits()); |
| 100 | EXPECT_EQ(0u, one.countTrailingZeros()); |
| 101 | EXPECT_EQ(1u, one.countTrailingOnes()); |
| 102 | EXPECT_EQ(1u, one.countPopulation()); |
| 103 | EXPECT_EQ(1, one.getSExtValue()); |
| 104 | EXPECT_EQ(1u, one.getZExtValue()); |
Nick Lewycky | 1999ff1 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 107 | TEST(APIntTest, i1) { |
Misha Brukman | e3bc46e | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 108 | const APInt neg_two(1, static_cast<uint64_t>(-2), true); |
| 109 | const APInt neg_one(1, static_cast<uint64_t>(-1), true); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 110 | const APInt zero(1, 0); |
| 111 | const APInt one(1, 1); |
| 112 | const APInt two(1, 2); |
| 113 | |
| 114 | EXPECT_EQ(0, neg_two.getSExtValue()); |
| 115 | EXPECT_EQ(-1, neg_one.getSExtValue()); |
| 116 | EXPECT_EQ(1u, neg_one.getZExtValue()); |
| 117 | EXPECT_EQ(0u, zero.getZExtValue()); |
| 118 | EXPECT_EQ(-1, one.getSExtValue()); |
| 119 | EXPECT_EQ(1u, one.getZExtValue()); |
| 120 | EXPECT_EQ(0u, two.getZExtValue()); |
| 121 | EXPECT_EQ(0, two.getSExtValue()); |
| 122 | |
| 123 | // Basic equalities for 1-bit values. |
| 124 | EXPECT_EQ(zero, two); |
| 125 | EXPECT_EQ(zero, neg_two); |
| 126 | EXPECT_EQ(one, neg_one); |
| 127 | EXPECT_EQ(two, neg_two); |
| 128 | |
| 129 | // Additions. |
| 130 | EXPECT_EQ(two, one + one); |
| 131 | EXPECT_EQ(zero, neg_one + one); |
| 132 | EXPECT_EQ(neg_two, neg_one + neg_one); |
| 133 | |
| 134 | // Subtractions. |
| 135 | EXPECT_EQ(neg_two, neg_one - one); |
| 136 | EXPECT_EQ(two, one - neg_one); |
| 137 | EXPECT_EQ(zero, one - one); |
| 138 | |
| 139 | // Shifts. |
| 140 | EXPECT_EQ(zero, one << one); |
| 141 | EXPECT_EQ(one, one << zero); |
| 142 | EXPECT_EQ(zero, one.shl(1)); |
| 143 | EXPECT_EQ(one, one.shl(0)); |
| 144 | EXPECT_EQ(zero, one.lshr(1)); |
| 145 | EXPECT_EQ(zero, one.ashr(1)); |
| 146 | |
Eli Friedman | 3a3a424 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 147 | // Rotates. |
| 148 | EXPECT_EQ(one, one.rotl(0)); |
| 149 | EXPECT_EQ(one, one.rotl(1)); |
| 150 | EXPECT_EQ(one, one.rotr(0)); |
| 151 | EXPECT_EQ(one, one.rotr(1)); |
| 152 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 153 | // Multiplies. |
| 154 | EXPECT_EQ(neg_one, neg_one * one); |
| 155 | EXPECT_EQ(neg_one, one * neg_one); |
| 156 | EXPECT_EQ(one, neg_one * neg_one); |
| 157 | EXPECT_EQ(one, one * one); |
| 158 | |
| 159 | // Divides. |
| 160 | EXPECT_EQ(neg_one, one.sdiv(neg_one)); |
| 161 | EXPECT_EQ(neg_one, neg_one.sdiv(one)); |
| 162 | EXPECT_EQ(one, neg_one.sdiv(neg_one)); |
| 163 | EXPECT_EQ(one, one.sdiv(one)); |
| 164 | |
| 165 | EXPECT_EQ(neg_one, one.udiv(neg_one)); |
| 166 | EXPECT_EQ(neg_one, neg_one.udiv(one)); |
| 167 | EXPECT_EQ(one, neg_one.udiv(neg_one)); |
| 168 | EXPECT_EQ(one, one.udiv(one)); |
| 169 | |
| 170 | // Remainders. |
| 171 | EXPECT_EQ(zero, neg_one.srem(one)); |
| 172 | EXPECT_EQ(zero, neg_one.urem(one)); |
| 173 | EXPECT_EQ(zero, one.srem(neg_one)); |
Nuno Lopes | a7a2a36 | 2012-05-22 01:09:48 +0000 | [diff] [blame] | 174 | |
| 175 | // sdivrem |
| 176 | { |
| 177 | APInt q(8, 0); |
| 178 | APInt r(8, 0); |
| 179 | APInt one(8, 1); |
| 180 | APInt two(8, 2); |
| 181 | APInt nine(8, 9); |
| 182 | APInt four(8, 4); |
| 183 | |
| 184 | EXPECT_EQ(nine.srem(two), one); |
| 185 | EXPECT_EQ(nine.srem(-two), one); |
| 186 | EXPECT_EQ((-nine).srem(two), -one); |
| 187 | EXPECT_EQ((-nine).srem(-two), -one); |
| 188 | |
| 189 | APInt::sdivrem(nine, two, q, r); |
| 190 | EXPECT_EQ(four, q); |
| 191 | EXPECT_EQ(one, r); |
| 192 | APInt::sdivrem(-nine, two, q, r); |
| 193 | EXPECT_EQ(-four, q); |
| 194 | EXPECT_EQ(-one, r); |
| 195 | APInt::sdivrem(nine, -two, q, r); |
| 196 | EXPECT_EQ(-four, q); |
| 197 | EXPECT_EQ(one, r); |
| 198 | APInt::sdivrem(-nine, -two, q, r); |
| 199 | EXPECT_EQ(four, q); |
| 200 | EXPECT_EQ(-one, r); |
| 201 | } |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Chris Lattner | 38300e9 | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 204 | TEST(APIntTest, fromString) { |
Erick Tryzelaar | bb97531 | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 205 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); |
| 206 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); |
| 207 | EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); |
| 208 | EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); |
| 209 | EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); |
| 210 | |
| 211 | EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); |
| 212 | EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); |
| 213 | EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); |
| 214 | EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); |
| 215 | EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); |
| 216 | |
Daniel Dunbar | 3933179 | 2009-09-18 17:48:05 +0000 | [diff] [blame] | 217 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); |
| 218 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); |
| 219 | EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); |
| 220 | EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); |
| 221 | EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); |
Erick Tryzelaar | bb97531 | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); |
| 225 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); |
| 226 | EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); |
| 227 | EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); |
| 228 | EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); |
| 229 | EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); |
| 230 | |
| 231 | EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); |
| 232 | EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); |
| 233 | EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); |
| 234 | EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); |
| 235 | EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); |
| 236 | EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); |
| 237 | |
Daniel Dunbar | f74610b | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 238 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); |
| 239 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); |
| 240 | EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); |
| 241 | EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); |
| 242 | EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); |
| 243 | EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); |
Erick Tryzelaar | bb97531 | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); |
| 247 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); |
| 248 | EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); |
| 249 | EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); |
| 250 | EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); |
| 251 | EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); |
| 252 | |
Daniel Dunbar | f74610b | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 253 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); |
| 254 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); |
| 255 | EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); |
| 256 | EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); |
| 257 | EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); |
| 258 | EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); |
Erick Tryzelaar | bb97531 | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); |
| 262 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); |
| 263 | EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); |
| 264 | EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); |
| 265 | EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); |
| 266 | EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); |
| 267 | |
Daniel Dunbar | f74610b | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 268 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); |
| 269 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); |
| 270 | EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); |
| 271 | EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); |
| 272 | EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); |
| 273 | EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); |
Douglas Gregor | dcd9996 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 274 | |
| 275 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); |
| 276 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); |
| 277 | EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36)); |
| 278 | EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36)); |
| 279 | EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36)); |
| 280 | EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36)); |
| 281 | |
| 282 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); |
| 283 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); |
| 284 | EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); |
| 285 | EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36)); |
| 286 | EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36)); |
| 287 | EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36)); |
Chris Lattner | 38300e9 | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Jeffrey Yasskin | 3ba292d | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 290 | TEST(APIntTest, FromArray) { |
| 291 | EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1))); |
| 292 | } |
| 293 | |
Erick Tryzelaar | ae8f78d | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 294 | TEST(APIntTest, StringBitsNeeded2) { |
| 295 | EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2)); |
| 296 | EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2)); |
| 297 | EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2)); |
| 298 | EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2)); |
| 299 | EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2)); |
| 300 | |
| 301 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2)); |
| 302 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2)); |
| 303 | EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2)); |
| 304 | EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2)); |
| 305 | EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2)); |
| 306 | |
| 307 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2)); |
| 308 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2)); |
| 309 | EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2)); |
| 310 | EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2)); |
| 311 | EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2)); |
| 312 | } |
| 313 | |
| 314 | TEST(APIntTest, StringBitsNeeded8) { |
| 315 | EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8)); |
| 316 | EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8)); |
| 317 | EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8)); |
| 318 | EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8)); |
| 319 | EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8)); |
| 320 | |
| 321 | EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8)); |
| 322 | EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8)); |
| 323 | EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8)); |
| 324 | EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8)); |
| 325 | EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8)); |
| 326 | |
| 327 | EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8)); |
| 328 | EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8)); |
| 329 | EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8)); |
| 330 | EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8)); |
| 331 | EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8)); |
| 332 | } |
| 333 | |
| 334 | TEST(APIntTest, StringBitsNeeded10) { |
| 335 | EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10)); |
| 336 | EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10)); |
| 337 | EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10)); |
| 338 | EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10)); |
| 339 | EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10)); |
| 340 | EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10)); |
| 341 | |
| 342 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10)); |
| 343 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10)); |
| 344 | EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10)); |
| 345 | EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10)); |
| 346 | EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10)); |
| 347 | |
| 348 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10)); |
| 349 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10)); |
| 350 | EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10)); |
| 351 | EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10)); |
| 352 | EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10)); |
| 353 | } |
| 354 | |
| 355 | TEST(APIntTest, StringBitsNeeded16) { |
| 356 | EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16)); |
| 357 | EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16)); |
| 358 | EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16)); |
| 359 | EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16)); |
| 360 | EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16)); |
| 361 | |
| 362 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16)); |
| 363 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16)); |
| 364 | EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16)); |
| 365 | EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16)); |
| 366 | EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16)); |
| 367 | |
| 368 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16)); |
| 369 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16)); |
| 370 | EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16)); |
| 371 | EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16)); |
| 372 | EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16)); |
| 373 | } |
| 374 | |
Dylan Noblesmith | 5f36bb1 | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 375 | TEST(APIntTest, toString) { |
| 376 | SmallString<16> S; |
| 377 | bool isSigned; |
| 378 | |
| 379 | APInt(8, 0).toString(S, 2, true, true); |
| 380 | EXPECT_EQ(S.str().str(), "0b0"); |
| 381 | S.clear(); |
| 382 | APInt(8, 0).toString(S, 8, true, true); |
| 383 | EXPECT_EQ(S.str().str(), "00"); |
| 384 | S.clear(); |
| 385 | APInt(8, 0).toString(S, 10, true, true); |
| 386 | EXPECT_EQ(S.str().str(), "0"); |
| 387 | S.clear(); |
| 388 | APInt(8, 0).toString(S, 16, true, true); |
| 389 | EXPECT_EQ(S.str().str(), "0x0"); |
| 390 | S.clear(); |
Dylan Noblesmith | efb0d1e | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 391 | APInt(8, 0).toString(S, 36, true, false); |
Douglas Gregor | dcd9996 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 392 | EXPECT_EQ(S.str().str(), "0"); |
| 393 | S.clear(); |
Dylan Noblesmith | 5f36bb1 | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 394 | |
| 395 | isSigned = false; |
| 396 | APInt(8, 255, isSigned).toString(S, 2, isSigned, true); |
| 397 | EXPECT_EQ(S.str().str(), "0b11111111"); |
| 398 | S.clear(); |
| 399 | APInt(8, 255, isSigned).toString(S, 8, isSigned, true); |
| 400 | EXPECT_EQ(S.str().str(), "0377"); |
| 401 | S.clear(); |
| 402 | APInt(8, 255, isSigned).toString(S, 10, isSigned, true); |
| 403 | EXPECT_EQ(S.str().str(), "255"); |
| 404 | S.clear(); |
| 405 | APInt(8, 255, isSigned).toString(S, 16, isSigned, true); |
| 406 | EXPECT_EQ(S.str().str(), "0xFF"); |
| 407 | S.clear(); |
Dylan Noblesmith | efb0d1e | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 408 | APInt(8, 255, isSigned).toString(S, 36, isSigned, false); |
Douglas Gregor | dcd9996 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 409 | EXPECT_EQ(S.str().str(), "73"); |
| 410 | S.clear(); |
Dylan Noblesmith | 5f36bb1 | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 411 | |
| 412 | isSigned = true; |
| 413 | APInt(8, 255, isSigned).toString(S, 2, isSigned, true); |
| 414 | EXPECT_EQ(S.str().str(), "-0b1"); |
| 415 | S.clear(); |
| 416 | APInt(8, 255, isSigned).toString(S, 8, isSigned, true); |
| 417 | EXPECT_EQ(S.str().str(), "-01"); |
| 418 | S.clear(); |
| 419 | APInt(8, 255, isSigned).toString(S, 10, isSigned, true); |
| 420 | EXPECT_EQ(S.str().str(), "-1"); |
| 421 | S.clear(); |
| 422 | APInt(8, 255, isSigned).toString(S, 16, isSigned, true); |
| 423 | EXPECT_EQ(S.str().str(), "-0x1"); |
| 424 | S.clear(); |
Dylan Noblesmith | efb0d1e | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 425 | APInt(8, 255, isSigned).toString(S, 36, isSigned, false); |
Douglas Gregor | dcd9996 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 426 | EXPECT_EQ(S.str().str(), "-1"); |
| 427 | S.clear(); |
Dylan Noblesmith | 5f36bb1 | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Dan Gohman | cbc7cc6 | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 430 | TEST(APIntTest, Log2) { |
Duncan Sands | 937708c | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 431 | EXPECT_EQ(APInt(15, 7).logBase2(), 2U); |
| 432 | EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U); |
Dan Gohman | cbc7cc6 | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 433 | EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1); |
Duncan Sands | 937708c | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 434 | EXPECT_EQ(APInt(15, 8).logBase2(), 3U); |
| 435 | EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U); |
Dan Gohman | cbc7cc6 | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 436 | EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3); |
Duncan Sands | 937708c | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 437 | EXPECT_EQ(APInt(15, 9).logBase2(), 3U); |
| 438 | EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U); |
Dan Gohman | cbc7cc6 | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 439 | EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); |
| 440 | } |
Erick Tryzelaar | ae8f78d | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 441 | |
Cameron Zwarich | 8d7285d | 2011-02-21 00:22:02 +0000 | [diff] [blame] | 442 | TEST(APIntTest, magic) { |
| 443 | EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16)); |
| 444 | EXPECT_EQ(APInt(32, 3).magic().s, 0U); |
| 445 | EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16)); |
| 446 | EXPECT_EQ(APInt(32, 5).magic().s, 1U); |
| 447 | EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16)); |
| 448 | EXPECT_EQ(APInt(32, 7).magic().s, 2U); |
| 449 | } |
| 450 | |
| 451 | TEST(APIntTest, magicu) { |
| 452 | EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16)); |
| 453 | EXPECT_EQ(APInt(32, 3).magicu().s, 1U); |
| 454 | EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16)); |
| 455 | EXPECT_EQ(APInt(32, 5).magicu().s, 2U); |
| 456 | EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16)); |
| 457 | EXPECT_EQ(APInt(32, 7).magicu().s, 3U); |
Benjamin Kramer | d9103df | 2011-03-17 20:39:06 +0000 | [diff] [blame] | 458 | EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16)); |
| 459 | EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U); |
Cameron Zwarich | 8d7285d | 2011-02-21 00:22:02 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Erick Tryzelaar | 2ad40a3 | 2009-08-17 00:55:33 +0000 | [diff] [blame] | 462 | #ifdef GTEST_HAS_DEATH_TEST |
Jeffrey Yasskin | b5f59f5 | 2010-03-17 01:18:45 +0000 | [diff] [blame] | 463 | #ifndef NDEBUG |
Erick Tryzelaar | 1b9104f | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 464 | TEST(APIntTest, StringDeath) { |
Erick Tryzelaar | bb97531 | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 465 | EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small"); |
| 466 | EXPECT_DEATH(APInt(32, "", 0), "Invalid string length"); |
Douglas Gregor | dcd9996 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 467 | EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!"); |
Erick Tryzelaar | 1b9104f | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 468 | EXPECT_DEATH(APInt(32, "", 10), "Invalid string length"); |
Bill Wendling | 5504225 | 2009-08-21 06:35:41 +0000 | [diff] [blame] | 469 | EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value."); |
Erick Tryzelaar | 1b9104f | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 470 | EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width"); |
| 471 | EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length"); |
| 472 | EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string"); |
| 473 | EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string"); |
| 474 | } |
Erick Tryzelaar | 2ad40a3 | 2009-08-17 00:55:33 +0000 | [diff] [blame] | 475 | #endif |
Jeffrey Yasskin | b5f59f5 | 2010-03-17 01:18:45 +0000 | [diff] [blame] | 476 | #endif |
Erick Tryzelaar | 1b9104f | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 477 | |
Eli Friedman | 9eb6b4d | 2011-10-07 23:40:49 +0000 | [diff] [blame] | 478 | TEST(APIntTest, mul_clear) { |
| 479 | APInt ValA(65, -1ULL); |
| 480 | APInt ValB(65, 4); |
| 481 | APInt ValC(65, 0); |
| 482 | ValC = ValA * ValB; |
| 483 | ValA *= ValB; |
| 484 | EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false)); |
| 485 | } |
| 486 | |
Eli Friedman | 3a3a424 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 487 | TEST(APIntTest, Rotate) { |
| 488 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0)); |
| 489 | EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1)); |
| 490 | EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2)); |
| 491 | EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4)); |
| 492 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8)); |
| 493 | |
| 494 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0)); |
| 495 | EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1)); |
| 496 | EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2)); |
| 497 | EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4)); |
| 498 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8)); |
| 499 | |
| 500 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0)); |
| 501 | EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1)); |
| 502 | EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2)); |
| 503 | EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4)); |
| 504 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8)); |
| 505 | |
| 506 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0)); |
| 507 | EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1)); |
| 508 | EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2)); |
| 509 | EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4)); |
| 510 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8)); |
Benjamin Kramer | 0ae29a6 | 2012-02-07 16:27:39 +0000 | [diff] [blame] | 511 | |
| 512 | APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16); |
| 513 | APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16); |
| 514 | EXPECT_EQ(Rot, Big.rotr(144)); |
Eli Friedman | 3a3a424 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Nick Lewycky | 1999ff1 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 517 | } |