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