blob: 6d98b554c8b906cb478b35eee5dcaa704ace560f [file] [log] [blame]
Zhou Shengfd43dcf2007-02-06 03:00:16 +00001//===-- APInt.cpp - Implement APInt class ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer610fad82007-02-24 10:01:42 +00005// This file was developed by Sheng Zhou and Reid Spencer and is distributed
6// under the // University of Illinois Open Source License. See LICENSE.TXT
7// for details.
Zhou Shengfd43dcf2007-02-06 03:00:16 +00008//
9//===----------------------------------------------------------------------===//
10//
Reid Spencer5d0d05c2007-02-25 19:32:03 +000011// This file implements a class to represent arbitrary precision integer
12// constant values and provide a variety of arithmetic operations on them.
Zhou Shengfd43dcf2007-02-06 03:00:16 +000013//
14//===----------------------------------------------------------------------===//
15
Reid Spencer9d6c9192007-02-24 03:58:46 +000016#define DEBUG_TYPE "apint"
Zhou Shengfd43dcf2007-02-06 03:00:16 +000017#include "llvm/ADT/APInt.h"
18#include "llvm/DerivedTypes.h"
Reid Spencer9d6c9192007-02-24 03:58:46 +000019#include "llvm/Support/Debug.h"
Zhou Shengfd43dcf2007-02-06 03:00:16 +000020#include "llvm/Support/MathExtras.h"
Zhou Shenga3832fd2007-02-07 06:14:53 +000021#include <cstring>
Zhou Shengfd43dcf2007-02-06 03:00:16 +000022#include <cstdlib>
Reid Spencer385f7542007-02-21 03:55:44 +000023#ifndef NDEBUG
Reid Spencer385f7542007-02-21 03:55:44 +000024#include <iomanip>
25#endif
26
Zhou Shengfd43dcf2007-02-06 03:00:16 +000027using namespace llvm;
28
Reid Spencer5d0d05c2007-02-25 19:32:03 +000029/// A utility function for allocating memory, checking for allocation failures,
30/// and ensuring the contents are zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000031inline static uint64_t* getClearedMemory(uint32_t numWords) {
32 uint64_t * result = new uint64_t[numWords];
33 assert(result && "APInt memory allocation fails!");
34 memset(result, 0, numWords * sizeof(uint64_t));
35 return result;
Zhou Sheng353815d2007-02-06 06:04:53 +000036}
37
Reid Spencer5d0d05c2007-02-25 19:32:03 +000038/// A utility function for allocating memory and checking for allocation
39/// failure. The content is not zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000040inline static uint64_t* getMemory(uint32_t numWords) {
41 uint64_t * result = new uint64_t[numWords];
42 assert(result && "APInt memory allocation fails!");
43 return result;
44}
45
46APInt::APInt(uint32_t numBits, uint64_t val)
Reid Spencer385f7542007-02-21 03:55:44 +000047 : BitWidth(numBits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000048 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
49 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Reid Spencer5d0d05c2007-02-25 19:32:03 +000050 if (isSingleWord())
51 VAL = val;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000052 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000053 pVal = getClearedMemory(getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000054 pVal[0] = val;
55 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +000056 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000057}
58
Reid Spenceraf0e9562007-02-18 18:38:44 +000059APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[])
Reid Spencer385f7542007-02-21 03:55:44 +000060 : BitWidth(numBits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000061 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
62 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000063 assert(bigVal && "Null pointer detected!");
64 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +000065 VAL = bigVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +000066 else {
Reid Spencer610fad82007-02-24 10:01:42 +000067 // Get memory, cleared to 0
68 pVal = getClearedMemory(getNumWords());
69 // Calculate the number of words to copy
70 uint32_t words = std::min<uint32_t>(numWords, getNumWords());
71 // Copy the words from bigVal to pVal
72 memcpy(pVal, bigVal, words * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000073 }
Reid Spencer610fad82007-02-24 10:01:42 +000074 // Make sure unused high bits are cleared
75 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000076}
77
Reid Spenceraf0e9562007-02-18 18:38:44 +000078APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
Reid Spencer9c0696f2007-02-20 08:51:03 +000079 uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000080 : BitWidth(numbits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000081 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000082}
83
Reid Spencer9c0696f2007-02-20 08:51:03 +000084APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000085 : BitWidth(numbits), VAL(0) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000086 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000087 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000088}
89
Reid Spencer54362ca2007-02-20 23:40:25 +000090APInt::APInt(const APInt& that)
Reid Spencer385f7542007-02-21 03:55:44 +000091 : BitWidth(that.BitWidth), VAL(0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +000092 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +000093 VAL = that.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000094 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000095 pVal = getMemory(getNumWords());
Reid Spencer54362ca2007-02-20 23:40:25 +000096 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000097 }
98}
99
100APInt::~APInt() {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000101 if (!isSingleWord() && pVal)
102 delete[] pVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000103}
104
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000105APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000106 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
107 if (isSingleWord())
Reid Spenceraf0e9562007-02-18 18:38:44 +0000108 VAL = RHS.VAL;
109 else
Reid Spencera58f0582007-02-18 20:09:41 +0000110 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000111 return *this;
112}
113
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000114APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000115 if (isSingleWord())
116 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000117 else {
118 pVal[0] = RHS;
Reid Spencera58f0582007-02-18 20:09:41 +0000119 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000120 }
121 return *this;
122}
123
Reid Spenceraf0e9562007-02-18 18:38:44 +0000124/// add_1 - This function adds a single "digit" integer, y, to the multiple
125/// "digit" integer array, x[]. x[] is modified to reflect the addition and
126/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000127/// @returns the carry of the addition.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000128static bool add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000129 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)
Reid Spencer610fad82007-02-24 10:01:42 +0000132 y = 1; // Carry one to next digit.
Reid Spencerf2c521c2007-02-18 06:39:42 +0000133 else {
Reid Spencer610fad82007-02-24 10:01:42 +0000134 y = 0; // No need to carry so exit early
Reid Spencerf2c521c2007-02-18 06:39:42 +0000135 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 Spencer5d0d05c2007-02-25 19:32:03 +0000147 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000148}
149
Reid Spenceraf0e9562007-02-18 18:38:44 +0000150/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
151/// the multi-digit integer array, x[], propagating the borrowed 1 value until
152/// no further borrowing is neeeded or it runs out of "digits" in x. The result
153/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
154/// In other words, if y > x then this function returns 1, otherwise 0.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000155/// @returns the borrow out of the subtraction
156static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000157 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000158 uint64_t X = x[i];
Reid Spencerf2c521c2007-02-18 06:39:42 +0000159 x[i] -= y;
160 if (y > X)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000161 y = 1; // We have to "borrow 1" from next "digit"
Reid Spencer5e0a8512007-02-17 03:16:00 +0000162 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000163 y = 0; // No need to borrow
164 break; // Remaining digits are unchanged so exit early
Reid Spencer5e0a8512007-02-17 03:16:00 +0000165 }
166 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000167 return bool(y);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000168}
169
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000170/// @brief Prefix decrement operator. Decrements the APInt by one.
171APInt& APInt::operator--() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000172 if (isSingleWord())
173 --VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000174 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000175 sub_1(pVal, getNumWords(), 1);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000176 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000177}
178
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000179/// add - This function adds the integer array x to the integer array Y and
180/// places the result in dest.
181/// @returns the carry out from the addition
182/// @brief General addition of 64-bit integer arrays
Reid Spencer9d6c9192007-02-24 03:58:46 +0000183static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
184 uint32_t len) {
185 bool carry = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000186 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer92904632007-02-23 01:57:13 +0000187 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
Reid Spencer54362ca2007-02-20 23:40:25 +0000188 dest[i] = x[i] + y[i] + carry;
Reid Spencer60c0a6a2007-02-21 05:44:56 +0000189 carry = dest[i] < limit || (carry && dest[i] == limit);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000190 }
191 return carry;
192}
193
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000194/// Adds the RHS APint to this APInt.
195/// @returns this, after addition of RHS.
196/// @brief Addition assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000197APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000198 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000199 if (isSingleWord())
200 VAL += RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201 else {
Reid Spencer54362ca2007-02-20 23:40:25 +0000202 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000203 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000204 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000205}
206
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000207/// Subtracts the integer array y from the integer array x
208/// @returns returns the borrow out.
209/// @brief Generalized subtraction of 64-bit integer arrays.
Reid Spencer9d6c9192007-02-24 03:58:46 +0000210static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
211 uint32_t len) {
Reid Spencer385f7542007-02-21 03:55:44 +0000212 bool borrow = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000213 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer385f7542007-02-21 03:55:44 +0000214 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
215 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
216 dest[i] = x_tmp - y[i];
Reid Spencer5e0a8512007-02-17 03:16:00 +0000217 }
Reid Spencer54362ca2007-02-20 23:40:25 +0000218 return borrow;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000219}
220
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000221/// Subtracts the RHS APInt from this APInt
222/// @returns this, after subtraction
223/// @brief Subtraction assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000224APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000225 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000226 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +0000227 VAL -= RHS.VAL;
228 else
229 sub(pVal, pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000230 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000231}
232
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000233/// Multiplies an integer array, x by a a uint64_t integer and places the result
234/// into dest.
235/// @returns the carry out of the multiplication.
236/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
Reid Spencer610fad82007-02-24 10:01:42 +0000237static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
238 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
Reid Spencer5e0a8512007-02-17 03:16:00 +0000239 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000240 uint64_t carry = 0;
241
242 // For each digit of x.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000243 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000244 // Split x into high and low words
245 uint64_t lx = x[i] & 0xffffffffULL;
246 uint64_t hx = x[i] >> 32;
247 // hasCarry - A flag to indicate if there is a carry to the next digit.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000248 // hasCarry == 0, no carry
249 // hasCarry == 1, has carry
250 // hasCarry == 2, no carry and the calculation result == 0.
251 uint8_t hasCarry = 0;
252 dest[i] = carry + lx * ly;
253 // Determine if the add above introduces carry.
254 hasCarry = (dest[i] < carry) ? 1 : 0;
255 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
256 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
257 // (2^32 - 1) + 2^32 = 2^64.
258 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
259
260 carry += (lx * hy) & 0xffffffffULL;
261 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
262 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
263 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
264 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000265 return carry;
266}
267
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000268/// Multiplies integer array x by integer array y and stores the result into
269/// the integer array dest. Note that dest's size must be >= xlen + ylen.
270/// @brief Generalized multiplicate of integer arrays.
Reid Spencer610fad82007-02-24 10:01:42 +0000271static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[],
272 uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000273 dest[xlen] = mul_1(dest, x, xlen, y[0]);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000274 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000275 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
Reid Spencere0cdd332007-02-21 08:21:52 +0000276 uint64_t carry = 0, lx = 0, hx = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000277 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000278 lx = x[j] & 0xffffffffULL;
279 hx = x[j] >> 32;
280 // hasCarry - A flag to indicate if has carry.
281 // hasCarry == 0, no carry
282 // hasCarry == 1, has carry
283 // hasCarry == 2, no carry and the calculation result == 0.
284 uint8_t hasCarry = 0;
285 uint64_t resul = carry + lx * ly;
286 hasCarry = (resul < carry) ? 1 : 0;
287 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
288 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
289
290 carry += (lx * hy) & 0xffffffffULL;
291 resul = (carry << 32) | (resul & 0xffffffffULL);
292 dest[i+j] += resul;
293 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
294 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
295 ((lx * hy) >> 32) + hx * hy;
296 }
297 dest[i+xlen] = carry;
298 }
299}
300
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000301APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000302 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencere0cdd332007-02-21 08:21:52 +0000303 if (isSingleWord()) {
Reid Spencer61eb1802007-02-20 20:42:10 +0000304 VAL *= RHS.VAL;
Reid Spencere0cdd332007-02-21 08:21:52 +0000305 clearUnusedBits();
306 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000307 }
Reid Spencere0cdd332007-02-21 08:21:52 +0000308
309 // Get some bit facts about LHS and check for zero
310 uint32_t lhsBits = getActiveBits();
311 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
312 if (!lhsWords)
313 // 0 * X ===> 0
314 return *this;
315
316 // Get some bit facts about RHS and check for zero
317 uint32_t rhsBits = RHS.getActiveBits();
318 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
319 if (!rhsWords) {
320 // X * 0 ===> 0
321 clear();
322 return *this;
323 }
324
325 // Allocate space for the result
326 uint32_t destWords = rhsWords + lhsWords;
327 uint64_t *dest = getMemory(destWords);
328
329 // Perform the long multiply
330 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
331
332 // Copy result back into *this
333 clear();
334 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
335 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
336
337 // delete dest array and return
338 delete[] dest;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000339 return *this;
340}
341
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000342APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000343 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000344 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000345 VAL &= RHS.VAL;
346 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000347 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000348 uint32_t numWords = getNumWords();
349 for (uint32_t i = 0; i < numWords; ++i)
350 pVal[i] &= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000351 return *this;
352}
353
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000354APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000355 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000356 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000357 VAL |= RHS.VAL;
358 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000359 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000360 uint32_t numWords = getNumWords();
361 for (uint32_t i = 0; i < numWords; ++i)
362 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000363 return *this;
364}
365
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000366APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000367 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000368 if (isSingleWord()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000369 VAL ^= RHS.VAL;
Reid Spencer54362ca2007-02-20 23:40:25 +0000370 this->clearUnusedBits();
Reid Spencerf2c521c2007-02-18 06:39:42 +0000371 return *this;
372 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000373 uint32_t numWords = getNumWords();
374 for (uint32_t i = 0; i < numWords; ++i)
375 pVal[i] ^= RHS.pVal[i];
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000376 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000377}
378
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000379APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000380 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000381 if (isSingleWord())
382 return APInt(getBitWidth(), VAL & RHS.VAL);
383
Reid Spenceraf0e9562007-02-18 18:38:44 +0000384 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000385 uint64_t* val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000386 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000387 val[i] = pVal[i] & RHS.pVal[i];
388 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000389}
390
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000391APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000392 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000393 if (isSingleWord())
394 return APInt(getBitWidth(), VAL | RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000395
Reid Spenceraf0e9562007-02-18 18:38:44 +0000396 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000397 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000398 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000399 val[i] = pVal[i] | RHS.pVal[i];
400 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000401}
402
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000403APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000404 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000405 if (isSingleWord())
406 return APInt(BitWidth, VAL ^ RHS.VAL).clearUnusedBits();
407
Reid Spenceraf0e9562007-02-18 18:38:44 +0000408 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000409 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000410 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000411 val[i] = pVal[i] ^ RHS.pVal[i];
412
413 // 0^0==1 so clear the high bits in case they got set.
414 return APInt(val, getBitWidth()).clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000415}
416
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000417bool APInt::operator !() const {
418 if (isSingleWord())
419 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000420
421 for (uint32_t i = 0; i < getNumWords(); ++i)
422 if (pVal[i])
423 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000424 return true;
425}
426
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000427APInt 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 Spencer5d0d05c2007-02-25 19:32:03 +0000429 if (isSingleWord())
430 return APInt(BitWidth, VAL * RHS.VAL).clearUnusedBits();
Reid Spencer61eb1802007-02-20 20:42:10 +0000431 APInt Result(*this);
432 Result *= RHS;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000433 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000434}
435
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000436APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000437 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000438 if (isSingleWord())
439 return APInt(BitWidth, VAL + RHS.VAL).clearUnusedBits();
Reid Spencer54362ca2007-02-20 23:40:25 +0000440 APInt Result(BitWidth, 0);
441 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000442 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000443}
444
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000445APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000446 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000447 if (isSingleWord())
448 return APInt(BitWidth, VAL - RHS.VAL).clearUnusedBits();
Reid Spencer54362ca2007-02-20 23:40:25 +0000449 APInt Result(BitWidth, 0);
450 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000451 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000452}
453
Reid Spenceraf0e9562007-02-18 18:38:44 +0000454bool APInt::operator[](uint32_t bitPosition) const {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000455 return (maskBit(bitPosition) &
456 (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000457}
458
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000459bool APInt::operator==(const APInt& RHS) const {
Reid Spencer54362ca2007-02-20 23:40:25 +0000460 if (isSingleWord())
461 return VAL == RHS.VAL;
462
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000463 // Get some facts about the number of bits used in the two operands.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000464 uint32_t n1 = getActiveBits();
465 uint32_t n2 = RHS.getActiveBits();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000466
467 // If the number of bits isn't the same, they aren't equal
Reid Spencer54362ca2007-02-20 23:40:25 +0000468 if (n1 != n2)
469 return false;
470
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000471 // If the number of bits fits in a word, we only need to compare the low word.
Reid Spencer54362ca2007-02-20 23:40:25 +0000472 if (n1 <= APINT_BITS_PER_WORD)
473 return pVal[0] == RHS.pVal[0];
474
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000475 // Otherwise, compare everything
Reid Spencer54362ca2007-02-20 23:40:25 +0000476 for (int i = whichWord(n1 - 1); i >= 0; --i)
477 if (pVal[i] != RHS.pVal[i])
478 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000479 return true;
480}
481
Zhou Shenga3832fd2007-02-07 06:14:53 +0000482bool APInt::operator==(uint64_t Val) const {
483 if (isSingleWord())
484 return VAL == Val;
Reid Spencer54362ca2007-02-20 23:40:25 +0000485
486 uint32_t n = getActiveBits();
487 if (n <= APINT_BITS_PER_WORD)
488 return pVal[0] == Val;
489 else
490 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000491}
492
Reid Spencere81d2da2007-02-16 22:36:51 +0000493bool APInt::ult(const APInt& RHS) const {
494 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
495 if (isSingleWord())
496 return VAL < RHS.VAL;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000497
498 // Get active bit length of both operands
499 uint32_t n1 = getActiveBits();
500 uint32_t n2 = RHS.getActiveBits();
501
502 // If magnitude of LHS is less than RHS, return true.
503 if (n1 < n2)
504 return true;
505
506 // If magnitude of RHS is greather than LHS, return false.
507 if (n2 < n1)
508 return false;
509
510 // If they bot fit in a word, just compare the low order word
511 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
512 return pVal[0] < RHS.pVal[0];
513
514 // Otherwise, compare all words
515 for (int i = whichWord(n1 - 1); i >= 0; --i) {
516 if (pVal[i] > RHS.pVal[i])
Reid Spencere81d2da2007-02-16 22:36:51 +0000517 return false;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000518 if (pVal[i] < RHS.pVal[i])
519 return true;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000520 }
521 return false;
522}
523
Reid Spencere81d2da2007-02-16 22:36:51 +0000524bool APInt::slt(const APInt& RHS) const {
525 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
Reid Spencera58f0582007-02-18 20:09:41 +0000526 if (isSingleWord()) {
527 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
528 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
529 return lhsSext < rhsSext;
Reid Spencere81d2da2007-02-16 22:36:51 +0000530 }
Reid Spencera58f0582007-02-18 20:09:41 +0000531
532 APInt lhs(*this);
533 APInt rhs(*this);
534 bool lhsNegative = false;
535 bool rhsNegative = false;
536 if (lhs[BitWidth-1]) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000537 // Sign bit is set so make a note of it and perform two's complement
Reid Spencera58f0582007-02-18 20:09:41 +0000538 lhsNegative = true;
539 lhs.flip();
540 lhs++;
541 }
542 if (rhs[BitWidth-1]) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000543 // Sign bit is set so make a note of it and perform two's complement
Reid Spencera58f0582007-02-18 20:09:41 +0000544 rhsNegative = true;
545 rhs.flip();
546 rhs++;
547 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000548
549 // Now we have unsigned values to compare so do the comparison if necessary
550 // based on the negativeness of the values.
Reid Spencera58f0582007-02-18 20:09:41 +0000551 if (lhsNegative)
552 if (rhsNegative)
553 return !lhs.ult(rhs);
554 else
555 return true;
556 else if (rhsNegative)
557 return false;
558 else
559 return lhs.ult(rhs);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000560}
561
Reid Spenceraf0e9562007-02-18 18:38:44 +0000562APInt& APInt::set(uint32_t bitPosition) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000563 if (isSingleWord())
564 VAL |= maskBit(bitPosition);
565 else
566 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000567 return *this;
568}
569
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000570APInt& APInt::set() {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000571 if (isSingleWord()) {
572 VAL = -1ULL;
573 return clearUnusedBits();
Zhou Shengb04973e2007-02-15 06:36:31 +0000574 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000575
576 // Set all the bits in all the words.
577 for (uint32_t i = 0; i < getNumWords() - 1; ++i)
578 pVal[i] = -1ULL;
579 // Clear the unused ones
580 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000581}
582
583/// Set the given bit to 0 whose position is given as "bitPosition".
584/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000585APInt& APInt::clear(uint32_t bitPosition) {
586 if (isSingleWord())
587 VAL &= ~maskBit(bitPosition);
588 else
589 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000590 return *this;
591}
592
593/// @brief Set every bit to 0.
594APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000595 if (isSingleWord())
596 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000597 else
Reid Spencera58f0582007-02-18 20:09:41 +0000598 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000599 return *this;
600}
601
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000602/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
603/// this APInt.
604APInt APInt::operator~() const {
605 APInt API(*this);
606 API.flip();
607 return API;
608}
609
610/// @brief Toggle every bit to its opposite value.
611APInt& APInt::flip() {
Reid Spencer443b5702007-02-18 00:44:22 +0000612 if (isSingleWord()) VAL = (~(VAL <<
613 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000614 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000615 uint32_t i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000616 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000617 pVal[i] = ~pVal[i];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000618 uint32_t offset =
Reid Spencer443b5702007-02-18 00:44:22 +0000619 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000620 pVal[i] = (~(pVal[i] << offset)) >> offset;
621 }
622 return *this;
623}
624
625/// Toggle a given bit to its opposite value whose position is given
626/// as "bitPosition".
627/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000628APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000629 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000630 if ((*this)[bitPosition]) clear(bitPosition);
631 else set(bitPosition);
632 return *this;
633}
634
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000635/// getMaxValue - This function returns the largest value
636/// for an APInt of the specified bit-width and if isSign == true,
637/// it should be largest signed value, otherwise unsigned value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000638APInt APInt::getMaxValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000639 APInt Result(numBits, 0);
640 Result.set();
641 if (isSign)
642 Result.clear(numBits - 1);
643 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000644}
645
646/// getMinValue - This function returns the smallest value for
647/// an APInt of the given bit-width and if isSign == true,
648/// it should be smallest signed value, otherwise zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000649APInt APInt::getMinValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000650 APInt Result(numBits, 0);
651 if (isSign)
652 Result.set(numBits - 1);
653 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000654}
655
656/// getAllOnesValue - This function returns an all-ones value for
657/// an APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000658APInt APInt::getAllOnesValue(uint32_t numBits) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000659 return getMaxValue(numBits, false);
660}
661
662/// getNullValue - This function creates an '0' value for an
663/// APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000664APInt APInt::getNullValue(uint32_t numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000665 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000666}
667
668/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000669APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000670 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000671}
672
673/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000674APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000675 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
676 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000677}
678
Reid Spencere81d2da2007-02-16 22:36:51 +0000679bool APInt::isPowerOf2() const {
680 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
681}
682
Reid Spenceraf0e9562007-02-18 18:38:44 +0000683uint32_t APInt::countLeadingZeros() const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000684 uint32_t Count = 0;
Reid Spencere549c492007-02-21 00:29:48 +0000685 if (isSingleWord())
686 Count = CountLeadingZeros_64(VAL);
687 else {
688 for (uint32_t i = getNumWords(); i > 0u; --i) {
689 if (pVal[i-1] == 0)
690 Count += APINT_BITS_PER_WORD;
691 else {
692 Count += CountLeadingZeros_64(pVal[i-1]);
693 break;
694 }
695 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000696 }
Reid Spencerab2b2c82007-02-22 00:22:00 +0000697 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD;
698 if (remainder)
699 Count -= APINT_BITS_PER_WORD - remainder;
700 return Count;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000701}
702
Reid Spenceraf0e9562007-02-18 18:38:44 +0000703uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000704 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000705 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000706 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000707 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000708}
709
Reid Spenceraf0e9562007-02-18 18:38:44 +0000710uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000711 if (isSingleWord())
712 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000713 uint32_t Count = 0;
714 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000715 Count += CountPopulation_64(pVal[i]);
716 return Count;
717}
718
Reid Spencere81d2da2007-02-16 22:36:51 +0000719APInt APInt::byteSwap() const {
720 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
721 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000722 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000723 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000724 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000725 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000726 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
727 Tmp1 = ByteSwap_32(Tmp1);
728 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
729 Tmp2 = ByteSwap_16(Tmp2);
730 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000731 APInt(BitWidth,
732 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000733 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000734 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000735 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000736 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000737 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000738 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000739 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000740 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
741 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000742 }
743 return Result;
744 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000745}
746
Zhou Sheng0b706b12007-02-08 14:35:19 +0000747APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
748 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000749 APInt A = API1, B = API2;
750 while (!!B) {
751 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000752 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000753 A = T;
754 }
755 return A;
756}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000757
Reid Spencere81d2da2007-02-16 22:36:51 +0000758APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000759 union {
760 double D;
761 uint64_t I;
762 } T;
763 T.D = Double;
764 bool isNeg = T.I >> 63;
765 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
766 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000767 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000768 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
769 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000770 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
771 APInt(64u, mantissa >> (52 - exp));
772 APInt Tmp(exp + 1, mantissa);
773 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000774 return isNeg ? -Tmp : Tmp;
775}
776
Reid Spencerdb3faa62007-02-13 22:41:58 +0000777/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000778/// The layout for double is as following (IEEE Standard 754):
779/// --------------------------------------
780/// | Sign Exponent Fraction Bias |
781/// |-------------------------------------- |
782/// | 1[63] 11[62-52] 52[51-00] 1023 |
783/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000784double APInt::roundToDouble(bool isSigned) const {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000785
786 // Handle the simple case where the value is contained in one uint64_t.
Reid Spencera58f0582007-02-18 20:09:41 +0000787 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
788 if (isSigned) {
789 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
790 return double(sext);
791 } else
792 return double(VAL);
793 }
794
Reid Spencer9c0696f2007-02-20 08:51:03 +0000795 // Determine if the value is negative.
Reid Spencere81d2da2007-02-16 22:36:51 +0000796 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000797
798 // Construct the absolute value if we're negative.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000799 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spencer9c0696f2007-02-20 08:51:03 +0000800
801 // Figure out how many bits we're using.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000802 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000803
Reid Spencer9c0696f2007-02-20 08:51:03 +0000804 // The exponent (without bias normalization) is just the number of bits
805 // we are using. Note that the sign bit is gone since we constructed the
806 // absolute value.
807 uint64_t exp = n;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000808
Reid Spencer9c0696f2007-02-20 08:51:03 +0000809 // Return infinity for exponent overflow
810 if (exp > 1023) {
811 if (!isSigned || !isNeg)
Reid Spencer61eb1802007-02-20 20:42:10 +0000812 return double(1.0E300 * 1.0E300); // positive infinity
Reid Spencer9c0696f2007-02-20 08:51:03 +0000813 else
Reid Spencer61eb1802007-02-20 20:42:10 +0000814 return double(-1.0E300 * 1.0E300); // negative infinity
Reid Spencer9c0696f2007-02-20 08:51:03 +0000815 }
816 exp += 1023; // Increment for 1023 bias
817
818 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
819 // extract the high 52 bits from the correct words in pVal.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000820 uint64_t mantissa;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000821 unsigned hiWord = whichWord(n-1);
822 if (hiWord == 0) {
823 mantissa = Tmp.pVal[0];
824 if (n > 52)
825 mantissa >>= n - 52; // shift down, we want the top 52 bits.
826 } else {
827 assert(hiWord > 0 && "huh?");
828 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
829 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
830 mantissa = hibits | lobits;
831 }
832
Zhou Shengd93f00c2007-02-12 20:02:55 +0000833 // The leading bit of mantissa is implicit, so get rid of it.
Reid Spencer443b5702007-02-18 00:44:22 +0000834 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000835 union {
836 double D;
837 uint64_t I;
838 } T;
839 T.I = sign | (exp << 52) | mantissa;
840 return T.D;
841}
842
Reid Spencere81d2da2007-02-16 22:36:51 +0000843// Truncate to new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000844void APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000845 assert(width < BitWidth && "Invalid APInt Truncate request");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000846 // FIXME: implement
Reid Spencere81d2da2007-02-16 22:36:51 +0000847}
848
849// Sign extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000850void APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000851 assert(width > BitWidth && "Invalid APInt SignExtend request");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000852 // FIXME: implement
Reid Spencere81d2da2007-02-16 22:36:51 +0000853}
854
855// Zero extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000856void APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000857 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000858 // FIXME: implement
Reid Spencere81d2da2007-02-16 22:36:51 +0000859}
860
Zhou Shengff4304f2007-02-09 07:48:24 +0000861/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000862/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000863APInt APInt::ashr(uint32_t shiftAmt) const {
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000864 if (isSingleWord()) {
865 if (shiftAmt == BitWidth)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000866 return APInt(BitWidth, -1ULL);
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000867 else
868 return APInt(BitWidth,
869 (((int64_t(VAL) << (APINT_BITS_PER_WORD - BitWidth)) >>
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000870 (APINT_BITS_PER_WORD - BitWidth)) >> shiftAmt)).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000871 }
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000872
873 APInt Result(*this);
874 if (shiftAmt >= BitWidth) {
875 memset(Result.pVal, Result[BitWidth-1] ? 1 : 0,
876 (getNumWords()-1) * APINT_WORD_SIZE);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000877 return Result.clearUnusedBits();
878 }
879
880 // FIXME: bit-at-a-time shift is really slow.
881 uint32_t i = 0;
882 for (; i < BitWidth - shiftAmt; ++i)
883 if (Result[i+shiftAmt])
884 Result.set(i);
885 else
886 Result.clear(i);
887 for (; i < BitWidth; ++i)
888 if (Result[BitWidth-1])
889 Result.set(i);
890 else
891 Result.clear(i);
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000892 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000893}
894
Zhou Shengff4304f2007-02-09 07:48:24 +0000895/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000896/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000897APInt APInt::lshr(uint32_t shiftAmt) const {
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000898 if (isSingleWord())
899 if (shiftAmt == BitWidth)
900 return APInt(BitWidth, 0);
901 else
902 return APInt(BitWidth, this->VAL >> shiftAmt);
903
904 APInt Result(*this);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000905 if (shiftAmt >= BitWidth) {
906 Result.clear();
907 return Result;
908 }
909
910 // FIXME: bit at a time shift is really slow
Reid Spencer24c4a8f2007-02-25 01:56:07 +0000911 uint32_t i = 0;
912 for (i = 0; i < Result.BitWidth - shiftAmt; ++i)
913 if (Result[i+shiftAmt])
914 Result.set(i);
915 else
916 Result.clear(i);
917 for (; i < Result.BitWidth; ++i)
918 Result.clear(i);
919 return Result;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000920}
921
Zhou Shengff4304f2007-02-09 07:48:24 +0000922/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000923/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000924APInt APInt::shl(uint32_t shiftAmt) const {
Reid Spencer5bce8542007-02-24 20:19:37 +0000925 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Reid Spencer87553802007-02-25 00:56:44 +0000926 if (isSingleWord()) {
Reid Spencer5bce8542007-02-24 20:19:37 +0000927 if (shiftAmt == BitWidth)
Reid Spencer87553802007-02-25 00:56:44 +0000928 return APInt(BitWidth, 0); // avoid undefined shift results
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000929 return APInt(BitWidth, VAL << shiftAmt).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000930 }
Reid Spencer5bce8542007-02-24 20:19:37 +0000931
Reid Spencer87553802007-02-25 00:56:44 +0000932 // If all the bits were shifted out, the result is 0. This avoids issues
933 // with shifting by the size of the integer type, which produces undefined
934 // results. We define these "undefined results" to always be 0.
935 if (shiftAmt == BitWidth)
936 return APInt(BitWidth, 0);
937
938 // Create some space for the result.
939 uint64_t * val = new uint64_t[getNumWords()];
940
941 // If we are shifting less than a word, do it the easy way
942 if (shiftAmt < APINT_BITS_PER_WORD) {
943 uint64_t carry = 0;
944 shiftAmt %= APINT_BITS_PER_WORD;
945 for (uint32_t i = 0; i < getNumWords(); i++) {
946 val[i] = pVal[i] << shiftAmt | carry;
947 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
948 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000949 return APInt(val, BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +0000950 }
951
Reid Spencer87553802007-02-25 00:56:44 +0000952 // Compute some values needed by the remaining shift algorithms
953 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
954 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
955
956 // If we are shifting whole words, just move whole words
957 if (wordShift == 0) {
958 for (uint32_t i = 0; i < offset; i++)
959 val[i] = 0;
960 for (uint32_t i = offset; i < getNumWords(); i++)
961 val[i] = pVal[i-offset];
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000962 return APInt(val,BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +0000963 }
Reid Spencer87553802007-02-25 00:56:44 +0000964
965 // Copy whole words from this to Result.
966 uint32_t i = getNumWords() - 1;
967 for (; i > offset; --i)
968 val[i] = pVal[i-offset] << wordShift |
969 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
Reid Spencer438d71e2007-02-25 01:08:58 +0000970 val[offset] = pVal[0] << wordShift;
Reid Spencer87553802007-02-25 00:56:44 +0000971 for (i = 0; i < offset; ++i)
972 val[i] = 0;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000973 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000974}
975
Reid Spencer9c0696f2007-02-20 08:51:03 +0000976/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
977/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
978/// variables here have the same names as in the algorithm. Comments explain
979/// the algorithm and any deviation from it.
980static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
981 uint32_t m, uint32_t n) {
982 assert(u && "Must provide dividend");
983 assert(v && "Must provide divisor");
984 assert(q && "Must provide quotient");
Reid Spencer9d6c9192007-02-24 03:58:46 +0000985 assert(u != v && u != q && v != q && "Must us different memory");
Reid Spencer9c0696f2007-02-20 08:51:03 +0000986 assert(n>1 && "n must be > 1");
987
988 // Knuth uses the value b as the base of the number system. In our case b
989 // is 2^31 so we just set it to -1u.
990 uint64_t b = uint64_t(1) << 32;
991
Reid Spencer9d6c9192007-02-24 03:58:46 +0000992 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n');
993 DEBUG(cerr << "KnuthDiv: original:");
994 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
995 DEBUG(cerr << " by");
996 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
997 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +0000998 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
999 // u and v by d. Note that we have taken Knuth's advice here to use a power
1000 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1001 // 2 allows us to shift instead of multiply and it is easy to determine the
1002 // shift amount from the leading zeros. We are basically normalizing the u
1003 // and v so that its high bits are shifted to the top of v's range without
1004 // overflow. Note that this can require an extra word in u so that u must
1005 // be of length m+n+1.
1006 uint32_t shift = CountLeadingZeros_32(v[n-1]);
1007 uint32_t v_carry = 0;
1008 uint32_t u_carry = 0;
1009 if (shift) {
1010 for (uint32_t i = 0; i < m+n; ++i) {
1011 uint32_t u_tmp = u[i] >> (32 - shift);
1012 u[i] = (u[i] << shift) | u_carry;
1013 u_carry = u_tmp;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001014 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001015 for (uint32_t i = 0; i < n; ++i) {
1016 uint32_t v_tmp = v[i] >> (32 - shift);
1017 v[i] = (v[i] << shift) | v_carry;
1018 v_carry = v_tmp;
1019 }
1020 }
1021 u[m+n] = u_carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001022 DEBUG(cerr << "KnuthDiv: normal:");
1023 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1024 DEBUG(cerr << " by");
1025 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1026 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001027
1028 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1029 int j = m;
1030 do {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001031 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001032 // D3. [Calculate q'.].
1033 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1034 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1035 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1036 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1037 // on v[n-2] determines at high speed most of the cases in which the trial
1038 // value qp is one too large, and it eliminates all cases where qp is two
1039 // too large.
Reid Spencer92904632007-02-23 01:57:13 +00001040 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001041 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n');
Reid Spencer92904632007-02-23 01:57:13 +00001042 uint64_t qp = dividend / v[n-1];
1043 uint64_t rp = dividend % v[n-1];
Reid Spencer9c0696f2007-02-20 08:51:03 +00001044 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1045 qp--;
1046 rp += v[n-1];
Reid Spencer610fad82007-02-24 10:01:42 +00001047 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
Reid Spencer9d6c9192007-02-24 03:58:46 +00001048 qp--;
Reid Spencer92904632007-02-23 01:57:13 +00001049 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001050 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001051
Reid Spencer92904632007-02-23 01:57:13 +00001052 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1053 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1054 // consists of a simple multiplication by a one-place number, combined with
Reid Spencer610fad82007-02-24 10:01:42 +00001055 // a subtraction.
1056 bool isNegative = false;
Reid Spencer92904632007-02-23 01:57:13 +00001057 for (uint32_t i = 0; i < n; ++i) {
Reid Spencer610fad82007-02-24 10:01:42 +00001058 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001059 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
Reid Spencer610fad82007-02-24 10:01:42 +00001060 bool borrow = subtrahend > u_tmp;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001061 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp
Reid Spencer610fad82007-02-24 10:01:42 +00001062 << ", subtrahend == " << subtrahend
1063 << ", borrow = " << borrow << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001064
Reid Spencer610fad82007-02-24 10:01:42 +00001065 uint64_t result = u_tmp - subtrahend;
1066 uint32_t k = j + i;
1067 u[k++] = result & (b-1); // subtract low word
1068 u[k++] = result >> 32; // subtract high word
1069 while (borrow && k <= m+n) { // deal with borrow to the left
1070 borrow = u[k] == 0;
1071 u[k]--;
1072 k++;
1073 }
1074 isNegative |= borrow;
1075 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " <<
1076 u[j+i+1] << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001077 }
1078 DEBUG(cerr << "KnuthDiv: after subtraction:");
1079 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1080 DEBUG(cerr << '\n');
Reid Spencer610fad82007-02-24 10:01:42 +00001081 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1082 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1083 // true value plus b**(n+1), namely as the b's complement of
Reid Spencer92904632007-02-23 01:57:13 +00001084 // the true value, and a "borrow" to the left should be remembered.
1085 //
Reid Spencer610fad82007-02-24 10:01:42 +00001086 if (isNegative) {
1087 bool carry = true; // true because b's complement is "complement + 1"
1088 for (uint32_t i = 0; i <= m+n; ++i) {
1089 u[i] = ~u[i] + carry; // b's complement
1090 carry = carry && u[i] == 0;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001091 }
Reid Spencer92904632007-02-23 01:57:13 +00001092 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001093 DEBUG(cerr << "KnuthDiv: after complement:");
1094 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1095 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001096
1097 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1098 // negative, go to step D6; otherwise go on to step D7.
1099 q[j] = qp;
Reid Spencer610fad82007-02-24 10:01:42 +00001100 if (isNegative) {
Reid Spencer9c0696f2007-02-20 08:51:03 +00001101 // D6. [Add back]. The probability that this step is necessary is very
1102 // small, on the order of only 2/b. Make sure that test data accounts for
Reid Spencer92904632007-02-23 01:57:13 +00001103 // this possibility. Decrease q[j] by 1
1104 q[j]--;
1105 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1106 // A carry will occur to the left of u[j+n], and it should be ignored
1107 // since it cancels with the borrow that occurred in D4.
1108 bool carry = false;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001109 for (uint32_t i = 0; i < n; i++) {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001110 uint32_t limit = std::min(u[j+i],v[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001111 u[j+i] += v[i] + carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001112 carry = u[j+i] < limit || (carry && u[j+i] == limit);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001113 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001114 u[j+n] += carry;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001115 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001116 DEBUG(cerr << "KnuthDiv: after correction:");
1117 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]);
1118 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001119
Reid Spencer92904632007-02-23 01:57:13 +00001120 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1121 } while (--j >= 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001122
Reid Spencer9d6c9192007-02-24 03:58:46 +00001123 DEBUG(cerr << "KnuthDiv: quotient:");
1124 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]);
1125 DEBUG(cerr << '\n');
1126
Reid Spencer9c0696f2007-02-20 08:51:03 +00001127 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1128 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1129 // compute the remainder (urem uses this).
1130 if (r) {
1131 // The value d is expressed by the "shift" value above since we avoided
1132 // multiplication by d by using a shift left. So, all we have to do is
1133 // shift right here. In order to mak
Reid Spencer1050ec52007-02-24 20:38:01 +00001134 if (shift) {
1135 uint32_t carry = 0;
1136 DEBUG(cerr << "KnuthDiv: remainder:");
1137 for (int i = n-1; i >= 0; i--) {
1138 r[i] = (u[i] >> shift) | carry;
1139 carry = u[i] << (32 - shift);
1140 DEBUG(cerr << " " << r[i]);
1141 }
1142 } else {
1143 for (int i = n-1; i >= 0; i--) {
1144 r[i] = u[i];
1145 DEBUG(cerr << " " << r[i]);
1146 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001147 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001148 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001149 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001150 DEBUG(cerr << std::setbase(10) << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001151}
1152
Reid Spencer9c0696f2007-02-20 08:51:03 +00001153void APInt::divide(const APInt LHS, uint32_t lhsWords,
1154 const APInt &RHS, uint32_t rhsWords,
1155 APInt *Quotient, APInt *Remainder)
1156{
1157 assert(lhsWords >= rhsWords && "Fractional result");
1158
1159 // First, compose the values into an array of 32-bit words instead of
1160 // 64-bit words. This is a necessity of both the "short division" algorithm
1161 // and the the Knuth "classical algorithm" which requires there to be native
1162 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1163 // can't use 64-bit operands here because we don't have native results of
1164 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't
1165 // work on large-endian machines.
1166 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8);
1167 uint32_t n = rhsWords * 2;
1168 uint32_t m = (lhsWords * 2) - n;
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001169
1170 // Allocate space for the temporary values we need either on the stack, if
1171 // it will fit, or on the heap if it won't.
1172 uint32_t SPACE[128];
1173 uint32_t *U = 0;
1174 uint32_t *V = 0;
1175 uint32_t *Q = 0;
1176 uint32_t *R = 0;
1177 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1178 U = &SPACE[0];
1179 V = &SPACE[m+n+1];
1180 Q = &SPACE[(m+n+1) + n];
1181 if (Remainder)
1182 R = &SPACE[(m+n+1) + n + (m+n)];
1183 } else {
1184 U = new uint32_t[m + n + 1];
1185 V = new uint32_t[n];
1186 Q = new uint32_t[m+n];
1187 if (Remainder)
1188 R = new uint32_t[n];
1189 }
1190
1191 // Initialize the dividend
Reid Spencer9c0696f2007-02-20 08:51:03 +00001192 memset(U, 0, (m+n+1)*sizeof(uint32_t));
1193 for (unsigned i = 0; i < lhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001194 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001195 U[i * 2] = tmp & mask;
1196 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1197 }
1198 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1199
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001200 // Initialize the divisor
Reid Spencer9c0696f2007-02-20 08:51:03 +00001201 memset(V, 0, (n)*sizeof(uint32_t));
1202 for (unsigned i = 0; i < rhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001203 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001204 V[i * 2] = tmp & mask;
1205 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1206 }
1207
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001208 // initialize the quotient and remainder
Reid Spencer9c0696f2007-02-20 08:51:03 +00001209 memset(Q, 0, (m+n) * sizeof(uint32_t));
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001210 if (Remainder)
Reid Spencer9c0696f2007-02-20 08:51:03 +00001211 memset(R, 0, n * sizeof(uint32_t));
Reid Spencer9c0696f2007-02-20 08:51:03 +00001212
1213 // Now, adjust m and n for the Knuth division. n is the number of words in
1214 // the divisor. m is the number of words by which the dividend exceeds the
1215 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1216 // contain any zero words or the Knuth algorithm fails.
1217 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1218 n--;
1219 m++;
1220 }
1221 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1222 m--;
1223
1224 // If we're left with only a single word for the divisor, Knuth doesn't work
1225 // so we implement the short division algorithm here. This is much simpler
1226 // and faster because we are certain that we can divide a 64-bit quantity
1227 // by a 32-bit quantity at hardware speed and short division is simply a
1228 // series of such operations. This is just like doing short division but we
1229 // are using base 2^32 instead of base 10.
1230 assert(n != 0 && "Divide by zero?");
1231 if (n == 1) {
1232 uint32_t divisor = V[0];
1233 uint32_t remainder = 0;
1234 for (int i = m+n-1; i >= 0; i--) {
1235 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1236 if (partial_dividend == 0) {
1237 Q[i] = 0;
1238 remainder = 0;
1239 } else if (partial_dividend < divisor) {
1240 Q[i] = 0;
1241 remainder = partial_dividend;
1242 } else if (partial_dividend == divisor) {
1243 Q[i] = 1;
1244 remainder = 0;
1245 } else {
1246 Q[i] = partial_dividend / divisor;
1247 remainder = partial_dividend - (Q[i] * divisor);
1248 }
1249 }
1250 if (R)
1251 R[0] = remainder;
1252 } else {
1253 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1254 // case n > 1.
1255 KnuthDiv(U, V, Q, R, m, n);
1256 }
1257
1258 // If the caller wants the quotient
1259 if (Quotient) {
1260 // Set up the Quotient value's memory.
1261 if (Quotient->BitWidth != LHS.BitWidth) {
1262 if (Quotient->isSingleWord())
1263 Quotient->VAL = 0;
1264 else
1265 delete Quotient->pVal;
1266 Quotient->BitWidth = LHS.BitWidth;
1267 if (!Quotient->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001268 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001269 } else
1270 Quotient->clear();
1271
1272 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1273 // order words.
1274 if (lhsWords == 1) {
1275 uint64_t tmp =
1276 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1277 if (Quotient->isSingleWord())
1278 Quotient->VAL = tmp;
1279 else
1280 Quotient->pVal[0] = tmp;
1281 } else {
1282 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1283 for (unsigned i = 0; i < lhsWords; ++i)
1284 Quotient->pVal[i] =
1285 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1286 }
1287 }
1288
1289 // If the caller wants the remainder
1290 if (Remainder) {
1291 // Set up the Remainder value's memory.
1292 if (Remainder->BitWidth != RHS.BitWidth) {
1293 if (Remainder->isSingleWord())
1294 Remainder->VAL = 0;
1295 else
1296 delete Remainder->pVal;
1297 Remainder->BitWidth = RHS.BitWidth;
1298 if (!Remainder->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001299 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001300 } else
1301 Remainder->clear();
1302
1303 // The remainder is in R. Reconstitute the remainder into Remainder's low
1304 // order words.
1305 if (rhsWords == 1) {
1306 uint64_t tmp =
1307 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1308 if (Remainder->isSingleWord())
1309 Remainder->VAL = tmp;
1310 else
1311 Remainder->pVal[0] = tmp;
1312 } else {
1313 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1314 for (unsigned i = 0; i < rhsWords; ++i)
1315 Remainder->pVal[i] =
1316 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1317 }
1318 }
1319
1320 // Clean up the memory we allocated.
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001321 if (U != &SPACE[0]) {
1322 delete [] U;
1323 delete [] V;
1324 delete [] Q;
1325 delete [] R;
1326 }
Reid Spencer5e0a8512007-02-17 03:16:00 +00001327}
1328
Reid Spencere81d2da2007-02-16 22:36:51 +00001329APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001330 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001331
1332 // First, deal with the easy case
1333 if (isSingleWord()) {
1334 assert(RHS.VAL != 0 && "Divide by zero?");
1335 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001336 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001337
Reid Spencer71bd08f2007-02-17 02:07:07 +00001338 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001339 uint32_t rhsBits = RHS.getActiveBits();
1340 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001341 assert(rhsWords && "Divided by zero???");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001342 uint32_t lhsBits = this->getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +00001343 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001344
1345 // Deal with some degenerate cases
1346 if (!lhsWords)
Reid Spencere0cdd332007-02-21 08:21:52 +00001347 // 0 / X ===> 0
1348 return APInt(BitWidth, 0);
1349 else if (lhsWords < rhsWords || this->ult(RHS)) {
1350 // X / Y ===> 0, iff X < Y
1351 return APInt(BitWidth, 0);
1352 } else if (*this == RHS) {
1353 // X / X ===> 1
1354 return APInt(BitWidth, 1);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001355 } else if (lhsWords == 1 && rhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001356 // All high words are zero, just use native divide
Reid Spencere0cdd332007-02-21 08:21:52 +00001357 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001358 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001359
1360 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1361 APInt Quotient(1,0); // to hold result.
1362 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0);
1363 return Quotient;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001364}
1365
Reid Spencere81d2da2007-02-16 22:36:51 +00001366APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001367 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001368 if (isSingleWord()) {
1369 assert(RHS.VAL != 0 && "Remainder by zero?");
1370 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001371 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001372
Reid Spencere0cdd332007-02-21 08:21:52 +00001373 // Get some facts about the LHS
1374 uint32_t lhsBits = getActiveBits();
1375 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001376
1377 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001378 uint32_t rhsBits = RHS.getActiveBits();
1379 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001380 assert(rhsWords && "Performing remainder operation by zero ???");
1381
Reid Spencer71bd08f2007-02-17 02:07:07 +00001382 // Check the degenerate cases
Reid Spencer9c0696f2007-02-20 08:51:03 +00001383 if (lhsWords == 0) {
Reid Spencere0cdd332007-02-21 08:21:52 +00001384 // 0 % Y ===> 0
1385 return APInt(BitWidth, 0);
1386 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1387 // X % Y ===> X, iff X < Y
1388 return *this;
1389 } else if (*this == RHS) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001390 // X % X == 0;
Reid Spencere0cdd332007-02-21 08:21:52 +00001391 return APInt(BitWidth, 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001392 } else if (lhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001393 // All high words are zero, just use native remainder
Reid Spencere0cdd332007-02-21 08:21:52 +00001394 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001395 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001396
1397 // We have to compute it the hard way. Invoke the Knute divide algorithm.
1398 APInt Remainder(1,0);
1399 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder);
1400 return Remainder;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001401}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001402
Reid Spencer385f7542007-02-21 03:55:44 +00001403void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001404 uint8_t radix) {
Reid Spencer385f7542007-02-21 03:55:44 +00001405 // Check our assumptions here
Reid Spencer5e0a8512007-02-17 03:16:00 +00001406 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1407 "Radix should be 2, 8, 10, or 16!");
Reid Spencer385f7542007-02-21 03:55:44 +00001408 assert(str && "String is null?");
1409 assert(slen <= numbits || radix != 2 && "Insufficient bit width");
1410 assert(slen*3 <= numbits || radix != 8 && "Insufficient bit width");
1411 assert(slen*4 <= numbits || radix != 16 && "Insufficient bit width");
1412 assert((slen*64)/20 <= numbits || radix != 10 && "Insufficient bit width");
1413
1414 // Allocate memory
1415 if (!isSingleWord())
1416 pVal = getClearedMemory(getNumWords());
1417
1418 // Figure out if we can shift instead of multiply
1419 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1420
1421 // Set up an APInt for the digit to add outside the loop so we don't
1422 // constantly construct/destruct it.
1423 APInt apdigit(getBitWidth(), 0);
1424 APInt apradix(getBitWidth(), radix);
1425
1426 // Enter digit traversal loop
1427 for (unsigned i = 0; i < slen; i++) {
1428 // Get a digit
1429 uint32_t digit = 0;
1430 char cdigit = str[i];
1431 if (isdigit(cdigit))
1432 digit = cdigit - '0';
1433 else if (isxdigit(cdigit))
1434 if (cdigit >= 'a')
1435 digit = cdigit - 'a' + 10;
1436 else if (cdigit >= 'A')
1437 digit = cdigit - 'A' + 10;
1438 else
1439 assert(0 && "huh?");
1440 else
1441 assert(0 && "Invalid character in digit string");
1442
1443 // Shift or multiple the value by the radix
1444 if (shift)
1445 this->shl(shift);
1446 else
1447 *this *= apradix;
1448
1449 // Add in the digit we just interpreted
Reid Spencer5bce8542007-02-24 20:19:37 +00001450 if (apdigit.isSingleWord())
1451 apdigit.VAL = digit;
1452 else
1453 apdigit.pVal[0] = digit;
Reid Spencer385f7542007-02-21 03:55:44 +00001454 *this += apdigit;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001455 }
1456}
Reid Spencer9c0696f2007-02-20 08:51:03 +00001457
Reid Spencer9c0696f2007-02-20 08:51:03 +00001458std::string APInt::toString(uint8_t radix, bool wantSigned) const {
1459 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1460 "Radix should be 2, 8, 10, or 16!");
1461 static const char *digits[] = {
1462 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
1463 };
1464 std::string result;
1465 uint32_t bits_used = getActiveBits();
1466 if (isSingleWord()) {
1467 char buf[65];
1468 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
1469 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
1470 if (format) {
1471 if (wantSigned) {
1472 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
1473 (APINT_BITS_PER_WORD-BitWidth);
1474 sprintf(buf, format, sextVal);
1475 } else
1476 sprintf(buf, format, VAL);
1477 } else {
1478 memset(buf, 0, 65);
1479 uint64_t v = VAL;
1480 while (bits_used) {
1481 uint32_t bit = v & 1;
1482 bits_used--;
1483 buf[bits_used] = digits[bit][0];
1484 v >>=1;
1485 }
1486 }
1487 result = buf;
1488 return result;
1489 }
1490
1491 if (radix != 10) {
1492 uint64_t mask = radix - 1;
1493 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1);
1494 uint32_t nibbles = APINT_BITS_PER_WORD / shift;
1495 for (uint32_t i = 0; i < getNumWords(); ++i) {
1496 uint64_t value = pVal[i];
1497 for (uint32_t j = 0; j < nibbles; ++j) {
1498 result.insert(0, digits[ value & mask ]);
1499 value >>= shift;
1500 }
1501 }
1502 return result;
1503 }
1504
1505 APInt tmp(*this);
1506 APInt divisor(4, radix);
1507 APInt zero(tmp.getBitWidth(), 0);
1508 size_t insert_at = 0;
1509 if (wantSigned && tmp[BitWidth-1]) {
1510 // They want to print the signed version and it is a negative value
1511 // Flip the bits and add one to turn it into the equivalent positive
1512 // value and put a '-' in the result.
1513 tmp.flip();
1514 tmp++;
1515 result = "-";
1516 insert_at = 1;
1517 }
Reid Spencere549c492007-02-21 00:29:48 +00001518 if (tmp == APInt(tmp.getBitWidth(), 0))
Reid Spencer9c0696f2007-02-20 08:51:03 +00001519 result = "0";
1520 else while (tmp.ne(zero)) {
1521 APInt APdigit(1,0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001522 APInt tmp2(tmp.getBitWidth(), 0);
Reid Spencer385f7542007-02-21 03:55:44 +00001523 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
1524 &APdigit);
1525 uint32_t digit = APdigit.getValue();
1526 assert(digit < radix && "divide failed");
1527 result.insert(insert_at,digits[digit]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001528 tmp = tmp2;
1529 }
1530
1531 return result;
1532}
1533
Reid Spencer385f7542007-02-21 03:55:44 +00001534#ifndef NDEBUG
1535void APInt::dump() const
1536{
Reid Spencer610fad82007-02-24 10:01:42 +00001537 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16);
Reid Spencer385f7542007-02-21 03:55:44 +00001538 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +00001539 cerr << VAL;
Reid Spencer385f7542007-02-21 03:55:44 +00001540 else for (unsigned i = getNumWords(); i > 0; i--) {
Reid Spencer610fad82007-02-24 10:01:42 +00001541 cerr << pVal[i-1] << " ";
Reid Spencer385f7542007-02-21 03:55:44 +00001542 }
Reid Spencer610fad82007-02-24 10:01:42 +00001543 cerr << " (" << this->toString(10, false) << ")\n" << std::setbase(10);
Reid Spencer385f7542007-02-21 03:55:44 +00001544}
1545#endif