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