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