blob: ce298fe72bd74cb5e87633007c6b8997a2f137d0 [file] [log] [blame]
Zhou Shengfd43dcf2007-02-06 03:00:16 +00001//===-- 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 Shenga3832fd2007-02-07 06:14:53 +000018#include <cstring>
Zhou Shengfd43dcf2007-02-06 03:00:16 +000019#include <cstdlib>
20using namespace llvm;
21
Reid Spencere81d2da2007-02-16 22:36:51 +000022#if 0
Zhou Sheng353815d2007-02-06 06:04:53 +000023/// 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.
27static uint64_t lshift(uint64_t dest[], unsigned d_offset,
28 uint64_t x[], unsigned len, unsigned shiftAmt) {
Reid Spencer443b5702007-02-18 00:44:22 +000029 unsigned count = APINT_BITS_PER_WORD - shiftAmt;
Zhou Sheng353815d2007-02-06 06:04:53 +000030 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 Spencere81d2da2007-02-16 22:36:51 +000041#endif
Zhou Sheng353815d2007-02-06 06:04:53 +000042
Reid Spencere81d2da2007-02-16 22:36:51 +000043APInt::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 Shengfd43dcf2007-02-06 03:00:16 +000047 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +000048 VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +000049 else {
50 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +000051 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +000052 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +000053 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000054 pVal[0] = val;
55 }
56}
57
Reid Spencere81d2da2007-02-16 22:36:51 +000058APInt::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 Shengfd43dcf2007-02-06 03:00:16 +000062 assert(bigVal && "Null pointer detected!");
63 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +000064 VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +000065 else {
66 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +000067 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +000068 "APInt memory allocation fails!");
69 // Calculate the actual length of bigVal[].
Reid Spencere81d2da2007-02-16 22:36:51 +000070 unsigned maxN = std::max<unsigned>(numWords, getNumWords());
71 unsigned minN = std::min<unsigned>(numWords, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000072 memcpy(pVal, bigVal, (minN - 1) * 8);
Reid Spencer443b5702007-02-18 00:44:22 +000073 pVal[minN-1] = bigVal[minN-1] &
74 (~uint64_t(0ULL) >>
75 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD));
Zhou Shenga3832fd2007-02-07 06:14:53 +000076 if (maxN == getNumWords())
Reid Spencere81d2da2007-02-16 22:36:51 +000077 memset(pVal+numWords, 0, (getNumWords() - numWords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000078 }
79}
80
Zhou Shenga3832fd2007-02-07 06:14:53 +000081/// @brief Create a new APInt by translating the char array represented
82/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +000083APInt::APInt(unsigned numbits, const char StrStart[], unsigned slen,
84 uint8_t radix) {
85 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000086}
87
88/// @brief Create a new APInt by translating the string represented
89/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +000090APInt::APInt(unsigned numbits, const std::string& Val, uint8_t radix) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000091 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000092 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000093}
94
Zhou Shengfd43dcf2007-02-06 03:00:16 +000095APInt::APInt(const APInt& APIVal)
Reid Spencere81d2da2007-02-16 22:36:51 +000096 : BitWidth(APIVal.BitWidth) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +000097 if (isSingleWord()) VAL = APIVal.VAL;
98 else {
99 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +0000100 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000101 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000102 memcpy(pVal, APIVal.pVal, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000103 }
104}
105
106APInt::~APInt() {
107 if (!isSingleWord() && pVal) delete[] pVal;
108}
109
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000110/// @brief Copy assignment operator. Create a new object from the given
111/// APInt one by initialization.
112APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000113 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
114 if (isSingleWord())
115 VAL = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000116 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000117 unsigned minN = std::min(getNumWords(), RHS.getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000118 memcpy(pVal, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, minN * 8);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000119 if (getNumWords() != minN)
120 memset(pVal + minN, 0, (getNumWords() - minN) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000121 }
122 return *this;
123}
124
125/// @brief Assignment operator. Assigns a common case integer value to
126/// the APInt.
127APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000128 if (isSingleWord())
129 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000130 else {
131 pVal[0] = RHS;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000132 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000133 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000134 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000135 return *this;
136}
137
Reid Spencer5e0a8512007-02-17 03:16:00 +0000138/// add_1 - This function adds the integer array x[] by integer y and
139/// returns the carry.
140/// @returns the carry of the addition.
141static 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 Shengfd43dcf2007-02-06 03:00:16 +0000151/// @brief Prefix increment operator. Increments the APInt by one.
152APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000153 if (isSingleWord())
154 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000155 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000156 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000157 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000158 return *this;
159}
160
Reid Spencer5e0a8512007-02-17 03:16:00 +0000161/// sub_1 - This function subtracts the integer array x[] by
162/// integer y and returns the borrow-out carry.
163static 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 Shengfd43dcf2007-02-06 03:00:16 +0000180/// @brief Prefix decrement operator. Decrements the APInt by one.
181APInt& APInt::operator--() {
182 if (isSingleWord()) --VAL;
183 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000184 sub_1(pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000185 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000186 return *this;
187}
188
Reid Spencer5e0a8512007-02-17 03:16:00 +0000189/// add - This function adds the integer array x[] by integer array
190/// y[] and returns the carry.
191static 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 Shengfd43dcf2007-02-06 03:00:16 +0000203/// @brief Addition assignment operator. Adds this APInt by the given APInt&
204/// RHS and assigns the result to this APInt.
205APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000206 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000207 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
208 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000209 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000210 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000211 if (getNumWords() <= RHS.getNumWords())
212 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000213 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000214 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 Shengfd43dcf2007-02-06 03:00:16 +0000217 }
218 }
219 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000220 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000221 return *this;
222}
223
Reid Spencer5e0a8512007-02-17 03:16:00 +0000224/// sub - This function subtracts the integer array x[] by
225/// integer array y[], and returns the borrow-out carry.
226static 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 Shengfd43dcf2007-02-06 03:00:16 +0000243/// @brief Subtraction assignment operator. Subtracts this APInt by the given
244/// APInt &RHS and assigns the result to this APInt.
245APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000246 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000247 if (isSingleWord())
248 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
249 else {
250 if (RHS.isSingleWord())
Zhou Shenga3832fd2007-02-07 06:14:53 +0000251 sub_1(pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000252 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000253 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 Shengfd43dcf2007-02-06 03:00:16 +0000256 }
257 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000258 sub(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000259 }
260 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000261 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000262 return *this;
263}
264
Reid Spencer5e0a8512007-02-17 03:16:00 +0000265/// 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.
268static 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.
301static 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 Shengfd43dcf2007-02-06 03:00:16 +0000332/// @brief Multiplication assignment operator. Multiplies this APInt by the
333/// given APInt& RHS and assigns the result to this APInt.
334APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000335 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000336 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
337 else {
338 // one-based first non-zero bit position.
Reid Spencere81d2da2007-02-16 22:36:51 +0000339 unsigned first = getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000340 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 Spencere81d2da2007-02-16 22:36:51 +0000346 first = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000347 unsigned ylen = !first ? 0 : whichWord(first - 1) + 1;
348 if (!ylen) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000349 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000350 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 Shenga3832fd2007-02-07 06:14:53 +0000355 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ?
356 getNumWords() : xlen + ylen) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000357 delete[] dest;
358 }
359 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000360 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000361 return *this;
362}
363
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000364/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
365/// this APInt and the given APInt& RHS, assigns the result to this APInt.
366APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000367 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000368 if (isSingleWord()) {
369 if (RHS.isSingleWord()) VAL &= RHS.VAL;
370 else VAL &= RHS.pVal[0];
371 } else {
372 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000373 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000374 pVal[0] &= RHS.VAL;
375 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000376 unsigned minwords = getNumWords() < RHS.getNumWords() ?
377 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000378 for (unsigned i = 0; i < minwords; ++i)
379 pVal[i] &= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000380 if (getNumWords() > minwords)
381 memset(pVal+minwords, 0, (getNumWords() - minwords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000382 }
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.
389APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000390 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000391 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 Shenga3832fd2007-02-07 06:14:53 +0000398 unsigned minwords = getNumWords() < RHS.getNumWords() ?
399 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000400 for (unsigned i = 0; i < minwords; ++i)
401 pVal[i] |= RHS.pVal[i];
402 }
403 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000404 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000405 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.
410APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000411 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000412 if (isSingleWord()) {
413 if (RHS.isSingleWord()) VAL ^= RHS.VAL;
414 else VAL ^= RHS.pVal[0];
415 } else {
416 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000417 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000418 pVal[i] ^= RHS.VAL;
419 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000420 unsigned minwords = getNumWords() < RHS.getNumWords() ?
421 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000422 for (unsigned i = 0; i < minwords; ++i)
423 pVal[i] ^= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000424 if (getNumWords() > minwords)
425 for (unsigned i = minwords; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000426 pVal[i] ^= 0;
427 }
428 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000429 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000430 return *this;
431}
432
433/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
434/// and the given APInt& RHS.
435APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000436 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000437 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.
443APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000444 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000445 APInt API(RHS);
446 API |= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000447 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000448 return API;
449}
450
451/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
452/// and the given APInt& RHS.
453APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000454 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000455 APInt API(RHS);
456 API ^= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000457 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000458 return API;
459}
460
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000461
462/// @brief Logical negation operator. Performs logical negation operation on
463/// this APInt.
464bool APInt::operator !() const {
465 if (isSingleWord())
466 return !VAL;
467 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000468 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000469 if (pVal[i])
470 return false;
471 return true;
472}
473
474/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
475/// RHS.
476APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000477 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000478 APInt API(RHS);
479 API *= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000480 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000481 return API;
482}
483
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000484/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
485APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000486 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000487 APInt API(*this);
488 API += RHS;
Reid Spencere81d2da2007-02-16 22:36:51 +0000489 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000490 return API;
491}
492
493/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
494APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000495 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000496 APInt API(*this);
497 API -= RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000498 return API;
499}
500
501/// @brief Array-indexing support.
502bool APInt::operator[](unsigned bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000503 return (maskBit(bitPosition) & (isSingleWord() ?
504 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000505}
506
507/// @brief Equality operator. Compare this APInt with the given APInt& RHS
508/// for the validity of the equality relationship.
509bool APInt::operator==(const APInt& RHS) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000510 unsigned n1 = getActiveBits();
511 unsigned n2 = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000512 if (n1 != n2) return false;
513 else if (isSingleWord())
514 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
515 else {
Reid Spencer443b5702007-02-18 00:44:22 +0000516 if (n1 <= APINT_BITS_PER_WORD)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000517 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 Shenga3832fd2007-02-07 06:14:53 +0000524/// @brief Equality operator. Compare this APInt with the given uint64_t value
525/// for the validity of the equality relationship.
526bool APInt::operator==(uint64_t Val) const {
527 if (isSingleWord())
528 return VAL == Val;
529 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000530 unsigned n = getActiveBits();
Reid Spencer443b5702007-02-18 00:44:22 +0000531 if (n <= APINT_BITS_PER_WORD)
Zhou Shenga3832fd2007-02-07 06:14:53 +0000532 return pVal[0] == Val;
533 else
534 return false;
535 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000536}
537
Reid Spencere81d2da2007-02-16 22:36:51 +0000538/// @brief Unsigned less than comparison
539bool 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 Shengfd43dcf2007-02-06 03:00:16 +0000543 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000544 unsigned n1 = getActiveBits();
545 unsigned n2 = RHS.getActiveBits();
546 if (n1 < n2)
547 return true;
548 else if (n2 < n1)
549 return false;
Reid Spencer443b5702007-02-18 00:44:22 +0000550 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000551 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000552 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 Spencere81d2da2007-02-16 22:36:51 +0000560/// @brief Signed less than comparison
561bool 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 Spencer443b5702007-02-18 00:44:22 +0000572 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000573 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 Shengfd43dcf2007-02-06 03:00:16 +0000580}
581
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000582/// Set the given bit to 1 whose poition is given as "bitPosition".
583/// @brief Set a given bit to 1.
584APInt& 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.
591APInt& APInt::set() {
Reid Spencer443b5702007-02-18 00:44:22 +0000592 if (isSingleWord())
593 VAL = ~0ULL >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000594 else {
595 for (unsigned i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000596 pVal[i] = -1ULL;
Reid Spencer443b5702007-02-18 00:44:22 +0000597 pVal[getNumWords() - 1] = ~0ULL >>
598 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD);
Zhou Shengb04973e2007-02-15 06:36:31 +0000599 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000600 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.
605APInt& 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.
612APInt& APInt::clear() {
613 if (isSingleWord()) VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000614 else
615 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000616 return *this;
617}
618
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000619/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
620/// this APInt.
621APInt APInt::operator~() const {
622 APInt API(*this);
623 API.flip();
624 return API;
625}
626
627/// @brief Toggle every bit to its opposite value.
628APInt& APInt::flip() {
Reid Spencer443b5702007-02-18 00:44:22 +0000629 if (isSingleWord()) VAL = (~(VAL <<
630 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000631 else {
632 unsigned i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000633 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000634 pVal[i] = ~pVal[i];
Reid Spencer443b5702007-02-18 00:44:22 +0000635 unsigned offset =
636 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000637 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.
645APInt& APInt::flip(unsigned bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000646 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000647 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 Spencer443b5702007-02-18 00:44:22 +0000653std::string APInt::toString(uint8_t radix, bool wantSigned) const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000654 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
655 "Radix should be 2, 8, 10, or 16!");
Reid Spencer879dfe12007-02-14 02:52:25 +0000656 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 Spencere81d2da2007-02-16 22:36:51 +0000660 unsigned bits_used = getActiveBits();
Reid Spencer879dfe12007-02-14 02:52:25 +0000661 if (isSingleWord()) {
662 char buf[65];
Reid Spencer443b5702007-02-18 00:44:22 +0000663 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
Reid Spencer879dfe12007-02-14 02:52:25 +0000664 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
665 if (format) {
Reid Spencer443b5702007-02-18 00:44:22 +0000666 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 Spencer879dfe12007-02-14 02:52:25 +0000672 } 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 Shengfd43dcf2007-02-06 03:00:16 +0000681 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000682 result = buf;
683 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000684 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000685
686 APInt tmp(*this);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000687 APInt divisor(tmp.getBitWidth(), radix);
688 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer443b5702007-02-18 00:44:22 +0000689 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 Spencer879dfe12007-02-14 02:52:25 +0000699 if (tmp == 0)
700 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000701 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000702 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spencer879dfe12007-02-14 02:52:25 +0000703 unsigned digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000704 assert(digit < radix && "urem failed");
Reid Spencer443b5702007-02-18 00:44:22 +0000705 result.insert(insert_at,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000706 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000707 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000708
709 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000710}
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.
715APInt APInt::getMaxValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000716 APInt APIVal(numBits, 0);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000717 APIVal.set();
Zhou Shengb04973e2007-02-15 06:36:31 +0000718 if (isSign) APIVal.clear(numBits - 1);
719 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000720}
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.
725APInt APInt::getMinValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000726 APInt APIVal(numBits, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000727 if (isSign) APIVal.set(numBits - 1);
728 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000729}
730
731/// getAllOnesValue - This function returns an all-ones value for
732/// an APInt of the specified bit-width.
733APInt 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.
739APInt APInt::getNullValue(unsigned numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000740 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000741}
742
743/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000744APInt APInt::getHiBits(unsigned numBits) const {
745 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000746}
747
748/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000749APInt APInt::getLoBits(unsigned numBits) const {
750 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
751 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000752}
753
Reid Spencere81d2da2007-02-16 22:36:51 +0000754bool APInt::isPowerOf2() const {
755 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
756}
757
758/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000759/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000760/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000761/// the number of zeros from the most significant bit to the first one bit.
762/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000763unsigned APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000764 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000765 return CountLeadingZeros_64(VAL) - (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000766 unsigned Count = 0;
Reid Spencer443b5702007-02-18 00:44:22 +0000767 for (unsigned i = getNumWords(); i > 0u; --i) {
768 unsigned tmp = CountLeadingZeros_64(pVal[i-1]);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000769 Count += tmp;
Reid Spencer443b5702007-02-18 00:44:22 +0000770 if (tmp != APINT_BITS_PER_WORD)
771 if (i == getNumWords())
772 Count -= (APINT_BITS_PER_WORD - whichBit(BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000773 break;
774 }
775 return Count;
776}
777
Reid Spencere81d2da2007-02-16 22:36:51 +0000778/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000779/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000780/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000781/// the number of zeros from the least significant bit to the first one bit.
782/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000783unsigned APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000784 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000785 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000786 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000787 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000788}
789
Reid Spencere81d2da2007-02-16 22:36:51 +0000790/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000791/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000792/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000793/// @returns 0 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000794unsigned APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000795 if (isSingleWord())
796 return CountPopulation_64(VAL);
797 unsigned Count = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000798 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000799 Count += CountPopulation_64(pVal[i]);
800 return Count;
801}
802
803
Reid Spencere81d2da2007-02-16 22:36:51 +0000804/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000805/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000806APInt APInt::byteSwap() const {
807 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
808 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000809 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000810 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000811 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000812 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000813 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 Spencercd6f2bf2007-02-17 00:18:01 +0000818 APInt(BitWidth,
819 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000820 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000821 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000822 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000823 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000824 char *pByte = (char*)Result.pVal;
Reid Spencere81d2da2007-02-16 22:36:51 +0000825 for (unsigned i = 0; i < BitWidth / 8 / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000826 char Tmp = pByte[i];
Reid Spencere81d2da2007-02-16 22:36:51 +0000827 pByte[i] = pByte[BitWidth / 8 - 1 - i];
828 pByte[BitWidth / 8 - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000829 }
830 return Result;
831 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000832}
833
834/// GreatestCommonDivisor - This function returns the greatest common
835/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000836APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
837 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000838 APInt A = API1, B = API2;
839 while (!!B) {
840 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000841 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000842 A = T;
843 }
844 return A;
845}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000846
Zhou Shengd93f00c2007-02-12 20:02:55 +0000847/// DoubleRoundToAPInt - This function convert a double value to
848/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000849APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000850 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 Spencere81d2da2007-02-16 22:36:51 +0000858 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000859 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
860 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000861 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 Shengd93f00c2007-02-12 20:02:55 +0000865 return isNeg ? -Tmp : Tmp;
866}
867
Reid Spencerdb3faa62007-02-13 22:41:58 +0000868/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000869/// 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 Spencere81d2da2007-02-16 22:36:51 +0000875double APInt::roundToDouble(bool isSigned) const {
876 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000877 APInt Tmp(isNeg ? -(*this) : (*this));
878 if (Tmp.isSingleWord())
879 return isSigned ? double(int64_t(Tmp.VAL)) : double(Tmp.VAL);
Reid Spencere81d2da2007-02-16 22:36:51 +0000880 unsigned n = Tmp.getActiveBits();
Reid Spencer443b5702007-02-18 00:44:22 +0000881 if (n <= APINT_BITS_PER_WORD)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000882 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 Spencer443b5702007-02-18 00:44:22 +0000893 if (n % APINT_BITS_PER_WORD >= 53)
894 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % APINT_BITS_PER_WORD - 53);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000895 else
Reid Spencer443b5702007-02-18 00:44:22 +0000896 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 Shengd93f00c2007-02-12 20:02:55 +0000899 // The leading bit of mantissa is implicit, so get rid of it.
900 mantissa &= ~(1ULL << 52);
Reid Spencer443b5702007-02-18 00:44:22 +0000901 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000902 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 Spencere81d2da2007-02-16 22:36:51 +0000911// Truncate to new width.
912void APInt::trunc(unsigned width) {
913 assert(width < BitWidth && "Invalid APInt Truncate request");
914}
915
916// Sign extend to a new width.
917void APInt::sext(unsigned width) {
918 assert(width > BitWidth && "Invalid APInt SignExtend request");
919}
920
921// Zero extend to a new width.
922void APInt::zext(unsigned width) {
923 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
924}
925
Zhou Shengff4304f2007-02-09 07:48:24 +0000926/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000927/// @brief Arithmetic right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000928APInt APInt::ashr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000929 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000930 if (API.isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000931 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 Sheng0b706b12007-02-08 14:35:19 +0000935 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000936 if (shiftAmt >= API.BitWidth) {
937 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0, (API.getNumWords()-1) * 8);
Reid Spencer443b5702007-02-18 00:44:22 +0000938 API.pVal[API.getNumWords() - 1] =
939 ~uint64_t(0UL) >>
940 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000941 } else {
942 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000943 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000944 if (API[i+shiftAmt])
945 API.set(i);
946 else
947 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000948 for (; i < API.BitWidth; ++i)
949 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000950 API.set(i);
951 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000952 }
953 }
954 return API;
955}
956
Zhou Shengff4304f2007-02-09 07:48:24 +0000957/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000958/// @brief Logical right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000959APInt APInt::lshr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000960 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000961 if (API.isSingleWord())
962 API.VAL >>= shiftAmt;
963 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000964 if (shiftAmt >= API.BitWidth)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000965 memset(API.pVal, 0, API.getNumWords() * 8);
966 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000967 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000968 if (API[i+shiftAmt]) API.set(i);
969 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000970 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000971 API.clear(i);
972 }
973 return API;
974}
975
Zhou Shengff4304f2007-02-09 07:48:24 +0000976/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000977/// @brief Left-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000978APInt APInt::shl(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000979 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000980 if (API.isSingleWord())
981 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000982 else if (shiftAmt >= API.BitWidth)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000983 memset(API.pVal, 0, API.getNumWords() * 8);
984 else {
Reid Spencer443b5702007-02-18 00:44:22 +0000985 if (unsigned offset = shiftAmt / APINT_BITS_PER_WORD) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000986 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 Sheng0b706b12007-02-08 14:35:19 +0000989 }
Reid Spencer443b5702007-02-18 00:44:22 +0000990 shiftAmt %= APINT_BITS_PER_WORD;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000991 unsigned i;
992 for (i = API.getNumWords() - 1; i > 0; --i)
993 API.pVal[i] = (API.pVal[i] << shiftAmt) |
Reid Spencer443b5702007-02-18 00:44:22 +0000994 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000995 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000996 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000997 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000998 return API;
999}
1000
Reid Spencer5e0a8512007-02-17 03:16:00 +00001001/// 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.
1004static 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 Spencerc72f2802007-02-17 22:38:07 +00001010 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001011 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.
1026static 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).
1056static 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 Shengff4304f2007-02-09 07:48:24 +00001091/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001092/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001093APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001094 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001095
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 Sheng0b706b12007-02-08 14:35:19 +00001100 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001101
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 Spencer443b5702007-02-18 00:44:22 +00001129 unsigned nshift =
1130 (APINT_BITS_PER_WORD - 1) - ((rhsBits - 1) % APINT_BITS_PER_WORD );
1131 if (nshift) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001132 Y = APIntOps::shl(RHS, nshift);
1133 X = APIntOps::shl(Result, nshift);
1134 ++lhsWords;
1135 }
Reid Spencerc72f2802007-02-17 22:38:07 +00001136 div((unsigned*)X.pVal, lhsWords * 2 - 1,
1137 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001138 memset(Result.pVal, 0, Result.getNumWords() * 8);
1139 memcpy(Result.pVal, X.pVal + rhsWords, (lhsWords - rhsWords) * 8);
1140 }
1141 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001142}
1143
1144/// Unsigned remainder operation on APInt.
1145/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001146APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001147 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001148 if (isSingleWord()) {
1149 assert(RHS.VAL != 0 && "Remainder by zero?");
1150 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001151 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001152
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 Spencer443b5702007-02-18 00:44:22 +00001180 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 Spencer71bd08f2007-02-17 02:07:07 +00001184 if (nshift) {
1185 APIntOps::shl(Y, nshift);
1186 APIntOps::shl(X, nshift);
1187 }
Reid Spencerc72f2802007-02-17 22:38:07 +00001188 div((unsigned*)X.pVal, rhsWords*2-1,
1189 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001190 memset(Result.pVal, 0, Result.getNumWords() * 8);
1191 for (unsigned i = 0; i < rhsWords-1; ++i)
Reid Spencer443b5702007-02-18 00:44:22 +00001192 Result.pVal[i] = (X.pVal[i] >> nshift) |
1193 (X.pVal[i+1] << (APINT_BITS_PER_WORD - nshift));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001194 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift;
1195 }
1196 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001197}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001198
1199/// @brief Converts a char array into an integer.
1200void 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 Spencer443b5702007-02-18 00:44:22 +00001216 uint64_t digit = StrStart[i] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001217 resDigit |= digit << nextBitPos;
1218 nextBitPos += bits_per_digit;
Reid Spencer443b5702007-02-18 00:44:22 +00001219 if (nextBitPos >= APINT_BITS_PER_WORD) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001220 if (isSingleWord()) {
1221 VAL = resDigit;
1222 break;
1223 }
1224 pVal[size++] = resDigit;
Reid Spencer443b5702007-02-18 00:44:22 +00001225 nextBitPos -= APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001226 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 Spencer443b5702007-02-18 00:44:22 +00001238 BitWidth = APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001239 VAL = strtoull(StrStart, 0, 10);
1240 } else { // In case the value > 2^64 - 1
Reid Spencer443b5702007-02-18 00:44:22 +00001241 BitWidth = (slen / chars_per_word + 1) * APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001242 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 Spencer443b5702007-02-18 00:44:22 +00001250 uint64_t resDigit = StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001251 uint64_t big_base = radix;
1252 while (--chunk > 0) {
Reid Spencer443b5702007-02-18 00:44:22 +00001253 resDigit = resDigit * radix + StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001254 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