blob: 2c5ed978056555f666f75fa66e02cd818c2a8c05 [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
Zhou Sheng353815d2007-02-06 06:04:53 +000022/// mul_1 - This function performs the multiplication operation on a
23/// large integer (represented as an integer array) and a uint64_t integer.
24/// @returns the carry of the multiplication.
25static uint64_t mul_1(uint64_t dest[], uint64_t x[],
26 unsigned len, uint64_t y) {
27 // Split y into high 32-bit part and low 32-bit part.
28 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
29 uint64_t carry = 0, lx, hx;
30 for (unsigned i = 0; i < len; ++i) {
31 lx = x[i] & 0xffffffffULL;
32 hx = x[i] >> 32;
33 // hasCarry - A flag to indicate if has carry.
34 // hasCarry == 0, no carry
35 // hasCarry == 1, has carry
36 // hasCarry == 2, no carry and the calculation result == 0.
37 uint8_t hasCarry = 0;
38 dest[i] = carry + lx * ly;
39 // Determine if the add above introduces carry.
40 hasCarry = (dest[i] < carry) ? 1 : 0;
41 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
42 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
43 // (2^32 - 1) + 2^32 = 2^64.
44 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
45
46 carry += (lx * hy) & 0xffffffffULL;
47 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
48 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
49 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
50 }
51
52 return carry;
53}
54
55/// mul - This function multiplies integer array x[] by integer array y[] and
56/// stores the result into integer array dest[].
57/// Note the array dest[]'s size should no less than xlen + ylen.
58static void mul(uint64_t dest[], uint64_t x[], unsigned xlen,
59 uint64_t y[], unsigned ylen) {
60 dest[xlen] = mul_1(dest, x, xlen, y[0]);
61
62 for (unsigned i = 1; i < ylen; ++i) {
63 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
64 uint64_t carry = 0, lx, hx;
65 for (unsigned j = 0; j < xlen; ++j) {
66 lx = x[j] & 0xffffffffULL;
67 hx = x[j] >> 32;
68 // hasCarry - A flag to indicate if has carry.
69 // hasCarry == 0, no carry
70 // hasCarry == 1, has carry
71 // hasCarry == 2, no carry and the calculation result == 0.
72 uint8_t hasCarry = 0;
73 uint64_t resul = carry + lx * ly;
74 hasCarry = (resul < carry) ? 1 : 0;
75 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
76 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
77
78 carry += (lx * hy) & 0xffffffffULL;
79 resul = (carry << 32) | (resul & 0xffffffffULL);
80 dest[i+j] += resul;
81 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
82 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
83 ((lx * hy) >> 32) + hx * hy;
84 }
85 dest[i+xlen] = carry;
86 }
87}
88
89/// add_1 - This function adds the integer array x[] by integer y and
90/// returns the carry.
91/// @returns the carry of the addition.
92static uint64_t add_1(uint64_t dest[], uint64_t x[],
93 unsigned len, uint64_t y) {
94 uint64_t carry = y;
95
96 for (unsigned i = 0; i < len; ++i) {
97 dest[i] = carry + x[i];
98 carry = (dest[i] < carry) ? 1 : 0;
99 }
100 return carry;
101}
102
103/// add - This function adds the integer array x[] by integer array
104/// y[] and returns the carry.
105static uint64_t add(uint64_t dest[], uint64_t x[],
106 uint64_t y[], unsigned len) {
107 unsigned carry = 0;
108
109 for (unsigned i = 0; i< len; ++i) {
110 carry += x[i];
111 dest[i] = carry + y[i];
112 carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0);
113 }
114 return carry;
115}
116
117/// sub_1 - This function subtracts the integer array x[] by
118/// integer y and returns the borrow-out carry.
119static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) {
120 uint64_t cy = y;
121
122 for (unsigned i = 0; i < len; ++i) {
123 uint64_t X = x[i];
124 x[i] -= cy;
125 if (cy > X)
126 cy = 1;
127 else {
128 cy = 0;
129 break;
130 }
131 }
132
133 return cy;
134}
135
136/// sub - This function subtracts the integer array x[] by
137/// integer array y[], and returns the borrow-out carry.
138static uint64_t sub(uint64_t dest[], uint64_t x[],
139 uint64_t y[], unsigned len) {
140 // Carry indicator.
141 uint64_t cy = 0;
142
143 for (unsigned i = 0; i < len; ++i) {
144 uint64_t Y = y[i], X = x[i];
145 Y += cy;
146
147 cy = Y < cy ? 1 : 0;
148 Y = X - Y;
149 cy += Y > X ? 1 : 0;
150 dest[i] = Y;
151 }
152 return cy;
153}
154
155/// UnitDiv - This function divides N by D,
156/// and returns (remainder << 32) | quotient.
157/// Assumes (N >> 32) < D.
158static uint64_t unitDiv(uint64_t N, unsigned D) {
159 uint64_t q, r; // q: quotient, r: remainder.
160 uint64_t a1 = N >> 32; // a1: high 32-bit part of N.
161 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N
162 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) {
163 q = N / D;
164 r = N % D;
165 }
166 else {
167 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d
168 uint64_t c = N - ((uint64_t) D << 31);
169 // Divide (c1*2^32 + c0) by d
170 q = c / D;
171 r = c % D;
172 // Add 2^31 to quotient
173 q += 1 << 31;
174 }
175
176 return (r << 32) | (q & 0xFFFFFFFFl);
177}
178
179/// subMul - This function substracts x[len-1:0] * y from
180/// dest[offset+len-1:offset], and returns the most significant
181/// word of the product, minus the borrow-out from the subtraction.
182static unsigned subMul(unsigned dest[], unsigned offset,
183 unsigned x[], unsigned len, unsigned y) {
184 uint64_t yl = (uint64_t) y & 0xffffffffL;
185 unsigned carry = 0;
186 unsigned j = 0;
187 do {
188 uint64_t prod = ((uint64_t) x[j] & 0xffffffffL) * yl;
189 unsigned prod_low = (unsigned) prod;
190 unsigned prod_high = (unsigned) (prod >> 32);
191 prod_low += carry;
192 carry = (prod_low < carry ? 1 : 0) + prod_high;
193 unsigned x_j = dest[offset+j];
194 prod_low = x_j - prod_low;
195 if (prod_low > x_j) ++carry;
196 dest[offset+j] = prod_low;
197 } while (++j < len);
198 return carry;
199}
200
201/// div - This is basically Knuth's formulation of the classical algorithm.
202/// Correspondance with Knuth's notation:
203/// Knuth's u[0:m+n] == zds[nx:0].
204/// Knuth's v[1:n] == y[ny-1:0]
205/// Knuth's n == ny.
206/// Knuth's m == nx-ny.
207/// Our nx == Knuth's m+n.
208/// Could be re-implemented using gmp's mpn_divrem:
209/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny).
210static void div(unsigned zds[], unsigned nx, unsigned y[], unsigned ny) {
211 unsigned j = nx;
212 do { // loop over digits of quotient
213 // Knuth's j == our nx-j.
214 // Knuth's u[j:j+n] == our zds[j:j-ny].
215 unsigned qhat; // treated as unsigned
216 if (zds[j] == y[ny-1]) qhat = -1U; // 0xffffffff
217 else {
218 uint64_t w = (((uint64_t)(zds[j])) << 32) +
219 ((uint64_t)zds[j-1] & 0xffffffffL);
220 qhat = (unsigned) unitDiv(w, y[ny-1]);
221 }
222 if (qhat) {
223 unsigned borrow = subMul(zds, j - ny, y, ny, qhat);
224 unsigned save = zds[j];
225 uint64_t num = ((uint64_t)save&0xffffffffL) -
226 ((uint64_t)borrow&0xffffffffL);
227 while (num) {
228 qhat--;
229 uint64_t carry = 0;
230 for (unsigned i = 0; i < ny; i++) {
231 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL)
232 + ((uint64_t) y[i] & 0xffffffffL);
233 zds[j-ny+i] = (unsigned) carry;
234 carry >>= 32;
235 }
236 zds[j] += carry;
237 num = carry - 1;
238 }
239 }
240 zds[j] = qhat;
241 } while (--j >= ny);
242}
243
Reid Spencere81d2da2007-02-16 22:36:51 +0000244#if 0
Zhou Sheng353815d2007-02-06 06:04:53 +0000245/// lshift - This function shift x[0:len-1] left by shiftAmt bits, and
246/// store the len least significant words of the result in
247/// dest[d_offset:d_offset+len-1]. It returns the bits shifted out from
248/// the most significant digit.
249static uint64_t lshift(uint64_t dest[], unsigned d_offset,
250 uint64_t x[], unsigned len, unsigned shiftAmt) {
251 unsigned count = 64 - shiftAmt;
252 int i = len - 1;
253 uint64_t high_word = x[i], retVal = high_word >> count;
254 ++d_offset;
255 while (--i >= 0) {
256 uint64_t low_word = x[i];
257 dest[d_offset+i] = (high_word << shiftAmt) | (low_word >> count);
258 high_word = low_word;
259 }
260 dest[d_offset+i] = high_word << shiftAmt;
261 return retVal;
262}
Reid Spencere81d2da2007-02-16 22:36:51 +0000263#endif
Zhou Sheng353815d2007-02-06 06:04:53 +0000264
Reid Spencere81d2da2007-02-16 22:36:51 +0000265APInt::APInt(unsigned numBits, uint64_t val)
266 : BitWidth(numBits) {
267 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
268 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000269 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +0000270 VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000271 else {
272 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +0000273 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000274 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000275 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000276 pVal[0] = val;
277 }
278}
279
Reid Spencere81d2da2007-02-16 22:36:51 +0000280APInt::APInt(unsigned numBits, unsigned numWords, uint64_t bigVal[])
281 : BitWidth(numBits) {
282 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
283 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000284 assert(bigVal && "Null pointer detected!");
285 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +0000286 VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000287 else {
288 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +0000289 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000290 "APInt memory allocation fails!");
291 // Calculate the actual length of bigVal[].
Reid Spencere81d2da2007-02-16 22:36:51 +0000292 unsigned maxN = std::max<unsigned>(numWords, getNumWords());
293 unsigned minN = std::min<unsigned>(numWords, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000294 memcpy(pVal, bigVal, (minN - 1) * 8);
Reid Spencere81d2da2007-02-16 22:36:51 +0000295 pVal[minN-1] = bigVal[minN-1] & (~uint64_t(0ULL) >> (64 - BitWidth % 64));
Zhou Shenga3832fd2007-02-07 06:14:53 +0000296 if (maxN == getNumWords())
Reid Spencere81d2da2007-02-16 22:36:51 +0000297 memset(pVal+numWords, 0, (getNumWords() - numWords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000298 }
299}
300
Zhou Shenga3832fd2007-02-07 06:14:53 +0000301/// @brief Create a new APInt by translating the char array represented
302/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000303APInt::APInt(unsigned numbits, const char StrStart[], unsigned slen,
304 uint8_t radix) {
305 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000306}
307
308/// @brief Create a new APInt by translating the string represented
309/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000310APInt::APInt(unsigned numbits, const std::string& Val, uint8_t radix) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000311 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +0000312 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000313}
314
315/// @brief Converts a char array into an integer.
Reid Spencere81d2da2007-02-16 22:36:51 +0000316void APInt::fromString(unsigned numbits, const char *StrStart, unsigned slen,
317 uint8_t radix) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000318 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
319 "Radix should be 2, 8, 10, or 16!");
Reid Spencere81d2da2007-02-16 22:36:51 +0000320 assert(StrStart && "String is null?");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000321 unsigned size = 0;
322 // If the radix is a power of 2, read the input
323 // from most significant to least significant.
324 if ((radix & (radix - 1)) == 0) {
325 unsigned nextBitPos = 0, bits_per_digit = radix / 8 + 2;
326 uint64_t resDigit = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000327 BitWidth = slen * bits_per_digit;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000328 if (getNumWords() > 1)
329 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000330 "APInt memory allocation fails!");
331 for (int i = slen - 1; i >= 0; --i) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000332 uint64_t digit = StrStart[i] - 48; // '0' == 48.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000333 resDigit |= digit << nextBitPos;
334 nextBitPos += bits_per_digit;
335 if (nextBitPos >= 64) {
336 if (isSingleWord()) {
337 VAL = resDigit;
338 break;
339 }
340 pVal[size++] = resDigit;
341 nextBitPos -= 64;
342 resDigit = digit >> (bits_per_digit - nextBitPos);
343 }
344 }
Zhou Shenga3832fd2007-02-07 06:14:53 +0000345 if (!isSingleWord() && size <= getNumWords())
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000346 pVal[size] = resDigit;
347 } else { // General case. The radix is not a power of 2.
348 // For 10-radix, the max value of 64-bit integer is 18446744073709551615,
Zhou Shengb04973e2007-02-15 06:36:31 +0000349 // and its digits number is 20.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000350 const unsigned chars_per_word = 20;
351 if (slen < chars_per_word ||
Zhou Shenga3832fd2007-02-07 06:14:53 +0000352 (slen == chars_per_word && // In case the value <= 2^64 - 1
353 strcmp(StrStart, "18446744073709551615") <= 0)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000354 BitWidth = 64;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000355 VAL = strtoull(StrStart, 0, 10);
356 } else { // In case the value > 2^64 - 1
Reid Spencere81d2da2007-02-16 22:36:51 +0000357 BitWidth = (slen / chars_per_word + 1) * 64;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000358 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000359 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000360 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000361 unsigned str_pos = 0;
362 while (str_pos < slen) {
363 unsigned chunk = slen - str_pos;
364 if (chunk > chars_per_word - 1)
365 chunk = chars_per_word - 1;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000366 uint64_t resDigit = StrStart[str_pos++] - 48; // 48 == '0'.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000367 uint64_t big_base = radix;
368 while (--chunk > 0) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000369 resDigit = resDigit * radix + StrStart[str_pos++] - 48;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000370 big_base *= radix;
371 }
372
373 uint64_t carry;
374 if (!size)
375 carry = resDigit;
376 else {
377 carry = mul_1(pVal, pVal, size, big_base);
378 carry += add_1(pVal, pVal, size, resDigit);
379 }
380
381 if (carry) pVal[size++] = carry;
382 }
383 }
384 }
385}
386
387APInt::APInt(const APInt& APIVal)
Reid Spencere81d2da2007-02-16 22:36:51 +0000388 : BitWidth(APIVal.BitWidth) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000389 if (isSingleWord()) VAL = APIVal.VAL;
390 else {
391 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +0000392 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000393 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000394 memcpy(pVal, APIVal.pVal, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000395 }
396}
397
398APInt::~APInt() {
399 if (!isSingleWord() && pVal) delete[] pVal;
400}
401
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000402/// @brief Copy assignment operator. Create a new object from the given
403/// APInt one by initialization.
404APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000405 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
406 if (isSingleWord())
407 VAL = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000408 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000409 unsigned minN = std::min(getNumWords(), RHS.getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000410 memcpy(pVal, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, minN * 8);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000411 if (getNumWords() != minN)
412 memset(pVal + minN, 0, (getNumWords() - minN) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000413 }
414 return *this;
415}
416
417/// @brief Assignment operator. Assigns a common case integer value to
418/// the APInt.
419APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000420 if (isSingleWord())
421 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000422 else {
423 pVal[0] = RHS;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000424 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000425 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000426 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000427 return *this;
428}
429
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000430/// @brief Prefix increment operator. Increments the APInt by one.
431APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000432 if (isSingleWord())
433 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000434 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000435 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000436 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000437 return *this;
438}
439
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000440/// @brief Prefix decrement operator. Decrements the APInt by one.
441APInt& APInt::operator--() {
442 if (isSingleWord()) --VAL;
443 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000444 sub_1(pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000445 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000446 return *this;
447}
448
449/// @brief Addition assignment operator. Adds this APInt by the given APInt&
450/// RHS and assigns the result to this APInt.
451APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000452 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000453 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
454 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000455 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000456 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000457 if (getNumWords() <= RHS.getNumWords())
458 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000459 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000460 uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.getNumWords());
461 add_1(pVal + RHS.getNumWords(), pVal + RHS.getNumWords(),
462 getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000463 }
464 }
465 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000466 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000467 return *this;
468}
469
470/// @brief Subtraction assignment operator. Subtracts this APInt by the given
471/// APInt &RHS and assigns the result to this APInt.
472APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000473 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000474 if (isSingleWord())
475 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
476 else {
477 if (RHS.isSingleWord())
Zhou Shenga3832fd2007-02-07 06:14:53 +0000478 sub_1(pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000479 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000480 if (RHS.getNumWords() < getNumWords()) {
481 uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
482 sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000483 }
484 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000485 sub(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000486 }
487 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000488 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000489 return *this;
490}
491
492/// @brief Multiplication assignment operator. Multiplies this APInt by the
493/// given APInt& RHS and assigns the result to this APInt.
494APInt& APInt::operator*=(const APInt& RHS) {
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 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
497 else {
498 // one-based first non-zero bit position.
Reid Spencere81d2da2007-02-16 22:36:51 +0000499 unsigned first = getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000500 unsigned xlen = !first ? 0 : whichWord(first - 1) + 1;
501 if (!xlen)
502 return *this;
503 else if (RHS.isSingleWord())
504 mul_1(pVal, pVal, xlen, RHS.VAL);
505 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000506 first = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000507 unsigned ylen = !first ? 0 : whichWord(first - 1) + 1;
508 if (!ylen) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000509 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000510 return *this;
511 }
512 uint64_t *dest = new uint64_t[xlen+ylen];
513 assert(dest && "Memory Allocation Failed!");
514 mul(dest, pVal, xlen, RHS.pVal, ylen);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000515 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ?
516 getNumWords() : xlen + ylen) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000517 delete[] dest;
518 }
519 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000520 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000521 return *this;
522}
523
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000524/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
525/// this APInt and the given APInt& RHS, assigns the result to this APInt.
526APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000527 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000528 if (isSingleWord()) {
529 if (RHS.isSingleWord()) VAL &= RHS.VAL;
530 else VAL &= RHS.pVal[0];
531 } else {
532 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000533 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000534 pVal[0] &= RHS.VAL;
535 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000536 unsigned minwords = getNumWords() < RHS.getNumWords() ?
537 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000538 for (unsigned i = 0; i < minwords; ++i)
539 pVal[i] &= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000540 if (getNumWords() > minwords)
541 memset(pVal+minwords, 0, (getNumWords() - minwords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000542 }
543 }
544 return *this;
545}
546
547/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on
548/// this APInt and the given APInt& RHS, assigns the result to this APInt.
549APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000550 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000551 if (isSingleWord()) {
552 if (RHS.isSingleWord()) VAL |= RHS.VAL;
553 else VAL |= RHS.pVal[0];
554 } else {
555 if (RHS.isSingleWord()) {
556 pVal[0] |= RHS.VAL;
557 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000558 unsigned minwords = getNumWords() < RHS.getNumWords() ?
559 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000560 for (unsigned i = 0; i < minwords; ++i)
561 pVal[i] |= RHS.pVal[i];
562 }
563 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000564 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000565 return *this;
566}
567
568/// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on
569/// this APInt and the given APInt& RHS, assigns the result to this APInt.
570APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000571 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000572 if (isSingleWord()) {
573 if (RHS.isSingleWord()) VAL ^= RHS.VAL;
574 else VAL ^= RHS.pVal[0];
575 } else {
576 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000577 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000578 pVal[i] ^= RHS.VAL;
579 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000580 unsigned minwords = getNumWords() < RHS.getNumWords() ?
581 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000582 for (unsigned i = 0; i < minwords; ++i)
583 pVal[i] ^= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000584 if (getNumWords() > minwords)
585 for (unsigned i = minwords; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000586 pVal[i] ^= 0;
587 }
588 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000589 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000590 return *this;
591}
592
593/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
594/// and the given APInt& RHS.
595APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000596 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000597 APInt API(RHS);
598 return API &= *this;
599}
600
601/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt
602/// and the given APInt& RHS.
603APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000604 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000605 APInt API(RHS);
606 API |= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000607 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000608 return API;
609}
610
611/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
612/// and the given APInt& RHS.
613APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000614 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000615 APInt API(RHS);
616 API ^= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000617 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000618 return API;
619}
620
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000621
622/// @brief Logical negation operator. Performs logical negation operation on
623/// this APInt.
624bool APInt::operator !() const {
625 if (isSingleWord())
626 return !VAL;
627 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000628 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000629 if (pVal[i])
630 return false;
631 return true;
632}
633
634/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
635/// RHS.
636APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000637 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000638 APInt API(RHS);
639 API *= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000640 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000641 return API;
642}
643
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000644/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
645APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000646 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000647 APInt API(*this);
648 API += RHS;
Reid Spencere81d2da2007-02-16 22:36:51 +0000649 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000650 return API;
651}
652
653/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
654APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000655 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000656 APInt API(*this);
657 API -= RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000658 return API;
659}
660
661/// @brief Array-indexing support.
662bool APInt::operator[](unsigned bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000663 return (maskBit(bitPosition) & (isSingleWord() ?
664 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000665}
666
667/// @brief Equality operator. Compare this APInt with the given APInt& RHS
668/// for the validity of the equality relationship.
669bool APInt::operator==(const APInt& RHS) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000670 unsigned n1 = getActiveBits();
671 unsigned n2 = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000672 if (n1 != n2) return false;
673 else if (isSingleWord())
674 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
675 else {
676 if (n1 <= 64)
677 return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
678 for (int i = whichWord(n1 - 1); i >= 0; --i)
679 if (pVal[i] != RHS.pVal[i]) return false;
680 }
681 return true;
682}
683
Zhou Shenga3832fd2007-02-07 06:14:53 +0000684/// @brief Equality operator. Compare this APInt with the given uint64_t value
685/// for the validity of the equality relationship.
686bool APInt::operator==(uint64_t Val) const {
687 if (isSingleWord())
688 return VAL == Val;
689 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000690 unsigned n = getActiveBits();
Zhou Shenga3832fd2007-02-07 06:14:53 +0000691 if (n <= 64)
692 return pVal[0] == Val;
693 else
694 return false;
695 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000696}
697
Reid Spencere81d2da2007-02-16 22:36:51 +0000698/// @brief Unsigned less than comparison
699bool APInt::ult(const APInt& RHS) const {
700 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
701 if (isSingleWord())
702 return VAL < RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000703 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000704 unsigned n1 = getActiveBits();
705 unsigned n2 = RHS.getActiveBits();
706 if (n1 < n2)
707 return true;
708 else if (n2 < n1)
709 return false;
710 else if (n1 <= 64 && n2 <= 64)
711 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000712 for (int i = whichWord(n1 - 1); i >= 0; --i) {
713 if (pVal[i] > RHS.pVal[i]) return false;
714 else if (pVal[i] < RHS.pVal[i]) return true;
715 }
716 }
717 return false;
718}
719
Reid Spencere81d2da2007-02-16 22:36:51 +0000720/// @brief Signed less than comparison
721bool APInt::slt(const APInt& RHS) const {
722 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
723 if (isSingleWord())
724 return VAL < RHS.VAL;
725 else {
726 unsigned n1 = getActiveBits();
727 unsigned n2 = RHS.getActiveBits();
728 if (n1 < n2)
729 return true;
730 else if (n2 < n1)
731 return false;
732 else if (n1 <= 64 && n2 <= 64)
733 return pVal[0] < RHS.pVal[0];
734 for (int i = whichWord(n1 - 1); i >= 0; --i) {
735 if (pVal[i] > RHS.pVal[i]) return false;
736 else if (pVal[i] < RHS.pVal[i]) return true;
737 }
738 }
739 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740}
741
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000742/// Set the given bit to 1 whose poition is given as "bitPosition".
743/// @brief Set a given bit to 1.
744APInt& APInt::set(unsigned bitPosition) {
745 if (isSingleWord()) VAL |= maskBit(bitPosition);
746 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
747 return *this;
748}
749
750/// @brief Set every bit to 1.
751APInt& APInt::set() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000752 if (isSingleWord()) VAL = ~0ULL >> (64 - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000753 else {
754 for (unsigned i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000755 pVal[i] = -1ULL;
Reid Spencere81d2da2007-02-16 22:36:51 +0000756 pVal[getNumWords() - 1] = ~0ULL >> (64 - BitWidth % 64);
Zhou Shengb04973e2007-02-15 06:36:31 +0000757 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000758 return *this;
759}
760
761/// Set the given bit to 0 whose position is given as "bitPosition".
762/// @brief Set a given bit to 0.
763APInt& APInt::clear(unsigned bitPosition) {
764 if (isSingleWord()) VAL &= ~maskBit(bitPosition);
765 else pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
766 return *this;
767}
768
769/// @brief Set every bit to 0.
770APInt& APInt::clear() {
771 if (isSingleWord()) VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000772 else
773 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000774 return *this;
775}
776
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000777/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
778/// this APInt.
779APInt APInt::operator~() const {
780 APInt API(*this);
781 API.flip();
782 return API;
783}
784
785/// @brief Toggle every bit to its opposite value.
786APInt& APInt::flip() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000787 if (isSingleWord()) VAL = (~(VAL << (64 - BitWidth))) >> (64 - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000788 else {
789 unsigned i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000790 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000791 pVal[i] = ~pVal[i];
Reid Spencere81d2da2007-02-16 22:36:51 +0000792 unsigned offset = 64 - (BitWidth - 64 * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000793 pVal[i] = (~(pVal[i] << offset)) >> offset;
794 }
795 return *this;
796}
797
798/// Toggle a given bit to its opposite value whose position is given
799/// as "bitPosition".
800/// @brief Toggles a given bit to its opposite value.
801APInt& APInt::flip(unsigned bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000802 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000803 if ((*this)[bitPosition]) clear(bitPosition);
804 else set(bitPosition);
805 return *this;
806}
807
808/// to_string - This function translates the APInt into a string.
Reid Spencere81d2da2007-02-16 22:36:51 +0000809std::string APInt::toString(uint8_t radix) const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000810 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
811 "Radix should be 2, 8, 10, or 16!");
Reid Spencer879dfe12007-02-14 02:52:25 +0000812 static const char *digits[] = {
813 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
814 };
815 std::string result;
Reid Spencere81d2da2007-02-16 22:36:51 +0000816 unsigned bits_used = getActiveBits();
Reid Spencer879dfe12007-02-14 02:52:25 +0000817 if (isSingleWord()) {
818 char buf[65];
819 const char *format = (radix == 10 ? "%llu" :
820 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
821 if (format) {
822 sprintf(buf, format, VAL);
823 } else {
824 memset(buf, 0, 65);
825 uint64_t v = VAL;
826 while (bits_used) {
827 unsigned bit = v & 1;
828 bits_used--;
829 buf[bits_used] = digits[bit][0];
830 v >>=1;
831 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000832 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000833 result = buf;
834 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000835 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000836
837 APInt tmp(*this);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000838 APInt divisor(tmp.getBitWidth(), radix);
839 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer879dfe12007-02-14 02:52:25 +0000840 if (tmp == 0)
841 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000842 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000843 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spencer879dfe12007-02-14 02:52:25 +0000844 unsigned digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000845 assert(digit < radix && "urem failed");
Reid Spencer879dfe12007-02-14 02:52:25 +0000846 result.insert(0,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000847 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000848 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000849
850 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000851}
852
853/// getMaxValue - This function returns the largest value
854/// for an APInt of the specified bit-width and if isSign == true,
855/// it should be largest signed value, otherwise unsigned value.
856APInt APInt::getMaxValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000857 APInt APIVal(numBits, 0);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000858 APIVal.set();
Zhou Shengb04973e2007-02-15 06:36:31 +0000859 if (isSign) APIVal.clear(numBits - 1);
860 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000861}
862
863/// getMinValue - This function returns the smallest value for
864/// an APInt of the given bit-width and if isSign == true,
865/// it should be smallest signed value, otherwise zero.
866APInt APInt::getMinValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000867 APInt APIVal(numBits, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000868 if (isSign) APIVal.set(numBits - 1);
869 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000870}
871
872/// getAllOnesValue - This function returns an all-ones value for
873/// an APInt of the specified bit-width.
874APInt APInt::getAllOnesValue(unsigned numBits) {
875 return getMaxValue(numBits, false);
876}
877
878/// getNullValue - This function creates an '0' value for an
879/// APInt of the specified bit-width.
880APInt APInt::getNullValue(unsigned numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000881 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000882}
883
884/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000885APInt APInt::getHiBits(unsigned numBits) const {
886 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000887}
888
889/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000890APInt APInt::getLoBits(unsigned numBits) const {
891 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
892 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000893}
894
Reid Spencere81d2da2007-02-16 22:36:51 +0000895bool APInt::isPowerOf2() const {
896 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
897}
898
899/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000900/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000901/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000902/// the number of zeros from the most significant bit to the first one bit.
903/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000904unsigned APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000905 if (isSingleWord())
906 return CountLeadingZeros_64(VAL);
907 unsigned Count = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000908 for (int i = getNumWords() - 1; i >= 0; --i) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000909 unsigned tmp = CountLeadingZeros_64(pVal[i]);
910 Count += tmp;
911 if (tmp != 64)
912 break;
913 }
914 return Count;
915}
916
Reid Spencere81d2da2007-02-16 22:36:51 +0000917/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000918/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000919/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000920/// the number of zeros from the least significant bit to the first one bit.
921/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000922unsigned APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000923 if (isSingleWord())
924 return CountTrailingZeros_64(~VAL & (VAL - 1));
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000925 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000926 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000927}
928
Reid Spencere81d2da2007-02-16 22:36:51 +0000929/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000930/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000931/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000932/// @returns 0 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000933unsigned APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000934 if (isSingleWord())
935 return CountPopulation_64(VAL);
936 unsigned Count = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000937 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000938 Count += CountPopulation_64(pVal[i]);
939 return Count;
940}
941
942
Reid Spencere81d2da2007-02-16 22:36:51 +0000943/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000944/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000945APInt APInt::byteSwap() const {
946 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
947 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000948 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000949 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000950 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000951 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000952 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
953 Tmp1 = ByteSwap_32(Tmp1);
954 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
955 Tmp2 = ByteSwap_16(Tmp2);
956 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000957 APInt(BitWidth,
958 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000959 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000960 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000961 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000962 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000963 char *pByte = (char*)Result.pVal;
Reid Spencere81d2da2007-02-16 22:36:51 +0000964 for (unsigned i = 0; i < BitWidth / 8 / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000965 char Tmp = pByte[i];
Reid Spencere81d2da2007-02-16 22:36:51 +0000966 pByte[i] = pByte[BitWidth / 8 - 1 - i];
967 pByte[BitWidth / 8 - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000968 }
969 return Result;
970 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000971}
972
973/// GreatestCommonDivisor - This function returns the greatest common
974/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000975APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
976 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000977 APInt A = API1, B = API2;
978 while (!!B) {
979 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000980 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000981 A = T;
982 }
983 return A;
984}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000985
Zhou Shengd93f00c2007-02-12 20:02:55 +0000986/// DoubleRoundToAPInt - This function convert a double value to
987/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000988APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000989 union {
990 double D;
991 uint64_t I;
992 } T;
993 T.D = Double;
994 bool isNeg = T.I >> 63;
995 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
996 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000997 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000998 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
999 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +00001000 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
1001 APInt(64u, mantissa >> (52 - exp));
1002 APInt Tmp(exp + 1, mantissa);
1003 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +00001004 return isNeg ? -Tmp : Tmp;
1005}
1006
Reid Spencerdb3faa62007-02-13 22:41:58 +00001007/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +00001008/// The layout for double is as following (IEEE Standard 754):
1009/// --------------------------------------
1010/// | Sign Exponent Fraction Bias |
1011/// |-------------------------------------- |
1012/// | 1[63] 11[62-52] 52[51-00] 1023 |
1013/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +00001014double APInt::roundToDouble(bool isSigned) const {
1015 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +00001016 APInt Tmp(isNeg ? -(*this) : (*this));
1017 if (Tmp.isSingleWord())
1018 return isSigned ? double(int64_t(Tmp.VAL)) : double(Tmp.VAL);
Reid Spencere81d2da2007-02-16 22:36:51 +00001019 unsigned n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +00001020 if (n <= 64)
1021 return isSigned ? double(int64_t(Tmp.pVal[0])) : double(Tmp.pVal[0]);
1022 // Exponent when normalized to have decimal point directly after
1023 // leading one. This is stored excess 1023 in the exponent bit field.
1024 uint64_t exp = n - 1;
1025
1026 // Gross overflow.
1027 assert(exp <= 1023 && "Infinity value!");
1028
1029 // Number of bits in mantissa including the leading one
1030 // equals to 53.
1031 uint64_t mantissa;
1032 if (n % 64 >= 53)
1033 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % 64 - 53);
1034 else
1035 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % 64)) |
1036 (Tmp.pVal[whichWord(n - 1) - 1] >> (11 + n % 64));
1037 // The leading bit of mantissa is implicit, so get rid of it.
1038 mantissa &= ~(1ULL << 52);
1039 uint64_t sign = isNeg ? (1ULL << 63) : 0;
1040 exp += 1023;
1041 union {
1042 double D;
1043 uint64_t I;
1044 } T;
1045 T.I = sign | (exp << 52) | mantissa;
1046 return T.D;
1047}
1048
Reid Spencere81d2da2007-02-16 22:36:51 +00001049// Truncate to new width.
1050void APInt::trunc(unsigned width) {
1051 assert(width < BitWidth && "Invalid APInt Truncate request");
1052}
1053
1054// Sign extend to a new width.
1055void APInt::sext(unsigned width) {
1056 assert(width > BitWidth && "Invalid APInt SignExtend request");
1057}
1058
1059// Zero extend to a new width.
1060void APInt::zext(unsigned width) {
1061 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
1062}
1063
Zhou Shengff4304f2007-02-09 07:48:24 +00001064/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001065/// @brief Arithmetic right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +00001066APInt APInt::ashr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +00001067 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001068 if (API.isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +00001069 API.VAL = (((int64_t(API.VAL) << (64 - API.BitWidth)) >> (64 - API.BitWidth))
1070 >> shiftAmt) & (~uint64_t(0UL) >> (64 - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +00001071 else {
Reid Spencere81d2da2007-02-16 22:36:51 +00001072 if (shiftAmt >= API.BitWidth) {
1073 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0, (API.getNumWords()-1) * 8);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001074 API.pVal[API.getNumWords() - 1] = ~uint64_t(0UL) >>
Reid Spencere81d2da2007-02-16 22:36:51 +00001075 (64 - API.BitWidth % 64);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001076 } else {
1077 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +00001078 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +00001079 if (API[i+shiftAmt])
1080 API.set(i);
1081 else
1082 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +00001083 for (; i < API.BitWidth; ++i)
1084 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +00001085 API.set(i);
1086 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001087 }
1088 }
1089 return API;
1090}
1091
Zhou Shengff4304f2007-02-09 07:48:24 +00001092/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001093/// @brief Logical right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +00001094APInt APInt::lshr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +00001095 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001096 if (API.isSingleWord())
1097 API.VAL >>= shiftAmt;
1098 else {
Reid Spencere81d2da2007-02-16 22:36:51 +00001099 if (shiftAmt >= API.BitWidth)
Zhou Sheng0b706b12007-02-08 14:35:19 +00001100 memset(API.pVal, 0, API.getNumWords() * 8);
1101 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +00001102 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +00001103 if (API[i+shiftAmt]) API.set(i);
1104 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +00001105 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +00001106 API.clear(i);
1107 }
1108 return API;
1109}
1110
Zhou Shengff4304f2007-02-09 07:48:24 +00001111/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001112/// @brief Left-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +00001113APInt APInt::shl(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +00001114 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +00001115 if (API.isSingleWord())
1116 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +00001117 else if (shiftAmt >= API.BitWidth)
Zhou Shengd93f00c2007-02-12 20:02:55 +00001118 memset(API.pVal, 0, API.getNumWords() * 8);
1119 else {
1120 if (unsigned offset = shiftAmt / 64) {
1121 for (unsigned i = API.getNumWords() - 1; i > offset - 1; --i)
1122 API.pVal[i] = API.pVal[i-offset];
1123 memset(API.pVal, 0, offset * 8);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001124 }
Zhou Shengd93f00c2007-02-12 20:02:55 +00001125 shiftAmt %= 64;
1126 unsigned i;
1127 for (i = API.getNumWords() - 1; i > 0; --i)
1128 API.pVal[i] = (API.pVal[i] << shiftAmt) |
1129 (API.pVal[i-1] >> (64-shiftAmt));
1130 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001131 }
Reid Spencere81d2da2007-02-16 22:36:51 +00001132 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001133 return API;
1134}
1135
Zhou Shengff4304f2007-02-09 07:48:24 +00001136/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001137/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001138APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001139 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengff4304f2007-02-09 07:48:24 +00001140 APInt API(*this);
Reid Spencere81d2da2007-02-16 22:36:51 +00001141 unsigned first = RHS.getActiveBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001142 unsigned ylen = !first ? 0 : APInt::whichWord(first - 1) + 1;
1143 assert(ylen && "Divided by zero???");
1144 if (API.isSingleWord()) {
1145 API.VAL = RHS.isSingleWord() ? (API.VAL / RHS.VAL) :
1146 (ylen > 1 ? 0 : API.VAL / RHS.pVal[0]);
1147 } else {
Reid Spencere81d2da2007-02-16 22:36:51 +00001148 unsigned first2 = API.getActiveBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001149 unsigned xlen = !first2 ? 0 : APInt::whichWord(first2 - 1) + 1;
1150 if (!xlen)
1151 return API;
Reid Spencere81d2da2007-02-16 22:36:51 +00001152 else if (xlen < ylen || API.ult(RHS))
Zhou Sheng0b706b12007-02-08 14:35:19 +00001153 memset(API.pVal, 0, API.getNumWords() * 8);
1154 else if (API == RHS) {
1155 memset(API.pVal, 0, API.getNumWords() * 8);
1156 API.pVal[0] = 1;
1157 } else if (xlen == 1)
1158 API.pVal[0] /= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
1159 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001160 APInt X(BitWidth, 0);
1161 APInt Y(BitWidth, 0);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001162 if (unsigned nshift = 63 - (first - 1) % 64) {
Reid Spencere81d2da2007-02-16 22:36:51 +00001163 Y = APIntOps::shl(RHS, nshift);
1164 X = APIntOps::shl(API, nshift);
Zhou Shengb04973e2007-02-15 06:36:31 +00001165 ++xlen;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001166 }
Zhou Shengb04973e2007-02-15 06:36:31 +00001167 div((unsigned*)X.pVal, xlen*2-1,
1168 (unsigned*)(Y.isSingleWord() ? &Y.VAL : Y.pVal), ylen*2);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001169 memset(API.pVal, 0, API.getNumWords() * 8);
Zhou Shengb04973e2007-02-15 06:36:31 +00001170 memcpy(API.pVal, X.pVal + ylen, (xlen - ylen) * 8);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001171 }
1172 }
1173 return API;
1174}
1175
1176/// Unsigned remainder operation on APInt.
1177/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001178APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001179 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengff4304f2007-02-09 07:48:24 +00001180 APInt API(*this);
Reid Spencere81d2da2007-02-16 22:36:51 +00001181 unsigned first = RHS.getActiveBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001182 unsigned ylen = !first ? 0 : APInt::whichWord(first - 1) + 1;
1183 assert(ylen && "Performing remainder operation by zero ???");
1184 if (API.isSingleWord()) {
1185 API.VAL = RHS.isSingleWord() ? (API.VAL % RHS.VAL) :
1186 (ylen > 1 ? API.VAL : API.VAL % RHS.pVal[0]);
1187 } else {
Reid Spencere81d2da2007-02-16 22:36:51 +00001188 unsigned first2 = API.getActiveBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001189 unsigned xlen = !first2 ? 0 : API.whichWord(first2 - 1) + 1;
Reid Spencere81d2da2007-02-16 22:36:51 +00001190 if (!xlen || xlen < ylen || API.ult(RHS))
Zhou Sheng0b706b12007-02-08 14:35:19 +00001191 return API;
Zhou Shengb04973e2007-02-15 06:36:31 +00001192 else if (API == RHS)
Zhou Sheng0b706b12007-02-08 14:35:19 +00001193 memset(API.pVal, 0, API.getNumWords() * 8);
1194 else if (xlen == 1)
1195 API.pVal[0] %= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
1196 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001197 APInt X((xlen+1)*64, 0), Y(ylen*64, 0);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001198 unsigned nshift = 63 - (first - 1) % 64;
1199 if (nshift) {
Reid Spencere81d2da2007-02-16 22:36:51 +00001200 APIntOps::shl(Y, nshift);
1201 APIntOps::shl(X, nshift);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001202 }
Zhou Shengb04973e2007-02-15 06:36:31 +00001203 div((unsigned*)X.pVal, xlen*2-1,
1204 (unsigned*)(Y.isSingleWord() ? &Y.VAL : Y.pVal), ylen*2);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001205 memset(API.pVal, 0, API.getNumWords() * 8);
1206 for (unsigned i = 0; i < ylen-1; ++i)
Zhou Shengb04973e2007-02-15 06:36:31 +00001207 API.pVal[i] = (X.pVal[i] >> nshift) | (X.pVal[i+1] << (64 - nshift));
1208 API.pVal[ylen-1] = X.pVal[ylen-1] >> nshift;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001209 }
1210 }
1211 return API;
1212}