blob: f8c3f4ae22e9ea9f11d06f993c9d0087f1a68148 [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());
62 memcpy(pVal, bigVal, (minN - 1) * sizeof(uint64_t));
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 Spenceraf0e9562007-02-18 18:38:44 +000067 memset(pVal+numWords, 0, (getNumWords() - numWords) * sizeof(uint64_t));
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
Zhou Shengfd43dcf2007-02-06 03:00:16 +000085APInt::APInt(const APInt& APIVal)
Reid Spencere81d2da2007-02-16 22:36:51 +000086 : BitWidth(APIVal.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +000087 if (isSingleWord())
88 VAL = APIVal.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000089 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000090 pVal = getMemory(getNumWords());
91 memcpy(pVal, APIVal.pVal, getNumWords() * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +000092 }
93}
94
95APInt::~APInt() {
96 if (!isSingleWord() && pVal) delete[] pVal;
97}
98
Zhou Shengfd43dcf2007-02-06 03:00:16 +000099/// @brief Copy assignment operator. Create a new object from the given
100/// APInt one by initialization.
101APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000102 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
103 if (isSingleWord())
Reid Spenceraf0e9562007-02-18 18:38:44 +0000104 VAL = RHS.VAL;
105 else
106 memcpy(pVal, RHS.pVal, getNumWords() * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000107 return *this;
108}
109
110/// @brief Assignment operator. Assigns a common case integer value to
111/// the APInt.
112APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000113 if (isSingleWord())
114 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000115 else {
116 pVal[0] = RHS;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000117 memset(pVal+1, 0, (getNumWords() - 1) * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000118 }
119 return *this;
120}
121
Reid Spenceraf0e9562007-02-18 18:38:44 +0000122/// add_1 - This function adds a single "digit" integer, y, to the multiple
123/// "digit" integer array, x[]. x[] is modified to reflect the addition and
124/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000125/// @returns the carry of the addition.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000126static uint64_t add_1(uint64_t dest[],
127 uint64_t x[], uint32_t len,
128 uint64_t y) {
129 for (uint32_t i = 0; i < len; ++i) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000130 dest[i] = y + x[i];
131 if (dest[i] < y)
132 y = 1;
133 else {
134 y = 0;
135 break;
136 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000137 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000138 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000139}
140
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000141/// @brief Prefix increment operator. Increments the APInt by one.
142APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000143 if (isSingleWord())
144 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000145 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000146 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000147 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000148 return *this;
149}
150
Reid Spenceraf0e9562007-02-18 18:38:44 +0000151/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
152/// the multi-digit integer array, x[], propagating the borrowed 1 value until
153/// no further borrowing is neeeded or it runs out of "digits" in x. The result
154/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
155/// In other words, if y > x then this function returns 1, otherwise 0.
156static uint64_t sub_1(uint64_t x[], uint32_t len,
157 uint64_t y) {
158 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000159 uint64_t X = x[i];
Reid Spencerf2c521c2007-02-18 06:39:42 +0000160 x[i] -= y;
161 if (y > X)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000162 y = 1; // We have to "borrow 1" from next "digit"
Reid Spencer5e0a8512007-02-17 03:16:00 +0000163 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000164 y = 0; // No need to borrow
165 break; // Remaining digits are unchanged so exit early
Reid Spencer5e0a8512007-02-17 03:16:00 +0000166 }
167 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000168 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000169}
170
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000171/// @brief Prefix decrement operator. Decrements the APInt by one.
172APInt& APInt::operator--() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000173 if (isSingleWord())
174 --VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000175 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000176 sub_1(pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000177 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000178 return *this;
179}
180
Reid Spencer5e0a8512007-02-17 03:16:00 +0000181/// add - This function adds the integer array x[] by integer array
182/// y[] and returns the carry.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000183static uint64_t add(uint64_t dest[], uint64_t x[],
184 uint64_t y[], uint32_t len) {
185 uint32_t carry = 0;
186 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000187 carry += x[i];
188 dest[i] = carry + y[i];
189 carry = carry < x[i] ? 1 : (dest[i] < carry ? 1 : 0);
190 }
191 return carry;
192}
193
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000194/// @brief Addition assignment operator. Adds this APInt by the given APInt&
195/// RHS and assigns the result to this APInt.
196APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000197 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000198 if (isSingleWord()) VAL += RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
199 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000200 if (RHS.isSingleWord()) add_1(pVal, pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000202 if (getNumWords() <= RHS.getNumWords())
203 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000204 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000205 uint64_t carry = add(pVal, pVal, RHS.pVal, RHS.getNumWords());
206 add_1(pVal + RHS.getNumWords(), pVal + RHS.getNumWords(),
207 getNumWords() - RHS.getNumWords(), carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000208 }
209 }
210 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000211 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000212 return *this;
213}
214
Reid Spencer5e0a8512007-02-17 03:16:00 +0000215/// sub - This function subtracts the integer array x[] by
216/// integer array y[], and returns the borrow-out carry.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000217static uint64_t sub(uint64_t dest[], uint64_t x[],
218 uint64_t y[], uint32_t len) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000219 // Carry indicator.
220 uint64_t cy = 0;
221
Reid Spenceraf0e9562007-02-18 18:38:44 +0000222 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000223 uint64_t Y = y[i], X = x[i];
224 Y += cy;
225
226 cy = Y < cy ? 1 : 0;
227 Y = X - Y;
228 cy += Y > X ? 1 : 0;
229 dest[i] = Y;
230 }
231 return cy;
232}
233
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000234/// @brief Subtraction assignment operator. Subtracts this APInt by the given
235/// APInt &RHS and assigns the result to this APInt.
236APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000237 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000238 if (isSingleWord())
239 VAL -= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
240 else {
241 if (RHS.isSingleWord())
Zhou Shenga3832fd2007-02-07 06:14:53 +0000242 sub_1(pVal, getNumWords(), RHS.VAL);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000243 else {
Zhou Shenga3832fd2007-02-07 06:14:53 +0000244 if (RHS.getNumWords() < getNumWords()) {
245 uint64_t carry = sub(pVal, pVal, RHS.pVal, RHS.getNumWords());
Reid Spencerf2c521c2007-02-18 06:39:42 +0000246 sub_1(pVal + RHS.getNumWords(), getNumWords() - RHS.getNumWords(),
247 carry);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000248 }
249 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000250 sub(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000251 }
252 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000253 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000254 return *this;
255}
256
Reid Spencer5e0a8512007-02-17 03:16:00 +0000257/// mul_1 - This function performs the multiplication operation on a
258/// large integer (represented as an integer array) and a uint64_t integer.
259/// @returns the carry of the multiplication.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000260static uint64_t mul_1(uint64_t dest[],
261 uint64_t x[], uint32_t len,
262 uint64_t y) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000263 // Split y into high 32-bit part and low 32-bit part.
264 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
265 uint64_t carry = 0, lx, hx;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000266 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000267 lx = x[i] & 0xffffffffULL;
268 hx = x[i] >> 32;
269 // hasCarry - A flag to indicate if has carry.
270 // hasCarry == 0, no carry
271 // hasCarry == 1, has carry
272 // hasCarry == 2, no carry and the calculation result == 0.
273 uint8_t hasCarry = 0;
274 dest[i] = carry + lx * ly;
275 // Determine if the add above introduces carry.
276 hasCarry = (dest[i] < carry) ? 1 : 0;
277 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
278 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
279 // (2^32 - 1) + 2^32 = 2^64.
280 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
281
282 carry += (lx * hy) & 0xffffffffULL;
283 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
284 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
285 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
286 }
287
288 return carry;
289}
290
291/// mul - This function multiplies integer array x[] by integer array y[] and
292/// stores the result into integer array dest[].
293/// Note the array dest[]'s size should no less than xlen + ylen.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000294static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen,
295 uint64_t y[], uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000296 dest[xlen] = mul_1(dest, x, xlen, y[0]);
297
Reid Spenceraf0e9562007-02-18 18:38:44 +0000298 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000299 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
300 uint64_t carry = 0, lx, hx;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000301 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000302 lx = x[j] & 0xffffffffULL;
303 hx = x[j] >> 32;
304 // hasCarry - A flag to indicate if has carry.
305 // hasCarry == 0, no carry
306 // hasCarry == 1, has carry
307 // hasCarry == 2, no carry and the calculation result == 0.
308 uint8_t hasCarry = 0;
309 uint64_t resul = carry + lx * ly;
310 hasCarry = (resul < carry) ? 1 : 0;
311 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
312 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
313
314 carry += (lx * hy) & 0xffffffffULL;
315 resul = (carry << 32) | (resul & 0xffffffffULL);
316 dest[i+j] += resul;
317 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
318 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
319 ((lx * hy) >> 32) + hx * hy;
320 }
321 dest[i+xlen] = carry;
322 }
323}
324
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000325/// @brief Multiplication assignment operator. Multiplies this APInt by the
326/// given APInt& RHS and assigns the result to this APInt.
327APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000328 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000329 if (isSingleWord()) VAL *= RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
330 else {
331 // one-based first non-zero bit position.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000332 uint32_t first = getActiveBits();
333 uint32_t xlen = !first ? 0 : whichWord(first - 1) + 1;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000334 if (!xlen)
335 return *this;
336 else if (RHS.isSingleWord())
337 mul_1(pVal, pVal, xlen, RHS.VAL);
338 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000339 first = RHS.getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +0000340 uint32_t ylen = !first ? 0 : whichWord(first - 1) + 1;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000341 if (!ylen) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000342 memset(pVal, 0, getNumWords() * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000343 return *this;
344 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000345 uint64_t *dest = getMemory(xlen+ylen);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000346 mul(dest, pVal, xlen, RHS.pVal, ylen);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000347 memcpy(pVal, dest, ((xlen + ylen >= getNumWords()) ?
Reid Spenceraf0e9562007-02-18 18:38:44 +0000348 getNumWords() : xlen + ylen) * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000349 delete[] dest;
350 }
351 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000352 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000353 return *this;
354}
355
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000356/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
357/// this APInt and the given APInt& RHS, assigns the result to this APInt.
358APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000359 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000360 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000361 VAL &= RHS.VAL;
362 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000363 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000364 uint32_t numWords = getNumWords();
365 for (uint32_t i = 0; i < numWords; ++i)
366 pVal[i] &= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000367 return *this;
368}
369
370/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on
371/// this APInt and the given APInt& RHS, assigns the result to this APInt.
372APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000373 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000374 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000375 VAL |= RHS.VAL;
376 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000377 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000378 uint32_t numWords = getNumWords();
379 for (uint32_t i = 0; i < numWords; ++i)
380 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000381 return *this;
382}
383
384/// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on
385/// this APInt and the given APInt& RHS, assigns the result to this APInt.
386APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000387 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000388 if (isSingleWord()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000389 VAL ^= RHS.VAL;
390 return *this;
391 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000392 uint32_t numWords = getNumWords();
393 for (uint32_t i = 0; i < numWords; ++i)
394 pVal[i] ^= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000395 return *this;
396}
397
398/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
399/// and the given APInt& RHS.
400APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000401 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000402 if (isSingleWord())
403 return APInt(getBitWidth(), VAL & RHS.VAL);
404
405 APInt Result(*this);
406 uint32_t numWords = getNumWords();
407 for (uint32_t i = 0; i < numWords; ++i)
408 Result.pVal[i] &= RHS.pVal[i];
409 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000410}
411
412/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt
413/// and the given APInt& RHS.
414APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000415 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000416 if (isSingleWord())
417 return APInt(getBitWidth(), VAL | RHS.VAL);
418 APInt Result(*this);
419 uint32_t numWords = getNumWords();
420 for (uint32_t i = 0; i < numWords; ++i)
421 Result.pVal[i] |= RHS.pVal[i];
422 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000423}
424
425/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
426/// and the given APInt& RHS.
427APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000428 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000429 if (isSingleWord())
430 return APInt(getBitWidth(), VAL ^ RHS.VAL);
431 APInt Result(*this);
432 uint32_t numWords = getNumWords();
433 for (uint32_t i = 0; i < numWords; ++i)
434 Result.pVal[i] ^= RHS.pVal[i];
435 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000436}
437
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000438/// @brief Logical negation operator. Performs logical negation operation on
439/// this APInt.
440bool APInt::operator !() const {
441 if (isSingleWord())
442 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000443
444 for (uint32_t i = 0; i < getNumWords(); ++i)
445 if (pVal[i])
446 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000447 return true;
448}
449
450/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
451/// RHS.
452APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000453 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000454 APInt API(RHS);
455 API *= *this;
Reid Spencere81d2da2007-02-16 22:36:51 +0000456 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000457 return API;
458}
459
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000460/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
461APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000462 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000463 APInt API(*this);
464 API += RHS;
Reid Spencere81d2da2007-02-16 22:36:51 +0000465 API.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000466 return API;
467}
468
469/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
470APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000471 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000472 APInt API(*this);
473 API -= RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000474 return API;
475}
476
477/// @brief Array-indexing support.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000478bool APInt::operator[](uint32_t bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000479 return (maskBit(bitPosition) & (isSingleWord() ?
480 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000481}
482
483/// @brief Equality operator. Compare this APInt with the given APInt& RHS
484/// for the validity of the equality relationship.
485bool APInt::operator==(const APInt& RHS) const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000486 uint32_t n1 = getActiveBits();
487 uint32_t n2 = RHS.getActiveBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000488 if (n1 != n2) return false;
489 else if (isSingleWord())
490 return VAL == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
491 else {
Reid Spencer443b5702007-02-18 00:44:22 +0000492 if (n1 <= APINT_BITS_PER_WORD)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000493 return pVal[0] == (RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0]);
494 for (int i = whichWord(n1 - 1); i >= 0; --i)
495 if (pVal[i] != RHS.pVal[i]) return false;
496 }
497 return true;
498}
499
Zhou Shenga3832fd2007-02-07 06:14:53 +0000500/// @brief Equality operator. Compare this APInt with the given uint64_t value
501/// for the validity of the equality relationship.
502bool APInt::operator==(uint64_t Val) const {
503 if (isSingleWord())
504 return VAL == Val;
505 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000506 uint32_t n = getActiveBits();
Reid Spencer443b5702007-02-18 00:44:22 +0000507 if (n <= APINT_BITS_PER_WORD)
Zhou Shenga3832fd2007-02-07 06:14:53 +0000508 return pVal[0] == Val;
509 else
510 return false;
511 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000512}
513
Reid Spencere81d2da2007-02-16 22:36:51 +0000514/// @brief Unsigned less than comparison
515bool APInt::ult(const APInt& RHS) const {
516 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
517 if (isSingleWord())
518 return VAL < RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000519 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000520 uint32_t n1 = getActiveBits();
521 uint32_t n2 = RHS.getActiveBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000522 if (n1 < n2)
523 return true;
524 else if (n2 < n1)
525 return false;
Reid Spencer443b5702007-02-18 00:44:22 +0000526 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000527 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000528 for (int i = whichWord(n1 - 1); i >= 0; --i) {
529 if (pVal[i] > RHS.pVal[i]) return false;
530 else if (pVal[i] < RHS.pVal[i]) return true;
531 }
532 }
533 return false;
534}
535
Reid Spencere81d2da2007-02-16 22:36:51 +0000536/// @brief Signed less than comparison
537bool APInt::slt(const APInt& RHS) const {
538 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
539 if (isSingleWord())
540 return VAL < RHS.VAL;
541 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000542 uint32_t n1 = getActiveBits();
543 uint32_t n2 = RHS.getActiveBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000544 if (n1 < n2)
545 return true;
546 else if (n2 < n1)
547 return false;
Reid Spencer443b5702007-02-18 00:44:22 +0000548 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000549 return pVal[0] < RHS.pVal[0];
550 for (int i = whichWord(n1 - 1); i >= 0; --i) {
551 if (pVal[i] > RHS.pVal[i]) return false;
552 else if (pVal[i] < RHS.pVal[i]) return true;
553 }
554 }
555 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000556}
557
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000558/// Set the given bit to 1 whose poition is given as "bitPosition".
559/// @brief Set a given bit to 1.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000560APInt& APInt::set(uint32_t bitPosition) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000561 if (isSingleWord()) VAL |= maskBit(bitPosition);
562 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
563 return *this;
564}
565
566/// @brief Set every bit to 1.
567APInt& APInt::set() {
Reid Spencer443b5702007-02-18 00:44:22 +0000568 if (isSingleWord())
569 VAL = ~0ULL >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000570 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000571 for (uint32_t i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000572 pVal[i] = -1ULL;
Reid Spencer443b5702007-02-18 00:44:22 +0000573 pVal[getNumWords() - 1] = ~0ULL >>
574 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD);
Zhou Shengb04973e2007-02-15 06:36:31 +0000575 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000576 return *this;
577}
578
579/// Set the given bit to 0 whose position is given as "bitPosition".
580/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000581APInt& APInt::clear(uint32_t bitPosition) {
582 if (isSingleWord())
583 VAL &= ~maskBit(bitPosition);
584 else
585 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000586 return *this;
587}
588
589/// @brief Set every bit to 0.
590APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000591 if (isSingleWord())
592 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000593 else
Reid Spenceraf0e9562007-02-18 18:38:44 +0000594 memset(pVal, 0, getNumWords() * sizeof(uint64_t));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000595 return *this;
596}
597
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000598/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
599/// this APInt.
600APInt APInt::operator~() const {
601 APInt API(*this);
602 API.flip();
603 return API;
604}
605
606/// @brief Toggle every bit to its opposite value.
607APInt& APInt::flip() {
Reid Spencer443b5702007-02-18 00:44:22 +0000608 if (isSingleWord()) VAL = (~(VAL <<
609 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000610 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000611 uint32_t i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000612 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000613 pVal[i] = ~pVal[i];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000614 uint32_t offset =
Reid Spencer443b5702007-02-18 00:44:22 +0000615 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000616 pVal[i] = (~(pVal[i] << offset)) >> offset;
617 }
618 return *this;
619}
620
621/// Toggle a given bit to its opposite value whose position is given
622/// as "bitPosition".
623/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000624APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000625 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000626 if ((*this)[bitPosition]) clear(bitPosition);
627 else set(bitPosition);
628 return *this;
629}
630
631/// to_string - This function translates the APInt into a string.
Reid Spencer443b5702007-02-18 00:44:22 +0000632std::string APInt::toString(uint8_t radix, bool wantSigned) const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000633 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
634 "Radix should be 2, 8, 10, or 16!");
Reid Spencer879dfe12007-02-14 02:52:25 +0000635 static const char *digits[] = {
636 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
637 };
638 std::string result;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000639 uint32_t bits_used = getActiveBits();
Reid Spencer879dfe12007-02-14 02:52:25 +0000640 if (isSingleWord()) {
641 char buf[65];
Reid Spencer443b5702007-02-18 00:44:22 +0000642 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
Reid Spencer879dfe12007-02-14 02:52:25 +0000643 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
644 if (format) {
Reid Spencer443b5702007-02-18 00:44:22 +0000645 if (wantSigned) {
646 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
647 (APINT_BITS_PER_WORD-BitWidth);
648 sprintf(buf, format, sextVal);
649 } else
650 sprintf(buf, format, VAL);
Reid Spencer879dfe12007-02-14 02:52:25 +0000651 } else {
652 memset(buf, 0, 65);
653 uint64_t v = VAL;
654 while (bits_used) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000655 uint32_t bit = v & 1;
Reid Spencer879dfe12007-02-14 02:52:25 +0000656 bits_used--;
657 buf[bits_used] = digits[bit][0];
658 v >>=1;
659 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000660 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000661 result = buf;
662 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000663 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000664
665 APInt tmp(*this);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000666 APInt divisor(tmp.getBitWidth(), radix);
667 APInt zero(tmp.getBitWidth(), 0);
Reid Spencer443b5702007-02-18 00:44:22 +0000668 size_t insert_at = 0;
669 if (wantSigned && tmp[BitWidth-1]) {
670 // They want to print the signed version and it is a negative value
671 // Flip the bits and add one to turn it into the equivalent positive
672 // value and put a '-' in the result.
673 tmp.flip();
674 tmp++;
675 result = "-";
676 insert_at = 1;
677 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000678 if (tmp == 0)
679 result = "0";
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000680 else while (tmp.ne(zero)) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000681 APInt APdigit = APIntOps::urem(tmp,divisor);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000682 uint32_t digit = APdigit.getValue();
Reid Spencere81d2da2007-02-16 22:36:51 +0000683 assert(digit < radix && "urem failed");
Reid Spencer443b5702007-02-18 00:44:22 +0000684 result.insert(insert_at,digits[digit]);
Reid Spencere81d2da2007-02-16 22:36:51 +0000685 tmp = APIntOps::udiv(tmp, divisor);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000686 }
Reid Spencer879dfe12007-02-14 02:52:25 +0000687
688 return result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000689}
690
691/// getMaxValue - This function returns the largest value
692/// for an APInt of the specified bit-width and if isSign == true,
693/// it should be largest signed value, otherwise unsigned value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000694APInt APInt::getMaxValue(uint32_t numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000695 APInt APIVal(numBits, 0);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000696 APIVal.set();
Zhou Shengb04973e2007-02-15 06:36:31 +0000697 if (isSign) APIVal.clear(numBits - 1);
698 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000699}
700
701/// getMinValue - This function returns the smallest value for
702/// an APInt of the given bit-width and if isSign == true,
703/// it should be smallest signed value, otherwise zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000704APInt APInt::getMinValue(uint32_t numBits, bool isSign) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000705 APInt APIVal(numBits, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000706 if (isSign) APIVal.set(numBits - 1);
707 return APIVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000708}
709
710/// getAllOnesValue - This function returns an all-ones value for
711/// an APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000712APInt APInt::getAllOnesValue(uint32_t numBits) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000713 return getMaxValue(numBits, false);
714}
715
716/// getNullValue - This function creates an '0' value for an
717/// APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000718APInt APInt::getNullValue(uint32_t numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000719 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000720}
721
722/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000723APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000724 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000725}
726
727/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000728APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000729 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
730 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000731}
732
Reid Spencere81d2da2007-02-16 22:36:51 +0000733bool APInt::isPowerOf2() const {
734 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
735}
736
737/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000738/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000739/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740/// the number of zeros from the most significant bit to the first one bit.
741/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000742uint32_t APInt::countLeadingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000743 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000744 return CountLeadingZeros_64(VAL) - (APINT_BITS_PER_WORD - BitWidth);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000745 uint32_t Count = 0;
746 for (uint32_t i = getNumWords(); i > 0u; --i) {
747 uint32_t tmp = CountLeadingZeros_64(pVal[i-1]);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000748 Count += tmp;
Reid Spencer443b5702007-02-18 00:44:22 +0000749 if (tmp != APINT_BITS_PER_WORD)
750 if (i == getNumWords())
751 Count -= (APINT_BITS_PER_WORD - whichBit(BitWidth));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000752 break;
753 }
754 return Count;
755}
756
Reid Spencere81d2da2007-02-16 22:36:51 +0000757/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000758/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000759/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000760/// the number of zeros from the least significant bit to the first one bit.
761/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000762uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000763 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000764 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000765 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000766 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000767}
768
Reid Spencere81d2da2007-02-16 22:36:51 +0000769/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000770/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000771/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000772/// @returns 0 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000773uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000774 if (isSingleWord())
775 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000776 uint32_t Count = 0;
777 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000778 Count += CountPopulation_64(pVal[i]);
779 return Count;
780}
781
782
Reid Spencere81d2da2007-02-16 22:36:51 +0000783/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000784/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000785APInt APInt::byteSwap() const {
786 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
787 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000788 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000789 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000790 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000791 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000792 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
793 Tmp1 = ByteSwap_32(Tmp1);
794 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
795 Tmp2 = ByteSwap_16(Tmp2);
796 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000797 APInt(BitWidth,
798 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000799 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000800 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000801 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000802 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000803 char *pByte = (char*)Result.pVal;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000804 for (uint32_t i = 0; i < BitWidth / sizeof(uint64_t) / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000805 char Tmp = pByte[i];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000806 pByte[i] = pByte[BitWidth / sizeof(uint64_t) - 1 - i];
807 pByte[BitWidth / sizeof(uint64_t) - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000808 }
809 return Result;
810 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000811}
812
813/// GreatestCommonDivisor - This function returns the greatest common
814/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000815APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
816 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000817 APInt A = API1, B = API2;
818 while (!!B) {
819 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000820 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000821 A = T;
822 }
823 return A;
824}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000825
Zhou Shengd93f00c2007-02-12 20:02:55 +0000826/// DoubleRoundToAPInt - This function convert a double value to
827/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000828APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000829 union {
830 double D;
831 uint64_t I;
832 } T;
833 T.D = Double;
834 bool isNeg = T.I >> 63;
835 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
836 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000837 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000838 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
839 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000840 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
841 APInt(64u, mantissa >> (52 - exp));
842 APInt Tmp(exp + 1, mantissa);
843 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000844 return isNeg ? -Tmp : Tmp;
845}
846
Reid Spencerdb3faa62007-02-13 22:41:58 +0000847/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000848/// The layout for double is as following (IEEE Standard 754):
849/// --------------------------------------
850/// | Sign Exponent Fraction Bias |
851/// |-------------------------------------- |
852/// | 1[63] 11[62-52] 52[51-00] 1023 |
853/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000854double APInt::roundToDouble(bool isSigned) const {
855 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000856 APInt Tmp(isNeg ? -(*this) : (*this));
857 if (Tmp.isSingleWord())
858 return isSigned ? double(int64_t(Tmp.VAL)) : double(Tmp.VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000859 uint32_t n = Tmp.getActiveBits();
Reid Spencer443b5702007-02-18 00:44:22 +0000860 if (n <= APINT_BITS_PER_WORD)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000861 return isSigned ? double(int64_t(Tmp.pVal[0])) : double(Tmp.pVal[0]);
862 // Exponent when normalized to have decimal point directly after
863 // leading one. This is stored excess 1023 in the exponent bit field.
864 uint64_t exp = n - 1;
865
866 // Gross overflow.
867 assert(exp <= 1023 && "Infinity value!");
868
869 // Number of bits in mantissa including the leading one
870 // equals to 53.
871 uint64_t mantissa;
Reid Spencer443b5702007-02-18 00:44:22 +0000872 if (n % APINT_BITS_PER_WORD >= 53)
873 mantissa = Tmp.pVal[whichWord(n - 1)] >> (n % APINT_BITS_PER_WORD - 53);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000874 else
Reid Spencer443b5702007-02-18 00:44:22 +0000875 mantissa = (Tmp.pVal[whichWord(n - 1)] << (53 - n % APINT_BITS_PER_WORD)) |
876 (Tmp.pVal[whichWord(n - 1) - 1] >>
877 (11 + n % APINT_BITS_PER_WORD));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000878 // The leading bit of mantissa is implicit, so get rid of it.
879 mantissa &= ~(1ULL << 52);
Reid Spencer443b5702007-02-18 00:44:22 +0000880 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000881 exp += 1023;
882 union {
883 double D;
884 uint64_t I;
885 } T;
886 T.I = sign | (exp << 52) | mantissa;
887 return T.D;
888}
889
Reid Spencere81d2da2007-02-16 22:36:51 +0000890// Truncate to new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000891void APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000892 assert(width < BitWidth && "Invalid APInt Truncate request");
893}
894
895// Sign extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000896void APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000897 assert(width > BitWidth && "Invalid APInt SignExtend request");
898}
899
900// Zero extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000901void APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000902 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
903}
904
Zhou Shengff4304f2007-02-09 07:48:24 +0000905/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000906/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000907APInt APInt::ashr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000908 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000909 if (API.isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000910 API.VAL =
911 (((int64_t(API.VAL) << (APINT_BITS_PER_WORD - API.BitWidth)) >>
912 (APINT_BITS_PER_WORD - API.BitWidth)) >> shiftAmt) &
913 (~uint64_t(0UL) >> (APINT_BITS_PER_WORD - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000914 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000915 if (shiftAmt >= API.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000916 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0,
917 (API.getNumWords()-1) * sizeof(uint64_t));
Reid Spencer443b5702007-02-18 00:44:22 +0000918 API.pVal[API.getNumWords() - 1] =
919 ~uint64_t(0UL) >>
920 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000921 } else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000922 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000923 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000924 if (API[i+shiftAmt])
925 API.set(i);
926 else
927 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000928 for (; i < API.BitWidth; ++i)
929 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000930 API.set(i);
931 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000932 }
933 }
934 return API;
935}
936
Zhou Shengff4304f2007-02-09 07:48:24 +0000937/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000938/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000939APInt APInt::lshr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000940 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000941 if (API.isSingleWord())
942 API.VAL >>= shiftAmt;
943 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000944 if (shiftAmt >= API.BitWidth)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000945 memset(API.pVal, 0, API.getNumWords() * sizeof(uint64_t));
946 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000947 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000948 if (API[i+shiftAmt]) API.set(i);
949 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000950 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000951 API.clear(i);
952 }
953 return API;
954}
955
Zhou Shengff4304f2007-02-09 07:48:24 +0000956/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000957/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000958APInt APInt::shl(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000959 APInt API(*this);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000960 if (API.isSingleWord())
961 API.VAL <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000962 else if (shiftAmt >= API.BitWidth)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000963 memset(API.pVal, 0, API.getNumWords() * sizeof(uint64_t));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000964 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000965 if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
966 for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
Zhou Shengd93f00c2007-02-12 20:02:55 +0000967 API.pVal[i] = API.pVal[i-offset];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000968 memset(API.pVal, 0, offset * sizeof(uint64_t));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000969 }
Reid Spencer443b5702007-02-18 00:44:22 +0000970 shiftAmt %= APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000971 uint32_t i;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000972 for (i = API.getNumWords() - 1; i > 0; --i)
973 API.pVal[i] = (API.pVal[i] << shiftAmt) |
Reid Spencer443b5702007-02-18 00:44:22 +0000974 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
Zhou Shengd93f00c2007-02-12 20:02:55 +0000975 API.pVal[i] <<= shiftAmt;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000976 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000977 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000978 return API;
979}
980
Reid Spencer5e0a8512007-02-17 03:16:00 +0000981/// subMul - This function substracts x[len-1:0] * y from
982/// dest[offset+len-1:offset], and returns the most significant
983/// word of the product, minus the borrow-out from the subtraction.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000984static uint32_t subMul(uint32_t dest[], uint32_t offset,
985 uint32_t x[], uint32_t len, uint32_t y) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000986 uint64_t yl = (uint64_t) y & 0xffffffffL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000987 uint32_t carry = 0;
988 uint32_t j = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000989 do {
Reid Spencerc72f2802007-02-17 22:38:07 +0000990 uint64_t prod = ((uint64_t) x[j] & 0xffffffffUL) * yl;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000991 uint32_t prod_low = (uint32_t) prod;
992 uint32_t prod_high = (uint32_t) (prod >> 32);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000993 prod_low += carry;
994 carry = (prod_low < carry ? 1 : 0) + prod_high;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000995 uint32_t x_j = dest[offset+j];
Reid Spencer5e0a8512007-02-17 03:16:00 +0000996 prod_low = x_j - prod_low;
997 if (prod_low > x_j) ++carry;
998 dest[offset+j] = prod_low;
999 } while (++j < len);
1000 return carry;
1001}
1002
1003/// unitDiv - This function divides N by D,
1004/// and returns (remainder << 32) | quotient.
1005/// Assumes (N >> 32) < D.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001006static uint64_t unitDiv(uint64_t N, uint32_t D) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001007 uint64_t q, r; // q: quotient, r: remainder.
1008 uint64_t a1 = N >> 32; // a1: high 32-bit part of N.
1009 uint64_t a0 = N & 0xffffffffL; // a0: low 32-bit part of N
1010 if (a1 < ((D - a1 - (a0 >> 31)) & 0xffffffffL)) {
1011 q = N / D;
1012 r = N % D;
1013 }
1014 else {
1015 // Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d
1016 uint64_t c = N - ((uint64_t) D << 31);
1017 // Divide (c1*2^32 + c0) by d
1018 q = c / D;
1019 r = c % D;
1020 // Add 2^31 to quotient
1021 q += 1 << 31;
1022 }
1023
1024 return (r << 32) | (q & 0xFFFFFFFFl);
1025}
1026
1027/// div - This is basically Knuth's formulation of the classical algorithm.
1028/// Correspondance with Knuth's notation:
1029/// Knuth's u[0:m+n] == zds[nx:0].
1030/// Knuth's v[1:n] == y[ny-1:0]
1031/// Knuth's n == ny.
1032/// Knuth's m == nx-ny.
1033/// Our nx == Knuth's m+n.
1034/// Could be re-implemented using gmp's mpn_divrem:
1035/// zds[nx] = mpn_divrem (&zds[ny], 0, zds, nx, y, ny).
Reid Spenceraf0e9562007-02-18 18:38:44 +00001036static void div(uint32_t zds[], uint32_t nx, uint32_t y[], uint32_t ny) {
1037 uint32_t j = nx;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001038 do { // loop over digits of quotient
1039 // Knuth's j == our nx-j.
1040 // Knuth's u[j:j+n] == our zds[j:j-ny].
Reid Spenceraf0e9562007-02-18 18:38:44 +00001041 uint32_t qhat; // treated as unsigned
Reid Spencer5e0a8512007-02-17 03:16:00 +00001042 if (zds[j] == y[ny-1])
1043 qhat = -1U; // 0xffffffff
1044 else {
1045 uint64_t w = (((uint64_t)(zds[j])) << 32) +
1046 ((uint64_t)zds[j-1] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001047 qhat = (uint32_t) unitDiv(w, y[ny-1]);
Reid Spencer5e0a8512007-02-17 03:16:00 +00001048 }
1049 if (qhat) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001050 uint32_t borrow = subMul(zds, j - ny, y, ny, qhat);
1051 uint32_t save = zds[j];
Reid Spencer5e0a8512007-02-17 03:16:00 +00001052 uint64_t num = ((uint64_t)save&0xffffffffL) -
1053 ((uint64_t)borrow&0xffffffffL);
1054 while (num) {
1055 qhat--;
1056 uint64_t carry = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001057 for (uint32_t i = 0; i < ny; i++) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001058 carry += ((uint64_t) zds[j-ny+i] & 0xffffffffL)
1059 + ((uint64_t) y[i] & 0xffffffffL);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001060 zds[j-ny+i] = (uint32_t) carry;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001061 carry >>= 32;
1062 }
1063 zds[j] += carry;
1064 num = carry - 1;
1065 }
1066 }
1067 zds[j] = qhat;
1068 } while (--j >= ny);
1069}
1070
Zhou Shengff4304f2007-02-09 07:48:24 +00001071/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001072/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001073APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001074 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001075
1076 // First, deal with the easy case
1077 if (isSingleWord()) {
1078 assert(RHS.VAL != 0 && "Divide by zero?");
1079 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001080 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001081
1082 // Make a temporary to hold the result
1083 APInt Result(*this);
1084
1085 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001086 uint32_t rhsBits = RHS.getActiveBits();
1087 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001088 assert(rhsWords && "Divided by zero???");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001089 uint32_t lhsBits = Result.getActiveBits();
1090 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001091
1092 // Deal with some degenerate cases
1093 if (!lhsWords)
1094 return Result; // 0 / X == 0
1095 else if (lhsWords < rhsWords || Result.ult(RHS))
1096 // X / Y with X < Y == 0
Reid Spenceraf0e9562007-02-18 18:38:44 +00001097 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001098 else if (Result == RHS) {
1099 // X / X == 1
Reid Spenceraf0e9562007-02-18 18:38:44 +00001100 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001101 Result.pVal[0] = 1;
1102 } else if (lhsWords == 1)
1103 // All high words are zero, just use native divide
1104 Result.pVal[0] /= RHS.pVal[0];
1105 else {
1106 // Compute it the hard way ..
1107 APInt X(BitWidth, 0);
1108 APInt Y(BitWidth, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001109 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001110 (APINT_BITS_PER_WORD - 1) - ((rhsBits - 1) % APINT_BITS_PER_WORD );
1111 if (nshift) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001112 Y = APIntOps::shl(RHS, nshift);
1113 X = APIntOps::shl(Result, nshift);
1114 ++lhsWords;
1115 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001116 div((uint32_t*)X.pVal, lhsWords * 2 - 1,
1117 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
1118 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
1119 memcpy(Result.pVal, X.pVal + rhsWords,
1120 (lhsWords - rhsWords) * sizeof(uint64_t));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001121 }
1122 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001123}
1124
1125/// Unsigned remainder operation on APInt.
1126/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001127APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001128 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001129 if (isSingleWord()) {
1130 assert(RHS.VAL != 0 && "Remainder by zero?");
1131 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001132 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001133
1134 // Make a temporary to hold the result
1135 APInt Result(*this);
1136
1137 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001138 uint32_t rhsBits = RHS.getActiveBits();
1139 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001140 assert(rhsWords && "Performing remainder operation by zero ???");
1141
1142 // Get some facts about the LHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001143 uint32_t lhsBits = Result.getActiveBits();
1144 uint32_t lhsWords = !lhsBits ? 0 : (Result.whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001145
1146 // Check the degenerate cases
1147 if (lhsWords == 0)
1148 // 0 % Y == 0
Reid Spenceraf0e9562007-02-18 18:38:44 +00001149 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001150 else if (lhsWords < rhsWords || Result.ult(RHS))
1151 // X % Y == X iff X < Y
1152 return Result;
1153 else if (Result == RHS)
1154 // X % X == 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001155 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001156 else if (lhsWords == 1)
1157 // All high words are zero, just use native remainder
1158 Result.pVal[0] %= RHS.pVal[0];
1159 else {
1160 // Do it the hard way
Reid Spencer443b5702007-02-18 00:44:22 +00001161 APInt X((lhsWords+1)*APINT_BITS_PER_WORD, 0);
1162 APInt Y(rhsWords*APINT_BITS_PER_WORD, 0);
Reid Spenceraf0e9562007-02-18 18:38:44 +00001163 uint32_t nshift =
Reid Spencer443b5702007-02-18 00:44:22 +00001164 (APINT_BITS_PER_WORD - 1) - (rhsBits - 1) % APINT_BITS_PER_WORD;
Reid Spencer71bd08f2007-02-17 02:07:07 +00001165 if (nshift) {
1166 APIntOps::shl(Y, nshift);
1167 APIntOps::shl(X, nshift);
1168 }
Reid Spenceraf0e9562007-02-18 18:38:44 +00001169 div((uint32_t*)X.pVal, rhsWords*2-1,
1170 (uint32_t*)(Y.isSingleWord()? &Y.VAL : Y.pVal), rhsWords*2);
1171 memset(Result.pVal, 0, Result.getNumWords() * sizeof(uint64_t));
1172 for (uint32_t i = 0; i < rhsWords-1; ++i)
Reid Spencer443b5702007-02-18 00:44:22 +00001173 Result.pVal[i] = (X.pVal[i] >> nshift) |
1174 (X.pVal[i+1] << (APINT_BITS_PER_WORD - nshift));
Reid Spencer71bd08f2007-02-17 02:07:07 +00001175 Result.pVal[rhsWords-1] = X.pVal[rhsWords-1] >> nshift;
1176 }
1177 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001178}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001179
1180/// @brief Converts a char array into an integer.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001181void APInt::fromString(uint32_t numbits, const char *StrStart, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001182 uint8_t radix) {
1183 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1184 "Radix should be 2, 8, 10, or 16!");
1185 assert(StrStart && "String is null?");
Reid Spenceraf0e9562007-02-18 18:38:44 +00001186 uint32_t size = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001187 // If the radix is a power of 2, read the input
1188 // from most significant to least significant.
1189 if ((radix & (radix - 1)) == 0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001190 uint32_t nextBitPos = 0;
1191 uint32_t bits_per_digit = radix / 8 + 2;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001192 uint64_t resDigit = 0;
1193 BitWidth = slen * bits_per_digit;
1194 if (getNumWords() > 1)
Reid Spenceraf0e9562007-02-18 18:38:44 +00001195 pVal = getMemory(getNumWords());
Reid Spencer5e0a8512007-02-17 03:16:00 +00001196 for (int i = slen - 1; i >= 0; --i) {
Reid Spencer443b5702007-02-18 00:44:22 +00001197 uint64_t digit = StrStart[i] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001198 resDigit |= digit << nextBitPos;
1199 nextBitPos += bits_per_digit;
Reid Spencer443b5702007-02-18 00:44:22 +00001200 if (nextBitPos >= APINT_BITS_PER_WORD) {
Reid Spencer5e0a8512007-02-17 03:16:00 +00001201 if (isSingleWord()) {
1202 VAL = resDigit;
1203 break;
1204 }
1205 pVal[size++] = resDigit;
Reid Spencer443b5702007-02-18 00:44:22 +00001206 nextBitPos -= APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001207 resDigit = digit >> (bits_per_digit - nextBitPos);
1208 }
1209 }
1210 if (!isSingleWord() && size <= getNumWords())
1211 pVal[size] = resDigit;
1212 } else { // General case. The radix is not a power of 2.
1213 // For 10-radix, the max value of 64-bit integer is 18446744073709551615,
1214 // and its digits number is 20.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001215 const uint32_t chars_per_word = 20;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001216 if (slen < chars_per_word ||
1217 (slen == chars_per_word && // In case the value <= 2^64 - 1
1218 strcmp(StrStart, "18446744073709551615") <= 0)) {
Reid Spencer443b5702007-02-18 00:44:22 +00001219 BitWidth = APINT_BITS_PER_WORD;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001220 VAL = strtoull(StrStart, 0, 10);
1221 } else { // In case the value > 2^64 - 1
Reid Spencer443b5702007-02-18 00:44:22 +00001222 BitWidth = (slen / chars_per_word + 1) * APINT_BITS_PER_WORD;
Reid Spenceraf0e9562007-02-18 18:38:44 +00001223 pVal = getClearedMemory(getNumWords());
1224 uint32_t str_pos = 0;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001225 while (str_pos < slen) {
Reid Spenceraf0e9562007-02-18 18:38:44 +00001226 uint32_t chunk = slen - str_pos;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001227 if (chunk > chars_per_word - 1)
1228 chunk = chars_per_word - 1;
Reid Spencer443b5702007-02-18 00:44:22 +00001229 uint64_t resDigit = StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001230 uint64_t big_base = radix;
1231 while (--chunk > 0) {
Reid Spencer443b5702007-02-18 00:44:22 +00001232 resDigit = resDigit * radix + StrStart[str_pos++] - '0';
Reid Spencer5e0a8512007-02-17 03:16:00 +00001233 big_base *= radix;
1234 }
1235
1236 uint64_t carry;
1237 if (!size)
1238 carry = resDigit;
1239 else {
1240 carry = mul_1(pVal, pVal, size, big_base);
1241 carry += add_1(pVal, pVal, size, resDigit);
1242 }
1243
1244 if (carry) pVal[size++] = carry;
1245 }
1246 }
1247 }
1248}