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