| Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 1 | //===-- APInt.cpp - Implement APInt class ---------------------------------===// | 
 | 2 | // | 
 | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
 | 5 | // This file was developed by Sheng Zhou and is distributed under the | 
 | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. | 
 | 7 | // | 
 | 8 | //===----------------------------------------------------------------------===// | 
 | 9 | // | 
 | 10 | // This file implements a class to represent arbitrary precision integral | 
 | 11 | // constant values. | 
 | 12 | // | 
 | 13 | //===----------------------------------------------------------------------===// | 
 | 14 |  | 
 | 15 | #include "llvm/ADT/APInt.h" | 
| Chris Lattner | 6ad4c14 | 2007-02-06 05:38:37 +0000 | [diff] [blame] | 16 |  | 
 | 17 | #if 0 | 
| Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" | 
 | 19 | #include "llvm/Support/MathExtras.h" | 
 | 20 | #include <strings.h> | 
 | 21 | #include <iostream> | 
 | 22 | #include <sstream> | 
 | 23 | #include <iomanip> | 
 | 24 | #include <cstdlib> | 
 | 25 | using namespace llvm; | 
 | 26 |  | 
| Zhou Sheng | 353815d | 2007-02-06 06:04:53 +0000 | [diff] [blame] | 27 | /// mul_1 - This function performs the multiplication operation on a | 
 | 28 | /// large integer (represented as an integer array) and a uint64_t integer. | 
 | 29 | /// @returns the carry of the multiplication. | 
 | 30 | static uint64_t mul_1(uint64_t dest[], uint64_t x[], | 
 | 31 |                      unsigned len, uint64_t y) { | 
 | 32 |   // Split y into high 32-bit part and low 32-bit part. | 
 | 33 |   uint64_t ly = y & 0xffffffffULL, hy = y >> 32; | 
 | 34 |   uint64_t carry = 0, lx, hx; | 
 | 35 |   for (unsigned i = 0; i < len; ++i) { | 
 | 36 |     lx = x[i] & 0xffffffffULL; | 
 | 37 |     hx = x[i] >> 32; | 
 | 38 |     // hasCarry - A flag to indicate if has carry. | 
 | 39 |     // hasCarry == 0, no carry | 
 | 40 |     // hasCarry == 1, has carry | 
 | 41 |     // hasCarry == 2, no carry and the calculation result == 0. | 
 | 42 |     uint8_t hasCarry = 0; | 
 | 43 |     dest[i] = carry + lx * ly; | 
 | 44 |     // Determine if the add above introduces carry. | 
 | 45 |     hasCarry = (dest[i] < carry) ? 1 : 0; | 
 | 46 |     carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0); | 
 | 47 |     // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +  | 
 | 48 |     // (2^32 - 1) + 2^32 = 2^64. | 
 | 49 |     hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); | 
 | 50 |  | 
 | 51 |     carry += (lx * hy) & 0xffffffffULL; | 
 | 52 |     dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL); | 
 | 53 |     carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +  | 
 | 54 |             (carry >> 32) + ((lx * hy) >> 32) + hx * hy; | 
 | 55 |   } | 
 | 56 |  | 
 | 57 |   return carry; | 
 | 58 | } | 
 | 59 |  | 
 | 60 | /// mul - This function multiplies integer array x[] by integer array y[] and | 
 | 61 | /// stores the result into integer array dest[]. | 
 | 62 | /// Note the array dest[]'s size should no less than xlen + ylen. | 
 | 63 | static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, | 
 | 64 |                uint64_t y[], unsigned ylen) { | 
 | 65 |   dest[xlen] = mul_1(dest, x, xlen, y[0]); | 
 | 66 |  | 
 | 67 |   for (unsigned i = 1; i < ylen; ++i) { | 
 | 68 |     uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32; | 
 | 69 |     uint64_t carry = 0, lx, hx; | 
 | 70 |     for (unsigned j = 0; j < xlen; ++j) { | 
 | 71 |       lx = x[j] & 0xffffffffULL; | 
 | 72 |       hx = x[j] >> 32; | 
 | 73 |       // hasCarry - A flag to indicate if has carry. | 
 | 74 |       // hasCarry == 0, no carry | 
 | 75 |       // hasCarry == 1, has carry | 
 | 76 |       // hasCarry == 2, no carry and the calculation result == 0. | 
 | 77 |       uint8_t hasCarry = 0; | 
 | 78 |       uint64_t resul = carry + lx * ly; | 
 | 79 |       hasCarry = (resul < carry) ? 1 : 0; | 
 | 80 |       carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32); | 
 | 81 |       hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0); | 
 | 82 |  | 
 | 83 |       carry += (lx * hy) & 0xffffffffULL; | 
 | 84 |       resul = (carry << 32) | (resul & 0xffffffffULL); | 
 | 85 |       dest[i+j] += resul; | 
 | 86 |       carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+ | 
 | 87 |               (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +  | 
 | 88 |               ((lx * hy) >> 32) + hx * hy; | 
 | 89 |     } | 
 | 90 |     dest[i+xlen] = carry; | 
 | 91 |   } | 
 | 92 | } | 
 | 93 |  | 
 | 94 | /// add_1 - This function adds the integer array x[] by integer y and | 
 | 95 | /// returns the carry. | 
 | 96 | /// @returns the carry of the addition. | 
 | 97 | static uint64_t add_1(uint64_t dest[], uint64_t x[], | 
 | 98 |                       unsigned len, uint64_t y) { | 
 | 99 |   uint64_t carry = y; | 
 | 100 |  | 
 | 101 |   for (unsigned i = 0; i < len; ++i) { | 
 | 102 |     dest[i] = carry + x[i]; | 
 | 103 |     carry = (dest[i] < carry) ? 1 : 0; | 
 | 104 |   } | 
 | 105 |   return carry; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | /// add - This function adds the integer array x[] by integer array | 
 | 109 | /// y[] and returns the carry. | 
 | 110 | static uint64_t add(uint64_t dest[], uint64_t x[], | 
 | 111 |                     uint64_t y[], unsigned len) { | 
 | 112 |   unsigned carry = 0; | 
 | 113 |    | 
 | 114 |   for (unsigned i = 0; i< len; ++i) { | 
 | 115 |     carry += x[i]; | 
 | 116 |     dest[i] = carry + y[i]; | 
 | 117 |     carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0); | 
 | 118 |   } | 
 | 119 |   return carry; | 
 | 120 | } | 
 | 121 |  | 
 | 122 | /// sub_1 - This function subtracts the integer array x[] by | 
 | 123 | /// integer y and returns the borrow-out carry. | 
 | 124 | static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) { | 
 | 125 |   uint64_t cy = y; | 
 | 126 |  | 
 | 127 |   for (unsigned i = 0; i < len; ++i) { | 
 | 128 |     uint64_t X = x[i]; | 
 | 129 |     x[i] -= cy; | 
 | 130 |     if (cy > X)  | 
 | 131 |       cy = 1; | 
 | 132 |     else { | 
 | 133 |       cy = 0; | 
 | 134 |       break; | 
 | 135 |     } | 
 | 136 |   } | 
 | 137 |  | 
 | 138 |   return cy; | 
 | 139 | } | 
 | 140 |  | 
 | 141 | /// sub - This function subtracts the integer array x[] by | 
 | 142 | /// integer array y[], and returns the borrow-out carry. | 
 | 143 | static uint64_t sub(uint64_t dest[], uint64_t x[], | 
 | 144 |                     uint64_t y[], unsigned len) { | 
 | 145 |   // Carry indicator. | 
 | 146 |   uint64_t cy = 0; | 
 | 147 |    | 
 | 148 |   for (unsigned i = 0; i < len; ++i) { | 
 | 149 |     uint64_t Y = y[i], X = x[i]; | 
 | 150 |     Y += cy; | 
 | 151 |  | 
 | 152 |     cy = Y < cy ? 1 : 0; | 
 | 153 |     Y = X - Y; | 
 | 154 |     cy += Y > X ? 1 : 0; | 
 | 155 |     dest[i] = Y; | 
 | 156 |   } | 
 | 157 |   return cy; | 
 | 158 | } | 
 | 159 |  | 
 | 160 | /// UnitDiv - This function divides N by D,  | 
 | 161 | /// and returns (remainder << 32) | quotient. | 
 | 162 | /// Assumes (N >> 32) < D. | 
 | 163 | static uint64_t unitDiv(uint64_t N, unsigned D) { | 
 | 164 |   uint64_t q, r;                   // q: quotient, r: remainder. | 
 | 165 |   uint64_t a1 = N >> 32;           // a1: high 32-bit part of N. | 
 | 166 |   uint64_t a0 = N & 0xffffffffL;   // a0: low 32-bit part of N | 
 | 167 |   if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) { | 
 | 168 |       q = N / D; | 
 | 169 |       r = N % D; | 
 | 170 |   } | 
 | 171 |   else { | 
 | 172 |     // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d | 
 | 173 |     uint64_t c = N - ((uint64_t) D << 31); | 
 | 174 |     // Divide (c1*2^32 + c0) by d | 
 | 175 |     q = c / D; | 
 | 176 |     r = c % D; | 
 | 177 |     // Add 2^31 to quotient  | 
 | 178 |     q += 1 << 31; | 
 | 179 |   } | 
 | 180 |  | 
 | 181 |   return (r << 32) | (q & 0xFFFFFFFFl); | 
 | 182 | } | 
 | 183 |  | 
 | 184 | /// subMul - This function substracts x[len-1:0] * y from  | 
 | 185 | /// dest[offset+len-1:offset], and returns the most significant  | 
 | 186 | /// word of the product, minus the borrow-out from the subtraction. | 
 | 187 | static unsigned subMul(unsigned dest[], unsigned offset,  | 
 | 188 |                         unsigned x[], unsigned len, unsigned y) { | 
 | 189 |   uint64_t yl = (uint64_t) y & 0xffffffffL; | 
 | 190 |   unsigned carry = 0; | 
 | 191 |   unsigned j = 0; | 
 | 192 |   do { | 
 | 193 |     uint64_t prod = ((uint64_t) x[j] & 0xffffffffL) * yl; | 
 | 194 |     unsigned prod_low = (unsigned) prod; | 
 | 195 |     unsigned prod_high = (unsigned) (prod >> 32); | 
 | 196 |     prod_low += carry; | 
 | 197 |     carry = (prod_low < carry ? 1 : 0) + prod_high; | 
 | 198 |     unsigned x_j = dest[offset+j]; | 
 | 199 |     prod_low = x_j - prod_low; | 
 | 200 |     if (prod_low > x_j) ++carry; | 
 | 201 |     dest[offset+j] = prod_low; | 
 | 202 |   } while (++j < len); | 
 | 203 |   return carry; | 
 | 204 | } | 
 | 205 |  | 
 | 206 | /// div - This is basically Knuth's formulation of the classical algorithm. | 
 | 207 | /// Correspondance with Knuth's notation: | 
 | 208 | /// Knuth's u[0:m+n] == zds[nx:0]. | 
 | 209 | /// Knuth's v[1:n] == y[ny-1:0] | 
 | 210 | /// Knuth's n == ny. | 
 | 211 | /// Knuth's m == nx-ny. | 
 | 212 | /// Our nx == Knuth's m+n. | 
 | 213 | /// Could be re-implemented using gmp's mpn_divrem: | 
 | 214 | /// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny). | 
 | 215 | static void div(unsigned zds[], unsigned nx, unsigned y[], unsigned ny) { | 
 | 216 |   unsigned j = nx; | 
 | 217 |   do {                          // loop over digits of quotient | 
 | 218 |     // Knuth's j == our nx-j. | 
 | 219 |     // Knuth's u[j:j+n] == our zds[j:j-ny]. | 
 | 220 |     unsigned qhat;  // treated as unsigned | 
 | 221 |     if (zds[j] == y[ny-1]) qhat = -1U;  // 0xffffffff | 
 | 222 |     else { | 
 | 223 |       uint64_t w = (((uint64_t)(zds[j])) << 32) +  | 
 | 224 |                    ((uint64_t)zds[j-1] & 0xffffffffL); | 
 | 225 |       qhat = (unsigned) unitDiv(w, y[ny-1]); | 
 | 226 |     } | 
 | 227 |     if (qhat) { | 
 | 228 |       unsigned borrow = subMul(zds, j - ny, y, ny, qhat); | 
 | 229 |       unsigned save = zds[j]; | 
 | 230 |       uint64_t num = ((uint64_t)save&0xffffffffL) -  | 
 | 231 |                      ((uint64_t)borrow&0xffffffffL); | 
 | 232 |       while (num) { | 
 | 233 |         qhat--; | 
 | 234 |         uint64_t carry = 0; | 
 | 235 |         for (unsigned i = 0;  i < ny; i++) { | 
 | 236 |           carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL) | 
 | 237 |             + ((uint64_t) y[i] & 0xffffffffL); | 
 | 238 |           zds[j-ny+i] = (unsigned) carry; | 
 | 239 |           carry >>= 32; | 
 | 240 |         } | 
 | 241 |         zds[j] += carry; | 
 | 242 |         num = carry - 1; | 
 | 243 |       } | 
 | 244 |     } | 
 | 245 |     zds[j] = qhat; | 
 | 246 |   } while (--j >= ny); | 
 | 247 | } | 
 | 248 |  | 
 | 249 | /// lshift - This function shift x[0:len-1] left by shiftAmt bits, and  | 
 | 250 | /// store the len least significant words of the result in  | 
 | 251 | /// dest[d_offset:d_offset+len-1]. It returns the bits shifted out from  | 
 | 252 | /// the most significant digit. | 
 | 253 | static uint64_t lshift(uint64_t dest[], unsigned d_offset, | 
 | 254 |                        uint64_t x[], unsigned len, unsigned shiftAmt) { | 
 | 255 |   unsigned count = 64 - shiftAmt; | 
 | 256 |   int i = len - 1; | 
 | 257 |   uint64_t high_word = x[i], retVal = high_word >> count; | 
 | 258 |   ++d_offset; | 
 | 259 |   while (--i >= 0) { | 
 | 260 |     uint64_t low_word = x[i]; | 
 | 261 |     dest[d_offset+i] = (high_word << shiftAmt) | (low_word >> count); | 
 | 262 |     high_word = low_word; | 
 | 263 |   } | 
 | 264 |   dest[d_offset+i] = high_word << shiftAmt; | 
 | 265 |   return retVal; | 
 | 266 | } | 
 | 267 |  | 
| Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 268 | APInt::APInt(uint64_t val, unsigned numBits, bool sign) | 
 | 269 |   : bitsnum(numBits), isSigned(sign) { | 
 | 270 |   assert(bitsnum >= IntegerType::MIN_INT_BITS && "bitwidth too small"); | 
 | 271 |   assert(bitsnum <= IntegerType::MAX_INT_BITS && "bitwidth too large"); | 
 | 272 |   if (isSingleWord())  | 
 | 273 |     VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - bitsnum)); | 
 | 274 |   else { | 
 | 275 |     // Memory allocation and check if successful. | 
 | 276 |     assert((pVal = new uint64_t[numWords()]) &&  | 
 | 277 |             "APInt memory allocation fails!"); | 
 | 278 |     bzero(pVal, numWords() * 8); | 
 | 279 |     pVal[0] = val; | 
 | 280 |   } | 
 | 281 | } | 
 | 282 |  | 
 | 283 | APInt::APInt(unsigned numBits, uint64_t bigVal[], bool sign) | 
 | 284 |   : bitsnum(numBits), isSigned(sign) { | 
 | 285 |   assert(bitsnum >= IntegerType::MIN_INT_BITS && "bitwidth too small"); | 
 | 286 |   assert(bitsnum <= IntegerType::MAX_INT_BITS && "bitwidth too large"); | 
 | 287 |   assert(bigVal && "Null pointer detected!"); | 
 | 288 |   if (isSingleWord()) | 
 | 289 |     VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - bitsnum)); | 
 | 290 |   else { | 
 | 291 |     // Memory allocation and check if successful. | 
 | 292 |     assert((pVal = new uint64_t[numWords()]) &&  | 
 | 293 |            "APInt memory allocation fails!"); | 
 | 294 |     // Calculate the actual length of bigVal[]. | 
 | 295 |     unsigned n = sizeof(*bigVal) / sizeof(bigVal[0]); | 
 | 296 |     unsigned maxN = std::max<unsigned>(n, numWords()); | 
 | 297 |     unsigned minN = std::min<unsigned>(n, numWords()); | 
 | 298 |     memcpy(pVal, bigVal, (minN - 1) * 8); | 
 | 299 |     pVal[minN-1] = bigVal[minN-1] & (~uint64_t(0ULL) >> (64 - bitsnum % 64)); | 
 | 300 |     if (maxN == numWords()) | 
 | 301 |       bzero(pVal+n, (numWords() - n) * 8); | 
 | 302 |   } | 
 | 303 | } | 
 | 304 |  | 
 | 305 | APInt::APInt(std::string& Val, uint8_t radix, bool sign) | 
 | 306 |   : isSigned(sign) { | 
 | 307 |   assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && | 
 | 308 |          "Radix should be 2, 8, 10, or 16!"); | 
 | 309 |   assert(!Val.empty() && "String empty?"); | 
 | 310 |   unsigned slen = Val.size(); | 
 | 311 |   unsigned size = 0; | 
 | 312 |   // If the radix is a power of 2, read the input | 
 | 313 |   // from most significant to least significant. | 
 | 314 |   if ((radix & (radix - 1)) == 0) { | 
 | 315 |     unsigned nextBitPos = 0, bits_per_digit = radix / 8 + 2; | 
 | 316 |     uint64_t resDigit = 0; | 
 | 317 |     bitsnum = slen * bits_per_digit; | 
 | 318 |     if (numWords() > 1) | 
 | 319 |       assert((pVal = new uint64_t[numWords()]) &&  | 
 | 320 |              "APInt memory allocation fails!"); | 
 | 321 |     for (int i = slen - 1; i >= 0; --i) { | 
 | 322 |       uint64_t digit = Val[i] - 48;             // '0' == 48. | 
 | 323 |       resDigit |= digit << nextBitPos; | 
 | 324 |       nextBitPos += bits_per_digit; | 
 | 325 |       if (nextBitPos >= 64) { | 
 | 326 |         if (isSingleWord()) { | 
 | 327 |           VAL = resDigit; | 
 | 328 |            break; | 
 | 329 |         } | 
 | 330 |         pVal[size++] = resDigit; | 
 | 331 |         nextBitPos -= 64; | 
 | 332 |         resDigit = digit >> (bits_per_digit - nextBitPos); | 
 | 333 |       } | 
 | 334 |     } | 
 | 335 |     if (!isSingleWord() && size <= numWords())  | 
 | 336 |       pVal[size] = resDigit; | 
 | 337 |   } else {   // General case.  The radix is not a power of 2. | 
 | 338 |     // For 10-radix, the max value of 64-bit integer is 18446744073709551615, | 
 | 339 |     // and its digits number is 14. | 
 | 340 |     const unsigned chars_per_word = 20; | 
 | 341 |     if (slen < chars_per_word ||  | 
 | 342 |         (Val <= "18446744073709551615" &&  | 
 | 343 |          slen == chars_per_word)) { // In case Val <= 2^64 - 1 | 
 | 344 |       bitsnum = 64; | 
 | 345 |       VAL = strtoull(Val.c_str(), 0, 10); | 
 | 346 |     } else { // In case Val > 2^64 - 1 | 
 | 347 |       bitsnum = (slen / chars_per_word + 1) * 64; | 
 | 348 |       assert((pVal = new uint64_t[numWords()]) &&  | 
 | 349 |              "APInt memory allocation fails!"); | 
 | 350 |       bzero(pVal, numWords() * 8); | 
 | 351 |       unsigned str_pos = 0; | 
 | 352 |       while (str_pos < slen) { | 
 | 353 |         unsigned chunk = slen - str_pos; | 
 | 354 |         if (chunk > chars_per_word - 1) | 
 | 355 |           chunk = chars_per_word - 1; | 
 | 356 |         uint64_t resDigit = Val[str_pos++] - 48;  // 48 == '0'. | 
 | 357 |         uint64_t big_base = radix; | 
 | 358 |         while (--chunk > 0) { | 
 | 359 |           resDigit = resDigit * radix + Val[str_pos++] - 48; | 
 | 360 |           big_base *= radix; | 
 | 361 |         } | 
 | 362 |         | 
 | 363 |         uint64_t carry; | 
 | 364 |         if (!size) | 
 | 365 |           carry = resDigit; | 
 | 366 |         else { | 
 | 367 |           carry = mul_1(pVal, pVal, size, big_base); | 
 | 368 |           carry += add_1(pVal, pVal, size, resDigit); | 
 | 369 |         } | 
 | 370 |          | 
 | 371 |         if (carry) pVal[size++] = carry; | 
 | 372 |       } | 
 | 373 |     } | 
 | 374 |   } | 
 | 375 | } | 
 | 376 |  | 
 | 377 | APInt::APInt(const APInt& APIVal) | 
 | 378 |   : bitsnum(APIVal.bitsnum), isSigned(APIVal.isSigned) { | 
 | 379 |   if (isSingleWord()) VAL = APIVal.VAL; | 
 | 380 |   else { | 
 | 381 |     // Memory allocation and check if successful. | 
 | 382 |     assert((pVal = new uint64_t[numWords()]) &&  | 
 | 383 |            "APInt memory allocation fails!"); | 
 | 384 |     memcpy(pVal, APIVal.pVal, numWords() * 8); | 
 | 385 |   } | 
 | 386 | } | 
 | 387 |  | 
 | 388 | APInt::~APInt() { | 
 | 389 |   if (!isSingleWord() && pVal) delete[] pVal; | 
 | 390 | } | 
 | 391 |  | 
 | 392 | /// whichByte - This function returns the word position  | 
 | 393 | /// for the specified bit position. | 
 | 394 | inline unsigned APInt::whichByte(unsigned bitPosition) | 
 | 395 | { return (bitPosition % APINT_BITS_PER_WORD) / 8; } | 
 | 396 |  | 
| Zhou Sheng | fd43dcf | 2007-02-06 03:00:16 +0000 | [diff] [blame] | 397 | /// @brief Copy assignment operator. Create a new object from the given | 
 | 398 | /// APInt one by initialization. | 
 | 399 | APInt& APInt::operator=(const APInt& RHS) { | 
 | 400 |   if (isSingleWord()) VAL = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 401 |   else { | 
 | 402 |     unsigned minN = std::min(numWords(), RHS.numWords()); | 
 | 403 |     memcpy(pVal, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, minN * 8); | 
 | 404 |     if (numWords() != minN) | 
 | 405 |       bzero(pVal + minN, (numWords() - minN) * 8); | 
 | 406 |   } | 
 | 407 |   return *this; | 
 | 408 | } | 
 | 409 |  | 
 | 410 | /// @brief Assignment operator. Assigns a common case integer value to  | 
 | 411 | /// the APInt. | 
 | 412 | APInt& APInt::operator=(uint64_t RHS) { | 
 | 413 |   if (isSingleWord()) VAL = RHS; | 
 | 414 |   else { | 
 | 415 |     pVal[0] = RHS; | 
 | 416 |     bzero(pVal, (numWords() - 1) * 8); | 
 | 417 |   } | 
 | 418 |   return *this; | 
 | 419 | } | 
 | 420 |  | 
 | 421 | /// @brief Postfix increment operator. Increments the APInt by one. | 
 | 422 | const APInt APInt::operator++(int) { | 
 | 423 |   APInt API(*this); | 
 | 424 |   if (isSingleWord()) ++VAL; | 
 | 425 |   else | 
 | 426 |     add_1(pVal, pVal, numWords(), 1); | 
 | 427 |   API.TruncToBits(); | 
 | 428 |   return API; | 
 | 429 | } | 
 | 430 |  | 
 | 431 | /// @brief Prefix increment operator. Increments the APInt by one. | 
 | 432 | APInt& APInt::operator++() { | 
 | 433 |   if (isSingleWord()) ++VAL; | 
 | 434 |   else | 
 | 435 |     add_1(pVal, pVal, numWords(), 1); | 
 | 436 |   TruncToBits(); | 
 | 437 |   return *this; | 
 | 438 | } | 
 | 439 |  | 
 | 440 | /// @brief Postfix decrement operator. Decrements the APInt by one. | 
 | 441 | const APInt APInt::operator--(int) { | 
 | 442 |   APInt API(*this); | 
 | 443 |   if (isSingleWord()) --VAL; | 
 | 444 |   else | 
 | 445 |     sub_1(API.pVal, API.numWords(), 1); | 
 | 446 |   API.TruncToBits(); | 
 | 447 |   return API; | 
 | 448 | } | 
 | 449 |  | 
 | 450 | /// @brief Prefix decrement operator. Decrements the APInt by one. | 
 | 451 | APInt& APInt::operator--() { | 
 | 452 |   if (isSingleWord()) --VAL; | 
 | 453 |   else | 
 | 454 |     sub_1(pVal, numWords(), 1); | 
 | 455 |   TruncToBits(); | 
 | 456 |   return *this; | 
 | 457 | } | 
 | 458 |  | 
 | 459 | /// @brief Addition assignment operator. Adds this APInt by the given APInt& | 
 | 460 | /// RHS and assigns the result to this APInt. | 
 | 461 | APInt& APInt::operator+=(const APInt& RHS) { | 
 | 462 |   if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 463 |   else { | 
 | 464 |     if (RHS.isSingleWord()) add_1(pVal, pVal, numWords(), RHS.VAL); | 
 | 465 |     else { | 
 | 466 |       if (numWords() <= RHS.numWords())  | 
 | 467 |         add(pVal, pVal, RHS.pVal, numWords()); | 
 | 468 |       else { | 
 | 469 |         uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.numWords()); | 
 | 470 |         add_1(pVal + RHS.numWords(), pVal + RHS.numWords(),  | 
 | 471 |               numWords() - RHS.numWords(), carry); | 
 | 472 |       } | 
 | 473 |     } | 
 | 474 |   } | 
 | 475 |   TruncToBits(); | 
 | 476 |   return *this; | 
 | 477 | } | 
 | 478 |  | 
 | 479 | /// @brief Subtraction assignment operator. Subtracts this APInt by the given | 
 | 480 | /// APInt &RHS and assigns the result to this APInt. | 
 | 481 | APInt& APInt::operator-=(const APInt& RHS) { | 
 | 482 |   if (isSingleWord())  | 
 | 483 |     VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 484 |   else { | 
 | 485 |     if (RHS.isSingleWord()) | 
 | 486 |       sub_1(pVal, numWords(), RHS.VAL); | 
 | 487 |     else { | 
 | 488 |       if (RHS.numWords() < numWords()) {  | 
 | 489 |         uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.numWords()); | 
 | 490 |         sub_1(pVal + RHS.numWords(), numWords() - RHS.numWords(), carry);  | 
 | 491 |       } | 
 | 492 |       else | 
 | 493 |         sub(pVal, pVal, RHS.pVal, numWords()); | 
 | 494 |     } | 
 | 495 |   } | 
 | 496 |   TruncToBits(); | 
 | 497 |   return *this; | 
 | 498 | } | 
 | 499 |  | 
 | 500 | /// @brief Multiplication assignment operator. Multiplies this APInt by the  | 
 | 501 | /// given APInt& RHS and assigns the result to this APInt. | 
 | 502 | APInt& APInt::operator*=(const APInt& RHS) { | 
 | 503 |   if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 504 |   else { | 
 | 505 |     // one-based first non-zero bit position. | 
 | 506 |     unsigned first = numWords() * APINT_BITS_PER_WORD - CountLeadingZeros(); | 
 | 507 |     unsigned xlen = !first ? 0 : whichWord(first - 1) + 1; | 
 | 508 |     if (!xlen)  | 
 | 509 |       return *this; | 
 | 510 |     else if (RHS.isSingleWord())  | 
 | 511 |       mul_1(pVal, pVal, xlen, RHS.VAL); | 
 | 512 |     else { | 
 | 513 |       first = RHS.numWords() * APINT_BITS_PER_WORD - RHS.CountLeadingZeros(); | 
 | 514 |       unsigned ylen = !first ? 0 : whichWord(first - 1) + 1; | 
 | 515 |       if (!ylen) { | 
 | 516 |         bzero(pVal, numWords() * 8); | 
 | 517 |         return *this; | 
 | 518 |       } | 
 | 519 |       uint64_t *dest = new uint64_t[xlen+ylen]; | 
 | 520 |       assert(dest && "Memory Allocation Failed!"); | 
 | 521 |       mul(dest, pVal, xlen, RHS.pVal, ylen); | 
 | 522 |       memcpy(pVal, dest, ((xlen + ylen >= numWords()) ? numWords() : xlen + ylen) * 8); | 
 | 523 |       delete[] dest; | 
 | 524 |     } | 
 | 525 |   } | 
 | 526 |   TruncToBits(); | 
 | 527 |   return *this; | 
 | 528 | } | 
 | 529 |  | 
 | 530 | /// @brief Division assignment operator. Divides this APInt by the given APInt | 
 | 531 | /// &RHS and assigns the result to this APInt. | 
 | 532 | APInt& APInt::operator/=(const APInt& RHS) { | 
 | 533 |   unsigned first = RHS.numWords() * APINT_BITS_PER_WORD -  | 
 | 534 |                    RHS.CountLeadingZeros(); | 
 | 535 |   unsigned ylen = !first ? 0 : whichWord(first - 1) + 1; | 
 | 536 |   assert(ylen && "Divided by zero???"); | 
 | 537 |   if (isSingleWord()) { | 
 | 538 |     if (isSigned && RHS.isSigned) | 
 | 539 |       VAL = RHS.isSingleWord() ? (int64_t(VAL) / int64_t(RHS.VAL)) : | 
 | 540 |             (ylen > 1 ? 0 : int64_t(VAL) / int64_t(RHS.pVal[0])); | 
 | 541 |     else | 
 | 542 |       VAL = RHS.isSingleWord() ? (VAL / RHS.VAL) :  | 
 | 543 |           (ylen > 1 ? 0 : VAL / RHS.pVal[0]); | 
 | 544 |   } else { | 
 | 545 |     unsigned first2 = numWords() * APINT_BITS_PER_WORD - CountLeadingZeros(); | 
 | 546 |     unsigned xlen = !first2 ? 0 : whichWord(first2 - 1) + 1; | 
 | 547 |     if (!xlen) | 
 | 548 |       return *this; | 
 | 549 |     else if ((*this) < RHS) | 
 | 550 |       bzero(pVal, numWords() * 8); | 
 | 551 |     else if ((*this) == RHS) { | 
 | 552 |       bzero(pVal, numWords() * 8); | 
 | 553 |       pVal[0] = 1; | 
 | 554 |     } else if (xlen == 1) | 
 | 555 |       pVal[0] /= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 556 |     else { | 
 | 557 |       uint64_t *xwords = new uint64_t[xlen+1], *ywords = new uint64_t[ylen]; | 
 | 558 |       assert(xwords && ywords && "Memory Allocation Failed!"); | 
 | 559 |       memcpy(xwords, pVal, xlen * 8); | 
 | 560 |       xwords[xlen] = 0; | 
 | 561 |       memcpy(ywords, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, ylen * 8); | 
 | 562 |       if (unsigned nshift = 63 - (first - 1) % 64) { | 
 | 563 |         lshift(ywords, 0, ywords, ylen, nshift); | 
 | 564 |         unsigned xlentmp = xlen; | 
 | 565 |         xwords[xlen++] = lshift(xwords, 0, xwords, xlentmp, nshift); | 
 | 566 |       } | 
 | 567 |       div((unsigned*)xwords, xlen*2-1, (unsigned*)ywords, ylen*2); | 
 | 568 |       bzero(pVal, numWords() * 8); | 
 | 569 |       memcpy(pVal, xwords + ylen, (xlen - ylen) * 8); | 
 | 570 |       delete[] xwords; | 
 | 571 |       delete[] ywords; | 
 | 572 |     } | 
 | 573 |   } | 
 | 574 |   return *this; | 
 | 575 | } | 
 | 576 |  | 
 | 577 | /// @brief Remainder assignment operator. Yields the remainder from the  | 
 | 578 | /// division of this APInt by the given APInt& RHS and assigns the remainder  | 
 | 579 | /// to this APInt. | 
 | 580 | APInt& APInt::operator%=(const APInt& RHS) { | 
 | 581 |   unsigned first = RHS.numWords() * APINT_BITS_PER_WORD - | 
 | 582 |                    RHS.CountLeadingZeros(); | 
 | 583 |   unsigned ylen = !first ? 0 : whichWord(first - 1) + 1; | 
 | 584 |   assert(ylen && "Performing remainder operation by zero ???"); | 
 | 585 |   if (isSingleWord()) { | 
 | 586 |     if (isSigned && RHS.isSigned) | 
 | 587 |       VAL = RHS.isSingleWord() ? (int64_t(VAL) % int64_t(RHS.VAL)) : | 
 | 588 |             (ylen > 1 ? VAL : int64_t(VAL) % int64_t(RHS.pVal[0])); | 
 | 589 |     else | 
 | 590 |       VAL = RHS.isSingleWord() ? (VAL % RHS.VAL) :  | 
 | 591 |           (ylen > 1 ? VAL : VAL % RHS.pVal[0]); | 
 | 592 |   } else { | 
 | 593 |     unsigned first2 = numWords() * APINT_BITS_PER_WORD - CountLeadingZeros(); | 
 | 594 |     unsigned xlen = !first2 ? 0 : whichWord(first2 - 1) + 1; | 
 | 595 |     if (!xlen || (*this) < RHS) | 
 | 596 |       return *this; | 
 | 597 |     else if ((*this) == RHS)  | 
 | 598 |       bzero(pVal, numWords() * 8); | 
 | 599 |     else if (xlen == 1)  | 
 | 600 |       pVal[0] %= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]; | 
 | 601 |     else { | 
 | 602 |       uint64_t *xwords = new uint64_t[xlen+1], *ywords = new uint64_t[ylen]; | 
 | 603 |       assert(xwords && ywords && "Memory Allocation Failed!"); | 
 | 604 |       memcpy(xwords, pVal, xlen * 8); | 
 | 605 |       xwords[xlen] = 0; | 
 | 606 |       memcpy(ywords, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, ylen * 8); | 
 | 607 |       unsigned nshift = 63 - (first - 1) % 64; | 
 | 608 |       if (nshift) { | 
 | 609 |         lshift(ywords, 0, ywords, ylen, nshift); | 
 | 610 |         unsigned xlentmp = xlen; | 
 | 611 |         xwords[xlen++] = lshift(xwords, 0, xwords, xlentmp, nshift); | 
 | 612 |       } | 
 | 613 |       div((unsigned*)xwords, xlen*2-1, (unsigned*)ywords, ylen*2); | 
 | 614 |       bzero(pVal, numWords() * 8); | 
 | 615 |       for (unsigned i = 0; i < ylen-1; ++i) | 
 | 616 |         pVal[i] = (xwords[i] >> nshift) | (xwords[i+1] << (64 - nshift)); | 
 | 617 |       pVal[ylen-1] = xwords[ylen-1] >> nshift; | 
 | 618 |       delete[] xwords; | 
 | 619 |       delete[] ywords; | 
 | 620 |     } | 
 | 621 |   } | 
 | 622 |   return *this; | 
 | 623 | } | 
 | 624 |  | 
 | 625 | /// @brief Bitwise AND assignment operator. Performs bitwise AND operation on | 
 | 626 | /// this APInt and the given APInt& RHS, assigns the result to this APInt. | 
 | 627 | APInt& APInt::operator&=(const APInt& RHS) { | 
 | 628 |   if (isSingleWord()) { | 
 | 629 |     if (RHS.isSingleWord()) VAL &= RHS.VAL; | 
 | 630 |     else VAL &= RHS.pVal[0]; | 
 | 631 |   } else { | 
 | 632 |     if (RHS.isSingleWord()) { | 
 | 633 |       bzero(pVal, (numWords() - 1) * 8); | 
 | 634 |       pVal[0] &= RHS.VAL; | 
 | 635 |     } else { | 
 | 636 |       unsigned minwords = numWords() < RHS.numWords() ? numWords() : RHS.numWords(); | 
 | 637 |       for (unsigned i = 0; i < minwords; ++i) | 
 | 638 |         pVal[i] &= RHS.pVal[i]; | 
 | 639 |       if (numWords() > minwords) bzero(pVal+minwords, (numWords() - minwords) * 8); | 
 | 640 |     } | 
 | 641 |   } | 
 | 642 |   return *this; | 
 | 643 | } | 
 | 644 |  | 
 | 645 | /// @brief Bitwise OR assignment operator. Performs bitwise OR operation on  | 
 | 646 | /// this APInt and the given APInt& RHS, assigns the result to this APInt. | 
 | 647 | APInt& APInt::operator|=(const APInt& RHS) { | 
 | 648 |   if (isSingleWord()) { | 
 | 649 |     if (RHS.isSingleWord()) VAL |= RHS.VAL; | 
 | 650 |     else VAL |= RHS.pVal[0]; | 
 | 651 |   } else { | 
 | 652 |     if (RHS.isSingleWord()) { | 
 | 653 |       pVal[0] |= RHS.VAL; | 
 | 654 |     } else { | 
 | 655 |       unsigned minwords = numWords() < RHS.numWords() ? numWords() : RHS.numWords(); | 
 | 656 |       for (unsigned i = 0; i < minwords; ++i) | 
 | 657 |         pVal[i] |= RHS.pVal[i]; | 
 | 658 |     } | 
 | 659 |   } | 
 | 660 |   TruncToBits(); | 
 | 661 |   return *this; | 
 | 662 | } | 
 | 663 |  | 
 | 664 | /// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on | 
 | 665 | /// this APInt and the given APInt& RHS, assigns the result to this APInt. | 
 | 666 | APInt& APInt::operator^=(const APInt& RHS) { | 
 | 667 |   if (isSingleWord()) { | 
 | 668 |     if (RHS.isSingleWord()) VAL ^= RHS.VAL; | 
 | 669 |     else VAL ^= RHS.pVal[0]; | 
 | 670 |   } else { | 
 | 671 |     if (RHS.isSingleWord()) { | 
 | 672 |       for (unsigned i = 0; i < numWords(); ++i) | 
 | 673 |         pVal[i] ^= RHS.VAL; | 
 | 674 |     } else { | 
 | 675 |       unsigned minwords = numWords() < RHS.numWords() ? numWords() : RHS.numWords(); | 
 | 676 |       for (unsigned i = 0; i < minwords; ++i) | 
 | 677 |         pVal[i] ^= RHS.pVal[i]; | 
 | 678 |       if (numWords() > minwords) | 
 | 679 |         for (unsigned i = minwords; i < numWords(); ++i) | 
 | 680 |           pVal[i] ^= 0; | 
 | 681 |     } | 
 | 682 |   } | 
 | 683 |   TruncToBits(); | 
 | 684 |   return *this; | 
 | 685 | } | 
 | 686 |  | 
 | 687 | /// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt | 
 | 688 | /// and the given APInt& RHS. | 
 | 689 | APInt APInt::operator&(const APInt& RHS) const { | 
 | 690 |   APInt API(RHS); | 
 | 691 |   return API &= *this; | 
 | 692 | } | 
 | 693 |  | 
 | 694 | /// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt  | 
 | 695 | /// and the given APInt& RHS. | 
 | 696 | APInt APInt::operator|(const APInt& RHS) const { | 
 | 697 |   APInt API(RHS); | 
 | 698 |   API |= *this; | 
 | 699 |   API.TruncToBits(); | 
 | 700 |   return API; | 
 | 701 | } | 
 | 702 |  | 
 | 703 | /// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt | 
 | 704 | /// and the given APInt& RHS. | 
 | 705 | APInt APInt::operator^(const APInt& RHS) const { | 
 | 706 |   APInt API(RHS); | 
 | 707 |   API ^= *this; | 
 | 708 |   API.TruncToBits(); | 
 | 709 |   return API; | 
 | 710 | } | 
 | 711 |  | 
 | 712 | /// @brief Logical AND operator. Performs logical AND operation on this APInt | 
 | 713 | /// and the given APInt& RHS. | 
 | 714 | bool APInt::operator&&(const APInt& RHS) const { | 
 | 715 |   if (isSingleWord())  | 
 | 716 |     return RHS.isSingleWord() ? VAL && RHS.VAL : VAL && RHS.pVal[0]; | 
 | 717 |   else if (RHS.isSingleWord()) | 
 | 718 |     return RHS.VAL && pVal[0]; | 
 | 719 |   else { | 
 | 720 |     unsigned minN = std::min(numWords(), RHS.numWords()); | 
 | 721 |     for (unsigned i = 0; i < minN; ++i) | 
 | 722 |       if (pVal[i] && RHS.pVal[i]) | 
 | 723 |         return true; | 
 | 724 |   } | 
 | 725 |   return false; | 
 | 726 | } | 
 | 727 |  | 
 | 728 | /// @brief Logical OR operator. Performs logical OR operation on this APInt  | 
 | 729 | /// and the given APInt& RHS. | 
 | 730 | bool APInt::operator||(const APInt& RHS) const { | 
 | 731 |   if (isSingleWord())  | 
 | 732 |     return RHS.isSingleWord() ? VAL || RHS.VAL : VAL || RHS.pVal[0]; | 
 | 733 |   else if (RHS.isSingleWord()) | 
 | 734 |     return RHS.VAL || pVal[0]; | 
 | 735 |   else { | 
 | 736 |     unsigned minN = std::min(numWords(), RHS.numWords()); | 
 | 737 |     for (unsigned i = 0; i < minN; ++i) | 
 | 738 |       if (pVal[i] || RHS.pVal[i]) | 
 | 739 |         return true; | 
 | 740 |   } | 
 | 741 |   return false; | 
 | 742 | } | 
 | 743 |  | 
 | 744 | /// @brief Logical negation operator. Performs logical negation operation on | 
 | 745 | /// this APInt. | 
 | 746 | bool APInt::operator !() const { | 
 | 747 |   if (isSingleWord()) | 
 | 748 |     return !VAL; | 
 | 749 |   else | 
 | 750 |     for (unsigned i = 0; i < numWords(); ++i) | 
 | 751 |        if (pVal[i])  | 
 | 752 |          return false; | 
 | 753 |   return true; | 
 | 754 | } | 
 | 755 |  | 
 | 756 | /// @brief Multiplication operator. Multiplies this APInt by the given APInt&  | 
 | 757 | /// RHS. | 
 | 758 | APInt APInt::operator*(const APInt& RHS) const { | 
 | 759 |   APInt API(RHS); | 
 | 760 |   API *= *this; | 
 | 761 |   API.TruncToBits(); | 
 | 762 |   return API; | 
 | 763 | } | 
 | 764 |  | 
 | 765 | /// @brief Division operator. Divides this APInt by the given APInt& RHS. | 
 | 766 | APInt APInt::operator/(const APInt& RHS) const { | 
 | 767 |   APInt API(*this); | 
 | 768 |   return API /= RHS; | 
 | 769 | } | 
 | 770 |  | 
 | 771 | /// @brief Remainder operator. Yields the remainder from the division of this | 
 | 772 | /// APInt and the given APInt& RHS. | 
 | 773 | APInt APInt::operator%(const APInt& RHS) const { | 
 | 774 |   APInt API(*this); | 
 | 775 |   return API %= RHS; | 
 | 776 | } | 
 | 777 |  | 
 | 778 | /// @brief Addition operator. Adds this APInt by the given APInt& RHS. | 
 | 779 | APInt APInt::operator+(const APInt& RHS) const { | 
 | 780 |   APInt API(*this); | 
 | 781 |   API += RHS; | 
 | 782 |   API.TruncToBits(); | 
 | 783 |   return API; | 
 | 784 | } | 
 | 785 |  | 
 | 786 | /// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS | 
 | 787 | APInt APInt::operator-(const APInt& RHS) const { | 
 | 788 |   APInt API(*this); | 
 | 789 |   API -= RHS; | 
 | 790 |   API.TruncToBits(); | 
 | 791 |   return API; | 
 | 792 | } | 
 | 793 |  | 
 | 794 | /// @brief Array-indexing support. | 
 | 795 | bool APInt::operator[](unsigned bitPosition) const { | 
 | 796 |   return maskBit(bitPosition) & (isSingleWord() ?  | 
 | 797 |          VAL : pVal[whichWord(bitPosition)]) != 0; | 
 | 798 | } | 
 | 799 |  | 
 | 800 | /// @brief Equality operator. Compare this APInt with the given APInt& RHS  | 
 | 801 | /// for the validity of the equality relationship. | 
 | 802 | bool APInt::operator==(const APInt& RHS) const { | 
 | 803 |   unsigned n1 = numWords() * APINT_BITS_PER_WORD - CountLeadingZeros(),  | 
 | 804 |            n2 = RHS.numWords() * APINT_BITS_PER_WORD - RHS.CountLeadingZeros(); | 
 | 805 |   if (n1 != n2) return false; | 
 | 806 |   else if (isSingleWord())  | 
 | 807 |     return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); | 
 | 808 |   else { | 
 | 809 |     if (n1 <= 64) | 
 | 810 |       return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); | 
 | 811 |     for (int i = whichWord(n1 - 1); i >= 0; --i) | 
 | 812 |       if (pVal[i] != RHS.pVal[i]) return false; | 
 | 813 |   } | 
 | 814 |   return true; | 
 | 815 | } | 
 | 816 |  | 
 | 817 | /// @brief Inequality operator. Compare this APInt with the given APInt& RHS | 
 | 818 | /// for the validity of the inequality relationship. | 
 | 819 | bool APInt::operator!=(const APInt& RHS) const { | 
 | 820 |   return !((*this) == RHS); | 
 | 821 | } | 
 | 822 |  | 
 | 823 | /// @brief Less-than operator. Compare this APInt with the given APInt& RHS | 
 | 824 | /// for the validity of the less-than relationship. | 
 | 825 | bool APInt::operator <(const APInt& RHS) const { | 
 | 826 |   if (isSigned && RHS.isSigned) { | 
 | 827 |     if ((*this)[bitsnum-1] > RHS[RHS.bitsnum-1]) | 
 | 828 |       return false; | 
 | 829 |     else if ((*this)[bitsnum-1] < RHS[RHS.bitsnum-1]) | 
 | 830 |       return true; | 
 | 831 |   } | 
 | 832 |   unsigned n1 = numWords() * 64 - CountLeadingZeros(),  | 
 | 833 |            n2 = RHS.numWords() * 64 - RHS.CountLeadingZeros(); | 
 | 834 |   if (n1 < n2) return true; | 
 | 835 |   else if (n1 > n2) return false; | 
 | 836 |   else if (isSingleWord()) | 
 | 837 |     return VAL < (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); | 
 | 838 |   else { | 
 | 839 |     if (n1 <= 64) | 
 | 840 |       return pVal[0] < (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]); | 
 | 841 |     for (int i = whichWord(n1 - 1); i >= 0; --i) { | 
 | 842 |       if (pVal[i] > RHS.pVal[i]) return false; | 
 | 843 |       else if (pVal[i] < RHS.pVal[i]) return true; | 
 | 844 |     } | 
 | 845 |   } | 
 | 846 |   return false; | 
 | 847 | } | 
 | 848 |  | 
 | 849 | /// @brief Less-than-or-equal operator. Compare this APInt with the given  | 
 | 850 | /// APInt& RHS for the validity of the less-than-or-equal relationship. | 
 | 851 | bool APInt::operator<=(const APInt& RHS) const { | 
 | 852 |   return (*this) == RHS || (*this) < RHS; | 
 | 853 | } | 
 | 854 |  | 
 | 855 | /// @brief Greater-than operator. Compare this APInt with the given APInt& RHS | 
 | 856 | /// for the validity of the greater-than relationship. | 
 | 857 | bool APInt::operator >(const APInt& RHS) const { | 
 | 858 |   return !((*this) <= RHS); | 
 | 859 | } | 
 | 860 |  | 
 | 861 | /// @brief Greater-than-or-equal operator. Compare this APInt with the given  | 
 | 862 | /// APInt& RHS for the validity of the greater-than-or-equal relationship. | 
 | 863 | bool APInt::operator>=(const APInt& RHS) const { | 
 | 864 |   return !((*this) < RHS); | 
 | 865 | }   | 
 | 866 |  | 
 | 867 | /// Set the given bit to 1 whose poition is given as "bitPosition". | 
 | 868 | /// @brief Set a given bit to 1. | 
 | 869 | APInt& APInt::set(unsigned bitPosition) { | 
 | 870 |   if (isSingleWord()) VAL |= maskBit(bitPosition); | 
 | 871 |   else pVal[whichWord(bitPosition)] |= maskBit(bitPosition); | 
 | 872 |   return *this; | 
 | 873 | } | 
 | 874 |  | 
 | 875 | /// @brief Set every bit to 1. | 
 | 876 | APInt& APInt::set() { | 
 | 877 |   if (isSingleWord()) VAL = -1ULL; | 
 | 878 |   else | 
 | 879 |     for (unsigned i = 0; i < numWords(); ++i) | 
 | 880 |       pVal[i] = -1ULL; | 
 | 881 |   return *this; | 
 | 882 | } | 
 | 883 |  | 
 | 884 | /// Set the given bit to 0 whose position is given as "bitPosition". | 
 | 885 | /// @brief Set a given bit to 0. | 
 | 886 | APInt& APInt::clear(unsigned bitPosition) { | 
 | 887 |   if (isSingleWord()) VAL &= ~maskBit(bitPosition); | 
 | 888 |   else pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition); | 
 | 889 |   return *this; | 
 | 890 | } | 
 | 891 |  | 
 | 892 | /// @brief Set every bit to 0. | 
 | 893 | APInt& APInt::clear() { | 
 | 894 |   if (isSingleWord()) VAL = 0; | 
 | 895 |   else bzero(pVal, numWords() * 8); | 
 | 896 |   return *this; | 
 | 897 | } | 
 | 898 |  | 
 | 899 | /// @brief Left-shift assignment operator. Left-shift the APInt by shiftAmt | 
 | 900 | /// and assigns the result to this APInt. | 
 | 901 | APInt& APInt::operator<<=(unsigned shiftAmt) { | 
 | 902 |   if (shiftAmt >= bitsnum) { | 
 | 903 |     if (isSingleWord()) VAL = 0; | 
 | 904 |     else bzero(pVal, numWords() * 8); | 
 | 905 |   } else { | 
 | 906 |     for (unsigned i = 0; i < shiftAmt; ++i) clear(i); | 
 | 907 |     for (unsigned i = shiftAmt; i < bitsnum; ++i) { | 
 | 908 |       if ((*this)[i-shiftAmt]) set(i); | 
 | 909 |       else clear(i); | 
 | 910 |     } | 
 | 911 |   } | 
 | 912 |   return *this; | 
 | 913 | } | 
 | 914 |  | 
 | 915 | /// @brief Left-shift operator. Left-shift the APInt by shiftAmt. | 
 | 916 | APInt APInt::operator<<(unsigned shiftAmt) const { | 
 | 917 |   APInt API(*this); | 
 | 918 |   API <<= shiftAmt; | 
 | 919 |   return API; | 
 | 920 | } | 
 | 921 |  | 
 | 922 | /// @brief Right-shift assignment operator. Right-shift the APInt by shiftAmt | 
 | 923 | /// and assigns the result to this APInt. | 
 | 924 | APInt& APInt::operator>>=(unsigned shiftAmt) { | 
 | 925 |   bool isAShr = isSigned && (*this)[bitsnum-1]; | 
 | 926 |   if (isSingleWord()) | 
 | 927 |     VAL = isAShr ? (int64_t(VAL) >> shiftAmt) : (VAL >> shiftAmt); | 
 | 928 |   else { | 
 | 929 |     unsigned i = 0; | 
 | 930 |     for (i = 0; i < bitsnum - shiftAmt; ++i) | 
 | 931 |       if ((*this)[i+shiftAmt]) set(i); | 
 | 932 |       else clear(i); | 
 | 933 |     for (; i < bitsnum; ++i) | 
 | 934 |       isAShr ? set(i) : clear(i); | 
 | 935 |   } | 
 | 936 |   return *this; | 
 | 937 | } | 
 | 938 |  | 
 | 939 | /// @brief Right-shift operator. Right-shift the APInt by shiftAmt. | 
 | 940 | APInt APInt::operator>>(unsigned shiftAmt) const { | 
 | 941 |   APInt API(*this); | 
 | 942 |   API >>= shiftAmt; | 
 | 943 |   return API; | 
 | 944 | } | 
 | 945 |  | 
 | 946 | /// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on | 
 | 947 | /// this APInt. | 
 | 948 | APInt APInt::operator~() const { | 
 | 949 |   APInt API(*this); | 
 | 950 |   API.flip(); | 
 | 951 |   return API; | 
 | 952 | } | 
 | 953 |  | 
 | 954 | /// @brief Toggle every bit to its opposite value. | 
 | 955 | APInt& APInt::flip() { | 
 | 956 |   if (isSingleWord()) VAL = (~(VAL << (64 - bitsnum))) >> (64 - bitsnum); | 
 | 957 |   else { | 
 | 958 |     unsigned i = 0; | 
 | 959 |     for (; i < numWords() - 1; ++i) | 
 | 960 |       pVal[i] = ~pVal[i]; | 
 | 961 |     unsigned offset = 64 - (bitsnum - 64 * (i - 1)); | 
 | 962 |     pVal[i] = (~(pVal[i] << offset)) >> offset; | 
 | 963 |   } | 
 | 964 |   return *this; | 
 | 965 | } | 
 | 966 |  | 
 | 967 | /// Toggle a given bit to its opposite value whose position is given  | 
 | 968 | /// as "bitPosition". | 
 | 969 | /// @brief Toggles a given bit to its opposite value. | 
 | 970 | APInt& APInt::flip(unsigned bitPosition) { | 
 | 971 |   assert(bitPosition < bitsnum && "Out of the bit-width range!"); | 
 | 972 |   if ((*this)[bitPosition]) clear(bitPosition); | 
 | 973 |   else set(bitPosition); | 
 | 974 |   return *this; | 
 | 975 | } | 
 | 976 |  | 
 | 977 | /// to_string - This function translates the APInt into a string. | 
 | 978 | std::string APInt::to_string(uint8_t radix) const { | 
 | 979 |   assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) && | 
 | 980 |          "Radix should be 2, 8, 10, or 16!"); | 
 | 981 |   std::ostringstream buf; | 
 | 982 |   buf << std::setbase(radix); | 
 | 983 |   // If the radix is a power of 2, set the format of ostringstream, | 
 | 984 |   // and output the value into buf. | 
 | 985 |   if ((radix & (radix - 1)) == 0) { | 
 | 986 |     if (isSingleWord()) buf << VAL; | 
 | 987 |     else { | 
 | 988 |       buf << pVal[numWords()-1]; | 
 | 989 |       buf << std::setw(64 / (radix / 8 + 2)) << std::setfill('0'); | 
 | 990 |       for (int i = numWords() - 2; i >= 0; --i) | 
 | 991 |         buf << pVal[i]; | 
 | 992 |     } | 
 | 993 |   } | 
 | 994 |   else {  // If the radix = 10, need to translate the value into a | 
 | 995 |           // string. | 
 | 996 |     if (isSingleWord()) buf << VAL; | 
 | 997 |     else { | 
 | 998 |       // FIXME: To be supported. | 
 | 999 |     } | 
 | 1000 |   } | 
 | 1001 |   return buf.str(); | 
 | 1002 | } | 
 | 1003 |  | 
 | 1004 | /// getMaxValue - This function returns the largest value | 
 | 1005 | /// for an APInt of the specified bit-width and if isSign == true, | 
 | 1006 | /// it should be largest signed value, otherwise unsigned value. | 
 | 1007 | APInt APInt::getMaxValue(unsigned numBits, bool isSign) { | 
 | 1008 |   APInt APIVal(numBits, 1); | 
 | 1009 |   APIVal.set(); | 
 | 1010 |   return isSign ? APIVal.clear(numBits) : APIVal; | 
 | 1011 | } | 
 | 1012 |  | 
 | 1013 | /// getMinValue - This function returns the smallest value for | 
 | 1014 | /// an APInt of the given bit-width and if isSign == true, | 
 | 1015 | /// it should be smallest signed value, otherwise zero. | 
 | 1016 | APInt APInt::getMinValue(unsigned numBits, bool isSign) { | 
 | 1017 |   APInt APIVal(0, numBits); | 
 | 1018 |   return isSign ? APIVal : APIVal.set(numBits); | 
 | 1019 | } | 
 | 1020 |  | 
 | 1021 | /// getAllOnesValue - This function returns an all-ones value for | 
 | 1022 | /// an APInt of the specified bit-width. | 
 | 1023 | APInt APInt::getAllOnesValue(unsigned numBits) { | 
 | 1024 |   return getMaxValue(numBits, false); | 
 | 1025 | } | 
 | 1026 |  | 
 | 1027 | /// getNullValue - This function creates an '0' value for an | 
 | 1028 | /// APInt of the specified bit-width. | 
 | 1029 | APInt APInt::getNullValue(unsigned numBits) { | 
 | 1030 |   return getMinValue(numBits, true); | 
 | 1031 | } | 
 | 1032 |  | 
 | 1033 | /// HiBits - This function returns the high "numBits" bits of this APInt. | 
 | 1034 | APInt APInt::HiBits(unsigned numBits) const { | 
 | 1035 |   return (*this) >> (bitsnum - numBits);  | 
 | 1036 | } | 
 | 1037 |  | 
 | 1038 | /// LoBits - This function returns the low "numBits" bits of this APInt. | 
 | 1039 | APInt APInt::LoBits(unsigned numBits) const { | 
 | 1040 |   return ((*this) << (bitsnum - numBits)) >> (bitsnum - numBits); | 
 | 1041 | } | 
 | 1042 |  | 
 | 1043 | /// CountLeadingZeros - This function is a APInt version corresponding to  | 
 | 1044 | /// llvm/include/llvm/Support/MathExtras.h's function  | 
 | 1045 | /// CountLeadingZeros_{32, 64}. It performs platform optimal form of counting  | 
 | 1046 | /// the number of zeros from the most significant bit to the first one bit. | 
 | 1047 | /// @returns numWord() * 64 if the value is zero. | 
 | 1048 | unsigned APInt::CountLeadingZeros() const { | 
 | 1049 |   if (isSingleWord()) | 
 | 1050 |     return CountLeadingZeros_64(VAL); | 
 | 1051 |   unsigned Count = 0; | 
 | 1052 |   for (int i = numWords() - 1; i >= 0; --i) { | 
 | 1053 |     unsigned tmp = CountLeadingZeros_64(pVal[i]); | 
 | 1054 |     Count += tmp; | 
 | 1055 |     if (tmp != 64) | 
 | 1056 |       break; | 
 | 1057 |   } | 
 | 1058 |   return Count; | 
 | 1059 | } | 
 | 1060 |  | 
 | 1061 | /// CountTrailingZero - This function is a APInt version corresponding to | 
 | 1062 | /// llvm/include/llvm/Support/MathExtras.h's function  | 
 | 1063 | /// CountTrailingZeros_{32, 64}. It performs platform optimal form of counting  | 
 | 1064 | /// the number of zeros from the least significant bit to the first one bit. | 
 | 1065 | /// @returns numWord() * 64 if the value is zero. | 
 | 1066 | unsigned APInt::CountTrailingZeros() const { | 
 | 1067 |   if (isSingleWord()) | 
 | 1068 |     return CountTrailingZeros_64(~VAL & (VAL - 1)); | 
 | 1069 |   APInt Tmp = ~(*this) & ((*this) - 1); | 
 | 1070 |   return numWords() * 64 - Tmp.CountLeadingZeros(); | 
 | 1071 | } | 
 | 1072 |  | 
 | 1073 | /// CountPopulation - This function is a APInt version corresponding to | 
 | 1074 | /// llvm/include/llvm/Support/MathExtras.h's function | 
 | 1075 | /// CountPopulation_{32, 64}. It counts the number of set bits in a value. | 
 | 1076 | /// @returns 0 if the value is zero. | 
 | 1077 | unsigned APInt::CountPopulation() const { | 
 | 1078 |   if (isSingleWord()) | 
 | 1079 |     return CountPopulation_64(VAL); | 
 | 1080 |   unsigned Count = 0; | 
 | 1081 |   for (unsigned i = 0; i < numWords(); ++i) | 
 | 1082 |     Count += CountPopulation_64(pVal[i]); | 
 | 1083 |   return Count; | 
 | 1084 | } | 
 | 1085 |  | 
 | 1086 |  | 
 | 1087 | /// ByteSwap - This function returns a byte-swapped representation of the | 
 | 1088 | /// APInt argument, APIVal. | 
 | 1089 | APInt llvm::ByteSwap(const APInt& APIVal) { | 
 | 1090 |   if (APIVal.bitsnum <= 32) | 
 | 1091 |     return APInt(APIVal.bitsnum, ByteSwap_32(unsigned(APIVal.VAL))); | 
 | 1092 |   else if (APIVal.bitsnum <= 64) | 
 | 1093 |     return APInt(APIVal.bitsnum, ByteSwap_64(APIVal.VAL)); | 
 | 1094 |   else | 
 | 1095 |     return APIVal; | 
 | 1096 | } | 
 | 1097 |  | 
 | 1098 | /// GreatestCommonDivisor - This function returns the greatest common | 
 | 1099 | /// divisor of the two APInt values using Enclid's algorithm. | 
 | 1100 | APInt llvm::GreatestCommonDivisor(const APInt& API1, const APInt& API2) { | 
 | 1101 |   APInt A = API1, B = API2; | 
 | 1102 |   while (!!B) { | 
 | 1103 |     APInt T = B; | 
 | 1104 |     B = A % B; | 
 | 1105 |     A = T; | 
 | 1106 |   } | 
 | 1107 |   return A; | 
 | 1108 | } | 
| Chris Lattner | 6ad4c14 | 2007-02-06 05:38:37 +0000 | [diff] [blame] | 1109 |  | 
 | 1110 | #endif | 
 | 1111 |  |