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