blob: 88f32810d6751a5061a1dd3b5f1925ddc892e148 [file] [log] [blame]
Zhou Shengfd43dcf2007-02-06 03:00:16 +00001//===-- APInt.cpp - Implement APInt class ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Ted Kremeneke420deb2008-01-19 04:23:33 +000017#include "llvm/ADT/FoldingSet.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#include <iomanip>
Reid Spencer385f7542007-02-21 03:55:44 +000025
Zhou Shengfd43dcf2007-02-06 03:00:16 +000026using namespace llvm;
27
Reid Spencer9af18872007-12-11 06:53:58 +000028/// This enumeration just provides for internal constants used in this
29/// translation unit.
30enum {
31 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
32 ///< Note that this must remain synchronized with IntegerType::MIN_INT_BITS
33 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
34 ///< Note that this must remain synchronized with IntegerType::MAX_INT_BITS
35};
36
Reid Spencer5d0d05c2007-02-25 19:32:03 +000037/// A utility function for allocating memory, checking for allocation failures,
38/// and ensuring the contents are zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000039inline static uint64_t* getClearedMemory(uint32_t numWords) {
40 uint64_t * result = new uint64_t[numWords];
41 assert(result && "APInt memory allocation fails!");
42 memset(result, 0, numWords * sizeof(uint64_t));
43 return result;
Zhou Sheng353815d2007-02-06 06:04:53 +000044}
45
Reid Spencer5d0d05c2007-02-25 19:32:03 +000046/// A utility function for allocating memory and checking for allocation
47/// failure. The content is not zeroed.
Reid Spenceraf0e9562007-02-18 18:38:44 +000048inline static uint64_t* getMemory(uint32_t numWords) {
49 uint64_t * result = new uint64_t[numWords];
50 assert(result && "APInt memory allocation fails!");
51 return result;
52}
53
Reid Spenceradf2a202007-03-19 21:19:02 +000054APInt::APInt(uint32_t numBits, uint64_t val, bool isSigned)
Reid Spencer3a341372007-03-19 20:37:47 +000055 : BitWidth(numBits), VAL(0) {
Reid Spencer9af18872007-12-11 06:53:58 +000056 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
57 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Reid Spencer5d0d05c2007-02-25 19:32:03 +000058 if (isSingleWord())
59 VAL = val;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000060 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +000061 pVal = getClearedMemory(getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +000062 pVal[0] = val;
Reid Spencer3a341372007-03-19 20:37:47 +000063 if (isSigned && int64_t(val) < 0)
64 for (unsigned i = 1; i < getNumWords(); ++i)
65 pVal[i] = -1ULL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +000066 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +000067 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000068}
69
Dale Johannesen910993e2007-09-21 22:09:37 +000070APInt::APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[])
Reid Spencer385f7542007-02-21 03:55:44 +000071 : BitWidth(numBits), VAL(0) {
Reid Spencer9af18872007-12-11 06:53:58 +000072 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
73 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Zhou Shengfd43dcf2007-02-06 03:00:16 +000074 assert(bigVal && "Null pointer detected!");
75 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +000076 VAL = bigVal[0];
Zhou Shengfd43dcf2007-02-06 03:00:16 +000077 else {
Reid Spencer610fad82007-02-24 10:01:42 +000078 // Get memory, cleared to 0
79 pVal = getClearedMemory(getNumWords());
80 // Calculate the number of words to copy
81 uint32_t words = std::min<uint32_t>(numWords, getNumWords());
82 // Copy the words from bigVal to pVal
83 memcpy(pVal, bigVal, words * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +000084 }
Reid Spencer610fad82007-02-24 10:01:42 +000085 // Make sure unused high bits are cleared
86 clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +000087}
88
Reid Spenceraf0e9562007-02-18 18:38:44 +000089APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
Reid Spencer9c0696f2007-02-20 08:51:03 +000090 uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000091 : BitWidth(numbits), VAL(0) {
Reid Spencer9af18872007-12-11 06:53:58 +000092 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
93 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Reid Spencere81d2da2007-02-16 22:36:51 +000094 fromString(numbits, StrStart, slen, radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +000095}
96
Reid Spencer9c0696f2007-02-20 08:51:03 +000097APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix)
Reid Spencer385f7542007-02-21 03:55:44 +000098 : BitWidth(numbits), VAL(0) {
Reid Spencer9af18872007-12-11 06:53:58 +000099 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
100 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Zhou Shenga3832fd2007-02-07 06:14:53 +0000101 assert(!Val.empty() && "String empty?");
Reid Spencere81d2da2007-02-16 22:36:51 +0000102 fromString(numbits, Val.c_str(), Val.size(), radix);
Zhou Shenga3832fd2007-02-07 06:14:53 +0000103}
104
Reid Spencer54362ca2007-02-20 23:40:25 +0000105APInt::APInt(const APInt& that)
Reid Spencer385f7542007-02-21 03:55:44 +0000106 : BitWidth(that.BitWidth), VAL(0) {
Reid Spencer9af18872007-12-11 06:53:58 +0000107 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
108 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000109 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +0000110 VAL = that.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000111 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000112 pVal = getMemory(getNumWords());
Reid Spencer54362ca2007-02-20 23:40:25 +0000113 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000114 }
115}
116
117APInt::~APInt() {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000118 if (!isSingleWord() && pVal)
Reid Spencer9ac44112007-02-26 23:38:21 +0000119 delete [] pVal;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000120}
121
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000122APInt& APInt::operator=(const APInt& RHS) {
Reid Spencer9ac44112007-02-26 23:38:21 +0000123 // Don't do anything for X = X
124 if (this == &RHS)
125 return *this;
126
127 // If the bitwidths are the same, we can avoid mucking with memory
128 if (BitWidth == RHS.getBitWidth()) {
129 if (isSingleWord())
130 VAL = RHS.VAL;
131 else
132 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
133 return *this;
134 }
135
136 if (isSingleWord())
137 if (RHS.isSingleWord())
138 VAL = RHS.VAL;
139 else {
140 VAL = 0;
141 pVal = getMemory(RHS.getNumWords());
142 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
143 }
144 else if (getNumWords() == RHS.getNumWords())
145 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
146 else if (RHS.isSingleWord()) {
147 delete [] pVal;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000148 VAL = RHS.VAL;
Reid Spencer9ac44112007-02-26 23:38:21 +0000149 } else {
150 delete [] pVal;
151 pVal = getMemory(RHS.getNumWords());
152 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
153 }
154 BitWidth = RHS.BitWidth;
155 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000156}
157
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000158APInt& APInt::operator=(uint64_t RHS) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000159 if (isSingleWord())
160 VAL = RHS;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000161 else {
162 pVal[0] = RHS;
Reid Spencera58f0582007-02-18 20:09:41 +0000163 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000164 }
Reid Spencer9ac44112007-02-26 23:38:21 +0000165 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000166}
167
Ted Kremeneke420deb2008-01-19 04:23:33 +0000168/// Profile - This method 'profiles' an APInt for use with FoldingSet.
169void APInt::Profile(FoldingSetNodeID& ID) const {
170 if (isSingleWord()) {
171 ID.AddInteger(VAL);
172 return;
173 }
174
175 uint32_t NumWords = getNumWords();
176 for (unsigned i = 0; i < NumWords; ++i)
177 ID.AddInteger(pVal[i]);
178}
179
Reid Spenceraf0e9562007-02-18 18:38:44 +0000180/// add_1 - This function adds a single "digit" integer, y, to the multiple
181/// "digit" integer array, x[]. x[] is modified to reflect the addition and
182/// 1 is returned if there is a carry out, otherwise 0 is returned.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000183/// @returns the carry of the addition.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000184static bool add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000185 for (uint32_t i = 0; i < len; ++i) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000186 dest[i] = y + x[i];
187 if (dest[i] < y)
Reid Spencer610fad82007-02-24 10:01:42 +0000188 y = 1; // Carry one to next digit.
Reid Spencerf2c521c2007-02-18 06:39:42 +0000189 else {
Reid Spencer610fad82007-02-24 10:01:42 +0000190 y = 0; // No need to carry so exit early
Reid Spencerf2c521c2007-02-18 06:39:42 +0000191 break;
192 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000193 }
Reid Spencerf2c521c2007-02-18 06:39:42 +0000194 return y;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000195}
196
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000197/// @brief Prefix increment operator. Increments the APInt by one.
198APInt& APInt::operator++() {
Reid Spencere81d2da2007-02-16 22:36:51 +0000199 if (isSingleWord())
200 ++VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000201 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000202 add_1(pVal, pVal, getNumWords(), 1);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000203 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000204}
205
Reid Spenceraf0e9562007-02-18 18:38:44 +0000206/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
207/// the multi-digit integer array, x[], propagating the borrowed 1 value until
208/// no further borrowing is neeeded or it runs out of "digits" in x. The result
209/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
210/// In other words, if y > x then this function returns 1, otherwise 0.
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000211/// @returns the borrow out of the subtraction
212static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000213 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000214 uint64_t X = x[i];
Reid Spencerf2c521c2007-02-18 06:39:42 +0000215 x[i] -= y;
216 if (y > X)
Reid Spenceraf0e9562007-02-18 18:38:44 +0000217 y = 1; // We have to "borrow 1" from next "digit"
Reid Spencer5e0a8512007-02-17 03:16:00 +0000218 else {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000219 y = 0; // No need to borrow
220 break; // Remaining digits are unchanged so exit early
Reid Spencer5e0a8512007-02-17 03:16:00 +0000221 }
222 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000223 return bool(y);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000224}
225
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000226/// @brief Prefix decrement operator. Decrements the APInt by one.
227APInt& APInt::operator--() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000228 if (isSingleWord())
229 --VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000230 else
Zhou Shenga3832fd2007-02-07 06:14:53 +0000231 sub_1(pVal, getNumWords(), 1);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000232 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000233}
234
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000235/// add - This function adds the integer array x to the integer array Y and
236/// places the result in dest.
237/// @returns the carry out from the addition
238/// @brief General addition of 64-bit integer arrays
Reid Spencer9d6c9192007-02-24 03:58:46 +0000239static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
240 uint32_t len) {
241 bool carry = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000242 for (uint32_t i = 0; i< len; ++i) {
Reid Spencer92904632007-02-23 01:57:13 +0000243 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
Reid Spencer54362ca2007-02-20 23:40:25 +0000244 dest[i] = x[i] + y[i] + carry;
Reid Spencer60c0a6a2007-02-21 05:44:56 +0000245 carry = dest[i] < limit || (carry && dest[i] == limit);
Reid Spencer5e0a8512007-02-17 03:16:00 +0000246 }
247 return carry;
248}
249
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000250/// Adds the RHS APint to this APInt.
251/// @returns this, after addition of RHS.
252/// @brief Addition assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000253APInt& APInt::operator+=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000254 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer54362ca2007-02-20 23:40:25 +0000255 if (isSingleWord())
256 VAL += RHS.VAL;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000257 else {
Reid Spencer54362ca2007-02-20 23:40:25 +0000258 add(pVal, pVal, RHS.pVal, getNumWords());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000259 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000260 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000261}
262
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000263/// Subtracts the integer array y from the integer array x
264/// @returns returns the borrow out.
265/// @brief Generalized subtraction of 64-bit integer arrays.
Reid Spencer9d6c9192007-02-24 03:58:46 +0000266static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
267 uint32_t len) {
Reid Spencer385f7542007-02-21 03:55:44 +0000268 bool borrow = false;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000269 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer385f7542007-02-21 03:55:44 +0000270 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
271 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
272 dest[i] = x_tmp - y[i];
Reid Spencer5e0a8512007-02-17 03:16:00 +0000273 }
Reid Spencer54362ca2007-02-20 23:40:25 +0000274 return borrow;
Reid Spencer5e0a8512007-02-17 03:16:00 +0000275}
276
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000277/// Subtracts the RHS APInt from this APInt
278/// @returns this, after subtraction
279/// @brief Subtraction assignment operator.
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000280APInt& APInt::operator-=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000281 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000282 if (isSingleWord())
Reid Spencer54362ca2007-02-20 23:40:25 +0000283 VAL -= RHS.VAL;
284 else
285 sub(pVal, pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000286 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000287}
288
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000289/// Multiplies an integer array, x by a a uint64_t integer and places the result
290/// into dest.
291/// @returns the carry out of the multiplication.
292/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
Reid Spencer610fad82007-02-24 10:01:42 +0000293static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
294 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
Reid Spencer5e0a8512007-02-17 03:16:00 +0000295 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000296 uint64_t carry = 0;
297
298 // For each digit of x.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000299 for (uint32_t i = 0; i < len; ++i) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000300 // Split x into high and low words
301 uint64_t lx = x[i] & 0xffffffffULL;
302 uint64_t hx = x[i] >> 32;
303 // hasCarry - A flag to indicate if there is a carry to the next digit.
Reid Spencer5e0a8512007-02-17 03:16:00 +0000304 // hasCarry == 0, no carry
305 // hasCarry == 1, has carry
306 // hasCarry == 2, no carry and the calculation result == 0.
307 uint8_t hasCarry = 0;
308 dest[i] = carry + lx * ly;
309 // Determine if the add above introduces carry.
310 hasCarry = (dest[i] < carry) ? 1 : 0;
311 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
312 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
313 // (2^32 - 1) + 2^32 = 2^64.
314 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
315
316 carry += (lx * hy) & 0xffffffffULL;
317 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
318 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
319 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
320 }
Reid Spencer5e0a8512007-02-17 03:16:00 +0000321 return carry;
322}
323
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000324/// Multiplies integer array x by integer array y and stores the result into
325/// the integer array dest. Note that dest's size must be >= xlen + ylen.
326/// @brief Generalized multiplicate of integer arrays.
Reid Spencer610fad82007-02-24 10:01:42 +0000327static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[],
328 uint32_t ylen) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000329 dest[xlen] = mul_1(dest, x, xlen, y[0]);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000330 for (uint32_t i = 1; i < ylen; ++i) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000331 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
Reid Spencere0cdd332007-02-21 08:21:52 +0000332 uint64_t carry = 0, lx = 0, hx = 0;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000333 for (uint32_t j = 0; j < xlen; ++j) {
Reid Spencer5e0a8512007-02-17 03:16:00 +0000334 lx = x[j] & 0xffffffffULL;
335 hx = x[j] >> 32;
336 // hasCarry - A flag to indicate if has carry.
337 // hasCarry == 0, no carry
338 // hasCarry == 1, has carry
339 // hasCarry == 2, no carry and the calculation result == 0.
340 uint8_t hasCarry = 0;
341 uint64_t resul = carry + lx * ly;
342 hasCarry = (resul < carry) ? 1 : 0;
343 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
344 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
345
346 carry += (lx * hy) & 0xffffffffULL;
347 resul = (carry << 32) | (resul & 0xffffffffULL);
348 dest[i+j] += resul;
349 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
350 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
351 ((lx * hy) >> 32) + hx * hy;
352 }
353 dest[i+xlen] = carry;
354 }
355}
356
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000357APInt& APInt::operator*=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000358 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencere0cdd332007-02-21 08:21:52 +0000359 if (isSingleWord()) {
Reid Spencer61eb1802007-02-20 20:42:10 +0000360 VAL *= RHS.VAL;
Reid Spencere0cdd332007-02-21 08:21:52 +0000361 clearUnusedBits();
362 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000363 }
Reid Spencere0cdd332007-02-21 08:21:52 +0000364
365 // Get some bit facts about LHS and check for zero
366 uint32_t lhsBits = getActiveBits();
367 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
368 if (!lhsWords)
369 // 0 * X ===> 0
370 return *this;
371
372 // Get some bit facts about RHS and check for zero
373 uint32_t rhsBits = RHS.getActiveBits();
374 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
375 if (!rhsWords) {
376 // X * 0 ===> 0
377 clear();
378 return *this;
379 }
380
381 // Allocate space for the result
382 uint32_t destWords = rhsWords + lhsWords;
383 uint64_t *dest = getMemory(destWords);
384
385 // Perform the long multiply
386 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
387
388 // Copy result back into *this
389 clear();
390 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
391 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
392
393 // delete dest array and return
394 delete[] dest;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000395 return *this;
396}
397
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000398APInt& APInt::operator&=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000399 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000400 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000401 VAL &= RHS.VAL;
402 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000403 }
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];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000407 return *this;
408}
409
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000410APInt& APInt::operator|=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000411 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000412 if (isSingleWord()) {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000413 VAL |= RHS.VAL;
414 return *this;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000415 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000416 uint32_t numWords = getNumWords();
417 for (uint32_t i = 0; i < numWords; ++i)
418 pVal[i] |= RHS.pVal[i];
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000419 return *this;
420}
421
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000422APInt& APInt::operator^=(const APInt& RHS) {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000423 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000424 if (isSingleWord()) {
Reid Spencerf2c521c2007-02-18 06:39:42 +0000425 VAL ^= RHS.VAL;
Reid Spencer54362ca2007-02-20 23:40:25 +0000426 this->clearUnusedBits();
Reid Spencerf2c521c2007-02-18 06:39:42 +0000427 return *this;
428 }
Reid Spenceraf0e9562007-02-18 18:38:44 +0000429 uint32_t numWords = getNumWords();
430 for (uint32_t i = 0; i < numWords; ++i)
431 pVal[i] ^= RHS.pVal[i];
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000432 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000433}
434
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000435APInt APInt::operator&(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000436 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000437 if (isSingleWord())
438 return APInt(getBitWidth(), VAL & RHS.VAL);
439
Reid Spenceraf0e9562007-02-18 18:38:44 +0000440 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000441 uint64_t* val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000442 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000443 val[i] = pVal[i] & RHS.pVal[i];
444 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000445}
446
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000447APInt APInt::operator|(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000448 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spenceraf0e9562007-02-18 18:38:44 +0000449 if (isSingleWord())
450 return APInt(getBitWidth(), VAL | RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000451
Reid Spenceraf0e9562007-02-18 18:38:44 +0000452 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000453 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000454 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000455 val[i] = pVal[i] | RHS.pVal[i];
456 return APInt(val, getBitWidth());
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000457}
458
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000459APInt APInt::operator^(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000460 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000461 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000462 return APInt(BitWidth, VAL ^ RHS.VAL);
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000463
Reid Spenceraf0e9562007-02-18 18:38:44 +0000464 uint32_t numWords = getNumWords();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000465 uint64_t *val = getMemory(numWords);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000466 for (uint32_t i = 0; i < numWords; ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000467 val[i] = pVal[i] ^ RHS.pVal[i];
468
469 // 0^0==1 so clear the high bits in case they got set.
470 return APInt(val, getBitWidth()).clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000471}
472
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000473bool APInt::operator !() const {
474 if (isSingleWord())
475 return !VAL;
Reid Spenceraf0e9562007-02-18 18:38:44 +0000476
477 for (uint32_t i = 0; i < getNumWords(); ++i)
478 if (pVal[i])
479 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000480 return true;
481}
482
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000483APInt APInt::operator*(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000484 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000485 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000486 return APInt(BitWidth, VAL * RHS.VAL);
Reid Spencer61eb1802007-02-20 20:42:10 +0000487 APInt Result(*this);
488 Result *= RHS;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000489 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000490}
491
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000492APInt APInt::operator+(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000493 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000494 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000495 return APInt(BitWidth, VAL + RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000496 APInt Result(BitWidth, 0);
497 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000498 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000499}
500
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000501APInt APInt::operator-(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000502 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000503 if (isSingleWord())
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000504 return APInt(BitWidth, VAL - RHS.VAL);
Reid Spencer54362ca2007-02-20 23:40:25 +0000505 APInt Result(BitWidth, 0);
506 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000507 return Result.clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000508}
509
Reid Spenceraf0e9562007-02-18 18:38:44 +0000510bool APInt::operator[](uint32_t bitPosition) const {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000511 return (maskBit(bitPosition) &
512 (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000513}
514
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000515bool APInt::operator==(const APInt& RHS) const {
Reid Spencer9ac44112007-02-26 23:38:21 +0000516 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
Reid Spencer54362ca2007-02-20 23:40:25 +0000517 if (isSingleWord())
518 return VAL == RHS.VAL;
519
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000520 // Get some facts about the number of bits used in the two operands.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000521 uint32_t n1 = getActiveBits();
522 uint32_t n2 = RHS.getActiveBits();
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000523
524 // If the number of bits isn't the same, they aren't equal
Reid Spencer54362ca2007-02-20 23:40:25 +0000525 if (n1 != n2)
526 return false;
527
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000528 // If the number of bits fits in a word, we only need to compare the low word.
Reid Spencer54362ca2007-02-20 23:40:25 +0000529 if (n1 <= APINT_BITS_PER_WORD)
530 return pVal[0] == RHS.pVal[0];
531
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000532 // Otherwise, compare everything
Reid Spencer54362ca2007-02-20 23:40:25 +0000533 for (int i = whichWord(n1 - 1); i >= 0; --i)
534 if (pVal[i] != RHS.pVal[i])
535 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000536 return true;
537}
538
Zhou Shenga3832fd2007-02-07 06:14:53 +0000539bool APInt::operator==(uint64_t Val) const {
540 if (isSingleWord())
541 return VAL == Val;
Reid Spencer54362ca2007-02-20 23:40:25 +0000542
543 uint32_t n = getActiveBits();
544 if (n <= APINT_BITS_PER_WORD)
545 return pVal[0] == Val;
546 else
547 return false;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000548}
549
Reid Spencere81d2da2007-02-16 22:36:51 +0000550bool APInt::ult(const APInt& RHS) const {
551 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
552 if (isSingleWord())
553 return VAL < RHS.VAL;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000554
555 // Get active bit length of both operands
556 uint32_t n1 = getActiveBits();
557 uint32_t n2 = RHS.getActiveBits();
558
559 // If magnitude of LHS is less than RHS, return true.
560 if (n1 < n2)
561 return true;
562
563 // If magnitude of RHS is greather than LHS, return false.
564 if (n2 < n1)
565 return false;
566
567 // If they bot fit in a word, just compare the low order word
568 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
569 return pVal[0] < RHS.pVal[0];
570
571 // Otherwise, compare all words
Reid Spencer1fa111e2007-02-27 18:23:40 +0000572 uint32_t topWord = whichWord(std::max(n1,n2)-1);
573 for (int i = topWord; i >= 0; --i) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000574 if (pVal[i] > RHS.pVal[i])
Reid Spencere81d2da2007-02-16 22:36:51 +0000575 return false;
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000576 if (pVal[i] < RHS.pVal[i])
577 return true;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000578 }
579 return false;
580}
581
Reid Spencere81d2da2007-02-16 22:36:51 +0000582bool APInt::slt(const APInt& RHS) const {
583 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
Reid Spencera58f0582007-02-18 20:09:41 +0000584 if (isSingleWord()) {
585 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
586 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
587 return lhsSext < rhsSext;
Reid Spencere81d2da2007-02-16 22:36:51 +0000588 }
Reid Spencera58f0582007-02-18 20:09:41 +0000589
590 APInt lhs(*this);
Reid Spencer1fa111e2007-02-27 18:23:40 +0000591 APInt rhs(RHS);
592 bool lhsNeg = isNegative();
593 bool rhsNeg = rhs.isNegative();
594 if (lhsNeg) {
595 // Sign bit is set so perform two's complement to make it positive
Reid Spencera58f0582007-02-18 20:09:41 +0000596 lhs.flip();
597 lhs++;
598 }
Reid Spencer1fa111e2007-02-27 18:23:40 +0000599 if (rhsNeg) {
600 // Sign bit is set so perform two's complement to make it positive
Reid Spencera58f0582007-02-18 20:09:41 +0000601 rhs.flip();
602 rhs++;
603 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000604
605 // Now we have unsigned values to compare so do the comparison if necessary
606 // based on the negativeness of the values.
Reid Spencer1fa111e2007-02-27 18:23:40 +0000607 if (lhsNeg)
608 if (rhsNeg)
609 return lhs.ugt(rhs);
Reid Spencera58f0582007-02-18 20:09:41 +0000610 else
611 return true;
Reid Spencer1fa111e2007-02-27 18:23:40 +0000612 else if (rhsNeg)
Reid Spencera58f0582007-02-18 20:09:41 +0000613 return false;
614 else
615 return lhs.ult(rhs);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000616}
617
Reid Spenceraf0e9562007-02-18 18:38:44 +0000618APInt& APInt::set(uint32_t bitPosition) {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000619 if (isSingleWord())
620 VAL |= maskBit(bitPosition);
621 else
622 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000623 return *this;
624}
625
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000626APInt& APInt::set() {
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000627 if (isSingleWord()) {
628 VAL = -1ULL;
629 return clearUnusedBits();
Zhou Shengb04973e2007-02-15 06:36:31 +0000630 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000631
632 // Set all the bits in all the words.
Zhou Sheng6dbe2332007-03-21 04:34:37 +0000633 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer5d0d05c2007-02-25 19:32:03 +0000634 pVal[i] = -1ULL;
635 // Clear the unused ones
636 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000637}
638
639/// Set the given bit to 0 whose position is given as "bitPosition".
640/// @brief Set a given bit to 0.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000641APInt& APInt::clear(uint32_t bitPosition) {
642 if (isSingleWord())
643 VAL &= ~maskBit(bitPosition);
644 else
645 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000646 return *this;
647}
648
649/// @brief Set every bit to 0.
650APInt& APInt::clear() {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000651 if (isSingleWord())
652 VAL = 0;
Zhou Shenga3832fd2007-02-07 06:14:53 +0000653 else
Reid Spencera58f0582007-02-18 20:09:41 +0000654 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000655 return *this;
656}
657
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000658/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
659/// this APInt.
660APInt APInt::operator~() const {
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000661 APInt Result(*this);
662 Result.flip();
663 return Result;
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000664}
665
666/// @brief Toggle every bit to its opposite value.
667APInt& APInt::flip() {
Reid Spencer9eec2412007-02-25 23:44:53 +0000668 if (isSingleWord()) {
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000669 VAL ^= -1ULL;
Reid Spencer9eec2412007-02-25 23:44:53 +0000670 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000671 }
Reid Spencer9eec2412007-02-25 23:44:53 +0000672 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000673 pVal[i] ^= -1ULL;
Reid Spencer9eec2412007-02-25 23:44:53 +0000674 return clearUnusedBits();
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000675}
676
677/// Toggle a given bit to its opposite value whose position is given
678/// as "bitPosition".
679/// @brief Toggles a given bit to its opposite value.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000680APInt& APInt::flip(uint32_t bitPosition) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000681 assert(bitPosition < BitWidth && "Out of the bit-width range!");
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000682 if ((*this)[bitPosition]) clear(bitPosition);
683 else set(bitPosition);
684 return *this;
685}
686
Reid Spencer57ae4f52007-04-13 19:19:07 +0000687uint32_t APInt::getBitsNeeded(const char* str, uint32_t slen, uint8_t radix) {
688 assert(str != 0 && "Invalid value string");
689 assert(slen > 0 && "Invalid string length");
690
691 // Each computation below needs to know if its negative
692 uint32_t isNegative = str[0] == '-';
693 if (isNegative) {
694 slen--;
695 str++;
696 }
697 // For radixes of power-of-two values, the bits required is accurately and
698 // easily computed
699 if (radix == 2)
700 return slen + isNegative;
701 if (radix == 8)
702 return slen * 3 + isNegative;
703 if (radix == 16)
704 return slen * 4 + isNegative;
705
706 // Otherwise it must be radix == 10, the hard case
707 assert(radix == 10 && "Invalid radix");
708
709 // This is grossly inefficient but accurate. We could probably do something
710 // with a computation of roughly slen*64/20 and then adjust by the value of
711 // the first few digits. But, I'm not sure how accurate that could be.
712
713 // Compute a sufficient number of bits that is always large enough but might
714 // be too large. This avoids the assertion in the constructor.
715 uint32_t sufficient = slen*64/18;
716
717 // Convert to the actual binary value.
718 APInt tmp(sufficient, str, slen, radix);
719
720 // Compute how many bits are required.
Reid Spencer0468ab32007-04-14 00:00:10 +0000721 return isNegative + tmp.logBase2() + 1;
Reid Spencer57ae4f52007-04-13 19:19:07 +0000722}
723
Reid Spencer794f4722007-02-26 21:02:27 +0000724uint64_t APInt::getHashValue() const {
Reid Spencer9ac44112007-02-26 23:38:21 +0000725 // Put the bit width into the low order bits.
726 uint64_t hash = BitWidth;
Reid Spencer794f4722007-02-26 21:02:27 +0000727
728 // Add the sum of the words to the hash.
729 if (isSingleWord())
Reid Spencer9ac44112007-02-26 23:38:21 +0000730 hash += VAL << 6; // clear separation of up to 64 bits
Reid Spencer794f4722007-02-26 21:02:27 +0000731 else
732 for (uint32_t i = 0; i < getNumWords(); ++i)
Reid Spencer9ac44112007-02-26 23:38:21 +0000733 hash += pVal[i] << 6; // clear sepration of up to 64 bits
Reid Spencer794f4722007-02-26 21:02:27 +0000734 return hash;
735}
736
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000737/// HiBits - This function returns the high "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000738APInt APInt::getHiBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000739 return APIntOps::lshr(*this, BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000740}
741
742/// LoBits - This function returns the low "numBits" bits of this APInt.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000743APInt APInt::getLoBits(uint32_t numBits) const {
Reid Spencere81d2da2007-02-16 22:36:51 +0000744 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
745 BitWidth - numBits);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000746}
747
Reid Spencere81d2da2007-02-16 22:36:51 +0000748bool APInt::isPowerOf2() const {
749 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
750}
751
Reid Spenceraf0e9562007-02-18 18:38:44 +0000752uint32_t APInt::countLeadingZeros() const {
Reid Spenceraf0e9562007-02-18 18:38:44 +0000753 uint32_t Count = 0;
Reid Spencere549c492007-02-21 00:29:48 +0000754 if (isSingleWord())
755 Count = CountLeadingZeros_64(VAL);
756 else {
757 for (uint32_t i = getNumWords(); i > 0u; --i) {
758 if (pVal[i-1] == 0)
759 Count += APINT_BITS_PER_WORD;
760 else {
761 Count += CountLeadingZeros_64(pVal[i-1]);
762 break;
763 }
764 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000765 }
Reid Spencerab2b2c82007-02-22 00:22:00 +0000766 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD;
767 if (remainder)
768 Count -= APINT_BITS_PER_WORD - remainder;
Chris Lattner9e513ac2007-11-23 22:42:31 +0000769 return std::min(Count, BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000770}
771
Reid Spencer681dcd12007-02-27 21:59:26 +0000772static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) {
773 uint32_t Count = 0;
774 if (skip)
775 V <<= skip;
776 while (V && (V & (1ULL << 63))) {
777 Count++;
778 V <<= 1;
779 }
780 return Count;
781}
782
783uint32_t APInt::countLeadingOnes() const {
784 if (isSingleWord())
785 return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth);
786
787 uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD;
788 uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits);
789 int i = getNumWords() - 1;
790 uint32_t Count = countLeadingOnes_64(pVal[i], shift);
791 if (Count == highWordBits) {
792 for (i--; i >= 0; --i) {
793 if (pVal[i] == -1ULL)
794 Count += APINT_BITS_PER_WORD;
795 else {
796 Count += countLeadingOnes_64(pVal[i], 0);
797 break;
798 }
799 }
800 }
801 return Count;
802}
803
Reid Spenceraf0e9562007-02-18 18:38:44 +0000804uint32_t APInt::countTrailingZeros() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000805 if (isSingleWord())
Anton Korobeynikov97d37262007-12-24 11:16:47 +0000806 return std::min(uint32_t(CountTrailingZeros_64(VAL)), BitWidth);
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000807 uint32_t Count = 0;
808 uint32_t i = 0;
809 for (; i < getNumWords() && pVal[i] == 0; ++i)
810 Count += APINT_BITS_PER_WORD;
811 if (i < getNumWords())
812 Count += CountTrailingZeros_64(pVal[i]);
Chris Lattner5e557122007-11-23 22:36:25 +0000813 return std::min(Count, BitWidth);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000814}
815
Reid Spenceraf0e9562007-02-18 18:38:44 +0000816uint32_t APInt::countPopulation() const {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000817 if (isSingleWord())
818 return CountPopulation_64(VAL);
Reid Spenceraf0e9562007-02-18 18:38:44 +0000819 uint32_t Count = 0;
820 for (uint32_t i = 0; i < getNumWords(); ++i)
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000821 Count += CountPopulation_64(pVal[i]);
822 return Count;
823}
824
Reid Spencere81d2da2007-02-16 22:36:51 +0000825APInt APInt::byteSwap() const {
826 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
827 if (BitWidth == 16)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000828 return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
Reid Spencere81d2da2007-02-16 22:36:51 +0000829 else if (BitWidth == 32)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000830 return APInt(BitWidth, ByteSwap_32(uint32_t(VAL)));
Reid Spencere81d2da2007-02-16 22:36:51 +0000831 else if (BitWidth == 48) {
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000832 uint32_t Tmp1 = uint32_t(VAL >> 16);
Zhou Shengb04973e2007-02-15 06:36:31 +0000833 Tmp1 = ByteSwap_32(Tmp1);
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000834 uint16_t Tmp2 = uint16_t(VAL);
Zhou Shengb04973e2007-02-15 06:36:31 +0000835 Tmp2 = ByteSwap_16(Tmp2);
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000836 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
Reid Spencere81d2da2007-02-16 22:36:51 +0000837 } else if (BitWidth == 64)
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000838 return APInt(BitWidth, ByteSwap_64(VAL));
Zhou Shengb04973e2007-02-15 06:36:31 +0000839 else {
Reid Spencercd6f2bf2007-02-17 00:18:01 +0000840 APInt Result(BitWidth, 0);
Zhou Shengb04973e2007-02-15 06:36:31 +0000841 char *pByte = (char*)Result.pVal;
Reid Spencera58f0582007-02-18 20:09:41 +0000842 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
Zhou Shengb04973e2007-02-15 06:36:31 +0000843 char Tmp = pByte[i];
Reid Spencera58f0582007-02-18 20:09:41 +0000844 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
845 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
Zhou Shengb04973e2007-02-15 06:36:31 +0000846 }
847 return Result;
848 }
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000849}
850
Zhou Sheng0b706b12007-02-08 14:35:19 +0000851APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
852 const APInt& API2) {
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000853 APInt A = API1, B = API2;
854 while (!!B) {
855 APInt T = B;
Reid Spencere81d2da2007-02-16 22:36:51 +0000856 B = APIntOps::urem(A, B);
Zhou Shengfd43dcf2007-02-06 03:00:16 +0000857 A = T;
858 }
859 return A;
860}
Chris Lattner6ad4c142007-02-06 05:38:37 +0000861
Reid Spencer1fa111e2007-02-27 18:23:40 +0000862APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) {
Zhou Shengd93f00c2007-02-12 20:02:55 +0000863 union {
864 double D;
865 uint64_t I;
866 } T;
867 T.D = Double;
Reid Spencer30f44f32007-02-27 01:28:10 +0000868
869 // Get the sign bit from the highest order bit
Zhou Shengd93f00c2007-02-12 20:02:55 +0000870 bool isNeg = T.I >> 63;
Reid Spencer30f44f32007-02-27 01:28:10 +0000871
872 // Get the 11-bit exponent and adjust for the 1023 bit bias
Zhou Shengd93f00c2007-02-12 20:02:55 +0000873 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
Reid Spencer30f44f32007-02-27 01:28:10 +0000874
875 // If the exponent is negative, the value is < 0 so just return 0.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000876 if (exp < 0)
Reid Spencerff605762007-02-28 01:30:08 +0000877 return APInt(width, 0u);
Reid Spencer30f44f32007-02-27 01:28:10 +0000878
879 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
880 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
881
882 // If the exponent doesn't shift all bits out of the mantissa
Zhou Shengd93f00c2007-02-12 20:02:55 +0000883 if (exp < 52)
Reid Spencer1fa111e2007-02-27 18:23:40 +0000884 return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
885 APInt(width, mantissa >> (52 - exp));
886
887 // If the client didn't provide enough bits for us to shift the mantissa into
888 // then the result is undefined, just return 0
889 if (width <= exp - 52)
890 return APInt(width, 0);
Reid Spencer30f44f32007-02-27 01:28:10 +0000891
892 // Otherwise, we have to shift the mantissa bits up to the right location
Reid Spencer1fa111e2007-02-27 18:23:40 +0000893 APInt Tmp(width, mantissa);
Reid Spencere81d2da2007-02-16 22:36:51 +0000894 Tmp = Tmp.shl(exp - 52);
Zhou Shengd93f00c2007-02-12 20:02:55 +0000895 return isNeg ? -Tmp : Tmp;
896}
897
Reid Spencerdb3faa62007-02-13 22:41:58 +0000898/// RoundToDouble - This function convert this APInt to a double.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000899/// The layout for double is as following (IEEE Standard 754):
900/// --------------------------------------
901/// | Sign Exponent Fraction Bias |
902/// |-------------------------------------- |
903/// | 1[63] 11[62-52] 52[51-00] 1023 |
904/// --------------------------------------
Reid Spencere81d2da2007-02-16 22:36:51 +0000905double APInt::roundToDouble(bool isSigned) const {
Reid Spencer9c0696f2007-02-20 08:51:03 +0000906
907 // Handle the simple case where the value is contained in one uint64_t.
Reid Spencera58f0582007-02-18 20:09:41 +0000908 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
909 if (isSigned) {
910 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
911 return double(sext);
912 } else
913 return double(VAL);
914 }
915
Reid Spencer9c0696f2007-02-20 08:51:03 +0000916 // Determine if the value is negative.
Reid Spencere81d2da2007-02-16 22:36:51 +0000917 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000918
919 // Construct the absolute value if we're negative.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000920 APInt Tmp(isNeg ? -(*this) : (*this));
Reid Spencer9c0696f2007-02-20 08:51:03 +0000921
922 // Figure out how many bits we're using.
Reid Spenceraf0e9562007-02-18 18:38:44 +0000923 uint32_t n = Tmp.getActiveBits();
Zhou Shengd93f00c2007-02-12 20:02:55 +0000924
Reid Spencer9c0696f2007-02-20 08:51:03 +0000925 // The exponent (without bias normalization) is just the number of bits
926 // we are using. Note that the sign bit is gone since we constructed the
927 // absolute value.
928 uint64_t exp = n;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000929
Reid Spencer9c0696f2007-02-20 08:51:03 +0000930 // Return infinity for exponent overflow
931 if (exp > 1023) {
932 if (!isSigned || !isNeg)
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000933 return std::numeric_limits<double>::infinity();
Reid Spencer9c0696f2007-02-20 08:51:03 +0000934 else
Jeff Cohen09dfd8e2007-03-20 20:42:36 +0000935 return -std::numeric_limits<double>::infinity();
Reid Spencer9c0696f2007-02-20 08:51:03 +0000936 }
937 exp += 1023; // Increment for 1023 bias
938
939 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
940 // extract the high 52 bits from the correct words in pVal.
Zhou Shengd93f00c2007-02-12 20:02:55 +0000941 uint64_t mantissa;
Reid Spencer9c0696f2007-02-20 08:51:03 +0000942 unsigned hiWord = whichWord(n-1);
943 if (hiWord == 0) {
944 mantissa = Tmp.pVal[0];
945 if (n > 52)
946 mantissa >>= n - 52; // shift down, we want the top 52 bits.
947 } else {
948 assert(hiWord > 0 && "huh?");
949 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
950 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
951 mantissa = hibits | lobits;
952 }
953
Zhou Shengd93f00c2007-02-12 20:02:55 +0000954 // The leading bit of mantissa is implicit, so get rid of it.
Reid Spencer443b5702007-02-18 00:44:22 +0000955 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
Zhou Shengd93f00c2007-02-12 20:02:55 +0000956 union {
957 double D;
958 uint64_t I;
959 } T;
960 T.I = sign | (exp << 52) | mantissa;
961 return T.D;
962}
963
Reid Spencere81d2da2007-02-16 22:36:51 +0000964// Truncate to new width.
Reid Spencer94900772007-02-28 17:34:32 +0000965APInt &APInt::trunc(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000966 assert(width < BitWidth && "Invalid APInt Truncate request");
Reid Spencer9af18872007-12-11 06:53:58 +0000967 assert(width >= MIN_INT_BITS && "Can't truncate to 0 bits");
Reid Spencer9eec2412007-02-25 23:44:53 +0000968 uint32_t wordsBefore = getNumWords();
969 BitWidth = width;
970 uint32_t wordsAfter = getNumWords();
971 if (wordsBefore != wordsAfter) {
972 if (wordsAfter == 1) {
973 uint64_t *tmp = pVal;
974 VAL = pVal[0];
Reid Spencer9ac44112007-02-26 23:38:21 +0000975 delete [] tmp;
Reid Spencer9eec2412007-02-25 23:44:53 +0000976 } else {
977 uint64_t *newVal = getClearedMemory(wordsAfter);
978 for (uint32_t i = 0; i < wordsAfter; ++i)
979 newVal[i] = pVal[i];
Reid Spencer9ac44112007-02-26 23:38:21 +0000980 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +0000981 pVal = newVal;
982 }
983 }
Reid Spencer94900772007-02-28 17:34:32 +0000984 return clearUnusedBits();
Reid Spencere81d2da2007-02-16 22:36:51 +0000985}
986
987// Sign extend to a new width.
Reid Spencer94900772007-02-28 17:34:32 +0000988APInt &APInt::sext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +0000989 assert(width > BitWidth && "Invalid APInt SignExtend request");
Reid Spencer9af18872007-12-11 06:53:58 +0000990 assert(width <= MAX_INT_BITS && "Too many bits");
Reid Spencer9eec2412007-02-25 23:44:53 +0000991 // If the sign bit isn't set, this is the same as zext.
Reid Spencer47fbe9e2007-02-26 07:44:38 +0000992 if (!isNegative()) {
Reid Spencer9eec2412007-02-25 23:44:53 +0000993 zext(width);
Reid Spencer94900772007-02-28 17:34:32 +0000994 return *this;
Reid Spencer9eec2412007-02-25 23:44:53 +0000995 }
996
997 // The sign bit is set. First, get some facts
998 uint32_t wordsBefore = getNumWords();
999 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
1000 BitWidth = width;
1001 uint32_t wordsAfter = getNumWords();
1002
1003 // Mask the high order word appropriately
1004 if (wordsBefore == wordsAfter) {
1005 uint32_t newWordBits = width % APINT_BITS_PER_WORD;
1006 // The extension is contained to the wordsBefore-1th word.
Reid Spencer36184ed2007-03-02 01:19:42 +00001007 uint64_t mask = ~0ULL;
1008 if (newWordBits)
1009 mask >>= APINT_BITS_PER_WORD - newWordBits;
1010 mask <<= wordBits;
Reid Spencer9eec2412007-02-25 23:44:53 +00001011 if (wordsBefore == 1)
1012 VAL |= mask;
1013 else
1014 pVal[wordsBefore-1] |= mask;
Reid Spencer295e40a2007-03-01 23:30:25 +00001015 return clearUnusedBits();
Reid Spencer9eec2412007-02-25 23:44:53 +00001016 }
1017
Reid Spencerf30b1882007-02-25 23:54:00 +00001018 uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits;
Reid Spencer9eec2412007-02-25 23:44:53 +00001019 uint64_t *newVal = getMemory(wordsAfter);
1020 if (wordsBefore == 1)
1021 newVal[0] = VAL | mask;
1022 else {
1023 for (uint32_t i = 0; i < wordsBefore; ++i)
1024 newVal[i] = pVal[i];
1025 newVal[wordsBefore-1] |= mask;
1026 }
1027 for (uint32_t i = wordsBefore; i < wordsAfter; i++)
1028 newVal[i] = -1ULL;
1029 if (wordsBefore != 1)
Reid Spencer9ac44112007-02-26 23:38:21 +00001030 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +00001031 pVal = newVal;
Reid Spencer94900772007-02-28 17:34:32 +00001032 return clearUnusedBits();
Reid Spencere81d2da2007-02-16 22:36:51 +00001033}
1034
1035// Zero extend to a new width.
Reid Spencer94900772007-02-28 17:34:32 +00001036APInt &APInt::zext(uint32_t width) {
Reid Spencere81d2da2007-02-16 22:36:51 +00001037 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
Reid Spencer9af18872007-12-11 06:53:58 +00001038 assert(width <= MAX_INT_BITS && "Too many bits");
Reid Spencer9eec2412007-02-25 23:44:53 +00001039 uint32_t wordsBefore = getNumWords();
1040 BitWidth = width;
1041 uint32_t wordsAfter = getNumWords();
1042 if (wordsBefore != wordsAfter) {
1043 uint64_t *newVal = getClearedMemory(wordsAfter);
1044 if (wordsBefore == 1)
1045 newVal[0] = VAL;
1046 else
1047 for (uint32_t i = 0; i < wordsBefore; ++i)
1048 newVal[i] = pVal[i];
1049 if (wordsBefore != 1)
Reid Spencer9ac44112007-02-26 23:38:21 +00001050 delete [] pVal;
Reid Spencer9eec2412007-02-25 23:44:53 +00001051 pVal = newVal;
1052 }
Reid Spencer94900772007-02-28 17:34:32 +00001053 return *this;
Reid Spencere81d2da2007-02-16 22:36:51 +00001054}
1055
Reid Spencer68e23002007-03-01 17:15:32 +00001056APInt &APInt::zextOrTrunc(uint32_t width) {
1057 if (BitWidth < width)
1058 return zext(width);
1059 if (BitWidth > width)
1060 return trunc(width);
1061 return *this;
1062}
1063
1064APInt &APInt::sextOrTrunc(uint32_t width) {
1065 if (BitWidth < width)
1066 return sext(width);
1067 if (BitWidth > width)
1068 return trunc(width);
1069 return *this;
1070}
1071
Zhou Shengff4304f2007-02-09 07:48:24 +00001072/// Arithmetic right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001073/// @brief Arithmetic right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001074APInt APInt::ashr(uint32_t shiftAmt) const {
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001075 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Reid Spencer46f9c942007-03-02 22:39:11 +00001076 // Handle a degenerate case
1077 if (shiftAmt == 0)
1078 return *this;
1079
1080 // Handle single word shifts with built-in ashr
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001081 if (isSingleWord()) {
1082 if (shiftAmt == BitWidth)
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001083 return APInt(BitWidth, 0); // undefined
1084 else {
1085 uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth;
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001086 return APInt(BitWidth,
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001087 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1088 }
Zhou Sheng0b706b12007-02-08 14:35:19 +00001089 }
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001090
Reid Spencer46f9c942007-03-02 22:39:11 +00001091 // If all the bits were shifted out, the result is, technically, undefined.
1092 // We return -1 if it was negative, 0 otherwise. We check this early to avoid
1093 // issues in the algorithm below.
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001094 if (shiftAmt == BitWidth) {
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001095 if (isNegative())
1096 return APInt(BitWidth, -1ULL);
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001097 else
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001098 return APInt(BitWidth, 0);
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001099 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001100
1101 // Create some space for the result.
1102 uint64_t * val = new uint64_t[getNumWords()];
1103
Reid Spencer46f9c942007-03-02 22:39:11 +00001104 // Compute some values needed by the following shift algorithms
1105 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
1106 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
1107 uint32_t breakWord = getNumWords() - 1 - offset; // last word affected
1108 uint32_t bitsInWord = whichBit(BitWidth); // how many bits in last word?
1109 if (bitsInWord == 0)
1110 bitsInWord = APINT_BITS_PER_WORD;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001111
1112 // If we are shifting whole words, just move whole words
1113 if (wordShift == 0) {
Reid Spencer46f9c942007-03-02 22:39:11 +00001114 // Move the words containing significant bits
1115 for (uint32_t i = 0; i <= breakWord; ++i)
1116 val[i] = pVal[i+offset]; // move whole word
1117
1118 // Adjust the top significant word for sign bit fill, if negative
1119 if (isNegative())
1120 if (bitsInWord < APINT_BITS_PER_WORD)
1121 val[breakWord] |= ~0ULL << bitsInWord; // set high bits
1122 } else {
1123 // Shift the low order words
1124 for (uint32_t i = 0; i < breakWord; ++i) {
1125 // This combines the shifted corresponding word with the low bits from
1126 // the next word (shifted into this word's high bits).
1127 val[i] = (pVal[i+offset] >> wordShift) |
1128 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1129 }
1130
1131 // Shift the break word. In this case there are no bits from the next word
1132 // to include in this word.
1133 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1134
1135 // Deal with sign extenstion in the break word, and possibly the word before
1136 // it.
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001137 if (isNegative()) {
Reid Spencer46f9c942007-03-02 22:39:11 +00001138 if (wordShift > bitsInWord) {
1139 if (breakWord > 0)
1140 val[breakWord-1] |=
1141 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1142 val[breakWord] |= ~0ULL;
1143 } else
1144 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001145 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001146 }
1147
Reid Spencer46f9c942007-03-02 22:39:11 +00001148 // Remaining words are 0 or -1, just assign them.
1149 uint64_t fillValue = (isNegative() ? -1ULL : 0);
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001150 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
Reid Spencer46f9c942007-03-02 22:39:11 +00001151 val[i] = fillValue;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001152 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001153}
1154
Zhou Shengff4304f2007-02-09 07:48:24 +00001155/// Logical right-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001156/// @brief Logical right-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001157APInt APInt::lshr(uint32_t shiftAmt) const {
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001158 if (isSingleWord()) {
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001159 if (shiftAmt == BitWidth)
1160 return APInt(BitWidth, 0);
1161 else
1162 return APInt(BitWidth, this->VAL >> shiftAmt);
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001163 }
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001164
Reid Spencerba81c2b2007-02-26 01:19:48 +00001165 // If all the bits were shifted out, the result is 0. This avoids issues
1166 // with shifting by the size of the integer type, which produces undefined
1167 // results. We define these "undefined results" to always be 0.
1168 if (shiftAmt == BitWidth)
1169 return APInt(BitWidth, 0);
1170
Reid Spencer02ae8b72007-05-17 06:26:29 +00001171 // If none of the bits are shifted out, the result is *this. This avoids
1172 // issues with shifting byt he size of the integer type, which produces
1173 // undefined results in the code below. This is also an optimization.
1174 if (shiftAmt == 0)
1175 return *this;
1176
Reid Spencerba81c2b2007-02-26 01:19:48 +00001177 // Create some space for the result.
1178 uint64_t * val = new uint64_t[getNumWords()];
1179
1180 // If we are shifting less than a word, compute the shift with a simple carry
1181 if (shiftAmt < APINT_BITS_PER_WORD) {
1182 uint64_t carry = 0;
1183 for (int i = getNumWords()-1; i >= 0; --i) {
Reid Spenceraf8fb192007-03-01 05:39:56 +00001184 val[i] = (pVal[i] >> shiftAmt) | carry;
Reid Spencerba81c2b2007-02-26 01:19:48 +00001185 carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt);
1186 }
1187 return APInt(val, BitWidth).clearUnusedBits();
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001188 }
1189
Reid Spencerba81c2b2007-02-26 01:19:48 +00001190 // Compute some values needed by the remaining shift algorithms
1191 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1192 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1193
1194 // If we are shifting whole words, just move whole words
1195 if (wordShift == 0) {
1196 for (uint32_t i = 0; i < getNumWords() - offset; ++i)
1197 val[i] = pVal[i+offset];
1198 for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++)
1199 val[i] = 0;
1200 return APInt(val,BitWidth).clearUnusedBits();
1201 }
1202
1203 // Shift the low order words
1204 uint32_t breakWord = getNumWords() - offset -1;
1205 for (uint32_t i = 0; i < breakWord; ++i)
Reid Spenceraf8fb192007-03-01 05:39:56 +00001206 val[i] = (pVal[i+offset] >> wordShift) |
1207 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
Reid Spencerba81c2b2007-02-26 01:19:48 +00001208 // Shift the break word.
1209 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1210
1211 // Remaining words are 0
1212 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
1213 val[i] = 0;
1214 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001215}
1216
Zhou Shengff4304f2007-02-09 07:48:24 +00001217/// Left-shift this APInt by shiftAmt.
Zhou Sheng0b706b12007-02-08 14:35:19 +00001218/// @brief Left-shift function.
Reid Spenceraf0e9562007-02-18 18:38:44 +00001219APInt APInt::shl(uint32_t shiftAmt) const {
Reid Spencer5bce8542007-02-24 20:19:37 +00001220 assert(shiftAmt <= BitWidth && "Invalid shift amount");
Reid Spencer87553802007-02-25 00:56:44 +00001221 if (isSingleWord()) {
Reid Spencer5bce8542007-02-24 20:19:37 +00001222 if (shiftAmt == BitWidth)
Reid Spencer87553802007-02-25 00:56:44 +00001223 return APInt(BitWidth, 0); // avoid undefined shift results
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001224 return APInt(BitWidth, VAL << shiftAmt);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001225 }
Reid Spencer5bce8542007-02-24 20:19:37 +00001226
Reid Spencer87553802007-02-25 00:56:44 +00001227 // If all the bits were shifted out, the result is 0. This avoids issues
1228 // with shifting by the size of the integer type, which produces undefined
1229 // results. We define these "undefined results" to always be 0.
1230 if (shiftAmt == BitWidth)
1231 return APInt(BitWidth, 0);
1232
Reid Spencer92c72832007-05-12 18:01:57 +00001233 // If none of the bits are shifted out, the result is *this. This avoids a
1234 // lshr by the words size in the loop below which can produce incorrect
1235 // results. It also avoids the expensive computation below for a common case.
1236 if (shiftAmt == 0)
1237 return *this;
1238
Reid Spencer87553802007-02-25 00:56:44 +00001239 // Create some space for the result.
1240 uint64_t * val = new uint64_t[getNumWords()];
1241
1242 // If we are shifting less than a word, do it the easy way
1243 if (shiftAmt < APINT_BITS_PER_WORD) {
1244 uint64_t carry = 0;
Reid Spencer87553802007-02-25 00:56:44 +00001245 for (uint32_t i = 0; i < getNumWords(); i++) {
1246 val[i] = pVal[i] << shiftAmt | carry;
1247 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1248 }
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001249 return APInt(val, BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +00001250 }
1251
Reid Spencer87553802007-02-25 00:56:44 +00001252 // Compute some values needed by the remaining shift algorithms
1253 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1254 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1255
1256 // If we are shifting whole words, just move whole words
1257 if (wordShift == 0) {
1258 for (uint32_t i = 0; i < offset; i++)
1259 val[i] = 0;
1260 for (uint32_t i = offset; i < getNumWords(); i++)
1261 val[i] = pVal[i-offset];
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001262 return APInt(val,BitWidth).clearUnusedBits();
Reid Spencer5bce8542007-02-24 20:19:37 +00001263 }
Reid Spencer87553802007-02-25 00:56:44 +00001264
1265 // Copy whole words from this to Result.
1266 uint32_t i = getNumWords() - 1;
1267 for (; i > offset; --i)
1268 val[i] = pVal[i-offset] << wordShift |
1269 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
Reid Spencer438d71e2007-02-25 01:08:58 +00001270 val[offset] = pVal[0] << wordShift;
Reid Spencer87553802007-02-25 00:56:44 +00001271 for (i = 0; i < offset; ++i)
1272 val[i] = 0;
Reid Spencer5d0d05c2007-02-25 19:32:03 +00001273 return APInt(val, BitWidth).clearUnusedBits();
Zhou Sheng0b706b12007-02-08 14:35:19 +00001274}
1275
Reid Spencer19dc32a2007-05-13 23:44:59 +00001276APInt APInt::rotl(uint32_t rotateAmt) const {
Reid Spencer69944e82007-05-14 00:15:28 +00001277 if (rotateAmt == 0)
1278 return *this;
Reid Spencer19dc32a2007-05-13 23:44:59 +00001279 // Don't get too fancy, just use existing shift/or facilities
1280 APInt hi(*this);
1281 APInt lo(*this);
1282 hi.shl(rotateAmt);
1283 lo.lshr(BitWidth - rotateAmt);
1284 return hi | lo;
1285}
1286
1287APInt APInt::rotr(uint32_t rotateAmt) const {
Reid Spencer69944e82007-05-14 00:15:28 +00001288 if (rotateAmt == 0)
1289 return *this;
Reid Spencer19dc32a2007-05-13 23:44:59 +00001290 // Don't get too fancy, just use existing shift/or facilities
1291 APInt hi(*this);
1292 APInt lo(*this);
1293 lo.lshr(rotateAmt);
1294 hi.shl(BitWidth - rotateAmt);
1295 return hi | lo;
1296}
Reid Spenceraf8fb192007-03-01 05:39:56 +00001297
1298// Square Root - this method computes and returns the square root of "this".
1299// Three mechanisms are used for computation. For small values (<= 5 bits),
1300// a table lookup is done. This gets some performance for common cases. For
1301// values using less than 52 bits, the value is converted to double and then
1302// the libc sqrt function is called. The result is rounded and then converted
1303// back to a uint64_t which is then used to construct the result. Finally,
1304// the Babylonian method for computing square roots is used.
1305APInt APInt::sqrt() const {
1306
1307 // Determine the magnitude of the value.
1308 uint32_t magnitude = getActiveBits();
1309
1310 // Use a fast table for some small values. This also gets rid of some
1311 // rounding errors in libc sqrt for small values.
1312 if (magnitude <= 5) {
Reid Spencer4e1e87f2007-03-01 17:47:31 +00001313 static const uint8_t results[32] = {
Reid Spencerb5ca2cd2007-03-01 06:23:32 +00001314 /* 0 */ 0,
1315 /* 1- 2 */ 1, 1,
1316 /* 3- 6 */ 2, 2, 2, 2,
1317 /* 7-12 */ 3, 3, 3, 3, 3, 3,
1318 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1319 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1320 /* 31 */ 6
1321 };
1322 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
Reid Spenceraf8fb192007-03-01 05:39:56 +00001323 }
1324
1325 // If the magnitude of the value fits in less than 52 bits (the precision of
1326 // an IEEE double precision floating point value), then we can use the
1327 // libc sqrt function which will probably use a hardware sqrt computation.
1328 // This should be faster than the algorithm below.
Jeff Cohenca5183d2007-03-05 00:00:42 +00001329 if (magnitude < 52) {
1330#ifdef _MSC_VER
1331 // Amazingly, VC++ doesn't have round().
1332 return APInt(BitWidth,
1333 uint64_t(::sqrt(double(isSingleWord()?VAL:pVal[0]))) + 0.5);
1334#else
Reid Spenceraf8fb192007-03-01 05:39:56 +00001335 return APInt(BitWidth,
1336 uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
Jeff Cohenca5183d2007-03-05 00:00:42 +00001337#endif
1338 }
Reid Spenceraf8fb192007-03-01 05:39:56 +00001339
1340 // Okay, all the short cuts are exhausted. We must compute it. The following
1341 // is a classical Babylonian method for computing the square root. This code
1342 // was adapted to APINt from a wikipedia article on such computations.
1343 // See http://www.wikipedia.org/ and go to the page named
1344 // Calculate_an_integer_square_root.
1345 uint32_t nbits = BitWidth, i = 4;
1346 APInt testy(BitWidth, 16);
1347 APInt x_old(BitWidth, 1);
1348 APInt x_new(BitWidth, 0);
1349 APInt two(BitWidth, 2);
1350
1351 // Select a good starting value using binary logarithms.
1352 for (;; i += 2, testy = testy.shl(2))
1353 if (i >= nbits || this->ule(testy)) {
1354 x_old = x_old.shl(i / 2);
1355 break;
1356 }
1357
1358 // Use the Babylonian method to arrive at the integer square root:
1359 for (;;) {
1360 x_new = (this->udiv(x_old) + x_old).udiv(two);
1361 if (x_old.ule(x_new))
1362 break;
1363 x_old = x_new;
1364 }
1365
1366 // Make sure we return the closest approximation
Reid Spencerf09aef72007-03-02 04:21:55 +00001367 // NOTE: The rounding calculation below is correct. It will produce an
1368 // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1369 // determined to be a rounding issue with pari/gp as it begins to use a
1370 // floating point representation after 192 bits. There are no discrepancies
1371 // between this algorithm and pari/gp for bit widths < 192 bits.
Reid Spenceraf8fb192007-03-01 05:39:56 +00001372 APInt square(x_old * x_old);
1373 APInt nextSquare((x_old + 1) * (x_old +1));
1374 if (this->ult(square))
1375 return x_old;
Reid Spencerf09aef72007-03-02 04:21:55 +00001376 else if (this->ule(nextSquare)) {
1377 APInt midpoint((nextSquare - square).udiv(two));
1378 APInt offset(*this - square);
1379 if (offset.ult(midpoint))
Reid Spenceraf8fb192007-03-01 05:39:56 +00001380 return x_old;
Reid Spencerf09aef72007-03-02 04:21:55 +00001381 else
1382 return x_old + 1;
1383 } else
Reid Spenceraf8fb192007-03-01 05:39:56 +00001384 assert(0 && "Error in APInt::sqrt computation");
1385 return x_old + 1;
1386}
1387
Reid Spencer9c0696f2007-02-20 08:51:03 +00001388/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1389/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1390/// variables here have the same names as in the algorithm. Comments explain
1391/// the algorithm and any deviation from it.
1392static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
1393 uint32_t m, uint32_t n) {
1394 assert(u && "Must provide dividend");
1395 assert(v && "Must provide divisor");
1396 assert(q && "Must provide quotient");
Reid Spencer9d6c9192007-02-24 03:58:46 +00001397 assert(u != v && u != q && v != q && "Must us different memory");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001398 assert(n>1 && "n must be > 1");
1399
1400 // Knuth uses the value b as the base of the number system. In our case b
1401 // is 2^31 so we just set it to -1u.
1402 uint64_t b = uint64_t(1) << 32;
1403
Reid Spencer9d6c9192007-02-24 03:58:46 +00001404 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n');
1405 DEBUG(cerr << "KnuthDiv: original:");
1406 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1407 DEBUG(cerr << " by");
1408 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1409 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001410 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1411 // u and v by d. Note that we have taken Knuth's advice here to use a power
1412 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1413 // 2 allows us to shift instead of multiply and it is easy to determine the
1414 // shift amount from the leading zeros. We are basically normalizing the u
1415 // and v so that its high bits are shifted to the top of v's range without
1416 // overflow. Note that this can require an extra word in u so that u must
1417 // be of length m+n+1.
1418 uint32_t shift = CountLeadingZeros_32(v[n-1]);
1419 uint32_t v_carry = 0;
1420 uint32_t u_carry = 0;
1421 if (shift) {
1422 for (uint32_t i = 0; i < m+n; ++i) {
1423 uint32_t u_tmp = u[i] >> (32 - shift);
1424 u[i] = (u[i] << shift) | u_carry;
1425 u_carry = u_tmp;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001426 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001427 for (uint32_t i = 0; i < n; ++i) {
1428 uint32_t v_tmp = v[i] >> (32 - shift);
1429 v[i] = (v[i] << shift) | v_carry;
1430 v_carry = v_tmp;
1431 }
1432 }
1433 u[m+n] = u_carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001434 DEBUG(cerr << "KnuthDiv: normal:");
1435 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1436 DEBUG(cerr << " by");
1437 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1438 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001439
1440 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1441 int j = m;
1442 do {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001443 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001444 // D3. [Calculate q'.].
1445 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1446 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1447 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1448 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1449 // on v[n-2] determines at high speed most of the cases in which the trial
1450 // value qp is one too large, and it eliminates all cases where qp is two
1451 // too large.
Reid Spencer92904632007-02-23 01:57:13 +00001452 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001453 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n');
Reid Spencer92904632007-02-23 01:57:13 +00001454 uint64_t qp = dividend / v[n-1];
1455 uint64_t rp = dividend % v[n-1];
Reid Spencer9c0696f2007-02-20 08:51:03 +00001456 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1457 qp--;
1458 rp += v[n-1];
Reid Spencer610fad82007-02-24 10:01:42 +00001459 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
Reid Spencer9d6c9192007-02-24 03:58:46 +00001460 qp--;
Reid Spencer92904632007-02-23 01:57:13 +00001461 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001462 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001463
Reid Spencer92904632007-02-23 01:57:13 +00001464 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1465 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1466 // consists of a simple multiplication by a one-place number, combined with
Reid Spencer610fad82007-02-24 10:01:42 +00001467 // a subtraction.
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001468 bool isNeg = false;
Reid Spencer92904632007-02-23 01:57:13 +00001469 for (uint32_t i = 0; i < n; ++i) {
Reid Spencer610fad82007-02-24 10:01:42 +00001470 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
Reid Spencer9d6c9192007-02-24 03:58:46 +00001471 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
Reid Spencer610fad82007-02-24 10:01:42 +00001472 bool borrow = subtrahend > u_tmp;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001473 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp
Reid Spencer610fad82007-02-24 10:01:42 +00001474 << ", subtrahend == " << subtrahend
1475 << ", borrow = " << borrow << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001476
Reid Spencer610fad82007-02-24 10:01:42 +00001477 uint64_t result = u_tmp - subtrahend;
1478 uint32_t k = j + i;
1479 u[k++] = result & (b-1); // subtract low word
1480 u[k++] = result >> 32; // subtract high word
1481 while (borrow && k <= m+n) { // deal with borrow to the left
1482 borrow = u[k] == 0;
1483 u[k]--;
1484 k++;
1485 }
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001486 isNeg |= borrow;
Reid Spencer610fad82007-02-24 10:01:42 +00001487 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " <<
1488 u[j+i+1] << '\n');
Reid Spencer9d6c9192007-02-24 03:58:46 +00001489 }
1490 DEBUG(cerr << "KnuthDiv: after subtraction:");
1491 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1492 DEBUG(cerr << '\n');
Reid Spencer610fad82007-02-24 10:01:42 +00001493 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1494 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1495 // true value plus b**(n+1), namely as the b's complement of
Reid Spencer92904632007-02-23 01:57:13 +00001496 // the true value, and a "borrow" to the left should be remembered.
1497 //
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001498 if (isNeg) {
Reid Spencer610fad82007-02-24 10:01:42 +00001499 bool carry = true; // true because b's complement is "complement + 1"
1500 for (uint32_t i = 0; i <= m+n; ++i) {
1501 u[i] = ~u[i] + carry; // b's complement
1502 carry = carry && u[i] == 0;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001503 }
Reid Spencer92904632007-02-23 01:57:13 +00001504 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001505 DEBUG(cerr << "KnuthDiv: after complement:");
1506 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1507 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001508
1509 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1510 // negative, go to step D6; otherwise go on to step D7.
1511 q[j] = qp;
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001512 if (isNeg) {
Reid Spencer9c0696f2007-02-20 08:51:03 +00001513 // D6. [Add back]. The probability that this step is necessary is very
1514 // small, on the order of only 2/b. Make sure that test data accounts for
Reid Spencer92904632007-02-23 01:57:13 +00001515 // this possibility. Decrease q[j] by 1
1516 q[j]--;
1517 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1518 // A carry will occur to the left of u[j+n], and it should be ignored
1519 // since it cancels with the borrow that occurred in D4.
1520 bool carry = false;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001521 for (uint32_t i = 0; i < n; i++) {
Reid Spencer9d6c9192007-02-24 03:58:46 +00001522 uint32_t limit = std::min(u[j+i],v[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001523 u[j+i] += v[i] + carry;
Reid Spencer9d6c9192007-02-24 03:58:46 +00001524 carry = u[j+i] < limit || (carry && u[j+i] == limit);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001525 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001526 u[j+n] += carry;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001527 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001528 DEBUG(cerr << "KnuthDiv: after correction:");
1529 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]);
1530 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001531
Reid Spencer92904632007-02-23 01:57:13 +00001532 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1533 } while (--j >= 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001534
Reid Spencer9d6c9192007-02-24 03:58:46 +00001535 DEBUG(cerr << "KnuthDiv: quotient:");
1536 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]);
1537 DEBUG(cerr << '\n');
1538
Reid Spencer9c0696f2007-02-20 08:51:03 +00001539 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1540 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1541 // compute the remainder (urem uses this).
1542 if (r) {
1543 // The value d is expressed by the "shift" value above since we avoided
1544 // multiplication by d by using a shift left. So, all we have to do is
1545 // shift right here. In order to mak
Reid Spencer1050ec52007-02-24 20:38:01 +00001546 if (shift) {
1547 uint32_t carry = 0;
1548 DEBUG(cerr << "KnuthDiv: remainder:");
1549 for (int i = n-1; i >= 0; i--) {
1550 r[i] = (u[i] >> shift) | carry;
1551 carry = u[i] << (32 - shift);
1552 DEBUG(cerr << " " << r[i]);
1553 }
1554 } else {
1555 for (int i = n-1; i >= 0; i--) {
1556 r[i] = u[i];
1557 DEBUG(cerr << " " << r[i]);
1558 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001559 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001560 DEBUG(cerr << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001561 }
Reid Spencer9d6c9192007-02-24 03:58:46 +00001562 DEBUG(cerr << std::setbase(10) << '\n');
Reid Spencer9c0696f2007-02-20 08:51:03 +00001563}
1564
Reid Spencer9c0696f2007-02-20 08:51:03 +00001565void APInt::divide(const APInt LHS, uint32_t lhsWords,
1566 const APInt &RHS, uint32_t rhsWords,
1567 APInt *Quotient, APInt *Remainder)
1568{
1569 assert(lhsWords >= rhsWords && "Fractional result");
1570
1571 // First, compose the values into an array of 32-bit words instead of
1572 // 64-bit words. This is a necessity of both the "short division" algorithm
1573 // and the the Knuth "classical algorithm" which requires there to be native
1574 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1575 // can't use 64-bit operands here because we don't have native results of
1576 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't
1577 // work on large-endian machines.
1578 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8);
1579 uint32_t n = rhsWords * 2;
1580 uint32_t m = (lhsWords * 2) - n;
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001581
1582 // Allocate space for the temporary values we need either on the stack, if
1583 // it will fit, or on the heap if it won't.
1584 uint32_t SPACE[128];
1585 uint32_t *U = 0;
1586 uint32_t *V = 0;
1587 uint32_t *Q = 0;
1588 uint32_t *R = 0;
1589 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1590 U = &SPACE[0];
1591 V = &SPACE[m+n+1];
1592 Q = &SPACE[(m+n+1) + n];
1593 if (Remainder)
1594 R = &SPACE[(m+n+1) + n + (m+n)];
1595 } else {
1596 U = new uint32_t[m + n + 1];
1597 V = new uint32_t[n];
1598 Q = new uint32_t[m+n];
1599 if (Remainder)
1600 R = new uint32_t[n];
1601 }
1602
1603 // Initialize the dividend
Reid Spencer9c0696f2007-02-20 08:51:03 +00001604 memset(U, 0, (m+n+1)*sizeof(uint32_t));
1605 for (unsigned i = 0; i < lhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001606 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001607 U[i * 2] = tmp & mask;
1608 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1609 }
1610 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1611
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001612 // Initialize the divisor
Reid Spencer9c0696f2007-02-20 08:51:03 +00001613 memset(V, 0, (n)*sizeof(uint32_t));
1614 for (unsigned i = 0; i < rhsWords; ++i) {
Reid Spencer15aab8a2007-02-22 00:58:45 +00001615 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001616 V[i * 2] = tmp & mask;
1617 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1618 }
1619
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001620 // initialize the quotient and remainder
Reid Spencer9c0696f2007-02-20 08:51:03 +00001621 memset(Q, 0, (m+n) * sizeof(uint32_t));
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001622 if (Remainder)
Reid Spencer9c0696f2007-02-20 08:51:03 +00001623 memset(R, 0, n * sizeof(uint32_t));
Reid Spencer9c0696f2007-02-20 08:51:03 +00001624
1625 // Now, adjust m and n for the Knuth division. n is the number of words in
1626 // the divisor. m is the number of words by which the dividend exceeds the
1627 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1628 // contain any zero words or the Knuth algorithm fails.
1629 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1630 n--;
1631 m++;
1632 }
1633 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1634 m--;
1635
1636 // If we're left with only a single word for the divisor, Knuth doesn't work
1637 // so we implement the short division algorithm here. This is much simpler
1638 // and faster because we are certain that we can divide a 64-bit quantity
1639 // by a 32-bit quantity at hardware speed and short division is simply a
1640 // series of such operations. This is just like doing short division but we
1641 // are using base 2^32 instead of base 10.
1642 assert(n != 0 && "Divide by zero?");
1643 if (n == 1) {
1644 uint32_t divisor = V[0];
1645 uint32_t remainder = 0;
1646 for (int i = m+n-1; i >= 0; i--) {
1647 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1648 if (partial_dividend == 0) {
1649 Q[i] = 0;
1650 remainder = 0;
1651 } else if (partial_dividend < divisor) {
1652 Q[i] = 0;
1653 remainder = partial_dividend;
1654 } else if (partial_dividend == divisor) {
1655 Q[i] = 1;
1656 remainder = 0;
1657 } else {
1658 Q[i] = partial_dividend / divisor;
1659 remainder = partial_dividend - (Q[i] * divisor);
1660 }
1661 }
1662 if (R)
1663 R[0] = remainder;
1664 } else {
1665 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1666 // case n > 1.
1667 KnuthDiv(U, V, Q, R, m, n);
1668 }
1669
1670 // If the caller wants the quotient
1671 if (Quotient) {
1672 // Set up the Quotient value's memory.
1673 if (Quotient->BitWidth != LHS.BitWidth) {
1674 if (Quotient->isSingleWord())
1675 Quotient->VAL = 0;
1676 else
Reid Spencer9ac44112007-02-26 23:38:21 +00001677 delete [] Quotient->pVal;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001678 Quotient->BitWidth = LHS.BitWidth;
1679 if (!Quotient->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001680 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001681 } else
1682 Quotient->clear();
1683
1684 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1685 // order words.
1686 if (lhsWords == 1) {
1687 uint64_t tmp =
1688 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1689 if (Quotient->isSingleWord())
1690 Quotient->VAL = tmp;
1691 else
1692 Quotient->pVal[0] = tmp;
1693 } else {
1694 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1695 for (unsigned i = 0; i < lhsWords; ++i)
1696 Quotient->pVal[i] =
1697 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1698 }
1699 }
1700
1701 // If the caller wants the remainder
1702 if (Remainder) {
1703 // Set up the Remainder value's memory.
1704 if (Remainder->BitWidth != RHS.BitWidth) {
1705 if (Remainder->isSingleWord())
1706 Remainder->VAL = 0;
1707 else
Reid Spencer9ac44112007-02-26 23:38:21 +00001708 delete [] Remainder->pVal;
Reid Spencer9c0696f2007-02-20 08:51:03 +00001709 Remainder->BitWidth = RHS.BitWidth;
1710 if (!Remainder->isSingleWord())
Reid Spencere0cdd332007-02-21 08:21:52 +00001711 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
Reid Spencer9c0696f2007-02-20 08:51:03 +00001712 } else
1713 Remainder->clear();
1714
1715 // The remainder is in R. Reconstitute the remainder into Remainder's low
1716 // order words.
1717 if (rhsWords == 1) {
1718 uint64_t tmp =
1719 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1720 if (Remainder->isSingleWord())
1721 Remainder->VAL = tmp;
1722 else
1723 Remainder->pVal[0] = tmp;
1724 } else {
1725 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1726 for (unsigned i = 0; i < rhsWords; ++i)
1727 Remainder->pVal[i] =
1728 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1729 }
1730 }
1731
1732 // Clean up the memory we allocated.
Reid Spencer24c4a8f2007-02-25 01:56:07 +00001733 if (U != &SPACE[0]) {
1734 delete [] U;
1735 delete [] V;
1736 delete [] Q;
1737 delete [] R;
1738 }
Reid Spencer5e0a8512007-02-17 03:16:00 +00001739}
1740
Reid Spencere81d2da2007-02-16 22:36:51 +00001741APInt APInt::udiv(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001742 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001743
1744 // First, deal with the easy case
1745 if (isSingleWord()) {
1746 assert(RHS.VAL != 0 && "Divide by zero?");
1747 return APInt(BitWidth, VAL / RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001748 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001749
Reid Spencer71bd08f2007-02-17 02:07:07 +00001750 // Get some facts about the LHS and RHS number of bits and words
Reid Spenceraf0e9562007-02-18 18:38:44 +00001751 uint32_t rhsBits = RHS.getActiveBits();
1752 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001753 assert(rhsWords && "Divided by zero???");
Reid Spencer9c0696f2007-02-20 08:51:03 +00001754 uint32_t lhsBits = this->getActiveBits();
Reid Spenceraf0e9562007-02-18 18:38:44 +00001755 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001756
1757 // Deal with some degenerate cases
1758 if (!lhsWords)
Reid Spencere0cdd332007-02-21 08:21:52 +00001759 // 0 / X ===> 0
1760 return APInt(BitWidth, 0);
1761 else if (lhsWords < rhsWords || this->ult(RHS)) {
1762 // X / Y ===> 0, iff X < Y
1763 return APInt(BitWidth, 0);
1764 } else if (*this == RHS) {
1765 // X / X ===> 1
1766 return APInt(BitWidth, 1);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001767 } else if (lhsWords == 1 && rhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001768 // All high words are zero, just use native divide
Reid Spencere0cdd332007-02-21 08:21:52 +00001769 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001770 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001771
1772 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1773 APInt Quotient(1,0); // to hold result.
1774 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0);
1775 return Quotient;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001776}
1777
Reid Spencere81d2da2007-02-16 22:36:51 +00001778APInt APInt::urem(const APInt& RHS) const {
Reid Spencercd6f2bf2007-02-17 00:18:01 +00001779 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
Reid Spencer71bd08f2007-02-17 02:07:07 +00001780 if (isSingleWord()) {
1781 assert(RHS.VAL != 0 && "Remainder by zero?");
1782 return APInt(BitWidth, VAL % RHS.VAL);
Zhou Sheng0b706b12007-02-08 14:35:19 +00001783 }
Reid Spencer71bd08f2007-02-17 02:07:07 +00001784
Reid Spencere0cdd332007-02-21 08:21:52 +00001785 // Get some facts about the LHS
1786 uint32_t lhsBits = getActiveBits();
1787 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001788
1789 // Get some facts about the RHS
Reid Spenceraf0e9562007-02-18 18:38:44 +00001790 uint32_t rhsBits = RHS.getActiveBits();
1791 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001792 assert(rhsWords && "Performing remainder operation by zero ???");
1793
Reid Spencer71bd08f2007-02-17 02:07:07 +00001794 // Check the degenerate cases
Reid Spencer9c0696f2007-02-20 08:51:03 +00001795 if (lhsWords == 0) {
Reid Spencere0cdd332007-02-21 08:21:52 +00001796 // 0 % Y ===> 0
1797 return APInt(BitWidth, 0);
1798 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1799 // X % Y ===> X, iff X < Y
1800 return *this;
1801 } else if (*this == RHS) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001802 // X % X == 0;
Reid Spencere0cdd332007-02-21 08:21:52 +00001803 return APInt(BitWidth, 0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001804 } else if (lhsWords == 1) {
Reid Spencer71bd08f2007-02-17 02:07:07 +00001805 // All high words are zero, just use native remainder
Reid Spencere0cdd332007-02-21 08:21:52 +00001806 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
Reid Spencer71bd08f2007-02-17 02:07:07 +00001807 }
Reid Spencer9c0696f2007-02-20 08:51:03 +00001808
Reid Spencer19dc32a2007-05-13 23:44:59 +00001809 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
Reid Spencer9c0696f2007-02-20 08:51:03 +00001810 APInt Remainder(1,0);
1811 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder);
1812 return Remainder;
Zhou Sheng0b706b12007-02-08 14:35:19 +00001813}
Reid Spencer5e0a8512007-02-17 03:16:00 +00001814
Reid Spencer19dc32a2007-05-13 23:44:59 +00001815void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1816 APInt &Quotient, APInt &Remainder) {
1817 // Get some size facts about the dividend and divisor
1818 uint32_t lhsBits = LHS.getActiveBits();
1819 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1820 uint32_t rhsBits = RHS.getActiveBits();
1821 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1822
1823 // Check the degenerate cases
1824 if (lhsWords == 0) {
1825 Quotient = 0; // 0 / Y ===> 0
1826 Remainder = 0; // 0 % Y ===> 0
1827 return;
1828 }
1829
1830 if (lhsWords < rhsWords || LHS.ult(RHS)) {
1831 Quotient = 0; // X / Y ===> 0, iff X < Y
1832 Remainder = LHS; // X % Y ===> X, iff X < Y
1833 return;
1834 }
1835
1836 if (LHS == RHS) {
1837 Quotient = 1; // X / X ===> 1
1838 Remainder = 0; // X % X ===> 0;
1839 return;
1840 }
1841
1842 if (lhsWords == 1 && rhsWords == 1) {
1843 // There is only one word to consider so use the native versions.
1844 if (LHS.isSingleWord()) {
1845 Quotient = APInt(LHS.getBitWidth(), LHS.VAL / RHS.VAL);
1846 Remainder = APInt(LHS.getBitWidth(), LHS.VAL % RHS.VAL);
1847 } else {
1848 Quotient = APInt(LHS.getBitWidth(), LHS.pVal[0] / RHS.pVal[0]);
1849 Remainder = APInt(LHS.getBitWidth(), LHS.pVal[0] % RHS.pVal[0]);
1850 }
1851 return;
1852 }
1853
1854 // Okay, lets do it the long way
1855 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1856}
1857
Reid Spencer385f7542007-02-21 03:55:44 +00001858void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
Reid Spencer5e0a8512007-02-17 03:16:00 +00001859 uint8_t radix) {
Reid Spencer385f7542007-02-21 03:55:44 +00001860 // Check our assumptions here
Reid Spencer5e0a8512007-02-17 03:16:00 +00001861 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1862 "Radix should be 2, 8, 10, or 16!");
Reid Spencer385f7542007-02-21 03:55:44 +00001863 assert(str && "String is null?");
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001864 bool isNeg = str[0] == '-';
1865 if (isNeg)
Reid Spencer9eec2412007-02-25 23:44:53 +00001866 str++, slen--;
Chris Lattnera5ae15e2007-05-03 18:15:36 +00001867 assert((slen <= numbits || radix != 2) && "Insufficient bit width");
1868 assert((slen*3 <= numbits || radix != 8) && "Insufficient bit width");
1869 assert((slen*4 <= numbits || radix != 16) && "Insufficient bit width");
1870 assert(((slen*64)/22 <= numbits || radix != 10) && "Insufficient bit width");
Reid Spencer385f7542007-02-21 03:55:44 +00001871
1872 // Allocate memory
1873 if (!isSingleWord())
1874 pVal = getClearedMemory(getNumWords());
1875
1876 // Figure out if we can shift instead of multiply
1877 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1878
1879 // Set up an APInt for the digit to add outside the loop so we don't
1880 // constantly construct/destruct it.
1881 APInt apdigit(getBitWidth(), 0);
1882 APInt apradix(getBitWidth(), radix);
1883
1884 // Enter digit traversal loop
1885 for (unsigned i = 0; i < slen; i++) {
1886 // Get a digit
1887 uint32_t digit = 0;
1888 char cdigit = str[i];
Reid Spencer6551dcd2007-05-16 19:18:22 +00001889 if (radix == 16) {
1890 if (!isxdigit(cdigit))
1891 assert(0 && "Invalid hex digit in string");
1892 if (isdigit(cdigit))
1893 digit = cdigit - '0';
1894 else if (cdigit >= 'a')
Reid Spencer385f7542007-02-21 03:55:44 +00001895 digit = cdigit - 'a' + 10;
1896 else if (cdigit >= 'A')
1897 digit = cdigit - 'A' + 10;
1898 else
Reid Spencer6551dcd2007-05-16 19:18:22 +00001899 assert(0 && "huh? we shouldn't get here");
1900 } else if (isdigit(cdigit)) {
1901 digit = cdigit - '0';
1902 } else {
Reid Spencer385f7542007-02-21 03:55:44 +00001903 assert(0 && "Invalid character in digit string");
Reid Spencer6551dcd2007-05-16 19:18:22 +00001904 }
Reid Spencer385f7542007-02-21 03:55:44 +00001905
Reid Spencer6551dcd2007-05-16 19:18:22 +00001906 // Shift or multiply the value by the radix
Reid Spencer385f7542007-02-21 03:55:44 +00001907 if (shift)
Reid Spencer6551dcd2007-05-16 19:18:22 +00001908 *this <<= shift;
Reid Spencer385f7542007-02-21 03:55:44 +00001909 else
1910 *this *= apradix;
1911
1912 // Add in the digit we just interpreted
Reid Spencer5bce8542007-02-24 20:19:37 +00001913 if (apdigit.isSingleWord())
1914 apdigit.VAL = digit;
1915 else
1916 apdigit.pVal[0] = digit;
Reid Spencer385f7542007-02-21 03:55:44 +00001917 *this += apdigit;
Reid Spencer5e0a8512007-02-17 03:16:00 +00001918 }
Reid Spencer9eec2412007-02-25 23:44:53 +00001919 // If its negative, put it in two's complement form
Reid Spencer47fbe9e2007-02-26 07:44:38 +00001920 if (isNeg) {
1921 (*this)--;
Reid Spencer9eec2412007-02-25 23:44:53 +00001922 this->flip();
Reid Spencer9eec2412007-02-25 23:44:53 +00001923 }
Reid Spencer5e0a8512007-02-17 03:16:00 +00001924}
Reid Spencer9c0696f2007-02-20 08:51:03 +00001925
Reid Spencer9c0696f2007-02-20 08:51:03 +00001926std::string APInt::toString(uint8_t radix, bool wantSigned) const {
1927 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1928 "Radix should be 2, 8, 10, or 16!");
1929 static const char *digits[] = {
1930 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
1931 };
1932 std::string result;
1933 uint32_t bits_used = getActiveBits();
1934 if (isSingleWord()) {
1935 char buf[65];
1936 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
1937 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
1938 if (format) {
1939 if (wantSigned) {
1940 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
1941 (APINT_BITS_PER_WORD-BitWidth);
1942 sprintf(buf, format, sextVal);
1943 } else
1944 sprintf(buf, format, VAL);
1945 } else {
1946 memset(buf, 0, 65);
1947 uint64_t v = VAL;
1948 while (bits_used) {
1949 uint32_t bit = v & 1;
1950 bits_used--;
1951 buf[bits_used] = digits[bit][0];
1952 v >>=1;
1953 }
1954 }
1955 result = buf;
1956 return result;
1957 }
1958
1959 if (radix != 10) {
Reid Spencerfb0709a2007-05-17 19:23:02 +00001960 // For the 2, 8 and 16 bit cases, we can just shift instead of divide
1961 // because the number of bits per digit (1,3 and 4 respectively) divides
1962 // equaly. We just shift until there value is zero.
1963
1964 // First, check for a zero value and just short circuit the logic below.
1965 if (*this == 0)
1966 result = "0";
1967 else {
1968 APInt tmp(*this);
1969 size_t insert_at = 0;
1970 if (wantSigned && this->isNegative()) {
1971 // They want to print the signed version and it is a negative value
1972 // Flip the bits and add one to turn it into the equivalent positive
1973 // value and put a '-' in the result.
1974 tmp.flip();
1975 tmp++;
1976 result = "-";
1977 insert_at = 1;
1978 }
1979 // Just shift tmp right for each digit width until it becomes zero
1980 uint32_t shift = (radix == 16 ? 4 : (radix == 8 ? 3 : 1));
1981 uint64_t mask = radix - 1;
1982 APInt zero(tmp.getBitWidth(), 0);
1983 while (tmp.ne(zero)) {
Reid Spencer20a4c232007-05-19 00:29:55 +00001984 unsigned digit = (tmp.isSingleWord() ? tmp.VAL : tmp.pVal[0]) & mask;
Reid Spencerfb0709a2007-05-17 19:23:02 +00001985 result.insert(insert_at, digits[digit]);
Reid Spencer20a4c232007-05-19 00:29:55 +00001986 tmp = tmp.lshr(shift);
Reid Spencer9c0696f2007-02-20 08:51:03 +00001987 }
1988 }
1989 return result;
1990 }
1991
1992 APInt tmp(*this);
1993 APInt divisor(4, radix);
1994 APInt zero(tmp.getBitWidth(), 0);
1995 size_t insert_at = 0;
1996 if (wantSigned && tmp[BitWidth-1]) {
1997 // They want to print the signed version and it is a negative value
1998 // Flip the bits and add one to turn it into the equivalent positive
1999 // value and put a '-' in the result.
2000 tmp.flip();
2001 tmp++;
2002 result = "-";
2003 insert_at = 1;
2004 }
Reid Spencere549c492007-02-21 00:29:48 +00002005 if (tmp == APInt(tmp.getBitWidth(), 0))
Reid Spencer9c0696f2007-02-20 08:51:03 +00002006 result = "0";
2007 else while (tmp.ne(zero)) {
2008 APInt APdigit(1,0);
Reid Spencer9c0696f2007-02-20 08:51:03 +00002009 APInt tmp2(tmp.getBitWidth(), 0);
Reid Spencer385f7542007-02-21 03:55:44 +00002010 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
2011 &APdigit);
Reid Spencer794f4722007-02-26 21:02:27 +00002012 uint32_t digit = APdigit.getZExtValue();
Reid Spencer385f7542007-02-21 03:55:44 +00002013 assert(digit < radix && "divide failed");
2014 result.insert(insert_at,digits[digit]);
Reid Spencer9c0696f2007-02-20 08:51:03 +00002015 tmp = tmp2;
2016 }
2017
2018 return result;
2019}
2020
Reid Spencer385f7542007-02-21 03:55:44 +00002021void APInt::dump() const
2022{
Reid Spencer610fad82007-02-24 10:01:42 +00002023 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16);
Reid Spencer385f7542007-02-21 03:55:44 +00002024 if (isSingleWord())
Reid Spencer610fad82007-02-24 10:01:42 +00002025 cerr << VAL;
Reid Spencer385f7542007-02-21 03:55:44 +00002026 else for (unsigned i = getNumWords(); i > 0; i--) {
Reid Spencer610fad82007-02-24 10:01:42 +00002027 cerr << pVal[i-1] << " ";
Reid Spencer385f7542007-02-21 03:55:44 +00002028 }
Chris Lattner9132a2b2007-08-23 05:15:32 +00002029 cerr << " U(" << this->toStringUnsigned(10) << ") S("
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002030 << this->toStringSigned(10) << ")" << std::setbase(10);
Reid Spencer385f7542007-02-21 03:55:44 +00002031}
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002032
2033// This implements a variety of operations on a representation of
2034// arbitrary precision, two's-complement, bignum integer values.
2035
2036/* Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2037 and unrestricting assumption. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002038COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0);
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002039
2040/* Some handy functions local to this file. */
2041namespace {
2042
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002043 /* Returns the integer part with the least significant BITS set.
2044 BITS cannot be zero. */
2045 inline integerPart
2046 lowBitMask(unsigned int bits)
2047 {
2048 assert (bits != 0 && bits <= integerPartWidth);
2049
2050 return ~(integerPart) 0 >> (integerPartWidth - bits);
2051 }
2052
Neil Booth055c0b32007-10-06 00:43:45 +00002053 /* Returns the value of the lower half of PART. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002054 inline integerPart
2055 lowHalf(integerPart part)
2056 {
2057 return part & lowBitMask(integerPartWidth / 2);
2058 }
2059
Neil Booth055c0b32007-10-06 00:43:45 +00002060 /* Returns the value of the upper half of PART. */
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002061 inline integerPart
2062 highHalf(integerPart part)
2063 {
2064 return part >> (integerPartWidth / 2);
2065 }
2066
Neil Booth055c0b32007-10-06 00:43:45 +00002067 /* Returns the bit number of the most significant set bit of a part.
2068 If the input number has no bits set -1U is returned. */
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002069 unsigned int
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002070 partMSB(integerPart value)
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002071 {
2072 unsigned int n, msb;
2073
2074 if (value == 0)
2075 return -1U;
2076
2077 n = integerPartWidth / 2;
2078
2079 msb = 0;
2080 do {
2081 if (value >> n) {
2082 value >>= n;
2083 msb += n;
2084 }
2085
2086 n >>= 1;
2087 } while (n);
2088
2089 return msb;
2090 }
2091
Neil Booth055c0b32007-10-06 00:43:45 +00002092 /* Returns the bit number of the least significant set bit of a
2093 part. If the input number has no bits set -1U is returned. */
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002094 unsigned int
2095 partLSB(integerPart value)
2096 {
2097 unsigned int n, lsb;
2098
2099 if (value == 0)
2100 return -1U;
2101
2102 lsb = integerPartWidth - 1;
2103 n = integerPartWidth / 2;
2104
2105 do {
2106 if (value << n) {
2107 value <<= n;
2108 lsb -= n;
2109 }
2110
2111 n >>= 1;
2112 } while (n);
2113
2114 return lsb;
2115 }
2116}
2117
2118/* Sets the least significant part of a bignum to the input value, and
2119 zeroes out higher parts. */
2120void
2121APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts)
2122{
2123 unsigned int i;
2124
Neil Booth68e53ad2007-10-08 13:47:12 +00002125 assert (parts > 0);
2126
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002127 dst[0] = part;
2128 for(i = 1; i < parts; i++)
2129 dst[i] = 0;
2130}
2131
2132/* Assign one bignum to another. */
2133void
2134APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts)
2135{
2136 unsigned int i;
2137
2138 for(i = 0; i < parts; i++)
2139 dst[i] = src[i];
2140}
2141
2142/* Returns true if a bignum is zero, false otherwise. */
2143bool
2144APInt::tcIsZero(const integerPart *src, unsigned int parts)
2145{
2146 unsigned int i;
2147
2148 for(i = 0; i < parts; i++)
2149 if (src[i])
2150 return false;
2151
2152 return true;
2153}
2154
2155/* Extract the given bit of a bignum; returns 0 or 1. */
2156int
2157APInt::tcExtractBit(const integerPart *parts, unsigned int bit)
2158{
2159 return(parts[bit / integerPartWidth]
2160 & ((integerPart) 1 << bit % integerPartWidth)) != 0;
2161}
2162
2163/* Set the given bit of a bignum. */
2164void
2165APInt::tcSetBit(integerPart *parts, unsigned int bit)
2166{
2167 parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
2168}
2169
Neil Booth055c0b32007-10-06 00:43:45 +00002170/* Returns the bit number of the least significant set bit of a
2171 number. If the input number has no bits set -1U is returned. */
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002172unsigned int
2173APInt::tcLSB(const integerPart *parts, unsigned int n)
2174{
2175 unsigned int i, lsb;
2176
2177 for(i = 0; i < n; i++) {
2178 if (parts[i] != 0) {
2179 lsb = partLSB(parts[i]);
2180
2181 return lsb + i * integerPartWidth;
2182 }
2183 }
2184
2185 return -1U;
2186}
2187
Neil Booth055c0b32007-10-06 00:43:45 +00002188/* Returns the bit number of the most significant set bit of a number.
2189 If the input number has no bits set -1U is returned. */
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002190unsigned int
2191APInt::tcMSB(const integerPart *parts, unsigned int n)
2192{
2193 unsigned int msb;
2194
2195 do {
2196 --n;
2197
2198 if (parts[n] != 0) {
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002199 msb = partMSB(parts[n]);
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002200
2201 return msb + n * integerPartWidth;
2202 }
2203 } while (n);
2204
2205 return -1U;
2206}
2207
Neil Booth68e53ad2007-10-08 13:47:12 +00002208/* Copy the bit vector of width srcBITS from SRC, starting at bit
2209 srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
2210 the least significant bit of DST. All high bits above srcBITS in
2211 DST are zero-filled. */
2212void
2213APInt::tcExtract(integerPart *dst, unsigned int dstCount, const integerPart *src,
2214 unsigned int srcBits, unsigned int srcLSB)
2215{
2216 unsigned int firstSrcPart, dstParts, shift, n;
2217
2218 dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
2219 assert (dstParts <= dstCount);
2220
2221 firstSrcPart = srcLSB / integerPartWidth;
2222 tcAssign (dst, src + firstSrcPart, dstParts);
2223
2224 shift = srcLSB % integerPartWidth;
2225 tcShiftRight (dst, dstParts, shift);
2226
2227 /* We now have (dstParts * integerPartWidth - shift) bits from SRC
2228 in DST. If this is less that srcBits, append the rest, else
2229 clear the high bits. */
2230 n = dstParts * integerPartWidth - shift;
2231 if (n < srcBits) {
2232 integerPart mask = lowBitMask (srcBits - n);
2233 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2234 << n % integerPartWidth);
2235 } else if (n > srcBits) {
Neil Booth1e8390d2007-10-12 15:31:31 +00002236 if (srcBits % integerPartWidth)
2237 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
Neil Booth68e53ad2007-10-08 13:47:12 +00002238 }
2239
2240 /* Clear high parts. */
2241 while (dstParts < dstCount)
2242 dst[dstParts++] = 0;
2243}
2244
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002245/* DST += RHS + C where C is zero or one. Returns the carry flag. */
2246integerPart
2247APInt::tcAdd(integerPart *dst, const integerPart *rhs,
2248 integerPart c, unsigned int parts)
2249{
2250 unsigned int i;
2251
2252 assert(c <= 1);
2253
2254 for(i = 0; i < parts; i++) {
2255 integerPart l;
2256
2257 l = dst[i];
2258 if (c) {
2259 dst[i] += rhs[i] + 1;
2260 c = (dst[i] <= l);
2261 } else {
2262 dst[i] += rhs[i];
2263 c = (dst[i] < l);
2264 }
2265 }
2266
2267 return c;
2268}
2269
2270/* DST -= RHS + C where C is zero or one. Returns the carry flag. */
2271integerPart
2272APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
2273 integerPart c, unsigned int parts)
2274{
2275 unsigned int i;
2276
2277 assert(c <= 1);
2278
2279 for(i = 0; i < parts; i++) {
2280 integerPart l;
2281
2282 l = dst[i];
2283 if (c) {
2284 dst[i] -= rhs[i] + 1;
2285 c = (dst[i] >= l);
2286 } else {
2287 dst[i] -= rhs[i];
2288 c = (dst[i] > l);
2289 }
2290 }
2291
2292 return c;
2293}
2294
2295/* Negate a bignum in-place. */
2296void
2297APInt::tcNegate(integerPart *dst, unsigned int parts)
2298{
2299 tcComplement(dst, parts);
2300 tcIncrement(dst, parts);
2301}
2302
Neil Booth055c0b32007-10-06 00:43:45 +00002303/* DST += SRC * MULTIPLIER + CARRY if add is true
2304 DST = SRC * MULTIPLIER + CARRY if add is false
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002305
2306 Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
2307 they must start at the same point, i.e. DST == SRC.
2308
2309 If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2310 returned. Otherwise DST is filled with the least significant
2311 DSTPARTS parts of the result, and if all of the omitted higher
2312 parts were zero return zero, otherwise overflow occurred and
2313 return one. */
2314int
2315APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
2316 integerPart multiplier, integerPart carry,
2317 unsigned int srcParts, unsigned int dstParts,
2318 bool add)
2319{
2320 unsigned int i, n;
2321
2322 /* Otherwise our writes of DST kill our later reads of SRC. */
2323 assert(dst <= src || dst >= src + srcParts);
2324 assert(dstParts <= srcParts + 1);
2325
2326 /* N loops; minimum of dstParts and srcParts. */
2327 n = dstParts < srcParts ? dstParts: srcParts;
2328
2329 for(i = 0; i < n; i++) {
2330 integerPart low, mid, high, srcPart;
2331
2332 /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2333
2334 This cannot overflow, because
2335
2336 (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2337
2338 which is less than n^2. */
2339
2340 srcPart = src[i];
2341
2342 if (multiplier == 0 || srcPart == 0) {
2343 low = carry;
2344 high = 0;
2345 } else {
2346 low = lowHalf(srcPart) * lowHalf(multiplier);
2347 high = highHalf(srcPart) * highHalf(multiplier);
2348
2349 mid = lowHalf(srcPart) * highHalf(multiplier);
2350 high += highHalf(mid);
2351 mid <<= integerPartWidth / 2;
2352 if (low + mid < low)
2353 high++;
2354 low += mid;
2355
2356 mid = highHalf(srcPart) * lowHalf(multiplier);
2357 high += highHalf(mid);
2358 mid <<= integerPartWidth / 2;
2359 if (low + mid < low)
2360 high++;
2361 low += mid;
2362
2363 /* Now add carry. */
2364 if (low + carry < low)
2365 high++;
2366 low += carry;
2367 }
2368
2369 if (add) {
2370 /* And now DST[i], and store the new low part there. */
2371 if (low + dst[i] < low)
2372 high++;
2373 dst[i] += low;
2374 } else
2375 dst[i] = low;
2376
2377 carry = high;
2378 }
2379
2380 if (i < dstParts) {
2381 /* Full multiplication, there is no overflow. */
2382 assert(i + 1 == dstParts);
2383 dst[i] = carry;
2384 return 0;
2385 } else {
2386 /* We overflowed if there is carry. */
2387 if (carry)
2388 return 1;
2389
2390 /* We would overflow if any significant unwritten parts would be
2391 non-zero. This is true if any remaining src parts are non-zero
2392 and the multiplier is non-zero. */
2393 if (multiplier)
2394 for(; i < srcParts; i++)
2395 if (src[i])
2396 return 1;
2397
2398 /* We fitted in the narrow destination. */
2399 return 0;
2400 }
2401}
2402
2403/* DST = LHS * RHS, where DST has the same width as the operands and
2404 is filled with the least significant parts of the result. Returns
2405 one if overflow occurred, otherwise zero. DST must be disjoint
2406 from both operands. */
2407int
2408APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
2409 const integerPart *rhs, unsigned int parts)
2410{
2411 unsigned int i;
2412 int overflow;
2413
2414 assert(dst != lhs && dst != rhs);
2415
2416 overflow = 0;
2417 tcSet(dst, 0, parts);
2418
2419 for(i = 0; i < parts; i++)
2420 overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
2421 parts - i, true);
2422
2423 return overflow;
2424}
2425
Neil Booth978661d2007-10-06 00:24:48 +00002426/* DST = LHS * RHS, where DST has width the sum of the widths of the
2427 operands. No overflow occurs. DST must be disjoint from both
2428 operands. Returns the number of parts required to hold the
2429 result. */
2430unsigned int
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002431APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
Neil Booth978661d2007-10-06 00:24:48 +00002432 const integerPart *rhs, unsigned int lhsParts,
2433 unsigned int rhsParts)
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002434{
Neil Booth978661d2007-10-06 00:24:48 +00002435 /* Put the narrower number on the LHS for less loops below. */
2436 if (lhsParts > rhsParts) {
2437 return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
2438 } else {
2439 unsigned int n;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002440
Neil Booth978661d2007-10-06 00:24:48 +00002441 assert(dst != lhs && dst != rhs);
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002442
Neil Booth978661d2007-10-06 00:24:48 +00002443 tcSet(dst, 0, rhsParts);
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002444
Neil Booth978661d2007-10-06 00:24:48 +00002445 for(n = 0; n < lhsParts; n++)
2446 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true);
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002447
Neil Booth978661d2007-10-06 00:24:48 +00002448 n = lhsParts + rhsParts;
2449
2450 return n - (dst[n - 1] == 0);
2451 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002452}
2453
2454/* If RHS is zero LHS and REMAINDER are left unchanged, return one.
2455 Otherwise set LHS to LHS / RHS with the fractional part discarded,
2456 set REMAINDER to the remainder, return zero. i.e.
2457
2458 OLD_LHS = RHS * LHS + REMAINDER
2459
2460 SCRATCH is a bignum of the same size as the operands and result for
2461 use by the routine; its contents need not be initialized and are
2462 destroyed. LHS, REMAINDER and SCRATCH must be distinct.
2463*/
2464int
2465APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
2466 integerPart *remainder, integerPart *srhs,
2467 unsigned int parts)
2468{
2469 unsigned int n, shiftCount;
2470 integerPart mask;
2471
2472 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2473
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002474 shiftCount = tcMSB(rhs, parts) + 1;
2475 if (shiftCount == 0)
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002476 return true;
2477
Chris Lattnerb39cdde2007-08-20 22:49:32 +00002478 shiftCount = parts * integerPartWidth - shiftCount;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002479 n = shiftCount / integerPartWidth;
2480 mask = (integerPart) 1 << (shiftCount % integerPartWidth);
2481
2482 tcAssign(srhs, rhs, parts);
2483 tcShiftLeft(srhs, parts, shiftCount);
2484 tcAssign(remainder, lhs, parts);
2485 tcSet(lhs, 0, parts);
2486
2487 /* Loop, subtracting SRHS if REMAINDER is greater and adding that to
2488 the total. */
2489 for(;;) {
2490 int compare;
2491
2492 compare = tcCompare(remainder, srhs, parts);
2493 if (compare >= 0) {
2494 tcSubtract(remainder, srhs, 0, parts);
2495 lhs[n] |= mask;
2496 }
2497
2498 if (shiftCount == 0)
2499 break;
2500 shiftCount--;
2501 tcShiftRight(srhs, parts, 1);
2502 if ((mask >>= 1) == 0)
2503 mask = (integerPart) 1 << (integerPartWidth - 1), n--;
2504 }
2505
2506 return false;
2507}
2508
2509/* Shift a bignum left COUNT bits in-place. Shifted in bits are zero.
2510 There are no restrictions on COUNT. */
2511void
2512APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count)
2513{
Neil Booth68e53ad2007-10-08 13:47:12 +00002514 if (count) {
2515 unsigned int jump, shift;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002516
Neil Booth68e53ad2007-10-08 13:47:12 +00002517 /* Jump is the inter-part jump; shift is is intra-part shift. */
2518 jump = count / integerPartWidth;
2519 shift = count % integerPartWidth;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002520
Neil Booth68e53ad2007-10-08 13:47:12 +00002521 while (parts > jump) {
2522 integerPart part;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002523
Neil Booth68e53ad2007-10-08 13:47:12 +00002524 parts--;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002525
Neil Booth68e53ad2007-10-08 13:47:12 +00002526 /* dst[i] comes from the two parts src[i - jump] and, if we have
2527 an intra-part shift, src[i - jump - 1]. */
2528 part = dst[parts - jump];
2529 if (shift) {
2530 part <<= shift;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002531 if (parts >= jump + 1)
2532 part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
2533 }
2534
Neil Booth68e53ad2007-10-08 13:47:12 +00002535 dst[parts] = part;
2536 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002537
Neil Booth68e53ad2007-10-08 13:47:12 +00002538 while (parts > 0)
2539 dst[--parts] = 0;
2540 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002541}
2542
2543/* Shift a bignum right COUNT bits in-place. Shifted in bits are
2544 zero. There are no restrictions on COUNT. */
2545void
2546APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count)
2547{
Neil Booth68e53ad2007-10-08 13:47:12 +00002548 if (count) {
2549 unsigned int i, jump, shift;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002550
Neil Booth68e53ad2007-10-08 13:47:12 +00002551 /* Jump is the inter-part jump; shift is is intra-part shift. */
2552 jump = count / integerPartWidth;
2553 shift = count % integerPartWidth;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002554
Neil Booth68e53ad2007-10-08 13:47:12 +00002555 /* Perform the shift. This leaves the most significant COUNT bits
2556 of the result at zero. */
2557 for(i = 0; i < parts; i++) {
2558 integerPart part;
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002559
Neil Booth68e53ad2007-10-08 13:47:12 +00002560 if (i + jump >= parts) {
2561 part = 0;
2562 } else {
2563 part = dst[i + jump];
2564 if (shift) {
2565 part >>= shift;
2566 if (i + jump + 1 < parts)
2567 part |= dst[i + jump + 1] << (integerPartWidth - shift);
2568 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002569 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002570
Neil Booth68e53ad2007-10-08 13:47:12 +00002571 dst[i] = part;
2572 }
Chris Lattnerfe8e14a2007-08-16 15:56:55 +00002573 }
2574}
2575
2576/* Bitwise and of two bignums. */
2577void
2578APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts)
2579{
2580 unsigned int i;
2581
2582 for(i = 0; i < parts; i++)
2583 dst[i] &= rhs[i];
2584}
2585
2586/* Bitwise inclusive or of two bignums. */
2587void
2588APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts)
2589{
2590 unsigned int i;
2591
2592 for(i = 0; i < parts; i++)
2593 dst[i] |= rhs[i];
2594}
2595
2596/* Bitwise exclusive or of two bignums. */
2597void
2598APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts)
2599{
2600 unsigned int i;
2601
2602 for(i = 0; i < parts; i++)
2603 dst[i] ^= rhs[i];
2604}
2605
2606/* Complement a bignum in-place. */
2607void
2608APInt::tcComplement(integerPart *dst, unsigned int parts)
2609{
2610 unsigned int i;
2611
2612 for(i = 0; i < parts; i++)
2613 dst[i] = ~dst[i];
2614}
2615
2616/* Comparison (unsigned) of two bignums. */
2617int
2618APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
2619 unsigned int parts)
2620{
2621 while (parts) {
2622 parts--;
2623 if (lhs[parts] == rhs[parts])
2624 continue;
2625
2626 if (lhs[parts] > rhs[parts])
2627 return 1;
2628 else
2629 return -1;
2630 }
2631
2632 return 0;
2633}
2634
2635/* Increment a bignum in-place, return the carry flag. */
2636integerPart
2637APInt::tcIncrement(integerPart *dst, unsigned int parts)
2638{
2639 unsigned int i;
2640
2641 for(i = 0; i < parts; i++)
2642 if (++dst[i] != 0)
2643 break;
2644
2645 return i == parts;
2646}
2647
2648/* Set the least significant BITS bits of a bignum, clear the
2649 rest. */
2650void
2651APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts,
2652 unsigned int bits)
2653{
2654 unsigned int i;
2655
2656 i = 0;
2657 while (bits > integerPartWidth) {
2658 dst[i++] = ~(integerPart) 0;
2659 bits -= integerPartWidth;
2660 }
2661
2662 if (bits)
2663 dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
2664
2665 while (i < parts)
2666 dst[i++] = 0;
2667}