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