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