Bill Wendling | b9ad492 | 2009-02-09 12:31:40 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===// |
Nick Lewycky | ee22611 | 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 | |
Nick Lewycky | ee22611 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/APInt.h" |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
Pawel Bylica | 0dda164 | 2015-06-25 10:47:08 +0000 | [diff] [blame] | 13 | #include <array> |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 14 | #include <ostream> |
Nick Lewycky | ee22611 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
Richard Smith | 55f5e65 | 2015-09-04 04:08:36 +0000 | [diff] [blame^] | 20 | TEST(APIntTest, ValueInit) { |
| 21 | APInt Zero = APInt(); |
| 22 | EXPECT_TRUE(!Zero); |
| 23 | EXPECT_TRUE(!Zero.zext(64)); |
| 24 | EXPECT_TRUE(!Zero.sext(64)); |
| 25 | } |
| 26 | |
Nick Lewycky | ee22611 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 27 | // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0 |
| 28 | TEST(APIntTest, ShiftLeftByZero) { |
| 29 | APInt One = APInt::getNullValue(65) + 1; |
| 30 | APInt Shl = One.shl(0); |
Chandler Carruth | bc3e8a7 | 2010-07-13 17:28:05 +0000 | [diff] [blame] | 31 | EXPECT_TRUE(Shl[0]); |
| 32 | EXPECT_FALSE(Shl[1]); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 35 | TEST(APIntTest, i128_NegativeCount) { |
Misha Brukman | 680336d | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 36 | APInt Minus3(128, static_cast<uint64_t>(-3), true); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 37 | EXPECT_EQ(126u, Minus3.countLeadingOnes()); |
| 38 | EXPECT_EQ(-3, Minus3.getSExtValue()); |
| 39 | |
Misha Brukman | 680336d | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 40 | APInt Minus1(128, static_cast<uint64_t>(-1), true); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 41 | EXPECT_EQ(0u, Minus1.countLeadingZeros()); |
| 42 | EXPECT_EQ(128u, Minus1.countLeadingOnes()); |
| 43 | EXPECT_EQ(128u, Minus1.getActiveBits()); |
| 44 | EXPECT_EQ(0u, Minus1.countTrailingZeros()); |
| 45 | EXPECT_EQ(128u, Minus1.countTrailingOnes()); |
| 46 | EXPECT_EQ(128u, Minus1.countPopulation()); |
| 47 | EXPECT_EQ(-1, Minus1.getSExtValue()); |
| 48 | } |
| 49 | |
Jakob Stoklund Olesen | e1e70b9 | 2010-09-14 00:51:58 +0000 | [diff] [blame] | 50 | // XFAIL this test on FreeBSD where the system gcc-4.2.1 seems to miscompile it. |
| 51 | #if defined(__llvm__) || !defined(__FreeBSD__) |
| 52 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 53 | TEST(APIntTest, i33_Count) { |
Misha Brukman | 680336d | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 54 | APInt i33minus2(33, static_cast<uint64_t>(-2), true); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 55 | EXPECT_EQ(0u, i33minus2.countLeadingZeros()); |
| 56 | EXPECT_EQ(32u, i33minus2.countLeadingOnes()); |
| 57 | EXPECT_EQ(33u, i33minus2.getActiveBits()); |
| 58 | EXPECT_EQ(1u, i33minus2.countTrailingZeros()); |
| 59 | EXPECT_EQ(32u, i33minus2.countPopulation()); |
| 60 | EXPECT_EQ(-2, i33minus2.getSExtValue()); |
| 61 | EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue()); |
| 62 | } |
| 63 | |
Jakob Stoklund Olesen | e1e70b9 | 2010-09-14 00:51:58 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 66 | TEST(APIntTest, i65_Count) { |
Meador Inge | 32dc724 | 2013-02-07 18:36:50 +0000 | [diff] [blame] | 67 | APInt i65(65, 0, true); |
| 68 | EXPECT_EQ(65u, i65.countLeadingZeros()); |
| 69 | EXPECT_EQ(0u, i65.countLeadingOnes()); |
| 70 | EXPECT_EQ(0u, i65.getActiveBits()); |
| 71 | EXPECT_EQ(1u, i65.getActiveWords()); |
| 72 | EXPECT_EQ(65u, i65.countTrailingZeros()); |
| 73 | EXPECT_EQ(0u, i65.countPopulation()); |
| 74 | |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 75 | APInt i65minus(65, 0, true); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 76 | i65minus.setBit(64); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 77 | EXPECT_EQ(0u, i65minus.countLeadingZeros()); |
| 78 | EXPECT_EQ(1u, i65minus.countLeadingOnes()); |
| 79 | EXPECT_EQ(65u, i65minus.getActiveBits()); |
| 80 | EXPECT_EQ(64u, i65minus.countTrailingZeros()); |
| 81 | EXPECT_EQ(1u, i65minus.countPopulation()); |
| 82 | } |
| 83 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 84 | TEST(APIntTest, i128_PositiveCount) { |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 85 | APInt u128max = APInt::getAllOnesValue(128); |
| 86 | EXPECT_EQ(128u, u128max.countLeadingOnes()); |
| 87 | EXPECT_EQ(0u, u128max.countLeadingZeros()); |
| 88 | EXPECT_EQ(128u, u128max.getActiveBits()); |
| 89 | EXPECT_EQ(0u, u128max.countTrailingZeros()); |
| 90 | EXPECT_EQ(128u, u128max.countTrailingOnes()); |
| 91 | EXPECT_EQ(128u, u128max.countPopulation()); |
| 92 | |
Misha Brukman | 680336d | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 93 | APInt u64max(128, static_cast<uint64_t>(-1), false); |
Torok Edwin | ec39eb8 | 2009-01-27 18:06:03 +0000 | [diff] [blame] | 94 | EXPECT_EQ(64u, u64max.countLeadingZeros()); |
| 95 | EXPECT_EQ(0u, u64max.countLeadingOnes()); |
| 96 | EXPECT_EQ(64u, u64max.getActiveBits()); |
| 97 | EXPECT_EQ(0u, u64max.countTrailingZeros()); |
| 98 | EXPECT_EQ(64u, u64max.countTrailingOnes()); |
| 99 | EXPECT_EQ(64u, u64max.countPopulation()); |
| 100 | EXPECT_EQ((uint64_t)~0ull, u64max.getZExtValue()); |
| 101 | |
| 102 | APInt zero(128, 0, true); |
| 103 | EXPECT_EQ(128u, zero.countLeadingZeros()); |
| 104 | EXPECT_EQ(0u, zero.countLeadingOnes()); |
| 105 | EXPECT_EQ(0u, zero.getActiveBits()); |
| 106 | EXPECT_EQ(128u, zero.countTrailingZeros()); |
| 107 | EXPECT_EQ(0u, zero.countTrailingOnes()); |
| 108 | EXPECT_EQ(0u, zero.countPopulation()); |
| 109 | EXPECT_EQ(0u, zero.getSExtValue()); |
| 110 | EXPECT_EQ(0u, zero.getZExtValue()); |
| 111 | |
| 112 | APInt one(128, 1, true); |
| 113 | EXPECT_EQ(127u, one.countLeadingZeros()); |
| 114 | EXPECT_EQ(0u, one.countLeadingOnes()); |
| 115 | EXPECT_EQ(1u, one.getActiveBits()); |
| 116 | EXPECT_EQ(0u, one.countTrailingZeros()); |
| 117 | EXPECT_EQ(1u, one.countTrailingOnes()); |
| 118 | EXPECT_EQ(1u, one.countPopulation()); |
| 119 | EXPECT_EQ(1, one.getSExtValue()); |
| 120 | EXPECT_EQ(1u, one.getZExtValue()); |
Nick Lewycky | ee22611 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 123 | TEST(APIntTest, i1) { |
Misha Brukman | 680336d | 2009-04-08 16:17:23 +0000 | [diff] [blame] | 124 | const APInt neg_two(1, static_cast<uint64_t>(-2), true); |
| 125 | const APInt neg_one(1, static_cast<uint64_t>(-1), true); |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 126 | const APInt zero(1, 0); |
| 127 | const APInt one(1, 1); |
| 128 | const APInt two(1, 2); |
| 129 | |
| 130 | EXPECT_EQ(0, neg_two.getSExtValue()); |
| 131 | EXPECT_EQ(-1, neg_one.getSExtValue()); |
| 132 | EXPECT_EQ(1u, neg_one.getZExtValue()); |
| 133 | EXPECT_EQ(0u, zero.getZExtValue()); |
| 134 | EXPECT_EQ(-1, one.getSExtValue()); |
| 135 | EXPECT_EQ(1u, one.getZExtValue()); |
| 136 | EXPECT_EQ(0u, two.getZExtValue()); |
| 137 | EXPECT_EQ(0, two.getSExtValue()); |
| 138 | |
| 139 | // Basic equalities for 1-bit values. |
| 140 | EXPECT_EQ(zero, two); |
| 141 | EXPECT_EQ(zero, neg_two); |
| 142 | EXPECT_EQ(one, neg_one); |
| 143 | EXPECT_EQ(two, neg_two); |
| 144 | |
Benjamin Kramer | 886461e | 2015-06-04 18:19:13 +0000 | [diff] [blame] | 145 | // Min/max signed values. |
| 146 | EXPECT_TRUE(zero.isMaxSignedValue()); |
| 147 | EXPECT_FALSE(one.isMaxSignedValue()); |
| 148 | EXPECT_FALSE(zero.isMinSignedValue()); |
| 149 | EXPECT_TRUE(one.isMinSignedValue()); |
| 150 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 151 | // Additions. |
| 152 | EXPECT_EQ(two, one + one); |
| 153 | EXPECT_EQ(zero, neg_one + one); |
| 154 | EXPECT_EQ(neg_two, neg_one + neg_one); |
| 155 | |
| 156 | // Subtractions. |
| 157 | EXPECT_EQ(neg_two, neg_one - one); |
| 158 | EXPECT_EQ(two, one - neg_one); |
| 159 | EXPECT_EQ(zero, one - one); |
| 160 | |
| 161 | // Shifts. |
| 162 | EXPECT_EQ(zero, one << one); |
| 163 | EXPECT_EQ(one, one << zero); |
| 164 | EXPECT_EQ(zero, one.shl(1)); |
| 165 | EXPECT_EQ(one, one.shl(0)); |
| 166 | EXPECT_EQ(zero, one.lshr(1)); |
| 167 | EXPECT_EQ(zero, one.ashr(1)); |
| 168 | |
Eli Friedman | f70c862 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 169 | // Rotates. |
| 170 | EXPECT_EQ(one, one.rotl(0)); |
| 171 | EXPECT_EQ(one, one.rotl(1)); |
| 172 | EXPECT_EQ(one, one.rotr(0)); |
| 173 | EXPECT_EQ(one, one.rotr(1)); |
| 174 | |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 175 | // Multiplies. |
| 176 | EXPECT_EQ(neg_one, neg_one * one); |
| 177 | EXPECT_EQ(neg_one, one * neg_one); |
| 178 | EXPECT_EQ(one, neg_one * neg_one); |
| 179 | EXPECT_EQ(one, one * one); |
| 180 | |
| 181 | // Divides. |
| 182 | EXPECT_EQ(neg_one, one.sdiv(neg_one)); |
| 183 | EXPECT_EQ(neg_one, neg_one.sdiv(one)); |
| 184 | EXPECT_EQ(one, neg_one.sdiv(neg_one)); |
| 185 | EXPECT_EQ(one, one.sdiv(one)); |
| 186 | |
| 187 | EXPECT_EQ(neg_one, one.udiv(neg_one)); |
| 188 | EXPECT_EQ(neg_one, neg_one.udiv(one)); |
| 189 | EXPECT_EQ(one, neg_one.udiv(neg_one)); |
| 190 | EXPECT_EQ(one, one.udiv(one)); |
| 191 | |
| 192 | // Remainders. |
| 193 | EXPECT_EQ(zero, neg_one.srem(one)); |
| 194 | EXPECT_EQ(zero, neg_one.urem(one)); |
| 195 | EXPECT_EQ(zero, one.srem(neg_one)); |
Nuno Lopes | 61b7fa2 | 2012-05-22 01:09:48 +0000 | [diff] [blame] | 196 | |
| 197 | // sdivrem |
| 198 | { |
| 199 | APInt q(8, 0); |
| 200 | APInt r(8, 0); |
| 201 | APInt one(8, 1); |
| 202 | APInt two(8, 2); |
| 203 | APInt nine(8, 9); |
| 204 | APInt four(8, 4); |
| 205 | |
| 206 | EXPECT_EQ(nine.srem(two), one); |
| 207 | EXPECT_EQ(nine.srem(-two), one); |
| 208 | EXPECT_EQ((-nine).srem(two), -one); |
| 209 | EXPECT_EQ((-nine).srem(-two), -one); |
| 210 | |
| 211 | APInt::sdivrem(nine, two, q, r); |
| 212 | EXPECT_EQ(four, q); |
| 213 | EXPECT_EQ(one, r); |
| 214 | APInt::sdivrem(-nine, two, q, r); |
| 215 | EXPECT_EQ(-four, q); |
| 216 | EXPECT_EQ(-one, r); |
| 217 | APInt::sdivrem(nine, -two, q, r); |
| 218 | EXPECT_EQ(-four, q); |
| 219 | EXPECT_EQ(one, r); |
| 220 | APInt::sdivrem(-nine, -two, q, r); |
| 221 | EXPECT_EQ(four, q); |
| 222 | EXPECT_EQ(-one, r); |
| 223 | } |
Misha Brukman | d1d2c50 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Pawel Bylica | 8bebed9 | 2015-06-25 10:23:52 +0000 | [diff] [blame] | 226 | TEST(APIntTest, compare) { |
| 227 | std::array<APInt, 5> testVals{{ |
| 228 | APInt{16, 2}, |
| 229 | APInt{16, 1}, |
| 230 | APInt{16, 0}, |
| 231 | APInt{16, (uint64_t)-1, true}, |
| 232 | APInt{16, (uint64_t)-2, true}, |
| 233 | }}; |
| 234 | |
| 235 | for (auto &arg1 : testVals) |
| 236 | for (auto &arg2 : testVals) { |
| 237 | auto uv1 = arg1.getZExtValue(); |
| 238 | auto uv2 = arg2.getZExtValue(); |
| 239 | auto sv1 = arg1.getSExtValue(); |
| 240 | auto sv2 = arg2.getSExtValue(); |
| 241 | |
| 242 | EXPECT_EQ(uv1 < uv2, arg1.ult(arg2)); |
| 243 | EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2)); |
| 244 | EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2)); |
| 245 | EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2)); |
| 246 | |
| 247 | EXPECT_EQ(sv1 < sv2, arg1.slt(arg2)); |
| 248 | EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2)); |
| 249 | EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2)); |
| 250 | EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2)); |
| 251 | |
| 252 | EXPECT_EQ(uv1 < uv2, arg1.ult(uv2)); |
| 253 | EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2)); |
| 254 | EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2)); |
| 255 | EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2)); |
| 256 | |
| 257 | EXPECT_EQ(sv1 < sv2, arg1.slt(sv2)); |
| 258 | EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2)); |
| 259 | EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2)); |
| 260 | EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2)); |
| 261 | } |
| 262 | } |
| 263 | |
Pawel Bylica | ea46a66 | 2015-07-01 22:56:43 +0000 | [diff] [blame] | 264 | TEST(APIntTest, compareWithRawIntegers) { |
| 265 | EXPECT_TRUE(!APInt(8, 1).uge(256)); |
| 266 | EXPECT_TRUE(!APInt(8, 1).ugt(256)); |
| 267 | EXPECT_TRUE( APInt(8, 1).ule(256)); |
| 268 | EXPECT_TRUE( APInt(8, 1).ult(256)); |
| 269 | EXPECT_TRUE(!APInt(8, 1).sge(256)); |
| 270 | EXPECT_TRUE(!APInt(8, 1).sgt(256)); |
| 271 | EXPECT_TRUE( APInt(8, 1).sle(256)); |
| 272 | EXPECT_TRUE( APInt(8, 1).slt(256)); |
| 273 | EXPECT_TRUE(!(APInt(8, 0) == 256)); |
| 274 | EXPECT_TRUE( APInt(8, 0) != 256); |
| 275 | EXPECT_TRUE(!(APInt(8, 1) == 256)); |
| 276 | EXPECT_TRUE( APInt(8, 1) != 256); |
| 277 | |
| 278 | auto uint64max = UINT64_MAX; |
| 279 | auto int64max = INT64_MAX; |
| 280 | auto int64min = INT64_MIN; |
| 281 | |
| 282 | auto u64 = APInt{128, uint64max}; |
| 283 | auto s64 = APInt{128, static_cast<uint64_t>(int64max), true}; |
| 284 | auto big = u64 + 1; |
| 285 | |
| 286 | EXPECT_TRUE( u64.uge(uint64max)); |
| 287 | EXPECT_TRUE(!u64.ugt(uint64max)); |
| 288 | EXPECT_TRUE( u64.ule(uint64max)); |
| 289 | EXPECT_TRUE(!u64.ult(uint64max)); |
| 290 | EXPECT_TRUE( u64.sge(int64max)); |
| 291 | EXPECT_TRUE( u64.sgt(int64max)); |
| 292 | EXPECT_TRUE(!u64.sle(int64max)); |
| 293 | EXPECT_TRUE(!u64.slt(int64max)); |
| 294 | EXPECT_TRUE( u64.sge(int64min)); |
| 295 | EXPECT_TRUE( u64.sgt(int64min)); |
| 296 | EXPECT_TRUE(!u64.sle(int64min)); |
| 297 | EXPECT_TRUE(!u64.slt(int64min)); |
| 298 | |
| 299 | EXPECT_TRUE(u64 == uint64max); |
| 300 | EXPECT_TRUE(u64 != int64max); |
| 301 | EXPECT_TRUE(u64 != int64min); |
| 302 | |
| 303 | EXPECT_TRUE(!s64.uge(uint64max)); |
| 304 | EXPECT_TRUE(!s64.ugt(uint64max)); |
| 305 | EXPECT_TRUE( s64.ule(uint64max)); |
| 306 | EXPECT_TRUE( s64.ult(uint64max)); |
| 307 | EXPECT_TRUE( s64.sge(int64max)); |
| 308 | EXPECT_TRUE(!s64.sgt(int64max)); |
| 309 | EXPECT_TRUE( s64.sle(int64max)); |
| 310 | EXPECT_TRUE(!s64.slt(int64max)); |
| 311 | EXPECT_TRUE( s64.sge(int64min)); |
| 312 | EXPECT_TRUE( s64.sgt(int64min)); |
| 313 | EXPECT_TRUE(!s64.sle(int64min)); |
| 314 | EXPECT_TRUE(!s64.slt(int64min)); |
| 315 | |
| 316 | EXPECT_TRUE(s64 != uint64max); |
| 317 | EXPECT_TRUE(s64 == int64max); |
| 318 | EXPECT_TRUE(s64 != int64min); |
| 319 | |
| 320 | EXPECT_TRUE( big.uge(uint64max)); |
| 321 | EXPECT_TRUE( big.ugt(uint64max)); |
| 322 | EXPECT_TRUE(!big.ule(uint64max)); |
| 323 | EXPECT_TRUE(!big.ult(uint64max)); |
| 324 | EXPECT_TRUE( big.sge(int64max)); |
| 325 | EXPECT_TRUE( big.sgt(int64max)); |
| 326 | EXPECT_TRUE(!big.sle(int64max)); |
| 327 | EXPECT_TRUE(!big.slt(int64max)); |
| 328 | EXPECT_TRUE( big.sge(int64min)); |
| 329 | EXPECT_TRUE( big.sgt(int64min)); |
| 330 | EXPECT_TRUE(!big.sle(int64min)); |
| 331 | EXPECT_TRUE(!big.slt(int64min)); |
| 332 | |
| 333 | EXPECT_TRUE(big != uint64max); |
| 334 | EXPECT_TRUE(big != int64max); |
| 335 | EXPECT_TRUE(big != int64min); |
| 336 | } |
| 337 | |
| 338 | TEST(APIntTest, compareWithInt64Min) { |
| 339 | int64_t edge = INT64_MIN; |
| 340 | int64_t edgeP1 = edge + 1; |
| 341 | int64_t edgeM1 = INT64_MAX; |
| 342 | auto a = APInt{64, static_cast<uint64_t>(edge), true}; |
| 343 | |
| 344 | EXPECT_TRUE(!a.slt(edge)); |
| 345 | EXPECT_TRUE( a.sle(edge)); |
| 346 | EXPECT_TRUE(!a.sgt(edge)); |
| 347 | EXPECT_TRUE( a.sge(edge)); |
| 348 | EXPECT_TRUE( a.slt(edgeP1)); |
| 349 | EXPECT_TRUE( a.sle(edgeP1)); |
| 350 | EXPECT_TRUE(!a.sgt(edgeP1)); |
| 351 | EXPECT_TRUE(!a.sge(edgeP1)); |
| 352 | EXPECT_TRUE( a.slt(edgeM1)); |
| 353 | EXPECT_TRUE( a.sle(edgeM1)); |
| 354 | EXPECT_TRUE(!a.sgt(edgeM1)); |
| 355 | EXPECT_TRUE(!a.sge(edgeM1)); |
| 356 | } |
| 357 | |
| 358 | TEST(APIntTest, compareWithHalfInt64Max) { |
| 359 | uint64_t edge = 0x4000000000000000; |
| 360 | uint64_t edgeP1 = edge + 1; |
| 361 | uint64_t edgeM1 = edge - 1; |
| 362 | auto a = APInt{64, edge}; |
| 363 | |
| 364 | EXPECT_TRUE(!a.ult(edge)); |
| 365 | EXPECT_TRUE( a.ule(edge)); |
| 366 | EXPECT_TRUE(!a.ugt(edge)); |
| 367 | EXPECT_TRUE( a.uge(edge)); |
| 368 | EXPECT_TRUE( a.ult(edgeP1)); |
| 369 | EXPECT_TRUE( a.ule(edgeP1)); |
| 370 | EXPECT_TRUE(!a.ugt(edgeP1)); |
| 371 | EXPECT_TRUE(!a.uge(edgeP1)); |
| 372 | EXPECT_TRUE(!a.ult(edgeM1)); |
| 373 | EXPECT_TRUE(!a.ule(edgeM1)); |
| 374 | EXPECT_TRUE( a.ugt(edgeM1)); |
| 375 | EXPECT_TRUE( a.uge(edgeM1)); |
| 376 | |
| 377 | EXPECT_TRUE(!a.slt(edge)); |
| 378 | EXPECT_TRUE( a.sle(edge)); |
| 379 | EXPECT_TRUE(!a.sgt(edge)); |
| 380 | EXPECT_TRUE( a.sge(edge)); |
| 381 | EXPECT_TRUE( a.slt(edgeP1)); |
| 382 | EXPECT_TRUE( a.sle(edgeP1)); |
| 383 | EXPECT_TRUE(!a.sgt(edgeP1)); |
| 384 | EXPECT_TRUE(!a.sge(edgeP1)); |
| 385 | EXPECT_TRUE(!a.slt(edgeM1)); |
| 386 | EXPECT_TRUE(!a.sle(edgeM1)); |
| 387 | EXPECT_TRUE( a.sgt(edgeM1)); |
| 388 | EXPECT_TRUE( a.sge(edgeM1)); |
| 389 | } |
| 390 | |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 391 | |
| 392 | // Tests different div/rem varaints using scheme (a * b + c) / a |
| 393 | void testDiv(APInt a, APInt b, APInt c) { |
| 394 | ASSERT_TRUE(a.uge(b)); // Must: a >= b |
| 395 | ASSERT_TRUE(a.ugt(c)); // Must: a > c |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 396 | |
| 397 | auto p = a * b + c; |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 398 | |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 399 | auto q = p.udiv(a); |
| 400 | auto r = p.urem(a); |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 401 | EXPECT_EQ(b, q); |
| 402 | EXPECT_EQ(c, r); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 403 | APInt::udivrem(p, a, q, r); |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 404 | EXPECT_EQ(b, q); |
| 405 | EXPECT_EQ(c, r); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 406 | q = p.sdiv(a); |
| 407 | r = p.srem(a); |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 408 | EXPECT_EQ(b, q); |
| 409 | EXPECT_EQ(c, r); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 410 | APInt::sdivrem(p, a, q, r); |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 411 | EXPECT_EQ(b, q); |
| 412 | EXPECT_EQ(c, r); |
| 413 | |
| 414 | if (b.ugt(c)) { // Test also symmetric case |
| 415 | q = p.udiv(b); |
| 416 | r = p.urem(b); |
| 417 | EXPECT_EQ(a, q); |
| 418 | EXPECT_EQ(c, r); |
| 419 | APInt::udivrem(p, b, q, r); |
| 420 | EXPECT_EQ(a, q); |
| 421 | EXPECT_EQ(c, r); |
| 422 | q = p.sdiv(b); |
| 423 | r = p.srem(b); |
| 424 | EXPECT_EQ(a, q); |
| 425 | EXPECT_EQ(c, r); |
| 426 | APInt::sdivrem(p, b, q, r); |
| 427 | EXPECT_EQ(a, q); |
| 428 | EXPECT_EQ(c, r); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | TEST(APIntTest, divrem_big1) { |
| 433 | // Tests KnuthDiv rare step D6 |
| 434 | testDiv({256, "1ffffffffffffffff", 16}, |
| 435 | {256, "1ffffffffffffffff", 16}, |
| 436 | {256, 0}); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | TEST(APIntTest, divrem_big2) { |
| 440 | // Tests KnuthDiv rare step D6 |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 441 | testDiv({1024, "112233ceff" |
| 442 | "cecece000000ffffffffffffffffffff" |
| 443 | "ffffffffffffffffffffffffffffffff" |
| 444 | "ffffffffffffffffffffffffffffffff" |
| 445 | "ffffffffffffffffffffffffffffff33", 16}, |
| 446 | {1024, "111111ffffffffffffffff" |
| 447 | "ffffffffffffffffffffffffffffffff" |
| 448 | "fffffffffffffffffffffffffffffccf" |
| 449 | "ffffffffffffffffffffffffffffff00", 16}, |
| 450 | {1024, 7919}); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST(APIntTest, divrem_big3) { |
| 454 | // Tests KnuthDiv case without shift |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 455 | testDiv({256, "80000001ffffffffffffffff", 16}, |
| 456 | {256, "ffffffffffffff0000000", 16}, |
| 457 | {256, 4219}); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | TEST(APIntTest, divrem_big4) { |
| 461 | // Tests heap allocation in divide() enfoced by huge numbers |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 462 | testDiv(APInt{4096, 5}.shl(2001), |
| 463 | APInt{4096, 1}.shl(2000), |
| 464 | APInt{4096, 4219*13}); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | TEST(APIntTest, divrem_big5) { |
| 468 | // Tests one word divisor case of divide() |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 469 | testDiv(APInt{1024, 19}.shl(811), |
| 470 | APInt{1024, 4356013}, // one word |
| 471 | APInt{1024, 1}); |
| 472 | } |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 473 | |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 474 | TEST(APIntTest, divrem_big6) { |
| 475 | // Tests some rare "borrow" cases in D4 step |
| 476 | testDiv(APInt{512, "ffffffffffffffff00000000000000000000000001", 16}, |
| 477 | APInt{512, "10000000000000001000000000000001", 16}, |
| 478 | APInt{512, "10000000000000000000000000000000", 16}); |
Yaron Keren | 39fc5a6 | 2015-03-26 19:45:19 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Yaron Keren | 61ad9d8 | 2015-04-22 18:49:59 +0000 | [diff] [blame] | 481 | TEST(APIntTest, divrem_big7) { |
| 482 | // Yet another test for KnuthDiv rare step D6. |
Pawel Bylica | 86ac447 | 2015-04-24 07:38:39 +0000 | [diff] [blame] | 483 | testDiv({224, "800000008000000200000005", 16}, |
| 484 | {224, "fffffffd", 16}, |
| 485 | {224, "80000000800000010000000f", 16}); |
Yaron Keren | 61ad9d8 | 2015-04-22 18:49:59 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Chris Lattner | b869a0a | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 488 | TEST(APIntTest, fromString) { |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 489 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); |
| 490 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); |
| 491 | EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); |
| 492 | EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); |
| 493 | EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); |
| 494 | |
| 495 | EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); |
| 496 | EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); |
| 497 | EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); |
| 498 | EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); |
| 499 | EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); |
| 500 | |
Daniel Dunbar | 73e76a1 | 2009-09-18 17:48:05 +0000 | [diff] [blame] | 501 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); |
| 502 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); |
| 503 | EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); |
| 504 | EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); |
| 505 | EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 506 | |
| 507 | |
| 508 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); |
| 509 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); |
| 510 | EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); |
| 511 | EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); |
| 512 | EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); |
| 513 | EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); |
| 514 | |
| 515 | EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); |
| 516 | EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); |
| 517 | EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); |
| 518 | EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); |
| 519 | EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); |
| 520 | EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); |
| 521 | |
Daniel Dunbar | 0ec4ed7 | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 522 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); |
| 523 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); |
| 524 | EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); |
| 525 | EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); |
| 526 | EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); |
| 527 | EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 528 | |
| 529 | |
| 530 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); |
| 531 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); |
| 532 | EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); |
| 533 | EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); |
| 534 | EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); |
| 535 | EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); |
| 536 | |
Daniel Dunbar | 0ec4ed7 | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 537 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); |
| 538 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); |
| 539 | EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); |
| 540 | EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); |
| 541 | EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); |
| 542 | EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 543 | |
| 544 | |
| 545 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); |
| 546 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); |
| 547 | EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); |
| 548 | EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); |
| 549 | EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); |
| 550 | EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); |
| 551 | |
Daniel Dunbar | 0ec4ed7 | 2009-09-17 17:46:53 +0000 | [diff] [blame] | 552 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); |
| 553 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); |
| 554 | EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); |
| 555 | EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); |
| 556 | EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); |
| 557 | EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 558 | |
| 559 | EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); |
| 560 | EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); |
| 561 | EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36)); |
| 562 | EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36)); |
| 563 | EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36)); |
| 564 | EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36)); |
| 565 | |
| 566 | EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); |
| 567 | EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); |
| 568 | EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); |
| 569 | EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36)); |
| 570 | EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36)); |
| 571 | EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36)); |
Chris Lattner | b869a0a | 2009-04-25 18:34:04 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Jeffrey Yasskin | 7a16288 | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 574 | TEST(APIntTest, FromArray) { |
| 575 | EXPECT_EQ(APInt(32, uint64_t(1)), APInt(32, ArrayRef<uint64_t>(1))); |
| 576 | } |
| 577 | |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 578 | TEST(APIntTest, StringBitsNeeded2) { |
| 579 | EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2)); |
| 580 | EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2)); |
| 581 | EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2)); |
| 582 | EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2)); |
| 583 | EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2)); |
| 584 | |
| 585 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2)); |
| 586 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2)); |
| 587 | EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2)); |
| 588 | EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2)); |
| 589 | EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2)); |
| 590 | |
| 591 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2)); |
| 592 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2)); |
| 593 | EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2)); |
| 594 | EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2)); |
| 595 | EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2)); |
| 596 | } |
| 597 | |
| 598 | TEST(APIntTest, StringBitsNeeded8) { |
| 599 | EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8)); |
| 600 | EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8)); |
| 601 | EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8)); |
| 602 | EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8)); |
| 603 | EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8)); |
| 604 | |
| 605 | EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8)); |
| 606 | EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8)); |
| 607 | EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8)); |
| 608 | EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8)); |
| 609 | EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8)); |
| 610 | |
| 611 | EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8)); |
| 612 | EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8)); |
| 613 | EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8)); |
| 614 | EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8)); |
| 615 | EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8)); |
| 616 | } |
| 617 | |
| 618 | TEST(APIntTest, StringBitsNeeded10) { |
| 619 | EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10)); |
| 620 | EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10)); |
| 621 | EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10)); |
| 622 | EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10)); |
| 623 | EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10)); |
| 624 | EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10)); |
| 625 | |
| 626 | EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10)); |
| 627 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10)); |
| 628 | EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10)); |
| 629 | EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10)); |
| 630 | EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10)); |
| 631 | |
| 632 | EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10)); |
| 633 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10)); |
| 634 | EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10)); |
| 635 | EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10)); |
| 636 | EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10)); |
| 637 | } |
| 638 | |
| 639 | TEST(APIntTest, StringBitsNeeded16) { |
| 640 | EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16)); |
| 641 | EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16)); |
| 642 | EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16)); |
| 643 | EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16)); |
| 644 | EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16)); |
| 645 | |
| 646 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16)); |
| 647 | EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16)); |
| 648 | EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16)); |
| 649 | EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16)); |
| 650 | EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16)); |
| 651 | |
| 652 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16)); |
| 653 | EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16)); |
| 654 | EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16)); |
| 655 | EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16)); |
| 656 | EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16)); |
| 657 | } |
| 658 | |
Dylan Noblesmith | c8c184d | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 659 | TEST(APIntTest, toString) { |
| 660 | SmallString<16> S; |
| 661 | bool isSigned; |
| 662 | |
| 663 | APInt(8, 0).toString(S, 2, true, true); |
| 664 | EXPECT_EQ(S.str().str(), "0b0"); |
| 665 | S.clear(); |
| 666 | APInt(8, 0).toString(S, 8, true, true); |
| 667 | EXPECT_EQ(S.str().str(), "00"); |
| 668 | S.clear(); |
| 669 | APInt(8, 0).toString(S, 10, true, true); |
| 670 | EXPECT_EQ(S.str().str(), "0"); |
| 671 | S.clear(); |
| 672 | APInt(8, 0).toString(S, 16, true, true); |
| 673 | EXPECT_EQ(S.str().str(), "0x0"); |
| 674 | S.clear(); |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 675 | APInt(8, 0).toString(S, 36, true, false); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 676 | EXPECT_EQ(S.str().str(), "0"); |
| 677 | S.clear(); |
Dylan Noblesmith | c8c184d | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 678 | |
| 679 | isSigned = false; |
| 680 | APInt(8, 255, isSigned).toString(S, 2, isSigned, true); |
| 681 | EXPECT_EQ(S.str().str(), "0b11111111"); |
| 682 | S.clear(); |
| 683 | APInt(8, 255, isSigned).toString(S, 8, isSigned, true); |
| 684 | EXPECT_EQ(S.str().str(), "0377"); |
| 685 | S.clear(); |
| 686 | APInt(8, 255, isSigned).toString(S, 10, isSigned, true); |
| 687 | EXPECT_EQ(S.str().str(), "255"); |
| 688 | S.clear(); |
| 689 | APInt(8, 255, isSigned).toString(S, 16, isSigned, true); |
| 690 | EXPECT_EQ(S.str().str(), "0xFF"); |
| 691 | S.clear(); |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 692 | APInt(8, 255, isSigned).toString(S, 36, isSigned, false); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 693 | EXPECT_EQ(S.str().str(), "73"); |
| 694 | S.clear(); |
Dylan Noblesmith | c8c184d | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 695 | |
| 696 | isSigned = true; |
| 697 | APInt(8, 255, isSigned).toString(S, 2, isSigned, true); |
| 698 | EXPECT_EQ(S.str().str(), "-0b1"); |
| 699 | S.clear(); |
| 700 | APInt(8, 255, isSigned).toString(S, 8, isSigned, true); |
| 701 | EXPECT_EQ(S.str().str(), "-01"); |
| 702 | S.clear(); |
| 703 | APInt(8, 255, isSigned).toString(S, 10, isSigned, true); |
| 704 | EXPECT_EQ(S.str().str(), "-1"); |
| 705 | S.clear(); |
| 706 | APInt(8, 255, isSigned).toString(S, 16, isSigned, true); |
| 707 | EXPECT_EQ(S.str().str(), "-0x1"); |
| 708 | S.clear(); |
Dylan Noblesmith | 1c419ff | 2011-12-16 20:36:31 +0000 | [diff] [blame] | 709 | APInt(8, 255, isSigned).toString(S, 36, isSigned, false); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 710 | EXPECT_EQ(S.str().str(), "-1"); |
| 711 | S.clear(); |
Dylan Noblesmith | c8c184d | 2011-06-15 23:36:34 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Dan Gohman | c4e367b | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 714 | TEST(APIntTest, Log2) { |
Duncan Sands | f7ad620 | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 715 | EXPECT_EQ(APInt(15, 7).logBase2(), 2U); |
| 716 | EXPECT_EQ(APInt(15, 7).ceilLogBase2(), 3U); |
Dan Gohman | c4e367b | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 717 | EXPECT_EQ(APInt(15, 7).exactLogBase2(), -1); |
Duncan Sands | f7ad620 | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 718 | EXPECT_EQ(APInt(15, 8).logBase2(), 3U); |
| 719 | EXPECT_EQ(APInt(15, 8).ceilLogBase2(), 3U); |
Dan Gohman | c4e367b | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 720 | EXPECT_EQ(APInt(15, 8).exactLogBase2(), 3); |
Duncan Sands | f7ad620 | 2009-10-13 09:23:11 +0000 | [diff] [blame] | 721 | EXPECT_EQ(APInt(15, 9).logBase2(), 3U); |
| 722 | EXPECT_EQ(APInt(15, 9).ceilLogBase2(), 4U); |
Dan Gohman | c4e367b | 2009-10-13 01:49:02 +0000 | [diff] [blame] | 723 | EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); |
| 724 | } |
Erick Tryzelaar | dadb1571 | 2009-08-21 03:15:28 +0000 | [diff] [blame] | 725 | |
Cameron Zwarich | 8731d0c | 2011-02-21 00:22:02 +0000 | [diff] [blame] | 726 | TEST(APIntTest, magic) { |
| 727 | EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16)); |
| 728 | EXPECT_EQ(APInt(32, 3).magic().s, 0U); |
| 729 | EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16)); |
| 730 | EXPECT_EQ(APInt(32, 5).magic().s, 1U); |
| 731 | EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16)); |
| 732 | EXPECT_EQ(APInt(32, 7).magic().s, 2U); |
| 733 | } |
| 734 | |
| 735 | TEST(APIntTest, magicu) { |
| 736 | EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16)); |
| 737 | EXPECT_EQ(APInt(32, 3).magicu().s, 1U); |
| 738 | EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16)); |
| 739 | EXPECT_EQ(APInt(32, 5).magicu().s, 2U); |
| 740 | EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16)); |
| 741 | EXPECT_EQ(APInt(32, 7).magicu().s, 3U); |
Benjamin Kramer | 09a51ba | 2011-03-17 20:39:06 +0000 | [diff] [blame] | 742 | EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16)); |
| 743 | EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U); |
Cameron Zwarich | 8731d0c | 2011-02-21 00:22:02 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Erick Tryzelaar | 927191f | 2009-08-17 00:55:33 +0000 | [diff] [blame] | 746 | #ifdef GTEST_HAS_DEATH_TEST |
Jeffrey Yasskin | b5cd013 | 2010-03-17 01:18:45 +0000 | [diff] [blame] | 747 | #ifndef NDEBUG |
Erick Tryzelaar | 2b01eab | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 748 | TEST(APIntTest, StringDeath) { |
Erick Tryzelaar | 1264bcb | 2009-08-21 03:15:14 +0000 | [diff] [blame] | 749 | EXPECT_DEATH(APInt(0, "", 0), "Bitwidth too small"); |
| 750 | EXPECT_DEATH(APInt(32, "", 0), "Invalid string length"); |
Douglas Gregor | 663c068 | 2011-09-14 15:54:46 +0000 | [diff] [blame] | 751 | EXPECT_DEATH(APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!"); |
Erick Tryzelaar | 2b01eab | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 752 | EXPECT_DEATH(APInt(32, "", 10), "Invalid string length"); |
Bill Wendling | ef793cc | 2009-08-21 06:35:41 +0000 | [diff] [blame] | 753 | EXPECT_DEATH(APInt(32, "-", 10), "String is only a sign, needs a value."); |
Erick Tryzelaar | 2b01eab | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 754 | EXPECT_DEATH(APInt(1, "1234", 10), "Insufficient bit width"); |
| 755 | EXPECT_DEATH(APInt(32, "\0", 10), "Invalid string length"); |
| 756 | EXPECT_DEATH(APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string"); |
| 757 | EXPECT_DEATH(APInt(32, "1L", 10), "Invalid character in digit string"); |
| 758 | } |
Erick Tryzelaar | 927191f | 2009-08-17 00:55:33 +0000 | [diff] [blame] | 759 | #endif |
Jeffrey Yasskin | b5cd013 | 2010-03-17 01:18:45 +0000 | [diff] [blame] | 760 | #endif |
Erick Tryzelaar | 2b01eab | 2009-08-16 23:36:01 +0000 | [diff] [blame] | 761 | |
Eli Friedman | 1954641 | 2011-10-07 23:40:49 +0000 | [diff] [blame] | 762 | TEST(APIntTest, mul_clear) { |
| 763 | APInt ValA(65, -1ULL); |
| 764 | APInt ValB(65, 4); |
| 765 | APInt ValC(65, 0); |
| 766 | ValC = ValA * ValB; |
| 767 | ValA *= ValB; |
| 768 | EXPECT_EQ(ValA.toString(10, false), ValC.toString(10, false)); |
| 769 | } |
| 770 | |
Eli Friedman | f70c862 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 771 | TEST(APIntTest, Rotate) { |
| 772 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0)); |
| 773 | EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1)); |
| 774 | EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2)); |
| 775 | EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4)); |
| 776 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8)); |
| 777 | |
| 778 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0)); |
| 779 | EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1)); |
| 780 | EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2)); |
| 781 | EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4)); |
| 782 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8)); |
| 783 | |
| 784 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0)); |
| 785 | EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1)); |
| 786 | EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2)); |
| 787 | EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4)); |
| 788 | EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8)); |
| 789 | |
| 790 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0)); |
| 791 | EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1)); |
| 792 | EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2)); |
| 793 | EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4)); |
| 794 | EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8)); |
Benjamin Kramer | 47ddf60 | 2012-02-07 16:27:39 +0000 | [diff] [blame] | 795 | |
| 796 | APInt Big(256, "00004000800000000000000000003fff8000000000000000", 16); |
| 797 | APInt Rot(256, "3fff80000000000000000000000000000000000040008000", 16); |
| 798 | EXPECT_EQ(Rot, Big.rotr(144)); |
Eli Friedman | f70c862 | 2011-12-22 22:11:19 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Benjamin Kramer | 5c3e21b | 2013-02-20 13:00:06 +0000 | [diff] [blame] | 801 | TEST(APIntTest, Splat) { |
| 802 | APInt ValA(8, 0x01); |
| 803 | EXPECT_EQ(ValA, APInt::getSplat(8, ValA)); |
| 804 | EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA)); |
| 805 | |
| 806 | APInt ValB(3, 5); |
| 807 | EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB)); |
| 808 | EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB)); |
| 809 | } |
| 810 | |
Michael Gottesman | 9d406f4 | 2013-05-28 19:50:20 +0000 | [diff] [blame] | 811 | TEST(APIntTest, tcDecrement) { |
| 812 | // Test single word decrement. |
| 813 | |
| 814 | // No out borrow. |
| 815 | { |
| 816 | integerPart singleWord = ~integerPart(0) << (integerPartWidth - 1); |
| 817 | integerPart carry = APInt::tcDecrement(&singleWord, 1); |
| 818 | EXPECT_EQ(carry, integerPart(0)); |
| 819 | EXPECT_EQ(singleWord, ~integerPart(0) >> 1); |
| 820 | } |
| 821 | |
| 822 | // With out borrow. |
| 823 | { |
| 824 | integerPart singleWord = 0; |
| 825 | integerPart carry = APInt::tcDecrement(&singleWord, 1); |
| 826 | EXPECT_EQ(carry, integerPart(1)); |
| 827 | EXPECT_EQ(singleWord, ~integerPart(0)); |
| 828 | } |
| 829 | |
| 830 | // Test multiword decrement. |
| 831 | |
| 832 | // No across word borrow, no out borrow. |
| 833 | { |
| 834 | integerPart test[4] = {0x1, 0x1, 0x1, 0x1}; |
| 835 | integerPart expected[4] = {0x0, 0x1, 0x1, 0x1}; |
| 836 | APInt::tcDecrement(test, 4); |
| 837 | EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); |
| 838 | } |
| 839 | |
| 840 | // 1 across word borrow, no out borrow. |
| 841 | { |
| 842 | integerPart test[4] = {0x0, 0xF, 0x1, 0x1}; |
| 843 | integerPart expected[4] = {~integerPart(0), 0xE, 0x1, 0x1}; |
| 844 | integerPart carry = APInt::tcDecrement(test, 4); |
| 845 | EXPECT_EQ(carry, integerPart(0)); |
| 846 | EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); |
| 847 | } |
| 848 | |
| 849 | // 2 across word borrow, no out borrow. |
| 850 | { |
| 851 | integerPart test[4] = {0x0, 0x0, 0xC, 0x1}; |
| 852 | integerPart expected[4] = {~integerPart(0), ~integerPart(0), 0xB, 0x1}; |
| 853 | integerPart carry = APInt::tcDecrement(test, 4); |
| 854 | EXPECT_EQ(carry, integerPart(0)); |
| 855 | EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); |
| 856 | } |
| 857 | |
| 858 | // 3 across word borrow, no out borrow. |
| 859 | { |
| 860 | integerPart test[4] = {0x0, 0x0, 0x0, 0x1}; |
| 861 | integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), 0x0}; |
| 862 | integerPart carry = APInt::tcDecrement(test, 4); |
| 863 | EXPECT_EQ(carry, integerPart(0)); |
| 864 | EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); |
| 865 | } |
| 866 | |
| 867 | // 3 across word borrow, with out borrow. |
| 868 | { |
| 869 | integerPart test[4] = {0x0, 0x0, 0x0, 0x0}; |
| 870 | integerPart expected[4] = {~integerPart(0), ~integerPart(0), ~integerPart(0), ~integerPart(0)}; |
| 871 | integerPart carry = APInt::tcDecrement(test, 4); |
| 872 | EXPECT_EQ(carry, integerPart(1)); |
| 873 | EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); |
| 874 | } |
| 875 | } |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 876 | |
Michael Gottesman | e1fad2b | 2013-12-13 22:00:19 +0000 | [diff] [blame] | 877 | TEST(APIntTest, arrayAccess) { |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 878 | // Single word check. |
| 879 | uint64_t E1 = 0x2CA7F46BF6569915ULL; |
| 880 | APInt A1(64, E1); |
Duncan P. N. Exon Smith | c47069b | 2014-01-31 21:45:51 +0000 | [diff] [blame] | 881 | for (unsigned i = 0, e = 64; i < e; ++i) { |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 882 | EXPECT_EQ(bool(E1 & (1ULL << i)), |
Michael Gottesman | e1fad2b | 2013-12-13 22:00:19 +0000 | [diff] [blame] | 883 | A1[i]); |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | // Multiword check. |
| 887 | integerPart E2[4] = { |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 888 | 0xEB6EB136591CBA21ULL, |
| 889 | 0x7B9358BD6A33F10AULL, |
| 890 | 0x7E7FFA5EADD8846ULL, |
| 891 | 0x305F341CA00B613DULL |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 892 | }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 893 | APInt A2(integerPartWidth*4, E2); |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 894 | for (unsigned i = 0; i < 4; ++i) { |
| 895 | for (unsigned j = 0; j < integerPartWidth; ++j) { |
| 896 | EXPECT_EQ(bool(E2[i] & (1ULL << j)), |
Michael Gottesman | e1fad2b | 2013-12-13 22:00:19 +0000 | [diff] [blame] | 897 | A2[i*integerPartWidth + j]); |
Michael Gottesman | 4497d96 | 2013-12-13 20:47:34 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Michael Gottesman | 073af74 | 2014-01-19 20:33:38 +0000 | [diff] [blame] | 902 | TEST(APIntTest, LargeAPIntConstruction) { |
| 903 | // Check that we can properly construct very large APInt. It is very |
| 904 | // unlikely that people will ever do this, but it is a legal input, |
| 905 | // so we should not crash on it. |
| 906 | APInt A9(UINT32_MAX, 0); |
| 907 | EXPECT_FALSE(A9.getBoolValue()); |
| 908 | } |
| 909 | |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 910 | TEST(APIntTest, nearestLogBase2) { |
Duncan P. N. Exon Smith | c47069b | 2014-01-31 21:45:51 +0000 | [diff] [blame] | 911 | // Single word check. |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 912 | |
| 913 | // Test round up. |
| 914 | uint64_t I1 = 0x1800001; |
| 915 | APInt A1(64, I1); |
| 916 | EXPECT_EQ(A1.nearestLogBase2(), A1.ceilLogBase2()); |
| 917 | |
| 918 | // Test round down. |
| 919 | uint64_t I2 = 0x1000011; |
| 920 | APInt A2(64, I2); |
| 921 | EXPECT_EQ(A2.nearestLogBase2(), A2.logBase2()); |
| 922 | |
| 923 | // Test ties round up. |
| 924 | uint64_t I3 = 0x1800000; |
| 925 | APInt A3(64, I3); |
| 926 | EXPECT_EQ(A3.nearestLogBase2(), A3.ceilLogBase2()); |
| 927 | |
| 928 | // Multiple word check. |
| 929 | |
| 930 | // Test round up. |
| 931 | integerPart I4[4] = {0x0, 0xF, 0x18, 0x0}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 932 | APInt A4(integerPartWidth*4, I4); |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 933 | EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2()); |
| 934 | |
| 935 | // Test round down. |
| 936 | integerPart I5[4] = {0x0, 0xF, 0x10, 0x0}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 937 | APInt A5(integerPartWidth*4, I5); |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 938 | EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2()); |
| 939 | |
| 940 | // Test ties round up. |
| 941 | uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 942 | APInt A6(integerPartWidth*4, I6); |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 943 | EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2()); |
Michael Gottesman | 84fcbde | 2014-01-19 20:33:48 +0000 | [diff] [blame] | 944 | |
| 945 | // Test BitWidth == 1 special cases. |
| 946 | APInt A7(1, 1); |
| 947 | EXPECT_EQ(A7.nearestLogBase2(), 0ULL); |
| 948 | APInt A8(1, 0); |
| 949 | EXPECT_EQ(A8.nearestLogBase2(), UINT32_MAX); |
| 950 | |
| 951 | // Test the zero case when we have a bit width large enough such |
| 952 | // that the bit width is larger than UINT32_MAX-1. |
| 953 | APInt A9(UINT32_MAX, 0); |
| 954 | EXPECT_EQ(A9.nearestLogBase2(), UINT32_MAX); |
Michael Gottesman | f6d58ff | 2013-12-13 20:47:37 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 957 | TEST(APIntTest, IsSplat) { |
| 958 | APInt A(32, 0x01010101); |
| 959 | EXPECT_FALSE(A.isSplat(1)); |
| 960 | EXPECT_FALSE(A.isSplat(2)); |
| 961 | EXPECT_FALSE(A.isSplat(4)); |
| 962 | EXPECT_TRUE(A.isSplat(8)); |
| 963 | EXPECT_TRUE(A.isSplat(16)); |
| 964 | EXPECT_TRUE(A.isSplat(32)); |
| 965 | |
| 966 | APInt B(24, 0xAAAAAA); |
| 967 | EXPECT_FALSE(B.isSplat(1)); |
| 968 | EXPECT_TRUE(B.isSplat(2)); |
| 969 | EXPECT_TRUE(B.isSplat(4)); |
| 970 | EXPECT_TRUE(B.isSplat(8)); |
| 971 | EXPECT_TRUE(B.isSplat(24)); |
| 972 | |
| 973 | APInt C(24, 0xABAAAB); |
| 974 | EXPECT_FALSE(C.isSplat(1)); |
| 975 | EXPECT_FALSE(C.isSplat(2)); |
| 976 | EXPECT_FALSE(C.isSplat(4)); |
| 977 | EXPECT_FALSE(C.isSplat(8)); |
| 978 | EXPECT_TRUE(C.isSplat(24)); |
| 979 | |
| 980 | APInt D(32, 0xABBAABBA); |
| 981 | EXPECT_FALSE(D.isSplat(1)); |
| 982 | EXPECT_FALSE(D.isSplat(2)); |
| 983 | EXPECT_FALSE(D.isSplat(4)); |
| 984 | EXPECT_FALSE(D.isSplat(8)); |
| 985 | EXPECT_TRUE(D.isSplat(16)); |
| 986 | EXPECT_TRUE(D.isSplat(32)); |
| 987 | |
| 988 | APInt E(32, 0); |
| 989 | EXPECT_TRUE(E.isSplat(1)); |
| 990 | EXPECT_TRUE(E.isSplat(2)); |
| 991 | EXPECT_TRUE(E.isSplat(4)); |
| 992 | EXPECT_TRUE(E.isSplat(8)); |
| 993 | EXPECT_TRUE(E.isSplat(16)); |
| 994 | EXPECT_TRUE(E.isSplat(32)); |
| 995 | } |
| 996 | |
Aaron Ballman | 8bd9897 | 2015-01-13 14:30:07 +0000 | [diff] [blame] | 997 | #if defined(__clang__) |
Richard Trieu | a64949d | 2015-01-14 01:50:12 +0000 | [diff] [blame] | 998 | // Disable the pragma warning from versions of Clang without -Wself-move |
| 999 | #pragma clang diagnostic push |
| 1000 | #pragma clang diagnostic ignored "-Wunknown-pragmas" |
Richard Trieu | 5dc76a5 | 2015-01-13 02:10:33 +0000 | [diff] [blame] | 1001 | // Disable the warning that triggers on exactly what is being tested. |
| 1002 | #pragma clang diagnostic push |
| 1003 | #pragma clang diagnostic ignored "-Wself-move" |
Aaron Ballman | 8bd9897 | 2015-01-13 14:30:07 +0000 | [diff] [blame] | 1004 | #endif |
Reid Kleckner | 3d4eae7 | 2014-08-12 22:01:39 +0000 | [diff] [blame] | 1005 | TEST(APIntTest, SelfMoveAssignment) { |
| 1006 | APInt X(32, 0xdeadbeef); |
| 1007 | X = std::move(X); |
David Blaikie | 1508a65 | 2014-08-12 23:23:05 +0000 | [diff] [blame] | 1008 | EXPECT_EQ(32u, X.getBitWidth()); |
Reid Kleckner | 3d4eae7 | 2014-08-12 22:01:39 +0000 | [diff] [blame] | 1009 | EXPECT_EQ(0xdeadbeefULL, X.getLimitedValue()); |
| 1010 | |
| 1011 | uint64_t Bits[] = {0xdeadbeefdeadbeefULL, 0xdeadbeefdeadbeefULL}; |
| 1012 | APInt Y(128, Bits); |
| 1013 | Y = std::move(Y); |
David Blaikie | 1508a65 | 2014-08-12 23:23:05 +0000 | [diff] [blame] | 1014 | EXPECT_EQ(128u, Y.getBitWidth()); |
Reid Kleckner | 3d4eae7 | 2014-08-12 22:01:39 +0000 | [diff] [blame] | 1015 | EXPECT_EQ(~0ULL, Y.getLimitedValue()); |
| 1016 | const uint64_t *Raw = Y.getRawData(); |
David Blaikie | 1508a65 | 2014-08-12 23:23:05 +0000 | [diff] [blame] | 1017 | EXPECT_EQ(2u, Y.getNumWords()); |
Reid Kleckner | 3d4eae7 | 2014-08-12 22:01:39 +0000 | [diff] [blame] | 1018 | EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[0]); |
| 1019 | EXPECT_EQ(0xdeadbeefdeadbeefULL, Raw[1]); |
| 1020 | } |
Aaron Ballman | 8bd9897 | 2015-01-13 14:30:07 +0000 | [diff] [blame] | 1021 | #if defined(__clang__) |
Richard Trieu | 5dc76a5 | 2015-01-13 02:10:33 +0000 | [diff] [blame] | 1022 | #pragma clang diagnostic pop |
Richard Trieu | a64949d | 2015-01-14 01:50:12 +0000 | [diff] [blame] | 1023 | #pragma clang diagnostic pop |
Aaron Ballman | 8bd9897 | 2015-01-13 14:30:07 +0000 | [diff] [blame] | 1024 | #endif |
Nick Lewycky | ee22611 | 2009-01-19 18:08:33 +0000 | [diff] [blame] | 1025 | } |