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