1 //===-- APInt.cpp - Implement APInt class ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <climits>
25 #include <cmath>
26 #include <cstdlib>
27 #include <cstring>
28 using namespace llvm;
29
30 #define DEBUG_TYPE "apint"
31
32 /// A utility function for allocating memory, checking for allocation failures,
33 /// and ensuring the contents are zeroed.
getClearedMemory(unsigned numWords)34 inline static uint64_t* getClearedMemory(unsigned numWords) {
35 uint64_t * result = new uint64_t[numWords];
36 assert(result && "APInt memory allocation fails!");
37 memset(result, 0, numWords * sizeof(uint64_t));
38 return result;
39 }
40
41 /// A utility function for allocating memory and checking for allocation
42 /// failure. The content is not zeroed.
getMemory(unsigned numWords)43 inline static uint64_t* getMemory(unsigned numWords) {
44 uint64_t * result = new uint64_t[numWords];
45 assert(result && "APInt memory allocation fails!");
46 return result;
47 }
48
49 /// A utility function that converts a character to a digit.
getDigit(char cdigit,uint8_t radix)50 inline static unsigned getDigit(char cdigit, uint8_t radix) {
51 unsigned r;
52
53 if (radix == 16 || radix == 36) {
54 r = cdigit - '0';
55 if (r <= 9)
56 return r;
57
58 r = cdigit - 'A';
59 if (r <= radix - 11U)
60 return r + 10;
61
62 r = cdigit - 'a';
63 if (r <= radix - 11U)
64 return r + 10;
65
66 radix = 10;
67 }
68
69 r = cdigit - '0';
70 if (r < radix)
71 return r;
72
73 return -1U;
74 }
75
76
initSlowCase(uint64_t val,bool isSigned)77 void APInt::initSlowCase(uint64_t val, bool isSigned) {
78 pVal = getClearedMemory(getNumWords());
79 pVal[0] = val;
80 if (isSigned && int64_t(val) < 0)
81 for (unsigned i = 1; i < getNumWords(); ++i)
82 pVal[i] = -1ULL;
83 }
84
initSlowCase(const APInt & that)85 void APInt::initSlowCase(const APInt& that) {
86 pVal = getMemory(getNumWords());
87 memcpy(pVal, that.pVal, getNumWords() * APINT_WORD_SIZE);
88 }
89
initFromArray(ArrayRef<uint64_t> bigVal)90 void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
91 assert(BitWidth && "Bitwidth too small");
92 assert(bigVal.data() && "Null pointer detected!");
93 if (isSingleWord())
94 VAL = bigVal[0];
95 else {
96 // Get memory, cleared to 0
97 pVal = getClearedMemory(getNumWords());
98 // Calculate the number of words to copy
99 unsigned words = std::min<unsigned>(bigVal.size(), getNumWords());
100 // Copy the words from bigVal to pVal
101 memcpy(pVal, bigVal.data(), words * APINT_WORD_SIZE);
102 }
103 // Make sure unused high bits are cleared
104 clearUnusedBits();
105 }
106
APInt(unsigned numBits,ArrayRef<uint64_t> bigVal)107 APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal)
108 : BitWidth(numBits), VAL(0) {
109 initFromArray(bigVal);
110 }
111
APInt(unsigned numBits,unsigned numWords,const uint64_t bigVal[])112 APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[])
113 : BitWidth(numBits), VAL(0) {
114 initFromArray(makeArrayRef(bigVal, numWords));
115 }
116
APInt(unsigned numbits,StringRef Str,uint8_t radix)117 APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix)
118 : BitWidth(numbits), VAL(0) {
119 assert(BitWidth && "Bitwidth too small");
120 fromString(numbits, Str, radix);
121 }
122
AssignSlowCase(const APInt & RHS)123 APInt& APInt::AssignSlowCase(const APInt& RHS) {
124 // Don't do anything for X = X
125 if (this == &RHS)
126 return *this;
127
128 if (BitWidth == RHS.getBitWidth()) {
129 // assume same bit-width single-word case is already handled
130 assert(!isSingleWord());
131 memcpy(pVal, RHS.pVal, getNumWords() * APINT_WORD_SIZE);
132 return *this;
133 }
134
135 if (isSingleWord()) {
136 // assume case where both are single words is already handled
137 assert(!RHS.isSingleWord());
138 VAL = 0;
139 pVal = getMemory(RHS.getNumWords());
140 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
141 } else if (getNumWords() == RHS.getNumWords())
142 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
143 else if (RHS.isSingleWord()) {
144 delete [] pVal;
145 VAL = RHS.VAL;
146 } else {
147 delete [] pVal;
148 pVal = getMemory(RHS.getNumWords());
149 memcpy(pVal, RHS.pVal, RHS.getNumWords() * APINT_WORD_SIZE);
150 }
151 BitWidth = RHS.BitWidth;
152 return clearUnusedBits();
153 }
154
operator =(uint64_t RHS)155 APInt& APInt::operator=(uint64_t RHS) {
156 if (isSingleWord())
157 VAL = RHS;
158 else {
159 pVal[0] = RHS;
160 memset(pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
161 }
162 return clearUnusedBits();
163 }
164
165 /// This function adds a single "digit" integer, y, to the multiple
166 /// "digit" integer array, x[]. x[] is modified to reflect the addition and
167 /// 1 is returned if there is a carry out, otherwise 0 is returned.
168 /// @returns the carry of the addition.
add_1(uint64_t dest[],uint64_t x[],unsigned len,uint64_t y)169 static bool add_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
170 for (unsigned i = 0; i < len; ++i) {
171 dest[i] = y + x[i];
172 if (dest[i] < y)
173 y = 1; // Carry one to next digit.
174 else {
175 y = 0; // No need to carry so exit early
176 break;
177 }
178 }
179 return y;
180 }
181
182 /// @brief Prefix increment operator. Increments the APInt by one.
operator ++()183 APInt& APInt::operator++() {
184 if (isSingleWord())
185 ++VAL;
186 else
187 add_1(pVal, pVal, getNumWords(), 1);
188 return clearUnusedBits();
189 }
190
191 /// This function subtracts a single "digit" (64-bit word), y, from
192 /// the multi-digit integer array, x[], propagating the borrowed 1 value until
193 /// no further borrowing is neeeded or it runs out of "digits" in x. The result
194 /// is 1 if "borrowing" exhausted the digits in x, or 0 if x was not exhausted.
195 /// In other words, if y > x then this function returns 1, otherwise 0.
196 /// @returns the borrow out of the subtraction
sub_1(uint64_t x[],unsigned len,uint64_t y)197 static bool sub_1(uint64_t x[], unsigned len, uint64_t y) {
198 for (unsigned i = 0; i < len; ++i) {
199 uint64_t X = x[i];
200 x[i] -= y;
201 if (y > X)
202 y = 1; // We have to "borrow 1" from next "digit"
203 else {
204 y = 0; // No need to borrow
205 break; // Remaining digits are unchanged so exit early
206 }
207 }
208 return bool(y);
209 }
210
211 /// @brief Prefix decrement operator. Decrements the APInt by one.
operator --()212 APInt& APInt::operator--() {
213 if (isSingleWord())
214 --VAL;
215 else
216 sub_1(pVal, getNumWords(), 1);
217 return clearUnusedBits();
218 }
219
220 /// This function adds the integer array x to the integer array Y and
221 /// places the result in dest.
222 /// @returns the carry out from the addition
223 /// @brief General addition of 64-bit integer arrays
add(uint64_t * dest,const uint64_t * x,const uint64_t * y,unsigned len)224 static bool add(uint64_t *dest, const uint64_t *x, const uint64_t *y,
225 unsigned len) {
226 bool carry = false;
227 for (unsigned i = 0; i< len; ++i) {
228 uint64_t limit = std::min(x[i],y[i]); // must come first in case dest == x
229 dest[i] = x[i] + y[i] + carry;
230 carry = dest[i] < limit || (carry && dest[i] == limit);
231 }
232 return carry;
233 }
234
235 /// Adds the RHS APint to this APInt.
236 /// @returns this, after addition of RHS.
237 /// @brief Addition assignment operator.
operator +=(const APInt & RHS)238 APInt& APInt::operator+=(const APInt& RHS) {
239 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
240 if (isSingleWord())
241 VAL += RHS.VAL;
242 else {
243 add(pVal, pVal, RHS.pVal, getNumWords());
244 }
245 return clearUnusedBits();
246 }
247
operator +=(uint64_t RHS)248 APInt& APInt::operator+=(uint64_t RHS) {
249 if (isSingleWord())
250 VAL += RHS;
251 else
252 add_1(pVal, pVal, getNumWords(), RHS);
253 return clearUnusedBits();
254 }
255
256 /// Subtracts the integer array y from the integer array x
257 /// @returns returns the borrow out.
258 /// @brief Generalized subtraction of 64-bit integer arrays.
sub(uint64_t * dest,const uint64_t * x,const uint64_t * y,unsigned len)259 static bool sub(uint64_t *dest, const uint64_t *x, const uint64_t *y,
260 unsigned len) {
261 bool borrow = false;
262 for (unsigned i = 0; i < len; ++i) {
263 uint64_t x_tmp = borrow ? x[i] - 1 : x[i];
264 borrow = y[i] > x_tmp || (borrow && x[i] == 0);
265 dest[i] = x_tmp - y[i];
266 }
267 return borrow;
268 }
269
270 /// Subtracts the RHS APInt from this APInt
271 /// @returns this, after subtraction
272 /// @brief Subtraction assignment operator.
operator -=(const APInt & RHS)273 APInt& APInt::operator-=(const APInt& RHS) {
274 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
275 if (isSingleWord())
276 VAL -= RHS.VAL;
277 else
278 sub(pVal, pVal, RHS.pVal, getNumWords());
279 return clearUnusedBits();
280 }
281
operator -=(uint64_t RHS)282 APInt& APInt::operator-=(uint64_t RHS) {
283 if (isSingleWord())
284 VAL -= RHS;
285 else
286 sub_1(pVal, getNumWords(), RHS);
287 return clearUnusedBits();
288 }
289
290 /// Multiplies an integer array, x, by a uint64_t integer and places the result
291 /// into dest.
292 /// @returns the carry out of the multiplication.
293 /// @brief Multiply a multi-digit APInt by a single digit (64-bit) integer.
mul_1(uint64_t dest[],uint64_t x[],unsigned len,uint64_t y)294 static uint64_t mul_1(uint64_t dest[], uint64_t x[], unsigned len, uint64_t y) {
295 // Split y into high 32-bit part (hy) and low 32-bit part (ly)
296 uint64_t ly = y & 0xffffffffULL, hy = y >> 32;
297 uint64_t carry = 0;
298
299 // For each digit of x.
300 for (unsigned i = 0; i < len; ++i) {
301 // Split x into high and low words
302 uint64_t lx = x[i] & 0xffffffffULL;
303 uint64_t hx = x[i] >> 32;
304 // hasCarry - A flag to indicate if there is a carry to the next digit.
305 // hasCarry == 0, no carry
306 // hasCarry == 1, has carry
307 // hasCarry == 2, no carry and the calculation result == 0.
308 uint8_t hasCarry = 0;
309 dest[i] = carry + lx * ly;
310 // Determine if the add above introduces carry.
311 hasCarry = (dest[i] < carry) ? 1 : 0;
312 carry = hx * ly + (dest[i] >> 32) + (hasCarry ? (1ULL << 32) : 0);
313 // The upper limit of carry can be (2^32 - 1)(2^32 - 1) +
314 // (2^32 - 1) + 2^32 = 2^64.
315 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
316
317 carry += (lx * hy) & 0xffffffffULL;
318 dest[i] = (carry << 32) | (dest[i] & 0xffffffffULL);
319 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0) +
320 (carry >> 32) + ((lx * hy) >> 32) + hx * hy;
321 }
322 return carry;
323 }
324
325 /// Multiplies integer array x by integer array y and stores the result into
326 /// the integer array dest. Note that dest's size must be >= xlen + ylen.
327 /// @brief Generalized multiplicate of integer arrays.
mul(uint64_t dest[],uint64_t x[],unsigned xlen,uint64_t y[],unsigned ylen)328 static void mul(uint64_t dest[], uint64_t x[], unsigned xlen, uint64_t y[],
329 unsigned ylen) {
330 dest[xlen] = mul_1(dest, x, xlen, y[0]);
331 for (unsigned i = 1; i < ylen; ++i) {
332 uint64_t ly = y[i] & 0xffffffffULL, hy = y[i] >> 32;
333 uint64_t carry = 0, lx = 0, hx = 0;
334 for (unsigned j = 0; j < xlen; ++j) {
335 lx = x[j] & 0xffffffffULL;
336 hx = x[j] >> 32;
337 // hasCarry - A flag to indicate if has carry.
338 // hasCarry == 0, no carry
339 // hasCarry == 1, has carry
340 // hasCarry == 2, no carry and the calculation result == 0.
341 uint8_t hasCarry = 0;
342 uint64_t resul = carry + lx * ly;
343 hasCarry = (resul < carry) ? 1 : 0;
344 carry = (hasCarry ? (1ULL << 32) : 0) + hx * ly + (resul >> 32);
345 hasCarry = (!carry && hasCarry) ? 1 : (!carry ? 2 : 0);
346
347 carry += (lx * hy) & 0xffffffffULL;
348 resul = (carry << 32) | (resul & 0xffffffffULL);
349 dest[i+j] += resul;
350 carry = (((!carry && hasCarry != 2) || hasCarry == 1) ? (1ULL << 32) : 0)+
351 (carry >> 32) + (dest[i+j] < resul ? 1 : 0) +
352 ((lx * hy) >> 32) + hx * hy;
353 }
354 dest[i+xlen] = carry;
355 }
356 }
357
operator *=(const APInt & RHS)358 APInt& APInt::operator*=(const APInt& RHS) {
359 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
360 if (isSingleWord()) {
361 VAL *= RHS.VAL;
362 clearUnusedBits();
363 return *this;
364 }
365
366 // Get some bit facts about LHS and check for zero
367 unsigned lhsBits = getActiveBits();
368 unsigned lhsWords = !lhsBits ? 0 : whichWord(lhsBits - 1) + 1;
369 if (!lhsWords)
370 // 0 * X ===> 0
371 return *this;
372
373 // Get some bit facts about RHS and check for zero
374 unsigned rhsBits = RHS.getActiveBits();
375 unsigned rhsWords = !rhsBits ? 0 : whichWord(rhsBits - 1) + 1;
376 if (!rhsWords) {
377 // X * 0 ===> 0
378 clearAllBits();
379 return *this;
380 }
381
382 // Allocate space for the result
383 unsigned destWords = rhsWords + lhsWords;
384 uint64_t *dest = getMemory(destWords);
385
386 // Perform the long multiply
387 mul(dest, pVal, lhsWords, RHS.pVal, rhsWords);
388
389 // Copy result back into *this
390 clearAllBits();
391 unsigned wordsToCopy = destWords >= getNumWords() ? getNumWords() : destWords;
392 memcpy(pVal, dest, wordsToCopy * APINT_WORD_SIZE);
393 clearUnusedBits();
394
395 // delete dest array and return
396 delete[] dest;
397 return *this;
398 }
399
operator &=(const APInt & RHS)400 APInt& 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 unsigned numWords = getNumWords();
407 for (unsigned i = 0; i < numWords; ++i)
408 pVal[i] &= RHS.pVal[i];
409 return *this;
410 }
411
operator |=(const APInt & RHS)412 APInt& 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 unsigned numWords = getNumWords();
419 for (unsigned i = 0; i < numWords; ++i)
420 pVal[i] |= RHS.pVal[i];
421 return *this;
422 }
423
operator ^=(const APInt & RHS)424 APInt& 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 unsigned numWords = getNumWords();
432 for (unsigned i = 0; i < numWords; ++i)
433 pVal[i] ^= RHS.pVal[i];
434 return clearUnusedBits();
435 }
436
AndSlowCase(const APInt & RHS) const437 APInt APInt::AndSlowCase(const APInt& RHS) const {
438 unsigned numWords = getNumWords();
439 uint64_t* val = getMemory(numWords);
440 for (unsigned i = 0; i < numWords; ++i)
441 val[i] = pVal[i] & RHS.pVal[i];
442 return APInt(val, getBitWidth());
443 }
444
OrSlowCase(const APInt & RHS) const445 APInt APInt::OrSlowCase(const APInt& RHS) const {
446 unsigned numWords = getNumWords();
447 uint64_t *val = getMemory(numWords);
448 for (unsigned i = 0; i < numWords; ++i)
449 val[i] = pVal[i] | RHS.pVal[i];
450 return APInt(val, getBitWidth());
451 }
452
XorSlowCase(const APInt & RHS) const453 APInt APInt::XorSlowCase(const APInt& RHS) const {
454 unsigned numWords = getNumWords();
455 uint64_t *val = getMemory(numWords);
456 for (unsigned i = 0; i < numWords; ++i)
457 val[i] = pVal[i] ^ RHS.pVal[i];
458
459 APInt Result(val, getBitWidth());
460 // 0^0==1 so clear the high bits in case they got set.
461 Result.clearUnusedBits();
462 return Result;
463 }
464
operator *(const APInt & RHS) const465 APInt APInt::operator*(const APInt& RHS) const {
466 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
467 if (isSingleWord())
468 return APInt(BitWidth, VAL * RHS.VAL);
469 APInt Result(*this);
470 Result *= RHS;
471 return Result;
472 }
473
EqualSlowCase(const APInt & RHS) const474 bool APInt::EqualSlowCase(const APInt& RHS) const {
475 return std::equal(pVal, pVal + getNumWords(), RHS.pVal);
476 }
477
EqualSlowCase(uint64_t Val) const478 bool APInt::EqualSlowCase(uint64_t Val) const {
479 unsigned n = getActiveBits();
480 if (n <= APINT_BITS_PER_WORD)
481 return pVal[0] == Val;
482 else
483 return false;
484 }
485
ult(const APInt & RHS) const486 bool APInt::ult(const APInt& RHS) const {
487 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
488 if (isSingleWord())
489 return VAL < RHS.VAL;
490
491 // Get active bit length of both operands
492 unsigned n1 = getActiveBits();
493 unsigned n2 = RHS.getActiveBits();
494
495 // If magnitude of LHS is less than RHS, return true.
496 if (n1 < n2)
497 return true;
498
499 // If magnitude of RHS is greather than LHS, return false.
500 if (n2 < n1)
501 return false;
502
503 // If they bot fit in a word, just compare the low order word
504 if (n1 <= APINT_BITS_PER_WORD && n2 <= APINT_BITS_PER_WORD)
505 return pVal[0] < RHS.pVal[0];
506
507 // Otherwise, compare all words
508 unsigned topWord = whichWord(std::max(n1,n2)-1);
509 for (int i = topWord; i >= 0; --i) {
510 if (pVal[i] > RHS.pVal[i])
511 return false;
512 if (pVal[i] < RHS.pVal[i])
513 return true;
514 }
515 return false;
516 }
517
slt(const APInt & RHS) const518 bool APInt::slt(const APInt& RHS) const {
519 assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
520 if (isSingleWord()) {
521 int64_t lhsSext = SignExtend64(VAL, BitWidth);
522 int64_t rhsSext = SignExtend64(RHS.VAL, BitWidth);
523 return lhsSext < rhsSext;
524 }
525
526 bool lhsNeg = isNegative();
527 bool rhsNeg = RHS.isNegative();
528
529 // If the sign bits don't match, then (LHS < RHS) if LHS is negative
530 if (lhsNeg != rhsNeg)
531 return lhsNeg;
532
533 // Otherwise we can just use an unsigned comparision, because even negative
534 // numbers compare correctly this way if both have the same signed-ness.
535 return ult(RHS);
536 }
537
setBit(unsigned bitPosition)538 void APInt::setBit(unsigned bitPosition) {
539 if (isSingleWord())
540 VAL |= maskBit(bitPosition);
541 else
542 pVal[whichWord(bitPosition)] |= maskBit(bitPosition);
543 }
544
545 /// Set the given bit to 0 whose position is given as "bitPosition".
546 /// @brief Set a given bit to 0.
clearBit(unsigned bitPosition)547 void APInt::clearBit(unsigned bitPosition) {
548 if (isSingleWord())
549 VAL &= ~maskBit(bitPosition);
550 else
551 pVal[whichWord(bitPosition)] &= ~maskBit(bitPosition);
552 }
553
554 /// @brief Toggle every bit to its opposite value.
555
556 /// Toggle a given bit to its opposite value whose position is given
557 /// as "bitPosition".
558 /// @brief Toggles a given bit to its opposite value.
flipBit(unsigned bitPosition)559 void APInt::flipBit(unsigned bitPosition) {
560 assert(bitPosition < BitWidth && "Out of the bit-width range!");
561 if ((*this)[bitPosition]) clearBit(bitPosition);
562 else setBit(bitPosition);
563 }
564
getBitsNeeded(StringRef str,uint8_t radix)565 unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
566 assert(!str.empty() && "Invalid string length");
567 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
568 radix == 36) &&
569 "Radix should be 2, 8, 10, 16, or 36!");
570
571 size_t slen = str.size();
572
573 // Each computation below needs to know if it's negative.
574 StringRef::iterator p = str.begin();
575 unsigned isNegative = *p == '-';
576 if (*p == '-' || *p == '+') {
577 p++;
578 slen--;
579 assert(slen && "String is only a sign, needs a value.");
580 }
581
582 // For radixes of power-of-two values, the bits required is accurately and
583 // easily computed
584 if (radix == 2)
585 return slen + isNegative;
586 if (radix == 8)
587 return slen * 3 + isNegative;
588 if (radix == 16)
589 return slen * 4 + isNegative;
590
591 // FIXME: base 36
592
593 // This is grossly inefficient but accurate. We could probably do something
594 // with a computation of roughly slen*64/20 and then adjust by the value of
595 // the first few digits. But, I'm not sure how accurate that could be.
596
597 // Compute a sufficient number of bits that is always large enough but might
598 // be too large. This avoids the assertion in the constructor. This
599 // calculation doesn't work appropriately for the numbers 0-9, so just use 4
600 // bits in that case.
601 unsigned sufficient
602 = radix == 10? (slen == 1 ? 4 : slen * 64/18)
603 : (slen == 1 ? 7 : slen * 16/3);
604
605 // Convert to the actual binary value.
606 APInt tmp(sufficient, StringRef(p, slen), radix);
607
608 // Compute how many bits are required. If the log is infinite, assume we need
609 // just bit.
610 unsigned log = tmp.logBase2();
611 if (log == (unsigned)-1) {
612 return isNegative + 1;
613 } else {
614 return isNegative + log + 1;
615 }
616 }
617
hash_value(const APInt & Arg)618 hash_code llvm::hash_value(const APInt &Arg) {
619 if (Arg.isSingleWord())
620 return hash_combine(Arg.VAL);
621
622 return hash_combine_range(Arg.pVal, Arg.pVal + Arg.getNumWords());
623 }
624
isSplat(unsigned SplatSizeInBits) const625 bool APInt::isSplat(unsigned SplatSizeInBits) const {
626 assert(getBitWidth() % SplatSizeInBits == 0 &&
627 "SplatSizeInBits must divide width!");
628 // We can check that all parts of an integer are equal by making use of a
629 // little trick: rotate and check if it's still the same value.
630 return *this == rotl(SplatSizeInBits);
631 }
632
633 /// This function returns the high "numBits" bits of this APInt.
getHiBits(unsigned numBits) const634 APInt APInt::getHiBits(unsigned numBits) const {
635 return APIntOps::lshr(*this, BitWidth - numBits);
636 }
637
638 /// This function returns the low "numBits" bits of this APInt.
getLoBits(unsigned numBits) const639 APInt APInt::getLoBits(unsigned numBits) const {
640 return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits),
641 BitWidth - numBits);
642 }
643
countLeadingZerosSlowCase() const644 unsigned APInt::countLeadingZerosSlowCase() const {
645 unsigned Count = 0;
646 for (int i = getNumWords()-1; i >= 0; --i) {
647 integerPart V = pVal[i];
648 if (V == 0)
649 Count += APINT_BITS_PER_WORD;
650 else {
651 Count += llvm::countLeadingZeros(V);
652 break;
653 }
654 }
655 // Adjust for unused bits in the most significant word (they are zero).
656 unsigned Mod = BitWidth % APINT_BITS_PER_WORD;
657 Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0;
658 return Count;
659 }
660
countLeadingOnes() const661 unsigned APInt::countLeadingOnes() const {
662 if (isSingleWord())
663 return llvm::countLeadingOnes(VAL << (APINT_BITS_PER_WORD - BitWidth));
664
665 unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
666 unsigned shift;
667 if (!highWordBits) {
668 highWordBits = APINT_BITS_PER_WORD;
669 shift = 0;
670 } else {
671 shift = APINT_BITS_PER_WORD - highWordBits;
672 }
673 int i = getNumWords() - 1;
674 unsigned Count = llvm::countLeadingOnes(pVal[i] << shift);
675 if (Count == highWordBits) {
676 for (i--; i >= 0; --i) {
677 if (pVal[i] == -1ULL)
678 Count += APINT_BITS_PER_WORD;
679 else {
680 Count += llvm::countLeadingOnes(pVal[i]);
681 break;
682 }
683 }
684 }
685 return Count;
686 }
687
countTrailingZeros() const688 unsigned APInt::countTrailingZeros() const {
689 if (isSingleWord())
690 return std::min(unsigned(llvm::countTrailingZeros(VAL)), BitWidth);
691 unsigned Count = 0;
692 unsigned i = 0;
693 for (; i < getNumWords() && pVal[i] == 0; ++i)
694 Count += APINT_BITS_PER_WORD;
695 if (i < getNumWords())
696 Count += llvm::countTrailingZeros(pVal[i]);
697 return std::min(Count, BitWidth);
698 }
699
countTrailingOnesSlowCase() const700 unsigned APInt::countTrailingOnesSlowCase() const {
701 unsigned Count = 0;
702 unsigned i = 0;
703 for (; i < getNumWords() && pVal[i] == -1ULL; ++i)
704 Count += APINT_BITS_PER_WORD;
705 if (i < getNumWords())
706 Count += llvm::countTrailingOnes(pVal[i]);
707 return std::min(Count, BitWidth);
708 }
709
countPopulationSlowCase() const710 unsigned APInt::countPopulationSlowCase() const {
711 unsigned Count = 0;
712 for (unsigned i = 0; i < getNumWords(); ++i)
713 Count += llvm::countPopulation(pVal[i]);
714 return Count;
715 }
716
717 /// Perform a logical right-shift from Src to Dst, which must be equal or
718 /// non-overlapping, of Words words, by Shift, which must be less than 64.
lshrNear(uint64_t * Dst,uint64_t * Src,unsigned Words,unsigned Shift)719 static void lshrNear(uint64_t *Dst, uint64_t *Src, unsigned Words,
720 unsigned Shift) {
721 uint64_t Carry = 0;
722 for (int I = Words - 1; I >= 0; --I) {
723 uint64_t Tmp = Src[I];
724 Dst[I] = (Tmp >> Shift) | Carry;
725 Carry = Tmp << (64 - Shift);
726 }
727 }
728
byteSwap() const729 APInt APInt::byteSwap() const {
730 assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
731 if (BitWidth == 16)
732 return APInt(BitWidth, ByteSwap_16(uint16_t(VAL)));
733 if (BitWidth == 32)
734 return APInt(BitWidth, ByteSwap_32(unsigned(VAL)));
735 if (BitWidth == 48) {
736 unsigned Tmp1 = unsigned(VAL >> 16);
737 Tmp1 = ByteSwap_32(Tmp1);
738 uint16_t Tmp2 = uint16_t(VAL);
739 Tmp2 = ByteSwap_16(Tmp2);
740 return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
741 }
742 if (BitWidth == 64)
743 return APInt(BitWidth, ByteSwap_64(VAL));
744
745 APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0);
746 for (unsigned I = 0, N = getNumWords(); I != N; ++I)
747 Result.pVal[I] = ByteSwap_64(pVal[N - I - 1]);
748 if (Result.BitWidth != BitWidth) {
749 lshrNear(Result.pVal, Result.pVal, getNumWords(),
750 Result.BitWidth - BitWidth);
751 Result.BitWidth = BitWidth;
752 }
753 return Result;
754 }
755
reverseBits() const756 APInt APInt::reverseBits() const {
757 switch (BitWidth) {
758 case 64:
759 return APInt(BitWidth, llvm::reverseBits<uint64_t>(VAL));
760 case 32:
761 return APInt(BitWidth, llvm::reverseBits<uint32_t>(VAL));
762 case 16:
763 return APInt(BitWidth, llvm::reverseBits<uint16_t>(VAL));
764 case 8:
765 return APInt(BitWidth, llvm::reverseBits<uint8_t>(VAL));
766 default:
767 break;
768 }
769
770 APInt Val(*this);
771 APInt Reversed(*this);
772 int S = BitWidth - 1;
773
774 const APInt One(BitWidth, 1);
775
776 for ((Val = Val.lshr(1)); Val != 0; (Val = Val.lshr(1))) {
777 Reversed <<= 1;
778 Reversed |= (Val & One);
779 --S;
780 }
781
782 Reversed <<= S;
783 return Reversed;
784 }
785
GreatestCommonDivisor(const APInt & API1,const APInt & API2)786 APInt llvm::APIntOps::GreatestCommonDivisor(const APInt& API1,
787 const APInt& API2) {
788 APInt A = API1, B = API2;
789 while (!!B) {
790 APInt T = B;
791 B = APIntOps::urem(A, B);
792 A = T;
793 }
794 return A;
795 }
796
RoundDoubleToAPInt(double Double,unsigned width)797 APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
798 union {
799 double D;
800 uint64_t I;
801 } T;
802 T.D = Double;
803
804 // Get the sign bit from the highest order bit
805 bool isNeg = T.I >> 63;
806
807 // Get the 11-bit exponent and adjust for the 1023 bit bias
808 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
809
810 // If the exponent is negative, the value is < 0 so just return 0.
811 if (exp < 0)
812 return APInt(width, 0u);
813
814 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
815 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
816
817 // If the exponent doesn't shift all bits out of the mantissa
818 if (exp < 52)
819 return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
820 APInt(width, mantissa >> (52 - exp));
821
822 // If the client didn't provide enough bits for us to shift the mantissa into
823 // then the result is undefined, just return 0
824 if (width <= exp - 52)
825 return APInt(width, 0);
826
827 // Otherwise, we have to shift the mantissa bits up to the right location
828 APInt Tmp(width, mantissa);
829 Tmp = Tmp.shl((unsigned)exp - 52);
830 return isNeg ? -Tmp : Tmp;
831 }
832
833 /// This function converts this APInt to a double.
834 /// The layout for double is as following (IEEE Standard 754):
835 /// --------------------------------------
836 /// | Sign Exponent Fraction Bias |
837 /// |-------------------------------------- |
838 /// | 1[63] 11[62-52] 52[51-00] 1023 |
839 /// --------------------------------------
roundToDouble(bool isSigned) const840 double APInt::roundToDouble(bool isSigned) const {
841
842 // Handle the simple case where the value is contained in one uint64_t.
843 // It is wrong to optimize getWord(0) to VAL; there might be more than one word.
844 if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
845 if (isSigned) {
846 int64_t sext = SignExtend64(getWord(0), BitWidth);
847 return double(sext);
848 } else
849 return double(getWord(0));
850 }
851
852 // Determine if the value is negative.
853 bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
854
855 // Construct the absolute value if we're negative.
856 APInt Tmp(isNeg ? -(*this) : (*this));
857
858 // Figure out how many bits we're using.
859 unsigned n = Tmp.getActiveBits();
860
861 // The exponent (without bias normalization) is just the number of bits
862 // we are using. Note that the sign bit is gone since we constructed the
863 // absolute value.
864 uint64_t exp = n;
865
866 // Return infinity for exponent overflow
867 if (exp > 1023) {
868 if (!isSigned || !isNeg)
869 return std::numeric_limits<double>::infinity();
870 else
871 return -std::numeric_limits<double>::infinity();
872 }
873 exp += 1023; // Increment for 1023 bias
874
875 // Number of bits in mantissa is 52. To obtain the mantissa value, we must
876 // extract the high 52 bits from the correct words in pVal.
877 uint64_t mantissa;
878 unsigned hiWord = whichWord(n-1);
879 if (hiWord == 0) {
880 mantissa = Tmp.pVal[0];
881 if (n > 52)
882 mantissa >>= n - 52; // shift down, we want the top 52 bits.
883 } else {
884 assert(hiWord > 0 && "huh?");
885 uint64_t hibits = Tmp.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
886 uint64_t lobits = Tmp.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
887 mantissa = hibits | lobits;
888 }
889
890 // The leading bit of mantissa is implicit, so get rid of it.
891 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
892 union {
893 double D;
894 uint64_t I;
895 } T;
896 T.I = sign | (exp << 52) | mantissa;
897 return T.D;
898 }
899
900 // Truncate to new width.
trunc(unsigned width) const901 APInt APInt::trunc(unsigned width) const {
902 assert(width < BitWidth && "Invalid APInt Truncate request");
903 assert(width && "Can't truncate to 0 bits");
904
905 if (width <= APINT_BITS_PER_WORD)
906 return APInt(width, getRawData()[0]);
907
908 APInt Result(getMemory(getNumWords(width)), width);
909
910 // Copy full words.
911 unsigned i;
912 for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
913 Result.pVal[i] = pVal[i];
914
915 // Truncate and copy any partial word.
916 unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
917 if (bits != 0)
918 Result.pVal[i] = pVal[i] << bits >> bits;
919
920 return Result;
921 }
922
923 // Sign extend to a new width.
sext(unsigned width) const924 APInt APInt::sext(unsigned width) const {
925 assert(width > BitWidth && "Invalid APInt SignExtend request");
926
927 if (width <= APINT_BITS_PER_WORD) {
928 uint64_t val = VAL << (APINT_BITS_PER_WORD - BitWidth);
929 val = (int64_t)val >> (width - BitWidth);
930 return APInt(width, val >> (APINT_BITS_PER_WORD - width));
931 }
932
933 APInt Result(getMemory(getNumWords(width)), width);
934
935 // Copy full words.
936 unsigned i;
937 uint64_t word = 0;
938 for (i = 0; i != BitWidth / APINT_BITS_PER_WORD; i++) {
939 word = getRawData()[i];
940 Result.pVal[i] = word;
941 }
942
943 // Read and sign-extend any partial word.
944 unsigned bits = (0 - BitWidth) % APINT_BITS_PER_WORD;
945 if (bits != 0)
946 word = (int64_t)getRawData()[i] << bits >> bits;
947 else
948 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
949
950 // Write remaining full words.
951 for (; i != width / APINT_BITS_PER_WORD; i++) {
952 Result.pVal[i] = word;
953 word = (int64_t)word >> (APINT_BITS_PER_WORD - 1);
954 }
955
956 // Write any partial word.
957 bits = (0 - width) % APINT_BITS_PER_WORD;
958 if (bits != 0)
959 Result.pVal[i] = word << bits >> bits;
960
961 return Result;
962 }
963
964 // Zero extend to a new width.
zext(unsigned width) const965 APInt APInt::zext(unsigned width) const {
966 assert(width > BitWidth && "Invalid APInt ZeroExtend request");
967
968 if (width <= APINT_BITS_PER_WORD)
969 return APInt(width, VAL);
970
971 APInt Result(getMemory(getNumWords(width)), width);
972
973 // Copy words.
974 unsigned i;
975 for (i = 0; i != getNumWords(); i++)
976 Result.pVal[i] = getRawData()[i];
977
978 // Zero remaining words.
979 memset(&Result.pVal[i], 0, (Result.getNumWords() - i) * APINT_WORD_SIZE);
980
981 return Result;
982 }
983
zextOrTrunc(unsigned width) const984 APInt APInt::zextOrTrunc(unsigned width) const {
985 if (BitWidth < width)
986 return zext(width);
987 if (BitWidth > width)
988 return trunc(width);
989 return *this;
990 }
991
sextOrTrunc(unsigned width) const992 APInt APInt::sextOrTrunc(unsigned width) const {
993 if (BitWidth < width)
994 return sext(width);
995 if (BitWidth > width)
996 return trunc(width);
997 return *this;
998 }
999
zextOrSelf(unsigned width) const1000 APInt APInt::zextOrSelf(unsigned width) const {
1001 if (BitWidth < width)
1002 return zext(width);
1003 return *this;
1004 }
1005
sextOrSelf(unsigned width) const1006 APInt APInt::sextOrSelf(unsigned width) const {
1007 if (BitWidth < width)
1008 return sext(width);
1009 return *this;
1010 }
1011
1012 /// Arithmetic right-shift this APInt by shiftAmt.
1013 /// @brief Arithmetic right-shift function.
ashr(const APInt & shiftAmt) const1014 APInt APInt::ashr(const APInt &shiftAmt) const {
1015 return ashr((unsigned)shiftAmt.getLimitedValue(BitWidth));
1016 }
1017
1018 /// Arithmetic right-shift this APInt by shiftAmt.
1019 /// @brief Arithmetic right-shift function.
ashr(unsigned shiftAmt) const1020 APInt APInt::ashr(unsigned shiftAmt) const {
1021 assert(shiftAmt <= BitWidth && "Invalid shift amount");
1022 // Handle a degenerate case
1023 if (shiftAmt == 0)
1024 return *this;
1025
1026 // Handle single word shifts with built-in ashr
1027 if (isSingleWord()) {
1028 if (shiftAmt == BitWidth)
1029 return APInt(BitWidth, 0); // undefined
1030 return APInt(BitWidth, SignExtend64(VAL, BitWidth) >> shiftAmt);
1031 }
1032
1033 // If all the bits were shifted out, the result is, technically, undefined.
1034 // We return -1 if it was negative, 0 otherwise. We check this early to avoid
1035 // issues in the algorithm below.
1036 if (shiftAmt == BitWidth) {
1037 if (isNegative())
1038 return APInt(BitWidth, -1ULL, true);
1039 else
1040 return APInt(BitWidth, 0);
1041 }
1042
1043 // Create some space for the result.
1044 uint64_t * val = new uint64_t[getNumWords()];
1045
1046 // Compute some values needed by the following shift algorithms
1047 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD; // bits to shift per word
1048 unsigned offset = shiftAmt / APINT_BITS_PER_WORD; // word offset for shift
1049 unsigned breakWord = getNumWords() - 1 - offset; // last word affected
1050 unsigned bitsInWord = whichBit(BitWidth); // how many bits in last word?
1051 if (bitsInWord == 0)
1052 bitsInWord = APINT_BITS_PER_WORD;
1053
1054 // If we are shifting whole words, just move whole words
1055 if (wordShift == 0) {
1056 // Move the words containing significant bits
1057 for (unsigned i = 0; i <= breakWord; ++i)
1058 val[i] = pVal[i+offset]; // move whole word
1059
1060 // Adjust the top significant word for sign bit fill, if negative
1061 if (isNegative())
1062 if (bitsInWord < APINT_BITS_PER_WORD)
1063 val[breakWord] |= ~0ULL << bitsInWord; // set high bits
1064 } else {
1065 // Shift the low order words
1066 for (unsigned i = 0; i < breakWord; ++i) {
1067 // This combines the shifted corresponding word with the low bits from
1068 // the next word (shifted into this word's high bits).
1069 val[i] = (pVal[i+offset] >> wordShift) |
1070 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1071 }
1072
1073 // Shift the break word. In this case there are no bits from the next word
1074 // to include in this word.
1075 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1076
1077 // Deal with sign extension in the break word, and possibly the word before
1078 // it.
1079 if (isNegative()) {
1080 if (wordShift > bitsInWord) {
1081 if (breakWord > 0)
1082 val[breakWord-1] |=
1083 ~0ULL << (APINT_BITS_PER_WORD - (wordShift - bitsInWord));
1084 val[breakWord] |= ~0ULL;
1085 } else
1086 val[breakWord] |= (~0ULL << (bitsInWord - wordShift));
1087 }
1088 }
1089
1090 // Remaining words are 0 or -1, just assign them.
1091 uint64_t fillValue = (isNegative() ? -1ULL : 0);
1092 for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1093 val[i] = fillValue;
1094 APInt Result(val, BitWidth);
1095 Result.clearUnusedBits();
1096 return Result;
1097 }
1098
1099 /// Logical right-shift this APInt by shiftAmt.
1100 /// @brief Logical right-shift function.
lshr(const APInt & shiftAmt) const1101 APInt APInt::lshr(const APInt &shiftAmt) const {
1102 return lshr((unsigned)shiftAmt.getLimitedValue(BitWidth));
1103 }
1104
1105 /// Logical right-shift this APInt by shiftAmt.
1106 /// @brief Logical right-shift function.
lshr(unsigned shiftAmt) const1107 APInt APInt::lshr(unsigned shiftAmt) const {
1108 if (isSingleWord()) {
1109 if (shiftAmt >= BitWidth)
1110 return APInt(BitWidth, 0);
1111 else
1112 return APInt(BitWidth, this->VAL >> shiftAmt);
1113 }
1114
1115 // If all the bits were shifted out, the result is 0. This avoids issues
1116 // with shifting by the size of the integer type, which produces undefined
1117 // results. We define these "undefined results" to always be 0.
1118 if (shiftAmt >= BitWidth)
1119 return APInt(BitWidth, 0);
1120
1121 // If none of the bits are shifted out, the result is *this. This avoids
1122 // issues with shifting by the size of the integer type, which produces
1123 // undefined results in the code below. This is also an optimization.
1124 if (shiftAmt == 0)
1125 return *this;
1126
1127 // Create some space for the result.
1128 uint64_t * val = new uint64_t[getNumWords()];
1129
1130 // If we are shifting less than a word, compute the shift with a simple carry
1131 if (shiftAmt < APINT_BITS_PER_WORD) {
1132 lshrNear(val, pVal, getNumWords(), shiftAmt);
1133 APInt Result(val, BitWidth);
1134 Result.clearUnusedBits();
1135 return Result;
1136 }
1137
1138 // Compute some values needed by the remaining shift algorithms
1139 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1140 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1141
1142 // If we are shifting whole words, just move whole words
1143 if (wordShift == 0) {
1144 for (unsigned i = 0; i < getNumWords() - offset; ++i)
1145 val[i] = pVal[i+offset];
1146 for (unsigned i = getNumWords()-offset; i < getNumWords(); i++)
1147 val[i] = 0;
1148 APInt Result(val, BitWidth);
1149 Result.clearUnusedBits();
1150 return Result;
1151 }
1152
1153 // Shift the low order words
1154 unsigned breakWord = getNumWords() - offset -1;
1155 for (unsigned i = 0; i < breakWord; ++i)
1156 val[i] = (pVal[i+offset] >> wordShift) |
1157 (pVal[i+offset+1] << (APINT_BITS_PER_WORD - wordShift));
1158 // Shift the break word.
1159 val[breakWord] = pVal[breakWord+offset] >> wordShift;
1160
1161 // Remaining words are 0
1162 for (unsigned i = breakWord+1; i < getNumWords(); ++i)
1163 val[i] = 0;
1164 APInt Result(val, BitWidth);
1165 Result.clearUnusedBits();
1166 return Result;
1167 }
1168
1169 /// Left-shift this APInt by shiftAmt.
1170 /// @brief Left-shift function.
shl(const APInt & shiftAmt) const1171 APInt APInt::shl(const APInt &shiftAmt) const {
1172 // It's undefined behavior in C to shift by BitWidth or greater.
1173 return shl((unsigned)shiftAmt.getLimitedValue(BitWidth));
1174 }
1175
shlSlowCase(unsigned shiftAmt) const1176 APInt APInt::shlSlowCase(unsigned shiftAmt) const {
1177 // If all the bits were shifted out, the result is 0. This avoids issues
1178 // with shifting by the size of the integer type, which produces undefined
1179 // results. We define these "undefined results" to always be 0.
1180 if (shiftAmt == BitWidth)
1181 return APInt(BitWidth, 0);
1182
1183 // If none of the bits are shifted out, the result is *this. This avoids a
1184 // lshr by the words size in the loop below which can produce incorrect
1185 // results. It also avoids the expensive computation below for a common case.
1186 if (shiftAmt == 0)
1187 return *this;
1188
1189 // Create some space for the result.
1190 uint64_t * val = new uint64_t[getNumWords()];
1191
1192 // If we are shifting less than a word, do it the easy way
1193 if (shiftAmt < APINT_BITS_PER_WORD) {
1194 uint64_t carry = 0;
1195 for (unsigned i = 0; i < getNumWords(); i++) {
1196 val[i] = pVal[i] << shiftAmt | carry;
1197 carry = pVal[i] >> (APINT_BITS_PER_WORD - shiftAmt);
1198 }
1199 APInt Result(val, BitWidth);
1200 Result.clearUnusedBits();
1201 return Result;
1202 }
1203
1204 // Compute some values needed by the remaining shift algorithms
1205 unsigned wordShift = shiftAmt % APINT_BITS_PER_WORD;
1206 unsigned offset = shiftAmt / APINT_BITS_PER_WORD;
1207
1208 // If we are shifting whole words, just move whole words
1209 if (wordShift == 0) {
1210 for (unsigned i = 0; i < offset; i++)
1211 val[i] = 0;
1212 for (unsigned i = offset; i < getNumWords(); i++)
1213 val[i] = pVal[i-offset];
1214 APInt Result(val, BitWidth);
1215 Result.clearUnusedBits();
1216 return Result;
1217 }
1218
1219 // Copy whole words from this to Result.
1220 unsigned i = getNumWords() - 1;
1221 for (; i > offset; --i)
1222 val[i] = pVal[i-offset] << wordShift |
1223 pVal[i-offset-1] >> (APINT_BITS_PER_WORD - wordShift);
1224 val[offset] = pVal[0] << wordShift;
1225 for (i = 0; i < offset; ++i)
1226 val[i] = 0;
1227 APInt Result(val, BitWidth);
1228 Result.clearUnusedBits();
1229 return Result;
1230 }
1231
rotl(const APInt & rotateAmt) const1232 APInt APInt::rotl(const APInt &rotateAmt) const {
1233 return rotl((unsigned)rotateAmt.getLimitedValue(BitWidth));
1234 }
1235
rotl(unsigned rotateAmt) const1236 APInt APInt::rotl(unsigned rotateAmt) const {
1237 rotateAmt %= BitWidth;
1238 if (rotateAmt == 0)
1239 return *this;
1240 return shl(rotateAmt) | lshr(BitWidth - rotateAmt);
1241 }
1242
rotr(const APInt & rotateAmt) const1243 APInt APInt::rotr(const APInt &rotateAmt) const {
1244 return rotr((unsigned)rotateAmt.getLimitedValue(BitWidth));
1245 }
1246
rotr(unsigned rotateAmt) const1247 APInt APInt::rotr(unsigned rotateAmt) const {
1248 rotateAmt %= BitWidth;
1249 if (rotateAmt == 0)
1250 return *this;
1251 return lshr(rotateAmt) | shl(BitWidth - rotateAmt);
1252 }
1253
1254 // Square Root - this method computes and returns the square root of "this".
1255 // Three mechanisms are used for computation. For small values (<= 5 bits),
1256 // a table lookup is done. This gets some performance for common cases. For
1257 // values using less than 52 bits, the value is converted to double and then
1258 // the libc sqrt function is called. The result is rounded and then converted
1259 // back to a uint64_t which is then used to construct the result. Finally,
1260 // the Babylonian method for computing square roots is used.
sqrt() const1261 APInt APInt::sqrt() const {
1262
1263 // Determine the magnitude of the value.
1264 unsigned magnitude = getActiveBits();
1265
1266 // Use a fast table for some small values. This also gets rid of some
1267 // rounding errors in libc sqrt for small values.
1268 if (magnitude <= 5) {
1269 static const uint8_t results[32] = {
1270 /* 0 */ 0,
1271 /* 1- 2 */ 1, 1,
1272 /* 3- 6 */ 2, 2, 2, 2,
1273 /* 7-12 */ 3, 3, 3, 3, 3, 3,
1274 /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1275 /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1276 /* 31 */ 6
1277 };
1278 return APInt(BitWidth, results[ (isSingleWord() ? VAL : pVal[0]) ]);
1279 }
1280
1281 // If the magnitude of the value fits in less than 52 bits (the precision of
1282 // an IEEE double precision floating point value), then we can use the
1283 // libc sqrt function which will probably use a hardware sqrt computation.
1284 // This should be faster than the algorithm below.
1285 if (magnitude < 52) {
1286 return APInt(BitWidth,
1287 uint64_t(::round(::sqrt(double(isSingleWord()?VAL:pVal[0])))));
1288 }
1289
1290 // Okay, all the short cuts are exhausted. We must compute it. The following
1291 // is a classical Babylonian method for computing the square root. This code
1292 // was adapted to APInt from a wikipedia article on such computations.
1293 // See http://www.wikipedia.org/ and go to the page named
1294 // Calculate_an_integer_square_root.
1295 unsigned nbits = BitWidth, i = 4;
1296 APInt testy(BitWidth, 16);
1297 APInt x_old(BitWidth, 1);
1298 APInt x_new(BitWidth, 0);
1299 APInt two(BitWidth, 2);
1300
1301 // Select a good starting value using binary logarithms.
1302 for (;; i += 2, testy = testy.shl(2))
1303 if (i >= nbits || this->ule(testy)) {
1304 x_old = x_old.shl(i / 2);
1305 break;
1306 }
1307
1308 // Use the Babylonian method to arrive at the integer square root:
1309 for (;;) {
1310 x_new = (this->udiv(x_old) + x_old).udiv(two);
1311 if (x_old.ule(x_new))
1312 break;
1313 x_old = x_new;
1314 }
1315
1316 // Make sure we return the closest approximation
1317 // NOTE: The rounding calculation below is correct. It will produce an
1318 // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1319 // determined to be a rounding issue with pari/gp as it begins to use a
1320 // floating point representation after 192 bits. There are no discrepancies
1321 // between this algorithm and pari/gp for bit widths < 192 bits.
1322 APInt square(x_old * x_old);
1323 APInt nextSquare((x_old + 1) * (x_old +1));
1324 if (this->ult(square))
1325 return x_old;
1326 assert(this->ule(nextSquare) && "Error in APInt::sqrt computation");
1327 APInt midpoint((nextSquare - square).udiv(two));
1328 APInt offset(*this - square);
1329 if (offset.ult(midpoint))
1330 return x_old;
1331 return x_old + 1;
1332 }
1333
1334 /// Computes the multiplicative inverse of this APInt for a given modulo. The
1335 /// iterative extended Euclidean algorithm is used to solve for this value,
1336 /// however we simplify it to speed up calculating only the inverse, and take
1337 /// advantage of div+rem calculations. We also use some tricks to avoid copying
1338 /// (potentially large) APInts around.
multiplicativeInverse(const APInt & modulo) const1339 APInt APInt::multiplicativeInverse(const APInt& modulo) const {
1340 assert(ult(modulo) && "This APInt must be smaller than the modulo");
1341
1342 // Using the properties listed at the following web page (accessed 06/21/08):
1343 // http://www.numbertheory.org/php/euclid.html
1344 // (especially the properties numbered 3, 4 and 9) it can be proved that
1345 // BitWidth bits suffice for all the computations in the algorithm implemented
1346 // below. More precisely, this number of bits suffice if the multiplicative
1347 // inverse exists, but may not suffice for the general extended Euclidean
1348 // algorithm.
1349
1350 APInt r[2] = { modulo, *this };
1351 APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
1352 APInt q(BitWidth, 0);
1353
1354 unsigned i;
1355 for (i = 0; r[i^1] != 0; i ^= 1) {
1356 // An overview of the math without the confusing bit-flipping:
1357 // q = r[i-2] / r[i-1]
1358 // r[i] = r[i-2] % r[i-1]
1359 // t[i] = t[i-2] - t[i-1] * q
1360 udivrem(r[i], r[i^1], q, r[i]);
1361 t[i] -= t[i^1] * q;
1362 }
1363
1364 // If this APInt and the modulo are not coprime, there is no multiplicative
1365 // inverse, so return 0. We check this by looking at the next-to-last
1366 // remainder, which is the gcd(*this,modulo) as calculated by the Euclidean
1367 // algorithm.
1368 if (r[i] != 1)
1369 return APInt(BitWidth, 0);
1370
1371 // The next-to-last t is the multiplicative inverse. However, we are
1372 // interested in a positive inverse. Calcuate a positive one from a negative
1373 // one if necessary. A simple addition of the modulo suffices because
1374 // abs(t[i]) is known to be less than *this/2 (see the link above).
1375 return t[i].isNegative() ? t[i] + modulo : t[i];
1376 }
1377
1378 /// Calculate the magic numbers required to implement a signed integer division
1379 /// by a constant as a sequence of multiplies, adds and shifts. Requires that
1380 /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
1381 /// Warren, Jr., chapter 10.
magic() const1382 APInt::ms APInt::magic() const {
1383 const APInt& d = *this;
1384 unsigned p;
1385 APInt ad, anc, delta, q1, r1, q2, r2, t;
1386 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1387 struct ms mag;
1388
1389 ad = d.abs();
1390 t = signedMin + (d.lshr(d.getBitWidth() - 1));
1391 anc = t - 1 - t.urem(ad); // absolute value of nc
1392 p = d.getBitWidth() - 1; // initialize p
1393 q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc)
1394 r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc))
1395 q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d)
1396 r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d))
1397 do {
1398 p = p + 1;
1399 q1 = q1<<1; // update q1 = 2p/abs(nc)
1400 r1 = r1<<1; // update r1 = rem(2p/abs(nc))
1401 if (r1.uge(anc)) { // must be unsigned comparison
1402 q1 = q1 + 1;
1403 r1 = r1 - anc;
1404 }
1405 q2 = q2<<1; // update q2 = 2p/abs(d)
1406 r2 = r2<<1; // update r2 = rem(2p/abs(d))
1407 if (r2.uge(ad)) { // must be unsigned comparison
1408 q2 = q2 + 1;
1409 r2 = r2 - ad;
1410 }
1411 delta = ad - r2;
1412 } while (q1.ult(delta) || (q1 == delta && r1 == 0));
1413
1414 mag.m = q2 + 1;
1415 if (d.isNegative()) mag.m = -mag.m; // resulting magic number
1416 mag.s = p - d.getBitWidth(); // resulting shift
1417 return mag;
1418 }
1419
1420 /// Calculate the magic numbers required to implement an unsigned integer
1421 /// division by a constant as a sequence of multiplies, adds and shifts.
1422 /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
1423 /// S. Warren, Jr., chapter 10.
1424 /// LeadingZeros can be used to simplify the calculation if the upper bits
1425 /// of the divided value are known zero.
magicu(unsigned LeadingZeros) const1426 APInt::mu APInt::magicu(unsigned LeadingZeros) const {
1427 const APInt& d = *this;
1428 unsigned p;
1429 APInt nc, delta, q1, r1, q2, r2;
1430 struct mu magu;
1431 magu.a = 0; // initialize "add" indicator
1432 APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
1433 APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1434 APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
1435
1436 nc = allOnes - (allOnes - d).urem(d);
1437 p = d.getBitWidth() - 1; // initialize p
1438 q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc
1439 r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc)
1440 q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d
1441 r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d)
1442 do {
1443 p = p + 1;
1444 if (r1.uge(nc - r1)) {
1445 q1 = q1 + q1 + 1; // update q1
1446 r1 = r1 + r1 - nc; // update r1
1447 }
1448 else {
1449 q1 = q1+q1; // update q1
1450 r1 = r1+r1; // update r1
1451 }
1452 if ((r2 + 1).uge(d - r2)) {
1453 if (q2.uge(signedMax)) magu.a = 1;
1454 q2 = q2+q2 + 1; // update q2
1455 r2 = r2+r2 + 1 - d; // update r2
1456 }
1457 else {
1458 if (q2.uge(signedMin)) magu.a = 1;
1459 q2 = q2+q2; // update q2
1460 r2 = r2+r2 + 1; // update r2
1461 }
1462 delta = d - 1 - r2;
1463 } while (p < d.getBitWidth()*2 &&
1464 (q1.ult(delta) || (q1 == delta && r1 == 0)));
1465 magu.m = q2 + 1; // resulting magic number
1466 magu.s = p - d.getBitWidth(); // resulting shift
1467 return magu;
1468 }
1469
1470 /// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1471 /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1472 /// variables here have the same names as in the algorithm. Comments explain
1473 /// the algorithm and any deviation from it.
KnuthDiv(unsigned * u,unsigned * v,unsigned * q,unsigned * r,unsigned m,unsigned n)1474 static void KnuthDiv(unsigned *u, unsigned *v, unsigned *q, unsigned* r,
1475 unsigned m, unsigned n) {
1476 assert(u && "Must provide dividend");
1477 assert(v && "Must provide divisor");
1478 assert(q && "Must provide quotient");
1479 assert(u != v && u != q && v != q && "Must use different memory");
1480 assert(n>1 && "n must be > 1");
1481
1482 // b denotes the base of the number system. In our case b is 2^32.
1483 const uint64_t b = uint64_t(1) << 32;
1484
1485 DEBUG(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n');
1486 DEBUG(dbgs() << "KnuthDiv: original:");
1487 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
1488 DEBUG(dbgs() << " by");
1489 DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
1490 DEBUG(dbgs() << '\n');
1491 // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1492 // u and v by d. Note that we have taken Knuth's advice here to use a power
1493 // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1494 // 2 allows us to shift instead of multiply and it is easy to determine the
1495 // shift amount from the leading zeros. We are basically normalizing the u
1496 // and v so that its high bits are shifted to the top of v's range without
1497 // overflow. Note that this can require an extra word in u so that u must
1498 // be of length m+n+1.
1499 unsigned shift = countLeadingZeros(v[n-1]);
1500 unsigned v_carry = 0;
1501 unsigned u_carry = 0;
1502 if (shift) {
1503 for (unsigned i = 0; i < m+n; ++i) {
1504 unsigned u_tmp = u[i] >> (32 - shift);
1505 u[i] = (u[i] << shift) | u_carry;
1506 u_carry = u_tmp;
1507 }
1508 for (unsigned i = 0; i < n; ++i) {
1509 unsigned v_tmp = v[i] >> (32 - shift);
1510 v[i] = (v[i] << shift) | v_carry;
1511 v_carry = v_tmp;
1512 }
1513 }
1514 u[m+n] = u_carry;
1515
1516 DEBUG(dbgs() << "KnuthDiv: normal:");
1517 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
1518 DEBUG(dbgs() << " by");
1519 DEBUG(for (int i = n; i >0; i--) dbgs() << " " << v[i-1]);
1520 DEBUG(dbgs() << '\n');
1521
1522 // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1523 int j = m;
1524 do {
1525 DEBUG(dbgs() << "KnuthDiv: quotient digit #" << j << '\n');
1526 // D3. [Calculate q'.].
1527 // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1528 // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1529 // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
1530 // qp by 1, inrease rp by v[n-1], and repeat this test if rp < b. The test
1531 // on v[n-2] determines at high speed most of the cases in which the trial
1532 // value qp is one too large, and it eliminates all cases where qp is two
1533 // too large.
1534 uint64_t dividend = ((uint64_t(u[j+n]) << 32) + u[j+n-1]);
1535 DEBUG(dbgs() << "KnuthDiv: dividend == " << dividend << '\n');
1536 uint64_t qp = dividend / v[n-1];
1537 uint64_t rp = dividend % v[n-1];
1538 if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1539 qp--;
1540 rp += v[n-1];
1541 if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1542 qp--;
1543 }
1544 DEBUG(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
1545
1546 // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1547 // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1548 // consists of a simple multiplication by a one-place number, combined with
1549 // a subtraction.
1550 // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1551 // this step is actually negative, (u[j+n]...u[j]) should be left as the
1552 // true value plus b**(n+1), namely as the b's complement of
1553 // the true value, and a "borrow" to the left should be remembered.
1554 int64_t borrow = 0;
1555 for (unsigned i = 0; i < n; ++i) {
1556 uint64_t p = uint64_t(qp) * uint64_t(v[i]);
1557 int64_t subres = int64_t(u[j+i]) - borrow - (unsigned)p;
1558 u[j+i] = (unsigned)subres;
1559 borrow = (p >> 32) - (subres >> 32);
1560 DEBUG(dbgs() << "KnuthDiv: u[j+i] = " << u[j+i]
1561 << ", borrow = " << borrow << '\n');
1562 }
1563 bool isNeg = u[j+n] < borrow;
1564 u[j+n] -= (unsigned)borrow;
1565
1566 DEBUG(dbgs() << "KnuthDiv: after subtraction:");
1567 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
1568 DEBUG(dbgs() << '\n');
1569
1570 // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1571 // negative, go to step D6; otherwise go on to step D7.
1572 q[j] = (unsigned)qp;
1573 if (isNeg) {
1574 // D6. [Add back]. The probability that this step is necessary is very
1575 // small, on the order of only 2/b. Make sure that test data accounts for
1576 // this possibility. Decrease q[j] by 1
1577 q[j]--;
1578 // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1579 // A carry will occur to the left of u[j+n], and it should be ignored
1580 // since it cancels with the borrow that occurred in D4.
1581 bool carry = false;
1582 for (unsigned i = 0; i < n; i++) {
1583 unsigned limit = std::min(u[j+i],v[i]);
1584 u[j+i] += v[i] + carry;
1585 carry = u[j+i] < limit || (carry && u[j+i] == limit);
1586 }
1587 u[j+n] += carry;
1588 }
1589 DEBUG(dbgs() << "KnuthDiv: after correction:");
1590 DEBUG(for (int i = m+n; i >=0; i--) dbgs() << " " << u[i]);
1591 DEBUG(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n');
1592
1593 // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1594 } while (--j >= 0);
1595
1596 DEBUG(dbgs() << "KnuthDiv: quotient:");
1597 DEBUG(for (int i = m; i >=0; i--) dbgs() <<" " << q[i]);
1598 DEBUG(dbgs() << '\n');
1599
1600 // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1601 // remainder may be obtained by dividing u[...] by d. If r is non-null we
1602 // compute the remainder (urem uses this).
1603 if (r) {
1604 // The value d is expressed by the "shift" value above since we avoided
1605 // multiplication by d by using a shift left. So, all we have to do is
1606 // shift right here. In order to mak
1607 if (shift) {
1608 unsigned carry = 0;
1609 DEBUG(dbgs() << "KnuthDiv: remainder:");
1610 for (int i = n-1; i >= 0; i--) {
1611 r[i] = (u[i] >> shift) | carry;
1612 carry = u[i] << (32 - shift);
1613 DEBUG(dbgs() << " " << r[i]);
1614 }
1615 } else {
1616 for (int i = n-1; i >= 0; i--) {
1617 r[i] = u[i];
1618 DEBUG(dbgs() << " " << r[i]);
1619 }
1620 }
1621 DEBUG(dbgs() << '\n');
1622 }
1623 DEBUG(dbgs() << '\n');
1624 }
1625
divide(const APInt & LHS,unsigned lhsWords,const APInt & RHS,unsigned rhsWords,APInt * Quotient,APInt * Remainder)1626 void APInt::divide(const APInt &LHS, unsigned lhsWords, const APInt &RHS,
1627 unsigned rhsWords, APInt *Quotient, APInt *Remainder) {
1628 assert(lhsWords >= rhsWords && "Fractional result");
1629
1630 // First, compose the values into an array of 32-bit words instead of
1631 // 64-bit words. This is a necessity of both the "short division" algorithm
1632 // and the Knuth "classical algorithm" which requires there to be native
1633 // operations for +, -, and * on an m bit value with an m*2 bit result. We
1634 // can't use 64-bit operands here because we don't have native results of
1635 // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
1636 // work on large-endian machines.
1637 uint64_t mask = ~0ull >> (sizeof(unsigned)*CHAR_BIT);
1638 unsigned n = rhsWords * 2;
1639 unsigned m = (lhsWords * 2) - n;
1640
1641 // Allocate space for the temporary values we need either on the stack, if
1642 // it will fit, or on the heap if it won't.
1643 unsigned SPACE[128];
1644 unsigned *U = nullptr;
1645 unsigned *V = nullptr;
1646 unsigned *Q = nullptr;
1647 unsigned *R = nullptr;
1648 if ((Remainder?4:3)*n+2*m+1 <= 128) {
1649 U = &SPACE[0];
1650 V = &SPACE[m+n+1];
1651 Q = &SPACE[(m+n+1) + n];
1652 if (Remainder)
1653 R = &SPACE[(m+n+1) + n + (m+n)];
1654 } else {
1655 U = new unsigned[m + n + 1];
1656 V = new unsigned[n];
1657 Q = new unsigned[m+n];
1658 if (Remainder)
1659 R = new unsigned[n];
1660 }
1661
1662 // Initialize the dividend
1663 memset(U, 0, (m+n+1)*sizeof(unsigned));
1664 for (unsigned i = 0; i < lhsWords; ++i) {
1665 uint64_t tmp = (LHS.getNumWords() == 1 ? LHS.VAL : LHS.pVal[i]);
1666 U[i * 2] = (unsigned)(tmp & mask);
1667 U[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
1668 }
1669 U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1670
1671 // Initialize the divisor
1672 memset(V, 0, (n)*sizeof(unsigned));
1673 for (unsigned i = 0; i < rhsWords; ++i) {
1674 uint64_t tmp = (RHS.getNumWords() == 1 ? RHS.VAL : RHS.pVal[i]);
1675 V[i * 2] = (unsigned)(tmp & mask);
1676 V[i * 2 + 1] = (unsigned)(tmp >> (sizeof(unsigned)*CHAR_BIT));
1677 }
1678
1679 // initialize the quotient and remainder
1680 memset(Q, 0, (m+n) * sizeof(unsigned));
1681 if (Remainder)
1682 memset(R, 0, n * sizeof(unsigned));
1683
1684 // Now, adjust m and n for the Knuth division. n is the number of words in
1685 // the divisor. m is the number of words by which the dividend exceeds the
1686 // divisor (i.e. m+n is the length of the dividend). These sizes must not
1687 // contain any zero words or the Knuth algorithm fails.
1688 for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1689 n--;
1690 m++;
1691 }
1692 for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1693 m--;
1694
1695 // If we're left with only a single word for the divisor, Knuth doesn't work
1696 // so we implement the short division algorithm here. This is much simpler
1697 // and faster because we are certain that we can divide a 64-bit quantity
1698 // by a 32-bit quantity at hardware speed and short division is simply a
1699 // series of such operations. This is just like doing short division but we
1700 // are using base 2^32 instead of base 10.
1701 assert(n != 0 && "Divide by zero?");
1702 if (n == 1) {
1703 unsigned divisor = V[0];
1704 unsigned remainder = 0;
1705 for (int i = m+n-1; i >= 0; i--) {
1706 uint64_t partial_dividend = uint64_t(remainder) << 32 | U[i];
1707 if (partial_dividend == 0) {
1708 Q[i] = 0;
1709 remainder = 0;
1710 } else if (partial_dividend < divisor) {
1711 Q[i] = 0;
1712 remainder = (unsigned)partial_dividend;
1713 } else if (partial_dividend == divisor) {
1714 Q[i] = 1;
1715 remainder = 0;
1716 } else {
1717 Q[i] = (unsigned)(partial_dividend / divisor);
1718 remainder = (unsigned)(partial_dividend - (Q[i] * divisor));
1719 }
1720 }
1721 if (R)
1722 R[0] = remainder;
1723 } else {
1724 // Now we're ready to invoke the Knuth classical divide algorithm. In this
1725 // case n > 1.
1726 KnuthDiv(U, V, Q, R, m, n);
1727 }
1728
1729 // If the caller wants the quotient
1730 if (Quotient) {
1731 // Set up the Quotient value's memory.
1732 if (Quotient->BitWidth != LHS.BitWidth) {
1733 if (Quotient->isSingleWord())
1734 Quotient->VAL = 0;
1735 else
1736 delete [] Quotient->pVal;
1737 Quotient->BitWidth = LHS.BitWidth;
1738 if (!Quotient->isSingleWord())
1739 Quotient->pVal = getClearedMemory(Quotient->getNumWords());
1740 } else
1741 Quotient->clearAllBits();
1742
1743 // The quotient is in Q. Reconstitute the quotient into Quotient's low
1744 // order words.
1745 // This case is currently dead as all users of divide() handle trivial cases
1746 // earlier.
1747 if (lhsWords == 1) {
1748 uint64_t tmp =
1749 uint64_t(Q[0]) | (uint64_t(Q[1]) << (APINT_BITS_PER_WORD / 2));
1750 if (Quotient->isSingleWord())
1751 Quotient->VAL = tmp;
1752 else
1753 Quotient->pVal[0] = tmp;
1754 } else {
1755 assert(!Quotient->isSingleWord() && "Quotient APInt not large enough");
1756 for (unsigned i = 0; i < lhsWords; ++i)
1757 Quotient->pVal[i] =
1758 uint64_t(Q[i*2]) | (uint64_t(Q[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1759 }
1760 }
1761
1762 // If the caller wants the remainder
1763 if (Remainder) {
1764 // Set up the Remainder value's memory.
1765 if (Remainder->BitWidth != RHS.BitWidth) {
1766 if (Remainder->isSingleWord())
1767 Remainder->VAL = 0;
1768 else
1769 delete [] Remainder->pVal;
1770 Remainder->BitWidth = RHS.BitWidth;
1771 if (!Remainder->isSingleWord())
1772 Remainder->pVal = getClearedMemory(Remainder->getNumWords());
1773 } else
1774 Remainder->clearAllBits();
1775
1776 // The remainder is in R. Reconstitute the remainder into Remainder's low
1777 // order words.
1778 if (rhsWords == 1) {
1779 uint64_t tmp =
1780 uint64_t(R[0]) | (uint64_t(R[1]) << (APINT_BITS_PER_WORD / 2));
1781 if (Remainder->isSingleWord())
1782 Remainder->VAL = tmp;
1783 else
1784 Remainder->pVal[0] = tmp;
1785 } else {
1786 assert(!Remainder->isSingleWord() && "Remainder APInt not large enough");
1787 for (unsigned i = 0; i < rhsWords; ++i)
1788 Remainder->pVal[i] =
1789 uint64_t(R[i*2]) | (uint64_t(R[i*2+1]) << (APINT_BITS_PER_WORD / 2));
1790 }
1791 }
1792
1793 // Clean up the memory we allocated.
1794 if (U != &SPACE[0]) {
1795 delete [] U;
1796 delete [] V;
1797 delete [] Q;
1798 delete [] R;
1799 }
1800 }
1801
udiv(const APInt & RHS) const1802 APInt APInt::udiv(const APInt& RHS) const {
1803 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1804
1805 // First, deal with the easy case
1806 if (isSingleWord()) {
1807 assert(RHS.VAL != 0 && "Divide by zero?");
1808 return APInt(BitWidth, VAL / RHS.VAL);
1809 }
1810
1811 // Get some facts about the LHS and RHS number of bits and words
1812 unsigned rhsBits = RHS.getActiveBits();
1813 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1814 assert(rhsWords && "Divided by zero???");
1815 unsigned lhsBits = this->getActiveBits();
1816 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1817
1818 // Deal with some degenerate cases
1819 if (!lhsWords)
1820 // 0 / X ===> 0
1821 return APInt(BitWidth, 0);
1822 else if (lhsWords < rhsWords || this->ult(RHS)) {
1823 // X / Y ===> 0, iff X < Y
1824 return APInt(BitWidth, 0);
1825 } else if (*this == RHS) {
1826 // X / X ===> 1
1827 return APInt(BitWidth, 1);
1828 } else if (lhsWords == 1 && rhsWords == 1) {
1829 // All high words are zero, just use native divide
1830 return APInt(BitWidth, this->pVal[0] / RHS.pVal[0]);
1831 }
1832
1833 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1834 APInt Quotient(1,0); // to hold result.
1835 divide(*this, lhsWords, RHS, rhsWords, &Quotient, nullptr);
1836 return Quotient;
1837 }
1838
sdiv(const APInt & RHS) const1839 APInt APInt::sdiv(const APInt &RHS) const {
1840 if (isNegative()) {
1841 if (RHS.isNegative())
1842 return (-(*this)).udiv(-RHS);
1843 return -((-(*this)).udiv(RHS));
1844 }
1845 if (RHS.isNegative())
1846 return -(this->udiv(-RHS));
1847 return this->udiv(RHS);
1848 }
1849
urem(const APInt & RHS) const1850 APInt APInt::urem(const APInt& RHS) const {
1851 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1852 if (isSingleWord()) {
1853 assert(RHS.VAL != 0 && "Remainder by zero?");
1854 return APInt(BitWidth, VAL % RHS.VAL);
1855 }
1856
1857 // Get some facts about the LHS
1858 unsigned lhsBits = getActiveBits();
1859 unsigned lhsWords = !lhsBits ? 0 : (whichWord(lhsBits - 1) + 1);
1860
1861 // Get some facts about the RHS
1862 unsigned rhsBits = RHS.getActiveBits();
1863 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1864 assert(rhsWords && "Performing remainder operation by zero ???");
1865
1866 // Check the degenerate cases
1867 if (lhsWords == 0) {
1868 // 0 % Y ===> 0
1869 return APInt(BitWidth, 0);
1870 } else if (lhsWords < rhsWords || this->ult(RHS)) {
1871 // X % Y ===> X, iff X < Y
1872 return *this;
1873 } else if (*this == RHS) {
1874 // X % X == 0;
1875 return APInt(BitWidth, 0);
1876 } else if (lhsWords == 1) {
1877 // All high words are zero, just use native remainder
1878 return APInt(BitWidth, pVal[0] % RHS.pVal[0]);
1879 }
1880
1881 // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1882 APInt Remainder(1,0);
1883 divide(*this, lhsWords, RHS, rhsWords, nullptr, &Remainder);
1884 return Remainder;
1885 }
1886
srem(const APInt & RHS) const1887 APInt APInt::srem(const APInt &RHS) const {
1888 if (isNegative()) {
1889 if (RHS.isNegative())
1890 return -((-(*this)).urem(-RHS));
1891 return -((-(*this)).urem(RHS));
1892 }
1893 if (RHS.isNegative())
1894 return this->urem(-RHS);
1895 return this->urem(RHS);
1896 }
1897
udivrem(const APInt & LHS,const APInt & RHS,APInt & Quotient,APInt & Remainder)1898 void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1899 APInt &Quotient, APInt &Remainder) {
1900 assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
1901
1902 // First, deal with the easy case
1903 if (LHS.isSingleWord()) {
1904 assert(RHS.VAL != 0 && "Divide by zero?");
1905 uint64_t QuotVal = LHS.VAL / RHS.VAL;
1906 uint64_t RemVal = LHS.VAL % RHS.VAL;
1907 Quotient = APInt(LHS.BitWidth, QuotVal);
1908 Remainder = APInt(LHS.BitWidth, RemVal);
1909 return;
1910 }
1911
1912 // Get some size facts about the dividend and divisor
1913 unsigned lhsBits = LHS.getActiveBits();
1914 unsigned lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
1915 unsigned rhsBits = RHS.getActiveBits();
1916 unsigned rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
1917
1918 // Check the degenerate cases
1919 if (lhsWords == 0) {
1920 Quotient = 0; // 0 / Y ===> 0
1921 Remainder = 0; // 0 % Y ===> 0
1922 return;
1923 }
1924
1925 if (lhsWords < rhsWords || LHS.ult(RHS)) {
1926 Remainder = LHS; // X % Y ===> X, iff X < Y
1927 Quotient = 0; // X / Y ===> 0, iff X < Y
1928 return;
1929 }
1930
1931 if (LHS == RHS) {
1932 Quotient = 1; // X / X ===> 1
1933 Remainder = 0; // X % X ===> 0;
1934 return;
1935 }
1936
1937 if (lhsWords == 1 && rhsWords == 1) {
1938 // There is only one word to consider so use the native versions.
1939 uint64_t lhsValue = LHS.isSingleWord() ? LHS.VAL : LHS.pVal[0];
1940 uint64_t rhsValue = RHS.isSingleWord() ? RHS.VAL : RHS.pVal[0];
1941 Quotient = APInt(LHS.getBitWidth(), lhsValue / rhsValue);
1942 Remainder = APInt(LHS.getBitWidth(), lhsValue % rhsValue);
1943 return;
1944 }
1945
1946 // Okay, lets do it the long way
1947 divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
1948 }
1949
sdivrem(const APInt & LHS,const APInt & RHS,APInt & Quotient,APInt & Remainder)1950 void APInt::sdivrem(const APInt &LHS, const APInt &RHS,
1951 APInt &Quotient, APInt &Remainder) {
1952 if (LHS.isNegative()) {
1953 if (RHS.isNegative())
1954 APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
1955 else {
1956 APInt::udivrem(-LHS, RHS, Quotient, Remainder);
1957 Quotient = -Quotient;
1958 }
1959 Remainder = -Remainder;
1960 } else if (RHS.isNegative()) {
1961 APInt::udivrem(LHS, -RHS, Quotient, Remainder);
1962 Quotient = -Quotient;
1963 } else {
1964 APInt::udivrem(LHS, RHS, Quotient, Remainder);
1965 }
1966 }
1967
sadd_ov(const APInt & RHS,bool & Overflow) const1968 APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const {
1969 APInt Res = *this+RHS;
1970 Overflow = isNonNegative() == RHS.isNonNegative() &&
1971 Res.isNonNegative() != isNonNegative();
1972 return Res;
1973 }
1974
uadd_ov(const APInt & RHS,bool & Overflow) const1975 APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const {
1976 APInt Res = *this+RHS;
1977 Overflow = Res.ult(RHS);
1978 return Res;
1979 }
1980
ssub_ov(const APInt & RHS,bool & Overflow) const1981 APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const {
1982 APInt Res = *this - RHS;
1983 Overflow = isNonNegative() != RHS.isNonNegative() &&
1984 Res.isNonNegative() != isNonNegative();
1985 return Res;
1986 }
1987
usub_ov(const APInt & RHS,bool & Overflow) const1988 APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const {
1989 APInt Res = *this-RHS;
1990 Overflow = Res.ugt(*this);
1991 return Res;
1992 }
1993
sdiv_ov(const APInt & RHS,bool & Overflow) const1994 APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const {
1995 // MININT/-1 --> overflow.
1996 Overflow = isMinSignedValue() && RHS.isAllOnesValue();
1997 return sdiv(RHS);
1998 }
1999
smul_ov(const APInt & RHS,bool & Overflow) const2000 APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const {
2001 APInt Res = *this * RHS;
2002
2003 if (*this != 0 && RHS != 0)
2004 Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS;
2005 else
2006 Overflow = false;
2007 return Res;
2008 }
2009
umul_ov(const APInt & RHS,bool & Overflow) const2010 APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
2011 APInt Res = *this * RHS;
2012
2013 if (*this != 0 && RHS != 0)
2014 Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS;
2015 else
2016 Overflow = false;
2017 return Res;
2018 }
2019
sshl_ov(const APInt & ShAmt,bool & Overflow) const2020 APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const {
2021 Overflow = ShAmt.uge(getBitWidth());
2022 if (Overflow)
2023 return APInt(BitWidth, 0);
2024
2025 if (isNonNegative()) // Don't allow sign change.
2026 Overflow = ShAmt.uge(countLeadingZeros());
2027 else
2028 Overflow = ShAmt.uge(countLeadingOnes());
2029
2030 return *this << ShAmt;
2031 }
2032
ushl_ov(const APInt & ShAmt,bool & Overflow) const2033 APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const {
2034 Overflow = ShAmt.uge(getBitWidth());
2035 if (Overflow)
2036 return APInt(BitWidth, 0);
2037
2038 Overflow = ShAmt.ugt(countLeadingZeros());
2039
2040 return *this << ShAmt;
2041 }
2042
2043
2044
2045
fromString(unsigned numbits,StringRef str,uint8_t radix)2046 void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
2047 // Check our assumptions here
2048 assert(!str.empty() && "Invalid string length");
2049 assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
2050 radix == 36) &&
2051 "Radix should be 2, 8, 10, 16, or 36!");
2052
2053 StringRef::iterator p = str.begin();
2054 size_t slen = str.size();
2055 bool isNeg = *p == '-';
2056 if (*p == '-' || *p == '+') {
2057 p++;
2058 slen--;
2059 assert(slen && "String is only a sign, needs a value.");
2060 }
2061 assert((slen <= numbits || radix != 2) && "Insufficient bit width");
2062 assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
2063 assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
2064 assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2065 "Insufficient bit width");
2066
2067 // Allocate memory
2068 if (!isSingleWord())
2069 pVal = getClearedMemory(getNumWords());
2070
2071 // Figure out if we can shift instead of multiply
2072 unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2073
2074 // Set up an APInt for the digit to add outside the loop so we don't
2075 // constantly construct/destruct it.
2076 APInt apdigit(getBitWidth(), 0);
2077 APInt apradix(getBitWidth(), radix);
2078
2079 // Enter digit traversal loop
2080 for (StringRef::iterator e = str.end(); p != e; ++p) {
2081 unsigned digit = getDigit(*p, radix);
2082 assert(digit < radix && "Invalid character in digit string");
2083
2084 // Shift or multiply the value by the radix
2085 if (slen > 1) {
2086 if (shift)
2087 *this <<= shift;
2088 else
2089 *this *= apradix;
2090 }
2091
2092 // Add in the digit we just interpreted
2093 if (apdigit.isSingleWord())
2094 apdigit.VAL = digit;
2095 else
2096 apdigit.pVal[0] = digit;
2097 *this += apdigit;
2098 }
2099 // If its negative, put it in two's complement form
2100 if (isNeg) {
2101 --(*this);
2102 this->flipAllBits();
2103 }
2104 }
2105
toString(SmallVectorImpl<char> & Str,unsigned Radix,bool Signed,bool formatAsCLiteral) const2106 void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
2107 bool Signed, bool formatAsCLiteral) const {
2108 assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
2109 Radix == 36) &&
2110 "Radix should be 2, 8, 10, 16, or 36!");
2111
2112 const char *Prefix = "";
2113 if (formatAsCLiteral) {
2114 switch (Radix) {
2115 case 2:
2116 // Binary literals are a non-standard extension added in gcc 4.3:
2117 // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html
2118 Prefix = "0b";
2119 break;
2120 case 8:
2121 Prefix = "0";
2122 break;
2123 case 10:
2124 break; // No prefix
2125 case 16:
2126 Prefix = "0x";
2127 break;
2128 default:
2129 llvm_unreachable("Invalid radix!");
2130 }
2131 }
2132
2133 // First, check for a zero value and just short circuit the logic below.
2134 if (*this == 0) {
2135 while (*Prefix) {
2136 Str.push_back(*Prefix);
2137 ++Prefix;
2138 };
2139 Str.push_back('0');
2140 return;
2141 }
2142
2143 static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2144
2145 if (isSingleWord()) {
2146 char Buffer[65];
2147 char *BufPtr = Buffer+65;
2148
2149 uint64_t N;
2150 if (!Signed) {
2151 N = getZExtValue();
2152 } else {
2153 int64_t I = getSExtValue();
2154 if (I >= 0) {
2155 N = I;
2156 } else {
2157 Str.push_back('-');
2158 N = -(uint64_t)I;
2159 }
2160 }
2161
2162 while (*Prefix) {
2163 Str.push_back(*Prefix);
2164 ++Prefix;
2165 };
2166
2167 while (N) {
2168 *--BufPtr = Digits[N % Radix];
2169 N /= Radix;
2170 }
2171 Str.append(BufPtr, Buffer+65);
2172 return;
2173 }
2174
2175 APInt Tmp(*this);
2176
2177 if (Signed && isNegative()) {
2178 // They want to print the signed version and it is a negative value
2179 // Flip the bits and add one to turn it into the equivalent positive
2180 // value and put a '-' in the result.
2181 Tmp.flipAllBits();
2182 ++Tmp;
2183 Str.push_back('-');
2184 }
2185
2186 while (*Prefix) {
2187 Str.push_back(*Prefix);
2188 ++Prefix;
2189 };
2190
2191 // We insert the digits backward, then reverse them to get the right order.
2192 unsigned StartDig = Str.size();
2193
2194 // For the 2, 8 and 16 bit cases, we can just shift instead of divide
2195 // because the number of bits per digit (1, 3 and 4 respectively) divides
2196 // equaly. We just shift until the value is zero.
2197 if (Radix == 2 || Radix == 8 || Radix == 16) {
2198 // Just shift tmp right for each digit width until it becomes zero
2199 unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2200 unsigned MaskAmt = Radix - 1;
2201
2202 while (Tmp != 0) {
2203 unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
2204 Str.push_back(Digits[Digit]);
2205 Tmp = Tmp.lshr(ShiftAmt);
2206 }
2207 } else {
2208 APInt divisor(Radix == 10? 4 : 8, Radix);
2209 while (Tmp != 0) {
2210 APInt APdigit(1, 0);
2211 APInt tmp2(Tmp.getBitWidth(), 0);
2212 divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
2213 &APdigit);
2214 unsigned Digit = (unsigned)APdigit.getZExtValue();
2215 assert(Digit < Radix && "divide failed");
2216 Str.push_back(Digits[Digit]);
2217 Tmp = tmp2;
2218 }
2219 }
2220
2221 // Reverse the digits before returning.
2222 std::reverse(Str.begin()+StartDig, Str.end());
2223 }
2224
2225 /// Returns the APInt as a std::string. Note that this is an inefficient method.
2226 /// It is better to pass in a SmallVector/SmallString to the methods above.
toString(unsigned Radix=10,bool Signed=true) const2227 std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const {
2228 SmallString<40> S;
2229 toString(S, Radix, Signed, /* formatAsCLiteral = */false);
2230 return S.str();
2231 }
2232
2233
dump() const2234 LLVM_DUMP_METHOD void APInt::dump() const {
2235 SmallString<40> S, U;
2236 this->toStringUnsigned(U);
2237 this->toStringSigned(S);
2238 dbgs() << "APInt(" << BitWidth << "b, "
2239 << U << "u " << S << "s)";
2240 }
2241
print(raw_ostream & OS,bool isSigned) const2242 void APInt::print(raw_ostream &OS, bool isSigned) const {
2243 SmallString<40> S;
2244 this->toString(S, 10, isSigned, /* formatAsCLiteral = */false);
2245 OS << S;
2246 }
2247
2248 // This implements a variety of operations on a representation of
2249 // arbitrary precision, two's-complement, bignum integer values.
2250
2251 // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2252 // and unrestricting assumption.
2253 static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!");
2254
2255 /* Some handy functions local to this file. */
2256 namespace {
2257
2258 /* Returns the integer part with the least significant BITS set.
2259 BITS cannot be zero. */
2260 static inline integerPart
lowBitMask(unsigned int bits)2261 lowBitMask(unsigned int bits)
2262 {
2263 assert(bits != 0 && bits <= integerPartWidth);
2264
2265 return ~(integerPart) 0 >> (integerPartWidth - bits);
2266 }
2267
2268 /* Returns the value of the lower half of PART. */
2269 static inline integerPart
lowHalf(integerPart part)2270 lowHalf(integerPart part)
2271 {
2272 return part & lowBitMask(integerPartWidth / 2);
2273 }
2274
2275 /* Returns the value of the upper half of PART. */
2276 static inline integerPart
highHalf(integerPart part)2277 highHalf(integerPart part)
2278 {
2279 return part >> (integerPartWidth / 2);
2280 }
2281
2282 /* Returns the bit number of the most significant set bit of a part.
2283 If the input number has no bits set -1U is returned. */
2284 static unsigned int
partMSB(integerPart value)2285 partMSB(integerPart value)
2286 {
2287 return findLastSet(value, ZB_Max);
2288 }
2289
2290 /* Returns the bit number of the least significant set bit of a
2291 part. If the input number has no bits set -1U is returned. */
2292 static unsigned int
partLSB(integerPart value)2293 partLSB(integerPart value)
2294 {
2295 return findFirstSet(value, ZB_Max);
2296 }
2297 }
2298
2299 /* Sets the least significant part of a bignum to the input value, and
2300 zeroes out higher parts. */
2301 void
tcSet(integerPart * dst,integerPart part,unsigned int parts)2302 APInt::tcSet(integerPart *dst, integerPart part, unsigned int parts)
2303 {
2304 unsigned int i;
2305
2306 assert(parts > 0);
2307
2308 dst[0] = part;
2309 for (i = 1; i < parts; i++)
2310 dst[i] = 0;
2311 }
2312
2313 /* Assign one bignum to another. */
2314 void
tcAssign(integerPart * dst,const integerPart * src,unsigned int parts)2315 APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned int parts)
2316 {
2317 unsigned int i;
2318
2319 for (i = 0; i < parts; i++)
2320 dst[i] = src[i];
2321 }
2322
2323 /* Returns true if a bignum is zero, false otherwise. */
2324 bool
tcIsZero(const integerPart * src,unsigned int parts)2325 APInt::tcIsZero(const integerPart *src, unsigned int parts)
2326 {
2327 unsigned int i;
2328
2329 for (i = 0; i < parts; i++)
2330 if (src[i])
2331 return false;
2332
2333 return true;
2334 }
2335
2336 /* Extract the given bit of a bignum; returns 0 or 1. */
2337 int
tcExtractBit(const integerPart * parts,unsigned int bit)2338 APInt::tcExtractBit(const integerPart *parts, unsigned int bit)
2339 {
2340 return (parts[bit / integerPartWidth] &
2341 ((integerPart) 1 << bit % integerPartWidth)) != 0;
2342 }
2343
2344 /* Set the given bit of a bignum. */
2345 void
tcSetBit(integerPart * parts,unsigned int bit)2346 APInt::tcSetBit(integerPart *parts, unsigned int bit)
2347 {
2348 parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
2349 }
2350
2351 /* Clears the given bit of a bignum. */
2352 void
tcClearBit(integerPart * parts,unsigned int bit)2353 APInt::tcClearBit(integerPart *parts, unsigned int bit)
2354 {
2355 parts[bit / integerPartWidth] &=
2356 ~((integerPart) 1 << (bit % integerPartWidth));
2357 }
2358
2359 /* Returns the bit number of the least significant set bit of a
2360 number. If the input number has no bits set -1U is returned. */
2361 unsigned int
tcLSB(const integerPart * parts,unsigned int n)2362 APInt::tcLSB(const integerPart *parts, unsigned int n)
2363 {
2364 unsigned int i, lsb;
2365
2366 for (i = 0; i < n; i++) {
2367 if (parts[i] != 0) {
2368 lsb = partLSB(parts[i]);
2369
2370 return lsb + i * integerPartWidth;
2371 }
2372 }
2373
2374 return -1U;
2375 }
2376
2377 /* Returns the bit number of the most significant set bit of a number.
2378 If the input number has no bits set -1U is returned. */
2379 unsigned int
tcMSB(const integerPart * parts,unsigned int n)2380 APInt::tcMSB(const integerPart *parts, unsigned int n)
2381 {
2382 unsigned int msb;
2383
2384 do {
2385 --n;
2386
2387 if (parts[n] != 0) {
2388 msb = partMSB(parts[n]);
2389
2390 return msb + n * integerPartWidth;
2391 }
2392 } while (n);
2393
2394 return -1U;
2395 }
2396
2397 /* Copy the bit vector of width srcBITS from SRC, starting at bit
2398 srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
2399 the least significant bit of DST. All high bits above srcBITS in
2400 DST are zero-filled. */
2401 void
tcExtract(integerPart * dst,unsigned int dstCount,const integerPart * src,unsigned int srcBits,unsigned int srcLSB)2402 APInt::tcExtract(integerPart *dst, unsigned int dstCount,const integerPart *src,
2403 unsigned int srcBits, unsigned int srcLSB)
2404 {
2405 unsigned int firstSrcPart, dstParts, shift, n;
2406
2407 dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
2408 assert(dstParts <= dstCount);
2409
2410 firstSrcPart = srcLSB / integerPartWidth;
2411 tcAssign (dst, src + firstSrcPart, dstParts);
2412
2413 shift = srcLSB % integerPartWidth;
2414 tcShiftRight (dst, dstParts, shift);
2415
2416 /* We now have (dstParts * integerPartWidth - shift) bits from SRC
2417 in DST. If this is less that srcBits, append the rest, else
2418 clear the high bits. */
2419 n = dstParts * integerPartWidth - shift;
2420 if (n < srcBits) {
2421 integerPart mask = lowBitMask (srcBits - n);
2422 dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
2423 << n % integerPartWidth);
2424 } else if (n > srcBits) {
2425 if (srcBits % integerPartWidth)
2426 dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
2427 }
2428
2429 /* Clear high parts. */
2430 while (dstParts < dstCount)
2431 dst[dstParts++] = 0;
2432 }
2433
2434 /* DST += RHS + C where C is zero or one. Returns the carry flag. */
2435 integerPart
tcAdd(integerPart * dst,const integerPart * rhs,integerPart c,unsigned int parts)2436 APInt::tcAdd(integerPart *dst, const integerPart *rhs,
2437 integerPart c, unsigned int parts)
2438 {
2439 unsigned int i;
2440
2441 assert(c <= 1);
2442
2443 for (i = 0; i < parts; i++) {
2444 integerPart l;
2445
2446 l = dst[i];
2447 if (c) {
2448 dst[i] += rhs[i] + 1;
2449 c = (dst[i] <= l);
2450 } else {
2451 dst[i] += rhs[i];
2452 c = (dst[i] < l);
2453 }
2454 }
2455
2456 return c;
2457 }
2458
2459 /* DST -= RHS + C where C is zero or one. Returns the carry flag. */
2460 integerPart
tcSubtract(integerPart * dst,const integerPart * rhs,integerPart c,unsigned int parts)2461 APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
2462 integerPart c, unsigned int parts)
2463 {
2464 unsigned int i;
2465
2466 assert(c <= 1);
2467
2468 for (i = 0; i < parts; i++) {
2469 integerPart l;
2470
2471 l = dst[i];
2472 if (c) {
2473 dst[i] -= rhs[i] + 1;
2474 c = (dst[i] >= l);
2475 } else {
2476 dst[i] -= rhs[i];
2477 c = (dst[i] > l);
2478 }
2479 }
2480
2481 return c;
2482 }
2483
2484 /* Negate a bignum in-place. */
2485 void
tcNegate(integerPart * dst,unsigned int parts)2486 APInt::tcNegate(integerPart *dst, unsigned int parts)
2487 {
2488 tcComplement(dst, parts);
2489 tcIncrement(dst, parts);
2490 }
2491
2492 /* DST += SRC * MULTIPLIER + CARRY if add is true
2493 DST = SRC * MULTIPLIER + CARRY if add is false
2494
2495 Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
2496 they must start at the same point, i.e. DST == SRC.
2497
2498 If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2499 returned. Otherwise DST is filled with the least significant
2500 DSTPARTS parts of the result, and if all of the omitted higher
2501 parts were zero return zero, otherwise overflow occurred and
2502 return one. */
2503 int
tcMultiplyPart(integerPart * dst,const integerPart * src,integerPart multiplier,integerPart carry,unsigned int srcParts,unsigned int dstParts,bool add)2504 APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
2505 integerPart multiplier, integerPart carry,
2506 unsigned int srcParts, unsigned int dstParts,
2507 bool add)
2508 {
2509 unsigned int i, n;
2510
2511 /* Otherwise our writes of DST kill our later reads of SRC. */
2512 assert(dst <= src || dst >= src + srcParts);
2513 assert(dstParts <= srcParts + 1);
2514
2515 /* N loops; minimum of dstParts and srcParts. */
2516 n = dstParts < srcParts ? dstParts: srcParts;
2517
2518 for (i = 0; i < n; i++) {
2519 integerPart low, mid, high, srcPart;
2520
2521 /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2522
2523 This cannot overflow, because
2524
2525 (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2526
2527 which is less than n^2. */
2528
2529 srcPart = src[i];
2530
2531 if (multiplier == 0 || srcPart == 0) {
2532 low = carry;
2533 high = 0;
2534 } else {
2535 low = lowHalf(srcPart) * lowHalf(multiplier);
2536 high = highHalf(srcPart) * highHalf(multiplier);
2537
2538 mid = lowHalf(srcPart) * highHalf(multiplier);
2539 high += highHalf(mid);
2540 mid <<= integerPartWidth / 2;
2541 if (low + mid < low)
2542 high++;
2543 low += mid;
2544
2545 mid = highHalf(srcPart) * lowHalf(multiplier);
2546 high += highHalf(mid);
2547 mid <<= integerPartWidth / 2;
2548 if (low + mid < low)
2549 high++;
2550 low += mid;
2551
2552 /* Now add carry. */
2553 if (low + carry < low)
2554 high++;
2555 low += carry;
2556 }
2557
2558 if (add) {
2559 /* And now DST[i], and store the new low part there. */
2560 if (low + dst[i] < low)
2561 high++;
2562 dst[i] += low;
2563 } else
2564 dst[i] = low;
2565
2566 carry = high;
2567 }
2568
2569 if (i < dstParts) {
2570 /* Full multiplication, there is no overflow. */
2571 assert(i + 1 == dstParts);
2572 dst[i] = carry;
2573 return 0;
2574 } else {
2575 /* We overflowed if there is carry. */
2576 if (carry)
2577 return 1;
2578
2579 /* We would overflow if any significant unwritten parts would be
2580 non-zero. This is true if any remaining src parts are non-zero
2581 and the multiplier is non-zero. */
2582 if (multiplier)
2583 for (; i < srcParts; i++)
2584 if (src[i])
2585 return 1;
2586
2587 /* We fitted in the narrow destination. */
2588 return 0;
2589 }
2590 }
2591
2592 /* DST = LHS * RHS, where DST has the same width as the operands and
2593 is filled with the least significant parts of the result. Returns
2594 one if overflow occurred, otherwise zero. DST must be disjoint
2595 from both operands. */
2596 int
tcMultiply(integerPart * dst,const integerPart * lhs,const integerPart * rhs,unsigned int parts)2597 APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
2598 const integerPart *rhs, unsigned int parts)
2599 {
2600 unsigned int i;
2601 int overflow;
2602
2603 assert(dst != lhs && dst != rhs);
2604
2605 overflow = 0;
2606 tcSet(dst, 0, parts);
2607
2608 for (i = 0; i < parts; i++)
2609 overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
2610 parts - i, true);
2611
2612 return overflow;
2613 }
2614
2615 /* DST = LHS * RHS, where DST has width the sum of the widths of the
2616 operands. No overflow occurs. DST must be disjoint from both
2617 operands. Returns the number of parts required to hold the
2618 result. */
2619 unsigned int
tcFullMultiply(integerPart * dst,const integerPart * lhs,const integerPart * rhs,unsigned int lhsParts,unsigned int rhsParts)2620 APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
2621 const integerPart *rhs, unsigned int lhsParts,
2622 unsigned int rhsParts)
2623 {
2624 /* Put the narrower number on the LHS for less loops below. */
2625 if (lhsParts > rhsParts) {
2626 return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
2627 } else {
2628 unsigned int n;
2629
2630 assert(dst != lhs && dst != rhs);
2631
2632 tcSet(dst, 0, rhsParts);
2633
2634 for (n = 0; n < lhsParts; n++)
2635 tcMultiplyPart(&dst[n], rhs, lhs[n], 0, rhsParts, rhsParts + 1, true);
2636
2637 n = lhsParts + rhsParts;
2638
2639 return n - (dst[n - 1] == 0);
2640 }
2641 }
2642
2643 /* If RHS is zero LHS and REMAINDER are left unchanged, return one.
2644 Otherwise set LHS to LHS / RHS with the fractional part discarded,
2645 set REMAINDER to the remainder, return zero. i.e.
2646
2647 OLD_LHS = RHS * LHS + REMAINDER
2648
2649 SCRATCH is a bignum of the same size as the operands and result for
2650 use by the routine; its contents need not be initialized and are
2651 destroyed. LHS, REMAINDER and SCRATCH must be distinct.
2652 */
2653 int
tcDivide(integerPart * lhs,const integerPart * rhs,integerPart * remainder,integerPart * srhs,unsigned int parts)2654 APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
2655 integerPart *remainder, integerPart *srhs,
2656 unsigned int parts)
2657 {
2658 unsigned int n, shiftCount;
2659 integerPart mask;
2660
2661 assert(lhs != remainder && lhs != srhs && remainder != srhs);
2662
2663 shiftCount = tcMSB(rhs, parts) + 1;
2664 if (shiftCount == 0)
2665 return true;
2666
2667 shiftCount = parts * integerPartWidth - shiftCount;
2668 n = shiftCount / integerPartWidth;
2669 mask = (integerPart) 1 << (shiftCount % integerPartWidth);
2670
2671 tcAssign(srhs, rhs, parts);
2672 tcShiftLeft(srhs, parts, shiftCount);
2673 tcAssign(remainder, lhs, parts);
2674 tcSet(lhs, 0, parts);
2675
2676 /* Loop, subtracting SRHS if REMAINDER is greater and adding that to
2677 the total. */
2678 for (;;) {
2679 int compare;
2680
2681 compare = tcCompare(remainder, srhs, parts);
2682 if (compare >= 0) {
2683 tcSubtract(remainder, srhs, 0, parts);
2684 lhs[n] |= mask;
2685 }
2686
2687 if (shiftCount == 0)
2688 break;
2689 shiftCount--;
2690 tcShiftRight(srhs, parts, 1);
2691 if ((mask >>= 1) == 0) {
2692 mask = (integerPart) 1 << (integerPartWidth - 1);
2693 n--;
2694 }
2695 }
2696
2697 return false;
2698 }
2699
2700 /* Shift a bignum left COUNT bits in-place. Shifted in bits are zero.
2701 There are no restrictions on COUNT. */
2702 void
tcShiftLeft(integerPart * dst,unsigned int parts,unsigned int count)2703 APInt::tcShiftLeft(integerPart *dst, unsigned int parts, unsigned int count)
2704 {
2705 if (count) {
2706 unsigned int jump, shift;
2707
2708 /* Jump is the inter-part jump; shift is is intra-part shift. */
2709 jump = count / integerPartWidth;
2710 shift = count % integerPartWidth;
2711
2712 while (parts > jump) {
2713 integerPart part;
2714
2715 parts--;
2716
2717 /* dst[i] comes from the two parts src[i - jump] and, if we have
2718 an intra-part shift, src[i - jump - 1]. */
2719 part = dst[parts - jump];
2720 if (shift) {
2721 part <<= shift;
2722 if (parts >= jump + 1)
2723 part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
2724 }
2725
2726 dst[parts] = part;
2727 }
2728
2729 while (parts > 0)
2730 dst[--parts] = 0;
2731 }
2732 }
2733
2734 /* Shift a bignum right COUNT bits in-place. Shifted in bits are
2735 zero. There are no restrictions on COUNT. */
2736 void
tcShiftRight(integerPart * dst,unsigned int parts,unsigned int count)2737 APInt::tcShiftRight(integerPart *dst, unsigned int parts, unsigned int count)
2738 {
2739 if (count) {
2740 unsigned int i, jump, shift;
2741
2742 /* Jump is the inter-part jump; shift is is intra-part shift. */
2743 jump = count / integerPartWidth;
2744 shift = count % integerPartWidth;
2745
2746 /* Perform the shift. This leaves the most significant COUNT bits
2747 of the result at zero. */
2748 for (i = 0; i < parts; i++) {
2749 integerPart part;
2750
2751 if (i + jump >= parts) {
2752 part = 0;
2753 } else {
2754 part = dst[i + jump];
2755 if (shift) {
2756 part >>= shift;
2757 if (i + jump + 1 < parts)
2758 part |= dst[i + jump + 1] << (integerPartWidth - shift);
2759 }
2760 }
2761
2762 dst[i] = part;
2763 }
2764 }
2765 }
2766
2767 /* Bitwise and of two bignums. */
2768 void
tcAnd(integerPart * dst,const integerPart * rhs,unsigned int parts)2769 APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned int parts)
2770 {
2771 unsigned int i;
2772
2773 for (i = 0; i < parts; i++)
2774 dst[i] &= rhs[i];
2775 }
2776
2777 /* Bitwise inclusive or of two bignums. */
2778 void
tcOr(integerPart * dst,const integerPart * rhs,unsigned int parts)2779 APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned int parts)
2780 {
2781 unsigned int i;
2782
2783 for (i = 0; i < parts; i++)
2784 dst[i] |= rhs[i];
2785 }
2786
2787 /* Bitwise exclusive or of two bignums. */
2788 void
tcXor(integerPart * dst,const integerPart * rhs,unsigned int parts)2789 APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned int parts)
2790 {
2791 unsigned int i;
2792
2793 for (i = 0; i < parts; i++)
2794 dst[i] ^= rhs[i];
2795 }
2796
2797 /* Complement a bignum in-place. */
2798 void
tcComplement(integerPart * dst,unsigned int parts)2799 APInt::tcComplement(integerPart *dst, unsigned int parts)
2800 {
2801 unsigned int i;
2802
2803 for (i = 0; i < parts; i++)
2804 dst[i] = ~dst[i];
2805 }
2806
2807 /* Comparison (unsigned) of two bignums. */
2808 int
tcCompare(const integerPart * lhs,const integerPart * rhs,unsigned int parts)2809 APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
2810 unsigned int parts)
2811 {
2812 while (parts) {
2813 parts--;
2814 if (lhs[parts] == rhs[parts])
2815 continue;
2816
2817 if (lhs[parts] > rhs[parts])
2818 return 1;
2819 else
2820 return -1;
2821 }
2822
2823 return 0;
2824 }
2825
2826 /* Increment a bignum in-place, return the carry flag. */
2827 integerPart
tcIncrement(integerPart * dst,unsigned int parts)2828 APInt::tcIncrement(integerPart *dst, unsigned int parts)
2829 {
2830 unsigned int i;
2831
2832 for (i = 0; i < parts; i++)
2833 if (++dst[i] != 0)
2834 break;
2835
2836 return i == parts;
2837 }
2838
2839 /* Decrement a bignum in-place, return the borrow flag. */
2840 integerPart
tcDecrement(integerPart * dst,unsigned int parts)2841 APInt::tcDecrement(integerPart *dst, unsigned int parts) {
2842 for (unsigned int i = 0; i < parts; i++) {
2843 // If the current word is non-zero, then the decrement has no effect on the
2844 // higher-order words of the integer and no borrow can occur. Exit early.
2845 if (dst[i]--)
2846 return 0;
2847 }
2848 // If every word was zero, then there is a borrow.
2849 return 1;
2850 }
2851
2852
2853 /* Set the least significant BITS bits of a bignum, clear the
2854 rest. */
2855 void
tcSetLeastSignificantBits(integerPart * dst,unsigned int parts,unsigned int bits)2856 APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned int parts,
2857 unsigned int bits)
2858 {
2859 unsigned int i;
2860
2861 i = 0;
2862 while (bits > integerPartWidth) {
2863 dst[i++] = ~(integerPart) 0;
2864 bits -= integerPartWidth;
2865 }
2866
2867 if (bits)
2868 dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
2869
2870 while (i < parts)
2871 dst[i++] = 0;
2872 }
2873