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