blob: bc6be8b1a1581afca86f7c496b0467997579f3c8 [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 Spenceraf0e9562007-02-18 18:38:44 +000022// A utility function for allocating memory, checking for allocation failures,
23// and ensuring the contents is zeroed.
24inline static uint64_t* getClearedMemory(uint32_t numWords) {
25 uint64_t * result = new uint64_t[numWords];
26 assert(result && "APInt memory allocation fails!");
27 memset(result, 0, numWords * sizeof(uint64_t));
28 return result;
Zhou Sheng353815d2007-02-06 06:04:53 +000029}
30
Reid Spenceraf0e9562007-02-18 18:38:44 +000031// A utility function for allocating memory and checking for allocation failure.
32inline static uint64_t* getMemory(uint32_t numWords) {
33 uint64_t * result = new uint64_t[numWords];
34 assert(result && "APInt memory allocation fails!");
35 return result;
36}
37
38APInt::APInt(uint32_t numBits, uint64_t val)
Reid Spencere81d2da2007-02-16 22:36:51 +000039 : BitWidth(numBits) {
40 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
41 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000042 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +000043 VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +000044 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000045 pVal = getClearedMemory(getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000046 pVal[0] = val;
47 }
48}
49
Reid Spenceraf0e9562007-02-18 18:38:44 +000050APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[])
Reid Spencere81d2da2007-02-16 22:36:51 +000051 : BitWidth(numBits) {
52 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
53 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000054 assert(bigVal && "Null pointer detected!");
55 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +000056 VAL = bigVal[0] & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +000057 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000058 pVal = getMemory(getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000059 // Calculate the actual length of bigVal[].
Reid Spenceraf0e9562007-02-18 18:38:44 +000060 uint32_t maxN = std::max<uint32_t>(numWords, getNumWords());
61 uint32_t minN = std::min<uint32_t>(numWords, getNumWords());
Reid Spencera58f0582007-02-18 20:09:41 +000062 memcpy(pVal, bigVal, (minN - 1) * APINT_WORD_SIZE);
Reid Spencer443b5702007-02-18 00:44:22 +000063 pVal[minN-1] = bigVal[minN-1] &
64 (~uint64_t(0ULL) >>
65 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD));
Zhou Shenga3832fd2007-02-07 06:14:53 +000066 if (maxN == getNumWords())
Reid Spencera58f0582007-02-18 20:09:41 +000067 memset(pVal+numWords, 0, (getNumWords() - numWords) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000068 }
69}
70
Zhou Shenga3832fd2007-02-07 06:14:53 +000071/// @brief Create a new APInt by translating the char array represented
72/// integer value.
Reid Spenceraf0e9562007-02-18 18:38:44 +000073APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
Reid Spencere81d2da2007-02-16 22:36:51 +000074 uint8_t radix) {
75 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000076}
77
78/// @brief Create a new APInt by translating the string represented
79/// integer value.
Reid Spenceraf0e9562007-02-18 18:38:44 +000080APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000081 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000082 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000083}
84
Reid Spencera58f0582007-02-18 20:09:41 +000085/// @brief Copy constructor
Zhou Shengfd43dcf2007-02-06 03:00:16 +000086APInt::APInt(const APInt& APIVal)
Reid Spencere81d2da2007-02-16 22:36:51 +000087 : BitWidth(APIVal.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +000088 if (isSingleWord())
89 VAL = APIVal.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000090 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000091 pVal = getMemory(getNumWords());
Reid Spencera58f0582007-02-18 20:09:41 +000092 memcpy(pVal, APIVal.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000093 }
94}
95
96APInt::~APInt() {
97 if (!isSingleWord() && pVal) delete[] pVal;
98}
99
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000100/// @brief Copy assignment operator. Create a new object from the given
101/// APInt one by initialization.
102APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000103 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
104 if (isSingleWord())
Reid Spenceraf0e9562007-02-18 18:38:44 +0000105 VAL = RHS.VAL;
106 else
Reid Spencera58f0582007-02-18 20:09:41 +0000107 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000108 return *this;
109}
110
111/// @brief Assignment operator. Assigns a common case integer value to
112/// the APInt.
113APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000114 if (isSingleWord())
115 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000116 else {
117 pVal[0] = RHS;
Reid Spencera58f0582007-02-18 20:09:41 +0000118 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000119 }
120 return *this;
121}
122
Reid Spenceraf0e9562007-02-18 18:38:44 +0000123/// add_1 - This function adds a single "digit" integer, y, to the multiple
124/// "digit" integer array, x[]. x[] is modified to reflect the addition and
125/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000126/// @returns the carry of the addition.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000127static uint64_t add_1(uint64_t dest[],
128 uint64_t x[], uint32_t len,
129 uint64_t y) {
130 for (uint32_t i = 0; i < len; ++i) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000131 dest[i] = y + x[i];
132 if (dest[i] < y)
133 y = 1;
134 else {
135 y = 0;
136 break;
137 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000138 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000139 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000140}
141
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000142/// @brief Prefix increment operator. Increments the APInt by one.
143APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000144 if (isSingleWord())
145 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000146 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000147 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000148 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000149 return *this;
150}
151
Reid Spenceraf0e9562007-02-18 18:38:44 +0000152/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
153/// the multi-digit integer array, x[], propagating the borrowed 1 value until
154/// no further borrowing is neeeded or it runs out of "digits" in x. The result
155/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
156/// In other words, if y > x then this function returns 1, otherwise 0.
157static uint64_t sub_1(uint64_t x[], uint32_t len,
158 uint64_t y) {
159 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000160 uint64_t X = x[i];
Reid Spencerf2c521c2007-02-18 06:39:42 +0000161 x[i] -= y;
162 if (y > X)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000163 y = 1; // We have to "borrow 1" from next "digit"
Reid Spencer5e0a8512007-02-17 03:16:00 +0000164 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000165 y = 0; // No need to borrow
166 break; // Remaining digits are unchanged so exit early
Reid Spencer5e0a8512007-02-17 03:16:00 +0000167 }
168 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000169 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000170}
171
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000172/// @brief Prefix decrement operator. Decrements the APInt by one.
173APInt& APInt::operator--() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000174 if (isSingleWord())
175 --VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000176 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000177 sub_1(pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000178 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000179 return *this;
180}
181
Reid Spencer5e0a8512007-02-17 03:16:00 +0000182/// add - This function adds the integer array x[] by integer array
183/// y[] and returns the carry.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000184static uint64_t add(uint64_t dest[], uint64_t x[],
185 uint64_t y[], uint32_t len) {
186 uint32_t carry = 0;
187 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000188 carry += x[i];
189 dest[i] = carry + y[i];
190 carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0);
191 }
192 return carry;
193}
194
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000195/// @brief Addition assignment operator. Adds this APInt by the given APInt&
196/// RHS and assigns the result to this APInt.
197APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000198 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000199 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
200 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000201 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000202 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000203 if (getNumWords() <= RHS.getNumWords())
204 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000205 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000206 uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.getNumWords());
207 add_1(pVal + RHS.getNumWords(), pVal + RHS.getNumWords(),
208 getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000209 }
210 }
211 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000212 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000213 return *this;
214}
215
Reid Spencer5e0a8512007-02-17 03:16:00 +0000216/// sub - This function subtracts the integer array x[] by
217/// integer array y[], and returns the borrow-out carry.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000218static uint64_t sub(uint64_t dest[], uint64_t x[],
219 uint64_t y[], uint32_t len) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000220 // Carry indicator.
221 uint64_t cy = 0;
222
Reid Spenceraf0e9562007-02-18 18:38:44 +0000223 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000224 uint64_t Y = y[i], X = x[i];
225 Y += cy;
226
227 cy = Y < cy ? 1 : 0;
228 Y = X - Y;
229 cy += Y > X ? 1 : 0;
230 dest[i] = Y;
231 }
232 return cy;
233}
234
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000235/// @brief Subtraction assignment operator. Subtracts this APInt by the given
236/// APInt &RHS and assigns the result to this APInt.
237APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000238 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000239 if (isSingleWord())
240 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
241 else {
242 if (RHS.isSingleWord())
Zhou Shenga3832fd2007-02-07 06:14:53 +0000243 sub_1(pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000244 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000245 if (RHS.getNumWords() < getNumWords()) {
246 uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
Reid Spencerf2c521c2007-02-18 06:39:42 +0000247 sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(),
248 carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000249 }
250 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000251 sub(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000252 }
253 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000254 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000255 return *this;
256}
257
Reid Spencer5e0a8512007-02-17 03:16:00 +0000258/// mul_1 - This function performs the multiplication operation on a
259/// large integer (represented as an integer array) and a uint64_t integer.
260/// @returns the carry of the multiplication.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000261static uint64_t mul_1(uint64_t dest[],
262 uint64_t x[], uint32_t len,
263 uint64_t y) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000264 // Split y into high 32-bit part and low 32-bit part.
265 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
266 uint64_t carry = 0, lx, hx;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000267 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000268 lx = x[i] & 0xffffffffULL;
269 hx = x[i] >> 32;
270 // hasCarry - A flag to indicate if has carry.
271 // hasCarry == 0, no carry
272 // hasCarry == 1, has carry
273 // hasCarry == 2, no carry and the calculation result == 0.
274 uint8_t hasCarry = 0;
275 dest[i] = carry + lx * ly;
276 // Determine if the add above introduces carry.
277 hasCarry = (dest[i] < carry) ? 1 : 0;
278 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
279 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
280 // (2^32 - 1) + 2^32 = 2^64.
281 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
282
283 carry += (lx * hy) & 0xffffffffULL;
284 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
285 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
286 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
287 }
288
289 return carry;
290}
291
292/// mul - This function multiplies integer array x[] by integer array y[] and
293/// stores the result into integer array dest[].
294/// Note the array dest[]'s size should no less than xlen + ylen.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000295static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen,
296 uint64_t y[], uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000297 dest[xlen] = mul_1(dest, x, xlen, y[0]);
298
Reid Spenceraf0e9562007-02-18 18:38:44 +0000299 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000300 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
301 uint64_t carry = 0, lx, hx;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000302 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000303 lx = x[j] & 0xffffffffULL;
304 hx = x[j] >> 32;
305 // hasCarry - A flag to indicate if has carry.
306 // hasCarry == 0, no carry
307 // hasCarry == 1, has carry
308 // hasCarry == 2, no carry and the calculation result == 0.
309 uint8_t hasCarry = 0;
310 uint64_t resul = carry + lx * ly;
311 hasCarry = (resul < carry) ? 1 : 0;
312 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
313 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
314
315 carry += (lx * hy) & 0xffffffffULL;
316 resul = (carry << 32) | (resul & 0xffffffffULL);
317 dest[i+j] += resul;
318 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
319 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
320 ((lx * hy) >> 32) + hx * hy;
321 }
322 dest[i+xlen] = carry;
323 }
324}
325
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000326/// @brief Multiplication assignment operator. Multiplies this APInt by the
327/// given APInt& RHS and assigns the result to this APInt.
328APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000329 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000330 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
331 else {
332 // one-based first non-zero bit position.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000333 uint32_t first = getActiveBits();
334 uint32_t xlen = !first ? 0 : whichWord(first - 1) + 1;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000335 if (!xlen)
336 return *this;
337 else if (RHS.isSingleWord())
338 mul_1(pVal, pVal, xlen, RHS.VAL);
339 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000340 first = RHS.getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +0000341 uint32_t ylen = !first ? 0 : whichWord(first - 1) + 1;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000342 if (!ylen) {
Reid Spencera58f0582007-02-18 20:09:41 +0000343 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000344 return *this;
345 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000346 uint64_t *dest = getMemory(xlen+ylen);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000347 mul(dest, pVal, xlen, RHS.pVal, ylen);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000348 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ?
Reid Spencera58f0582007-02-18 20:09:41 +0000349 getNumWords() : xlen + ylen) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000350 delete[] dest;
351 }
352 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000353 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000354 return *this;
355}
356
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000357/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
358/// this APInt and the given APInt& RHS, assigns the result to this APInt.
359APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000360 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000361 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000362 VAL &= RHS.VAL;
363 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000364 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000365 uint32_t numWords = getNumWords();
366 for (uint32_t i = 0; i < numWords; ++i)
367 pVal[i] &= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000368 return *this;
369}
370
371/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on
372/// this APInt and the given APInt& RHS, assigns the result to this APInt.
373APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000374 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000375 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000376 VAL |= RHS.VAL;
377 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000378 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000379 uint32_t numWords = getNumWords();
380 for (uint32_t i = 0; i < numWords; ++i)
381 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000382 return *this;
383}
384
385/// @brief Bitwise XOR assignment operator. Performs bitwise XOR 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()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000390 VAL ^= RHS.VAL;
391 return *this;
392 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000393 uint32_t numWords = getNumWords();
394 for (uint32_t i = 0; i < numWords; ++i)
395 pVal[i] ^= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000396 return *this;
397}
398
399/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
400/// and the given APInt& RHS.
401APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000402 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000403 if (isSingleWord())
404 return APInt(getBitWidth(), VAL & RHS.VAL);
405
406 APInt Result(*this);
407 uint32_t numWords = getNumWords();
408 for (uint32_t i = 0; i < numWords; ++i)
409 Result.pVal[i] &= RHS.pVal[i];
410 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000411}
412
413/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt
414/// and the given APInt& RHS.
415APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000416 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000417 if (isSingleWord())
418 return APInt(getBitWidth(), VAL | RHS.VAL);
419 APInt Result(*this);
420 uint32_t numWords = getNumWords();
421 for (uint32_t i = 0; i < numWords; ++i)
422 Result.pVal[i] |= RHS.pVal[i];
423 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000424}
425
426/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
427/// and the given APInt& RHS.
428APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000429 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000430 if (isSingleWord())
431 return APInt(getBitWidth(), VAL ^ RHS.VAL);
432 APInt Result(*this);
433 uint32_t numWords = getNumWords();
434 for (uint32_t i = 0; i < numWords; ++i)
435 Result.pVal[i] ^= RHS.pVal[i];
436 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000437}
438
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000439/// @brief Logical negation operator. Performs logical negation operation on
440/// this APInt.
441bool APInt::operator !() const {
442 if (isSingleWord())
443 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000444
445 for (uint32_t i = 0; i < getNumWords(); ++i)
446 if (pVal[i])
447 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000448 return true;
449}
450
451/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
452/// RHS.
453APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000454 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000455 APInt API(RHS);
456 API *= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000457 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000458 return API;
459}
460
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000461/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
462APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000463 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000464 APInt API(*this);
465 API += RHS;
Reid Spencere81d2da2007-02-16 22:36:51 +0000466 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000467 return API;
468}
469
470/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
471APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000472 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000473 APInt API(*this);
474 API -= RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000475 return API;
476}
477
478/// @brief Array-indexing support.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000479bool APInt::operator[](uint32_t bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000480 return (maskBit(bitPosition) & (isSingleWord() ?
481 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000482}
483
484/// @brief Equality operator. Compare this APInt with the given APInt& RHS
485/// for the validity of the equality relationship.
486bool APInt::operator==(const APInt& RHS) const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000487 uint32_t n1 = getActiveBits();
488 uint32_t n2 = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000489 if (n1 != n2) return false;
490 else if (isSingleWord())
491 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
492 else {
Reid Spencer443b5702007-02-18 00:44:22 +0000493 if (n1 <= APINT_BITS_PER_WORD)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000494 return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
495 for (int i = whichWord(n1 - 1); i >= 0; --i)
496 if (pVal[i] != RHS.pVal[i]) return false;
497 }
498 return true;
499}
500
Zhou Shenga3832fd2007-02-07 06:14:53 +0000501/// @brief Equality operator. Compare this APInt with the given uint64_t value
502/// for the validity of the equality relationship.
503bool APInt::operator==(uint64_t Val) const {
504 if (isSingleWord())
505 return VAL == Val;
506 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000507 uint32_t n = getActiveBits();
Reid Spencer443b5702007-02-18 00:44:22 +0000508 if (n <= APINT_BITS_PER_WORD)
Zhou Shenga3832fd2007-02-07 06:14:53 +0000509 return pVal[0] == Val;
510 else
511 return false;
512 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000513}
514
Reid Spencere81d2da2007-02-16 22:36:51 +0000515/// @brief Unsigned less than comparison
516bool APInt::ult(const APInt& RHS) const {
517 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
518 if (isSingleWord())
519 return VAL < RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000520 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000521 uint32_t n1 = getActiveBits();
522 uint32_t n2 = RHS.getActiveBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000523 if (n1 < n2)
524 return true;
525 else if (n2 < n1)
526 return false;
Reid Spencer443b5702007-02-18 00:44:22 +0000527 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000528 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000529 for (int i = whichWord(n1 - 1); i >= 0; --i) {
530 if (pVal[i] > RHS.pVal[i]) return false;
531 else if (pVal[i] < RHS.pVal[i]) return true;
532 }
533 }
534 return false;
535}
536
Reid Spencere81d2da2007-02-16 22:36:51 +0000537/// @brief Signed less than comparison
538bool APInt::slt(const APInt& RHS) const {
539 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
Reid Spencera58f0582007-02-18 20:09:41 +0000540 if (isSingleWord()) {
541 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
542 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
543 return lhsSext < rhsSext;
Reid Spencere81d2da2007-02-16 22:36:51 +0000544 }
Reid Spencera58f0582007-02-18 20:09:41 +0000545
546 APInt lhs(*this);
547 APInt rhs(*this);
548 bool lhsNegative = false;
549 bool rhsNegative = false;
550 if (lhs[BitWidth-1]) {
551 lhsNegative = true;
552 lhs.flip();
553 lhs++;
554 }
555 if (rhs[BitWidth-1]) {
556 rhsNegative = true;
557 rhs.flip();
558 rhs++;
559 }
560 if (lhsNegative)
561 if (rhsNegative)
562 return !lhs.ult(rhs);
563 else
564 return true;
565 else if (rhsNegative)
566 return false;
567 else
568 return lhs.ult(rhs);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000569}
570
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000571/// Set the given bit to 1 whose poition is given as "bitPosition".
572/// @brief Set a given bit to 1.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000573APInt& APInt::set(uint32_t bitPosition) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000574 if (isSingleWord()) VAL |= maskBit(bitPosition);
575 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
576 return *this;
577}
578
579/// @brief Set every bit to 1.
580APInt& APInt::set() {
Reid Spencer443b5702007-02-18 00:44:22 +0000581 if (isSingleWord())
582 VAL = ~0ULL >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000583 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000584 for (uint32_t i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000585 pVal[i] = -1ULL;
Reid Spencer443b5702007-02-18 00:44:22 +0000586 pVal[getNumWords() - 1] = ~0ULL >>
587 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD);
Zhou Shengb04973e2007-02-15 06:36:31 +0000588 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000589 return *this;
590}
591
592/// Set the given bit to 0 whose position is given as "bitPosition".
593/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000594APInt& APInt::clear(uint32_t bitPosition) {
595 if (isSingleWord())
596 VAL &= ~maskBit(bitPosition);
597 else
598 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000599 return *this;
600}
601
602/// @brief Set every bit to 0.
603APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000604 if (isSingleWord())
605 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000606 else
Reid Spencera58f0582007-02-18 20:09:41 +0000607 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000608 return *this;
609}
610
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000611/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
612/// this APInt.
613APInt APInt::operator~() const {
614 APInt API(*this);
615 API.flip();
616 return API;
617}
618
619/// @brief Toggle every bit to its opposite value.
620APInt& APInt::flip() {
Reid Spencer443b5702007-02-18 00:44:22 +0000621 if (isSingleWord()) VAL = (~(VAL <<
622 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000623 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000624 uint32_t i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000625 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000626 pVal[i] = ~pVal[i];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000627 uint32_t offset =
Reid Spencer443b5702007-02-18 00:44:22 +0000628 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000629 pVal[i] = (~(pVal[i] << offset)) >> offset;
630 }
631 return *this;
632}
633
634/// Toggle a given bit to its opposite value whose position is given
635/// as "bitPosition".
636/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000637APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000638 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000639 if ((*this)[bitPosition]) clear(bitPosition);
640 else set(bitPosition);
641 return *this;
642}
643
644/// to_string - This function translates the APInt into a string.
Reid Spencer443b5702007-02-18 00:44:22 +0000645std::string APInt::toString(uint8_t radix, bool wantSigned) const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000646 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
647 "Radix should be 2, 8, 10, or 16!");
Reid Spencer879dfe12007-02-14 02:52:25 +0000648 static const char *digits[] = {
649 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
650 };
651 std::string result;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000652 uint32_t bits_used = getActiveBits();
Reid Spencer879dfe12007-02-14 02:52:25 +0000653 if (isSingleWord()) {
654 char buf[65];
Reid Spencer443b5702007-02-18 00:44:22 +0000655 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
Reid Spencer879dfe12007-02-14 02:52:25 +0000656 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
657 if (format) {
Reid Spencer443b5702007-02-18 00:44:22 +0000658 if (wantSigned) {
659 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
660 (APINT_BITS_PER_WORD-BitWidth);
661 sprintf(buf, format, sextVal);
662 } else
663 sprintf(buf, format, VAL);
Reid Spencer879dfe12007-02-14 02:52:25 +0000664 } else {
665 memset(buf, 0, 65);
666 uint64_t v = VAL;
667 while (bits_used) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000668 uint32_t bit = v & 1;
Reid Spencer879dfe12007-02-14 02:52:25 +0000669 bits_used--;
670 buf[bits_used] = digits[bit][0];
671 v >>=1;
672 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000673 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000674 result = buf;
675 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000676 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000677
678 APInt tmp(*this);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000679 APInt divisor(tmp.getBitWidth(), radix);
680 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer443b5702007-02-18 00:44:22 +0000681 size_t insert_at = 0;
682 if (wantSigned && tmp[BitWidth-1]) {
683 // They want to print the signed version and it is a negative value
684 // Flip the bits and add one to turn it into the equivalent positive
685 // value and put a '-' in the result.
686 tmp.flip();
687 tmp++;
688 result = "-";
689 insert_at = 1;
690 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000691 if (tmp == 0)
692 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000693 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000694 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000695 uint32_t digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000696 assert(digit < radix && "urem failed");
Reid Spencer443b5702007-02-18 00:44:22 +0000697 result.insert(insert_at,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000698 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000699 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000700
701 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000702}
703
704/// getMaxValue - This function returns the largest value
705/// for an APInt of the specified bit-width and if isSign == true,
706/// it should be largest signed value, otherwise unsigned value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000707APInt APInt::getMaxValue(uint32_t numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000708 APInt APIVal(numBits, 0);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000709 APIVal.set();
Zhou Shengb04973e2007-02-15 06:36:31 +0000710 if (isSign) APIVal.clear(numBits - 1);
711 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000712}
713
714/// getMinValue - This function returns the smallest value for
715/// an APInt of the given bit-width and if isSign == true,
716/// it should be smallest signed value, otherwise zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000717APInt APInt::getMinValue(uint32_t numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000718 APInt APIVal(numBits, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000719 if (isSign) APIVal.set(numBits - 1);
720 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000721}
722
723/// getAllOnesValue - This function returns an all-ones value for
724/// an APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000725APInt APInt::getAllOnesValue(uint32_t numBits) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000726 return getMaxValue(numBits, false);
727}
728
729/// getNullValue - This function creates an '0' value for an
730/// APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000731APInt APInt::getNullValue(uint32_t numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000732 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000733}
734
735/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000736APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000737 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000738}
739
740/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000741APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000742 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
743 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000744}
745
Reid Spencere81d2da2007-02-16 22:36:51 +0000746bool APInt::isPowerOf2() const {
747 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
748}
749
750/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000751/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000752/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000753/// the number of zeros from the most significant bit to the first one bit.
754/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000755uint32_t APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000756 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000757 return CountLeadingZeros_64(VAL) - (APINT_BITS_PER_WORD - BitWidth);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000758 uint32_t Count = 0;
759 for (uint32_t i = getNumWords(); i > 0u; --i) {
760 uint32_t tmp = CountLeadingZeros_64(pVal[i-1]);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000761 Count += tmp;
Reid Spencer443b5702007-02-18 00:44:22 +0000762 if (tmp != APINT_BITS_PER_WORD)
763 if (i == getNumWords())
764 Count -= (APINT_BITS_PER_WORD - whichBit(BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000765 break;
766 }
767 return Count;
768}
769
Reid Spencere81d2da2007-02-16 22:36:51 +0000770/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000771/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000772/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000773/// the number of zeros from the least significant bit to the first one bit.
774/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000775uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000776 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000777 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000778 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000779 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000780}
781
Reid Spencere81d2da2007-02-16 22:36:51 +0000782/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000783/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000784/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000785/// @returns 0 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000786uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000787 if (isSingleWord())
788 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000789 uint32_t Count = 0;
790 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000791 Count += CountPopulation_64(pVal[i]);
792 return Count;
793}
794
795
Reid Spencere81d2da2007-02-16 22:36:51 +0000796/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000797/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000798APInt APInt::byteSwap() const {
799 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
800 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000801 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000802 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000803 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000804 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000805 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
806 Tmp1 = ByteSwap_32(Tmp1);
807 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
808 Tmp2 = ByteSwap_16(Tmp2);
809 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000810 APInt(BitWidth,
811 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000812 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000813 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000814 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000815 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000816 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000817 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000818 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000819 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
820 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000821 }
822 return Result;
823 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000824}
825
826/// GreatestCommonDivisor - This function returns the greatest common
827/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000828APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
829 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000830 APInt A = API1, B = API2;
831 while (!!B) {
832 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000833 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000834 A = T;
835 }
836 return A;
837}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000838
Zhou Shengd93f00c2007-02-12 20:02:55 +0000839/// DoubleRoundToAPInt - This function convert a double value to
840/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000841APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000842 union {
843 double D;
844 uint64_t I;
845 } T;
846 T.D = Double;
847 bool isNeg = T.I >> 63;
848 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
849 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000850 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000851 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
852 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000853 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
854 APInt(64u, mantissa >> (52 - exp));
855 APInt Tmp(exp + 1, mantissa);
856 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000857 return isNeg ? -Tmp : Tmp;
858}
859
Reid Spencerdb3faa62007-02-13 22:41:58 +0000860/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000861/// The layout for double is as following (IEEE Standard 754):
862/// --------------------------------------
863/// | Sign Exponent Fraction Bias |
864/// |-------------------------------------- |
865/// | 1[63] 11[62-52] 52[51-00] 1023 |
866/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000867double APInt::roundToDouble(bool isSigned) const {
Reid Spencera58f0582007-02-18 20:09:41 +0000868 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
869 if (isSigned) {
870 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
871 return double(sext);
872 } else
873 return double(VAL);
874 }
875
Reid Spencere81d2da2007-02-16 22:36:51 +0000876 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000877 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spenceraf0e9562007-02-18 18:38:44 +0000878 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000879 // Exponent when normalized to have decimal point directly after
880 // leading one. This is stored excess 1023 in the exponent bit field.
881 uint64_t exp = n - 1;
882
883 // Gross overflow.
884 assert(exp <= 1023 && "Infinity value!");
885
886 // Number of bits in mantissa including the leading one
887 // equals to 53.
888 uint64_t mantissa;
Reid Spencer443b5702007-02-18 00:44:22 +0000889 if (n % APINT_BITS_PER_WORD >= 53)
890 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % APINT_BITS_PER_WORD - 53);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000891 else
Reid Spencer443b5702007-02-18 00:44:22 +0000892 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % APINT_BITS_PER_WORD)) |
893 (Tmp.pVal[whichWord(n - 1) - 1] >>
894 (11 + n % APINT_BITS_PER_WORD));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000895 // The leading bit of mantissa is implicit, so get rid of it.
896 mantissa &= ~(1ULL << 52);
Reid Spencer443b5702007-02-18 00:44:22 +0000897 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000898 exp += 1023;
899 union {
900 double D;
901 uint64_t I;
902 } T;
903 T.I = sign | (exp << 52) | mantissa;
904 return T.D;
905}
906
Reid Spencere81d2da2007-02-16 22:36:51 +0000907// Truncate to new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000908void APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000909 assert(width < BitWidth && "Invalid APInt Truncate request");
910}
911
912// Sign extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000913void APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000914 assert(width > BitWidth && "Invalid APInt SignExtend request");
915}
916
917// Zero extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000918void APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000919 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
920}
921
Zhou Shengff4304f2007-02-09 07:48:24 +0000922/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000923/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000924APInt APInt::ashr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000925 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000926 if (API.isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000927 API.VAL =
928 (((int64_t(API.VAL) << (APINT_BITS_PER_WORD - API.BitWidth)) >>
929 (APINT_BITS_PER_WORD - API.BitWidth)) >> shiftAmt) &
930 (~uint64_t(0UL) >> (APINT_BITS_PER_WORD - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000931 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000932 if (shiftAmt >= API.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000933 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0,
Reid Spencera58f0582007-02-18 20:09:41 +0000934 (API.getNumWords()-1) * APINT_WORD_SIZE);
Reid Spencer443b5702007-02-18 00:44:22 +0000935 API.pVal[API.getNumWords() - 1] =
936 ~uint64_t(0UL) >>
937 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000938 } else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000939 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000940 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000941 if (API[i+shiftAmt])
942 API.set(i);
943 else
944 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000945 for (; i < API.BitWidth; ++i)
946 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000947 API.set(i);
948 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000949 }
950 }
951 return API;
952}
953
Zhou Shengff4304f2007-02-09 07:48:24 +0000954/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000955/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000956APInt APInt::lshr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000957 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000958 if (API.isSingleWord())
959 API.VAL >>= shiftAmt;
960 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000961 if (shiftAmt >= API.BitWidth)
Reid Spencera58f0582007-02-18 20:09:41 +0000962 memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000963 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000964 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000965 if (API[i+shiftAmt]) API.set(i);
966 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000967 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000968 API.clear(i);
969 }
970 return API;
971}
972
Zhou Shengff4304f2007-02-09 07:48:24 +0000973/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000974/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000975APInt APInt::shl(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000976 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000977 if (API.isSingleWord())
978 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000979 else if (shiftAmt >= API.BitWidth)
Reid Spencera58f0582007-02-18 20:09:41 +0000980 memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000981 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000982 if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
983 for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000984 API.pVal[i] = API.pVal[i-offset];
Reid Spencera58f0582007-02-18 20:09:41 +0000985 memset(API.pVal, 0, offset * APINT_WORD_SIZE);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000986 }
Reid Spencer443b5702007-02-18 00:44:22 +0000987 shiftAmt %= APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000988 uint32_t i;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000989 for (i = API.getNumWords() - 1; i > 0; --i)
990 API.pVal[i] = (API.pVal[i] << shiftAmt) |
Reid Spencer443b5702007-02-18 00:44:22 +0000991 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000992 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000993 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000994 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000995 return API;
996}
997
Reid Spencer5e0a8512007-02-17 03:16:00 +0000998/// subMul - This function substracts x[len-1:0] * y from
999/// dest[offset+len-1:offset], and returns the most significant
1000/// word of the product, minus the borrow-out from the subtraction.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001001static uint32_t subMul(uint32_t dest[], uint32_t offset,
1002 uint32_t x[], uint32_t len, uint32_t y) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001003 uint64_t yl = (uint64_t) y & 0xffffffffL;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001004 uint32_t carry = 0;
1005 uint32_t j = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001006 do {
Reid Spencerc72f2802007-02-17 22:38:07 +00001007 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001008 uint32_t prod_low = (uint32_t) prod;
1009 uint32_t prod_high = (uint32_t) (prod >> 32);
Reid Spencer5e0a8512007-02-17 03:16:00 +00001010 prod_low += carry;
1011 carry = (prod_low < carry ? 1 : 0) + prod_high;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001012 uint32_t x_j = dest[offset+j];
Reid Spencer5e0a8512007-02-17 03:16:00 +00001013 prod_low = x_j - prod_low;
1014 if (prod_low > x_j) ++carry;
1015 dest[offset+j] = prod_low;
1016 } while (++j < len);
1017 return carry;
1018}
1019
1020/// unitDiv - This function divides N by D,
1021/// and returns (remainder << 32) | quotient.
1022/// Assumes (N >> 32) < D.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001023static uint64_t unitDiv(uint64_t N, uint32_t D) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001024 uint64_t q, r; // q: quotient, r: remainder.
1025 uint64_t a1 = N >> 32; // a1: high 32-bit part of N.
1026 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N
1027 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) {
1028 q = N / D;
1029 r = N % D;
1030 }
1031 else {
1032 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d
1033 uint64_t c = N - ((uint64_t) D << 31);
1034 // Divide (c1*2^32 + c0) by d
1035 q = c / D;
1036 r = c % D;
1037 // Add 2^31 to quotient
1038 q += 1 << 31;
1039 }
1040
1041 return (r << 32) | (q & 0xFFFFFFFFl);
1042}
1043
1044/// div - This is basically Knuth's formulation of the classical algorithm.
1045/// Correspondance with Knuth's notation:
1046/// Knuth's u[0:m+n] == zds[nx:0].
1047/// Knuth's v[1:n] == y[ny-1:0]
1048/// Knuth's n == ny.
1049/// Knuth's m == nx-ny.
1050/// Our nx == Knuth's m+n.
1051/// Could be re-implemented using gmp's mpn_divrem:
1052/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny).
Reid Spenceraf0e9562007-02-18 18:38:44 +00001053static void div(uint32_t zds[], uint32_t nx, uint32_t y[], uint32_t ny) {
1054 uint32_t j = nx;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001055 do { // loop over digits of quotient
1056 // Knuth's j == our nx-j.
1057 // Knuth's u[j:j+n] == our zds[j:j-ny].
Reid Spenceraf0e9562007-02-18 18:38:44 +00001058 uint32_t qhat; // treated as unsigned
Reid Spencer5e0a8512007-02-17 03:16:00 +00001059 if (zds[j] == y[ny-1])
1060 qhat = -1U; // 0xffffffff
1061 else {
1062 uint64_t w = (((uint64_t)(zds[j])) << 32) +
1063 ((uint64_t)zds[j-1] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001064 qhat = (uint32_t) unitDiv(w, y[ny-1]);
Reid Spencer5e0a8512007-02-17 03:16:00 +00001065 }
1066 if (qhat) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001067 uint32_t borrow = subMul(zds, j - ny, y, ny, qhat);
1068 uint32_t save = zds[j];
Reid Spencer5e0a8512007-02-17 03:16:00 +00001069 uint64_t num = ((uint64_t)save&0xffffffffL) -
1070 ((uint64_t)borrow&0xffffffffL);
1071 while (num) {
1072 qhat--;
1073 uint64_t carry = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001074 for (uint32_t i = 0; i < ny; i++) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001075 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL)
1076 + ((uint64_t) y[i] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001077 zds[j-ny+i] = (uint32_t) carry;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001078 carry >>= 32;
1079 }
1080 zds[j] += carry;
1081 num = carry - 1;
1082 }
1083 }
1084 zds[j] = qhat;
1085 } while (--j >= ny);
1086}
1087
Zhou Shengff4304f2007-02-09 07:48:24 +00001088/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001089/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001090APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001091 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001092
1093 // First, deal with the easy case
1094 if (isSingleWord()) {
1095 assert(RHS.VAL != 0 && "Divide by zero?");
1096 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001097 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001098
1099 // Make a temporary to hold the result
1100 APInt Result(*this);
1101
1102 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001103 uint32_t rhsBits = RHS.getActiveBits();
1104 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001105 assert(rhsWords && "Divided by zero???");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001106 uint32_t lhsBits = Result.getActiveBits();
1107 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001108
1109 // Deal with some degenerate cases
1110 if (!lhsWords)
1111 return Result; // 0 / X == 0
1112 else if (lhsWords < rhsWords || Result.ult(RHS))
1113 // X / Y with X < Y == 0
Reid Spencera58f0582007-02-18 20:09:41 +00001114 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001115 else if (Result == RHS) {
1116 // X / X == 1
Reid Spencera58f0582007-02-18 20:09:41 +00001117 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001118 Result.pVal[0] = 1;
1119 } else if (lhsWords == 1)
1120 // All high words are zero, just use native divide
1121 Result.pVal[0] /= RHS.pVal[0];
1122 else {
1123 // Compute it the hard way ..
1124 APInt X(BitWidth, 0);
1125 APInt Y(BitWidth, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001126 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001127 (APINT_BITS_PER_WORD - 1) - ((rhsBits - 1) % APINT_BITS_PER_WORD );
1128 if (nshift) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001129 Y = APIntOps::shl(RHS, nshift);
1130 X = APIntOps::shl(Result, nshift);
1131 ++lhsWords;
1132 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001133 div((uint32_t*)X.pVal, lhsWords * 2 - 1,
1134 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencera58f0582007-02-18 20:09:41 +00001135 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001136 memcpy(Result.pVal, X.pVal + rhsWords,
Reid Spencera58f0582007-02-18 20:09:41 +00001137 (lhsWords - rhsWords) * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001138 }
1139 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001140}
1141
1142/// Unsigned remainder operation on APInt.
1143/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001144APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001145 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001146 if (isSingleWord()) {
1147 assert(RHS.VAL != 0 && "Remainder by zero?");
1148 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001149 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001150
1151 // Make a temporary to hold the result
1152 APInt Result(*this);
1153
1154 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001155 uint32_t rhsBits = RHS.getActiveBits();
1156 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001157 assert(rhsWords && "Performing remainder operation by zero ???");
1158
1159 // Get some facts about the LHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001160 uint32_t lhsBits = Result.getActiveBits();
1161 uint32_t lhsWords = !lhsBits ? 0 : (Result.whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001162
1163 // Check the degenerate cases
1164 if (lhsWords == 0)
1165 // 0 % Y == 0
Reid Spencera58f0582007-02-18 20:09:41 +00001166 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001167 else if (lhsWords < rhsWords || Result.ult(RHS))
1168 // X % Y == X iff X < Y
1169 return Result;
1170 else if (Result == RHS)
1171 // X % X == 0;
Reid Spencera58f0582007-02-18 20:09:41 +00001172 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001173 else if (lhsWords == 1)
1174 // All high words are zero, just use native remainder
1175 Result.pVal[0] %= RHS.pVal[0];
1176 else {
1177 // Do it the hard way
Reid Spencer443b5702007-02-18 00:44:22 +00001178 APInt X((lhsWords+1)*APINT_BITS_PER_WORD, 0);
1179 APInt Y(rhsWords*APINT_BITS_PER_WORD, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001180 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001181 (APINT_BITS_PER_WORD - 1) - (rhsBits - 1) % APINT_BITS_PER_WORD;
Reid Spencer71bd08f2007-02-17 02:07:07 +00001182 if (nshift) {
1183 APIntOps::shl(Y, nshift);
1184 APIntOps::shl(X, nshift);
1185 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001186 div((uint32_t*)X.pVal, rhsWords*2-1,
1187 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencera58f0582007-02-18 20:09:41 +00001188 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001189 for (uint32_t i = 0; i < rhsWords-1; ++i)
Reid Spencer443b5702007-02-18 00:44:22 +00001190 Result.pVal[i] = (X.pVal[i] >> nshift) |
1191 (X.pVal[i+1] << (APINT_BITS_PER_WORD - nshift));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001192 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift;
1193 }
1194 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001195}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001196
1197/// @brief Converts a char array into an integer.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001198void APInt::fromString(uint32_t numbits, const char *StrStart, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001199 uint8_t radix) {
1200 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1201 "Radix should be 2, 8, 10, or 16!");
1202 assert(StrStart && "String is null?");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001203 uint32_t size = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001204 // If the radix is a power of 2, read the input
1205 // from most significant to least significant.
1206 if ((radix & (radix - 1)) == 0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001207 uint32_t nextBitPos = 0;
1208 uint32_t bits_per_digit = radix / 8 + 2;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001209 uint64_t resDigit = 0;
1210 BitWidth = slen * bits_per_digit;
1211 if (getNumWords() > 1)
Reid Spenceraf0e9562007-02-18 18:38:44 +00001212 pVal = getMemory(getNumWords());
Reid Spencer5e0a8512007-02-17 03:16:00 +00001213 for (int i = slen - 1; i >= 0; --i) {
Reid Spencer443b5702007-02-18 00:44:22 +00001214 uint64_t digit = StrStart[i] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001215 resDigit |= digit << nextBitPos;
1216 nextBitPos += bits_per_digit;
Reid Spencer443b5702007-02-18 00:44:22 +00001217 if (nextBitPos >= APINT_BITS_PER_WORD) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001218 if (isSingleWord()) {
1219 VAL = resDigit;
1220 break;
1221 }
1222 pVal[size++] = resDigit;
Reid Spencer443b5702007-02-18 00:44:22 +00001223 nextBitPos -= APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001224 resDigit = digit >> (bits_per_digit - nextBitPos);
1225 }
1226 }
1227 if (!isSingleWord() && size <= getNumWords())
1228 pVal[size] = resDigit;
1229 } else { // General case. The radix is not a power of 2.
1230 // For 10-radix, the max value of 64-bit integer is 18446744073709551615,
1231 // and its digits number is 20.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001232 const uint32_t chars_per_word = 20;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001233 if (slen < chars_per_word ||
1234 (slen == chars_per_word && // In case the value <= 2^64 - 1
1235 strcmp(StrStart, "18446744073709551615") <= 0)) {
Reid Spencer443b5702007-02-18 00:44:22 +00001236 BitWidth = APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001237 VAL = strtoull(StrStart, 0, 10);
1238 } else { // In case the value > 2^64 - 1
Reid Spencer443b5702007-02-18 00:44:22 +00001239 BitWidth = (slen / chars_per_word + 1) * APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001240 pVal = getClearedMemory(getNumWords());
1241 uint32_t str_pos = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001242 while (str_pos < slen) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001243 uint32_t chunk = slen - str_pos;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001244 if (chunk > chars_per_word - 1)
1245 chunk = chars_per_word - 1;
Reid Spencer443b5702007-02-18 00:44:22 +00001246 uint64_t resDigit = StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001247 uint64_t big_base = radix;
1248 while (--chunk > 0) {
Reid Spencer443b5702007-02-18 00:44:22 +00001249 resDigit = resDigit * radix + StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001250 big_base *= radix;
1251 }
1252
1253 uint64_t carry;
1254 if (!size)
1255 carry = resDigit;
1256 else {
1257 carry = mul_1(pVal, pVal, size, big_base);
1258 carry += add_1(pVal, pVal, size, resDigit);
1259 }
1260
1261 if (carry) pVal[size++] = carry;
1262 }
1263 }
1264 }
1265}