blob: 4142c6ec8bc4a495f9178617ad61871737d8d3c0 [file] [log] [blame]
Zhou Shengfd43dcf2007-02-06 03:00:16 +00001//===-- APInt.cpp - Implement APInt class ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer96d91372007-02-27 19:31:09 +00005// This file was developed by Sheng Zhou and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Zhou Shengfd43dcf2007-02-06 03:00:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer5d0d05c2007-02-25 19:32:03 +000010// This file implements a class to represent arbitrary precision integer
11// constant values and provide a variety of arithmetic operations on them.
Zhou Shengfd43dcf2007-02-06 03:00:16 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencer9d6c9192007-02-24 03:58:46 +000015#define DEBUG_TYPE "apint"
Zhou Shengfd43dcf2007-02-06 03:00:16 +000016#include "llvm/ADT/APInt.h"
17#include "llvm/DerivedTypes.h"
Reid Spencer9d6c9192007-02-24 03:58:46 +000018#include "llvm/Support/Debug.h"
Zhou Shengfd43dcf2007-02-06 03:00:16 +000019#include "llvm/Support/MathExtras.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000020#include <math.h>
Jeff Cohen09dfd8e2007-03-20 20:42:36 +000021#include <limits>
Zhou Shenga3832fd2007-02-07 06:14:53 +000022#include <cstring>
Zhou Shengfd43dcf2007-02-06 03:00:16 +000023#include <cstdlib>
Reid Spencer385f7542007-02-21 03:55:44 +000024#ifndef NDEBUG
Reid Spencer385f7542007-02-21 03:55:44 +000025#include <iomanip>
26#endif
27
Zhou Shengfd43dcf2007-02-06 03:00:16 +000028using namespace llvm;
29
Reid Spencer5d0d05c2007-02-25 19:32:03 +000030/// A utility function for allocating memory, checking for allocation failures,
31/// and ensuring the contents are zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000032inline static uint64_t* getClearedMemory(uint32_t numWords) {
33 uint64_t * result = new uint64_t[numWords];
34 assert(result && "APInt memory allocation fails!");
35 memset(result, 0, numWords * sizeof(uint64_t));
36 return result;
Zhou Sheng353815d2007-02-06 06:04:53 +000037}
38
Reid Spencer5d0d05c2007-02-25 19:32:03 +000039/// A utility function for allocating memory and checking for allocation
40/// failure. The content is not zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000041inline static uint64_t* getMemory(uint32_t numWords) {
42 uint64_t * result = new uint64_t[numWords];
43 assert(result && "APInt memory allocation fails!");
44 return result;
45}
46
Reid Spenceradf2a202007-03-19 21:19:02 +000047APInt::APInt(uint32_t numBits, uint64_t val, bool isSigned)
Reid Spencer3a341372007-03-19 20:37:47 +000048 : BitWidth(numBits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000049 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
50 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Reid Spencer5d0d05c2007-02-25 19:32:03 +000051 if (isSingleWord())
52 VAL = val;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000053 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000054 pVal = getClearedMemory(getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000055 pVal[0] = val;
Reid Spencer3a341372007-03-19 20:37:47 +000056 if (isSigned && int64_t(val) < 0)
57 for (unsigned i = 1; i < getNumWords(); ++i)
58 pVal[i] = -1ULL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000059 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +000060 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000061}
62
Reid Spenceraf0e9562007-02-18 18:38:44 +000063APInt::APInt(uint32_t numBits, uint32_t numWords, uint64_t bigVal[])
Reid Spencer385f7542007-02-21 03:55:44 +000064 : BitWidth(numBits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000065 assert(BitWidth >= IntegerType::MIN_INT_BITS && "bitwidth too small");
66 assert(BitWidth <= IntegerType::MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000067 assert(bigVal && "Null pointer detected!");
68 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +000069 VAL = bigVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +000070 else {
Reid Spencer610fad82007-02-24 10:01:42 +000071 // Get memory, cleared to 0
72 pVal = getClearedMemory(getNumWords());
73 // Calculate the number of words to copy
74 uint32_t words = std::min<uint32_t>(numWords, getNumWords());
75 // Copy the words from bigVal to pVal
76 memcpy(pVal, bigVal, words * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000077 }
Reid Spencer610fad82007-02-24 10:01:42 +000078 // Make sure unused high bits are cleared
79 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000080}
81
Reid Spenceraf0e9562007-02-18 18:38:44 +000082APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
Reid Spencer9c0696f2007-02-20 08:51:03 +000083 uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000084 : BitWidth(numbits), VAL(0) {
Reid Spencere81d2da2007-02-16 22:36:51 +000085 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000086}
87
Reid Spencer9c0696f2007-02-20 08:51:03 +000088APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000089 : BitWidth(numbits), VAL(0) {
Zhou Shenga3832fd2007-02-07 06:14:53 +000090 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +000091 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000092}
93
Reid Spencer54362ca2007-02-20 23:40:25 +000094APInt::APInt(const APInt& that)
Reid Spencer385f7542007-02-21 03:55:44 +000095 : BitWidth(that.BitWidth), VAL(0) {
Reid Spenceraf0e9562007-02-18 18:38:44 +000096 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +000097 VAL = that.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000098 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000099 pVal = getMemory(getNumWords());
Reid Spencer54362ca2007-02-20 23:40:25 +0000100 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000101 }
102}
103
104APInt::~APInt() {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000105 if (!isSingleWord() && pVal)
Reid Spencer9ac44112007-02-26 23:38:21 +0000106 delete [] pVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000107}
108
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000109APInt& APInt::operator=(const APInt& RHS) {
Reid Spencer9ac44112007-02-26 23:38:21 +0000110 // Don't do anything for X = X
111 if (this == &RHS)
112 return *this;
113
114 // If the bitwidths are the same, we can avoid mucking with memory
115 if (BitWidth == RHS.getBitWidth()) {
116 if (isSingleWord())
117 VAL = RHS.VAL;
118 else
119 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
120 return *this;
121 }
122
123 if (isSingleWord())
124 if (RHS.isSingleWord())
125 VAL = RHS.VAL;
126 else {
127 VAL = 0;
128 pVal = getMemory(RHS.getNumWords());
129 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
130 }
131 else if (getNumWords() == RHS.getNumWords())
132 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
133 else if (RHS.isSingleWord()) {
134 delete [] pVal;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000135 VAL = RHS.VAL;
Reid Spencer9ac44112007-02-26 23:38:21 +0000136 } else {
137 delete [] pVal;
138 pVal = getMemory(RHS.getNumWords());
139 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
140 }
141 BitWidth = RHS.BitWidth;
142 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000143}
144
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000145APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000146 if (isSingleWord())
147 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000148 else {
149 pVal[0] = RHS;
Reid Spencera58f0582007-02-18 20:09:41 +0000150 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000151 }
Reid Spencer9ac44112007-02-26 23:38:21 +0000152 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000153}
154
Reid Spenceraf0e9562007-02-18 18:38:44 +0000155/// add_1 - This function adds a single "digit" integer, y, to the multiple
156/// "digit" integer array, x[]. x[] is modified to reflect the addition and
157/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000158/// @returns the carry of the addition.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000159static bool add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000160 for (uint32_t i = 0; i < len; ++i) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000161 dest[i] = y + x[i];
162 if (dest[i] < y)
Reid Spencer610fad82007-02-24 10:01:42 +0000163 y = 1; // Carry one to next digit.
Reid Spencerf2c521c2007-02-18 06:39:42 +0000164 else {
Reid Spencer610fad82007-02-24 10:01:42 +0000165 y = 0; // No need to carry so exit early
Reid Spencerf2c521c2007-02-18 06:39:42 +0000166 break;
167 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000168 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000169 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000170}
171
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000172/// @brief Prefix increment operator. Increments the APInt by one.
173APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000174 if (isSingleWord())
175 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000176 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000177 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000178 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000179}
180
Reid Spenceraf0e9562007-02-18 18:38:44 +0000181/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
182/// the multi-digit integer array, x[], propagating the borrowed 1 value until
183/// no further borrowing is neeeded or it runs out of "digits" in x. The result
184/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
185/// In other words, if y > x then this function returns 1, otherwise 0.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000186/// @returns the borrow out of the subtraction
187static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000188 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000189 uint64_t X = x[i];
Reid Spencerf2c521c2007-02-18 06:39:42 +0000190 x[i] -= y;
191 if (y > X)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000192 y = 1; // We have to "borrow 1" from next "digit"
Reid Spencer5e0a8512007-02-17 03:16:00 +0000193 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000194 y = 0; // No need to borrow
195 break; // Remaining digits are unchanged so exit early
Reid Spencer5e0a8512007-02-17 03:16:00 +0000196 }
197 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000198 return bool(y);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000199}
200
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201/// @brief Prefix decrement operator. Decrements the APInt by one.
202APInt& APInt::operator--() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000203 if (isSingleWord())
204 --VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000205 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000206 sub_1(pVal, getNumWords(), 1);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000207 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000208}
209
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000210/// add - This function adds the integer array x to the integer array Y and
211/// places the result in dest.
212/// @returns the carry out from the addition
213/// @brief General addition of 64-bit integer arrays
Reid Spencer9d6c9192007-02-24 03:58:46 +0000214static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
215 uint32_t len) {
216 bool carry = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000217 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer92904632007-02-23 01:57:13 +0000218 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
Reid Spencer54362ca2007-02-20 23:40:25 +0000219 dest[i] = x[i] + y[i] + carry;
Reid Spencer60c0a6a2007-02-21 05:44:56 +0000220 carry = dest[i] < limit || (carry && dest[i] == limit);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000221 }
222 return carry;
223}
224
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000225/// Adds the RHS APint to this APInt.
226/// @returns this, after addition of RHS.
227/// @brief Addition assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000228APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000229 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000230 if (isSingleWord())
231 VAL += RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000232 else {
Reid Spencer54362ca2007-02-20 23:40:25 +0000233 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000234 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000235 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000236}
237
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000238/// Subtracts the integer array y from the integer array x
239/// @returns returns the borrow out.
240/// @brief Generalized subtraction of 64-bit integer arrays.
Reid Spencer9d6c9192007-02-24 03:58:46 +0000241static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
242 uint32_t len) {
Reid Spencer385f7542007-02-21 03:55:44 +0000243 bool borrow = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000244 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer385f7542007-02-21 03:55:44 +0000245 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
246 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
247 dest[i] = x_tmp - y[i];
Reid Spencer5e0a8512007-02-17 03:16:00 +0000248 }
Reid Spencer54362ca2007-02-20 23:40:25 +0000249 return borrow;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000250}
251
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000252/// Subtracts the RHS APInt from this APInt
253/// @returns this, after subtraction
254/// @brief Subtraction assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000255APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000256 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000257 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +0000258 VAL -= RHS.VAL;
259 else
260 sub(pVal, pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000261 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000262}
263
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000264/// Multiplies an integer array, x by a a uint64_t integer and places the result
265/// into dest.
266/// @returns the carry out of the multiplication.
267/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
Reid Spencer610fad82007-02-24 10:01:42 +0000268static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
269 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
Reid Spencer5e0a8512007-02-17 03:16:00 +0000270 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000271 uint64_t carry = 0;
272
273 // For each digit of x.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000274 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000275 // Split x into high and low words
276 uint64_t lx = x[i] & 0xffffffffULL;
277 uint64_t hx = x[i] >> 32;
278 // hasCarry - A flag to indicate if there is a carry to the next digit.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000279 // hasCarry == 0, no carry
280 // hasCarry == 1, has carry
281 // hasCarry == 2, no carry and the calculation result == 0.
282 uint8_t hasCarry = 0;
283 dest[i] = carry + lx * ly;
284 // Determine if the add above introduces carry.
285 hasCarry = (dest[i] < carry) ? 1 : 0;
286 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
287 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
288 // (2^32 - 1) + 2^32 = 2^64.
289 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
290
291 carry += (lx * hy) & 0xffffffffULL;
292 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
293 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
294 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
295 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000296 return carry;
297}
298
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000299/// Multiplies integer array x by integer array y and stores the result into
300/// the integer array dest. Note that dest's size must be >= xlen + ylen.
301/// @brief Generalized multiplicate of integer arrays.
Reid Spencer610fad82007-02-24 10:01:42 +0000302static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[],
303 uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000304 dest[xlen] = mul_1(dest, x, xlen, y[0]);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000305 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000306 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
Reid Spencere0cdd332007-02-21 08:21:52 +0000307 uint64_t carry = 0, lx = 0, hx = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000308 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000309 lx = x[j] & 0xffffffffULL;
310 hx = x[j] >> 32;
311 // hasCarry - A flag to indicate if has carry.
312 // hasCarry == 0, no carry
313 // hasCarry == 1, has carry
314 // hasCarry == 2, no carry and the calculation result == 0.
315 uint8_t hasCarry = 0;
316 uint64_t resul = carry + lx * ly;
317 hasCarry = (resul < carry) ? 1 : 0;
318 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
319 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
320
321 carry += (lx * hy) & 0xffffffffULL;
322 resul = (carry << 32) | (resul & 0xffffffffULL);
323 dest[i+j] += resul;
324 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
325 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
326 ((lx * hy) >> 32) + hx * hy;
327 }
328 dest[i+xlen] = carry;
329 }
330}
331
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000332APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000333 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencere0cdd332007-02-21 08:21:52 +0000334 if (isSingleWord()) {
Reid Spencer61eb1802007-02-20 20:42:10 +0000335 VAL *= RHS.VAL;
Reid Spencere0cdd332007-02-21 08:21:52 +0000336 clearUnusedBits();
337 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000338 }
Reid Spencere0cdd332007-02-21 08:21:52 +0000339
340 // Get some bit facts about LHS and check for zero
341 uint32_t lhsBits = getActiveBits();
342 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
343 if (!lhsWords)
344 // 0 * X ===> 0
345 return *this;
346
347 // Get some bit facts about RHS and check for zero
348 uint32_t rhsBits = RHS.getActiveBits();
349 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
350 if (!rhsWords) {
351 // X * 0 ===> 0
352 clear();
353 return *this;
354 }
355
356 // Allocate space for the result
357 uint32_t destWords = rhsWords + lhsWords;
358 uint64_t *dest = getMemory(destWords);
359
360 // Perform the long multiply
361 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
362
363 // Copy result back into *this
364 clear();
365 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
366 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
367
368 // delete dest array and return
369 delete[] dest;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000370 return *this;
371}
372
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000373APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000374 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000375 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000376 VAL &= RHS.VAL;
377 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000378 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000379 uint32_t numWords = getNumWords();
380 for (uint32_t i = 0; i < numWords; ++i)
381 pVal[i] &= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000382 return *this;
383}
384
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000385APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000386 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000387 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000388 VAL |= RHS.VAL;
389 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000390 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000391 uint32_t numWords = getNumWords();
392 for (uint32_t i = 0; i < numWords; ++i)
393 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000394 return *this;
395}
396
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000397APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000398 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000399 if (isSingleWord()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000400 VAL ^= RHS.VAL;
Reid Spencer54362ca2007-02-20 23:40:25 +0000401 this->clearUnusedBits();
Reid Spencerf2c521c2007-02-18 06:39:42 +0000402 return *this;
403 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000404 uint32_t numWords = getNumWords();
405 for (uint32_t i = 0; i < numWords; ++i)
406 pVal[i] ^= RHS.pVal[i];
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000407 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000408}
409
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000410APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000411 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000412 if (isSingleWord())
413 return APInt(getBitWidth(), VAL & RHS.VAL);
414
Reid Spenceraf0e9562007-02-18 18:38:44 +0000415 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000416 uint64_t* val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000417 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000418 val[i] = pVal[i] & RHS.pVal[i];
419 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000420}
421
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000422APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000423 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000424 if (isSingleWord())
425 return APInt(getBitWidth(), VAL | RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000426
Reid Spenceraf0e9562007-02-18 18:38:44 +0000427 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000428 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000429 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000430 val[i] = pVal[i] | RHS.pVal[i];
431 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000432}
433
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000434APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000435 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000436 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000437 return APInt(BitWidth, VAL ^ RHS.VAL);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000438
Reid Spenceraf0e9562007-02-18 18:38:44 +0000439 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000440 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000441 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000442 val[i] = pVal[i] ^ RHS.pVal[i];
443
444 // 0^0==1 so clear the high bits in case they got set.
445 return APInt(val, getBitWidth()).clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000446}
447
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000448bool APInt::operator !() const {
449 if (isSingleWord())
450 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000451
452 for (uint32_t i = 0; i < getNumWords(); ++i)
453 if (pVal[i])
454 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000455 return true;
456}
457
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000458APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000459 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000460 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000461 return APInt(BitWidth, VAL * RHS.VAL);
Reid Spencer61eb1802007-02-20 20:42:10 +0000462 APInt Result(*this);
463 Result *= RHS;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000464 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000465}
466
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000467APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000468 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000469 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000470 return APInt(BitWidth, VAL + RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000471 APInt Result(BitWidth, 0);
472 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000473 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000474}
475
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000476APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000477 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000478 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000479 return APInt(BitWidth, VAL - RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000480 APInt Result(BitWidth, 0);
481 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000482 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000483}
484
Reid Spenceraf0e9562007-02-18 18:38:44 +0000485bool APInt::operator[](uint32_t bitPosition) const {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000486 return (maskBit(bitPosition) &
487 (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000488}
489
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000490bool APInt::operator==(const APInt& RHS) const {
Reid Spencer9ac44112007-02-26 23:38:21 +0000491 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
Reid Spencer54362ca2007-02-20 23:40:25 +0000492 if (isSingleWord())
493 return VAL == RHS.VAL;
494
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000495 // Get some facts about the number of bits used in the two operands.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000496 uint32_t n1 = getActiveBits();
497 uint32_t n2 = RHS.getActiveBits();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000498
499 // If the number of bits isn't the same, they aren't equal
Reid Spencer54362ca2007-02-20 23:40:25 +0000500 if (n1 != n2)
501 return false;
502
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000503 // If the number of bits fits in a word, we only need to compare the low word.
Reid Spencer54362ca2007-02-20 23:40:25 +0000504 if (n1 <= APINT_BITS_PER_WORD)
505 return pVal[0] == RHS.pVal[0];
506
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000507 // Otherwise, compare everything
Reid Spencer54362ca2007-02-20 23:40:25 +0000508 for (int i = whichWord(n1 - 1); i >= 0; --i)
509 if (pVal[i] != RHS.pVal[i])
510 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000511 return true;
512}
513
Zhou Shenga3832fd2007-02-07 06:14:53 +0000514bool APInt::operator==(uint64_t Val) const {
515 if (isSingleWord())
516 return VAL == Val;
Reid Spencer54362ca2007-02-20 23:40:25 +0000517
518 uint32_t n = getActiveBits();
519 if (n <= APINT_BITS_PER_WORD)
520 return pVal[0] == Val;
521 else
522 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000523}
524
Reid Spencere81d2da2007-02-16 22:36:51 +0000525bool APInt::ult(const APInt& RHS) const {
526 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
527 if (isSingleWord())
528 return VAL < RHS.VAL;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000529
530 // Get active bit length of both operands
531 uint32_t n1 = getActiveBits();
532 uint32_t n2 = RHS.getActiveBits();
533
534 // If magnitude of LHS is less than RHS, return true.
535 if (n1 < n2)
536 return true;
537
538 // If magnitude of RHS is greather than LHS, return false.
539 if (n2 < n1)
540 return false;
541
542 // If they bot fit in a word, just compare the low order word
543 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
544 return pVal[0] < RHS.pVal[0];
545
546 // Otherwise, compare all words
Reid Spencer1fa111e2007-02-27 18:23:40 +0000547 uint32_t topWord = whichWord(std::max(n1,n2)-1);
548 for (int i = topWord; i >= 0; --i) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000549 if (pVal[i] > RHS.pVal[i])
Reid Spencere81d2da2007-02-16 22:36:51 +0000550 return false;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000551 if (pVal[i] < RHS.pVal[i])
552 return true;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000553 }
554 return false;
555}
556
Reid Spencere81d2da2007-02-16 22:36:51 +0000557bool APInt::slt(const APInt& RHS) const {
558 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
Reid Spencera58f0582007-02-18 20:09:41 +0000559 if (isSingleWord()) {
560 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
561 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
562 return lhsSext < rhsSext;
Reid Spencere81d2da2007-02-16 22:36:51 +0000563 }
Reid Spencera58f0582007-02-18 20:09:41 +0000564
565 APInt lhs(*this);
Reid Spencer1fa111e2007-02-27 18:23:40 +0000566 APInt rhs(RHS);
567 bool lhsNeg = isNegative();
568 bool rhsNeg = rhs.isNegative();
569 if (lhsNeg) {
570 // Sign bit is set so perform two's complement to make it positive
Reid Spencera58f0582007-02-18 20:09:41 +0000571 lhs.flip();
572 lhs++;
573 }
Reid Spencer1fa111e2007-02-27 18:23:40 +0000574 if (rhsNeg) {
575 // Sign bit is set so perform two's complement to make it positive
Reid Spencera58f0582007-02-18 20:09:41 +0000576 rhs.flip();
577 rhs++;
578 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000579
580 // Now we have unsigned values to compare so do the comparison if necessary
581 // based on the negativeness of the values.
Reid Spencer1fa111e2007-02-27 18:23:40 +0000582 if (lhsNeg)
583 if (rhsNeg)
584 return lhs.ugt(rhs);
Reid Spencera58f0582007-02-18 20:09:41 +0000585 else
586 return true;
Reid Spencer1fa111e2007-02-27 18:23:40 +0000587 else if (rhsNeg)
Reid Spencera58f0582007-02-18 20:09:41 +0000588 return false;
589 else
590 return lhs.ult(rhs);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000591}
592
Reid Spenceraf0e9562007-02-18 18:38:44 +0000593APInt& APInt::set(uint32_t bitPosition) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000594 if (isSingleWord())
595 VAL |= maskBit(bitPosition);
596 else
597 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000598 return *this;
599}
600
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000601APInt& APInt::set() {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000602 if (isSingleWord()) {
603 VAL = -1ULL;
604 return clearUnusedBits();
Zhou Shengb04973e2007-02-15 06:36:31 +0000605 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000606
607 // Set all the bits in all the words.
Zhou Sheng6dbe2332007-03-21 04:34:37 +0000608 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000609 pVal[i] = -1ULL;
610 // Clear the unused ones
611 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000612}
613
614/// Set the given bit to 0 whose position is given as "bitPosition".
615/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000616APInt& APInt::clear(uint32_t bitPosition) {
617 if (isSingleWord())
618 VAL &= ~maskBit(bitPosition);
619 else
620 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000621 return *this;
622}
623
624/// @brief Set every bit to 0.
625APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000626 if (isSingleWord())
627 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000628 else
Reid Spencera58f0582007-02-18 20:09:41 +0000629 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000630 return *this;
631}
632
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000633/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
634/// this APInt.
635APInt APInt::operator~() const {
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000636 APInt Result(*this);
637 Result.flip();
638 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000639}
640
641/// @brief Toggle every bit to its opposite value.
642APInt& APInt::flip() {
Reid Spencer9eec2412007-02-25 23:44:53 +0000643 if (isSingleWord()) {
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000644 VAL ^= -1ULL;
Reid Spencer9eec2412007-02-25 23:44:53 +0000645 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000646 }
Reid Spencer9eec2412007-02-25 23:44:53 +0000647 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000648 pVal[i] ^= -1ULL;
Reid Spencer9eec2412007-02-25 23:44:53 +0000649 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000650}
651
652/// Toggle a given bit to its opposite value whose position is given
653/// as "bitPosition".
654/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000655APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000656 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000657 if ((*this)[bitPosition]) clear(bitPosition);
658 else set(bitPosition);
659 return *this;
660}
661
Reid Spencer57ae4f52007-04-13 19:19:07 +0000662uint32_t APInt::getBitsNeeded(const char* str, uint32_t slen, uint8_t radix) {
663 assert(str != 0 && "Invalid value string");
664 assert(slen > 0 && "Invalid string length");
665
666 // Each computation below needs to know if its negative
667 uint32_t isNegative = str[0] == '-';
668 if (isNegative) {
669 slen--;
670 str++;
671 }
672 // For radixes of power-of-two values, the bits required is accurately and
673 // easily computed
674 if (radix == 2)
675 return slen + isNegative;
676 if (radix == 8)
677 return slen * 3 + isNegative;
678 if (radix == 16)
679 return slen * 4 + isNegative;
680
681 // Otherwise it must be radix == 10, the hard case
682 assert(radix == 10 && "Invalid radix");
683
684 // This is grossly inefficient but accurate. We could probably do something
685 // with a computation of roughly slen*64/20 and then adjust by the value of
686 // the first few digits. But, I'm not sure how accurate that could be.
687
688 // Compute a sufficient number of bits that is always large enough but might
689 // be too large. This avoids the assertion in the constructor.
690 uint32_t sufficient = slen*64/18;
691
692 // Convert to the actual binary value.
693 APInt tmp(sufficient, str, slen, radix);
694
695 // Compute how many bits are required.
Reid Spencer0468ab32007-04-14 00:00:10 +0000696 return isNegative + tmp.logBase2() + 1;
Reid Spencer57ae4f52007-04-13 19:19:07 +0000697}
698
Reid Spencer794f4722007-02-26 21:02:27 +0000699uint64_t APInt::getHashValue() const {
Reid Spencer9ac44112007-02-26 23:38:21 +0000700 // Put the bit width into the low order bits.
701 uint64_t hash = BitWidth;
Reid Spencer794f4722007-02-26 21:02:27 +0000702
703 // Add the sum of the words to the hash.
704 if (isSingleWord())
Reid Spencer9ac44112007-02-26 23:38:21 +0000705 hash += VAL << 6; // clear separation of up to 64 bits
Reid Spencer794f4722007-02-26 21:02:27 +0000706 else
707 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer9ac44112007-02-26 23:38:21 +0000708 hash += pVal[i] << 6; // clear sepration of up to 64 bits
Reid Spencer794f4722007-02-26 21:02:27 +0000709 return hash;
710}
711
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000712/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000713APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000714 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000715}
716
717/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000718APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000719 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
720 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000721}
722
Reid Spencere81d2da2007-02-16 22:36:51 +0000723bool APInt::isPowerOf2() const {
724 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
725}
726
Reid Spenceraf0e9562007-02-18 18:38:44 +0000727uint32_t APInt::countLeadingZeros() const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000728 uint32_t Count = 0;
Reid Spencere549c492007-02-21 00:29:48 +0000729 if (isSingleWord())
730 Count = CountLeadingZeros_64(VAL);
731 else {
732 for (uint32_t i = getNumWords(); i > 0u; --i) {
733 if (pVal[i-1] == 0)
734 Count += APINT_BITS_PER_WORD;
735 else {
736 Count += CountLeadingZeros_64(pVal[i-1]);
737 break;
738 }
739 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740 }
Reid Spencerab2b2c82007-02-22 00:22:00 +0000741 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD;
742 if (remainder)
743 Count -= APINT_BITS_PER_WORD - remainder;
744 return Count;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000745}
746
Reid Spencer681dcd12007-02-27 21:59:26 +0000747static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) {
748 uint32_t Count = 0;
749 if (skip)
750 V <<= skip;
751 while (V && (V & (1ULL << 63))) {
752 Count++;
753 V <<= 1;
754 }
755 return Count;
756}
757
758uint32_t APInt::countLeadingOnes() const {
759 if (isSingleWord())
760 return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth);
761
762 uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD;
763 uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits);
764 int i = getNumWords() - 1;
765 uint32_t Count = countLeadingOnes_64(pVal[i], shift);
766 if (Count == highWordBits) {
767 for (i--; i >= 0; --i) {
768 if (pVal[i] == -1ULL)
769 Count += APINT_BITS_PER_WORD;
770 else {
771 Count += countLeadingOnes_64(pVal[i], 0);
772 break;
773 }
774 }
775 }
776 return Count;
777}
778
Reid Spenceraf0e9562007-02-18 18:38:44 +0000779uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000780 if (isSingleWord())
Reid Spencer443b5702007-02-18 00:44:22 +0000781 return CountTrailingZeros_64(VAL);
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000782 uint32_t Count = 0;
783 uint32_t i = 0;
784 for (; i < getNumWords() && pVal[i] == 0; ++i)
785 Count += APINT_BITS_PER_WORD;
786 if (i < getNumWords())
787 Count += CountTrailingZeros_64(pVal[i]);
788 return Count;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000789}
790
Reid Spenceraf0e9562007-02-18 18:38:44 +0000791uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000792 if (isSingleWord())
793 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000794 uint32_t Count = 0;
795 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000796 Count += CountPopulation_64(pVal[i]);
797 return Count;
798}
799
Reid Spencere81d2da2007-02-16 22:36:51 +0000800APInt APInt::byteSwap() const {
801 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
802 if (BitWidth == 16)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000803 return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
Reid Spencere81d2da2007-02-16 22:36:51 +0000804 else if (BitWidth == 32)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000805 return APInt(BitWidth, ByteSwap_32(uint32_t(VAL)));
Reid Spencere81d2da2007-02-16 22:36:51 +0000806 else if (BitWidth == 48) {
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000807 uint32_t Tmp1 = uint32_t(VAL >> 16);
Zhou Shengb04973e2007-02-15 06:36:31 +0000808 Tmp1 = ByteSwap_32(Tmp1);
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000809 uint16_t Tmp2 = uint16_t(VAL);
Zhou Shengb04973e2007-02-15 06:36:31 +0000810 Tmp2 = ByteSwap_16(Tmp2);
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000811 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000812 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000813 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000814 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000815 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000816 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000817 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000818 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000819 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
820 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000821 }
822 return Result;
823 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000824}
825
Zhou Sheng0b706b12007-02-08 14:35:19 +0000826APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
827 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000828 APInt A = API1, B = API2;
829 while (!!B) {
830 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000831 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000832 A = T;
833 }
834 return A;
835}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000836
Reid Spencer1fa111e2007-02-27 18:23:40 +0000837APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000838 union {
839 double D;
840 uint64_t I;
841 } T;
842 T.D = Double;
Reid Spencer30f44f32007-02-27 01:28:10 +0000843
844 // Get the sign bit from the highest order bit
Zhou Shengd93f00c2007-02-12 20:02:55 +0000845 bool isNeg = T.I >> 63;
Reid Spencer30f44f32007-02-27 01:28:10 +0000846
847 // Get the 11-bit exponent and adjust for the 1023 bit bias
Zhou Shengd93f00c2007-02-12 20:02:55 +0000848 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
Reid Spencer30f44f32007-02-27 01:28:10 +0000849
850 // If the exponent is negative, the value is < 0 so just return 0.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000851 if (exp < 0)
Reid Spencerff605762007-02-28 01:30:08 +0000852 return APInt(width, 0u);
Reid Spencer30f44f32007-02-27 01:28:10 +0000853
854 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
855 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
856
857 // If the exponent doesn't shift all bits out of the mantissa
Zhou Shengd93f00c2007-02-12 20:02:55 +0000858 if (exp < 52)
Reid Spencer1fa111e2007-02-27 18:23:40 +0000859 return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
860 APInt(width, mantissa >> (52 - exp));
861
862 // If the client didn't provide enough bits for us to shift the mantissa into
863 // then the result is undefined, just return 0
864 if (width <= exp - 52)
865 return APInt(width, 0);
Reid Spencer30f44f32007-02-27 01:28:10 +0000866
867 // Otherwise, we have to shift the mantissa bits up to the right location
Reid Spencer1fa111e2007-02-27 18:23:40 +0000868 APInt Tmp(width, mantissa);
Reid Spencere81d2da2007-02-16 22:36:51 +0000869 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000870 return isNeg ? -Tmp : Tmp;
871}
872
Reid Spencerdb3faa62007-02-13 22:41:58 +0000873/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000874/// The layout for double is as following (IEEE Standard 754):
875/// --------------------------------------
876/// | Sign Exponent Fraction Bias |
877/// |-------------------------------------- |
878/// | 1[63] 11[62-52] 52[51-00] 1023 |
879/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000880double APInt::roundToDouble(bool isSigned) const {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000881
882 // Handle the simple case where the value is contained in one uint64_t.
Reid Spencera58f0582007-02-18 20:09:41 +0000883 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
884 if (isSigned) {
885 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
886 return double(sext);
887 } else
888 return double(VAL);
889 }
890
Reid Spencer9c0696f2007-02-20 08:51:03 +0000891 // Determine if the value is negative.
Reid Spencere81d2da2007-02-16 22:36:51 +0000892 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000893
894 // Construct the absolute value if we're negative.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000895 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spencer9c0696f2007-02-20 08:51:03 +0000896
897 // Figure out how many bits we're using.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000898 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000899
Reid Spencer9c0696f2007-02-20 08:51:03 +0000900 // The exponent (without bias normalization) is just the number of bits
901 // we are using. Note that the sign bit is gone since we constructed the
902 // absolute value.
903 uint64_t exp = n;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000904
Reid Spencer9c0696f2007-02-20 08:51:03 +0000905 // Return infinity for exponent overflow
906 if (exp > 1023) {
907 if (!isSigned || !isNeg)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000908 return std::numeric_limits<double>::infinity();
Reid Spencer9c0696f2007-02-20 08:51:03 +0000909 else
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000910 return -std::numeric_limits<double>::infinity();
Reid Spencer9c0696f2007-02-20 08:51:03 +0000911 }
912 exp += 1023; // Increment for 1023 bias
913
914 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
915 // extract the high 52 bits from the correct words in pVal.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000916 uint64_t mantissa;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000917 unsigned hiWord = whichWord(n-1);
918 if (hiWord == 0) {
919 mantissa = Tmp.pVal[0];
920 if (n > 52)
921 mantissa >>= n - 52; // shift down, we want the top 52 bits.
922 } else {
923 assert(hiWord > 0 && "huh?");
924 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
925 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
926 mantissa = hibits | lobits;
927 }
928
Zhou Shengd93f00c2007-02-12 20:02:55 +0000929 // The leading bit of mantissa is implicit, so get rid of it.
Reid Spencer443b5702007-02-18 00:44:22 +0000930 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000931 union {
932 double D;
933 uint64_t I;
934 } T;
935 T.I = sign | (exp << 52) | mantissa;
936 return T.D;
937}
938
Reid Spencere81d2da2007-02-16 22:36:51 +0000939// Truncate to new width.
Reid Spencer94900772007-02-28 17:34:32 +0000940APInt &APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000941 assert(width < BitWidth && "Invalid APInt Truncate request");
Reid Spencer9eec2412007-02-25 23:44:53 +0000942 assert(width >= IntegerType::MIN_INT_BITS && "Can't truncate to 0 bits");
943 uint32_t wordsBefore = getNumWords();
944 BitWidth = width;
945 uint32_t wordsAfter = getNumWords();
946 if (wordsBefore != wordsAfter) {
947 if (wordsAfter == 1) {
948 uint64_t *tmp = pVal;
949 VAL = pVal[0];
Reid Spencer9ac44112007-02-26 23:38:21 +0000950 delete [] tmp;
Reid Spencer9eec2412007-02-25 23:44:53 +0000951 } else {
952 uint64_t *newVal = getClearedMemory(wordsAfter);
953 for (uint32_t i = 0; i < wordsAfter; ++i)
954 newVal[i] = pVal[i];
Reid Spencer9ac44112007-02-26 23:38:21 +0000955 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +0000956 pVal = newVal;
957 }
958 }
Reid Spencer94900772007-02-28 17:34:32 +0000959 return clearUnusedBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000960}
961
962// Sign extend to a new width.
Reid Spencer94900772007-02-28 17:34:32 +0000963APInt &APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000964 assert(width > BitWidth && "Invalid APInt SignExtend request");
Reid Spencer9eec2412007-02-25 23:44:53 +0000965 assert(width <= IntegerType::MAX_INT_BITS && "Too many bits");
Reid Spencer9eec2412007-02-25 23:44:53 +0000966 // If the sign bit isn't set, this is the same as zext.
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000967 if (!isNegative()) {
Reid Spencer9eec2412007-02-25 23:44:53 +0000968 zext(width);
Reid Spencer94900772007-02-28 17:34:32 +0000969 return *this;
Reid Spencer9eec2412007-02-25 23:44:53 +0000970 }
971
972 // The sign bit is set. First, get some facts
973 uint32_t wordsBefore = getNumWords();
974 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
975 BitWidth = width;
976 uint32_t wordsAfter = getNumWords();
977
978 // Mask the high order word appropriately
979 if (wordsBefore == wordsAfter) {
980 uint32_t newWordBits = width % APINT_BITS_PER_WORD;
981 // The extension is contained to the wordsBefore-1th word.
Reid Spencer36184ed2007-03-02 01:19:42 +0000982 uint64_t mask = ~0ULL;
983 if (newWordBits)
984 mask >>= APINT_BITS_PER_WORD - newWordBits;
985 mask <<= wordBits;
Reid Spencer9eec2412007-02-25 23:44:53 +0000986 if (wordsBefore == 1)
987 VAL |= mask;
988 else
989 pVal[wordsBefore-1] |= mask;
Reid Spencer295e40a2007-03-01 23:30:25 +0000990 return clearUnusedBits();
Reid Spencer9eec2412007-02-25 23:44:53 +0000991 }
992
Reid Spencerf30b1882007-02-25 23:54:00 +0000993 uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits;
Reid Spencer9eec2412007-02-25 23:44:53 +0000994 uint64_t *newVal = getMemory(wordsAfter);
995 if (wordsBefore == 1)
996 newVal[0] = VAL | mask;
997 else {
998 for (uint32_t i = 0; i < wordsBefore; ++i)
999 newVal[i] = pVal[i];
1000 newVal[wordsBefore-1] |= mask;
1001 }
1002 for (uint32_t i = wordsBefore; i < wordsAfter; i++)
1003 newVal[i] = -1ULL;
1004 if (wordsBefore != 1)
Reid Spencer9ac44112007-02-26 23:38:21 +00001005 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +00001006 pVal = newVal;
Reid Spencer94900772007-02-28 17:34:32 +00001007 return clearUnusedBits();
Reid Spencere81d2da2007-02-16 22:36:51 +00001008}
1009
1010// Zero extend to a new width.
Reid Spencer94900772007-02-28 17:34:32 +00001011APInt &APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +00001012 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
Reid Spencer9eec2412007-02-25 23:44:53 +00001013 assert(width <= IntegerType::MAX_INT_BITS && "Too many bits");
1014 uint32_t wordsBefore = getNumWords();
1015 BitWidth = width;
1016 uint32_t wordsAfter = getNumWords();
1017 if (wordsBefore != wordsAfter) {
1018 uint64_t *newVal = getClearedMemory(wordsAfter);
1019 if (wordsBefore == 1)
1020 newVal[0] = VAL;
1021 else
1022 for (uint32_t i = 0; i < wordsBefore; ++i)
1023 newVal[i] = pVal[i];
1024 if (wordsBefore != 1)
Reid Spencer9ac44112007-02-26 23:38:21 +00001025 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +00001026 pVal = newVal;
1027 }
Reid Spencer94900772007-02-28 17:34:32 +00001028 return *this;
Reid Spencere81d2da2007-02-16 22:36:51 +00001029}
1030
Reid Spencer68e23002007-03-01 17:15:32 +00001031APInt &APInt::zextOrTrunc(uint32_t width) {
1032 if (BitWidth < width)
1033 return zext(width);
1034 if (BitWidth > width)
1035 return trunc(width);
1036 return *this;
1037}
1038
1039APInt &APInt::sextOrTrunc(uint32_t width) {
1040 if (BitWidth < width)
1041 return sext(width);
1042 if (BitWidth > width)
1043 return trunc(width);
1044 return *this;
1045}
1046
Zhou Shengff4304f2007-02-09 07:48:24 +00001047/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001048/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001049APInt APInt::ashr(uint32_t shiftAmt) const {
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001050 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Reid Spencer46f9c942007-03-02 22:39:11 +00001051 // Handle a degenerate case
1052 if (shiftAmt == 0)
1053 return *this;
1054
1055 // Handle single word shifts with built-in ashr
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001056 if (isSingleWord()) {
1057 if (shiftAmt == BitWidth)
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001058 return APInt(BitWidth, 0); // undefined
1059 else {
1060 uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth;
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001061 return APInt(BitWidth,
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001062 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1063 }
Zhou Sheng0b706b12007-02-08 14:35:19 +00001064 }
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001065
Reid Spencer46f9c942007-03-02 22:39:11 +00001066 // If all the bits were shifted out, the result is, technically, undefined.
1067 // We return -1 if it was negative, 0 otherwise. We check this early to avoid
1068 // issues in the algorithm below.
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001069 if (shiftAmt == BitWidth) {
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001070 if (isNegative())
1071 return APInt(BitWidth, -1ULL);
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001072 else
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001073 return APInt(BitWidth, 0);
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001074 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001075
1076 // Create some space for the result.
1077 uint64_t * val = new uint64_t[getNumWords()];
1078
Reid Spencer46f9c942007-03-02 22:39:11 +00001079 // Compute some values needed by the following shift algorithms
1080 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
1081 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
1082 uint32_t breakWord = getNumWords() - 1 - offset; // last word affected
1083 uint32_t bitsInWord = whichBit(BitWidth); // how many bits in last word?
1084 if (bitsInWord == 0)
1085 bitsInWord = APINT_BITS_PER_WORD;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001086
1087 // If we are shifting whole words, just move whole words
1088 if (wordShift == 0) {
Reid Spencer46f9c942007-03-02 22:39:11 +00001089 // Move the words containing significant bits
1090 for (uint32_t i = 0; i <= breakWord; ++i)
1091 val[i] = pVal[i+offset]; // move whole word
1092
1093 // Adjust the top significant word for sign bit fill, if negative
1094 if (isNegative())
1095 if (bitsInWord < APINT_BITS_PER_WORD)
1096 val[breakWord] |= ~0ULL << bitsInWord; // set high bits
1097 } else {
1098 // Shift the low order words
1099 for (uint32_t i = 0; i < breakWord; ++i) {
1100 // This combines the shifted corresponding word with the low bits from
1101 // the next word (shifted into this word's high bits).
1102 val[i] = (pVal[i+offset] >> wordShift) |
1103 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1104 }
1105
1106 // Shift the break word. In this case there are no bits from the next word
1107 // to include in this word.
1108 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1109
1110 // Deal with sign extenstion in the break word, and possibly the word before
1111 // it.
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001112 if (isNegative()) {
Reid Spencer46f9c942007-03-02 22:39:11 +00001113 if (wordShift > bitsInWord) {
1114 if (breakWord > 0)
1115 val[breakWord-1] |=
1116 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1117 val[breakWord] |= ~0ULL;
1118 } else
1119 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001120 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001121 }
1122
Reid Spencer46f9c942007-03-02 22:39:11 +00001123 // Remaining words are 0 or -1, just assign them.
1124 uint64_t fillValue = (isNegative() ? -1ULL : 0);
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001125 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
Reid Spencer46f9c942007-03-02 22:39:11 +00001126 val[i] = fillValue;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001127 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001128}
1129
Zhou Shengff4304f2007-02-09 07:48:24 +00001130/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001131/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001132APInt APInt::lshr(uint32_t shiftAmt) const {
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001133 if (isSingleWord()) {
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001134 if (shiftAmt == BitWidth)
1135 return APInt(BitWidth, 0);
1136 else
1137 return APInt(BitWidth, this->VAL >> shiftAmt);
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001138 }
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001139
Reid Spencerba81c2b2007-02-26 01:19:48 +00001140 // If all the bits were shifted out, the result is 0. This avoids issues
1141 // with shifting by the size of the integer type, which produces undefined
1142 // results. We define these "undefined results" to always be 0.
1143 if (shiftAmt == BitWidth)
1144 return APInt(BitWidth, 0);
1145
1146 // Create some space for the result.
1147 uint64_t * val = new uint64_t[getNumWords()];
1148
1149 // If we are shifting less than a word, compute the shift with a simple carry
1150 if (shiftAmt < APINT_BITS_PER_WORD) {
1151 uint64_t carry = 0;
1152 for (int i = getNumWords()-1; i >= 0; --i) {
Reid Spenceraf8fb192007-03-01 05:39:56 +00001153 val[i] = (pVal[i] >> shiftAmt) | carry;
Reid Spencerba81c2b2007-02-26 01:19:48 +00001154 carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt);
1155 }
1156 return APInt(val, BitWidth).clearUnusedBits();
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001157 }
1158
Reid Spencerba81c2b2007-02-26 01:19:48 +00001159 // Compute some values needed by the remaining shift algorithms
1160 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1161 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1162
1163 // If we are shifting whole words, just move whole words
1164 if (wordShift == 0) {
1165 for (uint32_t i = 0; i < getNumWords() - offset; ++i)
1166 val[i] = pVal[i+offset];
1167 for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++)
1168 val[i] = 0;
1169 return APInt(val,BitWidth).clearUnusedBits();
1170 }
1171
1172 // Shift the low order words
1173 uint32_t breakWord = getNumWords() - offset -1;
1174 for (uint32_t i = 0; i < breakWord; ++i)
Reid Spenceraf8fb192007-03-01 05:39:56 +00001175 val[i] = (pVal[i+offset] >> wordShift) |
1176 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
Reid Spencerba81c2b2007-02-26 01:19:48 +00001177 // Shift the break word.
1178 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1179
1180 // Remaining words are 0
1181 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
1182 val[i] = 0;
1183 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001184}
1185
Zhou Shengff4304f2007-02-09 07:48:24 +00001186/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001187/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001188APInt APInt::shl(uint32_t shiftAmt) const {
Reid Spencer5bce8542007-02-24 20:19:37 +00001189 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Reid Spencer87553802007-02-25 00:56:44 +00001190 if (isSingleWord()) {
Reid Spencer5bce8542007-02-24 20:19:37 +00001191 if (shiftAmt == BitWidth)
Reid Spencer87553802007-02-25 00:56:44 +00001192 return APInt(BitWidth, 0); // avoid undefined shift results
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001193 return APInt(BitWidth, VAL << shiftAmt);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001194 }
Reid Spencer5bce8542007-02-24 20:19:37 +00001195
Reid Spencer87553802007-02-25 00:56:44 +00001196 // If all the bits were shifted out, the result is 0. This avoids issues
1197 // with shifting by the size of the integer type, which produces undefined
1198 // results. We define these "undefined results" to always be 0.
1199 if (shiftAmt == BitWidth)
1200 return APInt(BitWidth, 0);
1201
1202 // Create some space for the result.
1203 uint64_t * val = new uint64_t[getNumWords()];
1204
1205 // If we are shifting less than a word, do it the easy way
1206 if (shiftAmt < APINT_BITS_PER_WORD) {
1207 uint64_t carry = 0;
Reid Spencer87553802007-02-25 00:56:44 +00001208 for (uint32_t i = 0; i < getNumWords(); i++) {
1209 val[i] = pVal[i] << shiftAmt | carry;
1210 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1211 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001212 return APInt(val, BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +00001213 }
1214
Reid Spencer87553802007-02-25 00:56:44 +00001215 // Compute some values needed by the remaining shift algorithms
1216 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1217 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1218
1219 // If we are shifting whole words, just move whole words
1220 if (wordShift == 0) {
1221 for (uint32_t i = 0; i < offset; i++)
1222 val[i] = 0;
1223 for (uint32_t i = offset; i < getNumWords(); i++)
1224 val[i] = pVal[i-offset];
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001225 return APInt(val,BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +00001226 }
Reid Spencer87553802007-02-25 00:56:44 +00001227
1228 // Copy whole words from this to Result.
1229 uint32_t i = getNumWords() - 1;
1230 for (; i > offset; --i)
1231 val[i] = pVal[i-offset] << wordShift |
1232 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
Reid Spencer438d71e2007-02-25 01:08:58 +00001233 val[offset] = pVal[0] << wordShift;
Reid Spencer87553802007-02-25 00:56:44 +00001234 for (i = 0; i < offset; ++i)
1235 val[i] = 0;
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001236 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001237}
1238
Reid Spenceraf8fb192007-03-01 05:39:56 +00001239
1240// Square Root - this method computes and returns the square root of "this".
1241// Three mechanisms are used for computation. For small values (<= 5 bits),
1242// a table lookup is done. This gets some performance for common cases. For
1243// values using less than 52 bits, the value is converted to double and then
1244// the libc sqrt function is called. The result is rounded and then converted
1245// back to a uint64_t which is then used to construct the result. Finally,
1246// the Babylonian method for computing square roots is used.
1247APInt APInt::sqrt() const {
1248
1249 // Determine the magnitude of the value.
1250 uint32_t magnitude = getActiveBits();
1251
1252 // Use a fast table for some small values. This also gets rid of some
1253 // rounding errors in libc sqrt for small values.
1254 if (magnitude <= 5) {
Reid Spencer4e1e87f2007-03-01 17:47:31 +00001255 static const uint8_t results[32] = {
Reid Spencerb5ca2cd2007-03-01 06:23:32 +00001256 /* 0 */ 0,
1257 /* 1- 2 */ 1, 1,
1258 /* 3- 6 */ 2, 2, 2, 2,
1259 /* 7-12 */ 3, 3, 3, 3, 3, 3,
1260 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1261 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1262 /* 31 */ 6
1263 };
1264 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
Reid Spenceraf8fb192007-03-01 05:39:56 +00001265 }
1266
1267 // If the magnitude of the value fits in less than 52 bits (the precision of
1268 // an IEEE double precision floating point value), then we can use the
1269 // libc sqrt function which will probably use a hardware sqrt computation.
1270 // This should be faster than the algorithm below.
Jeff Cohenca5183d2007-03-05 00:00:42 +00001271 if (magnitude < 52) {
1272#ifdef _MSC_VER
1273 // Amazingly, VC++ doesn't have round().
1274 return APInt(BitWidth,
1275 uint64_t(::sqrt(double(isSingleWord()?VAL:pVal[0]))) + 0.5);
1276#else
Reid Spenceraf8fb192007-03-01 05:39:56 +00001277 return APInt(BitWidth,
1278 uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
Jeff Cohenca5183d2007-03-05 00:00:42 +00001279#endif
1280 }
Reid Spenceraf8fb192007-03-01 05:39:56 +00001281
1282 // Okay, all the short cuts are exhausted. We must compute it. The following
1283 // is a classical Babylonian method for computing the square root. This code
1284 // was adapted to APINt from a wikipedia article on such computations.
1285 // See http://www.wikipedia.org/ and go to the page named
1286 // Calculate_an_integer_square_root.
1287 uint32_t nbits = BitWidth, i = 4;
1288 APInt testy(BitWidth, 16);
1289 APInt x_old(BitWidth, 1);
1290 APInt x_new(BitWidth, 0);
1291 APInt two(BitWidth, 2);
1292
1293 // Select a good starting value using binary logarithms.
1294 for (;; i += 2, testy = testy.shl(2))
1295 if (i >= nbits || this->ule(testy)) {
1296 x_old = x_old.shl(i / 2);
1297 break;
1298 }
1299
1300 // Use the Babylonian method to arrive at the integer square root:
1301 for (;;) {
1302 x_new = (this->udiv(x_old) + x_old).udiv(two);
1303 if (x_old.ule(x_new))
1304 break;
1305 x_old = x_new;
1306 }
1307
1308 // Make sure we return the closest approximation
Reid Spencerf09aef72007-03-02 04:21:55 +00001309 // NOTE: The rounding calculation below is correct. It will produce an
1310 // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1311 // determined to be a rounding issue with pari/gp as it begins to use a
1312 // floating point representation after 192 bits. There are no discrepancies
1313 // between this algorithm and pari/gp for bit widths < 192 bits.
Reid Spenceraf8fb192007-03-01 05:39:56 +00001314 APInt square(x_old * x_old);
1315 APInt nextSquare((x_old + 1) * (x_old +1));
1316 if (this->ult(square))
1317 return x_old;
Reid Spencerf09aef72007-03-02 04:21:55 +00001318 else if (this->ule(nextSquare)) {
1319 APInt midpoint((nextSquare - square).udiv(two));
1320 APInt offset(*this - square);
1321 if (offset.ult(midpoint))
Reid Spenceraf8fb192007-03-01 05:39:56 +00001322 return x_old;
Reid Spencerf09aef72007-03-02 04:21:55 +00001323 else
1324 return x_old + 1;
1325 } else
Reid Spenceraf8fb192007-03-01 05:39:56 +00001326 assert(0 && "Error in APInt::sqrt computation");
1327 return x_old + 1;
1328}
1329
Reid Spencer9c0696f2007-02-20 08:51:03 +00001330/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1331/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1332/// variables here have the same names as in the algorithm. Comments explain
1333/// the algorithm and any deviation from it.
1334static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
1335 uint32_t m, uint32_t n) {
1336 assert(u && "Must provide dividend");
1337 assert(v && "Must provide divisor");
1338 assert(q && "Must provide quotient");
Reid Spencer9d6c9192007-02-24 03:58:46 +00001339 assert(u != v && u != q && v != q && "Must us different memory");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001340 assert(n>1 && "n must be > 1");
1341
1342 // Knuth uses the value b as the base of the number system. In our case b
1343 // is 2^31 so we just set it to -1u.
1344 uint64_t b = uint64_t(1) << 32;
1345
Reid Spencer9d6c9192007-02-24 03:58:46 +00001346 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n');
1347 DEBUG(cerr << "KnuthDiv: original:");
1348 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1349 DEBUG(cerr << " by");
1350 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1351 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001352 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1353 // u and v by d. Note that we have taken Knuth's advice here to use a power
1354 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1355 // 2 allows us to shift instead of multiply and it is easy to determine the
1356 // shift amount from the leading zeros. We are basically normalizing the u
1357 // and v so that its high bits are shifted to the top of v's range without
1358 // overflow. Note that this can require an extra word in u so that u must
1359 // be of length m+n+1.
1360 uint32_t shift = CountLeadingZeros_32(v[n-1]);
1361 uint32_t v_carry = 0;
1362 uint32_t u_carry = 0;
1363 if (shift) {
1364 for (uint32_t i = 0; i < m+n; ++i) {
1365 uint32_t u_tmp = u[i] >> (32 - shift);
1366 u[i] = (u[i] << shift) | u_carry;
1367 u_carry = u_tmp;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001368 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001369 for (uint32_t i = 0; i < n; ++i) {
1370 uint32_t v_tmp = v[i] >> (32 - shift);
1371 v[i] = (v[i] << shift) | v_carry;
1372 v_carry = v_tmp;
1373 }
1374 }
1375 u[m+n] = u_carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001376 DEBUG(cerr << "KnuthDiv: normal:");
1377 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1378 DEBUG(cerr << " by");
1379 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1380 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001381
1382 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1383 int j = m;
1384 do {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001385 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001386 // D3. [Calculate q'.].
1387 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1388 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1389 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1390 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1391 // on v[n-2] determines at high speed most of the cases in which the trial
1392 // value qp is one too large, and it eliminates all cases where qp is two
1393 // too large.
Reid Spencer92904632007-02-23 01:57:13 +00001394 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001395 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n');
Reid Spencer92904632007-02-23 01:57:13 +00001396 uint64_t qp = dividend / v[n-1];
1397 uint64_t rp = dividend % v[n-1];
Reid Spencer9c0696f2007-02-20 08:51:03 +00001398 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1399 qp--;
1400 rp += v[n-1];
Reid Spencer610fad82007-02-24 10:01:42 +00001401 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
Reid Spencer9d6c9192007-02-24 03:58:46 +00001402 qp--;
Reid Spencer92904632007-02-23 01:57:13 +00001403 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001404 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001405
Reid Spencer92904632007-02-23 01:57:13 +00001406 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1407 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1408 // consists of a simple multiplication by a one-place number, combined with
Reid Spencer610fad82007-02-24 10:01:42 +00001409 // a subtraction.
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001410 bool isNeg = false;
Reid Spencer92904632007-02-23 01:57:13 +00001411 for (uint32_t i = 0; i < n; ++i) {
Reid Spencer610fad82007-02-24 10:01:42 +00001412 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001413 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
Reid Spencer610fad82007-02-24 10:01:42 +00001414 bool borrow = subtrahend > u_tmp;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001415 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp
Reid Spencer610fad82007-02-24 10:01:42 +00001416 << ", subtrahend == " << subtrahend
1417 << ", borrow = " << borrow << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001418
Reid Spencer610fad82007-02-24 10:01:42 +00001419 uint64_t result = u_tmp - subtrahend;
1420 uint32_t k = j + i;
1421 u[k++] = result & (b-1); // subtract low word
1422 u[k++] = result >> 32; // subtract high word
1423 while (borrow && k <= m+n) { // deal with borrow to the left
1424 borrow = u[k] == 0;
1425 u[k]--;
1426 k++;
1427 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001428 isNeg |= borrow;
Reid Spencer610fad82007-02-24 10:01:42 +00001429 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " <<
1430 u[j+i+1] << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001431 }
1432 DEBUG(cerr << "KnuthDiv: after subtraction:");
1433 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1434 DEBUG(cerr << '\n');
Reid Spencer610fad82007-02-24 10:01:42 +00001435 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1436 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1437 // true value plus b**(n+1), namely as the b's complement of
Reid Spencer92904632007-02-23 01:57:13 +00001438 // the true value, and a "borrow" to the left should be remembered.
1439 //
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001440 if (isNeg) {
Reid Spencer610fad82007-02-24 10:01:42 +00001441 bool carry = true; // true because b's complement is "complement + 1"
1442 for (uint32_t i = 0; i <= m+n; ++i) {
1443 u[i] = ~u[i] + carry; // b's complement
1444 carry = carry && u[i] == 0;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001445 }
Reid Spencer92904632007-02-23 01:57:13 +00001446 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001447 DEBUG(cerr << "KnuthDiv: after complement:");
1448 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1449 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001450
1451 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1452 // negative, go to step D6; otherwise go on to step D7.
1453 q[j] = qp;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001454 if (isNeg) {
Reid Spencer9c0696f2007-02-20 08:51:03 +00001455 // D6. [Add back]. The probability that this step is necessary is very
1456 // small, on the order of only 2/b. Make sure that test data accounts for
Reid Spencer92904632007-02-23 01:57:13 +00001457 // this possibility. Decrease q[j] by 1
1458 q[j]--;
1459 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1460 // A carry will occur to the left of u[j+n], and it should be ignored
1461 // since it cancels with the borrow that occurred in D4.
1462 bool carry = false;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001463 for (uint32_t i = 0; i < n; i++) {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001464 uint32_t limit = std::min(u[j+i],v[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001465 u[j+i] += v[i] + carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001466 carry = u[j+i] < limit || (carry && u[j+i] == limit);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001467 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001468 u[j+n] += carry;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001469 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001470 DEBUG(cerr << "KnuthDiv: after correction:");
1471 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]);
1472 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001473
Reid Spencer92904632007-02-23 01:57:13 +00001474 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1475 } while (--j >= 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001476
Reid Spencer9d6c9192007-02-24 03:58:46 +00001477 DEBUG(cerr << "KnuthDiv: quotient:");
1478 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]);
1479 DEBUG(cerr << '\n');
1480
Reid Spencer9c0696f2007-02-20 08:51:03 +00001481 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1482 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1483 // compute the remainder (urem uses this).
1484 if (r) {
1485 // The value d is expressed by the "shift" value above since we avoided
1486 // multiplication by d by using a shift left. So, all we have to do is
1487 // shift right here. In order to mak
Reid Spencer1050ec52007-02-24 20:38:01 +00001488 if (shift) {
1489 uint32_t carry = 0;
1490 DEBUG(cerr << "KnuthDiv: remainder:");
1491 for (int i = n-1; i >= 0; i--) {
1492 r[i] = (u[i] >> shift) | carry;
1493 carry = u[i] << (32 - shift);
1494 DEBUG(cerr << " " << r[i]);
1495 }
1496 } else {
1497 for (int i = n-1; i >= 0; i--) {
1498 r[i] = u[i];
1499 DEBUG(cerr << " " << r[i]);
1500 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001501 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001502 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001503 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001504 DEBUG(cerr << std::setbase(10) << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001505}
1506
Reid Spencer9c0696f2007-02-20 08:51:03 +00001507void APInt::divide(const APInt LHS, uint32_t lhsWords,
1508 const APInt &RHS, uint32_t rhsWords,
1509 APInt *Quotient, APInt *Remainder)
1510{
1511 assert(lhsWords >= rhsWords && "Fractional result");
1512
1513 // First, compose the values into an array of 32-bit words instead of
1514 // 64-bit words. This is a necessity of both the "short division" algorithm
1515 // and the the Knuth "classical algorithm" which requires there to be native
1516 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1517 // can't use 64-bit operands here because we don't have native results of
1518 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't
1519 // work on large-endian machines.
1520 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8);
1521 uint32_t n = rhsWords * 2;
1522 uint32_t m = (lhsWords * 2) - n;
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001523
1524 // Allocate space for the temporary values we need either on the stack, if
1525 // it will fit, or on the heap if it won't.
1526 uint32_t SPACE[128];
1527 uint32_t *U = 0;
1528 uint32_t *V = 0;
1529 uint32_t *Q = 0;
1530 uint32_t *R = 0;
1531 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1532 U = &SPACE[0];
1533 V = &SPACE[m+n+1];
1534 Q = &SPACE[(m+n+1) + n];
1535 if (Remainder)
1536 R = &SPACE[(m+n+1) + n + (m+n)];
1537 } else {
1538 U = new uint32_t[m + n + 1];
1539 V = new uint32_t[n];
1540 Q = new uint32_t[m+n];
1541 if (Remainder)
1542 R = new uint32_t[n];
1543 }
1544
1545 // Initialize the dividend
Reid Spencer9c0696f2007-02-20 08:51:03 +00001546 memset(U, 0, (m+n+1)*sizeof(uint32_t));
1547 for (unsigned i = 0; i < lhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001548 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001549 U[i * 2] = tmp & mask;
1550 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1551 }
1552 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1553
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001554 // Initialize the divisor
Reid Spencer9c0696f2007-02-20 08:51:03 +00001555 memset(V, 0, (n)*sizeof(uint32_t));
1556 for (unsigned i = 0; i < rhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001557 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001558 V[i * 2] = tmp & mask;
1559 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1560 }
1561
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001562 // initialize the quotient and remainder
Reid Spencer9c0696f2007-02-20 08:51:03 +00001563 memset(Q, 0, (m+n) * sizeof(uint32_t));
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001564 if (Remainder)
Reid Spencer9c0696f2007-02-20 08:51:03 +00001565 memset(R, 0, n * sizeof(uint32_t));
Reid Spencer9c0696f2007-02-20 08:51:03 +00001566
1567 // Now, adjust m and n for the Knuth division. n is the number of words in
1568 // the divisor. m is the number of words by which the dividend exceeds the
1569 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1570 // contain any zero words or the Knuth algorithm fails.
1571 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1572 n--;
1573 m++;
1574 }
1575 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1576 m--;
1577
1578 // If we're left with only a single word for the divisor, Knuth doesn't work
1579 // so we implement the short division algorithm here. This is much simpler
1580 // and faster because we are certain that we can divide a 64-bit quantity
1581 // by a 32-bit quantity at hardware speed and short division is simply a
1582 // series of such operations. This is just like doing short division but we
1583 // are using base 2^32 instead of base 10.
1584 assert(n != 0 && "Divide by zero?");
1585 if (n == 1) {
1586 uint32_t divisor = V[0];
1587 uint32_t remainder = 0;
1588 for (int i = m+n-1; i >= 0; i--) {
1589 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1590 if (partial_dividend == 0) {
1591 Q[i] = 0;
1592 remainder = 0;
1593 } else if (partial_dividend < divisor) {
1594 Q[i] = 0;
1595 remainder = partial_dividend;
1596 } else if (partial_dividend == divisor) {
1597 Q[i] = 1;
1598 remainder = 0;
1599 } else {
1600 Q[i] = partial_dividend / divisor;
1601 remainder = partial_dividend - (Q[i] * divisor);
1602 }
1603 }
1604 if (R)
1605 R[0] = remainder;
1606 } else {
1607 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1608 // case n > 1.
1609 KnuthDiv(U, V, Q, R, m, n);
1610 }
1611
1612 // If the caller wants the quotient
1613 if (Quotient) {
1614 // Set up the Quotient value's memory.
1615 if (Quotient->BitWidth != LHS.BitWidth) {
1616 if (Quotient->isSingleWord())
1617 Quotient->VAL = 0;
1618 else
Reid Spencer9ac44112007-02-26 23:38:21 +00001619 delete [] Quotient->pVal;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001620 Quotient->BitWidth = LHS.BitWidth;
1621 if (!Quotient->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001622 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001623 } else
1624 Quotient->clear();
1625
1626 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1627 // order words.
1628 if (lhsWords == 1) {
1629 uint64_t tmp =
1630 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1631 if (Quotient->isSingleWord())
1632 Quotient->VAL = tmp;
1633 else
1634 Quotient->pVal[0] = tmp;
1635 } else {
1636 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1637 for (unsigned i = 0; i < lhsWords; ++i)
1638 Quotient->pVal[i] =
1639 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1640 }
1641 }
1642
1643 // If the caller wants the remainder
1644 if (Remainder) {
1645 // Set up the Remainder value's memory.
1646 if (Remainder->BitWidth != RHS.BitWidth) {
1647 if (Remainder->isSingleWord())
1648 Remainder->VAL = 0;
1649 else
Reid Spencer9ac44112007-02-26 23:38:21 +00001650 delete [] Remainder->pVal;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001651 Remainder->BitWidth = RHS.BitWidth;
1652 if (!Remainder->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001653 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001654 } else
1655 Remainder->clear();
1656
1657 // The remainder is in R. Reconstitute the remainder into Remainder's low
1658 // order words.
1659 if (rhsWords == 1) {
1660 uint64_t tmp =
1661 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1662 if (Remainder->isSingleWord())
1663 Remainder->VAL = tmp;
1664 else
1665 Remainder->pVal[0] = tmp;
1666 } else {
1667 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1668 for (unsigned i = 0; i < rhsWords; ++i)
1669 Remainder->pVal[i] =
1670 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1671 }
1672 }
1673
1674 // Clean up the memory we allocated.
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001675 if (U != &SPACE[0]) {
1676 delete [] U;
1677 delete [] V;
1678 delete [] Q;
1679 delete [] R;
1680 }
Reid Spencer5e0a8512007-02-17 03:16:00 +00001681}
1682
Reid Spencere81d2da2007-02-16 22:36:51 +00001683APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001684 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001685
1686 // First, deal with the easy case
1687 if (isSingleWord()) {
1688 assert(RHS.VAL != 0 && "Divide by zero?");
1689 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001690 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001691
Reid Spencer71bd08f2007-02-17 02:07:07 +00001692 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001693 uint32_t rhsBits = RHS.getActiveBits();
1694 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001695 assert(rhsWords && "Divided by zero???");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001696 uint32_t lhsBits = this->getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +00001697 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001698
1699 // Deal with some degenerate cases
1700 if (!lhsWords)
Reid Spencere0cdd332007-02-21 08:21:52 +00001701 // 0 / X ===> 0
1702 return APInt(BitWidth, 0);
1703 else if (lhsWords < rhsWords || this->ult(RHS)) {
1704 // X / Y ===> 0, iff X < Y
1705 return APInt(BitWidth, 0);
1706 } else if (*this == RHS) {
1707 // X / X ===> 1
1708 return APInt(BitWidth, 1);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001709 } else if (lhsWords == 1 && rhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001710 // All high words are zero, just use native divide
Reid Spencere0cdd332007-02-21 08:21:52 +00001711 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001712 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001713
1714 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1715 APInt Quotient(1,0); // to hold result.
1716 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0);
1717 return Quotient;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001718}
1719
Reid Spencere81d2da2007-02-16 22:36:51 +00001720APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001721 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001722 if (isSingleWord()) {
1723 assert(RHS.VAL != 0 && "Remainder by zero?");
1724 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001725 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001726
Reid Spencere0cdd332007-02-21 08:21:52 +00001727 // Get some facts about the LHS
1728 uint32_t lhsBits = getActiveBits();
1729 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001730
1731 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001732 uint32_t rhsBits = RHS.getActiveBits();
1733 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001734 assert(rhsWords && "Performing remainder operation by zero ???");
1735
Reid Spencer71bd08f2007-02-17 02:07:07 +00001736 // Check the degenerate cases
Reid Spencer9c0696f2007-02-20 08:51:03 +00001737 if (lhsWords == 0) {
Reid Spencere0cdd332007-02-21 08:21:52 +00001738 // 0 % Y ===> 0
1739 return APInt(BitWidth, 0);
1740 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1741 // X % Y ===> X, iff X < Y
1742 return *this;
1743 } else if (*this == RHS) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001744 // X % X == 0;
Reid Spencere0cdd332007-02-21 08:21:52 +00001745 return APInt(BitWidth, 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001746 } else if (lhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001747 // All high words are zero, just use native remainder
Reid Spencere0cdd332007-02-21 08:21:52 +00001748 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001749 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001750
1751 // We have to compute it the hard way. Invoke the Knute divide algorithm.
1752 APInt Remainder(1,0);
1753 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder);
1754 return Remainder;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001755}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001756
Reid Spencer385f7542007-02-21 03:55:44 +00001757void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001758 uint8_t radix) {
Reid Spencer385f7542007-02-21 03:55:44 +00001759 // Check our assumptions here
Reid Spencer5e0a8512007-02-17 03:16:00 +00001760 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1761 "Radix should be 2, 8, 10, or 16!");
Reid Spencer385f7542007-02-21 03:55:44 +00001762 assert(str && "String is null?");
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001763 bool isNeg = str[0] == '-';
1764 if (isNeg)
Reid Spencer9eec2412007-02-25 23:44:53 +00001765 str++, slen--;
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001766 assert((slen <= numbits || radix != 2) && "Insufficient bit width");
1767 assert((slen*3 <= numbits || radix != 8) && "Insufficient bit width");
1768 assert((slen*4 <= numbits || radix != 16) && "Insufficient bit width");
1769 assert(((slen*64)/22 <= numbits || radix != 10) && "Insufficient bit width");
Reid Spencer385f7542007-02-21 03:55:44 +00001770
1771 // Allocate memory
1772 if (!isSingleWord())
1773 pVal = getClearedMemory(getNumWords());
1774
1775 // Figure out if we can shift instead of multiply
1776 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1777
1778 // Set up an APInt for the digit to add outside the loop so we don't
1779 // constantly construct/destruct it.
1780 APInt apdigit(getBitWidth(), 0);
1781 APInt apradix(getBitWidth(), radix);
1782
1783 // Enter digit traversal loop
1784 for (unsigned i = 0; i < slen; i++) {
1785 // Get a digit
1786 uint32_t digit = 0;
1787 char cdigit = str[i];
1788 if (isdigit(cdigit))
1789 digit = cdigit - '0';
1790 else if (isxdigit(cdigit))
1791 if (cdigit >= 'a')
1792 digit = cdigit - 'a' + 10;
1793 else if (cdigit >= 'A')
1794 digit = cdigit - 'A' + 10;
1795 else
1796 assert(0 && "huh?");
1797 else
1798 assert(0 && "Invalid character in digit string");
1799
1800 // Shift or multiple the value by the radix
1801 if (shift)
1802 this->shl(shift);
1803 else
1804 *this *= apradix;
1805
1806 // Add in the digit we just interpreted
Reid Spencer5bce8542007-02-24 20:19:37 +00001807 if (apdigit.isSingleWord())
1808 apdigit.VAL = digit;
1809 else
1810 apdigit.pVal[0] = digit;
Reid Spencer385f7542007-02-21 03:55:44 +00001811 *this += apdigit;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001812 }
Reid Spencer9eec2412007-02-25 23:44:53 +00001813 // If its negative, put it in two's complement form
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001814 if (isNeg) {
1815 (*this)--;
Reid Spencer9eec2412007-02-25 23:44:53 +00001816 this->flip();
Reid Spencer9eec2412007-02-25 23:44:53 +00001817 }
Reid Spencer5e0a8512007-02-17 03:16:00 +00001818}
Reid Spencer9c0696f2007-02-20 08:51:03 +00001819
Reid Spencer9c0696f2007-02-20 08:51:03 +00001820std::string APInt::toString(uint8_t radix, bool wantSigned) const {
1821 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1822 "Radix should be 2, 8, 10, or 16!");
1823 static const char *digits[] = {
1824 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
1825 };
1826 std::string result;
1827 uint32_t bits_used = getActiveBits();
1828 if (isSingleWord()) {
1829 char buf[65];
1830 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
1831 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
1832 if (format) {
1833 if (wantSigned) {
1834 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
1835 (APINT_BITS_PER_WORD-BitWidth);
1836 sprintf(buf, format, sextVal);
1837 } else
1838 sprintf(buf, format, VAL);
1839 } else {
1840 memset(buf, 0, 65);
1841 uint64_t v = VAL;
1842 while (bits_used) {
1843 uint32_t bit = v & 1;
1844 bits_used--;
1845 buf[bits_used] = digits[bit][0];
1846 v >>=1;
1847 }
1848 }
1849 result = buf;
1850 return result;
1851 }
1852
1853 if (radix != 10) {
1854 uint64_t mask = radix - 1;
1855 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : 1);
1856 uint32_t nibbles = APINT_BITS_PER_WORD / shift;
1857 for (uint32_t i = 0; i < getNumWords(); ++i) {
1858 uint64_t value = pVal[i];
1859 for (uint32_t j = 0; j < nibbles; ++j) {
1860 result.insert(0, digits[ value & mask ]);
1861 value >>= shift;
1862 }
1863 }
1864 return result;
1865 }
1866
1867 APInt tmp(*this);
1868 APInt divisor(4, radix);
1869 APInt zero(tmp.getBitWidth(), 0);
1870 size_t insert_at = 0;
1871 if (wantSigned && tmp[BitWidth-1]) {
1872 // They want to print the signed version and it is a negative value
1873 // Flip the bits and add one to turn it into the equivalent positive
1874 // value and put a '-' in the result.
1875 tmp.flip();
1876 tmp++;
1877 result = "-";
1878 insert_at = 1;
1879 }
Reid Spencere549c492007-02-21 00:29:48 +00001880 if (tmp == APInt(tmp.getBitWidth(), 0))
Reid Spencer9c0696f2007-02-20 08:51:03 +00001881 result = "0";
1882 else while (tmp.ne(zero)) {
1883 APInt APdigit(1,0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001884 APInt tmp2(tmp.getBitWidth(), 0);
Reid Spencer385f7542007-02-21 03:55:44 +00001885 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
1886 &APdigit);
Reid Spencer794f4722007-02-26 21:02:27 +00001887 uint32_t digit = APdigit.getZExtValue();
Reid Spencer385f7542007-02-21 03:55:44 +00001888 assert(digit < radix && "divide failed");
1889 result.insert(insert_at,digits[digit]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001890 tmp = tmp2;
1891 }
1892
1893 return result;
1894}
1895
Reid Spencer385f7542007-02-21 03:55:44 +00001896#ifndef NDEBUG
1897void APInt::dump() const
1898{
Reid Spencer610fad82007-02-24 10:01:42 +00001899 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16);
Reid Spencer385f7542007-02-21 03:55:44 +00001900 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +00001901 cerr << VAL;
Reid Spencer385f7542007-02-21 03:55:44 +00001902 else for (unsigned i = getNumWords(); i > 0; i--) {
Reid Spencer610fad82007-02-24 10:01:42 +00001903 cerr << pVal[i-1] << " ";
Reid Spencer385f7542007-02-21 03:55:44 +00001904 }
Reid Spencer681dcd12007-02-27 21:59:26 +00001905 cerr << " U(" << this->toString(10) << ") S(" << this->toStringSigned(10)
1906 << ")\n" << std::setbase(10);
Reid Spencer385f7542007-02-21 03:55:44 +00001907}
1908#endif