blob: 60c940c566da75209e31daeb3667f93c8818949f [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//
11// This file implements a class to represent arbitrary precision integral
12// constant values.
13//
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 Spenceraf0e9562007-02-18 18:38:44 +000029// A utility function for allocating memory, checking for allocation failures,
Reid Spencer610fad82007-02-24 10:01:42 +000030// 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 Spenceraf0e9562007-02-18 18:38:44 +000038// A utility function for allocating memory and checking for allocation failure.
Reid Spencer610fad82007-02-24 10:01:42 +000039// The content is not zero'd
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");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000050 if (isSingleWord())
Reid Spencere81d2da2007-02-16 22:36:51 +000051 VAL = val & (~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth));
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 }
56}
57
Reid Spenceraf0e9562007-02-18 18:38:44 +000058APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[])
Reid Spencer385f7542007-02-21 03:55:44 +000059 : BitWidth(numBits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000060 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
61 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000062 assert(bigVal && "Null pointer detected!");
63 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +000064 VAL = bigVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +000065 else {
Reid Spencer610fad82007-02-24 10:01:42 +000066 // Get memory, cleared to 0
67 pVal = getClearedMemory(getNumWords());
68 // Calculate the number of words to copy
69 uint32_t words = std::min<uint32_t>(numWords, getNumWords());
70 // Copy the words from bigVal to pVal
71 memcpy(pVal, bigVal, words * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000072 }
Reid Spencer610fad82007-02-24 10:01:42 +000073 // Make sure unused high bits are cleared
74 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000075}
76
Reid Spenceraf0e9562007-02-18 18:38:44 +000077APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
Reid Spencer9c0696f2007-02-20 08:51:03 +000078 uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000079 : BitWidth(numbits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000080 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000081}
82
Reid Spencer9c0696f2007-02-20 08:51:03 +000083APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000084 : BitWidth(numbits), VAL(0) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000085 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000086 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000087}
88
Reid Spencer54362ca2007-02-20 23:40:25 +000089APInt::APInt(const APInt& that)
Reid Spencer385f7542007-02-21 03:55:44 +000090 : BitWidth(that.BitWidth), VAL(0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +000091 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +000092 VAL = that.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000093 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000094 pVal = getMemory(getNumWords());
Reid Spencer54362ca2007-02-20 23:40:25 +000095 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000096 }
97}
98
99APInt::~APInt() {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000100 if (!isSingleWord() && pVal)
101 delete[] pVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000102}
103
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000104APInt& APInt::operator=(const APInt& RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000105 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
106 if (isSingleWord())
Reid Spenceraf0e9562007-02-18 18:38:44 +0000107 VAL = RHS.VAL;
108 else
Reid Spencera58f0582007-02-18 20:09:41 +0000109 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000110 return *this;
111}
112
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000113APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000114 if (isSingleWord())
115 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000116 else {
117 pVal[0] = RHS;
Reid Spencera58f0582007-02-18 20:09:41 +0000118 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000119 }
120 return *this;
121}
122
Reid Spenceraf0e9562007-02-18 18:38:44 +0000123/// add_1 - This function adds a single "digit" integer, y, to the multiple
124/// "digit" integer array, x[]. x[] is modified to reflect the addition and
125/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000126/// @returns the carry of the addition.
Reid Spencer610fad82007-02-24 10:01:42 +0000127static uint64_t add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000128 for (uint32_t i = 0; i < len; ++i) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000129 dest[i] = y + x[i];
130 if (dest[i] < y)
Reid Spencer610fad82007-02-24 10:01:42 +0000131 y = 1; // Carry one to next digit.
Reid Spencerf2c521c2007-02-18 06:39:42 +0000132 else {
Reid Spencer610fad82007-02-24 10:01:42 +0000133 y = 0; // No need to carry so exit early
Reid Spencerf2c521c2007-02-18 06:39:42 +0000134 break;
135 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000136 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000137 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000138}
139
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000140/// @brief Prefix increment operator. Increments the APInt by one.
141APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000142 if (isSingleWord())
143 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000144 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000145 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000146 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000147 return *this;
148}
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.
155static uint64_t sub_1(uint64_t x[], uint32_t len,
156 uint64_t y) {
157 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 Spencerf2c521c2007-02-18 06:39:42 +0000167 return 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 Spencere81d2da2007-02-16 22:36:51 +0000176 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000177 return *this;
178}
179
Reid Spencer5e0a8512007-02-17 03:16:00 +0000180/// add - This function adds the integer array x[] by integer array
181/// y[] and returns the carry.
Reid Spencer9d6c9192007-02-24 03:58:46 +0000182static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
183 uint32_t len) {
184 bool carry = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000185 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer92904632007-02-23 01:57:13 +0000186 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
Reid Spencer54362ca2007-02-20 23:40:25 +0000187 dest[i] = x[i] + y[i] + carry;
Reid Spencer60c0a6a2007-02-21 05:44:56 +0000188 carry = dest[i] < limit || (carry && dest[i] == limit);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000189 }
190 return carry;
191}
192
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000193/// @brief Addition assignment operator. Adds this APInt by the given APInt&
194/// RHS and assigns the result to this APInt.
195APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000196 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000197 if (isSingleWord())
198 VAL += RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000199 else {
Reid Spencer54362ca2007-02-20 23:40:25 +0000200 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201 }
Reid Spencere81d2da2007-02-16 22:36:51 +0000202 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000203 return *this;
204}
205
Reid Spencer5e0a8512007-02-17 03:16:00 +0000206/// sub - This function subtracts the integer array x[] by
Reid Spencer610fad82007-02-24 10:01:42 +0000207/// integer array y[], and returns the borrow-out.
Reid Spencer9d6c9192007-02-24 03:58:46 +0000208static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
209 uint32_t len) {
Reid Spencer385f7542007-02-21 03:55:44 +0000210 bool borrow = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000211 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer385f7542007-02-21 03:55:44 +0000212 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
213 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
214 dest[i] = x_tmp - y[i];
Reid Spencer5e0a8512007-02-17 03:16:00 +0000215 }
Reid Spencer54362ca2007-02-20 23:40:25 +0000216 return borrow;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000217}
218
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000219/// @brief Subtraction assignment operator. Subtracts this APInt by the given
220/// APInt &RHS and assigns the result to this APInt.
221APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000222 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000223 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +0000224 VAL -= RHS.VAL;
225 else
226 sub(pVal, pVal, RHS.pVal, getNumWords());
Reid Spencere81d2da2007-02-16 22:36:51 +0000227 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000228 return *this;
229}
230
Reid Spencer5e0a8512007-02-17 03:16:00 +0000231/// mul_1 - This function performs the multiplication operation on a
232/// large integer (represented as an integer array) and a uint64_t integer.
233/// @returns the carry of the multiplication.
Reid Spencer610fad82007-02-24 10:01:42 +0000234static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
235 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
Reid Spencer5e0a8512007-02-17 03:16:00 +0000236 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
237 uint64_t carry = 0, lx, hx;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000238 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000239 lx = x[i] & 0xffffffffULL;
240 hx = x[i] >> 32;
241 // hasCarry - A flag to indicate if has carry.
242 // hasCarry == 0, no carry
243 // hasCarry == 1, has carry
244 // hasCarry == 2, no carry and the calculation result == 0.
245 uint8_t hasCarry = 0;
246 dest[i] = carry + lx * ly;
247 // Determine if the add above introduces carry.
248 hasCarry = (dest[i] < carry) ? 1 : 0;
249 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
250 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
251 // (2^32 - 1) + 2^32 = 2^64.
252 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
253
254 carry += (lx * hy) & 0xffffffffULL;
255 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
256 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
257 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
258 }
259
260 return carry;
261}
262
263/// mul - This function multiplies integer array x[] by integer array y[] and
264/// stores the result into integer array dest[].
265/// Note the array dest[]'s size should no less than xlen + ylen.
Reid Spencer610fad82007-02-24 10:01:42 +0000266static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[],
267 uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000268 dest[xlen] = mul_1(dest, x, xlen, y[0]);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000269 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000270 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
Reid Spencere0cdd332007-02-21 08:21:52 +0000271 uint64_t carry = 0, lx = 0, hx = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000272 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000273 lx = x[j] & 0xffffffffULL;
274 hx = x[j] >> 32;
275 // hasCarry - A flag to indicate if has carry.
276 // hasCarry == 0, no carry
277 // hasCarry == 1, has carry
278 // hasCarry == 2, no carry and the calculation result == 0.
279 uint8_t hasCarry = 0;
280 uint64_t resul = carry + lx * ly;
281 hasCarry = (resul < carry) ? 1 : 0;
282 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
283 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
284
285 carry += (lx * hy) & 0xffffffffULL;
286 resul = (carry << 32) | (resul & 0xffffffffULL);
287 dest[i+j] += resul;
288 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
289 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
290 ((lx * hy) >> 32) + hx * hy;
291 }
292 dest[i+xlen] = carry;
293 }
294}
295
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000296/// @brief Multiplication assignment operator. Multiplies this APInt by the
297/// given APInt& RHS and assigns the result to this APInt.
298APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000299 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencere0cdd332007-02-21 08:21:52 +0000300 if (isSingleWord()) {
Reid Spencer61eb1802007-02-20 20:42:10 +0000301 VAL *= RHS.VAL;
Reid Spencere0cdd332007-02-21 08:21:52 +0000302 clearUnusedBits();
303 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000304 }
Reid Spencere0cdd332007-02-21 08:21:52 +0000305
306 // Get some bit facts about LHS and check for zero
307 uint32_t lhsBits = getActiveBits();
308 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
309 if (!lhsWords)
310 // 0 * X ===> 0
311 return *this;
312
313 // Get some bit facts about RHS and check for zero
314 uint32_t rhsBits = RHS.getActiveBits();
315 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
316 if (!rhsWords) {
317 // X * 0 ===> 0
318 clear();
319 return *this;
320 }
321
322 // Allocate space for the result
323 uint32_t destWords = rhsWords + lhsWords;
324 uint64_t *dest = getMemory(destWords);
325
326 // Perform the long multiply
327 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
328
329 // Copy result back into *this
330 clear();
331 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
332 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
333
334 // delete dest array and return
335 delete[] dest;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000336 return *this;
337}
338
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000339/// @brief Bitwise AND assignment operator. Performs bitwise AND operation on
340/// this APInt and the given APInt& RHS, assigns the result to this APInt.
341APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000342 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000343 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000344 VAL &= RHS.VAL;
345 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000346 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000347 uint32_t numWords = getNumWords();
348 for (uint32_t i = 0; i < numWords; ++i)
349 pVal[i] &= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000350 return *this;
351}
352
353/// @brief Bitwise OR assignment operator. Performs bitwise OR operation on
354/// this APInt and the given APInt& RHS, assigns the result to this APInt.
355APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000356 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000357 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000358 VAL |= RHS.VAL;
359 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000360 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000361 uint32_t numWords = getNumWords();
362 for (uint32_t i = 0; i < numWords; ++i)
363 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000364 return *this;
365}
366
367/// @brief Bitwise XOR assignment operator. Performs bitwise XOR operation on
368/// this APInt and the given APInt& RHS, assigns the result to this APInt.
369APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000370 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000371 if (isSingleWord()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000372 VAL ^= RHS.VAL;
Reid Spencer54362ca2007-02-20 23:40:25 +0000373 this->clearUnusedBits();
Reid Spencerf2c521c2007-02-18 06:39:42 +0000374 return *this;
375 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000376 uint32_t numWords = getNumWords();
377 for (uint32_t i = 0; i < numWords; ++i)
378 pVal[i] ^= RHS.pVal[i];
Reid Spencer54362ca2007-02-20 23:40:25 +0000379 this->clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000380 return *this;
381}
382
383/// @brief Bitwise AND operator. Performs bitwise AND operation on this APInt
384/// and the given APInt& RHS.
385APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000386 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000387 if (isSingleWord())
388 return APInt(getBitWidth(), VAL & RHS.VAL);
389
390 APInt Result(*this);
391 uint32_t numWords = getNumWords();
392 for (uint32_t i = 0; i < numWords; ++i)
393 Result.pVal[i] &= RHS.pVal[i];
394 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000395}
396
397/// @brief Bitwise OR operator. Performs bitwise OR operation on this APInt
398/// and the given APInt& RHS.
399APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000400 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000401 if (isSingleWord())
402 return APInt(getBitWidth(), VAL | RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000403
Reid Spenceraf0e9562007-02-18 18:38:44 +0000404 APInt Result(*this);
405 uint32_t numWords = getNumWords();
406 for (uint32_t i = 0; i < numWords; ++i)
407 Result.pVal[i] |= RHS.pVal[i];
408 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000409}
410
411/// @brief Bitwise XOR operator. Performs bitwise XOR operation on this APInt
412/// and the given APInt& RHS.
413APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000414 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000415 if (isSingleWord()) {
416 APInt Result(BitWidth, VAL ^ RHS.VAL);
417 Result.clearUnusedBits();
418 return Result;
419 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000420 APInt Result(*this);
421 uint32_t numWords = getNumWords();
422 for (uint32_t i = 0; i < numWords; ++i)
423 Result.pVal[i] ^= RHS.pVal[i];
424 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000425}
426
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000427/// @brief Logical negation operator. Performs logical negation operation on
428/// this APInt.
429bool APInt::operator !() const {
430 if (isSingleWord())
431 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000432
433 for (uint32_t i = 0; i < getNumWords(); ++i)
434 if (pVal[i])
435 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000436 return true;
437}
438
439/// @brief Multiplication operator. Multiplies this APInt by the given APInt&
440/// RHS.
441APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000442 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000443 if (isSingleWord()) {
444 APInt Result(BitWidth, VAL * RHS.VAL);
445 Result.clearUnusedBits();
446 return Result;
447 }
Reid Spencer61eb1802007-02-20 20:42:10 +0000448 APInt Result(*this);
449 Result *= RHS;
450 Result.clearUnusedBits();
451 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000452}
453
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000454/// @brief Addition operator. Adds this APInt by the given APInt& RHS.
455APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000456 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000457 if (isSingleWord()) {
458 APInt Result(BitWidth, VAL + RHS.VAL);
459 Result.clearUnusedBits();
460 return Result;
461 }
462 APInt Result(BitWidth, 0);
463 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
464 Result.clearUnusedBits();
465 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000466}
467
468/// @brief Subtraction operator. Subtracts this APInt by the given APInt& RHS
469APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000470 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000471 if (isSingleWord()) {
472 APInt Result(BitWidth, VAL - RHS.VAL);
473 Result.clearUnusedBits();
474 return Result;
475 }
476 APInt Result(BitWidth, 0);
477 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
478 Result.clearUnusedBits();
479 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000480}
481
482/// @brief Array-indexing support.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000483bool APInt::operator[](uint32_t bitPosition) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000484 return (maskBit(bitPosition) & (isSingleWord() ?
485 VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000486}
487
488/// @brief Equality operator. Compare this APInt with the given APInt& RHS
489/// for the validity of the equality relationship.
490bool APInt::operator==(const APInt& RHS) const {
Reid Spencer54362ca2007-02-20 23:40:25 +0000491 if (isSingleWord())
492 return VAL == RHS.VAL;
493
Reid Spenceraf0e9562007-02-18 18:38:44 +0000494 uint32_t n1 = getActiveBits();
495 uint32_t n2 = RHS.getActiveBits();
Reid Spencer54362ca2007-02-20 23:40:25 +0000496 if (n1 != n2)
497 return false;
498
499 if (n1 <= APINT_BITS_PER_WORD)
500 return pVal[0] == RHS.pVal[0];
501
502 for (int i = whichWord(n1 - 1); i >= 0; --i)
503 if (pVal[i] != RHS.pVal[i])
504 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000505 return true;
506}
507
Zhou Shenga3832fd2007-02-07 06:14:53 +0000508/// @brief Equality operator. Compare this APInt with the given uint64_t value
509/// for the validity of the equality relationship.
510bool APInt::operator==(uint64_t Val) const {
511 if (isSingleWord())
512 return VAL == Val;
Reid Spencer54362ca2007-02-20 23:40:25 +0000513
514 uint32_t n = getActiveBits();
515 if (n <= APINT_BITS_PER_WORD)
516 return pVal[0] == Val;
517 else
518 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000519}
520
Reid Spencere81d2da2007-02-16 22:36:51 +0000521/// @brief Unsigned less than comparison
522bool APInt::ult(const APInt& RHS) const {
523 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
524 if (isSingleWord())
525 return VAL < RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000526 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000527 uint32_t n1 = getActiveBits();
528 uint32_t n2 = RHS.getActiveBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000529 if (n1 < n2)
530 return true;
531 else if (n2 < n1)
532 return false;
Reid Spencer443b5702007-02-18 00:44:22 +0000533 else if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
Reid Spencere81d2da2007-02-16 22:36:51 +0000534 return pVal[0] < RHS.pVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000535 for (int i = whichWord(n1 - 1); i >= 0; --i) {
536 if (pVal[i] > RHS.pVal[i]) return false;
537 else if (pVal[i] < RHS.pVal[i]) return true;
538 }
539 }
540 return false;
541}
542
Reid Spencere81d2da2007-02-16 22:36:51 +0000543/// @brief Signed less than comparison
544bool APInt::slt(const APInt& RHS) const {
545 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
Reid Spencera58f0582007-02-18 20:09:41 +0000546 if (isSingleWord()) {
547 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
548 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
549 return lhsSext < rhsSext;
Reid Spencere81d2da2007-02-16 22:36:51 +0000550 }
Reid Spencera58f0582007-02-18 20:09:41 +0000551
552 APInt lhs(*this);
553 APInt rhs(*this);
554 bool lhsNegative = false;
555 bool rhsNegative = false;
556 if (lhs[BitWidth-1]) {
557 lhsNegative = true;
558 lhs.flip();
559 lhs++;
560 }
561 if (rhs[BitWidth-1]) {
562 rhsNegative = true;
563 rhs.flip();
564 rhs++;
565 }
566 if (lhsNegative)
567 if (rhsNegative)
568 return !lhs.ult(rhs);
569 else
570 return true;
571 else if (rhsNegative)
572 return false;
573 else
574 return lhs.ult(rhs);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000575}
576
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000577/// Set the given bit to 1 whose poition is given as "bitPosition".
578/// @brief Set a given bit to 1.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000579APInt& APInt::set(uint32_t bitPosition) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000580 if (isSingleWord()) VAL |= maskBit(bitPosition);
581 else pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
582 return *this;
583}
584
585/// @brief Set every bit to 1.
586APInt& APInt::set() {
Reid Spencer443b5702007-02-18 00:44:22 +0000587 if (isSingleWord())
588 VAL = ~0ULL >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengb04973e2007-02-15 06:36:31 +0000589 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000590 for (uint32_t i = 0; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000591 pVal[i] = -1ULL;
Reid Spencer443b5702007-02-18 00:44:22 +0000592 pVal[getNumWords() - 1] = ~0ULL >>
593 (APINT_BITS_PER_WORD - BitWidth % APINT_BITS_PER_WORD);
Zhou Shengb04973e2007-02-15 06:36:31 +0000594 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000595 return *this;
596}
597
598/// Set the given bit to 0 whose position is given as "bitPosition".
599/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000600APInt& APInt::clear(uint32_t bitPosition) {
601 if (isSingleWord())
602 VAL &= ~maskBit(bitPosition);
603 else
604 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000605 return *this;
606}
607
608/// @brief Set every bit to 0.
609APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000610 if (isSingleWord())
611 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000612 else
Reid Spencera58f0582007-02-18 20:09:41 +0000613 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000614 return *this;
615}
616
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000617/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
618/// this APInt.
619APInt APInt::operator~() const {
620 APInt API(*this);
621 API.flip();
622 return API;
623}
624
625/// @brief Toggle every bit to its opposite value.
626APInt& APInt::flip() {
Reid Spencer443b5702007-02-18 00:44:22 +0000627 if (isSingleWord()) VAL = (~(VAL <<
628 (APINT_BITS_PER_WORD - BitWidth))) >> (APINT_BITS_PER_WORD - BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000629 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000630 uint32_t i = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000631 for (; i < getNumWords() - 1; ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000632 pVal[i] = ~pVal[i];
Reid Spenceraf0e9562007-02-18 18:38:44 +0000633 uint32_t offset =
Reid Spencer443b5702007-02-18 00:44:22 +0000634 APINT_BITS_PER_WORD - (BitWidth - APINT_BITS_PER_WORD * (i - 1));
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000635 pVal[i] = (~(pVal[i] << offset)) >> offset;
636 }
637 return *this;
638}
639
640/// Toggle a given bit to its opposite value whose position is given
641/// as "bitPosition".
642/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000643APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000644 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000645 if ((*this)[bitPosition]) clear(bitPosition);
646 else set(bitPosition);
647 return *this;
648}
649
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000650/// getMaxValue - This function returns the largest value
651/// for an APInt of the specified bit-width and if isSign == true,
652/// it should be largest signed value, otherwise unsigned value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000653APInt APInt::getMaxValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000654 APInt Result(numBits, 0);
655 Result.set();
656 if (isSign)
657 Result.clear(numBits - 1);
658 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000659}
660
661/// getMinValue - This function returns the smallest value for
662/// an APInt of the given bit-width and if isSign == true,
663/// it should be smallest signed value, otherwise zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000664APInt APInt::getMinValue(uint32_t numBits, bool isSign) {
Reid Spencerf99a0ac2007-02-18 22:29:05 +0000665 APInt Result(numBits, 0);
666 if (isSign)
667 Result.set(numBits - 1);
668 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000669}
670
671/// getAllOnesValue - This function returns an all-ones value for
672/// an APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000673APInt APInt::getAllOnesValue(uint32_t numBits) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000674 return getMaxValue(numBits, false);
675}
676
677/// getNullValue - This function creates an '0' value for an
678/// APInt of the specified bit-width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000679APInt APInt::getNullValue(uint32_t numBits) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000680 return getMinValue(numBits, false);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000681}
682
683/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000684APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000685 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000686}
687
688/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000689APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000690 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
691 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000692}
693
Reid Spencere81d2da2007-02-16 22:36:51 +0000694bool APInt::isPowerOf2() const {
695 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
696}
697
698/// countLeadingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000699/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000700/// countLeadingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000701/// the number of zeros from the most significant bit to the first one bit.
702/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000703uint32_t APInt::countLeadingZeros() const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000704 uint32_t Count = 0;
Reid Spencere549c492007-02-21 00:29:48 +0000705 if (isSingleWord())
706 Count = CountLeadingZeros_64(VAL);
707 else {
708 for (uint32_t i = getNumWords(); i > 0u; --i) {
709 if (pVal[i-1] == 0)
710 Count += APINT_BITS_PER_WORD;
711 else {
712 Count += CountLeadingZeros_64(pVal[i-1]);
713 break;
714 }
715 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000716 }
Reid Spencerab2b2c82007-02-22 00:22:00 +0000717 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD;
718 if (remainder)
719 Count -= APINT_BITS_PER_WORD - remainder;
720 return Count;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000721}
722
Reid Spencere81d2da2007-02-16 22:36:51 +0000723/// countTrailingZeros - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000724/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000725/// countTrailingZeros_{32, 64}. It performs platform optimal form of counting
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000726/// the number of zeros from the least significant bit to the first one bit.
727/// @returns numWord() * 64 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000728uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000729 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000730 return CountTrailingZeros_64(VAL);
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000731 APInt Tmp( ~(*this) & ((*this) - APInt(BitWidth,1)) );
Reid Spencere81d2da2007-02-16 22:36:51 +0000732 return getNumWords() * APINT_BITS_PER_WORD - Tmp.countLeadingZeros();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000733}
734
Reid Spencere81d2da2007-02-16 22:36:51 +0000735/// countPopulation - This function is a APInt version corresponding to
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000736/// llvm/include/llvm/Support/MathExtras.h's function
Reid Spencere81d2da2007-02-16 22:36:51 +0000737/// countPopulation_{32, 64}. It counts the number of set bits in a value.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000738/// @returns 0 if the value is zero.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000739uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740 if (isSingleWord())
741 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000742 uint32_t Count = 0;
743 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000744 Count += CountPopulation_64(pVal[i]);
745 return Count;
746}
747
748
Reid Spencere81d2da2007-02-16 22:36:51 +0000749/// byteSwap - This function returns a byte-swapped representation of the
Zhou Shengff4304f2007-02-09 07:48:24 +0000750/// this APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +0000751APInt APInt::byteSwap() const {
752 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
753 if (BitWidth == 16)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000754 return APInt(BitWidth, ByteSwap_16(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000755 else if (BitWidth == 32)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000756 return APInt(BitWidth, ByteSwap_32(VAL));
Reid Spencere81d2da2007-02-16 22:36:51 +0000757 else if (BitWidth == 48) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000758 uint64_t Tmp1 = ((VAL >> 32) << 16) | (VAL & 0xFFFF);
759 Tmp1 = ByteSwap_32(Tmp1);
760 uint64_t Tmp2 = (VAL >> 16) & 0xFFFF;
761 Tmp2 = ByteSwap_16(Tmp2);
762 return
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000763 APInt(BitWidth,
764 (Tmp1 & 0xff) | ((Tmp1<<16) & 0xffff00000000ULL) | (Tmp2 << 16));
Reid Spencere81d2da2007-02-16 22:36:51 +0000765 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000766 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000767 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000768 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000769 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000770 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000771 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000772 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
773 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000774 }
775 return Result;
776 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000777}
778
779/// GreatestCommonDivisor - This function returns the greatest common
780/// divisor of the two APInt values using Enclid's algorithm.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000781APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
782 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000783 APInt A = API1, B = API2;
784 while (!!B) {
785 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000786 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000787 A = T;
788 }
789 return A;
790}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000791
Zhou Shengd93f00c2007-02-12 20:02:55 +0000792/// DoubleRoundToAPInt - This function convert a double value to
793/// a APInt value.
Reid Spencere81d2da2007-02-16 22:36:51 +0000794APInt llvm::APIntOps::RoundDoubleToAPInt(double Double) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000795 union {
796 double D;
797 uint64_t I;
798 } T;
799 T.D = Double;
800 bool isNeg = T.I >> 63;
801 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
802 if (exp < 0)
Reid Spencere81d2da2007-02-16 22:36:51 +0000803 return APInt(64ull, 0u);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000804 uint64_t mantissa = ((T.I << 12) >> 12) | (1ULL << 52);
805 if (exp < 52)
Reid Spencere81d2da2007-02-16 22:36:51 +0000806 return isNeg ? -APInt(64u, mantissa >> (52 - exp)) :
807 APInt(64u, mantissa >> (52 - exp));
808 APInt Tmp(exp + 1, mantissa);
809 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000810 return isNeg ? -Tmp : Tmp;
811}
812
Reid Spencerdb3faa62007-02-13 22:41:58 +0000813/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000814/// The layout for double is as following (IEEE Standard 754):
815/// --------------------------------------
816/// | Sign Exponent Fraction Bias |
817/// |-------------------------------------- |
818/// | 1[63] 11[62-52] 52[51-00] 1023 |
819/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000820double APInt::roundToDouble(bool isSigned) const {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000821
822 // Handle the simple case where the value is contained in one uint64_t.
Reid Spencera58f0582007-02-18 20:09:41 +0000823 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
824 if (isSigned) {
825 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
826 return double(sext);
827 } else
828 return double(VAL);
829 }
830
Reid Spencer9c0696f2007-02-20 08:51:03 +0000831 // Determine if the value is negative.
Reid Spencere81d2da2007-02-16 22:36:51 +0000832 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000833
834 // Construct the absolute value if we're negative.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000835 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spencer9c0696f2007-02-20 08:51:03 +0000836
837 // Figure out how many bits we're using.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000838 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000839
Reid Spencer9c0696f2007-02-20 08:51:03 +0000840 // The exponent (without bias normalization) is just the number of bits
841 // we are using. Note that the sign bit is gone since we constructed the
842 // absolute value.
843 uint64_t exp = n;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000844
Reid Spencer9c0696f2007-02-20 08:51:03 +0000845 // Return infinity for exponent overflow
846 if (exp > 1023) {
847 if (!isSigned || !isNeg)
Reid Spencer61eb1802007-02-20 20:42:10 +0000848 return double(1.0E300 * 1.0E300); // positive infinity
Reid Spencer9c0696f2007-02-20 08:51:03 +0000849 else
Reid Spencer61eb1802007-02-20 20:42:10 +0000850 return double(-1.0E300 * 1.0E300); // negative infinity
Reid Spencer9c0696f2007-02-20 08:51:03 +0000851 }
852 exp += 1023; // Increment for 1023 bias
853
854 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
855 // extract the high 52 bits from the correct words in pVal.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000856 uint64_t mantissa;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000857 unsigned hiWord = whichWord(n-1);
858 if (hiWord == 0) {
859 mantissa = Tmp.pVal[0];
860 if (n > 52)
861 mantissa >>= n - 52; // shift down, we want the top 52 bits.
862 } else {
863 assert(hiWord > 0 && "huh?");
864 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
865 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
866 mantissa = hibits | lobits;
867 }
868
Zhou Shengd93f00c2007-02-12 20:02:55 +0000869 // The leading bit of mantissa is implicit, so get rid of it.
Reid Spencer443b5702007-02-18 00:44:22 +0000870 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000871 union {
872 double D;
873 uint64_t I;
874 } T;
875 T.I = sign | (exp << 52) | mantissa;
876 return T.D;
877}
878
Reid Spencere81d2da2007-02-16 22:36:51 +0000879// Truncate to new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000880void APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000881 assert(width < BitWidth && "Invalid APInt Truncate request");
882}
883
884// Sign extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000885void APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000886 assert(width > BitWidth && "Invalid APInt SignExtend request");
887}
888
889// Zero extend to a new width.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000890void APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000891 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
892}
893
Zhou Shengff4304f2007-02-09 07:48:24 +0000894/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000895/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000896APInt APInt::ashr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000897 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000898 if (API.isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000899 API.VAL =
900 (((int64_t(API.VAL) << (APINT_BITS_PER_WORD - API.BitWidth)) >>
901 (APINT_BITS_PER_WORD - API.BitWidth)) >> shiftAmt) &
902 (~uint64_t(0UL) >> (APINT_BITS_PER_WORD - API.BitWidth));
Zhou Sheng0b706b12007-02-08 14:35:19 +0000903 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000904 if (shiftAmt >= API.BitWidth) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000905 memset(API.pVal, API[API.BitWidth-1] ? 1 : 0,
Reid Spencera58f0582007-02-18 20:09:41 +0000906 (API.getNumWords()-1) * APINT_WORD_SIZE);
Reid Spencer443b5702007-02-18 00:44:22 +0000907 API.pVal[API.getNumWords() - 1] =
908 ~uint64_t(0UL) >>
909 (APINT_BITS_PER_WORD - API.BitWidth % APINT_BITS_PER_WORD);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000910 } else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000911 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000912 for (; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000913 if (API[i+shiftAmt])
914 API.set(i);
915 else
916 API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000917 for (; i < API.BitWidth; ++i)
918 if (API[API.BitWidth-1])
Zhou Shengb04973e2007-02-15 06:36:31 +0000919 API.set(i);
920 else API.clear(i);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000921 }
922 }
923 return API;
924}
925
Zhou Shengff4304f2007-02-09 07:48:24 +0000926/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000927/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000928APInt APInt::lshr(uint32_t shiftAmt) const {
Zhou Shengff4304f2007-02-09 07:48:24 +0000929 APInt API(*this);
Zhou Sheng0b706b12007-02-08 14:35:19 +0000930 if (API.isSingleWord())
931 API.VAL >>= shiftAmt;
932 else {
Reid Spencere81d2da2007-02-16 22:36:51 +0000933 if (shiftAmt >= API.BitWidth)
Reid Spencera58f0582007-02-18 20:09:41 +0000934 memset(API.pVal, 0, API.getNumWords() * APINT_WORD_SIZE);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000935 uint32_t i = 0;
Reid Spencere81d2da2007-02-16 22:36:51 +0000936 for (i = 0; i < API.BitWidth - shiftAmt; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000937 if (API[i+shiftAmt]) API.set(i);
938 else API.clear(i);
Reid Spencere81d2da2007-02-16 22:36:51 +0000939 for (; i < API.BitWidth; ++i)
Zhou Sheng0b706b12007-02-08 14:35:19 +0000940 API.clear(i);
941 }
942 return API;
943}
944
Zhou Shengff4304f2007-02-09 07:48:24 +0000945/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +0000946/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000947APInt APInt::shl(uint32_t shiftAmt) const {
Reid Spencer5bce8542007-02-24 20:19:37 +0000948 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Zhou Shengff4304f2007-02-09 07:48:24 +0000949 APInt API(*this);
Reid Spencer5bce8542007-02-24 20:19:37 +0000950 if (API.isSingleWord()) {
951 if (shiftAmt == BitWidth)
952 API.VAL = 0;
953 else
954 API.VAL <<= shiftAmt;
955 API.clearUnusedBits();
956 return API;
Zhou Sheng0b706b12007-02-08 14:35:19 +0000957 }
Reid Spencer5bce8542007-02-24 20:19:37 +0000958
959 if (shiftAmt == BitWidth) {
960 memset(API.pVal, 0, getNumWords() * APINT_WORD_SIZE);
961 return API;
962 }
963
964 if (uint32_t offset = shiftAmt / APINT_BITS_PER_WORD) {
965 for (uint32_t i = API.getNumWords() - 1; i > offset - 1; --i)
966 API.pVal[i] = API.pVal[i-offset];
967 memset(API.pVal, 0, offset * APINT_WORD_SIZE);
968 }
969 shiftAmt %= APINT_BITS_PER_WORD;
970 uint32_t i;
971 for (i = API.getNumWords() - 1; i > 0; --i)
972 API.pVal[i] = (API.pVal[i] << shiftAmt) |
973 (API.pVal[i-1] >> (APINT_BITS_PER_WORD - shiftAmt));
974 API.pVal[i] <<= shiftAmt;
Reid Spencere81d2da2007-02-16 22:36:51 +0000975 API.clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +0000976 return API;
977}
978
Reid Spencer9c0696f2007-02-20 08:51:03 +0000979/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
980/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
981/// variables here have the same names as in the algorithm. Comments explain
982/// the algorithm and any deviation from it.
983static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
984 uint32_t m, uint32_t n) {
985 assert(u && "Must provide dividend");
986 assert(v && "Must provide divisor");
987 assert(q && "Must provide quotient");
Reid Spencer9d6c9192007-02-24 03:58:46 +0000988 assert(u != v && u != q && v != q && "Must us different memory");
Reid Spencer9c0696f2007-02-20 08:51:03 +0000989 assert(n>1 && "n must be > 1");
990
991 // Knuth uses the value b as the base of the number system. In our case b
992 // is 2^31 so we just set it to -1u.
993 uint64_t b = uint64_t(1) << 32;
994
Reid Spencer9d6c9192007-02-24 03:58:46 +0000995 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n');
996 DEBUG(cerr << "KnuthDiv: original:");
997 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
998 DEBUG(cerr << " by");
999 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1000 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001001 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1002 // u and v by d. Note that we have taken Knuth's advice here to use a power
1003 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1004 // 2 allows us to shift instead of multiply and it is easy to determine the
1005 // shift amount from the leading zeros. We are basically normalizing the u
1006 // and v so that its high bits are shifted to the top of v's range without
1007 // overflow. Note that this can require an extra word in u so that u must
1008 // be of length m+n+1.
1009 uint32_t shift = CountLeadingZeros_32(v[n-1]);
1010 uint32_t v_carry = 0;
1011 uint32_t u_carry = 0;
1012 if (shift) {
1013 for (uint32_t i = 0; i < m+n; ++i) {
1014 uint32_t u_tmp = u[i] >> (32 - shift);
1015 u[i] = (u[i] << shift) | u_carry;
1016 u_carry = u_tmp;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001017 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001018 for (uint32_t i = 0; i < n; ++i) {
1019 uint32_t v_tmp = v[i] >> (32 - shift);
1020 v[i] = (v[i] << shift) | v_carry;
1021 v_carry = v_tmp;
1022 }
1023 }
1024 u[m+n] = u_carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001025 DEBUG(cerr << "KnuthDiv: normal:");
1026 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1027 DEBUG(cerr << " by");
1028 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1029 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001030
1031 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1032 int j = m;
1033 do {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001034 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001035 // D3. [Calculate q'.].
1036 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1037 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1038 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1039 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1040 // on v[n-2] determines at high speed most of the cases in which the trial
1041 // value qp is one too large, and it eliminates all cases where qp is two
1042 // too large.
Reid Spencer92904632007-02-23 01:57:13 +00001043 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001044 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n');
Reid Spencer92904632007-02-23 01:57:13 +00001045 uint64_t qp = dividend / v[n-1];
1046 uint64_t rp = dividend % v[n-1];
Reid Spencer9c0696f2007-02-20 08:51:03 +00001047 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1048 qp--;
1049 rp += v[n-1];
Reid Spencer610fad82007-02-24 10:01:42 +00001050 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
Reid Spencer9d6c9192007-02-24 03:58:46 +00001051 qp--;
Reid Spencer92904632007-02-23 01:57:13 +00001052 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001053 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001054
Reid Spencer92904632007-02-23 01:57:13 +00001055 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1056 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1057 // consists of a simple multiplication by a one-place number, combined with
Reid Spencer610fad82007-02-24 10:01:42 +00001058 // a subtraction.
1059 bool isNegative = false;
Reid Spencer92904632007-02-23 01:57:13 +00001060 for (uint32_t i = 0; i < n; ++i) {
Reid Spencer610fad82007-02-24 10:01:42 +00001061 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001062 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
Reid Spencer610fad82007-02-24 10:01:42 +00001063 bool borrow = subtrahend > u_tmp;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001064 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp
Reid Spencer610fad82007-02-24 10:01:42 +00001065 << ", subtrahend == " << subtrahend
1066 << ", borrow = " << borrow << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001067
Reid Spencer610fad82007-02-24 10:01:42 +00001068 uint64_t result = u_tmp - subtrahend;
1069 uint32_t k = j + i;
1070 u[k++] = result & (b-1); // subtract low word
1071 u[k++] = result >> 32; // subtract high word
1072 while (borrow && k <= m+n) { // deal with borrow to the left
1073 borrow = u[k] == 0;
1074 u[k]--;
1075 k++;
1076 }
1077 isNegative |= borrow;
1078 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " <<
1079 u[j+i+1] << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001080 }
1081 DEBUG(cerr << "KnuthDiv: after subtraction:");
1082 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1083 DEBUG(cerr << '\n');
Reid Spencer610fad82007-02-24 10:01:42 +00001084 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1085 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1086 // true value plus b**(n+1), namely as the b's complement of
Reid Spencer92904632007-02-23 01:57:13 +00001087 // the true value, and a "borrow" to the left should be remembered.
1088 //
Reid Spencer610fad82007-02-24 10:01:42 +00001089 if (isNegative) {
1090 bool carry = true; // true because b's complement is "complement + 1"
1091 for (uint32_t i = 0; i <= m+n; ++i) {
1092 u[i] = ~u[i] + carry; // b's complement
1093 carry = carry && u[i] == 0;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001094 }
Reid Spencer92904632007-02-23 01:57:13 +00001095 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001096 DEBUG(cerr << "KnuthDiv: after complement:");
1097 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1098 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001099
1100 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1101 // negative, go to step D6; otherwise go on to step D7.
1102 q[j] = qp;
Reid Spencer610fad82007-02-24 10:01:42 +00001103 if (isNegative) {
Reid Spencer9c0696f2007-02-20 08:51:03 +00001104 // D6. [Add back]. The probability that this step is necessary is very
1105 // small, on the order of only 2/b. Make sure that test data accounts for
Reid Spencer92904632007-02-23 01:57:13 +00001106 // this possibility. Decrease q[j] by 1
1107 q[j]--;
1108 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1109 // A carry will occur to the left of u[j+n], and it should be ignored
1110 // since it cancels with the borrow that occurred in D4.
1111 bool carry = false;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001112 for (uint32_t i = 0; i < n; i++) {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001113 uint32_t limit = std::min(u[j+i],v[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001114 u[j+i] += v[i] + carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001115 carry = u[j+i] < limit || (carry && u[j+i] == limit);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001116 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001117 u[j+n] += carry;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001118 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001119 DEBUG(cerr << "KnuthDiv: after correction:");
1120 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]);
1121 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001122
Reid Spencer92904632007-02-23 01:57:13 +00001123 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1124 } while (--j >= 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001125
Reid Spencer9d6c9192007-02-24 03:58:46 +00001126 DEBUG(cerr << "KnuthDiv: quotient:");
1127 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]);
1128 DEBUG(cerr << '\n');
1129
Reid Spencer9c0696f2007-02-20 08:51:03 +00001130 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1131 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1132 // compute the remainder (urem uses this).
1133 if (r) {
1134 // The value d is expressed by the "shift" value above since we avoided
1135 // multiplication by d by using a shift left. So, all we have to do is
1136 // shift right here. In order to mak
Reid Spencer1050ec52007-02-24 20:38:01 +00001137 if (shift) {
1138 uint32_t carry = 0;
1139 DEBUG(cerr << "KnuthDiv: remainder:");
1140 for (int i = n-1; i >= 0; i--) {
1141 r[i] = (u[i] >> shift) | carry;
1142 carry = u[i] << (32 - shift);
1143 DEBUG(cerr << " " << r[i]);
1144 }
1145 } else {
1146 for (int i = n-1; i >= 0; i--) {
1147 r[i] = u[i];
1148 DEBUG(cerr << " " << r[i]);
1149 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001150 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001151 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001152 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001153 DEBUG(cerr << std::setbase(10) << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001154}
1155
1156// This function makes calling KnuthDiv a little more convenient. It uses
1157// APInt parameters instead of uint32_t* parameters. It can also divide APInt
1158// values of different widths.
1159void APInt::divide(const APInt LHS, uint32_t lhsWords,
1160 const APInt &RHS, uint32_t rhsWords,
1161 APInt *Quotient, APInt *Remainder)
1162{
1163 assert(lhsWords >= rhsWords && "Fractional result");
1164
1165 // First, compose the values into an array of 32-bit words instead of
1166 // 64-bit words. This is a necessity of both the "short division" algorithm
1167 // and the the Knuth "classical algorithm" which requires there to be native
1168 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1169 // can't use 64-bit operands here because we don't have native results of
1170 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't
1171 // work on large-endian machines.
1172 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8);
1173 uint32_t n = rhsWords * 2;
1174 uint32_t m = (lhsWords * 2) - n;
1175 // FIXME: allocate space on stack if m and n are sufficiently small.
1176 uint32_t *U = new uint32_t[m + n + 1];
1177 memset(U, 0, (m+n+1)*sizeof(uint32_t));
1178 for (unsigned i = 0; i < lhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001179 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001180 U[i * 2] = tmp & mask;
1181 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1182 }
1183 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1184
1185 uint32_t *V = new uint32_t[n];
1186 memset(V, 0, (n)*sizeof(uint32_t));
1187 for (unsigned i = 0; i < rhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001188 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001189 V[i * 2] = tmp & mask;
1190 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1191 }
1192
1193 // Set up the quotient and remainder
1194 uint32_t *Q = new uint32_t[m+n];
1195 memset(Q, 0, (m+n) * sizeof(uint32_t));
1196 uint32_t *R = 0;
1197 if (Remainder) {
1198 R = new uint32_t[n];
1199 memset(R, 0, n * sizeof(uint32_t));
1200 }
1201
1202 // Now, adjust m and n for the Knuth division. n is the number of words in
1203 // the divisor. m is the number of words by which the dividend exceeds the
1204 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1205 // contain any zero words or the Knuth algorithm fails.
1206 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1207 n--;
1208 m++;
1209 }
1210 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1211 m--;
1212
1213 // If we're left with only a single word for the divisor, Knuth doesn't work
1214 // so we implement the short division algorithm here. This is much simpler
1215 // and faster because we are certain that we can divide a 64-bit quantity
1216 // by a 32-bit quantity at hardware speed and short division is simply a
1217 // series of such operations. This is just like doing short division but we
1218 // are using base 2^32 instead of base 10.
1219 assert(n != 0 && "Divide by zero?");
1220 if (n == 1) {
1221 uint32_t divisor = V[0];
1222 uint32_t remainder = 0;
1223 for (int i = m+n-1; i >= 0; i--) {
1224 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1225 if (partial_dividend == 0) {
1226 Q[i] = 0;
1227 remainder = 0;
1228 } else if (partial_dividend < divisor) {
1229 Q[i] = 0;
1230 remainder = partial_dividend;
1231 } else if (partial_dividend == divisor) {
1232 Q[i] = 1;
1233 remainder = 0;
1234 } else {
1235 Q[i] = partial_dividend / divisor;
1236 remainder = partial_dividend - (Q[i] * divisor);
1237 }
1238 }
1239 if (R)
1240 R[0] = remainder;
1241 } else {
1242 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1243 // case n > 1.
1244 KnuthDiv(U, V, Q, R, m, n);
1245 }
1246
1247 // If the caller wants the quotient
1248 if (Quotient) {
1249 // Set up the Quotient value's memory.
1250 if (Quotient->BitWidth != LHS.BitWidth) {
1251 if (Quotient->isSingleWord())
1252 Quotient->VAL = 0;
1253 else
1254 delete Quotient->pVal;
1255 Quotient->BitWidth = LHS.BitWidth;
1256 if (!Quotient->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001257 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001258 } else
1259 Quotient->clear();
1260
1261 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1262 // order words.
1263 if (lhsWords == 1) {
1264 uint64_t tmp =
1265 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1266 if (Quotient->isSingleWord())
1267 Quotient->VAL = tmp;
1268 else
1269 Quotient->pVal[0] = tmp;
1270 } else {
1271 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1272 for (unsigned i = 0; i < lhsWords; ++i)
1273 Quotient->pVal[i] =
1274 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1275 }
1276 }
1277
1278 // If the caller wants the remainder
1279 if (Remainder) {
1280 // Set up the Remainder value's memory.
1281 if (Remainder->BitWidth != RHS.BitWidth) {
1282 if (Remainder->isSingleWord())
1283 Remainder->VAL = 0;
1284 else
1285 delete Remainder->pVal;
1286 Remainder->BitWidth = RHS.BitWidth;
1287 if (!Remainder->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001288 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001289 } else
1290 Remainder->clear();
1291
1292 // The remainder is in R. Reconstitute the remainder into Remainder's low
1293 // order words.
1294 if (rhsWords == 1) {
1295 uint64_t tmp =
1296 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1297 if (Remainder->isSingleWord())
1298 Remainder->VAL = tmp;
1299 else
1300 Remainder->pVal[0] = tmp;
1301 } else {
1302 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1303 for (unsigned i = 0; i < rhsWords; ++i)
1304 Remainder->pVal[i] =
1305 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1306 }
1307 }
1308
1309 // Clean up the memory we allocated.
1310 delete [] U;
1311 delete [] V;
1312 delete [] Q;
1313 delete [] R;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001314}
1315
Zhou Shengff4304f2007-02-09 07:48:24 +00001316/// Unsigned divide this APInt by APInt RHS.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001317/// @brief Unsigned division function for APInt.
Reid Spencere81d2da2007-02-16 22:36:51 +00001318APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001319 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001320
1321 // First, deal with the easy case
1322 if (isSingleWord()) {
1323 assert(RHS.VAL != 0 && "Divide by zero?");
1324 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001325 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001326
Reid Spencer71bd08f2007-02-17 02:07:07 +00001327 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001328 uint32_t rhsBits = RHS.getActiveBits();
1329 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001330 assert(rhsWords && "Divided by zero???");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001331 uint32_t lhsBits = this->getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +00001332 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001333
1334 // Deal with some degenerate cases
1335 if (!lhsWords)
Reid Spencere0cdd332007-02-21 08:21:52 +00001336 // 0 / X ===> 0
1337 return APInt(BitWidth, 0);
1338 else if (lhsWords < rhsWords || this->ult(RHS)) {
1339 // X / Y ===> 0, iff X < Y
1340 return APInt(BitWidth, 0);
1341 } else if (*this == RHS) {
1342 // X / X ===> 1
1343 return APInt(BitWidth, 1);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001344 } else if (lhsWords == 1 && rhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001345 // All high words are zero, just use native divide
Reid Spencere0cdd332007-02-21 08:21:52 +00001346 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001347 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001348
1349 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1350 APInt Quotient(1,0); // to hold result.
1351 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0);
1352 return Quotient;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001353}
1354
1355/// Unsigned remainder operation on APInt.
1356/// @brief Function for unsigned remainder operation.
Reid Spencere81d2da2007-02-16 22:36:51 +00001357APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001358 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001359 if (isSingleWord()) {
1360 assert(RHS.VAL != 0 && "Remainder by zero?");
1361 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001362 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001363
Reid Spencere0cdd332007-02-21 08:21:52 +00001364 // Get some facts about the LHS
1365 uint32_t lhsBits = getActiveBits();
1366 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001367
1368 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001369 uint32_t rhsBits = RHS.getActiveBits();
1370 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001371 assert(rhsWords && "Performing remainder operation by zero ???");
1372
Reid Spencer71bd08f2007-02-17 02:07:07 +00001373 // Check the degenerate cases
Reid Spencer9c0696f2007-02-20 08:51:03 +00001374 if (lhsWords == 0) {
Reid Spencere0cdd332007-02-21 08:21:52 +00001375 // 0 % Y ===> 0
1376 return APInt(BitWidth, 0);
1377 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1378 // X % Y ===> X, iff X < Y
1379 return *this;
1380 } else if (*this == RHS) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001381 // X % X == 0;
Reid Spencere0cdd332007-02-21 08:21:52 +00001382 return APInt(BitWidth, 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001383 } else if (lhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001384 // All high words are zero, just use native remainder
Reid Spencere0cdd332007-02-21 08:21:52 +00001385 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001386 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001387
1388 // We have to compute it the hard way. Invoke the Knute divide algorithm.
1389 APInt Remainder(1,0);
1390 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder);
1391 return Remainder;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001392}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001393
1394/// @brief Converts a char array into an integer.
Reid Spencer385f7542007-02-21 03:55:44 +00001395void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001396 uint8_t radix) {
Reid Spencer385f7542007-02-21 03:55:44 +00001397 // Check our assumptions here
Reid Spencer5e0a8512007-02-17 03:16:00 +00001398 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1399 "Radix should be 2, 8, 10, or 16!");
Reid Spencer385f7542007-02-21 03:55:44 +00001400 assert(str && "String is null?");
1401 assert(slen <= numbits || radix != 2 && "Insufficient bit width");
1402 assert(slen*3 <= numbits || radix != 8 && "Insufficient bit width");
1403 assert(slen*4 <= numbits || radix != 16 && "Insufficient bit width");
1404 assert((slen*64)/20 <= numbits || radix != 10 && "Insufficient bit width");
1405
1406 // Allocate memory
1407 if (!isSingleWord())
1408 pVal = getClearedMemory(getNumWords());
1409
1410 // Figure out if we can shift instead of multiply
1411 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1412
1413 // Set up an APInt for the digit to add outside the loop so we don't
1414 // constantly construct/destruct it.
1415 APInt apdigit(getBitWidth(), 0);
1416 APInt apradix(getBitWidth(), radix);
1417
1418 // Enter digit traversal loop
1419 for (unsigned i = 0; i < slen; i++) {
1420 // Get a digit
1421 uint32_t digit = 0;
1422 char cdigit = str[i];
1423 if (isdigit(cdigit))
1424 digit = cdigit - '0';
1425 else if (isxdigit(cdigit))
1426 if (cdigit >= 'a')
1427 digit = cdigit - 'a' + 10;
1428 else if (cdigit >= 'A')
1429 digit = cdigit - 'A' + 10;
1430 else
1431 assert(0 && "huh?");
1432 else
1433 assert(0 && "Invalid character in digit string");
1434
1435 // Shift or multiple the value by the radix
1436 if (shift)
1437 this->shl(shift);
1438 else
1439 *this *= apradix;
1440
1441 // Add in the digit we just interpreted
Reid Spencer5bce8542007-02-24 20:19:37 +00001442 if (apdigit.isSingleWord())
1443 apdigit.VAL = digit;
1444 else
1445 apdigit.pVal[0] = digit;
Reid Spencer385f7542007-02-21 03:55:44 +00001446 *this += apdigit;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001447 }
1448}
Reid Spencer9c0696f2007-02-20 08:51:03 +00001449
1450/// to_string - This function translates the APInt into a string.
1451std::string APInt::toString(uint8_t radix, bool wantSigned) const {
1452 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1453 "Radix should be 2, 8, 10, or 16!");
1454 static const char *digits[] = {
1455 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
1456 };
1457 std::string result;
1458 uint32_t bits_used = getActiveBits();
1459 if (isSingleWord()) {
1460 char buf[65];
1461 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
1462 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
1463 if (format) {
1464 if (wantSigned) {
1465 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
1466 (APINT_BITS_PER_WORD-BitWidth);
1467 sprintf(buf, format, sextVal);
1468 } else
1469 sprintf(buf, format, VAL);
1470 } else {
1471 memset(buf, 0, 65);
1472 uint64_t v = VAL;
1473 while (bits_used) {
1474 uint32_t bit = v & 1;
1475 bits_used--;
1476 buf[bits_used] = digits[bit][0];
1477 v >>=1;
1478 }
1479 }
1480 result = buf;
1481 return result;
1482 }
1483
1484 if (radix != 10) {
1485 uint64_t mask = radix - 1;
1486 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1);
1487 uint32_t nibbles = APINT_BITS_PER_WORD / shift;
1488 for (uint32_t i = 0; i < getNumWords(); ++i) {
1489 uint64_t value = pVal[i];
1490 for (uint32_t j = 0; j < nibbles; ++j) {
1491 result.insert(0, digits[ value & mask ]);
1492 value >>= shift;
1493 }
1494 }
1495 return result;
1496 }
1497
1498 APInt tmp(*this);
1499 APInt divisor(4, radix);
1500 APInt zero(tmp.getBitWidth(), 0);
1501 size_t insert_at = 0;
1502 if (wantSigned && tmp[BitWidth-1]) {
1503 // They want to print the signed version and it is a negative value
1504 // Flip the bits and add one to turn it into the equivalent positive
1505 // value and put a '-' in the result.
1506 tmp.flip();
1507 tmp++;
1508 result = "-";
1509 insert_at = 1;
1510 }
Reid Spencere549c492007-02-21 00:29:48 +00001511 if (tmp == APInt(tmp.getBitWidth(), 0))
Reid Spencer9c0696f2007-02-20 08:51:03 +00001512 result = "0";
1513 else while (tmp.ne(zero)) {
1514 APInt APdigit(1,0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001515 APInt tmp2(tmp.getBitWidth(), 0);
Reid Spencer385f7542007-02-21 03:55:44 +00001516 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
1517 &APdigit);
1518 uint32_t digit = APdigit.getValue();
1519 assert(digit < radix && "divide failed");
1520 result.insert(insert_at,digits[digit]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001521 tmp = tmp2;
1522 }
1523
1524 return result;
1525}
1526
Reid Spencer385f7542007-02-21 03:55:44 +00001527#ifndef NDEBUG
1528void APInt::dump() const
1529{
Reid Spencer610fad82007-02-24 10:01:42 +00001530 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16);
Reid Spencer385f7542007-02-21 03:55:44 +00001531 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +00001532 cerr << VAL;
Reid Spencer385f7542007-02-21 03:55:44 +00001533 else for (unsigned i = getNumWords(); i > 0; i--) {
Reid Spencer610fad82007-02-24 10:01:42 +00001534 cerr << pVal[i-1] << " ";
Reid Spencer385f7542007-02-21 03:55:44 +00001535 }
Reid Spencer610fad82007-02-24 10:01:42 +00001536 cerr << " (" << this->toString(10, false) << ")\n" << std::setbase(10);
Reid Spencer385f7542007-02-21 03:55:44 +00001537}
1538#endif