blob: 69cbdc7a35e1ccd373acdd5a163eef59e352222b [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) {
29 unsigned count = 64 - shiftAmt;
30 int i = len - 1;
31 uint64_t high_word = x[i], retVal = high_word >> count;
32 ++d_offset;
33 while (--i >= 0) {
34 uint64_t low_word = x[i];
35 dest[d_offset+i] = (high_word << shiftAmt) | (low_word >> count);
36 high_word = low_word;
37 }
38 dest[d_offset+i] = high_word << shiftAmt;
39 return retVal;
40}
Reid 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 Spencere81d2da2007-02-16 22:36:51 +000073 pVal[minN-1] = bigVal[minN-1] & (~uint64_t(0ULL) >> (64 - BitWidth % 64));
Zhou Shenga3832fd2007-02-07 06:14:53 +000074 if (maxN == getNumWords())
Reid Spencere81d2da2007-02-16 22:36:51 +000075 memset(pVal+numWords, 0, (getNumWords() - numWords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000076 }
77}
78
Zhou Shenga3832fd2007-02-07 06:14:53 +000079/// @brief Create a new APInt by translating the char array represented
80/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +000081APInt::APInt(unsigned numbits, const char StrStart[], unsigned slen,
82 uint8_t radix) {
83 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000084}
85
86/// @brief Create a new APInt by translating the string represented
87/// integer value.
Reid Spencere81d2da2007-02-16 22:36:51 +000088APInt::APInt(unsigned numbits, const std::string& Val, uint8_t radix) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000089 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000090 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000091}
92
Zhou Shengfd43dcf2007-02-06 03:00:16 +000093APInt::APInt(const APInt& APIVal)
Reid Spencere81d2da2007-02-16 22:36:51 +000094 : BitWidth(APIVal.BitWidth) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +000095 if (isSingleWord()) VAL = APIVal.VAL;
96 else {
97 // Memory allocation and check if successful.
Zhou Shenga3832fd2007-02-07 06:14:53 +000098 assert((pVal = new uint64_t[getNumWords()]) &&
Zhou Shengfd43dcf2007-02-06 03:00:16 +000099 "APInt memory allocation fails!");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000100 memcpy(pVal, APIVal.pVal, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000101 }
102}
103
104APInt::~APInt() {
105 if (!isSingleWord() && pVal) delete[] pVal;
106}
107
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000108/// @brief Copy assignment operator. Create a new object from the given
109/// APInt one by initialization.
110APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000111 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
112 if (isSingleWord())
113 VAL = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000114 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000115 unsigned minN = std::min(getNumWords(), RHS.getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000116 memcpy(pVal, RHS.isSingleWord() ? &RHS.VAL : RHS.pVal, minN * 8);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000117 if (getNumWords() != minN)
118 memset(pVal + minN, 0, (getNumWords() - minN) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000119 }
120 return *this;
121}
122
123/// @brief Assignment operator. Assigns a common case integer value to
124/// the APInt.
125APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000126 if (isSingleWord())
127 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000128 else {
129 pVal[0] = RHS;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000130 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000131 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000132 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000133 return *this;
134}
135
Reid Spencer5e0a8512007-02-17 03:16:00 +0000136/// add_1 - This function adds the integer array x[] by integer y and
137/// returns the carry.
138/// @returns the carry of the addition.
139static uint64_t add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
140 uint64_t carry = y;
141
142 for (unsigned i = 0; i < len; ++i) {
143 dest[i] = carry + x[i];
144 carry = (dest[i] < carry) ? 1 : 0;
145 }
146 return carry;
147}
148
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000149/// @brief Prefix increment operator. Increments the APInt by one.
150APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000151 if (isSingleWord())
152 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000153 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000154 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000155 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000156 return *this;
157}
158
Reid Spencer5e0a8512007-02-17 03:16:00 +0000159/// sub_1 - This function subtracts the integer array x[] by
160/// integer y and returns the borrow-out carry.
161static uint64_t sub_1(uint64_t x[], unsigned len, uint64_t y) {
162 uint64_t cy = y;
163
164 for (unsigned i = 0; i < len; ++i) {
165 uint64_t X = x[i];
166 x[i] -= cy;
167 if (cy > X)
168 cy = 1;
169 else {
170 cy = 0;
171 break;
172 }
173 }
174
175 return cy;
176}
177
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000178/// @brief Prefix decrement operator. Decrements the APInt by one.
179APInt& APInt::operator--() {
180 if (isSingleWord()) --VAL;
181 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000182 sub_1(pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000183 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000184 return *this;
185}
186
Reid Spencer5e0a8512007-02-17 03:16:00 +0000187/// add - This function adds the integer array x[] by integer array
188/// y[] and returns the carry.
189static uint64_t add(uint64_t dest[], uint64_t x[],
190 uint64_t y[], unsigned len) {
191 unsigned carry = 0;
192
193 for (unsigned i = 0; i< len; ++i) {
194 carry += x[i];
195 dest[i] = carry + y[i];
196 carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0);
197 }
198 return carry;
199}
200
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201/// @brief Addition assignment operator. Adds this APInt by the given APInt&
202/// RHS and assigns the result to this APInt.
203APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000204 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000205 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
206 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000207 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000208 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000209 if (getNumWords() <= RHS.getNumWords())
210 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000211 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000212 uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.getNumWords());
213 add_1(pVal + RHS.getNumWords(), pVal + RHS.getNumWords(),
214 getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000215 }
216 }
217 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000218 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000219 return *this;
220}
221
Reid Spencer5e0a8512007-02-17 03:16:00 +0000222/// sub - This function subtracts the integer array x[] by
223/// integer array y[], and returns the borrow-out carry.
224static uint64_t sub(uint64_t dest[], uint64_t x[],
225 uint64_t y[], unsigned len) {
226 // Carry indicator.
227 uint64_t cy = 0;
228
229 for (unsigned i = 0; i < len; ++i) {
230 uint64_t Y = y[i], X = x[i];
231 Y += cy;
232
233 cy = Y < cy ? 1 : 0;
234 Y = X - Y;
235 cy += Y > X ? 1 : 0;
236 dest[i] = Y;
237 }
238 return cy;
239}
240
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000241/// @brief Subtraction assignment operator. Subtracts this APInt by the given
242/// APInt &RHS and assigns the result to this APInt.
243APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000244 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000245 if (isSingleWord())
246 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
247 else {
248 if (RHS.isSingleWord())
Zhou Shenga3832fd2007-02-07 06:14:53 +0000249 sub_1(pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000250 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000251 if (RHS.getNumWords() < getNumWords()) {
252 uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
253 sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000254 }
255 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000256 sub(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000257 }
258 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000259 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000260 return *this;
261}
262
Reid Spencer5e0a8512007-02-17 03:16:00 +0000263/// mul_1 - This function performs the multiplication operation on a
264/// large integer (represented as an integer array) and a uint64_t integer.
265/// @returns the carry of the multiplication.
266static uint64_t mul_1(uint64_t dest[], uint64_t x[],
267 unsigned len, uint64_t y) {
268 // Split y into high 32-bit part and low 32-bit part.
269 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
270 uint64_t carry = 0, lx, hx;
271 for (unsigned i = 0; i < len; ++i) {
272 lx = x[i] & 0xffffffffULL;
273 hx = x[i] >> 32;
274 // hasCarry - A flag to indicate if has carry.
275 // hasCarry == 0, no carry
276 // hasCarry == 1, has carry
277 // hasCarry == 2, no carry and the calculation result == 0.
278 uint8_t hasCarry = 0;
279 dest[i] = carry + lx * ly;
280 // Determine if the add above introduces carry.
281 hasCarry = (dest[i] < carry) ? 1 : 0;
282 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
283 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
284 // (2^32 - 1) + 2^32 = 2^64.
285 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
286
287 carry += (lx * hy) & 0xffffffffULL;
288 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
289 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
290 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
291 }
292
293 return carry;
294}
295
296/// mul - This function multiplies integer array x[] by integer array y[] and
297/// stores the result into integer array dest[].
298/// Note the array dest[]'s size should no less than xlen + ylen.
299static void mul(uint64_t dest[], uint64_t x[], unsigned xlen,
300 uint64_t y[], unsigned ylen) {
301 dest[xlen] = mul_1(dest, x, xlen, y[0]);
302
303 for (unsigned i = 1; i < ylen; ++i) {
304 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
305 uint64_t carry = 0, lx, hx;
306 for (unsigned j = 0; j < xlen; ++j) {
307 lx = x[j] & 0xffffffffULL;
308 hx = x[j] >> 32;
309 // hasCarry - A flag to indicate if has carry.
310 // hasCarry == 0, no carry
311 // hasCarry == 1, has carry
312 // hasCarry == 2, no carry and the calculation result == 0.
313 uint8_t hasCarry = 0;
314 uint64_t resul = carry + lx * ly;
315 hasCarry = (resul < carry) ? 1 : 0;
316 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
317 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
318
319 carry += (lx * hy) & 0xffffffffULL;
320 resul = (carry << 32) | (resul & 0xffffffffULL);
321 dest[i+j] += resul;
322 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
323 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
324 ((lx * hy) >> 32) + hx * hy;
325 }
326 dest[i+xlen] = carry;
327 }
328}
329
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000330/// @brief Multiplication assignment operator. Multiplies this APInt by the
331/// given APInt& RHS and assigns the result to this APInt.
332APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000333 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000334 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
335 else {
336 // one-based first non-zero bit position.
Reid Spencere81d2da2007-02-16 22:36:51 +0000337 unsigned first = getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000338 unsigned xlen = !first ? 0 : whichWord(first - 1) + 1;
339 if (!xlen)
340 return *this;
341 else if (RHS.isSingleWord())
342 mul_1(pVal, pVal, xlen, RHS.VAL);
343 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000344 first = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000345 unsigned ylen = !first ? 0 : whichWord(first - 1) + 1;
346 if (!ylen) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000347 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000348 return *this;
349 }
350 uint64_t *dest = new uint64_t[xlen+ylen];
351 assert(dest && "Memory Allocation Failed!");
352 mul(dest, pVal, xlen, RHS.pVal, ylen);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000353 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ?
354 getNumWords() : xlen + ylen) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000355 delete[] dest;
356 }
357 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000358 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000359 return *this;
360}
361
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000362/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
363/// this APInt and the given APInt& RHS, assigns the result to this APInt.
364APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000365 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000366 if (isSingleWord()) {
367 if (RHS.isSingleWord()) VAL &= RHS.VAL;
368 else VAL &= RHS.pVal[0];
369 } else {
370 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000371 memset(pVal, 0, (getNumWords() - 1) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000372 pVal[0] &= RHS.VAL;
373 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000374 unsigned minwords = getNumWords() < RHS.getNumWords() ?
375 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000376 for (unsigned i = 0; i < minwords; ++i)
377 pVal[i] &= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000378 if (getNumWords() > minwords)
379 memset(pVal+minwords, 0, (getNumWords() - minwords) * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000380 }
381 }
382 return *this;
383}
384
385/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on
386/// this APInt and the given APInt& RHS, assigns the result to this APInt.
387APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000388 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000389 if (isSingleWord()) {
390 if (RHS.isSingleWord()) VAL |= RHS.VAL;
391 else VAL |= RHS.pVal[0];
392 } else {
393 if (RHS.isSingleWord()) {
394 pVal[0] |= RHS.VAL;
395 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000396 unsigned minwords = getNumWords() < RHS.getNumWords() ?
397 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000398 for (unsigned i = 0; i < minwords; ++i)
399 pVal[i] |= RHS.pVal[i];
400 }
401 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000402 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000403 return *this;
404}
405
406/// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on
407/// this APInt and the given APInt& RHS, assigns the result to this APInt.
408APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000409 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000410 if (isSingleWord()) {
411 if (RHS.isSingleWord()) VAL ^= RHS.VAL;
412 else VAL ^= RHS.pVal[0];
413 } else {
414 if (RHS.isSingleWord()) {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000415 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000416 pVal[i] ^= RHS.VAL;
417 } else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000418 unsigned minwords = getNumWords() < RHS.getNumWords() ?
419 getNumWords() : RHS.getNumWords();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000420 for (unsigned i = 0; i < minwords; ++i)
421 pVal[i] ^= RHS.pVal[i];
Zhou Shenga3832fd2007-02-07 06:14:53 +0000422 if (getNumWords() > minwords)
423 for (unsigned i = minwords; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000424 pVal[i] ^= 0;
425 }
426 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000427 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000428 return *this;
429}
430
431/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
432/// and the given APInt& RHS.
433APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000434 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000435 APInt API(RHS);
436 return API &= *this;
437}
438
439/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt
440/// and the given APInt& RHS.
441APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000442 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000443 APInt API(RHS);
444 API |= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000445 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000446 return API;
447}
448
449/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
450/// and the given APInt& RHS.
451APInt APInt::operator^(const APInt& RHS) const {
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 APInt API(RHS);
454 API ^= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000455 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000456 return API;
457}
458
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000459
460/// @brief Logical negation operator. Performs logical negation operation on
461/// this APInt.
462bool APInt::operator !() const {
463 if (isSingleWord())
464 return !VAL;
465 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000466 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000467 if (pVal[i])
468 return false;
469 return true;
470}
471
472/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
473/// RHS.
474APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000475 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000476 APInt API(RHS);
477 API *= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000478 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000479 return API;
480}
481
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000482/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
483APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000484 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000485 APInt API(*this);
486 API += RHS;
Reid Spencere81d2da2007-02-16 22:36:51 +0000487 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000488 return API;
489}
490
491/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
492APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000493 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000494 APInt API(*this);
495 API -= RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000496 return API;
497}
498
499/// @brief Array-indexing support.
500bool APInt::operator[](unsigned bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000501 return (maskBit(bitPosition) & (isSingleWord() ?
502 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000503}
504
505/// @brief Equality operator. Compare this APInt with the given APInt& RHS
506/// for the validity of the equality relationship.
507bool APInt::operator==(const APInt& RHS) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000508 unsigned n1 = getActiveBits();
509 unsigned n2 = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000510 if (n1 != n2) return false;
511 else if (isSingleWord())
512 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
513 else {
514 if (n1 <= 64)
515 return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
516 for (int i = whichWord(n1 - 1); i >= 0; --i)
517 if (pVal[i] != RHS.pVal[i]) return false;
518 }
519 return true;
520}
521
Zhou Shenga3832fd2007-02-07 06:14:53 +0000522/// @brief Equality operator. Compare this APInt with the given uint64_t value
523/// for the validity of the equality relationship.
524bool APInt::operator==(uint64_t Val) const {
525 if (isSingleWord())
526 return VAL == Val;
527 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000528 unsigned n = getActiveBits();
Zhou Shenga3832fd2007-02-07 06:14:53 +0000529 if (n <= 64)
530 return pVal[0] == Val;
531 else
532 return false;
533 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000534}
535
Reid Spencere81d2da2007-02-16 22:36:51 +0000536/// @brief Unsigned less than comparison
537bool APInt::ult(const APInt& RHS) const {
538 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
539 if (isSingleWord())
540 return VAL < RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000541 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000542 unsigned n1 = getActiveBits();
543 unsigned n2 = RHS.getActiveBits();
544 if (n1 < n2)
545 return true;
546 else if (n2 < n1)
547 return false;
548 else if (n1 <= 64 && n2 <= 64)
549 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000550 for (int i = whichWord(n1 - 1); i >= 0; --i) {
551 if (pVal[i] > RHS.pVal[i]) return false;
552 else if (pVal[i] < RHS.pVal[i]) return true;
553 }
554 }
555 return false;
556}
557
Reid Spencere81d2da2007-02-16 22:36:51 +0000558/// @brief Signed less than comparison
559bool APInt::slt(const APInt& RHS) const {
560 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
561 if (isSingleWord())
562 return VAL < RHS.VAL;
563 else {
564 unsigned n1 = getActiveBits();
565 unsigned n2 = RHS.getActiveBits();
566 if (n1 < n2)
567 return true;
568 else if (n2 < n1)
569 return false;
570 else if (n1 <= 64 && n2 <= 64)
571 return pVal[0] < RHS.pVal[0];
572 for (int i = whichWord(n1 - 1); i >= 0; --i) {
573 if (pVal[i] > RHS.pVal[i]) return false;
574 else if (pVal[i] < RHS.pVal[i]) return true;
575 }
576 }
577 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000578}
579
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000580/// Set the given bit to 1 whose poition is given as "bitPosition".
581/// @brief Set a given bit to 1.
582APInt& APInt::set(unsigned bitPosition) {
583 if (isSingleWord()) VAL |= maskBit(bitPosition);
584 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
585 return *this;
586}
587
588/// @brief Set every bit to 1.
589APInt& APInt::set() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000590 if (isSingleWord()) VAL = ~0ULL >> (64 - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000591 else {
592 for (unsigned i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000593 pVal[i] = -1ULL;
Reid Spencere81d2da2007-02-16 22:36:51 +0000594 pVal[getNumWords() - 1] = ~0ULL >> (64 - BitWidth % 64);
Zhou Shengb04973e2007-02-15 06:36:31 +0000595 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000596 return *this;
597}
598
599/// Set the given bit to 0 whose position is given as "bitPosition".
600/// @brief Set a given bit to 0.
601APInt& APInt::clear(unsigned bitPosition) {
602 if (isSingleWord()) VAL &= ~maskBit(bitPosition);
603 else pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
604 return *this;
605}
606
607/// @brief Set every bit to 0.
608APInt& APInt::clear() {
609 if (isSingleWord()) VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000610 else
611 memset(pVal, 0, getNumWords() * 8);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000612 return *this;
613}
614
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000615/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
616/// this APInt.
617APInt APInt::operator~() const {
618 APInt API(*this);
619 API.flip();
620 return API;
621}
622
623/// @brief Toggle every bit to its opposite value.
624APInt& APInt::flip() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000625 if (isSingleWord()) VAL = (~(VAL << (64 - BitWidth))) >> (64 - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000626 else {
627 unsigned i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000628 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000629 pVal[i] = ~pVal[i];
Reid Spencere81d2da2007-02-16 22:36:51 +0000630 unsigned offset = 64 - (BitWidth - 64 * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000631 pVal[i] = (~(pVal[i] << offset)) >> offset;
632 }
633 return *this;
634}
635
636/// Toggle a given bit to its opposite value whose position is given
637/// as "bitPosition".
638/// @brief Toggles a given bit to its opposite value.
639APInt& APInt::flip(unsigned bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000640 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000641 if ((*this)[bitPosition]) clear(bitPosition);
642 else set(bitPosition);
643 return *this;
644}
645
646/// to_string - This function translates the APInt into a string.
Reid Spencere81d2da2007-02-16 22:36:51 +0000647std::string APInt::toString(uint8_t radix) const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000648 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
649 "Radix should be 2, 8, 10, or 16!");
Reid Spencer879dfe12007-02-14 02:52:25 +0000650 static const char *digits[] = {
651 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
652 };
653 std::string result;
Reid Spencere81d2da2007-02-16 22:36:51 +0000654 unsigned bits_used = getActiveBits();
Reid Spencer879dfe12007-02-14 02:52:25 +0000655 if (isSingleWord()) {
656 char buf[65];
657 const char *format = (radix == 10 ? "%llu" :
658 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
659 if (format) {
660 sprintf(buf, format, VAL);
661 } else {
662 memset(buf, 0, 65);
663 uint64_t v = VAL;
664 while (bits_used) {
665 unsigned bit = v & 1;
666 bits_used--;
667 buf[bits_used] = digits[bit][0];
668 v >>=1;
669 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000670 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000671 result = buf;
672 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000673 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000674
675 APInt tmp(*this);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000676 APInt divisor(tmp.getBitWidth(), radix);
677 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer879dfe12007-02-14 02:52:25 +0000678 if (tmp == 0)
679 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000680 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000681 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spencer879dfe12007-02-14 02:52:25 +0000682 unsigned digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000683 assert(digit < radix && "urem failed");
Reid Spencer879dfe12007-02-14 02:52:25 +0000684 result.insert(0,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000685 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000686 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000687
688 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000689}
690
691/// getMaxValue - This function returns the largest value
692/// for an APInt of the specified bit-width and if isSign == true,
693/// it should be largest signed value, otherwise unsigned value.
694APInt APInt::getMaxValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000695 APInt APIVal(numBits, 0);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000696 APIVal.set();
Zhou Shengb04973e2007-02-15 06:36:31 +0000697 if (isSign) APIVal.clear(numBits - 1);
698 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000699}
700
701/// getMinValue - This function returns the smallest value for
702/// an APInt of the given bit-width and if isSign == true,
703/// it should be smallest signed value, otherwise zero.
704APInt APInt::getMinValue(unsigned numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000705 APInt APIVal(numBits, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000706 if (isSign) APIVal.set(numBits - 1);
707 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000708}
709
710/// getAllOnesValue - This function returns an all-ones value for
711/// an APInt of the specified bit-width.
712APInt APInt::getAllOnesValue(unsigned numBits) {
713 return getMaxValue(numBits, false);
714}
715
716/// getNullValue - This function creates an '0' value for an
717/// APInt of the specified bit-width.
718APInt APInt::getNullValue(unsigned numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000719 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000720}
721
722/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000723APInt APInt::getHiBits(unsigned numBits) const {
724 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000725}
726
727/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000728APInt APInt::getLoBits(unsigned numBits) const {
729 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
730 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000731}
732
Reid Spencere81d2da2007-02-16 22:36:51 +0000733bool APInt::isPowerOf2() const {
734 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
735}
736
737/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000738/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000739/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740/// the number of zeros from the most significant bit to the first one bit.
741/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000742unsigned APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000743 if (isSingleWord())
744 return CountLeadingZeros_64(VAL);
745 unsigned Count = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000746 for (int i = getNumWords() - 1; i >= 0; --i) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000747 unsigned tmp = CountLeadingZeros_64(pVal[i]);
748 Count += tmp;
749 if (tmp != 64)
750 break;
751 }
752 return Count;
753}
754
Reid Spencere81d2da2007-02-16 22:36:51 +0000755/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000756/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000757/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000758/// the number of zeros from the least significant bit to the first one bit.
759/// @returns numWord() * 64 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000760unsigned APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000761 if (isSingleWord())
762 return CountTrailingZeros_64(~VAL & (VAL - 1));
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000763 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000764 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000765}
766
Reid Spencere81d2da2007-02-16 22:36:51 +0000767/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000768/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000769/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000770/// @returns 0 if the value is zero.
Reid Spencere81d2da2007-02-16 22:36:51 +0000771unsigned APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000772 if (isSingleWord())
773 return CountPopulation_64(VAL);
774 unsigned Count = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000775 for (unsigned i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000776 Count += CountPopulation_64(pVal[i]);
777 return Count;
778}
779
780
Reid Spencere81d2da2007-02-16 22:36:51 +0000781/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000782/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000783APInt APInt::byteSwap() const {
784 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
785 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000786 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000787 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000788 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000789 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000790 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
791 Tmp1 = ByteSwap_32(Tmp1);
792 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
793 Tmp2 = ByteSwap_16(Tmp2);
794 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000795 APInt(BitWidth,
796 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000797 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000798 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000799 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000800 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000801 char *pByte = (char*)Result.pVal;
Reid Spencere81d2da2007-02-16 22:36:51 +0000802 for (unsigned i = 0; i < BitWidth / 8 / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000803 char Tmp = pByte[i];
Reid Spencere81d2da2007-02-16 22:36:51 +0000804 pByte[i] = pByte[BitWidth / 8 - 1 - i];
805 pByte[BitWidth / 8 - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000806 }
807 return Result;
808 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000809}
810
811/// GreatestCommonDivisor - This function returns the greatest common
812/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000813APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
814 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000815 APInt A = API1, B = API2;
816 while (!!B) {
817 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000818 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000819 A = T;
820 }
821 return A;
822}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000823
Zhou Shengd93f00c2007-02-12 20:02:55 +0000824/// DoubleRoundToAPInt - This function convert a double value to
825/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000826APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000827 union {
828 double D;
829 uint64_t I;
830 } T;
831 T.D = Double;
832 bool isNeg = T.I >> 63;
833 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
834 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000835 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000836 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
837 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000838 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
839 APInt(64u, mantissa >> (52 - exp));
840 APInt Tmp(exp + 1, mantissa);
841 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000842 return isNeg ? -Tmp : Tmp;
843}
844
Reid Spencerdb3faa62007-02-13 22:41:58 +0000845/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000846/// The layout for double is as following (IEEE Standard 754):
847/// --------------------------------------
848/// | Sign Exponent Fraction Bias |
849/// |-------------------------------------- |
850/// | 1[63] 11[62-52] 52[51-00] 1023 |
851/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000852double APInt::roundToDouble(bool isSigned) const {
853 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000854 APInt Tmp(isNeg ? -(*this) : (*this));
855 if (Tmp.isSingleWord())
856 return isSigned ? double(int64_t(Tmp.VAL)) : double(Tmp.VAL);
Reid Spencere81d2da2007-02-16 22:36:51 +0000857 unsigned n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000858 if (n <= 64)
859 return isSigned ? double(int64_t(Tmp.pVal[0])) : double(Tmp.pVal[0]);
860 // Exponent when normalized to have decimal point directly after
861 // leading one. This is stored excess 1023 in the exponent bit field.
862 uint64_t exp = n - 1;
863
864 // Gross overflow.
865 assert(exp <= 1023 && "Infinity value!");
866
867 // Number of bits in mantissa including the leading one
868 // equals to 53.
869 uint64_t mantissa;
870 if (n % 64 >= 53)
871 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % 64 - 53);
872 else
873 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % 64)) |
874 (Tmp.pVal[whichWord(n - 1) - 1] >> (11 + n % 64));
875 // The leading bit of mantissa is implicit, so get rid of it.
876 mantissa &= ~(1ULL << 52);
877 uint64_t sign = isNeg ? (1ULL << 63) : 0;
878 exp += 1023;
879 union {
880 double D;
881 uint64_t I;
882 } T;
883 T.I = sign | (exp << 52) | mantissa;
884 return T.D;
885}
886
Reid Spencere81d2da2007-02-16 22:36:51 +0000887// Truncate to new width.
888void APInt::trunc(unsigned width) {
889 assert(width < BitWidth && "Invalid APInt Truncate request");
890}
891
892// Sign extend to a new width.
893void APInt::sext(unsigned width) {
894 assert(width > BitWidth && "Invalid APInt SignExtend request");
895}
896
897// Zero extend to a new width.
898void APInt::zext(unsigned width) {
899 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
900}
901
Zhou Shengff4304f2007-02-09 07:48:24 +0000902/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000903/// @brief Arithmetic right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000904APInt APInt::ashr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000905 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000906 if (API.isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +0000907 API.VAL = (((int64_t(API.VAL) << (64 - API.BitWidth)) >> (64 - API.BitWidth))
908 >> shiftAmt) & (~uint64_t(0UL) >> (64 - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000909 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000910 if (shiftAmt >= API.BitWidth) {
911 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0, (API.getNumWords()-1) * 8);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000912 API.pVal[API.getNumWords() - 1] = ~uint64_t(0UL) >>
Reid Spencere81d2da2007-02-16 22:36:51 +0000913 (64 - API.BitWidth % 64);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000914 } else {
915 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000916 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000917 if (API[i+shiftAmt])
918 API.set(i);
919 else
920 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000921 for (; i < API.BitWidth; ++i)
922 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000923 API.set(i);
924 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000925 }
926 }
927 return API;
928}
929
Zhou Shengff4304f2007-02-09 07:48:24 +0000930/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000931/// @brief Logical right-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000932APInt APInt::lshr(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000933 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000934 if (API.isSingleWord())
935 API.VAL >>= shiftAmt;
936 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000937 if (shiftAmt >= API.BitWidth)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000938 memset(API.pVal, 0, API.getNumWords() * 8);
939 unsigned i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000940 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000941 if (API[i+shiftAmt]) API.set(i);
942 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000943 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000944 API.clear(i);
945 }
946 return API;
947}
948
Zhou Shengff4304f2007-02-09 07:48:24 +0000949/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000950/// @brief Left-shift function.
Reid Spencere81d2da2007-02-16 22:36:51 +0000951APInt APInt::shl(unsigned shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000952 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000953 if (API.isSingleWord())
954 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000955 else if (shiftAmt >= API.BitWidth)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000956 memset(API.pVal, 0, API.getNumWords() * 8);
957 else {
958 if (unsigned offset = shiftAmt / 64) {
959 for (unsigned i = API.getNumWords() - 1; i > offset - 1; --i)
960 API.pVal[i] = API.pVal[i-offset];
961 memset(API.pVal, 0, offset * 8);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000962 }
Zhou Shengd93f00c2007-02-12 20:02:55 +0000963 shiftAmt %= 64;
964 unsigned i;
965 for (i = API.getNumWords() - 1; i > 0; --i)
966 API.pVal[i] = (API.pVal[i] << shiftAmt) |
967 (API.pVal[i-1] >> (64-shiftAmt));
968 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000969 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000970 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000971 return API;
972}
973
Reid Spencer5e0a8512007-02-17 03:16:00 +0000974/// subMul - This function substracts x[len-1:0] * y from
975/// dest[offset+len-1:offset], and returns the most significant
976/// word of the product, minus the borrow-out from the subtraction.
977static unsigned subMul(unsigned dest[], unsigned offset,
978 unsigned x[], unsigned len, unsigned y) {
979 uint64_t yl = (uint64_t) y & 0xffffffffL;
980 unsigned carry = 0;
981 unsigned j = 0;
982 do {
Reid Spencerc72f2802007-02-17 22:38:07 +0000983 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000984 unsigned prod_low = (unsigned) prod;
985 unsigned prod_high = (unsigned) (prod >> 32);
986 prod_low += carry;
987 carry = (prod_low < carry ? 1 : 0) + prod_high;
988 unsigned x_j = dest[offset+j];
989 prod_low = x_j - prod_low;
990 if (prod_low > x_j) ++carry;
991 dest[offset+j] = prod_low;
992 } while (++j < len);
993 return carry;
994}
995
996/// unitDiv - This function divides N by D,
997/// and returns (remainder << 32) | quotient.
998/// Assumes (N >> 32) < D.
999static uint64_t unitDiv(uint64_t N, unsigned D) {
1000 uint64_t q, r; // q: quotient, r: remainder.
1001 uint64_t a1 = N >> 32; // a1: high 32-bit part of N.
1002 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N
1003 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) {
1004 q = N / D;
1005 r = N % D;
1006 }
1007 else {
1008 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d
1009 uint64_t c = N - ((uint64_t) D << 31);
1010 // Divide (c1*2^32 + c0) by d
1011 q = c / D;
1012 r = c % D;
1013 // Add 2^31 to quotient
1014 q += 1 << 31;
1015 }
1016
1017 return (r << 32) | (q & 0xFFFFFFFFl);
1018}
1019
1020/// div - This is basically Knuth's formulation of the classical algorithm.
1021/// Correspondance with Knuth's notation:
1022/// Knuth's u[0:m+n] == zds[nx:0].
1023/// Knuth's v[1:n] == y[ny-1:0]
1024/// Knuth's n == ny.
1025/// Knuth's m == nx-ny.
1026/// Our nx == Knuth's m+n.
1027/// Could be re-implemented using gmp's mpn_divrem:
1028/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny).
1029static void div(unsigned zds[], unsigned nx, unsigned y[], unsigned ny) {
1030 unsigned j = nx;
1031 do { // loop over digits of quotient
1032 // Knuth's j == our nx-j.
1033 // Knuth's u[j:j+n] == our zds[j:j-ny].
1034 unsigned qhat; // treated as unsigned
1035 if (zds[j] == y[ny-1])
1036 qhat = -1U; // 0xffffffff
1037 else {
1038 uint64_t w = (((uint64_t)(zds[j])) << 32) +
1039 ((uint64_t)zds[j-1] & 0xffffffffL);
1040 qhat = (unsigned) unitDiv(w, y[ny-1]);
1041 }
1042 if (qhat) {
1043 unsigned borrow = subMul(zds, j - ny, y, ny, qhat);
1044 unsigned save = zds[j];
1045 uint64_t num = ((uint64_t)save&0xffffffffL) -
1046 ((uint64_t)borrow&0xffffffffL);
1047 while (num) {
1048 qhat--;
1049 uint64_t carry = 0;
1050 for (unsigned i = 0; i < ny; i++) {
1051 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL)
1052 + ((uint64_t) y[i] & 0xffffffffL);
1053 zds[j-ny+i] = (unsigned) carry;
1054 carry >>= 32;
1055 }
1056 zds[j] += carry;
1057 num = carry - 1;
1058 }
1059 }
1060 zds[j] = qhat;
1061 } while (--j >= ny);
1062}
1063
Zhou Shengff4304f2007-02-09 07:48:24 +00001064/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001065/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001066APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001067 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001068
1069 // First, deal with the easy case
1070 if (isSingleWord()) {
1071 assert(RHS.VAL != 0 && "Divide by zero?");
1072 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001073 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001074
1075 // Make a temporary to hold the result
1076 APInt Result(*this);
1077
1078 // Get some facts about the LHS and RHS number of bits and words
1079 unsigned rhsBits = RHS.getActiveBits();
1080 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1081 assert(rhsWords && "Divided by zero???");
1082 unsigned lhsBits = Result.getActiveBits();
1083 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1084
1085 // Deal with some degenerate cases
1086 if (!lhsWords)
1087 return Result; // 0 / X == 0
1088 else if (lhsWords < rhsWords || Result.ult(RHS))
1089 // X / Y with X < Y == 0
1090 memset(Result.pVal, 0, Result.getNumWords() * 8);
1091 else if (Result == RHS) {
1092 // X / X == 1
1093 memset(Result.pVal, 0, Result.getNumWords() * 8);
1094 Result.pVal[0] = 1;
1095 } else if (lhsWords == 1)
1096 // All high words are zero, just use native divide
1097 Result.pVal[0] /= RHS.pVal[0];
1098 else {
1099 // Compute it the hard way ..
1100 APInt X(BitWidth, 0);
1101 APInt Y(BitWidth, 0);
1102 if (unsigned nshift = 63 - ((rhsBits - 1) % 64 )) {
1103 Y = APIntOps::shl(RHS, nshift);
1104 X = APIntOps::shl(Result, nshift);
1105 ++lhsWords;
1106 }
Reid Spencerc72f2802007-02-17 22:38:07 +00001107 div((unsigned*)X.pVal, lhsWords * 2 - 1,
1108 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001109 memset(Result.pVal, 0, Result.getNumWords() * 8);
1110 memcpy(Result.pVal, X.pVal + rhsWords, (lhsWords - rhsWords) * 8);
1111 }
1112 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001113}
1114
1115/// Unsigned remainder operation on APInt.
1116/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001117APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001118 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001119 if (isSingleWord()) {
1120 assert(RHS.VAL != 0 && "Remainder by zero?");
1121 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001122 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001123
1124 // Make a temporary to hold the result
1125 APInt Result(*this);
1126
1127 // Get some facts about the RHS
1128 unsigned rhsBits = RHS.getActiveBits();
1129 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1130 assert(rhsWords && "Performing remainder operation by zero ???");
1131
1132 // Get some facts about the LHS
1133 unsigned lhsBits = Result.getActiveBits();
1134 unsigned lhsWords = !lhsBits ? 0 : (Result.whichWord(lhsBits - 1) + 1);
1135
1136 // Check the degenerate cases
1137 if (lhsWords == 0)
1138 // 0 % Y == 0
1139 memset(Result.pVal, 0, Result.getNumWords() * 8);
1140 else if (lhsWords < rhsWords || Result.ult(RHS))
1141 // X % Y == X iff X < Y
1142 return Result;
1143 else if (Result == RHS)
1144 // X % X == 0;
1145 memset(Result.pVal, 0, Result.getNumWords() * 8);
1146 else if (lhsWords == 1)
1147 // All high words are zero, just use native remainder
1148 Result.pVal[0] %= RHS.pVal[0];
1149 else {
1150 // Do it the hard way
1151 APInt X((lhsWords+1)*64, 0);
1152 APInt Y(rhsWords*64, 0);
1153 unsigned nshift = 63 - (rhsBits - 1) % 64;
1154 if (nshift) {
1155 APIntOps::shl(Y, nshift);
1156 APIntOps::shl(X, nshift);
1157 }
Reid Spencerc72f2802007-02-17 22:38:07 +00001158 div((unsigned*)X.pVal, rhsWords*2-1,
1159 (unsigned*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001160 memset(Result.pVal, 0, Result.getNumWords() * 8);
1161 for (unsigned i = 0; i < rhsWords-1; ++i)
1162 Result.pVal[i] = (X.pVal[i] >> nshift) | (X.pVal[i+1] << (64 - nshift));
1163 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift;
1164 }
1165 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001166}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001167
1168/// @brief Converts a char array into an integer.
1169void APInt::fromString(unsigned numbits, const char *StrStart, unsigned slen,
1170 uint8_t radix) {
1171 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1172 "Radix should be 2, 8, 10, or 16!");
1173 assert(StrStart && "String is null?");
1174 unsigned size = 0;
1175 // If the radix is a power of 2, read the input
1176 // from most significant to least significant.
1177 if ((radix & (radix - 1)) == 0) {
1178 unsigned nextBitPos = 0, bits_per_digit = radix / 8 + 2;
1179 uint64_t resDigit = 0;
1180 BitWidth = slen * bits_per_digit;
1181 if (getNumWords() > 1)
1182 assert((pVal = new uint64_t[getNumWords()]) &&
1183 "APInt memory allocation fails!");
1184 for (int i = slen - 1; i >= 0; --i) {
1185 uint64_t digit = StrStart[i] - 48; // '0' == 48.
1186 resDigit |= digit << nextBitPos;
1187 nextBitPos += bits_per_digit;
1188 if (nextBitPos >= 64) {
1189 if (isSingleWord()) {
1190 VAL = resDigit;
1191 break;
1192 }
1193 pVal[size++] = resDigit;
1194 nextBitPos -= 64;
1195 resDigit = digit >> (bits_per_digit - nextBitPos);
1196 }
1197 }
1198 if (!isSingleWord() && size <= getNumWords())
1199 pVal[size] = resDigit;
1200 } else { // General case. The radix is not a power of 2.
1201 // For 10-radix, the max value of 64-bit integer is 18446744073709551615,
1202 // and its digits number is 20.
1203 const unsigned chars_per_word = 20;
1204 if (slen < chars_per_word ||
1205 (slen == chars_per_word && // In case the value <= 2^64 - 1
1206 strcmp(StrStart, "18446744073709551615") <= 0)) {
1207 BitWidth = 64;
1208 VAL = strtoull(StrStart, 0, 10);
1209 } else { // In case the value > 2^64 - 1
1210 BitWidth = (slen / chars_per_word + 1) * 64;
1211 assert((pVal = new uint64_t[getNumWords()]) &&
1212 "APInt memory allocation fails!");
1213 memset(pVal, 0, getNumWords() * 8);
1214 unsigned str_pos = 0;
1215 while (str_pos < slen) {
1216 unsigned chunk = slen - str_pos;
1217 if (chunk > chars_per_word - 1)
1218 chunk = chars_per_word - 1;
1219 uint64_t resDigit = StrStart[str_pos++] - 48; // 48 == '0'.
1220 uint64_t big_base = radix;
1221 while (--chunk > 0) {
1222 resDigit = resDigit * radix + StrStart[str_pos++] - 48;
1223 big_base *= radix;
1224 }
1225
1226 uint64_t carry;
1227 if (!size)
1228 carry = resDigit;
1229 else {
1230 carry = mul_1(pVal, pVal, size, big_base);
1231 carry += add_1(pVal, pVal, size, resDigit);
1232 }
1233
1234 if (carry) pVal[size++] = carry;
1235 }
1236 }
1237 }
1238}
1239