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