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