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