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