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