blob: bc5df9cf47907f5b304147349449df27409af99b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- APInt.cpp - Implement APInt class ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a class to represent arbitrary precision integer
11// constant values and provide a variety of arithmetic operations on them.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "apint"
16#include "llvm/ADT/APInt.h"
Ted Kremenek109de0d2008-01-19 04:23:33 +000017#include "llvm/ADT/FoldingSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Support/Debug.h"
19#include "llvm/Support/MathExtras.h"
20#include <math.h>
21#include <limits>
22#include <cstring>
23#include <cstdlib>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include <iomanip>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025
26using namespace llvm;
27
Reid Spencera15c5012007-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037/// A utility function for allocating memory, checking for allocation failures,
38/// and ensuring the contents are zeroed.
39inline 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;
44}
45
46/// A utility function for allocating memory and checking for allocation
47/// failure. The content is not zeroed.
48inline 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
54APInt::APInt(uint32_t numBits, uint64_t val, bool isSigned)
55 : BitWidth(numBits), VAL(0) {
Reid Spencera15c5012007-12-11 06:53:58 +000056 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
57 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 if (isSingleWord())
59 VAL = val;
60 else {
61 pVal = getClearedMemory(getNumWords());
62 pVal[0] = val;
63 if (isSigned && int64_t(val) < 0)
64 for (unsigned i = 1; i < getNumWords(); ++i)
65 pVal[i] = -1ULL;
66 }
67 clearUnusedBits();
68}
69
Dale Johannesena6f79742007-09-21 22:09:37 +000070APInt::APInt(uint32_t numBits, uint32_t numWords, const uint64_t bigVal[])
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 : BitWidth(numBits), VAL(0) {
Reid Spencera15c5012007-12-11 06:53:58 +000072 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
73 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 assert(bigVal && "Null pointer detected!");
75 if (isSingleWord())
76 VAL = bigVal[0];
77 else {
78 // 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);
84 }
85 // Make sure unused high bits are cleared
86 clearUnusedBits();
87}
88
89APInt::APInt(uint32_t numbits, const char StrStart[], uint32_t slen,
90 uint8_t radix)
91 : BitWidth(numbits), VAL(0) {
Reid Spencera15c5012007-12-11 06:53:58 +000092 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
93 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 fromString(numbits, StrStart, slen, radix);
95}
96
97APInt::APInt(uint32_t numbits, const std::string& Val, uint8_t radix)
98 : BitWidth(numbits), VAL(0) {
Reid Spencera15c5012007-12-11 06:53:58 +000099 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
100 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 assert(!Val.empty() && "String empty?");
102 fromString(numbits, Val.c_str(), Val.size(), radix);
103}
104
105APInt::APInt(const APInt& that)
106 : BitWidth(that.BitWidth), VAL(0) {
Reid Spencera15c5012007-12-11 06:53:58 +0000107 assert(BitWidth >= MIN_INT_BITS && "bitwidth too small");
108 assert(BitWidth <= MAX_INT_BITS && "bitwidth too large");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 if (isSingleWord())
110 VAL = that.VAL;
111 else {
112 pVal = getMemory(getNumWords());
113 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
114 }
115}
116
117APInt::~APInt() {
118 if (!isSingleWord() && pVal)
119 delete [] pVal;
120}
121
122APInt& APInt::operator=(const APInt& RHS) {
123 // 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;
148 VAL = RHS.VAL;
149 } 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();
156}
157
158APInt& APInt::operator=(uint64_t RHS) {
159 if (isSingleWord())
160 VAL = RHS;
161 else {
162 pVal[0] = RHS;
163 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
164 }
165 return clearUnusedBits();
166}
167
Ted Kremenek109de0d2008-01-19 04:23:33 +0000168/// Profile - This method 'profiles' an APInt for use with FoldingSet.
169void APInt::Profile(FoldingSetNodeID& ID) const {
Ted Kremenek79f65912008-02-19 20:50:41 +0000170 ID.AddInteger(BitWidth);
171
Ted Kremenek109de0d2008-01-19 04:23:33 +0000172 if (isSingleWord()) {
173 ID.AddInteger(VAL);
174 return;
175 }
176
177 uint32_t NumWords = getNumWords();
178 for (unsigned i = 0; i < NumWords; ++i)
179 ID.AddInteger(pVal[i]);
180}
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182/// add_1 - This function adds a single "digit" integer, y, to the multiple
183/// "digit" integer array, x[]. x[] is modified to reflect the addition and
184/// 1 is returned if there is a carry out, otherwise 0 is returned.
185/// @returns the carry of the addition.
186static bool add_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
187 for (uint32_t i = 0; i < len; ++i) {
188 dest[i] = y + x[i];
189 if (dest[i] < y)
190 y = 1; // Carry one to next digit.
191 else {
192 y = 0; // No need to carry so exit early
193 break;
194 }
195 }
196 return y;
197}
198
199/// @brief Prefix increment operator. Increments the APInt by one.
200APInt& APInt::operator++() {
201 if (isSingleWord())
202 ++VAL;
203 else
204 add_1(pVal, pVal, getNumWords(), 1);
205 return clearUnusedBits();
206}
207
208/// sub_1 - This function subtracts a single "digit" (64-bit word), y, from
209/// the multi-digit integer array, x[], propagating the borrowed 1 value until
210/// no further borrowing is neeeded or it runs out of "digits" in x. The result
211/// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
212/// In other words, if y > x then this function returns 1, otherwise 0.
213/// @returns the borrow out of the subtraction
214static bool sub_1(uint64_t x[], uint32_t len, uint64_t y) {
215 for (uint32_t i = 0; i < len; ++i) {
216 uint64_t X = x[i];
217 x[i] -= y;
218 if (y > X)
219 y = 1; // We have to "borrow 1" from next "digit"
220 else {
221 y = 0; // No need to borrow
222 break; // Remaining digits are unchanged so exit early
223 }
224 }
225 return bool(y);
226}
227
228/// @brief Prefix decrement operator. Decrements the APInt by one.
229APInt& APInt::operator--() {
230 if (isSingleWord())
231 --VAL;
232 else
233 sub_1(pVal, getNumWords(), 1);
234 return clearUnusedBits();
235}
236
237/// add - This function adds the integer array x to the integer array Y and
238/// places the result in dest.
239/// @returns the carry out from the addition
240/// @brief General addition of 64-bit integer arrays
241static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
242 uint32_t len) {
243 bool carry = false;
244 for (uint32_t i = 0; i< len; ++i) {
245 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
246 dest[i] = x[i] + y[i] + carry;
247 carry = dest[i] < limit || (carry && dest[i] == limit);
248 }
249 return carry;
250}
251
252/// Adds the RHS APint to this APInt.
253/// @returns this, after addition of RHS.
254/// @brief Addition assignment operator.
255APInt& APInt::operator+=(const APInt& RHS) {
256 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
257 if (isSingleWord())
258 VAL += RHS.VAL;
259 else {
260 add(pVal, pVal, RHS.pVal, getNumWords());
261 }
262 return clearUnusedBits();
263}
264
265/// Subtracts the integer array y from the integer array x
266/// @returns returns the borrow out.
267/// @brief Generalized subtraction of 64-bit integer arrays.
268static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
269 uint32_t len) {
270 bool borrow = false;
271 for (uint32_t i = 0; i < len; ++i) {
272 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
273 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
274 dest[i] = x_tmp - y[i];
275 }
276 return borrow;
277}
278
279/// Subtracts the RHS APInt from this APInt
280/// @returns this, after subtraction
281/// @brief Subtraction assignment operator.
282APInt& APInt::operator-=(const APInt& RHS) {
283 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
284 if (isSingleWord())
285 VAL -= RHS.VAL;
286 else
287 sub(pVal, pVal, RHS.pVal, getNumWords());
288 return clearUnusedBits();
289}
290
291/// Multiplies an integer array, x by a a uint64_t integer and places the result
292/// into dest.
293/// @returns the carry out of the multiplication.
294/// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
295static uint64_t mul_1(uint64_t dest[], uint64_t x[], uint32_t len, uint64_t y) {
296 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
297 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
298 uint64_t carry = 0;
299
300 // For each digit of x.
301 for (uint32_t i = 0; i < len; ++i) {
302 // Split x into high and low words
303 uint64_t lx = x[i] & 0xffffffffULL;
304 uint64_t hx = x[i] >> 32;
305 // hasCarry - A flag to indicate if there is a carry to the next digit.
306 // hasCarry == 0, no carry
307 // hasCarry == 1, has carry
308 // hasCarry == 2, no carry and the calculation result == 0.
309 uint8_t hasCarry = 0;
310 dest[i] = carry + lx * ly;
311 // Determine if the add above introduces carry.
312 hasCarry = (dest[i] < carry) ? 1 : 0;
313 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
314 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
315 // (2^32 - 1) + 2^32 = 2^64.
316 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
317
318 carry += (lx * hy) & 0xffffffffULL;
319 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
320 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
321 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
322 }
323 return carry;
324}
325
326/// Multiplies integer array x by integer array y and stores the result into
327/// the integer array dest. Note that dest's size must be >= xlen + ylen.
328/// @brief Generalized multiplicate of integer arrays.
329static void mul(uint64_t dest[], uint64_t x[], uint32_t xlen, uint64_t y[],
330 uint32_t ylen) {
331 dest[xlen] = mul_1(dest, x, xlen, y[0]);
332 for (uint32_t i = 1; i < ylen; ++i) {
333 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
334 uint64_t carry = 0, lx = 0, hx = 0;
335 for (uint32_t j = 0; j < xlen; ++j) {
336 lx = x[j] & 0xffffffffULL;
337 hx = x[j] >> 32;
338 // hasCarry - A flag to indicate if has carry.
339 // hasCarry == 0, no carry
340 // hasCarry == 1, has carry
341 // hasCarry == 2, no carry and the calculation result == 0.
342 uint8_t hasCarry = 0;
343 uint64_t resul = carry + lx * ly;
344 hasCarry = (resul < carry) ? 1 : 0;
345 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
346 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
347
348 carry += (lx * hy) & 0xffffffffULL;
349 resul = (carry << 32) | (resul & 0xffffffffULL);
350 dest[i+j] += resul;
351 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
352 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
353 ((lx * hy) >> 32) + hx * hy;
354 }
355 dest[i+xlen] = carry;
356 }
357}
358
359APInt& APInt::operator*=(const APInt& RHS) {
360 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
361 if (isSingleWord()) {
362 VAL *= RHS.VAL;
363 clearUnusedBits();
364 return *this;
365 }
366
367 // Get some bit facts about LHS and check for zero
368 uint32_t lhsBits = getActiveBits();
369 uint32_t lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
370 if (!lhsWords)
371 // 0 * X ===> 0
372 return *this;
373
374 // Get some bit facts about RHS and check for zero
375 uint32_t rhsBits = RHS.getActiveBits();
376 uint32_t rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
377 if (!rhsWords) {
378 // X * 0 ===> 0
379 clear();
380 return *this;
381 }
382
383 // Allocate space for the result
384 uint32_t destWords = rhsWords + lhsWords;
385 uint64_t *dest = getMemory(destWords);
386
387 // Perform the long multiply
388 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
389
390 // Copy result back into *this
391 clear();
392 uint32_t wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
393 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
394
395 // delete dest array and return
396 delete[] dest;
397 return *this;
398}
399
400APInt& APInt::operator&=(const APInt& RHS) {
401 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
402 if (isSingleWord()) {
403 VAL &= RHS.VAL;
404 return *this;
405 }
406 uint32_t numWords = getNumWords();
407 for (uint32_t i = 0; i < numWords; ++i)
408 pVal[i] &= RHS.pVal[i];
409 return *this;
410}
411
412APInt& APInt::operator|=(const APInt& RHS) {
413 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
414 if (isSingleWord()) {
415 VAL |= RHS.VAL;
416 return *this;
417 }
418 uint32_t numWords = getNumWords();
419 for (uint32_t i = 0; i < numWords; ++i)
420 pVal[i] |= RHS.pVal[i];
421 return *this;
422}
423
424APInt& APInt::operator^=(const APInt& RHS) {
425 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
426 if (isSingleWord()) {
427 VAL ^= RHS.VAL;
428 this->clearUnusedBits();
429 return *this;
430 }
431 uint32_t numWords = getNumWords();
432 for (uint32_t i = 0; i < numWords; ++i)
433 pVal[i] ^= RHS.pVal[i];
434 return clearUnusedBits();
435}
436
437APInt APInt::operator&(const APInt& RHS) const {
438 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
439 if (isSingleWord())
440 return APInt(getBitWidth(), VAL & RHS.VAL);
441
442 uint32_t numWords = getNumWords();
443 uint64_t* val = getMemory(numWords);
444 for (uint32_t i = 0; i < numWords; ++i)
445 val[i] = pVal[i] & RHS.pVal[i];
446 return APInt(val, getBitWidth());
447}
448
449APInt APInt::operator|(const APInt& RHS) const {
450 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
451 if (isSingleWord())
452 return APInt(getBitWidth(), VAL | RHS.VAL);
453
454 uint32_t numWords = getNumWords();
455 uint64_t *val = getMemory(numWords);
456 for (uint32_t i = 0; i < numWords; ++i)
457 val[i] = pVal[i] | RHS.pVal[i];
458 return APInt(val, getBitWidth());
459}
460
461APInt APInt::operator^(const APInt& RHS) const {
462 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
463 if (isSingleWord())
464 return APInt(BitWidth, VAL ^ RHS.VAL);
465
466 uint32_t numWords = getNumWords();
467 uint64_t *val = getMemory(numWords);
468 for (uint32_t i = 0; i < numWords; ++i)
469 val[i] = pVal[i] ^ RHS.pVal[i];
470
471 // 0^0==1 so clear the high bits in case they got set.
472 return APInt(val, getBitWidth()).clearUnusedBits();
473}
474
475bool APInt::operator !() const {
476 if (isSingleWord())
477 return !VAL;
478
479 for (uint32_t i = 0; i < getNumWords(); ++i)
480 if (pVal[i])
481 return false;
482 return true;
483}
484
485APInt APInt::operator*(const APInt& RHS) const {
486 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
487 if (isSingleWord())
488 return APInt(BitWidth, VAL * RHS.VAL);
489 APInt Result(*this);
490 Result *= RHS;
491 return Result.clearUnusedBits();
492}
493
494APInt APInt::operator+(const APInt& RHS) const {
495 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
496 if (isSingleWord())
497 return APInt(BitWidth, VAL + RHS.VAL);
498 APInt Result(BitWidth, 0);
499 add(Result.pVal, this->pVal, RHS.pVal, getNumWords());
500 return Result.clearUnusedBits();
501}
502
503APInt APInt::operator-(const APInt& RHS) const {
504 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
505 if (isSingleWord())
506 return APInt(BitWidth, VAL - RHS.VAL);
507 APInt Result(BitWidth, 0);
508 sub(Result.pVal, this->pVal, RHS.pVal, getNumWords());
509 return Result.clearUnusedBits();
510}
511
512bool APInt::operator[](uint32_t bitPosition) const {
513 return (maskBit(bitPosition) &
514 (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) != 0;
515}
516
517bool APInt::operator==(const APInt& RHS) const {
518 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
519 if (isSingleWord())
520 return VAL == RHS.VAL;
521
522 // Get some facts about the number of bits used in the two operands.
523 uint32_t n1 = getActiveBits();
524 uint32_t n2 = RHS.getActiveBits();
525
526 // If the number of bits isn't the same, they aren't equal
527 if (n1 != n2)
528 return false;
529
530 // If the number of bits fits in a word, we only need to compare the low word.
531 if (n1 <= APINT_BITS_PER_WORD)
532 return pVal[0] == RHS.pVal[0];
533
534 // Otherwise, compare everything
535 for (int i = whichWord(n1 - 1); i >= 0; --i)
536 if (pVal[i] != RHS.pVal[i])
537 return false;
538 return true;
539}
540
541bool APInt::operator==(uint64_t Val) const {
542 if (isSingleWord())
543 return VAL == Val;
544
545 uint32_t n = getActiveBits();
546 if (n <= APINT_BITS_PER_WORD)
547 return pVal[0] == Val;
548 else
549 return false;
550}
551
552bool APInt::ult(const APInt& RHS) const {
553 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
554 if (isSingleWord())
555 return VAL < RHS.VAL;
556
557 // Get active bit length of both operands
558 uint32_t n1 = getActiveBits();
559 uint32_t n2 = RHS.getActiveBits();
560
561 // If magnitude of LHS is less than RHS, return true.
562 if (n1 < n2)
563 return true;
564
565 // If magnitude of RHS is greather than LHS, return false.
566 if (n2 < n1)
567 return false;
568
569 // If they bot fit in a word, just compare the low order word
570 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
571 return pVal[0] < RHS.pVal[0];
572
573 // Otherwise, compare all words
574 uint32_t topWord = whichWord(std::max(n1,n2)-1);
575 for (int i = topWord; i >= 0; --i) {
576 if (pVal[i] > RHS.pVal[i])
577 return false;
578 if (pVal[i] < RHS.pVal[i])
579 return true;
580 }
581 return false;
582}
583
584bool APInt::slt(const APInt& RHS) const {
585 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
586 if (isSingleWord()) {
587 int64_t lhsSext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
588 int64_t rhsSext = (int64_t(RHS.VAL) << (64-BitWidth)) >> (64-BitWidth);
589 return lhsSext < rhsSext;
590 }
591
592 APInt lhs(*this);
593 APInt rhs(RHS);
594 bool lhsNeg = isNegative();
595 bool rhsNeg = rhs.isNegative();
596 if (lhsNeg) {
597 // Sign bit is set so perform two's complement to make it positive
598 lhs.flip();
599 lhs++;
600 }
601 if (rhsNeg) {
602 // Sign bit is set so perform two's complement to make it positive
603 rhs.flip();
604 rhs++;
605 }
606
607 // Now we have unsigned values to compare so do the comparison if necessary
608 // based on the negativeness of the values.
609 if (lhsNeg)
610 if (rhsNeg)
611 return lhs.ugt(rhs);
612 else
613 return true;
614 else if (rhsNeg)
615 return false;
616 else
617 return lhs.ult(rhs);
618}
619
620APInt& APInt::set(uint32_t bitPosition) {
621 if (isSingleWord())
622 VAL |= maskBit(bitPosition);
623 else
624 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
625 return *this;
626}
627
628APInt& APInt::set() {
629 if (isSingleWord()) {
630 VAL = -1ULL;
631 return clearUnusedBits();
632 }
633
634 // Set all the bits in all the words.
635 for (uint32_t i = 0; i < getNumWords(); ++i)
636 pVal[i] = -1ULL;
637 // Clear the unused ones
638 return clearUnusedBits();
639}
640
641/// Set the given bit to 0 whose position is given as "bitPosition".
642/// @brief Set a given bit to 0.
643APInt& APInt::clear(uint32_t bitPosition) {
644 if (isSingleWord())
645 VAL &= ~maskBit(bitPosition);
646 else
647 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
648 return *this;
649}
650
651/// @brief Set every bit to 0.
652APInt& APInt::clear() {
653 if (isSingleWord())
654 VAL = 0;
655 else
656 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
657 return *this;
658}
659
660/// @brief Bitwise NOT operator. Performs a bitwise logical NOT operation on
661/// this APInt.
662APInt APInt::operator~() const {
663 APInt Result(*this);
664 Result.flip();
665 return Result;
666}
667
668/// @brief Toggle every bit to its opposite value.
669APInt& APInt::flip() {
670 if (isSingleWord()) {
671 VAL ^= -1ULL;
672 return clearUnusedBits();
673 }
674 for (uint32_t i = 0; i < getNumWords(); ++i)
675 pVal[i] ^= -1ULL;
676 return clearUnusedBits();
677}
678
679/// Toggle a given bit to its opposite value whose position is given
680/// as "bitPosition".
681/// @brief Toggles a given bit to its opposite value.
682APInt& APInt::flip(uint32_t bitPosition) {
683 assert(bitPosition < BitWidth && "Out of the bit-width range!");
684 if ((*this)[bitPosition]) clear(bitPosition);
685 else set(bitPosition);
686 return *this;
687}
688
689uint32_t APInt::getBitsNeeded(const char* str, uint32_t slen, uint8_t radix) {
690 assert(str != 0 && "Invalid value string");
691 assert(slen > 0 && "Invalid string length");
692
693 // Each computation below needs to know if its negative
694 uint32_t isNegative = str[0] == '-';
695 if (isNegative) {
696 slen--;
697 str++;
698 }
699 // For radixes of power-of-two values, the bits required is accurately and
700 // easily computed
701 if (radix == 2)
702 return slen + isNegative;
703 if (radix == 8)
704 return slen * 3 + isNegative;
705 if (radix == 16)
706 return slen * 4 + isNegative;
707
708 // Otherwise it must be radix == 10, the hard case
709 assert(radix == 10 && "Invalid radix");
710
711 // This is grossly inefficient but accurate. We could probably do something
712 // with a computation of roughly slen*64/20 and then adjust by the value of
713 // the first few digits. But, I'm not sure how accurate that could be.
714
715 // Compute a sufficient number of bits that is always large enough but might
716 // be too large. This avoids the assertion in the constructor.
717 uint32_t sufficient = slen*64/18;
718
719 // Convert to the actual binary value.
720 APInt tmp(sufficient, str, slen, radix);
721
722 // Compute how many bits are required.
723 return isNegative + tmp.logBase2() + 1;
724}
725
726uint64_t APInt::getHashValue() const {
727 // Put the bit width into the low order bits.
728 uint64_t hash = BitWidth;
729
730 // Add the sum of the words to the hash.
731 if (isSingleWord())
732 hash += VAL << 6; // clear separation of up to 64 bits
733 else
734 for (uint32_t i = 0; i < getNumWords(); ++i)
735 hash += pVal[i] << 6; // clear sepration of up to 64 bits
736 return hash;
737}
738
739/// HiBits - This function returns the high "numBits" bits of this APInt.
740APInt APInt::getHiBits(uint32_t numBits) const {
741 return APIntOps::lshr(*this, BitWidth - numBits);
742}
743
744/// LoBits - This function returns the low "numBits" bits of this APInt.
745APInt APInt::getLoBits(uint32_t numBits) const {
746 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
747 BitWidth - numBits);
748}
749
750bool APInt::isPowerOf2() const {
751 return (!!*this) && !(*this & (*this - APInt(BitWidth,1)));
752}
753
754uint32_t APInt::countLeadingZeros() const {
755 uint32_t Count = 0;
756 if (isSingleWord())
757 Count = CountLeadingZeros_64(VAL);
758 else {
759 for (uint32_t i = getNumWords(); i > 0u; --i) {
760 if (pVal[i-1] == 0)
761 Count += APINT_BITS_PER_WORD;
762 else {
763 Count += CountLeadingZeros_64(pVal[i-1]);
764 break;
765 }
766 }
767 }
768 uint32_t remainder = BitWidth % APINT_BITS_PER_WORD;
769 if (remainder)
770 Count -= APINT_BITS_PER_WORD - remainder;
Chris Lattner00b08ce2007-11-23 22:42:31 +0000771 return std::min(Count, BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772}
773
774static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) {
775 uint32_t Count = 0;
776 if (skip)
777 V <<= skip;
778 while (V && (V & (1ULL << 63))) {
779 Count++;
780 V <<= 1;
781 }
782 return Count;
783}
784
785uint32_t APInt::countLeadingOnes() const {
786 if (isSingleWord())
787 return countLeadingOnes_64(VAL, APINT_BITS_PER_WORD - BitWidth);
788
789 uint32_t highWordBits = BitWidth % APINT_BITS_PER_WORD;
790 uint32_t shift = (highWordBits == 0 ? 0 : APINT_BITS_PER_WORD - highWordBits);
791 int i = getNumWords() - 1;
792 uint32_t Count = countLeadingOnes_64(pVal[i], shift);
793 if (Count == highWordBits) {
794 for (i--; i >= 0; --i) {
795 if (pVal[i] == -1ULL)
796 Count += APINT_BITS_PER_WORD;
797 else {
798 Count += countLeadingOnes_64(pVal[i], 0);
799 break;
800 }
801 }
802 }
803 return Count;
804}
805
806uint32_t APInt::countTrailingZeros() const {
807 if (isSingleWord())
Anton Korobeynikova0bd36c2007-12-24 11:16:47 +0000808 return std::min(uint32_t(CountTrailingZeros_64(VAL)), BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 uint32_t Count = 0;
810 uint32_t i = 0;
811 for (; i < getNumWords() && pVal[i] == 0; ++i)
812 Count += APINT_BITS_PER_WORD;
813 if (i < getNumWords())
814 Count += CountTrailingZeros_64(pVal[i]);
Chris Lattner9ee26cf2007-11-23 22:36:25 +0000815 return std::min(Count, BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816}
817
Dan Gohmanf550d412008-02-13 21:11:05 +0000818uint32_t APInt::countTrailingOnes() const {
819 if (isSingleWord())
820 return std::min(uint32_t(CountTrailingOnes_64(VAL)), BitWidth);
821 uint32_t Count = 0;
822 uint32_t i = 0;
Dan Gohmane4428412008-02-14 22:38:45 +0000823 for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
Dan Gohmanf550d412008-02-13 21:11:05 +0000824 Count += APINT_BITS_PER_WORD;
825 if (i < getNumWords())
826 Count += CountTrailingOnes_64(pVal[i]);
827 return std::min(Count, BitWidth);
828}
829
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830uint32_t APInt::countPopulation() const {
831 if (isSingleWord())
832 return CountPopulation_64(VAL);
833 uint32_t Count = 0;
834 for (uint32_t i = 0; i < getNumWords(); ++i)
835 Count += CountPopulation_64(pVal[i]);
836 return Count;
837}
838
839APInt APInt::byteSwap() const {
840 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
841 if (BitWidth == 16)
842 return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
843 else if (BitWidth == 32)
844 return APInt(BitWidth, ByteSwap_32(uint32_t(VAL)));
845 else if (BitWidth == 48) {
846 uint32_t Tmp1 = uint32_t(VAL >> 16);
847 Tmp1 = ByteSwap_32(Tmp1);
848 uint16_t Tmp2 = uint16_t(VAL);
849 Tmp2 = ByteSwap_16(Tmp2);
850 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
851 } else if (BitWidth == 64)
852 return APInt(BitWidth, ByteSwap_64(VAL));
853 else {
854 APInt Result(BitWidth, 0);
855 char *pByte = (char*)Result.pVal;
856 for (uint32_t i = 0; i < BitWidth / APINT_WORD_SIZE / 2; ++i) {
857 char Tmp = pByte[i];
858 pByte[i] = pByte[BitWidth / APINT_WORD_SIZE - 1 - i];
859 pByte[BitWidth / APINT_WORD_SIZE - i - 1] = Tmp;
860 }
861 return Result;
862 }
863}
864
865APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
866 const APInt& API2) {
867 APInt A = API1, B = API2;
868 while (!!B) {
869 APInt T = B;
870 B = APIntOps::urem(A, B);
871 A = T;
872 }
873 return A;
874}
875
876APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, uint32_t width) {
877 union {
878 double D;
879 uint64_t I;
880 } T;
881 T.D = Double;
882
883 // Get the sign bit from the highest order bit
884 bool isNeg = T.I >> 63;
885
886 // Get the 11-bit exponent and adjust for the 1023 bit bias
887 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
888
889 // If the exponent is negative, the value is < 0 so just return 0.
890 if (exp < 0)
891 return APInt(width, 0u);
892
893 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
894 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
895
896 // If the exponent doesn't shift all bits out of the mantissa
897 if (exp < 52)
898 return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
899 APInt(width, mantissa >> (52 - exp));
900
901 // If the client didn't provide enough bits for us to shift the mantissa into
902 // then the result is undefined, just return 0
903 if (width <= exp - 52)
904 return APInt(width, 0);
905
906 // Otherwise, we have to shift the mantissa bits up to the right location
907 APInt Tmp(width, mantissa);
908 Tmp = Tmp.shl(exp - 52);
909 return isNeg ? -Tmp : Tmp;
910}
911
912/// RoundToDouble - This function convert this APInt to a double.
913/// The layout for double is as following (IEEE Standard 754):
914/// --------------------------------------
915/// | Sign Exponent Fraction Bias |
916/// |-------------------------------------- |
917/// | 1[63] 11[62-52] 52[51-00] 1023 |
918/// --------------------------------------
919double APInt::roundToDouble(bool isSigned) const {
920
921 // Handle the simple case where the value is contained in one uint64_t.
922 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
923 if (isSigned) {
924 int64_t sext = (int64_t(VAL) << (64-BitWidth)) >> (64-BitWidth);
925 return double(sext);
926 } else
927 return double(VAL);
928 }
929
930 // Determine if the value is negative.
931 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
932
933 // Construct the absolute value if we're negative.
934 APInt Tmp(isNeg ? -(*this) : (*this));
935
936 // Figure out how many bits we're using.
937 uint32_t n = Tmp.getActiveBits();
938
939 // The exponent (without bias normalization) is just the number of bits
940 // we are using. Note that the sign bit is gone since we constructed the
941 // absolute value.
942 uint64_t exp = n;
943
944 // Return infinity for exponent overflow
945 if (exp > 1023) {
946 if (!isSigned || !isNeg)
947 return std::numeric_limits<double>::infinity();
948 else
949 return -std::numeric_limits<double>::infinity();
950 }
951 exp += 1023; // Increment for 1023 bias
952
953 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
954 // extract the high 52 bits from the correct words in pVal.
955 uint64_t mantissa;
956 unsigned hiWord = whichWord(n-1);
957 if (hiWord == 0) {
958 mantissa = Tmp.pVal[0];
959 if (n > 52)
960 mantissa >>= n - 52; // shift down, we want the top 52 bits.
961 } else {
962 assert(hiWord > 0 && "huh?");
963 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
964 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
965 mantissa = hibits | lobits;
966 }
967
968 // The leading bit of mantissa is implicit, so get rid of it.
969 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
970 union {
971 double D;
972 uint64_t I;
973 } T;
974 T.I = sign | (exp << 52) | mantissa;
975 return T.D;
976}
977
978// Truncate to new width.
979APInt &APInt::trunc(uint32_t width) {
980 assert(width < BitWidth && "Invalid APInt Truncate request");
Reid Spencera15c5012007-12-11 06:53:58 +0000981 assert(width >= MIN_INT_BITS && "Can't truncate to 0 bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 uint32_t wordsBefore = getNumWords();
983 BitWidth = width;
984 uint32_t wordsAfter = getNumWords();
985 if (wordsBefore != wordsAfter) {
986 if (wordsAfter == 1) {
987 uint64_t *tmp = pVal;
988 VAL = pVal[0];
989 delete [] tmp;
990 } else {
991 uint64_t *newVal = getClearedMemory(wordsAfter);
992 for (uint32_t i = 0; i < wordsAfter; ++i)
993 newVal[i] = pVal[i];
994 delete [] pVal;
995 pVal = newVal;
996 }
997 }
998 return clearUnusedBits();
999}
1000
1001// Sign extend to a new width.
1002APInt &APInt::sext(uint32_t width) {
1003 assert(width > BitWidth && "Invalid APInt SignExtend request");
Reid Spencera15c5012007-12-11 06:53:58 +00001004 assert(width <= MAX_INT_BITS && "Too many bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 // If the sign bit isn't set, this is the same as zext.
1006 if (!isNegative()) {
1007 zext(width);
1008 return *this;
1009 }
1010
1011 // The sign bit is set. First, get some facts
1012 uint32_t wordsBefore = getNumWords();
1013 uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
1014 BitWidth = width;
1015 uint32_t wordsAfter = getNumWords();
1016
1017 // Mask the high order word appropriately
1018 if (wordsBefore == wordsAfter) {
1019 uint32_t newWordBits = width % APINT_BITS_PER_WORD;
1020 // The extension is contained to the wordsBefore-1th word.
1021 uint64_t mask = ~0ULL;
1022 if (newWordBits)
1023 mask >>= APINT_BITS_PER_WORD - newWordBits;
1024 mask <<= wordBits;
1025 if (wordsBefore == 1)
1026 VAL |= mask;
1027 else
1028 pVal[wordsBefore-1] |= mask;
1029 return clearUnusedBits();
1030 }
1031
1032 uint64_t mask = wordBits == 0 ? 0 : ~0ULL << wordBits;
1033 uint64_t *newVal = getMemory(wordsAfter);
1034 if (wordsBefore == 1)
1035 newVal[0] = VAL | mask;
1036 else {
1037 for (uint32_t i = 0; i < wordsBefore; ++i)
1038 newVal[i] = pVal[i];
1039 newVal[wordsBefore-1] |= mask;
1040 }
1041 for (uint32_t i = wordsBefore; i < wordsAfter; i++)
1042 newVal[i] = -1ULL;
1043 if (wordsBefore != 1)
1044 delete [] pVal;
1045 pVal = newVal;
1046 return clearUnusedBits();
1047}
1048
1049// Zero extend to a new width.
1050APInt &APInt::zext(uint32_t width) {
1051 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
Reid Spencera15c5012007-12-11 06:53:58 +00001052 assert(width <= MAX_INT_BITS && "Too many bits");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 uint32_t wordsBefore = getNumWords();
1054 BitWidth = width;
1055 uint32_t wordsAfter = getNumWords();
1056 if (wordsBefore != wordsAfter) {
1057 uint64_t *newVal = getClearedMemory(wordsAfter);
1058 if (wordsBefore == 1)
1059 newVal[0] = VAL;
1060 else
1061 for (uint32_t i = 0; i < wordsBefore; ++i)
1062 newVal[i] = pVal[i];
1063 if (wordsBefore != 1)
1064 delete [] pVal;
1065 pVal = newVal;
1066 }
1067 return *this;
1068}
1069
1070APInt &APInt::zextOrTrunc(uint32_t width) {
1071 if (BitWidth < width)
1072 return zext(width);
1073 if (BitWidth > width)
1074 return trunc(width);
1075 return *this;
1076}
1077
1078APInt &APInt::sextOrTrunc(uint32_t width) {
1079 if (BitWidth < width)
1080 return sext(width);
1081 if (BitWidth > width)
1082 return trunc(width);
1083 return *this;
1084}
1085
1086/// Arithmetic right-shift this APInt by shiftAmt.
1087/// @brief Arithmetic right-shift function.
1088APInt APInt::ashr(uint32_t shiftAmt) const {
1089 assert(shiftAmt <= BitWidth && "Invalid shift amount");
1090 // Handle a degenerate case
1091 if (shiftAmt == 0)
1092 return *this;
1093
1094 // Handle single word shifts with built-in ashr
1095 if (isSingleWord()) {
1096 if (shiftAmt == BitWidth)
1097 return APInt(BitWidth, 0); // undefined
1098 else {
1099 uint32_t SignBit = APINT_BITS_PER_WORD - BitWidth;
1100 return APInt(BitWidth,
1101 (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt));
1102 }
1103 }
1104
1105 // If all the bits were shifted out, the result is, technically, undefined.
1106 // We return -1 if it was negative, 0 otherwise. We check this early to avoid
1107 // issues in the algorithm below.
1108 if (shiftAmt == BitWidth) {
1109 if (isNegative())
1110 return APInt(BitWidth, -1ULL);
1111 else
1112 return APInt(BitWidth, 0);
1113 }
1114
1115 // Create some space for the result.
1116 uint64_t * val = new uint64_t[getNumWords()];
1117
1118 // Compute some values needed by the following shift algorithms
1119 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
1120 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
1121 uint32_t breakWord = getNumWords() - 1 - offset; // last word affected
1122 uint32_t bitsInWord = whichBit(BitWidth); // how many bits in last word?
1123 if (bitsInWord == 0)
1124 bitsInWord = APINT_BITS_PER_WORD;
1125
1126 // If we are shifting whole words, just move whole words
1127 if (wordShift == 0) {
1128 // Move the words containing significant bits
1129 for (uint32_t i = 0; i <= breakWord; ++i)
1130 val[i] = pVal[i+offset]; // move whole word
1131
1132 // Adjust the top significant word for sign bit fill, if negative
1133 if (isNegative())
1134 if (bitsInWord < APINT_BITS_PER_WORD)
1135 val[breakWord] |= ~0ULL << bitsInWord; // set high bits
1136 } else {
1137 // Shift the low order words
1138 for (uint32_t i = 0; i < breakWord; ++i) {
1139 // This combines the shifted corresponding word with the low bits from
1140 // the next word (shifted into this word's high bits).
1141 val[i] = (pVal[i+offset] >> wordShift) |
1142 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1143 }
1144
1145 // Shift the break word. In this case there are no bits from the next word
1146 // to include in this word.
1147 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1148
1149 // Deal with sign extenstion in the break word, and possibly the word before
1150 // it.
1151 if (isNegative()) {
1152 if (wordShift > bitsInWord) {
1153 if (breakWord > 0)
1154 val[breakWord-1] |=
1155 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1156 val[breakWord] |= ~0ULL;
1157 } else
1158 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1159 }
1160 }
1161
1162 // Remaining words are 0 or -1, just assign them.
1163 uint64_t fillValue = (isNegative() ? -1ULL : 0);
1164 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
1165 val[i] = fillValue;
1166 return APInt(val, BitWidth).clearUnusedBits();
1167}
1168
1169/// Logical right-shift this APInt by shiftAmt.
1170/// @brief Logical right-shift function.
1171APInt APInt::lshr(uint32_t shiftAmt) const {
1172 if (isSingleWord()) {
1173 if (shiftAmt == BitWidth)
1174 return APInt(BitWidth, 0);
1175 else
1176 return APInt(BitWidth, this->VAL >> shiftAmt);
1177 }
1178
1179 // If all the bits were shifted out, the result is 0. This avoids issues
1180 // with shifting by the size of the integer type, which produces undefined
1181 // results. We define these "undefined results" to always be 0.
1182 if (shiftAmt == BitWidth)
1183 return APInt(BitWidth, 0);
1184
1185 // If none of the bits are shifted out, the result is *this. This avoids
1186 // issues with shifting byt he size of the integer type, which produces
1187 // undefined results in the code below. This is also an optimization.
1188 if (shiftAmt == 0)
1189 return *this;
1190
1191 // Create some space for the result.
1192 uint64_t * val = new uint64_t[getNumWords()];
1193
1194 // If we are shifting less than a word, compute the shift with a simple carry
1195 if (shiftAmt < APINT_BITS_PER_WORD) {
1196 uint64_t carry = 0;
1197 for (int i = getNumWords()-1; i >= 0; --i) {
1198 val[i] = (pVal[i] >> shiftAmt) | carry;
1199 carry = pVal[i] << (APINT_BITS_PER_WORD - shiftAmt);
1200 }
1201 return APInt(val, BitWidth).clearUnusedBits();
1202 }
1203
1204 // Compute some values needed by the remaining shift algorithms
1205 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1206 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1207
1208 // If we are shifting whole words, just move whole words
1209 if (wordShift == 0) {
1210 for (uint32_t i = 0; i < getNumWords() - offset; ++i)
1211 val[i] = pVal[i+offset];
1212 for (uint32_t i = getNumWords()-offset; i < getNumWords(); i++)
1213 val[i] = 0;
1214 return APInt(val,BitWidth).clearUnusedBits();
1215 }
1216
1217 // Shift the low order words
1218 uint32_t breakWord = getNumWords() - offset -1;
1219 for (uint32_t i = 0; i < breakWord; ++i)
1220 val[i] = (pVal[i+offset] >> wordShift) |
1221 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1222 // Shift the break word.
1223 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1224
1225 // Remaining words are 0
1226 for (uint32_t i = breakWord+1; i < getNumWords(); ++i)
1227 val[i] = 0;
1228 return APInt(val, BitWidth).clearUnusedBits();
1229}
1230
1231/// Left-shift this APInt by shiftAmt.
1232/// @brief Left-shift function.
1233APInt APInt::shl(uint32_t shiftAmt) const {
1234 assert(shiftAmt <= BitWidth && "Invalid shift amount");
1235 if (isSingleWord()) {
1236 if (shiftAmt == BitWidth)
1237 return APInt(BitWidth, 0); // avoid undefined shift results
1238 return APInt(BitWidth, VAL << shiftAmt);
1239 }
1240
1241 // If all the bits were shifted out, the result is 0. This avoids issues
1242 // with shifting by the size of the integer type, which produces undefined
1243 // results. We define these "undefined results" to always be 0.
1244 if (shiftAmt == BitWidth)
1245 return APInt(BitWidth, 0);
1246
1247 // If none of the bits are shifted out, the result is *this. This avoids a
1248 // lshr by the words size in the loop below which can produce incorrect
1249 // results. It also avoids the expensive computation below for a common case.
1250 if (shiftAmt == 0)
1251 return *this;
1252
1253 // Create some space for the result.
1254 uint64_t * val = new uint64_t[getNumWords()];
1255
1256 // If we are shifting less than a word, do it the easy way
1257 if (shiftAmt < APINT_BITS_PER_WORD) {
1258 uint64_t carry = 0;
1259 for (uint32_t i = 0; i < getNumWords(); i++) {
1260 val[i] = pVal[i] << shiftAmt | carry;
1261 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1262 }
1263 return APInt(val, BitWidth).clearUnusedBits();
1264 }
1265
1266 // Compute some values needed by the remaining shift algorithms
1267 uint32_t wordShift = shiftAmt % APINT_BITS_PER_WORD;
1268 uint32_t offset = shiftAmt / APINT_BITS_PER_WORD;
1269
1270 // If we are shifting whole words, just move whole words
1271 if (wordShift == 0) {
1272 for (uint32_t i = 0; i < offset; i++)
1273 val[i] = 0;
1274 for (uint32_t i = offset; i < getNumWords(); i++)
1275 val[i] = pVal[i-offset];
1276 return APInt(val,BitWidth).clearUnusedBits();
1277 }
1278
1279 // Copy whole words from this to Result.
1280 uint32_t i = getNumWords() - 1;
1281 for (; i > offset; --i)
1282 val[i] = pVal[i-offset] << wordShift |
1283 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1284 val[offset] = pVal[0] << wordShift;
1285 for (i = 0; i < offset; ++i)
1286 val[i] = 0;
1287 return APInt(val, BitWidth).clearUnusedBits();
1288}
1289
1290APInt APInt::rotl(uint32_t rotateAmt) const {
1291 if (rotateAmt == 0)
1292 return *this;
1293 // Don't get too fancy, just use existing shift/or facilities
1294 APInt hi(*this);
1295 APInt lo(*this);
1296 hi.shl(rotateAmt);
1297 lo.lshr(BitWidth - rotateAmt);
1298 return hi | lo;
1299}
1300
1301APInt APInt::rotr(uint32_t rotateAmt) const {
1302 if (rotateAmt == 0)
1303 return *this;
1304 // Don't get too fancy, just use existing shift/or facilities
1305 APInt hi(*this);
1306 APInt lo(*this);
1307 lo.lshr(rotateAmt);
1308 hi.shl(BitWidth - rotateAmt);
1309 return hi | lo;
1310}
1311
1312// Square Root - this method computes and returns the square root of "this".
1313// Three mechanisms are used for computation. For small values (<= 5 bits),
1314// a table lookup is done. This gets some performance for common cases. For
1315// values using less than 52 bits, the value is converted to double and then
1316// the libc sqrt function is called. The result is rounded and then converted
1317// back to a uint64_t which is then used to construct the result. Finally,
1318// the Babylonian method for computing square roots is used.
1319APInt APInt::sqrt() const {
1320
1321 // Determine the magnitude of the value.
1322 uint32_t magnitude = getActiveBits();
1323
1324 // Use a fast table for some small values. This also gets rid of some
1325 // rounding errors in libc sqrt for small values.
1326 if (magnitude <= 5) {
1327 static const uint8_t results[32] = {
1328 /* 0 */ 0,
1329 /* 1- 2 */ 1, 1,
1330 /* 3- 6 */ 2, 2, 2, 2,
1331 /* 7-12 */ 3, 3, 3, 3, 3, 3,
1332 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1333 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1334 /* 31 */ 6
1335 };
1336 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1337 }
1338
1339 // If the magnitude of the value fits in less than 52 bits (the precision of
1340 // an IEEE double precision floating point value), then we can use the
1341 // libc sqrt function which will probably use a hardware sqrt computation.
1342 // This should be faster than the algorithm below.
1343 if (magnitude < 52) {
1344#ifdef _MSC_VER
1345 // Amazingly, VC++ doesn't have round().
1346 return APInt(BitWidth,
1347 uint64_t(::sqrt(double(isSingleWord()?VAL:pVal[0]))) + 0.5);
1348#else
1349 return APInt(BitWidth,
1350 uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
1351#endif
1352 }
1353
1354 // Okay, all the short cuts are exhausted. We must compute it. The following
1355 // is a classical Babylonian method for computing the square root. This code
1356 // was adapted to APINt from a wikipedia article on such computations.
1357 // See http://www.wikipedia.org/ and go to the page named
1358 // Calculate_an_integer_square_root.
1359 uint32_t nbits = BitWidth, i = 4;
1360 APInt testy(BitWidth, 16);
1361 APInt x_old(BitWidth, 1);
1362 APInt x_new(BitWidth, 0);
1363 APInt two(BitWidth, 2);
1364
1365 // Select a good starting value using binary logarithms.
1366 for (;; i += 2, testy = testy.shl(2))
1367 if (i >= nbits || this->ule(testy)) {
1368 x_old = x_old.shl(i / 2);
1369 break;
1370 }
1371
1372 // Use the Babylonian method to arrive at the integer square root:
1373 for (;;) {
1374 x_new = (this->udiv(x_old) + x_old).udiv(two);
1375 if (x_old.ule(x_new))
1376 break;
1377 x_old = x_new;
1378 }
1379
1380 // Make sure we return the closest approximation
1381 // NOTE: The rounding calculation below is correct. It will produce an
1382 // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1383 // determined to be a rounding issue with pari/gp as it begins to use a
1384 // floating point representation after 192 bits. There are no discrepancies
1385 // between this algorithm and pari/gp for bit widths < 192 bits.
1386 APInt square(x_old * x_old);
1387 APInt nextSquare((x_old + 1) * (x_old +1));
1388 if (this->ult(square))
1389 return x_old;
1390 else if (this->ule(nextSquare)) {
1391 APInt midpoint((nextSquare - square).udiv(two));
1392 APInt offset(*this - square);
1393 if (offset.ult(midpoint))
1394 return x_old;
1395 else
1396 return x_old + 1;
1397 } else
1398 assert(0 && "Error in APInt::sqrt computation");
1399 return x_old + 1;
1400}
1401
1402/// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1403/// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1404/// variables here have the same names as in the algorithm. Comments explain
1405/// the algorithm and any deviation from it.
1406static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
1407 uint32_t m, uint32_t n) {
1408 assert(u && "Must provide dividend");
1409 assert(v && "Must provide divisor");
1410 assert(q && "Must provide quotient");
1411 assert(u != v && u != q && v != q && "Must us different memory");
1412 assert(n>1 && "n must be > 1");
1413
1414 // Knuth uses the value b as the base of the number system. In our case b
1415 // is 2^31 so we just set it to -1u.
1416 uint64_t b = uint64_t(1) << 32;
1417
1418 DEBUG(cerr << "KnuthDiv: m=" << m << " n=" << n << '\n');
1419 DEBUG(cerr << "KnuthDiv: original:");
1420 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1421 DEBUG(cerr << " by");
1422 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1423 DEBUG(cerr << '\n');
1424 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1425 // u and v by d. Note that we have taken Knuth's advice here to use a power
1426 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1427 // 2 allows us to shift instead of multiply and it is easy to determine the
1428 // shift amount from the leading zeros. We are basically normalizing the u
1429 // and v so that its high bits are shifted to the top of v's range without
1430 // overflow. Note that this can require an extra word in u so that u must
1431 // be of length m+n+1.
1432 uint32_t shift = CountLeadingZeros_32(v[n-1]);
1433 uint32_t v_carry = 0;
1434 uint32_t u_carry = 0;
1435 if (shift) {
1436 for (uint32_t i = 0; i < m+n; ++i) {
1437 uint32_t u_tmp = u[i] >> (32 - shift);
1438 u[i] = (u[i] << shift) | u_carry;
1439 u_carry = u_tmp;
1440 }
1441 for (uint32_t i = 0; i < n; ++i) {
1442 uint32_t v_tmp = v[i] >> (32 - shift);
1443 v[i] = (v[i] << shift) | v_carry;
1444 v_carry = v_tmp;
1445 }
1446 }
1447 u[m+n] = u_carry;
1448 DEBUG(cerr << "KnuthDiv: normal:");
1449 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << std::setbase(16) << u[i]);
1450 DEBUG(cerr << " by");
1451 DEBUG(for (int i = n; i >0; i--) cerr << " " << std::setbase(16) << v[i-1]);
1452 DEBUG(cerr << '\n');
1453
1454 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1455 int j = m;
1456 do {
1457 DEBUG(cerr << "KnuthDiv: quotient digit #" << j << '\n');
1458 // D3. [Calculate q'.].
1459 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1460 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1461 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1462 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1463 // on v[n-2] determines at high speed most of the cases in which the trial
1464 // value qp is one too large, and it eliminates all cases where qp is two
1465 // too large.
1466 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1467 DEBUG(cerr << "KnuthDiv: dividend == " << dividend << '\n');
1468 uint64_t qp = dividend / v[n-1];
1469 uint64_t rp = dividend % v[n-1];
1470 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1471 qp--;
1472 rp += v[n-1];
1473 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1474 qp--;
1475 }
1476 DEBUG(cerr << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
1477
1478 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1479 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1480 // consists of a simple multiplication by a one-place number, combined with
1481 // a subtraction.
1482 bool isNeg = false;
1483 for (uint32_t i = 0; i < n; ++i) {
1484 uint64_t u_tmp = uint64_t(u[j+i]) | (uint64_t(u[j+i+1]) << 32);
1485 uint64_t subtrahend = uint64_t(qp) * uint64_t(v[i]);
1486 bool borrow = subtrahend > u_tmp;
1487 DEBUG(cerr << "KnuthDiv: u_tmp == " << u_tmp
1488 << ", subtrahend == " << subtrahend
1489 << ", borrow = " << borrow << '\n');
1490
1491 uint64_t result = u_tmp - subtrahend;
1492 uint32_t k = j + i;
1493 u[k++] = result & (b-1); // subtract low word
1494 u[k++] = result >> 32; // subtract high word
1495 while (borrow && k <= m+n) { // deal with borrow to the left
1496 borrow = u[k] == 0;
1497 u[k]--;
1498 k++;
1499 }
1500 isNeg |= borrow;
1501 DEBUG(cerr << "KnuthDiv: u[j+i] == " << u[j+i] << ", u[j+i+1] == " <<
1502 u[j+i+1] << '\n');
1503 }
1504 DEBUG(cerr << "KnuthDiv: after subtraction:");
1505 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1506 DEBUG(cerr << '\n');
1507 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1508 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1509 // true value plus b**(n+1), namely as the b's complement of
1510 // the true value, and a "borrow" to the left should be remembered.
1511 //
1512 if (isNeg) {
1513 bool carry = true; // true because b's complement is "complement + 1"
1514 for (uint32_t i = 0; i <= m+n; ++i) {
1515 u[i] = ~u[i] + carry; // b's complement
1516 carry = carry && u[i] == 0;
1517 }
1518 }
1519 DEBUG(cerr << "KnuthDiv: after complement:");
1520 DEBUG(for (int i = m+n; i >=0; i--) cerr << " " << u[i]);
1521 DEBUG(cerr << '\n');
1522
1523 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1524 // negative, go to step D6; otherwise go on to step D7.
1525 q[j] = qp;
1526 if (isNeg) {
1527 // D6. [Add back]. The probability that this step is necessary is very
1528 // small, on the order of only 2/b. Make sure that test data accounts for
1529 // this possibility. Decrease q[j] by 1
1530 q[j]--;
1531 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1532 // A carry will occur to the left of u[j+n], and it should be ignored
1533 // since it cancels with the borrow that occurred in D4.
1534 bool carry = false;
1535 for (uint32_t i = 0; i < n; i++) {
1536 uint32_t limit = std::min(u[j+i],v[i]);
1537 u[j+i] += v[i] + carry;
1538 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1539 }
1540 u[j+n] += carry;
1541 }
1542 DEBUG(cerr << "KnuthDiv: after correction:");
1543 DEBUG(for (int i = m+n; i >=0; i--) cerr <<" " << u[i]);
1544 DEBUG(cerr << "\nKnuthDiv: digit result = " << q[j] << '\n');
1545
1546 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1547 } while (--j >= 0);
1548
1549 DEBUG(cerr << "KnuthDiv: quotient:");
1550 DEBUG(for (int i = m; i >=0; i--) cerr <<" " << q[i]);
1551 DEBUG(cerr << '\n');
1552
1553 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1554 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1555 // compute the remainder (urem uses this).
1556 if (r) {
1557 // The value d is expressed by the "shift" value above since we avoided
1558 // multiplication by d by using a shift left. So, all we have to do is
1559 // shift right here. In order to mak
1560 if (shift) {
1561 uint32_t carry = 0;
1562 DEBUG(cerr << "KnuthDiv: remainder:");
1563 for (int i = n-1; i >= 0; i--) {
1564 r[i] = (u[i] >> shift) | carry;
1565 carry = u[i] << (32 - shift);
1566 DEBUG(cerr << " " << r[i]);
1567 }
1568 } else {
1569 for (int i = n-1; i >= 0; i--) {
1570 r[i] = u[i];
1571 DEBUG(cerr << " " << r[i]);
1572 }
1573 }
1574 DEBUG(cerr << '\n');
1575 }
1576 DEBUG(cerr << std::setbase(10) << '\n');
1577}
1578
1579void APInt::divide(const APInt LHS, uint32_t lhsWords,
1580 const APInt &RHS, uint32_t rhsWords,
1581 APInt *Quotient, APInt *Remainder)
1582{
1583 assert(lhsWords >= rhsWords && "Fractional result");
1584
1585 // First, compose the values into an array of 32-bit words instead of
1586 // 64-bit words. This is a necessity of both the "short division" algorithm
1587 // and the the Knuth "classical algorithm" which requires there to be native
1588 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1589 // can't use 64-bit operands here because we don't have native results of
1590 // 128-bits. Furthremore, casting the 64-bit values to 32-bit values won't
1591 // work on large-endian machines.
1592 uint64_t mask = ~0ull >> (sizeof(uint32_t)*8);
1593 uint32_t n = rhsWords * 2;
1594 uint32_t m = (lhsWords * 2) - n;
1595
1596 // Allocate space for the temporary values we need either on the stack, if
1597 // it will fit, or on the heap if it won't.
1598 uint32_t SPACE[128];
1599 uint32_t *U = 0;
1600 uint32_t *V = 0;
1601 uint32_t *Q = 0;
1602 uint32_t *R = 0;
1603 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1604 U = &SPACE[0];
1605 V = &SPACE[m+n+1];
1606 Q = &SPACE[(m+n+1) + n];
1607 if (Remainder)
1608 R = &SPACE[(m+n+1) + n + (m+n)];
1609 } else {
1610 U = new uint32_t[m + n + 1];
1611 V = new uint32_t[n];
1612 Q = new uint32_t[m+n];
1613 if (Remainder)
1614 R = new uint32_t[n];
1615 }
1616
1617 // Initialize the dividend
1618 memset(U, 0, (m+n+1)*sizeof(uint32_t));
1619 for (unsigned i = 0; i < lhsWords; ++i) {
1620 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
1621 U[i * 2] = tmp & mask;
1622 U[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1623 }
1624 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1625
1626 // Initialize the divisor
1627 memset(V, 0, (n)*sizeof(uint32_t));
1628 for (unsigned i = 0; i < rhsWords; ++i) {
1629 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
1630 V[i * 2] = tmp & mask;
1631 V[i * 2 + 1] = tmp >> (sizeof(uint32_t)*8);
1632 }
1633
1634 // initialize the quotient and remainder
1635 memset(Q, 0, (m+n) * sizeof(uint32_t));
1636 if (Remainder)
1637 memset(R, 0, n * sizeof(uint32_t));
1638
1639 // Now, adjust m and n for the Knuth division. n is the number of words in
1640 // the divisor. m is the number of words by which the dividend exceeds the
1641 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1642 // contain any zero words or the Knuth algorithm fails.
1643 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1644 n--;
1645 m++;
1646 }
1647 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1648 m--;
1649
1650 // If we're left with only a single word for the divisor, Knuth doesn't work
1651 // so we implement the short division algorithm here. This is much simpler
1652 // and faster because we are certain that we can divide a 64-bit quantity
1653 // by a 32-bit quantity at hardware speed and short division is simply a
1654 // series of such operations. This is just like doing short division but we
1655 // are using base 2^32 instead of base 10.
1656 assert(n != 0 && "Divide by zero?");
1657 if (n == 1) {
1658 uint32_t divisor = V[0];
1659 uint32_t remainder = 0;
1660 for (int i = m+n-1; i >= 0; i--) {
1661 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1662 if (partial_dividend == 0) {
1663 Q[i] = 0;
1664 remainder = 0;
1665 } else if (partial_dividend < divisor) {
1666 Q[i] = 0;
1667 remainder = partial_dividend;
1668 } else if (partial_dividend == divisor) {
1669 Q[i] = 1;
1670 remainder = 0;
1671 } else {
1672 Q[i] = partial_dividend / divisor;
1673 remainder = partial_dividend - (Q[i] * divisor);
1674 }
1675 }
1676 if (R)
1677 R[0] = remainder;
1678 } else {
1679 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1680 // case n > 1.
1681 KnuthDiv(U, V, Q, R, m, n);
1682 }
1683
1684 // If the caller wants the quotient
1685 if (Quotient) {
1686 // Set up the Quotient value's memory.
1687 if (Quotient->BitWidth != LHS.BitWidth) {
1688 if (Quotient->isSingleWord())
1689 Quotient->VAL = 0;
1690 else
1691 delete [] Quotient->pVal;
1692 Quotient->BitWidth = LHS.BitWidth;
1693 if (!Quotient->isSingleWord())
1694 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
1695 } else
1696 Quotient->clear();
1697
1698 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1699 // order words.
1700 if (lhsWords == 1) {
1701 uint64_t tmp =
1702 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1703 if (Quotient->isSingleWord())
1704 Quotient->VAL = tmp;
1705 else
1706 Quotient->pVal[0] = tmp;
1707 } else {
1708 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1709 for (unsigned i = 0; i < lhsWords; ++i)
1710 Quotient->pVal[i] =
1711 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1712 }
1713 }
1714
1715 // If the caller wants the remainder
1716 if (Remainder) {
1717 // Set up the Remainder value's memory.
1718 if (Remainder->BitWidth != RHS.BitWidth) {
1719 if (Remainder->isSingleWord())
1720 Remainder->VAL = 0;
1721 else
1722 delete [] Remainder->pVal;
1723 Remainder->BitWidth = RHS.BitWidth;
1724 if (!Remainder->isSingleWord())
1725 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
1726 } else
1727 Remainder->clear();
1728
1729 // The remainder is in R. Reconstitute the remainder into Remainder's low
1730 // order words.
1731 if (rhsWords == 1) {
1732 uint64_t tmp =
1733 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1734 if (Remainder->isSingleWord())
1735 Remainder->VAL = tmp;
1736 else
1737 Remainder->pVal[0] = tmp;
1738 } else {
1739 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1740 for (unsigned i = 0; i < rhsWords; ++i)
1741 Remainder->pVal[i] =
1742 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1743 }
1744 }
1745
1746 // Clean up the memory we allocated.
1747 if (U != &SPACE[0]) {
1748 delete [] U;
1749 delete [] V;
1750 delete [] Q;
1751 delete [] R;
1752 }
1753}
1754
1755APInt APInt::udiv(const APInt& RHS) const {
1756 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1757
1758 // First, deal with the easy case
1759 if (isSingleWord()) {
1760 assert(RHS.VAL != 0 && "Divide by zero?");
1761 return APInt(BitWidth, VAL / RHS.VAL);
1762 }
1763
1764 // Get some facts about the LHS and RHS number of bits and words
1765 uint32_t rhsBits = RHS.getActiveBits();
1766 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1767 assert(rhsWords && "Divided by zero???");
1768 uint32_t lhsBits = this->getActiveBits();
1769 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1770
1771 // Deal with some degenerate cases
1772 if (!lhsWords)
1773 // 0 / X ===> 0
1774 return APInt(BitWidth, 0);
1775 else if (lhsWords < rhsWords || this->ult(RHS)) {
1776 // X / Y ===> 0, iff X < Y
1777 return APInt(BitWidth, 0);
1778 } else if (*this == RHS) {
1779 // X / X ===> 1
1780 return APInt(BitWidth, 1);
1781 } else if (lhsWords == 1 && rhsWords == 1) {
1782 // All high words are zero, just use native divide
1783 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
1784 }
1785
1786 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1787 APInt Quotient(1,0); // to hold result.
1788 divide(*this, lhsWords, RHS, rhsWords, &Quotient, 0);
1789 return Quotient;
1790}
1791
1792APInt APInt::urem(const APInt& RHS) const {
1793 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1794 if (isSingleWord()) {
1795 assert(RHS.VAL != 0 && "Remainder by zero?");
1796 return APInt(BitWidth, VAL % RHS.VAL);
1797 }
1798
1799 // Get some facts about the LHS
1800 uint32_t lhsBits = getActiveBits();
1801 uint32_t lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1802
1803 // Get some facts about the RHS
1804 uint32_t rhsBits = RHS.getActiveBits();
1805 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1806 assert(rhsWords && "Performing remainder operation by zero ???");
1807
1808 // Check the degenerate cases
1809 if (lhsWords == 0) {
1810 // 0 % Y ===> 0
1811 return APInt(BitWidth, 0);
1812 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1813 // X % Y ===> X, iff X < Y
1814 return *this;
1815 } else if (*this == RHS) {
1816 // X % X == 0;
1817 return APInt(BitWidth, 0);
1818 } else if (lhsWords == 1) {
1819 // All high words are zero, just use native remainder
1820 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
1821 }
1822
1823 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1824 APInt Remainder(1,0);
1825 divide(*this, lhsWords, RHS, rhsWords, 0, &Remainder);
1826 return Remainder;
1827}
1828
1829void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1830 APInt &Quotient, APInt &Remainder) {
1831 // Get some size facts about the dividend and divisor
1832 uint32_t lhsBits = LHS.getActiveBits();
1833 uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1834 uint32_t rhsBits = RHS.getActiveBits();
1835 uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1836
1837 // Check the degenerate cases
1838 if (lhsWords == 0) {
1839 Quotient = 0; // 0 / Y ===> 0
1840 Remainder = 0; // 0 % Y ===> 0
1841 return;
1842 }
1843
1844 if (lhsWords < rhsWords || LHS.ult(RHS)) {
1845 Quotient = 0; // X / Y ===> 0, iff X < Y
1846 Remainder = LHS; // X % Y ===> X, iff X < Y
1847 return;
1848 }
1849
1850 if (LHS == RHS) {
1851 Quotient = 1; // X / X ===> 1
1852 Remainder = 0; // X % X ===> 0;
1853 return;
1854 }
1855
1856 if (lhsWords == 1 && rhsWords == 1) {
1857 // There is only one word to consider so use the native versions.
1858 if (LHS.isSingleWord()) {
1859 Quotient = APInt(LHS.getBitWidth(), LHS.VAL / RHS.VAL);
1860 Remainder = APInt(LHS.getBitWidth(), LHS.VAL % RHS.VAL);
1861 } else {
1862 Quotient = APInt(LHS.getBitWidth(), LHS.pVal[0] / RHS.pVal[0]);
1863 Remainder = APInt(LHS.getBitWidth(), LHS.pVal[0] % RHS.pVal[0]);
1864 }
1865 return;
1866 }
1867
1868 // Okay, lets do it the long way
1869 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1870}
1871
1872void APInt::fromString(uint32_t numbits, const char *str, uint32_t slen,
1873 uint8_t radix) {
1874 // Check our assumptions here
1875 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1876 "Radix should be 2, 8, 10, or 16!");
1877 assert(str && "String is null?");
1878 bool isNeg = str[0] == '-';
1879 if (isNeg)
1880 str++, slen--;
1881 assert((slen <= numbits || radix != 2) && "Insufficient bit width");
1882 assert((slen*3 <= numbits || radix != 8) && "Insufficient bit width");
1883 assert((slen*4 <= numbits || radix != 16) && "Insufficient bit width");
1884 assert(((slen*64)/22 <= numbits || radix != 10) && "Insufficient bit width");
1885
1886 // Allocate memory
1887 if (!isSingleWord())
1888 pVal = getClearedMemory(getNumWords());
1889
1890 // Figure out if we can shift instead of multiply
1891 uint32_t shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
1892
1893 // Set up an APInt for the digit to add outside the loop so we don't
1894 // constantly construct/destruct it.
1895 APInt apdigit(getBitWidth(), 0);
1896 APInt apradix(getBitWidth(), radix);
1897
1898 // Enter digit traversal loop
1899 for (unsigned i = 0; i < slen; i++) {
1900 // Get a digit
1901 uint32_t digit = 0;
1902 char cdigit = str[i];
1903 if (radix == 16) {
1904 if (!isxdigit(cdigit))
1905 assert(0 && "Invalid hex digit in string");
1906 if (isdigit(cdigit))
1907 digit = cdigit - '0';
1908 else if (cdigit >= 'a')
1909 digit = cdigit - 'a' + 10;
1910 else if (cdigit >= 'A')
1911 digit = cdigit - 'A' + 10;
1912 else
1913 assert(0 && "huh? we shouldn't get here");
1914 } else if (isdigit(cdigit)) {
1915 digit = cdigit - '0';
1916 } else {
1917 assert(0 && "Invalid character in digit string");
1918 }
1919
1920 // Shift or multiply the value by the radix
1921 if (shift)
1922 *this <<= shift;
1923 else
1924 *this *= apradix;
1925
1926 // Add in the digit we just interpreted
1927 if (apdigit.isSingleWord())
1928 apdigit.VAL = digit;
1929 else
1930 apdigit.pVal[0] = digit;
1931 *this += apdigit;
1932 }
1933 // If its negative, put it in two's complement form
1934 if (isNeg) {
1935 (*this)--;
1936 this->flip();
1937 }
1938}
1939
1940std::string APInt::toString(uint8_t radix, bool wantSigned) const {
1941 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2) &&
1942 "Radix should be 2, 8, 10, or 16!");
1943 static const char *digits[] = {
1944 "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
1945 };
1946 std::string result;
1947 uint32_t bits_used = getActiveBits();
1948 if (isSingleWord()) {
1949 char buf[65];
1950 const char *format = (radix == 10 ? (wantSigned ? "%lld" : "%llu") :
1951 (radix == 16 ? "%llX" : (radix == 8 ? "%llo" : 0)));
1952 if (format) {
1953 if (wantSigned) {
1954 int64_t sextVal = (int64_t(VAL) << (APINT_BITS_PER_WORD-BitWidth)) >>
1955 (APINT_BITS_PER_WORD-BitWidth);
1956 sprintf(buf, format, sextVal);
1957 } else
1958 sprintf(buf, format, VAL);
1959 } else {
1960 memset(buf, 0, 65);
1961 uint64_t v = VAL;
1962 while (bits_used) {
1963 uint32_t bit = v & 1;
1964 bits_used--;
1965 buf[bits_used] = digits[bit][0];
1966 v >>=1;
1967 }
1968 }
1969 result = buf;
1970 return result;
1971 }
1972
1973 if (radix != 10) {
1974 // For the 2, 8 and 16 bit cases, we can just shift instead of divide
1975 // because the number of bits per digit (1,3 and 4 respectively) divides
1976 // equaly. We just shift until there value is zero.
1977
1978 // First, check for a zero value and just short circuit the logic below.
1979 if (*this == 0)
1980 result = "0";
1981 else {
1982 APInt tmp(*this);
1983 size_t insert_at = 0;
1984 if (wantSigned && this->isNegative()) {
1985 // They want to print the signed version and it is a negative value
1986 // Flip the bits and add one to turn it into the equivalent positive
1987 // value and put a '-' in the result.
1988 tmp.flip();
1989 tmp++;
1990 result = "-";
1991 insert_at = 1;
1992 }
1993 // Just shift tmp right for each digit width until it becomes zero
1994 uint32_t shift = (radix == 16 ? 4 : (radix == 8 ? 3 : 1));
1995 uint64_t mask = radix - 1;
1996 APInt zero(tmp.getBitWidth(), 0);
1997 while (tmp.ne(zero)) {
1998 unsigned digit = (tmp.isSingleWord() ? tmp.VAL : tmp.pVal[0]) & mask;
1999 result.insert(insert_at, digits[digit]);
2000 tmp = tmp.lshr(shift);
2001 }
2002 }
2003 return result;
2004 }
2005
2006 APInt tmp(*this);
2007 APInt divisor(4, radix);
2008 APInt zero(tmp.getBitWidth(), 0);
2009 size_t insert_at = 0;
2010 if (wantSigned && tmp[BitWidth-1]) {
2011 // They want to print the signed version and it is a negative value
2012 // Flip the bits and add one to turn it into the equivalent positive
2013 // value and put a '-' in the result.
2014 tmp.flip();
2015 tmp++;
2016 result = "-";
2017 insert_at = 1;
2018 }
2019 if (tmp == APInt(tmp.getBitWidth(), 0))
2020 result = "0";
2021 else while (tmp.ne(zero)) {
2022 APInt APdigit(1,0);
2023 APInt tmp2(tmp.getBitWidth(), 0);
2024 divide(tmp, tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
2025 &APdigit);
2026 uint32_t digit = APdigit.getZExtValue();
2027 assert(digit < radix && "divide failed");
2028 result.insert(insert_at,digits[digit]);
2029 tmp = tmp2;
2030 }
2031
2032 return result;
2033}
2034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002035void APInt::dump() const
2036{
2037 cerr << "APInt(" << BitWidth << ")=" << std::setbase(16);
2038 if (isSingleWord())
2039 cerr << VAL;
2040 else for (unsigned i = getNumWords(); i > 0; i--) {
2041 cerr << pVal[i-1] << " ";
2042 }
Chris Lattner9b502d42007-08-23 05:15:32 +00002043 cerr << " U(" << this->toStringUnsigned(10) << ") S("
Dale Johannesen2fc20782007-09-14 22:26:36 +00002044 << this->toStringSigned(10) << ")" << std::setbase(10);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045}
Chris Lattner73cde982007-08-16 15:56:55 +00002046
2047// This implements a variety of operations on a representation of
2048// arbitrary precision, two's-complement, bignum integer values.
2049
2050/* Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2051 and unrestricting assumption. */
Chris Lattnerdb80e212007-08-20 22:49:32 +00002052COMPILE_TIME_ASSERT(integerPartWidth % 2 == 0);
Chris Lattner73cde982007-08-16 15:56:55 +00002053
2054/* Some handy functions local to this file. */
2055namespace {
2056
Chris Lattnerdb80e212007-08-20 22:49:32 +00002057 /* Returns the integer part with the least significant BITS set.
2058 BITS cannot be zero. */
2059 inline integerPart
2060 lowBitMask(unsigned int bits)
2061 {
2062 assert (bits != 0 && bits <= integerPartWidth);
2063
2064 return ~(integerPart) 0 >> (integerPartWidth - bits);
2065 }
2066
Neil Booth58ffb232007-10-06 00:43:45 +00002067 /* Returns the value of the lower half of PART. */
Chris Lattnerdb80e212007-08-20 22:49:32 +00002068 inline integerPart
2069 lowHalf(integerPart part)
2070 {
2071 return part & lowBitMask(integerPartWidth / 2);
2072 }
2073
Neil Booth58ffb232007-10-06 00:43:45 +00002074 /* Returns the value of the upper half of PART. */
Chris Lattnerdb80e212007-08-20 22:49:32 +00002075 inline integerPart
2076 highHalf(integerPart part)
2077 {
2078 return part >> (integerPartWidth / 2);
2079 }
2080
Neil Booth58ffb232007-10-06 00:43:45 +00002081 /* Returns the bit number of the most significant set bit of a part.
2082 If the input number has no bits set -1U is returned. */
Chris Lattner73cde982007-08-16 15:56:55 +00002083 unsigned int
Chris Lattnerdb80e212007-08-20 22:49:32 +00002084 partMSB(integerPart value)
Chris Lattner73cde982007-08-16 15:56:55 +00002085 {
2086 unsigned int n, msb;
2087
2088 if (value == 0)
2089 return -1U;
2090
2091 n = integerPartWidth / 2;
2092
2093 msb = 0;
2094 do {
2095 if (value >> n) {
2096 value >>= n;
2097 msb += n;
2098 }
2099
2100 n >>= 1;
2101 } while (n);
2102
2103 return msb;
2104 }
2105
Neil Booth58ffb232007-10-06 00:43:45 +00002106 /* Returns the bit number of the least significant set bit of a
2107 part. If the input number has no bits set -1U is returned. */
Chris Lattner73cde982007-08-16 15:56:55 +00002108 unsigned int
2109 partLSB(integerPart value)
2110 {
2111 unsigned int n, lsb;
2112
2113 if (value == 0)
2114 return -1U;
2115
2116 lsb = integerPartWidth - 1;
2117 n = integerPartWidth / 2;
2118
2119 do {
2120 if (value << n) {
2121 value <<= n;
2122 lsb -= n;
2123 }
2124
2125 n >>= 1;
2126 } while (n);
2127
2128 return lsb;
2129 }
2130}
2131
2132/* Sets the least significant part of a bignum to the input value, and
2133 zeroes out higher parts. */
2134void
2135APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts)
2136{
2137 unsigned int i;
2138
Neil Bootha0f524a2007-10-08 13:47:12 +00002139 assert (parts > 0);
2140
Chris Lattner73cde982007-08-16 15:56:55 +00002141 dst[0] = part;
2142 for(i = 1; i < parts; i++)
2143 dst[i] = 0;
2144}
2145
2146/* Assign one bignum to another. */
2147void
2148APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts)
2149{
2150 unsigned int i;
2151
2152 for(i = 0; i < parts; i++)
2153 dst[i] = src[i];
2154}
2155
2156/* Returns true if a bignum is zero, false otherwise. */
2157bool
2158APInt::tcIsZero(const integerPart *src, unsigned int parts)
2159{
2160 unsigned int i;
2161
2162 for(i = 0; i < parts; i++)
2163 if (src[i])
2164 return false;
2165
2166 return true;
2167}
2168
2169/* Extract the given bit of a bignum; returns 0 or 1. */
2170int
2171APInt::tcExtractBit(const integerPart *parts, unsigned int bit)
2172{
2173 return(parts[bit / integerPartWidth]
2174 & ((integerPart) 1 << bit % integerPartWidth)) != 0;
2175}
2176
2177/* Set the given bit of a bignum. */
2178void
2179APInt::tcSetBit(integerPart *parts, unsigned int bit)
2180{
2181 parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
2182}
2183
Neil Booth58ffb232007-10-06 00:43:45 +00002184/* Returns the bit number of the least significant set bit of a
2185 number. If the input number has no bits set -1U is returned. */
Chris Lattner73cde982007-08-16 15:56:55 +00002186unsigned int
2187APInt::tcLSB(const integerPart *parts, unsigned int n)
2188{
2189 unsigned int i, lsb;
2190
2191 for(i = 0; i < n; i++) {
2192 if (parts[i] != 0) {
2193 lsb = partLSB(parts[i]);
2194
2195 return lsb + i * integerPartWidth;
2196 }
2197 }
2198
2199 return -1U;
2200}
2201
Neil Booth58ffb232007-10-06 00:43:45 +00002202/* Returns the bit number of the most significant set bit of a number.
2203 If the input number has no bits set -1U is returned. */
Chris Lattner73cde982007-08-16 15:56:55 +00002204unsigned int
2205APInt::tcMSB(const integerPart *parts, unsigned int n)
2206{
2207 unsigned int msb;
2208
2209 do {
2210 --n;
2211
2212 if (parts[n] != 0) {
Chris Lattnerdb80e212007-08-20 22:49:32 +00002213 msb = partMSB(parts[n]);
Chris Lattner73cde982007-08-16 15:56:55 +00002214
2215 return msb + n * integerPartWidth;
2216 }
2217 } while (n);
2218
2219 return -1U;
2220}
2221
Neil Bootha0f524a2007-10-08 13:47:12 +00002222/* Copy the bit vector of width srcBITS from SRC, starting at bit
2223 srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
2224 the least significant bit of DST. All high bits above srcBITS in
2225 DST are zero-filled. */
2226void
2227APInt::tcExtract(integerPart *dst, unsigned int dstCount, const integerPart *src,
2228 unsigned int srcBits, unsigned int srcLSB)
2229{
2230 unsigned int firstSrcPart, dstParts, shift, n;
2231
2232 dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
2233 assert (dstParts <= dstCount);
2234
2235 firstSrcPart = srcLSB / integerPartWidth;
2236 tcAssign (dst, src + firstSrcPart, dstParts);
2237
2238 shift = srcLSB % integerPartWidth;
2239 tcShiftRight (dst, dstParts, shift);
2240
2241 /* We now have (dstParts * integerPartWidth - shift) bits from SRC
2242 in DST. If this is less that srcBits, append the rest, else
2243 clear the high bits. */
2244 n = dstParts * integerPartWidth - shift;
2245 if (n < srcBits) {
2246 integerPart mask = lowBitMask (srcBits - n);
2247 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2248 << n % integerPartWidth);
2249 } else if (n > srcBits) {
Neil Booth69731ff2007-10-12 15:31:31 +00002250 if (srcBits % integerPartWidth)
2251 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
Neil Bootha0f524a2007-10-08 13:47:12 +00002252 }
2253
2254 /* Clear high parts. */
2255 while (dstParts < dstCount)
2256 dst[dstParts++] = 0;
2257}
2258
Chris Lattner73cde982007-08-16 15:56:55 +00002259/* DST += RHS + C where C is zero or one. Returns the carry flag. */
2260integerPart
2261APInt::tcAdd(integerPart *dst, const integerPart *rhs,
2262 integerPart c, unsigned int parts)
2263{
2264 unsigned int i;
2265
2266 assert(c <= 1);
2267
2268 for(i = 0; i < parts; i++) {
2269 integerPart l;
2270
2271 l = dst[i];
2272 if (c) {
2273 dst[i] += rhs[i] + 1;
2274 c = (dst[i] <= l);
2275 } else {
2276 dst[i] += rhs[i];
2277 c = (dst[i] < l);
2278 }
2279 }
2280
2281 return c;
2282}
2283
2284/* DST -= RHS + C where C is zero or one. Returns the carry flag. */
2285integerPart
2286APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
2287 integerPart c, unsigned int parts)
2288{
2289 unsigned int i;
2290
2291 assert(c <= 1);
2292
2293 for(i = 0; i < parts; i++) {
2294 integerPart l;
2295
2296 l = dst[i];
2297 if (c) {
2298 dst[i] -= rhs[i] + 1;
2299 c = (dst[i] >= l);
2300 } else {
2301 dst[i] -= rhs[i];
2302 c = (dst[i] > l);
2303 }
2304 }
2305
2306 return c;
2307}
2308
2309/* Negate a bignum in-place. */
2310void
2311APInt::tcNegate(integerPart *dst, unsigned int parts)
2312{
2313 tcComplement(dst, parts);
2314 tcIncrement(dst, parts);
2315}
2316
Neil Booth58ffb232007-10-06 00:43:45 +00002317/* DST += SRC * MULTIPLIER + CARRY if add is true
2318 DST = SRC * MULTIPLIER + CARRY if add is false
Chris Lattner73cde982007-08-16 15:56:55 +00002319
2320 Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
2321 they must start at the same point, i.e. DST == SRC.
2322
2323 If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2324 returned. Otherwise DST is filled with the least significant
2325 DSTPARTS parts of the result, and if all of the omitted higher
2326 parts were zero return zero, otherwise overflow occurred and
2327 return one. */
2328int
2329APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
2330 integerPart multiplier, integerPart carry,
2331 unsigned int srcParts, unsigned int dstParts,
2332 bool add)
2333{
2334 unsigned int i, n;
2335
2336 /* Otherwise our writes of DST kill our later reads of SRC. */
2337 assert(dst <= src || dst >= src + srcParts);
2338 assert(dstParts <= srcParts + 1);
2339
2340 /* N loops; minimum of dstParts and srcParts. */
2341 n = dstParts < srcParts ? dstParts: srcParts;
2342
2343 for(i = 0; i < n; i++) {
2344 integerPart low, mid, high, srcPart;
2345
2346 /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2347
2348 This cannot overflow, because
2349
2350 (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2351
2352 which is less than n^2. */
2353
2354 srcPart = src[i];
2355
2356 if (multiplier == 0 || srcPart == 0) {
2357 low = carry;
2358 high = 0;
2359 } else {
2360 low = lowHalf(srcPart) * lowHalf(multiplier);
2361 high = highHalf(srcPart) * highHalf(multiplier);
2362
2363 mid = lowHalf(srcPart) * highHalf(multiplier);
2364 high += highHalf(mid);
2365 mid <<= integerPartWidth / 2;
2366 if (low + mid < low)
2367 high++;
2368 low += mid;
2369
2370 mid = highHalf(srcPart) * lowHalf(multiplier);
2371 high += highHalf(mid);
2372 mid <<= integerPartWidth / 2;
2373 if (low + mid < low)
2374 high++;
2375 low += mid;
2376
2377 /* Now add carry. */
2378 if (low + carry < low)
2379 high++;
2380 low += carry;
2381 }
2382
2383 if (add) {
2384 /* And now DST[i], and store the new low part there. */
2385 if (low + dst[i] < low)
2386 high++;
2387 dst[i] += low;
2388 } else
2389 dst[i] = low;
2390
2391 carry = high;
2392 }
2393
2394 if (i < dstParts) {
2395 /* Full multiplication, there is no overflow. */
2396 assert(i + 1 == dstParts);
2397 dst[i] = carry;
2398 return 0;
2399 } else {
2400 /* We overflowed if there is carry. */
2401 if (carry)
2402 return 1;
2403
2404 /* We would overflow if any significant unwritten parts would be
2405 non-zero. This is true if any remaining src parts are non-zero
2406 and the multiplier is non-zero. */
2407 if (multiplier)
2408 for(; i < srcParts; i++)
2409 if (src[i])
2410 return 1;
2411
2412 /* We fitted in the narrow destination. */
2413 return 0;
2414 }
2415}
2416
2417/* DST = LHS * RHS, where DST has the same width as the operands and
2418 is filled with the least significant parts of the result. Returns
2419 one if overflow occurred, otherwise zero. DST must be disjoint
2420 from both operands. */
2421int
2422APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
2423 const integerPart *rhs, unsigned int parts)
2424{
2425 unsigned int i;
2426 int overflow;
2427
2428 assert(dst != lhs && dst != rhs);
2429
2430 overflow = 0;
2431 tcSet(dst, 0, parts);
2432
2433 for(i = 0; i < parts; i++)
2434 overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
2435 parts - i, true);
2436
2437 return overflow;
2438}
2439
Neil Booth004e9f42007-10-06 00:24:48 +00002440/* DST = LHS * RHS, where DST has width the sum of the widths of the
2441 operands. No overflow occurs. DST must be disjoint from both
2442 operands. Returns the number of parts required to hold the
2443 result. */
2444unsigned int
Chris Lattner73cde982007-08-16 15:56:55 +00002445APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
Neil Booth004e9f42007-10-06 00:24:48 +00002446 const integerPart *rhs, unsigned int lhsParts,
2447 unsigned int rhsParts)
Chris Lattner73cde982007-08-16 15:56:55 +00002448{
Neil Booth004e9f42007-10-06 00:24:48 +00002449 /* Put the narrower number on the LHS for less loops below. */
2450 if (lhsParts > rhsParts) {
2451 return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
2452 } else {
2453 unsigned int n;
Chris Lattner73cde982007-08-16 15:56:55 +00002454
Neil Booth004e9f42007-10-06 00:24:48 +00002455 assert(dst != lhs && dst != rhs);
Chris Lattner73cde982007-08-16 15:56:55 +00002456
Neil Booth004e9f42007-10-06 00:24:48 +00002457 tcSet(dst, 0, rhsParts);
Chris Lattner73cde982007-08-16 15:56:55 +00002458
Neil Booth004e9f42007-10-06 00:24:48 +00002459 for(n = 0; n < lhsParts; n++)
2460 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true);
Chris Lattner73cde982007-08-16 15:56:55 +00002461
Neil Booth004e9f42007-10-06 00:24:48 +00002462 n = lhsParts + rhsParts;
2463
2464 return n - (dst[n - 1] == 0);
2465 }
Chris Lattner73cde982007-08-16 15:56:55 +00002466}
2467
2468/* If RHS is zero LHS and REMAINDER are left unchanged, return one.
2469 Otherwise set LHS to LHS / RHS with the fractional part discarded,
2470 set REMAINDER to the remainder, return zero. i.e.
2471
2472 OLD_LHS = RHS * LHS + REMAINDER
2473
2474 SCRATCH is a bignum of the same size as the operands and result for
2475 use by the routine; its contents need not be initialized and are
2476 destroyed. LHS, REMAINDER and SCRATCH must be distinct.
2477*/
2478int
2479APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
2480 integerPart *remainder, integerPart *srhs,
2481 unsigned int parts)
2482{
2483 unsigned int n, shiftCount;
2484 integerPart mask;
2485
2486 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2487
Chris Lattnerdb80e212007-08-20 22:49:32 +00002488 shiftCount = tcMSB(rhs, parts) + 1;
2489 if (shiftCount == 0)
Chris Lattner73cde982007-08-16 15:56:55 +00002490 return true;
2491
Chris Lattnerdb80e212007-08-20 22:49:32 +00002492 shiftCount = parts * integerPartWidth - shiftCount;
Chris Lattner73cde982007-08-16 15:56:55 +00002493 n = shiftCount / integerPartWidth;
2494 mask = (integerPart) 1 << (shiftCount % integerPartWidth);
2495
2496 tcAssign(srhs, rhs, parts);
2497 tcShiftLeft(srhs, parts, shiftCount);
2498 tcAssign(remainder, lhs, parts);
2499 tcSet(lhs, 0, parts);
2500
2501 /* Loop, subtracting SRHS if REMAINDER is greater and adding that to
2502 the total. */
2503 for(;;) {
2504 int compare;
2505
2506 compare = tcCompare(remainder, srhs, parts);
2507 if (compare >= 0) {
2508 tcSubtract(remainder, srhs, 0, parts);
2509 lhs[n] |= mask;
2510 }
2511
2512 if (shiftCount == 0)
2513 break;
2514 shiftCount--;
2515 tcShiftRight(srhs, parts, 1);
2516 if ((mask >>= 1) == 0)
2517 mask = (integerPart) 1 << (integerPartWidth - 1), n--;
2518 }
2519
2520 return false;
2521}
2522
2523/* Shift a bignum left COUNT bits in-place. Shifted in bits are zero.
2524 There are no restrictions on COUNT. */
2525void
2526APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count)
2527{
Neil Bootha0f524a2007-10-08 13:47:12 +00002528 if (count) {
2529 unsigned int jump, shift;
Chris Lattner73cde982007-08-16 15:56:55 +00002530
Neil Bootha0f524a2007-10-08 13:47:12 +00002531 /* Jump is the inter-part jump; shift is is intra-part shift. */
2532 jump = count / integerPartWidth;
2533 shift = count % integerPartWidth;
Chris Lattner73cde982007-08-16 15:56:55 +00002534
Neil Bootha0f524a2007-10-08 13:47:12 +00002535 while (parts > jump) {
2536 integerPart part;
Chris Lattner73cde982007-08-16 15:56:55 +00002537
Neil Bootha0f524a2007-10-08 13:47:12 +00002538 parts--;
Chris Lattner73cde982007-08-16 15:56:55 +00002539
Neil Bootha0f524a2007-10-08 13:47:12 +00002540 /* dst[i] comes from the two parts src[i - jump] and, if we have
2541 an intra-part shift, src[i - jump - 1]. */
2542 part = dst[parts - jump];
2543 if (shift) {
2544 part <<= shift;
Chris Lattner73cde982007-08-16 15:56:55 +00002545 if (parts >= jump + 1)
2546 part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
2547 }
2548
Neil Bootha0f524a2007-10-08 13:47:12 +00002549 dst[parts] = part;
2550 }
Chris Lattner73cde982007-08-16 15:56:55 +00002551
Neil Bootha0f524a2007-10-08 13:47:12 +00002552 while (parts > 0)
2553 dst[--parts] = 0;
2554 }
Chris Lattner73cde982007-08-16 15:56:55 +00002555}
2556
2557/* Shift a bignum right COUNT bits in-place. Shifted in bits are
2558 zero. There are no restrictions on COUNT. */
2559void
2560APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count)
2561{
Neil Bootha0f524a2007-10-08 13:47:12 +00002562 if (count) {
2563 unsigned int i, jump, shift;
Chris Lattner73cde982007-08-16 15:56:55 +00002564
Neil Bootha0f524a2007-10-08 13:47:12 +00002565 /* Jump is the inter-part jump; shift is is intra-part shift. */
2566 jump = count / integerPartWidth;
2567 shift = count % integerPartWidth;
Chris Lattner73cde982007-08-16 15:56:55 +00002568
Neil Bootha0f524a2007-10-08 13:47:12 +00002569 /* Perform the shift. This leaves the most significant COUNT bits
2570 of the result at zero. */
2571 for(i = 0; i < parts; i++) {
2572 integerPart part;
Chris Lattner73cde982007-08-16 15:56:55 +00002573
Neil Bootha0f524a2007-10-08 13:47:12 +00002574 if (i + jump >= parts) {
2575 part = 0;
2576 } else {
2577 part = dst[i + jump];
2578 if (shift) {
2579 part >>= shift;
2580 if (i + jump + 1 < parts)
2581 part |= dst[i + jump + 1] << (integerPartWidth - shift);
2582 }
Chris Lattner73cde982007-08-16 15:56:55 +00002583 }
Chris Lattner73cde982007-08-16 15:56:55 +00002584
Neil Bootha0f524a2007-10-08 13:47:12 +00002585 dst[i] = part;
2586 }
Chris Lattner73cde982007-08-16 15:56:55 +00002587 }
2588}
2589
2590/* Bitwise and of two bignums. */
2591void
2592APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts)
2593{
2594 unsigned int i;
2595
2596 for(i = 0; i < parts; i++)
2597 dst[i] &= rhs[i];
2598}
2599
2600/* Bitwise inclusive or of two bignums. */
2601void
2602APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts)
2603{
2604 unsigned int i;
2605
2606 for(i = 0; i < parts; i++)
2607 dst[i] |= rhs[i];
2608}
2609
2610/* Bitwise exclusive or of two bignums. */
2611void
2612APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts)
2613{
2614 unsigned int i;
2615
2616 for(i = 0; i < parts; i++)
2617 dst[i] ^= rhs[i];
2618}
2619
2620/* Complement a bignum in-place. */
2621void
2622APInt::tcComplement(integerPart *dst, unsigned int parts)
2623{
2624 unsigned int i;
2625
2626 for(i = 0; i < parts; i++)
2627 dst[i] = ~dst[i];
2628}
2629
2630/* Comparison (unsigned) of two bignums. */
2631int
2632APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
2633 unsigned int parts)
2634{
2635 while (parts) {
2636 parts--;
2637 if (lhs[parts] == rhs[parts])
2638 continue;
2639
2640 if (lhs[parts] > rhs[parts])
2641 return 1;
2642 else
2643 return -1;
2644 }
2645
2646 return 0;
2647}
2648
2649/* Increment a bignum in-place, return the carry flag. */
2650integerPart
2651APInt::tcIncrement(integerPart *dst, unsigned int parts)
2652{
2653 unsigned int i;
2654
2655 for(i = 0; i < parts; i++)
2656 if (++dst[i] != 0)
2657 break;
2658
2659 return i == parts;
2660}
2661
2662/* Set the least significant BITS bits of a bignum, clear the
2663 rest. */
2664void
2665APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts,
2666 unsigned int bits)
2667{
2668 unsigned int i;
2669
2670 i = 0;
2671 while (bits > integerPartWidth) {
2672 dst[i++] = ~(integerPart) 0;
2673 bits -= integerPartWidth;
2674 }
2675
2676 if (bits)
2677 dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
2678
2679 while (i < parts)
2680 dst[i++] = 0;
2681}