blob: ec47a69146a164693b0d9bf527d2df767a0a3477 [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
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000678 if (radix != 10) {
679 uint64_t mask = radix - 1;
680 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1);
681 uint32_t nibbles = APINT_BITS_PER_WORD / shift;
682 for (uint32_t i = 0; i < getNumWords(); ++i) {
683 uint64_t value = pVal[i];
684 for (uint32_t j = 0; j < nibbles; ++j) {
685 result.insert(0, digits[ value & mask ]);
686 value >>= shift;
687 }
688 }
689 return result;
690 }
691
Reid Spencer879dfe12007-02-14 02:52:25 +0000692 APInt tmp(*this);
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000693 APInt divisor(tmp.getBitWidth(), 10);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000694 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer443b5702007-02-18 00:44:22 +0000695 size_t insert_at = 0;
696 if (wantSigned && tmp[BitWidth-1]) {
697 // They want to print the signed version and it is a negative value
698 // Flip the bits and add one to turn it into the equivalent positive
699 // value and put a '-' in the result.
700 tmp.flip();
701 tmp++;
702 result = "-";
703 insert_at = 1;
704 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000705 if (tmp == 0)
706 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000707 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000708 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000709 uint32_t digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000710 assert(digit < radix && "urem failed");
Reid Spencer443b5702007-02-18 00:44:22 +0000711 result.insert(insert_at,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000712 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000713 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000714
715 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000716}
717
718/// getMaxValue - This function returns the largest value
719/// for an APInt of the specified bit-width and if isSign == true,
720/// it should be largest signed value, otherwise unsigned value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000721APInt APInt::getMaxValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000722 APInt Result(numBits, 0);
723 Result.set();
724 if (isSign)
725 Result.clear(numBits - 1);
726 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000727}
728
729/// getMinValue - This function returns the smallest value for
730/// an APInt of the given bit-width and if isSign == true,
731/// it should be smallest signed value, otherwise zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000732APInt APInt::getMinValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000733 APInt Result(numBits, 0);
734 if (isSign)
735 Result.set(numBits - 1);
736 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000737}
738
739/// getAllOnesValue - This function returns an all-ones value for
740/// an APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000741APInt APInt::getAllOnesValue(uint32_t numBits) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000742 return getMaxValue(numBits, false);
743}
744
745/// getNullValue - This function creates an '0' value for an
746/// APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000747APInt APInt::getNullValue(uint32_t numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000748 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000749}
750
751/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000752APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000753 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000754}
755
756/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000757APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000758 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
759 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000760}
761
Reid Spencere81d2da2007-02-16 22:36:51 +0000762bool APInt::isPowerOf2() const {
763 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
764}
765
766/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000767/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000768/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000769/// the number of zeros from the most significant bit to the first one bit.
770/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000771uint32_t APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000772 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000773 return CountLeadingZeros_64(VAL) - (APINT_BITS_PER_WORD - BitWidth);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000774 uint32_t Count = 0;
775 for (uint32_t i = getNumWords(); i > 0u; --i) {
776 uint32_t tmp = CountLeadingZeros_64(pVal[i-1]);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000777 Count += tmp;
Reid Spencer443b5702007-02-18 00:44:22 +0000778 if (tmp != APINT_BITS_PER_WORD)
779 if (i == getNumWords())
780 Count -= (APINT_BITS_PER_WORD - whichBit(BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000781 break;
782 }
783 return Count;
784}
785
Reid Spencere81d2da2007-02-16 22:36:51 +0000786/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000787/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000788/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000789/// the number of zeros from the least significant bit to the first one bit.
790/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000791uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000792 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000793 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000794 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000795 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000796}
797
Reid Spencere81d2da2007-02-16 22:36:51 +0000798/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000799/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000800/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000801/// @returns 0 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000802uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000803 if (isSingleWord())
804 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000805 uint32_t Count = 0;
806 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000807 Count += CountPopulation_64(pVal[i]);
808 return Count;
809}
810
811
Reid Spencere81d2da2007-02-16 22:36:51 +0000812/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000813/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000814APInt APInt::byteSwap() const {
815 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
816 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000817 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000818 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000819 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000820 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000821 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
822 Tmp1 = ByteSwap_32(Tmp1);
823 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
824 Tmp2 = ByteSwap_16(Tmp2);
825 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000826 APInt(BitWidth,
827 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000828 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000829 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000830 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000831 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000832 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000833 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000834 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000835 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
836 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000837 }
838 return Result;
839 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000840}
841
842/// GreatestCommonDivisor - This function returns the greatest common
843/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000844APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
845 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000846 APInt A = API1, B = API2;
847 while (!!B) {
848 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000849 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000850 A = T;
851 }
852 return A;
853}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000854
Zhou Shengd93f00c2007-02-12 20:02:55 +0000855/// DoubleRoundToAPInt - This function convert a double value to
856/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000857APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000858 union {
859 double D;
860 uint64_t I;
861 } T;
862 T.D = Double;
863 bool isNeg = T.I >> 63;
864 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
865 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000866 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000867 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
868 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000869 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
870 APInt(64u, mantissa >> (52 - exp));
871 APInt Tmp(exp + 1, mantissa);
872 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000873 return isNeg ? -Tmp : Tmp;
874}
875
Reid Spencerdb3faa62007-02-13 22:41:58 +0000876/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000877/// The layout for double is as following (IEEE Standard 754):
878/// --------------------------------------
879/// | Sign Exponent Fraction Bias |
880/// |-------------------------------------- |
881/// | 1[63] 11[62-52] 52[51-00] 1023 |
882/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000883double APInt::roundToDouble(bool isSigned) const {
Reid Spencera58f0582007-02-18 20:09:41 +0000884 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
885 if (isSigned) {
886 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
887 return double(sext);
888 } else
889 return double(VAL);
890 }
891
Reid Spencere81d2da2007-02-16 22:36:51 +0000892 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000893 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spenceraf0e9562007-02-18 18:38:44 +0000894 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000895 // Exponent when normalized to have decimal point directly after
896 // leading one. This is stored excess 1023 in the exponent bit field.
897 uint64_t exp = n - 1;
898
899 // Gross overflow.
900 assert(exp <= 1023 && "Infinity value!");
901
902 // Number of bits in mantissa including the leading one
903 // equals to 53.
904 uint64_t mantissa;
Reid Spencer443b5702007-02-18 00:44:22 +0000905 if (n % APINT_BITS_PER_WORD >= 53)
906 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % APINT_BITS_PER_WORD - 53);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000907 else
Reid Spencer443b5702007-02-18 00:44:22 +0000908 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % APINT_BITS_PER_WORD)) |
909 (Tmp.pVal[whichWord(n - 1) - 1] >>
910 (11 + n % APINT_BITS_PER_WORD));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000911 // The leading bit of mantissa is implicit, so get rid of it.
912 mantissa &= ~(1ULL << 52);
Reid Spencer443b5702007-02-18 00:44:22 +0000913 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000914 exp += 1023;
915 union {
916 double D;
917 uint64_t I;
918 } T;
919 T.I = sign | (exp << 52) | mantissa;
920 return T.D;
921}
922
Reid Spencere81d2da2007-02-16 22:36:51 +0000923// Truncate to new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000924void APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000925 assert(width < BitWidth && "Invalid APInt Truncate request");
926}
927
928// Sign extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000929void APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000930 assert(width > BitWidth && "Invalid APInt SignExtend request");
931}
932
933// Zero extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000934void APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000935 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
936}
937
Zhou Shengff4304f2007-02-09 07:48:24 +0000938/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000939/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000940APInt APInt::ashr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000941 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000942 if (API.isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000943 API.VAL =
944 (((int64_t(API.VAL) << (APINT_BITS_PER_WORD - API.BitWidth)) >>
945 (APINT_BITS_PER_WORD - API.BitWidth)) >> shiftAmt) &
946 (~uint64_t(0UL) >> (APINT_BITS_PER_WORD - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000947 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000948 if (shiftAmt >= API.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000949 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0,
Reid Spencera58f0582007-02-18 20:09:41 +0000950 (API.getNumWords()-1) * APINT_WORD_SIZE);
Reid Spencer443b5702007-02-18 00:44:22 +0000951 API.pVal[API.getNumWords() - 1] =
952 ~uint64_t(0UL) >>
953 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000954 } else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000955 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000956 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000957 if (API[i+shiftAmt])
958 API.set(i);
959 else
960 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000961 for (; i < API.BitWidth; ++i)
962 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000963 API.set(i);
964 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000965 }
966 }
967 return API;
968}
969
Zhou Shengff4304f2007-02-09 07:48:24 +0000970/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000971/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000972APInt APInt::lshr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000973 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000974 if (API.isSingleWord())
975 API.VAL >>= shiftAmt;
976 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000977 if (shiftAmt >= API.BitWidth)
Reid Spencera58f0582007-02-18 20:09:41 +0000978 memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000979 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000980 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000981 if (API[i+shiftAmt]) API.set(i);
982 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000983 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000984 API.clear(i);
985 }
986 return API;
987}
988
Zhou Shengff4304f2007-02-09 07:48:24 +0000989/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000990/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000991APInt APInt::shl(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000992 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000993 if (API.isSingleWord())
994 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000995 else if (shiftAmt >= API.BitWidth)
Reid Spencera58f0582007-02-18 20:09:41 +0000996 memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000997 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000998 if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
999 for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
Zhou Shengd93f00c2007-02-12 20:02:55 +00001000 API.pVal[i] = API.pVal[i-offset];
Reid Spencera58f0582007-02-18 20:09:41 +00001001 memset(API.pVal, 0, offset * APINT_WORD_SIZE);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001002 }
Reid Spencer443b5702007-02-18 00:44:22 +00001003 shiftAmt %= APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001004 uint32_t i;
Zhou Shengd93f00c2007-02-12 20:02:55 +00001005 for (i = API.getNumWords() - 1; i > 0; --i)
1006 API.pVal[i] = (API.pVal[i] << shiftAmt) |
Reid Spencer443b5702007-02-18 00:44:22 +00001007 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
Zhou Shengd93f00c2007-02-12 20:02:55 +00001008 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001009 }
Reid Spencere81d2da2007-02-16 22:36:51 +00001010 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001011 return API;
1012}
1013
Reid Spencer5e0a8512007-02-17 03:16:00 +00001014/// subMul - This function substracts x[len-1:0] * y from
1015/// dest[offset+len-1:offset], and returns the most significant
1016/// word of the product, minus the borrow-out from the subtraction.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001017static uint32_t subMul(uint32_t dest[], uint32_t offset,
1018 uint32_t x[], uint32_t len, uint32_t y) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001019 uint64_t yl = (uint64_t) y & 0xffffffffL;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001020 uint32_t carry = 0;
1021 uint32_t j = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001022 do {
Reid Spencerc72f2802007-02-17 22:38:07 +00001023 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001024 uint32_t prod_low = (uint32_t) prod;
1025 uint32_t prod_high = (uint32_t) (prod >> 32);
Reid Spencer5e0a8512007-02-17 03:16:00 +00001026 prod_low += carry;
1027 carry = (prod_low < carry ? 1 : 0) + prod_high;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001028 uint32_t x_j = dest[offset+j];
Reid Spencer5e0a8512007-02-17 03:16:00 +00001029 prod_low = x_j - prod_low;
1030 if (prod_low > x_j) ++carry;
1031 dest[offset+j] = prod_low;
1032 } while (++j < len);
1033 return carry;
1034}
1035
1036/// unitDiv - This function divides N by D,
1037/// and returns (remainder << 32) | quotient.
1038/// Assumes (N >> 32) < D.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001039static uint64_t unitDiv(uint64_t N, uint32_t D) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001040 uint64_t q, r; // q: quotient, r: remainder.
1041 uint64_t a1 = N >> 32; // a1: high 32-bit part of N.
1042 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N
1043 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) {
1044 q = N / D;
1045 r = N % D;
1046 }
1047 else {
1048 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d
1049 uint64_t c = N - ((uint64_t) D << 31);
1050 // Divide (c1*2^32 + c0) by d
1051 q = c / D;
1052 r = c % D;
1053 // Add 2^31 to quotient
1054 q += 1 << 31;
1055 }
1056
1057 return (r << 32) | (q & 0xFFFFFFFFl);
1058}
1059
1060/// div - This is basically Knuth's formulation of the classical algorithm.
1061/// Correspondance with Knuth's notation:
1062/// Knuth's u[0:m+n] == zds[nx:0].
1063/// Knuth's v[1:n] == y[ny-1:0]
1064/// Knuth's n == ny.
1065/// Knuth's m == nx-ny.
1066/// Our nx == Knuth's m+n.
1067/// Could be re-implemented using gmp's mpn_divrem:
1068/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny).
Reid Spenceraf0e9562007-02-18 18:38:44 +00001069static void div(uint32_t zds[], uint32_t nx, uint32_t y[], uint32_t ny) {
1070 uint32_t j = nx;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001071 do { // loop over digits of quotient
1072 // Knuth's j == our nx-j.
1073 // Knuth's u[j:j+n] == our zds[j:j-ny].
Reid Spenceraf0e9562007-02-18 18:38:44 +00001074 uint32_t qhat; // treated as unsigned
Reid Spencer5e0a8512007-02-17 03:16:00 +00001075 if (zds[j] == y[ny-1])
1076 qhat = -1U; // 0xffffffff
1077 else {
1078 uint64_t w = (((uint64_t)(zds[j])) << 32) +
1079 ((uint64_t)zds[j-1] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001080 qhat = (uint32_t) unitDiv(w, y[ny-1]);
Reid Spencer5e0a8512007-02-17 03:16:00 +00001081 }
1082 if (qhat) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001083 uint32_t borrow = subMul(zds, j - ny, y, ny, qhat);
1084 uint32_t save = zds[j];
Reid Spencer5e0a8512007-02-17 03:16:00 +00001085 uint64_t num = ((uint64_t)save&0xffffffffL) -
1086 ((uint64_t)borrow&0xffffffffL);
1087 while (num) {
1088 qhat--;
1089 uint64_t carry = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001090 for (uint32_t i = 0; i < ny; i++) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001091 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL)
1092 + ((uint64_t) y[i] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001093 zds[j-ny+i] = (uint32_t) carry;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001094 carry >>= 32;
1095 }
1096 zds[j] += carry;
1097 num = carry - 1;
1098 }
1099 }
1100 zds[j] = qhat;
1101 } while (--j >= ny);
1102}
1103
Zhou Shengff4304f2007-02-09 07:48:24 +00001104/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001105/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001106APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001107 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001108
1109 // First, deal with the easy case
1110 if (isSingleWord()) {
1111 assert(RHS.VAL != 0 && "Divide by zero?");
1112 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001113 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001114
1115 // Make a temporary to hold the result
1116 APInt Result(*this);
1117
1118 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001119 uint32_t rhsBits = RHS.getActiveBits();
1120 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001121 assert(rhsWords && "Divided by zero???");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001122 uint32_t lhsBits = Result.getActiveBits();
1123 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001124
1125 // Deal with some degenerate cases
1126 if (!lhsWords)
1127 return Result; // 0 / X == 0
1128 else if (lhsWords < rhsWords || Result.ult(RHS))
1129 // X / Y with X < Y == 0
Reid Spencera58f0582007-02-18 20:09:41 +00001130 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001131 else if (Result == RHS) {
1132 // X / X == 1
Reid Spencera58f0582007-02-18 20:09:41 +00001133 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001134 Result.pVal[0] = 1;
1135 } else if (lhsWords == 1)
1136 // All high words are zero, just use native divide
1137 Result.pVal[0] /= RHS.pVal[0];
1138 else {
1139 // Compute it the hard way ..
1140 APInt X(BitWidth, 0);
1141 APInt Y(BitWidth, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001142 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001143 (APINT_BITS_PER_WORD - 1) - ((rhsBits - 1) % APINT_BITS_PER_WORD );
1144 if (nshift) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001145 Y = APIntOps::shl(RHS, nshift);
1146 X = APIntOps::shl(Result, nshift);
1147 ++lhsWords;
1148 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001149 div((uint32_t*)X.pVal, lhsWords * 2 - 1,
1150 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencera58f0582007-02-18 20:09:41 +00001151 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001152 memcpy(Result.pVal, X.pVal + rhsWords,
Reid Spencera58f0582007-02-18 20:09:41 +00001153 (lhsWords - rhsWords) * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001154 }
1155 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001156}
1157
1158/// Unsigned remainder operation on APInt.
1159/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001160APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001161 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001162 if (isSingleWord()) {
1163 assert(RHS.VAL != 0 && "Remainder by zero?");
1164 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001165 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001166
1167 // Make a temporary to hold the result
1168 APInt Result(*this);
1169
1170 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001171 uint32_t rhsBits = RHS.getActiveBits();
1172 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001173 assert(rhsWords && "Performing remainder operation by zero ???");
1174
1175 // Get some facts about the LHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001176 uint32_t lhsBits = Result.getActiveBits();
1177 uint32_t lhsWords = !lhsBits ? 0 : (Result.whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001178
1179 // Check the degenerate cases
1180 if (lhsWords == 0)
1181 // 0 % Y == 0
Reid Spencera58f0582007-02-18 20:09:41 +00001182 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001183 else if (lhsWords < rhsWords || Result.ult(RHS))
1184 // X % Y == X iff X < Y
1185 return Result;
1186 else if (Result == RHS)
1187 // X % X == 0;
Reid Spencera58f0582007-02-18 20:09:41 +00001188 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001189 else if (lhsWords == 1)
1190 // All high words are zero, just use native remainder
1191 Result.pVal[0] %= RHS.pVal[0];
1192 else {
1193 // Do it the hard way
Reid Spencer443b5702007-02-18 00:44:22 +00001194 APInt X((lhsWords+1)*APINT_BITS_PER_WORD, 0);
1195 APInt Y(rhsWords*APINT_BITS_PER_WORD, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001196 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001197 (APINT_BITS_PER_WORD - 1) - (rhsBits - 1) % APINT_BITS_PER_WORD;
Reid Spencer71bd08f2007-02-17 02:07:07 +00001198 if (nshift) {
1199 APIntOps::shl(Y, nshift);
1200 APIntOps::shl(X, nshift);
1201 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001202 div((uint32_t*)X.pVal, rhsWords*2-1,
1203 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
Reid Spencera58f0582007-02-18 20:09:41 +00001204 memset(Result.pVal, 0, Result.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001205 for (uint32_t i = 0; i < rhsWords-1; ++i)
Reid Spencer443b5702007-02-18 00:44:22 +00001206 Result.pVal[i] = (X.pVal[i] >> nshift) |
1207 (X.pVal[i+1] << (APINT_BITS_PER_WORD - nshift));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001208 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift;
1209 }
1210 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001211}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001212
1213/// @brief Converts a char array into an integer.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001214void APInt::fromString(uint32_t numbits, const char *StrStart, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001215 uint8_t radix) {
1216 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1217 "Radix should be 2, 8, 10, or 16!");
1218 assert(StrStart && "String is null?");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001219 uint32_t size = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001220 // If the radix is a power of 2, read the input
1221 // from most significant to least significant.
1222 if ((radix & (radix - 1)) == 0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001223 uint32_t nextBitPos = 0;
1224 uint32_t bits_per_digit = radix / 8 + 2;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001225 uint64_t resDigit = 0;
1226 BitWidth = slen * bits_per_digit;
1227 if (getNumWords() > 1)
Reid Spenceraf0e9562007-02-18 18:38:44 +00001228 pVal = getMemory(getNumWords());
Reid Spencer5e0a8512007-02-17 03:16:00 +00001229 for (int i = slen - 1; i >= 0; --i) {
Reid Spencer443b5702007-02-18 00:44:22 +00001230 uint64_t digit = StrStart[i] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001231 resDigit |= digit << nextBitPos;
1232 nextBitPos += bits_per_digit;
Reid Spencer443b5702007-02-18 00:44:22 +00001233 if (nextBitPos >= APINT_BITS_PER_WORD) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001234 if (isSingleWord()) {
1235 VAL = resDigit;
1236 break;
1237 }
1238 pVal[size++] = resDigit;
Reid Spencer443b5702007-02-18 00:44:22 +00001239 nextBitPos -= APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001240 resDigit = digit >> (bits_per_digit - nextBitPos);
1241 }
1242 }
1243 if (!isSingleWord() && size <= getNumWords())
1244 pVal[size] = resDigit;
1245 } else { // General case. The radix is not a power of 2.
1246 // For 10-radix, the max value of 64-bit integer is 18446744073709551615,
1247 // and its digits number is 20.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001248 const uint32_t chars_per_word = 20;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001249 if (slen < chars_per_word ||
1250 (slen == chars_per_word && // In case the value <= 2^64 - 1
1251 strcmp(StrStart, "18446744073709551615") <= 0)) {
Reid Spencer443b5702007-02-18 00:44:22 +00001252 BitWidth = APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001253 VAL = strtoull(StrStart, 0, 10);
1254 } else { // In case the value > 2^64 - 1
Reid Spencer443b5702007-02-18 00:44:22 +00001255 BitWidth = (slen / chars_per_word + 1) * APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001256 pVal = getClearedMemory(getNumWords());
1257 uint32_t str_pos = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001258 while (str_pos < slen) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001259 uint32_t chunk = slen - str_pos;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001260 if (chunk > chars_per_word - 1)
1261 chunk = chars_per_word - 1;
Reid Spencer443b5702007-02-18 00:44:22 +00001262 uint64_t resDigit = StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001263 uint64_t big_base = radix;
1264 while (--chunk > 0) {
Reid Spencer443b5702007-02-18 00:44:22 +00001265 resDigit = resDigit * radix + StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001266 big_base *= radix;
1267 }
1268
1269 uint64_t carry;
1270 if (!size)
1271 carry = resDigit;
1272 else {
1273 carry = mul_1(pVal, pVal, size, big_base);
1274 carry += add_1(pVal, pVal, size, resDigit);
1275 }
1276
1277 if (carry) pVal[size++] = carry;
1278 }
1279 }
1280 }
1281}