1f22ef01cSRoman Divacky //===-- APInt.cpp - Implement APInt class ---------------------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements a class to represent arbitrary precision integer
11f22ef01cSRoman Divacky // constant values and provide a variety of arithmetic operations on them.
12f22ef01cSRoman Divacky //
13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
14f22ef01cSRoman Divacky
15f22ef01cSRoman Divacky #include "llvm/ADT/APInt.h"
163ca95b02SDimitry Andric #include "llvm/ADT/ArrayRef.h"
17f22ef01cSRoman Divacky #include "llvm/ADT/FoldingSet.h"
18dff0c46cSDimitry Andric #include "llvm/ADT/Hashing.h"
19*b5893f02SDimitry Andric #include "llvm/ADT/Optional.h"
20f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h"
21dff0c46cSDimitry Andric #include "llvm/ADT/StringRef.h"
22*b5893f02SDimitry Andric #include "llvm/ADT/bit.h"
234ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
24f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
25f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
26f22ef01cSRoman Divacky #include "llvm/Support/MathExtras.h"
27f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
28d88c1a5aSDimitry Andric #include <climits>
29f22ef01cSRoman Divacky #include <cmath>
30f22ef01cSRoman Divacky #include <cstdlib>
31139f7f9bSDimitry Andric #include <cstring>
32f22ef01cSRoman Divacky using namespace llvm;
33f22ef01cSRoman Divacky
3491bc56edSDimitry Andric #define DEBUG_TYPE "apint"
3591bc56edSDimitry Andric
36f22ef01cSRoman Divacky /// A utility function for allocating memory, checking for allocation failures,
37f22ef01cSRoman Divacky /// and ensuring the contents are zeroed.
getClearedMemory(unsigned numWords)38f22ef01cSRoman Divacky inline static uint64_t* getClearedMemory(unsigned numWords) {
39f22ef01cSRoman Divacky uint64_t *result = new uint64_t[numWords];
40f22ef01cSRoman Divacky memset(result, 0, numWords * sizeof(uint64_t));
41f22ef01cSRoman Divacky return result;
42f22ef01cSRoman Divacky }
43f22ef01cSRoman Divacky
44f22ef01cSRoman Divacky /// A utility function for allocating memory and checking for allocation
45f22ef01cSRoman Divacky /// failure. The content is not zeroed.
getMemory(unsigned numWords)46f22ef01cSRoman Divacky inline static uint64_t* getMemory(unsigned numWords) {
474ba319b5SDimitry Andric return new uint64_t[numWords];
48f22ef01cSRoman Divacky }
49f22ef01cSRoman Divacky
50f22ef01cSRoman Divacky /// A utility function that converts a character to a digit.
getDigit(char cdigit,uint8_t radix)51f22ef01cSRoman Divacky inline static unsigned getDigit(char cdigit, uint8_t radix) {
52f22ef01cSRoman Divacky unsigned r;
53f22ef01cSRoman Divacky
546122f3e6SDimitry Andric if (radix == 16 || radix == 36) {
55f22ef01cSRoman Divacky r = cdigit - '0';
56f22ef01cSRoman Divacky if (r <= 9)
57f22ef01cSRoman Divacky return r;
58f22ef01cSRoman Divacky
59f22ef01cSRoman Divacky r = cdigit - 'A';
606122f3e6SDimitry Andric if (r <= radix - 11U)
61f22ef01cSRoman Divacky return r + 10;
62f22ef01cSRoman Divacky
63f22ef01cSRoman Divacky r = cdigit - 'a';
646122f3e6SDimitry Andric if (r <= radix - 11U)
65f22ef01cSRoman Divacky return r + 10;
666122f3e6SDimitry Andric
676122f3e6SDimitry Andric radix = 10;
68f22ef01cSRoman Divacky }
69f22ef01cSRoman Divacky
70f22ef01cSRoman Divacky r = cdigit - '0';
71f22ef01cSRoman Divacky if (r < radix)
72f22ef01cSRoman Divacky return r;
73f22ef01cSRoman Divacky
74f22ef01cSRoman Divacky return -1U;
75f22ef01cSRoman Divacky }
76f22ef01cSRoman Divacky
77f22ef01cSRoman Divacky
initSlowCase(uint64_t val,bool isSigned)783ca95b02SDimitry Andric void APInt::initSlowCase(uint64_t val, bool isSigned) {
79f37b6182SDimitry Andric U.pVal = getClearedMemory(getNumWords());
80f37b6182SDimitry Andric U.pVal[0] = val;
81f22ef01cSRoman Divacky if (isSigned && int64_t(val) < 0)
82f22ef01cSRoman Divacky for (unsigned i = 1; i < getNumWords(); ++i)
83*b5893f02SDimitry Andric U.pVal[i] = WORDTYPE_MAX;
847a7e6055SDimitry Andric clearUnusedBits();
85f22ef01cSRoman Divacky }
86f22ef01cSRoman Divacky
initSlowCase(const APInt & that)87f22ef01cSRoman Divacky void APInt::initSlowCase(const APInt& that) {
88f37b6182SDimitry Andric U.pVal = getMemory(getNumWords());
89f37b6182SDimitry Andric memcpy(U.pVal, that.U.pVal, getNumWords() * APINT_WORD_SIZE);
90f22ef01cSRoman Divacky }
91f22ef01cSRoman Divacky
initFromArray(ArrayRef<uint64_t> bigVal)926122f3e6SDimitry Andric void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
93f22ef01cSRoman Divacky assert(BitWidth && "Bitwidth too small");
946122f3e6SDimitry Andric assert(bigVal.data() && "Null pointer detected!");
95f22ef01cSRoman Divacky if (isSingleWord())
96f37b6182SDimitry Andric U.VAL = bigVal[0];
97f22ef01cSRoman Divacky else {
98f22ef01cSRoman Divacky // Get memory, cleared to 0
99f37b6182SDimitry Andric U.pVal = getClearedMemory(getNumWords());
100f22ef01cSRoman Divacky // Calculate the number of words to copy
1016122f3e6SDimitry Andric unsigned words = std::min<unsigned>(bigVal.size(), getNumWords());
102f22ef01cSRoman Divacky // Copy the words from bigVal to pVal
103f37b6182SDimitry Andric memcpy(U.pVal, bigVal.data(), words * APINT_WORD_SIZE);
104f22ef01cSRoman Divacky }
105f22ef01cSRoman Divacky // Make sure unused high bits are cleared
106f22ef01cSRoman Divacky clearUnusedBits();
107f22ef01cSRoman Divacky }
108f22ef01cSRoman Divacky
APInt(unsigned numBits,ArrayRef<uint64_t> bigVal)1096122f3e6SDimitry Andric APInt::APInt(unsigned numBits, ArrayRef<uint64_t> bigVal)
1107a7e6055SDimitry Andric : BitWidth(numBits) {
1116122f3e6SDimitry Andric initFromArray(bigVal);
1126122f3e6SDimitry Andric }
1136122f3e6SDimitry Andric
APInt(unsigned numBits,unsigned numWords,const uint64_t bigVal[])1146122f3e6SDimitry Andric APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[])
1157a7e6055SDimitry Andric : BitWidth(numBits) {
1166122f3e6SDimitry Andric initFromArray(makeArrayRef(bigVal, numWords));
1176122f3e6SDimitry Andric }
1186122f3e6SDimitry Andric
APInt(unsigned numbits,StringRef Str,uint8_t radix)119ffd1746dSEd Schouten APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix)
120f37b6182SDimitry Andric : BitWidth(numbits) {
121f22ef01cSRoman Divacky assert(BitWidth && "Bitwidth too small");
122f22ef01cSRoman Divacky fromString(numbits, Str, radix);
123f22ef01cSRoman Divacky }
124f22ef01cSRoman Divacky
reallocate(unsigned NewBitWidth)1255517e702SDimitry Andric void APInt::reallocate(unsigned NewBitWidth) {
1265517e702SDimitry Andric // If the number of words is the same we can just change the width and stop.
1275517e702SDimitry Andric if (getNumWords() == getNumWords(NewBitWidth)) {
1285517e702SDimitry Andric BitWidth = NewBitWidth;
1295517e702SDimitry Andric return;
1305517e702SDimitry Andric }
1315517e702SDimitry Andric
1325517e702SDimitry Andric // If we have an allocation, delete it.
1335517e702SDimitry Andric if (!isSingleWord())
1345517e702SDimitry Andric delete [] U.pVal;
1355517e702SDimitry Andric
1365517e702SDimitry Andric // Update BitWidth.
1375517e702SDimitry Andric BitWidth = NewBitWidth;
1385517e702SDimitry Andric
1395517e702SDimitry Andric // If we are supposed to have an allocation, create it.
1405517e702SDimitry Andric if (!isSingleWord())
1415517e702SDimitry Andric U.pVal = getMemory(getNumWords());
1425517e702SDimitry Andric }
1435517e702SDimitry Andric
AssignSlowCase(const APInt & RHS)1446bc11b14SDimitry Andric void APInt::AssignSlowCase(const APInt& RHS) {
145f22ef01cSRoman Divacky // Don't do anything for X = X
146f22ef01cSRoman Divacky if (this == &RHS)
1476bc11b14SDimitry Andric return;
148f22ef01cSRoman Divacky
1495517e702SDimitry Andric // Adjust the bit width and handle allocations as necessary.
1505517e702SDimitry Andric reallocate(RHS.getBitWidth());
151f22ef01cSRoman Divacky
1525517e702SDimitry Andric // Copy the data.
1535517e702SDimitry Andric if (isSingleWord())
154f37b6182SDimitry Andric U.VAL = RHS.U.VAL;
1555517e702SDimitry Andric else
1565517e702SDimitry Andric memcpy(U.pVal, RHS.U.pVal, getNumWords() * APINT_WORD_SIZE);
157f22ef01cSRoman Divacky }
158f22ef01cSRoman Divacky
159ff0cc061SDimitry Andric /// This method 'profiles' an APInt for use with FoldingSet.
Profile(FoldingSetNodeID & ID) const160f22ef01cSRoman Divacky void APInt::Profile(FoldingSetNodeID& ID) const {
161f22ef01cSRoman Divacky ID.AddInteger(BitWidth);
162f22ef01cSRoman Divacky
163f22ef01cSRoman Divacky if (isSingleWord()) {
164f37b6182SDimitry Andric ID.AddInteger(U.VAL);
165f22ef01cSRoman Divacky return;
166f22ef01cSRoman Divacky }
167f22ef01cSRoman Divacky
168f22ef01cSRoman Divacky unsigned NumWords = getNumWords();
169f22ef01cSRoman Divacky for (unsigned i = 0; i < NumWords; ++i)
170f37b6182SDimitry Andric ID.AddInteger(U.pVal[i]);
171f22ef01cSRoman Divacky }
172f22ef01cSRoman Divacky
1734ba319b5SDimitry Andric /// Prefix increment operator. Increments the APInt by one.
operator ++()174f22ef01cSRoman Divacky APInt& APInt::operator++() {
175f22ef01cSRoman Divacky if (isSingleWord())
176f37b6182SDimitry Andric ++U.VAL;
177f22ef01cSRoman Divacky else
178f37b6182SDimitry Andric tcIncrement(U.pVal, getNumWords());
179f22ef01cSRoman Divacky return clearUnusedBits();
180f22ef01cSRoman Divacky }
181f22ef01cSRoman Divacky
1824ba319b5SDimitry Andric /// Prefix decrement operator. Decrements the APInt by one.
operator --()183f22ef01cSRoman Divacky APInt& APInt::operator--() {
184f22ef01cSRoman Divacky if (isSingleWord())
185f37b6182SDimitry Andric --U.VAL;
186f22ef01cSRoman Divacky else
187f37b6182SDimitry Andric tcDecrement(U.pVal, getNumWords());
188f22ef01cSRoman Divacky return clearUnusedBits();
189f22ef01cSRoman Divacky }
190f22ef01cSRoman Divacky
191f22ef01cSRoman Divacky /// Adds the RHS APint to this APInt.
192f22ef01cSRoman Divacky /// @returns this, after addition of RHS.
1934ba319b5SDimitry Andric /// Addition assignment operator.
operator +=(const APInt & RHS)194f22ef01cSRoman Divacky APInt& APInt::operator+=(const APInt& RHS) {
195f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
196f22ef01cSRoman Divacky if (isSingleWord())
197f37b6182SDimitry Andric U.VAL += RHS.U.VAL;
1987a7e6055SDimitry Andric else
199f37b6182SDimitry Andric tcAdd(U.pVal, RHS.U.pVal, 0, getNumWords());
200f22ef01cSRoman Divacky return clearUnusedBits();
201f22ef01cSRoman Divacky }
202f22ef01cSRoman Divacky
operator +=(uint64_t RHS)203d88c1a5aSDimitry Andric APInt& APInt::operator+=(uint64_t RHS) {
204d88c1a5aSDimitry Andric if (isSingleWord())
205f37b6182SDimitry Andric U.VAL += RHS;
206d88c1a5aSDimitry Andric else
207f37b6182SDimitry Andric tcAddPart(U.pVal, RHS, getNumWords());
208d88c1a5aSDimitry Andric return clearUnusedBits();
209d88c1a5aSDimitry Andric }
210d88c1a5aSDimitry Andric
211f22ef01cSRoman Divacky /// Subtracts the RHS APInt from this APInt
212f22ef01cSRoman Divacky /// @returns this, after subtraction
2134ba319b5SDimitry Andric /// Subtraction assignment operator.
operator -=(const APInt & RHS)214f22ef01cSRoman Divacky APInt& APInt::operator-=(const APInt& RHS) {
215f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
216f22ef01cSRoman Divacky if (isSingleWord())
217f37b6182SDimitry Andric U.VAL -= RHS.U.VAL;
218f22ef01cSRoman Divacky else
219f37b6182SDimitry Andric tcSubtract(U.pVal, RHS.U.pVal, 0, getNumWords());
220f22ef01cSRoman Divacky return clearUnusedBits();
221f22ef01cSRoman Divacky }
222f22ef01cSRoman Divacky
operator -=(uint64_t RHS)223d88c1a5aSDimitry Andric APInt& APInt::operator-=(uint64_t RHS) {
224d88c1a5aSDimitry Andric if (isSingleWord())
225f37b6182SDimitry Andric U.VAL -= RHS;
226d88c1a5aSDimitry Andric else
227f37b6182SDimitry Andric tcSubtractPart(U.pVal, RHS, getNumWords());
228d88c1a5aSDimitry Andric return clearUnusedBits();
229d88c1a5aSDimitry Andric }
230d88c1a5aSDimitry Andric
operator *(const APInt & RHS) const2310f5676f4SDimitry Andric APInt APInt::operator*(const APInt& RHS) const {
232f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
2330f5676f4SDimitry Andric if (isSingleWord())
2340f5676f4SDimitry Andric return APInt(BitWidth, U.VAL * RHS.U.VAL);
235f22ef01cSRoman Divacky
2360f5676f4SDimitry Andric APInt Result(getMemory(getNumWords()), getBitWidth());
237f22ef01cSRoman Divacky
2380f5676f4SDimitry Andric tcMultiply(Result.U.pVal, U.pVal, RHS.U.pVal, getNumWords());
239f22ef01cSRoman Divacky
2400f5676f4SDimitry Andric Result.clearUnusedBits();
2410f5676f4SDimitry Andric return Result;
242f22ef01cSRoman Divacky }
243f22ef01cSRoman Divacky
AndAssignSlowCase(const APInt & RHS)2446bc11b14SDimitry Andric void APInt::AndAssignSlowCase(const APInt& RHS) {
245f37b6182SDimitry Andric tcAnd(U.pVal, RHS.U.pVal, getNumWords());
246f22ef01cSRoman Divacky }
247f22ef01cSRoman Divacky
OrAssignSlowCase(const APInt & RHS)2486bc11b14SDimitry Andric void APInt::OrAssignSlowCase(const APInt& RHS) {
249f37b6182SDimitry Andric tcOr(U.pVal, RHS.U.pVal, getNumWords());
250f22ef01cSRoman Divacky }
251f22ef01cSRoman Divacky
XorAssignSlowCase(const APInt & RHS)2526bc11b14SDimitry Andric void APInt::XorAssignSlowCase(const APInt& RHS) {
253f37b6182SDimitry Andric tcXor(U.pVal, RHS.U.pVal, getNumWords());
254f22ef01cSRoman Divacky }
255f22ef01cSRoman Divacky
operator *=(const APInt & RHS)2560f5676f4SDimitry Andric APInt& APInt::operator*=(const APInt& RHS) {
257f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
2580f5676f4SDimitry Andric *this = *this * RHS;
2590f5676f4SDimitry Andric return *this;
2600f5676f4SDimitry Andric }
2610f5676f4SDimitry Andric
operator *=(uint64_t RHS)2620f5676f4SDimitry Andric APInt& APInt::operator*=(uint64_t RHS) {
2630f5676f4SDimitry Andric if (isSingleWord()) {
2640f5676f4SDimitry Andric U.VAL *= RHS;
2650f5676f4SDimitry Andric } else {
2660f5676f4SDimitry Andric unsigned NumWords = getNumWords();
2670f5676f4SDimitry Andric tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords, false);
2680f5676f4SDimitry Andric }
2690f5676f4SDimitry Andric return clearUnusedBits();
270f22ef01cSRoman Divacky }
271f22ef01cSRoman Divacky
EqualSlowCase(const APInt & RHS) const272f22ef01cSRoman Divacky bool APInt::EqualSlowCase(const APInt& RHS) const {
273f37b6182SDimitry Andric return std::equal(U.pVal, U.pVal + getNumWords(), RHS.U.pVal);
274f22ef01cSRoman Divacky }
275f22ef01cSRoman Divacky
compare(const APInt & RHS) const27651690af2SDimitry Andric int APInt::compare(const APInt& RHS) const {
277f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
278f22ef01cSRoman Divacky if (isSingleWord())
279f37b6182SDimitry Andric return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL;
280f22ef01cSRoman Divacky
281f37b6182SDimitry Andric return tcCompare(U.pVal, RHS.U.pVal, getNumWords());
282f22ef01cSRoman Divacky }
283f22ef01cSRoman Divacky
compareSigned(const APInt & RHS) const28451690af2SDimitry Andric int APInt::compareSigned(const APInt& RHS) const {
285f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
286f22ef01cSRoman Divacky if (isSingleWord()) {
287f37b6182SDimitry Andric int64_t lhsSext = SignExtend64(U.VAL, BitWidth);
288f37b6182SDimitry Andric int64_t rhsSext = SignExtend64(RHS.U.VAL, BitWidth);
28951690af2SDimitry Andric return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
290f22ef01cSRoman Divacky }
291f22ef01cSRoman Divacky
292f22ef01cSRoman Divacky bool lhsNeg = isNegative();
2933ca95b02SDimitry Andric bool rhsNeg = RHS.isNegative();
294f22ef01cSRoman Divacky
2953ca95b02SDimitry Andric // If the sign bits don't match, then (LHS < RHS) if LHS is negative
2963ca95b02SDimitry Andric if (lhsNeg != rhsNeg)
29751690af2SDimitry Andric return lhsNeg ? -1 : 1;
2983ca95b02SDimitry Andric
2997a7e6055SDimitry Andric // Otherwise we can just use an unsigned comparison, because even negative
3003ca95b02SDimitry Andric // numbers compare correctly this way if both have the same signed-ness.
301f37b6182SDimitry Andric return tcCompare(U.pVal, RHS.U.pVal, getNumWords());
302f22ef01cSRoman Divacky }
303f22ef01cSRoman Divacky
setBitsSlowCase(unsigned loBit,unsigned hiBit)3047a7e6055SDimitry Andric void APInt::setBitsSlowCase(unsigned loBit, unsigned hiBit) {
3057a7e6055SDimitry Andric unsigned loWord = whichWord(loBit);
3067a7e6055SDimitry Andric unsigned hiWord = whichWord(hiBit);
3077a7e6055SDimitry Andric
3087a7e6055SDimitry Andric // Create an initial mask for the low word with zeros below loBit.
309*b5893f02SDimitry Andric uint64_t loMask = WORDTYPE_MAX << whichBit(loBit);
3107a7e6055SDimitry Andric
3117a7e6055SDimitry Andric // If hiBit is not aligned, we need a high mask.
3127a7e6055SDimitry Andric unsigned hiShiftAmt = whichBit(hiBit);
3137a7e6055SDimitry Andric if (hiShiftAmt != 0) {
3147a7e6055SDimitry Andric // Create a high mask with zeros above hiBit.
315*b5893f02SDimitry Andric uint64_t hiMask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - hiShiftAmt);
3167a7e6055SDimitry Andric // If loWord and hiWord are equal, then we combine the masks. Otherwise,
3177a7e6055SDimitry Andric // set the bits in hiWord.
3187a7e6055SDimitry Andric if (hiWord == loWord)
3197a7e6055SDimitry Andric loMask &= hiMask;
3207a7e6055SDimitry Andric else
321f37b6182SDimitry Andric U.pVal[hiWord] |= hiMask;
3227a7e6055SDimitry Andric }
3237a7e6055SDimitry Andric // Apply the mask to the low word.
324f37b6182SDimitry Andric U.pVal[loWord] |= loMask;
3257a7e6055SDimitry Andric
3267a7e6055SDimitry Andric // Fill any words between loWord and hiWord with all ones.
3277a7e6055SDimitry Andric for (unsigned word = loWord + 1; word < hiWord; ++word)
328*b5893f02SDimitry Andric U.pVal[word] = WORDTYPE_MAX;
329f22ef01cSRoman Divacky }
330f22ef01cSRoman Divacky
3314ba319b5SDimitry Andric /// Toggle every bit to its opposite value.
flipAllBitsSlowCase()3327a7e6055SDimitry Andric void APInt::flipAllBitsSlowCase() {
333f37b6182SDimitry Andric tcComplement(U.pVal, getNumWords());
3347a7e6055SDimitry Andric clearUnusedBits();
3357a7e6055SDimitry Andric }
336f22ef01cSRoman Divacky
337f22ef01cSRoman Divacky /// Toggle a given bit to its opposite value whose position is given
338f22ef01cSRoman Divacky /// as "bitPosition".
3394ba319b5SDimitry Andric /// Toggles a given bit to its opposite value.
flipBit(unsigned bitPosition)3402754fe60SDimitry Andric void APInt::flipBit(unsigned bitPosition) {
341f22ef01cSRoman Divacky assert(bitPosition < BitWidth && "Out of the bit-width range!");
3422754fe60SDimitry Andric if ((*this)[bitPosition]) clearBit(bitPosition);
3432754fe60SDimitry Andric else setBit(bitPosition);
344f22ef01cSRoman Divacky }
345f22ef01cSRoman Divacky
insertBits(const APInt & subBits,unsigned bitPosition)3467a7e6055SDimitry Andric void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
3477a7e6055SDimitry Andric unsigned subBitWidth = subBits.getBitWidth();
3487a7e6055SDimitry Andric assert(0 < subBitWidth && (subBitWidth + bitPosition) <= BitWidth &&
3497a7e6055SDimitry Andric "Illegal bit insertion");
3507a7e6055SDimitry Andric
3517a7e6055SDimitry Andric // Insertion is a direct copy.
3527a7e6055SDimitry Andric if (subBitWidth == BitWidth) {
3537a7e6055SDimitry Andric *this = subBits;
3547a7e6055SDimitry Andric return;
3557a7e6055SDimitry Andric }
3567a7e6055SDimitry Andric
3577a7e6055SDimitry Andric // Single word result can be done as a direct bitmask.
3587a7e6055SDimitry Andric if (isSingleWord()) {
359*b5893f02SDimitry Andric uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
360f37b6182SDimitry Andric U.VAL &= ~(mask << bitPosition);
361f37b6182SDimitry Andric U.VAL |= (subBits.U.VAL << bitPosition);
3627a7e6055SDimitry Andric return;
3637a7e6055SDimitry Andric }
3647a7e6055SDimitry Andric
3657a7e6055SDimitry Andric unsigned loBit = whichBit(bitPosition);
3667a7e6055SDimitry Andric unsigned loWord = whichWord(bitPosition);
3677a7e6055SDimitry Andric unsigned hi1Word = whichWord(bitPosition + subBitWidth - 1);
3687a7e6055SDimitry Andric
3697a7e6055SDimitry Andric // Insertion within a single word can be done as a direct bitmask.
3707a7e6055SDimitry Andric if (loWord == hi1Word) {
371*b5893f02SDimitry Andric uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
372f37b6182SDimitry Andric U.pVal[loWord] &= ~(mask << loBit);
373f37b6182SDimitry Andric U.pVal[loWord] |= (subBits.U.VAL << loBit);
3747a7e6055SDimitry Andric return;
3757a7e6055SDimitry Andric }
3767a7e6055SDimitry Andric
3777a7e6055SDimitry Andric // Insert on word boundaries.
3787a7e6055SDimitry Andric if (loBit == 0) {
3797a7e6055SDimitry Andric // Direct copy whole words.
3807a7e6055SDimitry Andric unsigned numWholeSubWords = subBitWidth / APINT_BITS_PER_WORD;
381f37b6182SDimitry Andric memcpy(U.pVal + loWord, subBits.getRawData(),
3827a7e6055SDimitry Andric numWholeSubWords * APINT_WORD_SIZE);
3837a7e6055SDimitry Andric
3847a7e6055SDimitry Andric // Mask+insert remaining bits.
3857a7e6055SDimitry Andric unsigned remainingBits = subBitWidth % APINT_BITS_PER_WORD;
3867a7e6055SDimitry Andric if (remainingBits != 0) {
387*b5893f02SDimitry Andric uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - remainingBits);
388f37b6182SDimitry Andric U.pVal[hi1Word] &= ~mask;
389f37b6182SDimitry Andric U.pVal[hi1Word] |= subBits.getWord(subBitWidth - 1);
3907a7e6055SDimitry Andric }
3917a7e6055SDimitry Andric return;
3927a7e6055SDimitry Andric }
3937a7e6055SDimitry Andric
3947a7e6055SDimitry Andric // General case - set/clear individual bits in dst based on src.
3957a7e6055SDimitry Andric // TODO - there is scope for optimization here, but at the moment this code
3967a7e6055SDimitry Andric // path is barely used so prefer readability over performance.
3977a7e6055SDimitry Andric for (unsigned i = 0; i != subBitWidth; ++i) {
3987a7e6055SDimitry Andric if (subBits[i])
3997a7e6055SDimitry Andric setBit(bitPosition + i);
4007a7e6055SDimitry Andric else
4017a7e6055SDimitry Andric clearBit(bitPosition + i);
4027a7e6055SDimitry Andric }
4037a7e6055SDimitry Andric }
4047a7e6055SDimitry Andric
extractBits(unsigned numBits,unsigned bitPosition) const4057a7e6055SDimitry Andric APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
4067a7e6055SDimitry Andric assert(numBits > 0 && "Can't extract zero bits");
4077a7e6055SDimitry Andric assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
4087a7e6055SDimitry Andric "Illegal bit extraction");
4097a7e6055SDimitry Andric
4107a7e6055SDimitry Andric if (isSingleWord())
411f37b6182SDimitry Andric return APInt(numBits, U.VAL >> bitPosition);
4127a7e6055SDimitry Andric
4137a7e6055SDimitry Andric unsigned loBit = whichBit(bitPosition);
4147a7e6055SDimitry Andric unsigned loWord = whichWord(bitPosition);
4157a7e6055SDimitry Andric unsigned hiWord = whichWord(bitPosition + numBits - 1);
4167a7e6055SDimitry Andric
4177a7e6055SDimitry Andric // Single word result extracting bits from a single word source.
4187a7e6055SDimitry Andric if (loWord == hiWord)
419f37b6182SDimitry Andric return APInt(numBits, U.pVal[loWord] >> loBit);
4207a7e6055SDimitry Andric
4217a7e6055SDimitry Andric // Extracting bits that start on a source word boundary can be done
4227a7e6055SDimitry Andric // as a fast memory copy.
4237a7e6055SDimitry Andric if (loBit == 0)
424f37b6182SDimitry Andric return APInt(numBits, makeArrayRef(U.pVal + loWord, 1 + hiWord - loWord));
4257a7e6055SDimitry Andric
4267a7e6055SDimitry Andric // General case - shift + copy source words directly into place.
4277a7e6055SDimitry Andric APInt Result(numBits, 0);
4287a7e6055SDimitry Andric unsigned NumSrcWords = getNumWords();
4297a7e6055SDimitry Andric unsigned NumDstWords = Result.getNumWords();
4307a7e6055SDimitry Andric
4314ba319b5SDimitry Andric uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
4327a7e6055SDimitry Andric for (unsigned word = 0; word < NumDstWords; ++word) {
433f37b6182SDimitry Andric uint64_t w0 = U.pVal[loWord + word];
4347a7e6055SDimitry Andric uint64_t w1 =
435f37b6182SDimitry Andric (loWord + word + 1) < NumSrcWords ? U.pVal[loWord + word + 1] : 0;
4364ba319b5SDimitry Andric DestPtr[word] = (w0 >> loBit) | (w1 << (APINT_BITS_PER_WORD - loBit));
4377a7e6055SDimitry Andric }
4387a7e6055SDimitry Andric
4397a7e6055SDimitry Andric return Result.clearUnusedBits();
4407a7e6055SDimitry Andric }
4417a7e6055SDimitry Andric
getBitsNeeded(StringRef str,uint8_t radix)442ffd1746dSEd Schouten unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
443f22ef01cSRoman Divacky assert(!str.empty() && "Invalid string length");
4446122f3e6SDimitry Andric assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
4456122f3e6SDimitry Andric radix == 36) &&
4466122f3e6SDimitry Andric "Radix should be 2, 8, 10, 16, or 36!");
447f22ef01cSRoman Divacky
448f22ef01cSRoman Divacky size_t slen = str.size();
449f22ef01cSRoman Divacky
450f22ef01cSRoman Divacky // Each computation below needs to know if it's negative.
451f22ef01cSRoman Divacky StringRef::iterator p = str.begin();
452f22ef01cSRoman Divacky unsigned isNegative = *p == '-';
453f22ef01cSRoman Divacky if (*p == '-' || *p == '+') {
454f22ef01cSRoman Divacky p++;
455f22ef01cSRoman Divacky slen--;
456f22ef01cSRoman Divacky assert(slen && "String is only a sign, needs a value.");
457f22ef01cSRoman Divacky }
458f22ef01cSRoman Divacky
459f22ef01cSRoman Divacky // For radixes of power-of-two values, the bits required is accurately and
460f22ef01cSRoman Divacky // easily computed
461f22ef01cSRoman Divacky if (radix == 2)
462f22ef01cSRoman Divacky return slen + isNegative;
463f22ef01cSRoman Divacky if (radix == 8)
464f22ef01cSRoman Divacky return slen * 3 + isNegative;
465f22ef01cSRoman Divacky if (radix == 16)
466f22ef01cSRoman Divacky return slen * 4 + isNegative;
467f22ef01cSRoman Divacky
4686122f3e6SDimitry Andric // FIXME: base 36
4696122f3e6SDimitry Andric
470f22ef01cSRoman Divacky // This is grossly inefficient but accurate. We could probably do something
471f22ef01cSRoman Divacky // with a computation of roughly slen*64/20 and then adjust by the value of
472f22ef01cSRoman Divacky // the first few digits. But, I'm not sure how accurate that could be.
473f22ef01cSRoman Divacky
474f22ef01cSRoman Divacky // Compute a sufficient number of bits that is always large enough but might
475f22ef01cSRoman Divacky // be too large. This avoids the assertion in the constructor. This
476f22ef01cSRoman Divacky // calculation doesn't work appropriately for the numbers 0-9, so just use 4
477f22ef01cSRoman Divacky // bits in that case.
4786122f3e6SDimitry Andric unsigned sufficient
4796122f3e6SDimitry Andric = radix == 10? (slen == 1 ? 4 : slen * 64/18)
4806122f3e6SDimitry Andric : (slen == 1 ? 7 : slen * 16/3);
481f22ef01cSRoman Divacky
482f22ef01cSRoman Divacky // Convert to the actual binary value.
483f22ef01cSRoman Divacky APInt tmp(sufficient, StringRef(p, slen), radix);
484f22ef01cSRoman Divacky
485f22ef01cSRoman Divacky // Compute how many bits are required. If the log is infinite, assume we need
486f22ef01cSRoman Divacky // just bit.
487f22ef01cSRoman Divacky unsigned log = tmp.logBase2();
488f22ef01cSRoman Divacky if (log == (unsigned)-1) {
489f22ef01cSRoman Divacky return isNegative + 1;
490f22ef01cSRoman Divacky } else {
491f22ef01cSRoman Divacky return isNegative + log + 1;
492f22ef01cSRoman Divacky }
493f22ef01cSRoman Divacky }
494f22ef01cSRoman Divacky
hash_value(const APInt & Arg)495dff0c46cSDimitry Andric hash_code llvm::hash_value(const APInt &Arg) {
496dff0c46cSDimitry Andric if (Arg.isSingleWord())
497f37b6182SDimitry Andric return hash_combine(Arg.U.VAL);
498f22ef01cSRoman Divacky
499f37b6182SDimitry Andric return hash_combine_range(Arg.U.pVal, Arg.U.pVal + Arg.getNumWords());
500f22ef01cSRoman Divacky }
501f22ef01cSRoman Divacky
isSplat(unsigned SplatSizeInBits) const502ff0cc061SDimitry Andric bool APInt::isSplat(unsigned SplatSizeInBits) const {
503ff0cc061SDimitry Andric assert(getBitWidth() % SplatSizeInBits == 0 &&
504ff0cc061SDimitry Andric "SplatSizeInBits must divide width!");
505ff0cc061SDimitry Andric // We can check that all parts of an integer are equal by making use of a
506ff0cc061SDimitry Andric // little trick: rotate and check if it's still the same value.
507ff0cc061SDimitry Andric return *this == rotl(SplatSizeInBits);
508ff0cc061SDimitry Andric }
509ff0cc061SDimitry Andric
510ff0cc061SDimitry Andric /// This function returns the high "numBits" bits of this APInt.
getHiBits(unsigned numBits) const511f22ef01cSRoman Divacky APInt APInt::getHiBits(unsigned numBits) const {
5127a7e6055SDimitry Andric return this->lshr(BitWidth - numBits);
513f22ef01cSRoman Divacky }
514f22ef01cSRoman Divacky
515ff0cc061SDimitry Andric /// This function returns the low "numBits" bits of this APInt.
getLoBits(unsigned numBits) const516f22ef01cSRoman Divacky APInt APInt::getLoBits(unsigned numBits) const {
5177a7e6055SDimitry Andric APInt Result(getLowBitsSet(BitWidth, numBits));
5187a7e6055SDimitry Andric Result &= *this;
5197a7e6055SDimitry Andric return Result;
520f22ef01cSRoman Divacky }
521f22ef01cSRoman Divacky
522f37b6182SDimitry Andric /// Return a value containing V broadcasted over NewLen bits.
getSplat(unsigned NewLen,const APInt & V)523f37b6182SDimitry Andric APInt APInt::getSplat(unsigned NewLen, const APInt &V) {
524f37b6182SDimitry Andric assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
525f37b6182SDimitry Andric
526f37b6182SDimitry Andric APInt Val = V.zextOrSelf(NewLen);
527f37b6182SDimitry Andric for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
528f37b6182SDimitry Andric Val |= Val << I;
529f37b6182SDimitry Andric
530f37b6182SDimitry Andric return Val;
531f37b6182SDimitry Andric }
532f37b6182SDimitry Andric
countLeadingZerosSlowCase() const533f22ef01cSRoman Divacky unsigned APInt::countLeadingZerosSlowCase() const {
5343ca95b02SDimitry Andric unsigned Count = 0;
5353ca95b02SDimitry Andric for (int i = getNumWords()-1; i >= 0; --i) {
536f37b6182SDimitry Andric uint64_t V = U.pVal[i];
5373ca95b02SDimitry Andric if (V == 0)
538f22ef01cSRoman Divacky Count += APINT_BITS_PER_WORD;
539f22ef01cSRoman Divacky else {
5403ca95b02SDimitry Andric Count += llvm::countLeadingZeros(V);
541f22ef01cSRoman Divacky break;
542f22ef01cSRoman Divacky }
543f22ef01cSRoman Divacky }
5443ca95b02SDimitry Andric // Adjust for unused bits in the most significant word (they are zero).
5453ca95b02SDimitry Andric unsigned Mod = BitWidth % APINT_BITS_PER_WORD;
5463ca95b02SDimitry Andric Count -= Mod > 0 ? APINT_BITS_PER_WORD - Mod : 0;
547f22ef01cSRoman Divacky return Count;
548f22ef01cSRoman Divacky }
549f22ef01cSRoman Divacky
countLeadingOnesSlowCase() const550edd7eaddSDimitry Andric unsigned APInt::countLeadingOnesSlowCase() const {
551f22ef01cSRoman Divacky unsigned highWordBits = BitWidth % APINT_BITS_PER_WORD;
552f22ef01cSRoman Divacky unsigned shift;
553f22ef01cSRoman Divacky if (!highWordBits) {
554f22ef01cSRoman Divacky highWordBits = APINT_BITS_PER_WORD;
555f22ef01cSRoman Divacky shift = 0;
556f22ef01cSRoman Divacky } else {
557f22ef01cSRoman Divacky shift = APINT_BITS_PER_WORD - highWordBits;
558f22ef01cSRoman Divacky }
559f22ef01cSRoman Divacky int i = getNumWords() - 1;
560f37b6182SDimitry Andric unsigned Count = llvm::countLeadingOnes(U.pVal[i] << shift);
561f22ef01cSRoman Divacky if (Count == highWordBits) {
562f22ef01cSRoman Divacky for (i--; i >= 0; --i) {
563*b5893f02SDimitry Andric if (U.pVal[i] == WORDTYPE_MAX)
564f22ef01cSRoman Divacky Count += APINT_BITS_PER_WORD;
565f22ef01cSRoman Divacky else {
566f37b6182SDimitry Andric Count += llvm::countLeadingOnes(U.pVal[i]);
567f22ef01cSRoman Divacky break;
568f22ef01cSRoman Divacky }
569f22ef01cSRoman Divacky }
570f22ef01cSRoman Divacky }
571f22ef01cSRoman Divacky return Count;
572f22ef01cSRoman Divacky }
573f22ef01cSRoman Divacky
countTrailingZerosSlowCase() const574edd7eaddSDimitry Andric unsigned APInt::countTrailingZerosSlowCase() const {
575f22ef01cSRoman Divacky unsigned Count = 0;
576f22ef01cSRoman Divacky unsigned i = 0;
577f37b6182SDimitry Andric for (; i < getNumWords() && U.pVal[i] == 0; ++i)
578f22ef01cSRoman Divacky Count += APINT_BITS_PER_WORD;
579f22ef01cSRoman Divacky if (i < getNumWords())
580f37b6182SDimitry Andric Count += llvm::countTrailingZeros(U.pVal[i]);
581f22ef01cSRoman Divacky return std::min(Count, BitWidth);
582f22ef01cSRoman Divacky }
583f22ef01cSRoman Divacky
countTrailingOnesSlowCase() const584f22ef01cSRoman Divacky unsigned APInt::countTrailingOnesSlowCase() const {
585f22ef01cSRoman Divacky unsigned Count = 0;
586f22ef01cSRoman Divacky unsigned i = 0;
587*b5893f02SDimitry Andric for (; i < getNumWords() && U.pVal[i] == WORDTYPE_MAX; ++i)
588f22ef01cSRoman Divacky Count += APINT_BITS_PER_WORD;
589f22ef01cSRoman Divacky if (i < getNumWords())
590f37b6182SDimitry Andric Count += llvm::countTrailingOnes(U.pVal[i]);
59151690af2SDimitry Andric assert(Count <= BitWidth);
59251690af2SDimitry Andric return Count;
593f22ef01cSRoman Divacky }
594f22ef01cSRoman Divacky
countPopulationSlowCase() const595f22ef01cSRoman Divacky unsigned APInt::countPopulationSlowCase() const {
596f22ef01cSRoman Divacky unsigned Count = 0;
597f22ef01cSRoman Divacky for (unsigned i = 0; i < getNumWords(); ++i)
598f37b6182SDimitry Andric Count += llvm::countPopulation(U.pVal[i]);
599f22ef01cSRoman Divacky return Count;
600f22ef01cSRoman Divacky }
601f22ef01cSRoman Divacky
intersectsSlowCase(const APInt & RHS) const6026bc11b14SDimitry Andric bool APInt::intersectsSlowCase(const APInt &RHS) const {
6036bc11b14SDimitry Andric for (unsigned i = 0, e = getNumWords(); i != e; ++i)
604f37b6182SDimitry Andric if ((U.pVal[i] & RHS.U.pVal[i]) != 0)
6056bc11b14SDimitry Andric return true;
6066bc11b14SDimitry Andric
6076bc11b14SDimitry Andric return false;
6086bc11b14SDimitry Andric }
6096bc11b14SDimitry Andric
isSubsetOfSlowCase(const APInt & RHS) const6106bc11b14SDimitry Andric bool APInt::isSubsetOfSlowCase(const APInt &RHS) const {
6116bc11b14SDimitry Andric for (unsigned i = 0, e = getNumWords(); i != e; ++i)
612f37b6182SDimitry Andric if ((U.pVal[i] & ~RHS.U.pVal[i]) != 0)
6136bc11b14SDimitry Andric return false;
6146bc11b14SDimitry Andric
6156bc11b14SDimitry Andric return true;
6166bc11b14SDimitry Andric }
6176bc11b14SDimitry Andric
byteSwap() const618f22ef01cSRoman Divacky APInt APInt::byteSwap() const {
619f22ef01cSRoman Divacky assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
620f22ef01cSRoman Divacky if (BitWidth == 16)
621f37b6182SDimitry Andric return APInt(BitWidth, ByteSwap_16(uint16_t(U.VAL)));
622dff0c46cSDimitry Andric if (BitWidth == 32)
623f37b6182SDimitry Andric return APInt(BitWidth, ByteSwap_32(unsigned(U.VAL)));
624dff0c46cSDimitry Andric if (BitWidth == 48) {
625f37b6182SDimitry Andric unsigned Tmp1 = unsigned(U.VAL >> 16);
626f22ef01cSRoman Divacky Tmp1 = ByteSwap_32(Tmp1);
627f37b6182SDimitry Andric uint16_t Tmp2 = uint16_t(U.VAL);
628f22ef01cSRoman Divacky Tmp2 = ByteSwap_16(Tmp2);
629f22ef01cSRoman Divacky return APInt(BitWidth, (uint64_t(Tmp2) << 32) | Tmp1);
630dff0c46cSDimitry Andric }
631dff0c46cSDimitry Andric if (BitWidth == 64)
632f37b6182SDimitry Andric return APInt(BitWidth, ByteSwap_64(U.VAL));
633dff0c46cSDimitry Andric
634dff0c46cSDimitry Andric APInt Result(getNumWords() * APINT_BITS_PER_WORD, 0);
635dff0c46cSDimitry Andric for (unsigned I = 0, N = getNumWords(); I != N; ++I)
636f37b6182SDimitry Andric Result.U.pVal[I] = ByteSwap_64(U.pVal[N - I - 1]);
637dff0c46cSDimitry Andric if (Result.BitWidth != BitWidth) {
6387a7e6055SDimitry Andric Result.lshrInPlace(Result.BitWidth - BitWidth);
639dff0c46cSDimitry Andric Result.BitWidth = BitWidth;
640f22ef01cSRoman Divacky }
641f22ef01cSRoman Divacky return Result;
642f22ef01cSRoman Divacky }
643f22ef01cSRoman Divacky
reverseBits() const6443ca95b02SDimitry Andric APInt APInt::reverseBits() const {
6453ca95b02SDimitry Andric switch (BitWidth) {
6463ca95b02SDimitry Andric case 64:
647f37b6182SDimitry Andric return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL));
6483ca95b02SDimitry Andric case 32:
649f37b6182SDimitry Andric return APInt(BitWidth, llvm::reverseBits<uint32_t>(U.VAL));
6503ca95b02SDimitry Andric case 16:
651f37b6182SDimitry Andric return APInt(BitWidth, llvm::reverseBits<uint16_t>(U.VAL));
6523ca95b02SDimitry Andric case 8:
653f37b6182SDimitry Andric return APInt(BitWidth, llvm::reverseBits<uint8_t>(U.VAL));
6543ca95b02SDimitry Andric default:
6553ca95b02SDimitry Andric break;
6563ca95b02SDimitry Andric }
6573ca95b02SDimitry Andric
6583ca95b02SDimitry Andric APInt Val(*this);
6596bc11b14SDimitry Andric APInt Reversed(BitWidth, 0);
6606bc11b14SDimitry Andric unsigned S = BitWidth;
6613ca95b02SDimitry Andric
6626bc11b14SDimitry Andric for (; Val != 0; Val.lshrInPlace(1)) {
6633ca95b02SDimitry Andric Reversed <<= 1;
6646bc11b14SDimitry Andric Reversed |= Val[0];
6653ca95b02SDimitry Andric --S;
6663ca95b02SDimitry Andric }
6673ca95b02SDimitry Andric
6683ca95b02SDimitry Andric Reversed <<= S;
6693ca95b02SDimitry Andric return Reversed;
6703ca95b02SDimitry Andric }
6713ca95b02SDimitry Andric
GreatestCommonDivisor(APInt A,APInt B)6727a7e6055SDimitry Andric APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) {
6737a7e6055SDimitry Andric // Fast-path a common case.
6747a7e6055SDimitry Andric if (A == B) return A;
6757a7e6055SDimitry Andric
6767a7e6055SDimitry Andric // Corner cases: if either operand is zero, the other is the gcd.
6777a7e6055SDimitry Andric if (!A) return B;
6787a7e6055SDimitry Andric if (!B) return A;
6797a7e6055SDimitry Andric
6807a7e6055SDimitry Andric // Count common powers of 2 and remove all other powers of 2.
6817a7e6055SDimitry Andric unsigned Pow2;
6827a7e6055SDimitry Andric {
6837a7e6055SDimitry Andric unsigned Pow2_A = A.countTrailingZeros();
6847a7e6055SDimitry Andric unsigned Pow2_B = B.countTrailingZeros();
6857a7e6055SDimitry Andric if (Pow2_A > Pow2_B) {
6867a7e6055SDimitry Andric A.lshrInPlace(Pow2_A - Pow2_B);
6877a7e6055SDimitry Andric Pow2 = Pow2_B;
6887a7e6055SDimitry Andric } else if (Pow2_B > Pow2_A) {
6897a7e6055SDimitry Andric B.lshrInPlace(Pow2_B - Pow2_A);
6907a7e6055SDimitry Andric Pow2 = Pow2_A;
6917a7e6055SDimitry Andric } else {
6927a7e6055SDimitry Andric Pow2 = Pow2_A;
693f22ef01cSRoman Divacky }
6947a7e6055SDimitry Andric }
6957a7e6055SDimitry Andric
6967a7e6055SDimitry Andric // Both operands are odd multiples of 2^Pow_2:
6977a7e6055SDimitry Andric //
6987a7e6055SDimitry Andric // gcd(a, b) = gcd(|a - b| / 2^i, min(a, b))
6997a7e6055SDimitry Andric //
7007a7e6055SDimitry Andric // This is a modified version of Stein's algorithm, taking advantage of
7017a7e6055SDimitry Andric // efficient countTrailingZeros().
7027a7e6055SDimitry Andric while (A != B) {
7037a7e6055SDimitry Andric if (A.ugt(B)) {
7047a7e6055SDimitry Andric A -= B;
7057a7e6055SDimitry Andric A.lshrInPlace(A.countTrailingZeros() - Pow2);
7067a7e6055SDimitry Andric } else {
7077a7e6055SDimitry Andric B -= A;
7087a7e6055SDimitry Andric B.lshrInPlace(B.countTrailingZeros() - Pow2);
7097a7e6055SDimitry Andric }
7107a7e6055SDimitry Andric }
7117a7e6055SDimitry Andric
712f22ef01cSRoman Divacky return A;
713f22ef01cSRoman Divacky }
714f22ef01cSRoman Divacky
RoundDoubleToAPInt(double Double,unsigned width)715f22ef01cSRoman Divacky APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
716*b5893f02SDimitry Andric uint64_t I = bit_cast<uint64_t>(Double);
717f22ef01cSRoman Divacky
718f22ef01cSRoman Divacky // Get the sign bit from the highest order bit
719*b5893f02SDimitry Andric bool isNeg = I >> 63;
720f22ef01cSRoman Divacky
721f22ef01cSRoman Divacky // Get the 11-bit exponent and adjust for the 1023 bit bias
722*b5893f02SDimitry Andric int64_t exp = ((I >> 52) & 0x7ff) - 1023;
723f22ef01cSRoman Divacky
724f22ef01cSRoman Divacky // If the exponent is negative, the value is < 0 so just return 0.
725f22ef01cSRoman Divacky if (exp < 0)
726f22ef01cSRoman Divacky return APInt(width, 0u);
727f22ef01cSRoman Divacky
728f22ef01cSRoman Divacky // Extract the mantissa by clearing the top 12 bits (sign + exponent).
729*b5893f02SDimitry Andric uint64_t mantissa = (I & (~0ULL >> 12)) | 1ULL << 52;
730f22ef01cSRoman Divacky
731f22ef01cSRoman Divacky // If the exponent doesn't shift all bits out of the mantissa
732f22ef01cSRoman Divacky if (exp < 52)
733f22ef01cSRoman Divacky return isNeg ? -APInt(width, mantissa >> (52 - exp)) :
734f22ef01cSRoman Divacky APInt(width, mantissa >> (52 - exp));
735f22ef01cSRoman Divacky
736f22ef01cSRoman Divacky // If the client didn't provide enough bits for us to shift the mantissa into
737f22ef01cSRoman Divacky // then the result is undefined, just return 0
738f22ef01cSRoman Divacky if (width <= exp - 52)
739f22ef01cSRoman Divacky return APInt(width, 0);
740f22ef01cSRoman Divacky
741f22ef01cSRoman Divacky // Otherwise, we have to shift the mantissa bits up to the right location
742f22ef01cSRoman Divacky APInt Tmp(width, mantissa);
743f37b6182SDimitry Andric Tmp <<= (unsigned)exp - 52;
744f22ef01cSRoman Divacky return isNeg ? -Tmp : Tmp;
745f22ef01cSRoman Divacky }
746f22ef01cSRoman Divacky
747ff0cc061SDimitry Andric /// This function converts this APInt to a double.
748f22ef01cSRoman Divacky /// The layout for double is as following (IEEE Standard 754):
749f22ef01cSRoman Divacky /// --------------------------------------
750f22ef01cSRoman Divacky /// | Sign Exponent Fraction Bias |
751f22ef01cSRoman Divacky /// |-------------------------------------- |
752f22ef01cSRoman Divacky /// | 1[63] 11[62-52] 52[51-00] 1023 |
753f22ef01cSRoman Divacky /// --------------------------------------
roundToDouble(bool isSigned) const754f22ef01cSRoman Divacky double APInt::roundToDouble(bool isSigned) const {
755f22ef01cSRoman Divacky
756f22ef01cSRoman Divacky // Handle the simple case where the value is contained in one uint64_t.
757f22ef01cSRoman Divacky // It is wrong to optimize getWord(0) to VAL; there might be more than one word.
758f22ef01cSRoman Divacky if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
759f22ef01cSRoman Divacky if (isSigned) {
7603ca95b02SDimitry Andric int64_t sext = SignExtend64(getWord(0), BitWidth);
761f22ef01cSRoman Divacky return double(sext);
762f22ef01cSRoman Divacky } else
763f22ef01cSRoman Divacky return double(getWord(0));
764f22ef01cSRoman Divacky }
765f22ef01cSRoman Divacky
766f22ef01cSRoman Divacky // Determine if the value is negative.
767f22ef01cSRoman Divacky bool isNeg = isSigned ? (*this)[BitWidth-1] : false;
768f22ef01cSRoman Divacky
769f22ef01cSRoman Divacky // Construct the absolute value if we're negative.
770f22ef01cSRoman Divacky APInt Tmp(isNeg ? -(*this) : (*this));
771f22ef01cSRoman Divacky
772f22ef01cSRoman Divacky // Figure out how many bits we're using.
773f22ef01cSRoman Divacky unsigned n = Tmp.getActiveBits();
774f22ef01cSRoman Divacky
775f22ef01cSRoman Divacky // The exponent (without bias normalization) is just the number of bits
776f22ef01cSRoman Divacky // we are using. Note that the sign bit is gone since we constructed the
777f22ef01cSRoman Divacky // absolute value.
778f22ef01cSRoman Divacky uint64_t exp = n;
779f22ef01cSRoman Divacky
780f22ef01cSRoman Divacky // Return infinity for exponent overflow
781f22ef01cSRoman Divacky if (exp > 1023) {
782f22ef01cSRoman Divacky if (!isSigned || !isNeg)
783f22ef01cSRoman Divacky return std::numeric_limits<double>::infinity();
784f22ef01cSRoman Divacky else
785f22ef01cSRoman Divacky return -std::numeric_limits<double>::infinity();
786f22ef01cSRoman Divacky }
787f22ef01cSRoman Divacky exp += 1023; // Increment for 1023 bias
788f22ef01cSRoman Divacky
789f22ef01cSRoman Divacky // Number of bits in mantissa is 52. To obtain the mantissa value, we must
790f22ef01cSRoman Divacky // extract the high 52 bits from the correct words in pVal.
791f22ef01cSRoman Divacky uint64_t mantissa;
792f22ef01cSRoman Divacky unsigned hiWord = whichWord(n-1);
793f22ef01cSRoman Divacky if (hiWord == 0) {
794f37b6182SDimitry Andric mantissa = Tmp.U.pVal[0];
795f22ef01cSRoman Divacky if (n > 52)
796f22ef01cSRoman Divacky mantissa >>= n - 52; // shift down, we want the top 52 bits.
797f22ef01cSRoman Divacky } else {
798f22ef01cSRoman Divacky assert(hiWord > 0 && "huh?");
799f37b6182SDimitry Andric uint64_t hibits = Tmp.U.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD);
800f37b6182SDimitry Andric uint64_t lobits = Tmp.U.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD);
801f22ef01cSRoman Divacky mantissa = hibits | lobits;
802f22ef01cSRoman Divacky }
803f22ef01cSRoman Divacky
804f22ef01cSRoman Divacky // The leading bit of mantissa is implicit, so get rid of it.
805f22ef01cSRoman Divacky uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
806*b5893f02SDimitry Andric uint64_t I = sign | (exp << 52) | mantissa;
807*b5893f02SDimitry Andric return bit_cast<double>(I);
808f22ef01cSRoman Divacky }
809f22ef01cSRoman Divacky
810f22ef01cSRoman Divacky // Truncate to new width.
trunc(unsigned width) const8112754fe60SDimitry Andric APInt APInt::trunc(unsigned width) const {
812f22ef01cSRoman Divacky assert(width < BitWidth && "Invalid APInt Truncate request");
813f22ef01cSRoman Divacky assert(width && "Can't truncate to 0 bits");
8142754fe60SDimitry Andric
8152754fe60SDimitry Andric if (width <= APINT_BITS_PER_WORD)
8162754fe60SDimitry Andric return APInt(width, getRawData()[0]);
8172754fe60SDimitry Andric
8182754fe60SDimitry Andric APInt Result(getMemory(getNumWords(width)), width);
8192754fe60SDimitry Andric
8202754fe60SDimitry Andric // Copy full words.
8212754fe60SDimitry Andric unsigned i;
8222754fe60SDimitry Andric for (i = 0; i != width / APINT_BITS_PER_WORD; i++)
823f37b6182SDimitry Andric Result.U.pVal[i] = U.pVal[i];
8242754fe60SDimitry Andric
8252754fe60SDimitry Andric // Truncate and copy any partial word.
8262754fe60SDimitry Andric unsigned bits = (0 - width) % APINT_BITS_PER_WORD;
8272754fe60SDimitry Andric if (bits != 0)
828f37b6182SDimitry Andric Result.U.pVal[i] = U.pVal[i] << bits >> bits;
8292754fe60SDimitry Andric
8302754fe60SDimitry Andric return Result;
831f22ef01cSRoman Divacky }
832f22ef01cSRoman Divacky
833f22ef01cSRoman Divacky // Sign extend to a new width.
sext(unsigned Width) const83451690af2SDimitry Andric APInt APInt::sext(unsigned Width) const {
83551690af2SDimitry Andric assert(Width > BitWidth && "Invalid APInt SignExtend request");
8362754fe60SDimitry Andric
83751690af2SDimitry Andric if (Width <= APINT_BITS_PER_WORD)
838f37b6182SDimitry Andric return APInt(Width, SignExtend64(U.VAL, BitWidth));
839f22ef01cSRoman Divacky
84051690af2SDimitry Andric APInt Result(getMemory(getNumWords(Width)), Width);
841f22ef01cSRoman Divacky
84251690af2SDimitry Andric // Copy words.
843f37b6182SDimitry Andric std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE);
8442754fe60SDimitry Andric
84551690af2SDimitry Andric // Sign extend the last word since there may be unused bits in the input.
846f37b6182SDimitry Andric Result.U.pVal[getNumWords() - 1] =
847f37b6182SDimitry Andric SignExtend64(Result.U.pVal[getNumWords() - 1],
84851690af2SDimitry Andric ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1);
8492754fe60SDimitry Andric
85051690af2SDimitry Andric // Fill with sign bits.
851f37b6182SDimitry Andric std::memset(Result.U.pVal + getNumWords(), isNegative() ? -1 : 0,
85251690af2SDimitry Andric (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE);
85351690af2SDimitry Andric Result.clearUnusedBits();
8542754fe60SDimitry Andric return Result;
855f22ef01cSRoman Divacky }
856f22ef01cSRoman Divacky
857f22ef01cSRoman Divacky // Zero extend to a new width.
zext(unsigned width) const8582754fe60SDimitry Andric APInt APInt::zext(unsigned width) const {
859f22ef01cSRoman Divacky assert(width > BitWidth && "Invalid APInt ZeroExtend request");
8602754fe60SDimitry Andric
8612754fe60SDimitry Andric if (width <= APINT_BITS_PER_WORD)
862f37b6182SDimitry Andric return APInt(width, U.VAL);
8632754fe60SDimitry Andric
8642754fe60SDimitry Andric APInt Result(getMemory(getNumWords(width)), width);
8652754fe60SDimitry Andric
8662754fe60SDimitry Andric // Copy words.
867f37b6182SDimitry Andric std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE);
8682754fe60SDimitry Andric
8692754fe60SDimitry Andric // Zero remaining words.
870f37b6182SDimitry Andric std::memset(Result.U.pVal + getNumWords(), 0,
87151690af2SDimitry Andric (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE);
8722754fe60SDimitry Andric
8732754fe60SDimitry Andric return Result;
874f22ef01cSRoman Divacky }
875f22ef01cSRoman Divacky
zextOrTrunc(unsigned width) const8762754fe60SDimitry Andric APInt APInt::zextOrTrunc(unsigned width) const {
877f22ef01cSRoman Divacky if (BitWidth < width)
878f22ef01cSRoman Divacky return zext(width);
879f22ef01cSRoman Divacky if (BitWidth > width)
880f22ef01cSRoman Divacky return trunc(width);
881f22ef01cSRoman Divacky return *this;
882f22ef01cSRoman Divacky }
883f22ef01cSRoman Divacky
sextOrTrunc(unsigned width) const8842754fe60SDimitry Andric APInt APInt::sextOrTrunc(unsigned width) const {
885f22ef01cSRoman Divacky if (BitWidth < width)
886f22ef01cSRoman Divacky return sext(width);
887f22ef01cSRoman Divacky if (BitWidth > width)
888f22ef01cSRoman Divacky return trunc(width);
889f22ef01cSRoman Divacky return *this;
890f22ef01cSRoman Divacky }
891f22ef01cSRoman Divacky
zextOrSelf(unsigned width) const892dff0c46cSDimitry Andric APInt APInt::zextOrSelf(unsigned width) const {
893dff0c46cSDimitry Andric if (BitWidth < width)
894dff0c46cSDimitry Andric return zext(width);
895dff0c46cSDimitry Andric return *this;
896dff0c46cSDimitry Andric }
897dff0c46cSDimitry Andric
sextOrSelf(unsigned width) const898dff0c46cSDimitry Andric APInt APInt::sextOrSelf(unsigned width) const {
899dff0c46cSDimitry Andric if (BitWidth < width)
900dff0c46cSDimitry Andric return sext(width);
901dff0c46cSDimitry Andric return *this;
902dff0c46cSDimitry Andric }
903dff0c46cSDimitry Andric
904f22ef01cSRoman Divacky /// Arithmetic right-shift this APInt by shiftAmt.
9054ba319b5SDimitry Andric /// Arithmetic right-shift function.
ashrInPlace(const APInt & shiftAmt)90651690af2SDimitry Andric void APInt::ashrInPlace(const APInt &shiftAmt) {
90751690af2SDimitry Andric ashrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth));
908f22ef01cSRoman Divacky }
909f22ef01cSRoman Divacky
910f22ef01cSRoman Divacky /// Arithmetic right-shift this APInt by shiftAmt.
9114ba319b5SDimitry Andric /// Arithmetic right-shift function.
ashrSlowCase(unsigned ShiftAmt)91251690af2SDimitry Andric void APInt::ashrSlowCase(unsigned ShiftAmt) {
91351690af2SDimitry Andric // Don't bother performing a no-op shift.
91451690af2SDimitry Andric if (!ShiftAmt)
91551690af2SDimitry Andric return;
916f22ef01cSRoman Divacky
91751690af2SDimitry Andric // Save the original sign bit for later.
91851690af2SDimitry Andric bool Negative = isNegative();
919f22ef01cSRoman Divacky
9204ba319b5SDimitry Andric // WordShift is the inter-part shift; BitShift is intra-part shift.
92151690af2SDimitry Andric unsigned WordShift = ShiftAmt / APINT_BITS_PER_WORD;
92251690af2SDimitry Andric unsigned BitShift = ShiftAmt % APINT_BITS_PER_WORD;
923f22ef01cSRoman Divacky
92451690af2SDimitry Andric unsigned WordsToMove = getNumWords() - WordShift;
92551690af2SDimitry Andric if (WordsToMove != 0) {
92651690af2SDimitry Andric // Sign extend the last word to fill in the unused bits.
927f37b6182SDimitry Andric U.pVal[getNumWords() - 1] = SignExtend64(
928f37b6182SDimitry Andric U.pVal[getNumWords() - 1], ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1);
929f22ef01cSRoman Divacky
93051690af2SDimitry Andric // Fastpath for moving by whole words.
93151690af2SDimitry Andric if (BitShift == 0) {
932f37b6182SDimitry Andric std::memmove(U.pVal, U.pVal + WordShift, WordsToMove * APINT_WORD_SIZE);
933f22ef01cSRoman Divacky } else {
93451690af2SDimitry Andric // Move the words containing significant bits.
93551690af2SDimitry Andric for (unsigned i = 0; i != WordsToMove - 1; ++i)
936f37b6182SDimitry Andric U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
937f37b6182SDimitry Andric (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift));
938f22ef01cSRoman Divacky
93951690af2SDimitry Andric // Handle the last word which has no high bits to copy.
940f37b6182SDimitry Andric U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift;
94151690af2SDimitry Andric // Sign extend one more time.
942f37b6182SDimitry Andric U.pVal[WordsToMove - 1] =
943f37b6182SDimitry Andric SignExtend64(U.pVal[WordsToMove - 1], APINT_BITS_PER_WORD - BitShift);
944f22ef01cSRoman Divacky }
945f22ef01cSRoman Divacky }
946f22ef01cSRoman Divacky
94751690af2SDimitry Andric // Fill in the remainder based on the original sign.
948f37b6182SDimitry Andric std::memset(U.pVal + WordsToMove, Negative ? -1 : 0,
94951690af2SDimitry Andric WordShift * APINT_WORD_SIZE);
95051690af2SDimitry Andric clearUnusedBits();
951f22ef01cSRoman Divacky }
952f22ef01cSRoman Divacky
953f22ef01cSRoman Divacky /// Logical right-shift this APInt by shiftAmt.
9544ba319b5SDimitry Andric /// Logical right-shift function.
lshrInPlace(const APInt & shiftAmt)9556bc11b14SDimitry Andric void APInt::lshrInPlace(const APInt &shiftAmt) {
9566bc11b14SDimitry Andric lshrInPlace((unsigned)shiftAmt.getLimitedValue(BitWidth));
9577a7e6055SDimitry Andric }
9587a7e6055SDimitry Andric
959f22ef01cSRoman Divacky /// Logical right-shift this APInt by shiftAmt.
9604ba319b5SDimitry Andric /// Logical right-shift function.
lshrSlowCase(unsigned ShiftAmt)9616bc11b14SDimitry Andric void APInt::lshrSlowCase(unsigned ShiftAmt) {
962f37b6182SDimitry Andric tcShiftRight(U.pVal, getNumWords(), ShiftAmt);
963f22ef01cSRoman Divacky }
964f22ef01cSRoman Divacky
965f22ef01cSRoman Divacky /// Left-shift this APInt by shiftAmt.
9664ba319b5SDimitry Andric /// Left-shift function.
operator <<=(const APInt & shiftAmt)967f37b6182SDimitry Andric APInt &APInt::operator<<=(const APInt &shiftAmt) {
968f22ef01cSRoman Divacky // It's undefined behavior in C to shift by BitWidth or greater.
969f37b6182SDimitry Andric *this <<= (unsigned)shiftAmt.getLimitedValue(BitWidth);
970f37b6182SDimitry Andric return *this;
971f22ef01cSRoman Divacky }
972f22ef01cSRoman Divacky
shlSlowCase(unsigned ShiftAmt)9736bc11b14SDimitry Andric void APInt::shlSlowCase(unsigned ShiftAmt) {
974f37b6182SDimitry Andric tcShiftLeft(U.pVal, getNumWords(), ShiftAmt);
9756bc11b14SDimitry Andric clearUnusedBits();
976f22ef01cSRoman Divacky }
977f22ef01cSRoman Divacky
9787a7e6055SDimitry Andric // Calculate the rotate amount modulo the bit width.
rotateModulo(unsigned BitWidth,const APInt & rotateAmt)9797a7e6055SDimitry Andric static unsigned rotateModulo(unsigned BitWidth, const APInt &rotateAmt) {
9807a7e6055SDimitry Andric unsigned rotBitWidth = rotateAmt.getBitWidth();
9817a7e6055SDimitry Andric APInt rot = rotateAmt;
9827a7e6055SDimitry Andric if (rotBitWidth < BitWidth) {
9837a7e6055SDimitry Andric // Extend the rotate APInt, so that the urem doesn't divide by 0.
9847a7e6055SDimitry Andric // e.g. APInt(1, 32) would give APInt(1, 0).
9857a7e6055SDimitry Andric rot = rotateAmt.zext(BitWidth);
9867a7e6055SDimitry Andric }
9877a7e6055SDimitry Andric rot = rot.urem(APInt(rot.getBitWidth(), BitWidth));
9887a7e6055SDimitry Andric return rot.getLimitedValue(BitWidth);
9897a7e6055SDimitry Andric }
9907a7e6055SDimitry Andric
rotl(const APInt & rotateAmt) const991f22ef01cSRoman Divacky APInt APInt::rotl(const APInt &rotateAmt) const {
9927a7e6055SDimitry Andric return rotl(rotateModulo(BitWidth, rotateAmt));
993f22ef01cSRoman Divacky }
994f22ef01cSRoman Divacky
rotl(unsigned rotateAmt) const995f22ef01cSRoman Divacky APInt APInt::rotl(unsigned rotateAmt) const {
996dff0c46cSDimitry Andric rotateAmt %= BitWidth;
997f22ef01cSRoman Divacky if (rotateAmt == 0)
998f22ef01cSRoman Divacky return *this;
999dff0c46cSDimitry Andric return shl(rotateAmt) | lshr(BitWidth - rotateAmt);
1000f22ef01cSRoman Divacky }
1001f22ef01cSRoman Divacky
rotr(const APInt & rotateAmt) const1002f22ef01cSRoman Divacky APInt APInt::rotr(const APInt &rotateAmt) const {
10037a7e6055SDimitry Andric return rotr(rotateModulo(BitWidth, rotateAmt));
1004f22ef01cSRoman Divacky }
1005f22ef01cSRoman Divacky
rotr(unsigned rotateAmt) const1006f22ef01cSRoman Divacky APInt APInt::rotr(unsigned rotateAmt) const {
1007dff0c46cSDimitry Andric rotateAmt %= BitWidth;
1008f22ef01cSRoman Divacky if (rotateAmt == 0)
1009f22ef01cSRoman Divacky return *this;
1010dff0c46cSDimitry Andric return lshr(rotateAmt) | shl(BitWidth - rotateAmt);
1011f22ef01cSRoman Divacky }
1012f22ef01cSRoman Divacky
1013f22ef01cSRoman Divacky // Square Root - this method computes and returns the square root of "this".
1014f22ef01cSRoman Divacky // Three mechanisms are used for computation. For small values (<= 5 bits),
1015f22ef01cSRoman Divacky // a table lookup is done. This gets some performance for common cases. For
1016f22ef01cSRoman Divacky // values using less than 52 bits, the value is converted to double and then
1017f22ef01cSRoman Divacky // the libc sqrt function is called. The result is rounded and then converted
1018f22ef01cSRoman Divacky // back to a uint64_t which is then used to construct the result. Finally,
1019f22ef01cSRoman Divacky // the Babylonian method for computing square roots is used.
sqrt() const1020f22ef01cSRoman Divacky APInt APInt::sqrt() const {
1021f22ef01cSRoman Divacky
1022f22ef01cSRoman Divacky // Determine the magnitude of the value.
1023f22ef01cSRoman Divacky unsigned magnitude = getActiveBits();
1024f22ef01cSRoman Divacky
1025f22ef01cSRoman Divacky // Use a fast table for some small values. This also gets rid of some
1026f22ef01cSRoman Divacky // rounding errors in libc sqrt for small values.
1027f22ef01cSRoman Divacky if (magnitude <= 5) {
1028f22ef01cSRoman Divacky static const uint8_t results[32] = {
1029f22ef01cSRoman Divacky /* 0 */ 0,
1030f22ef01cSRoman Divacky /* 1- 2 */ 1, 1,
1031f22ef01cSRoman Divacky /* 3- 6 */ 2, 2, 2, 2,
1032f22ef01cSRoman Divacky /* 7-12 */ 3, 3, 3, 3, 3, 3,
1033f22ef01cSRoman Divacky /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4,
1034f22ef01cSRoman Divacky /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1035f22ef01cSRoman Divacky /* 31 */ 6
1036f22ef01cSRoman Divacky };
1037f37b6182SDimitry Andric return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]);
1038f22ef01cSRoman Divacky }
1039f22ef01cSRoman Divacky
1040f22ef01cSRoman Divacky // If the magnitude of the value fits in less than 52 bits (the precision of
1041f22ef01cSRoman Divacky // an IEEE double precision floating point value), then we can use the
1042f22ef01cSRoman Divacky // libc sqrt function which will probably use a hardware sqrt computation.
1043f22ef01cSRoman Divacky // This should be faster than the algorithm below.
1044f22ef01cSRoman Divacky if (magnitude < 52) {
1045f22ef01cSRoman Divacky return APInt(BitWidth,
1046f37b6182SDimitry Andric uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL
1047f37b6182SDimitry Andric : U.pVal[0])))));
1048f22ef01cSRoman Divacky }
1049f22ef01cSRoman Divacky
1050f22ef01cSRoman Divacky // Okay, all the short cuts are exhausted. We must compute it. The following
1051f22ef01cSRoman Divacky // is a classical Babylonian method for computing the square root. This code
105239d628a0SDimitry Andric // was adapted to APInt from a wikipedia article on such computations.
1053f22ef01cSRoman Divacky // See http://www.wikipedia.org/ and go to the page named
1054f22ef01cSRoman Divacky // Calculate_an_integer_square_root.
1055f22ef01cSRoman Divacky unsigned nbits = BitWidth, i = 4;
1056f22ef01cSRoman Divacky APInt testy(BitWidth, 16);
1057f22ef01cSRoman Divacky APInt x_old(BitWidth, 1);
1058f22ef01cSRoman Divacky APInt x_new(BitWidth, 0);
1059f22ef01cSRoman Divacky APInt two(BitWidth, 2);
1060f22ef01cSRoman Divacky
1061f22ef01cSRoman Divacky // Select a good starting value using binary logarithms.
1062f22ef01cSRoman Divacky for (;; i += 2, testy = testy.shl(2))
1063f22ef01cSRoman Divacky if (i >= nbits || this->ule(testy)) {
1064f22ef01cSRoman Divacky x_old = x_old.shl(i / 2);
1065f22ef01cSRoman Divacky break;
1066f22ef01cSRoman Divacky }
1067f22ef01cSRoman Divacky
1068f22ef01cSRoman Divacky // Use the Babylonian method to arrive at the integer square root:
1069f22ef01cSRoman Divacky for (;;) {
1070f22ef01cSRoman Divacky x_new = (this->udiv(x_old) + x_old).udiv(two);
1071f22ef01cSRoman Divacky if (x_old.ule(x_new))
1072f22ef01cSRoman Divacky break;
1073f22ef01cSRoman Divacky x_old = x_new;
1074f22ef01cSRoman Divacky }
1075f22ef01cSRoman Divacky
1076f22ef01cSRoman Divacky // Make sure we return the closest approximation
1077f22ef01cSRoman Divacky // NOTE: The rounding calculation below is correct. It will produce an
1078f22ef01cSRoman Divacky // off-by-one discrepancy with results from pari/gp. That discrepancy has been
1079f22ef01cSRoman Divacky // determined to be a rounding issue with pari/gp as it begins to use a
1080f22ef01cSRoman Divacky // floating point representation after 192 bits. There are no discrepancies
1081f22ef01cSRoman Divacky // between this algorithm and pari/gp for bit widths < 192 bits.
1082f22ef01cSRoman Divacky APInt square(x_old * x_old);
1083f22ef01cSRoman Divacky APInt nextSquare((x_old + 1) * (x_old +1));
1084f22ef01cSRoman Divacky if (this->ult(square))
1085f22ef01cSRoman Divacky return x_old;
1086dff0c46cSDimitry Andric assert(this->ule(nextSquare) && "Error in APInt::sqrt computation");
1087f22ef01cSRoman Divacky APInt midpoint((nextSquare - square).udiv(two));
1088f22ef01cSRoman Divacky APInt offset(*this - square);
1089f22ef01cSRoman Divacky if (offset.ult(midpoint))
1090f22ef01cSRoman Divacky return x_old;
1091f22ef01cSRoman Divacky return x_old + 1;
1092f22ef01cSRoman Divacky }
1093f22ef01cSRoman Divacky
1094f22ef01cSRoman Divacky /// Computes the multiplicative inverse of this APInt for a given modulo. The
1095f22ef01cSRoman Divacky /// iterative extended Euclidean algorithm is used to solve for this value,
1096f22ef01cSRoman Divacky /// however we simplify it to speed up calculating only the inverse, and take
1097f22ef01cSRoman Divacky /// advantage of div+rem calculations. We also use some tricks to avoid copying
1098f22ef01cSRoman Divacky /// (potentially large) APInts around.
multiplicativeInverse(const APInt & modulo) const1099f22ef01cSRoman Divacky APInt APInt::multiplicativeInverse(const APInt& modulo) const {
1100f22ef01cSRoman Divacky assert(ult(modulo) && "This APInt must be smaller than the modulo");
1101f22ef01cSRoman Divacky
1102f22ef01cSRoman Divacky // Using the properties listed at the following web page (accessed 06/21/08):
1103f22ef01cSRoman Divacky // http://www.numbertheory.org/php/euclid.html
1104f22ef01cSRoman Divacky // (especially the properties numbered 3, 4 and 9) it can be proved that
1105f22ef01cSRoman Divacky // BitWidth bits suffice for all the computations in the algorithm implemented
1106f22ef01cSRoman Divacky // below. More precisely, this number of bits suffice if the multiplicative
1107f22ef01cSRoman Divacky // inverse exists, but may not suffice for the general extended Euclidean
1108f22ef01cSRoman Divacky // algorithm.
1109f22ef01cSRoman Divacky
1110f22ef01cSRoman Divacky APInt r[2] = { modulo, *this };
1111f22ef01cSRoman Divacky APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) };
1112f22ef01cSRoman Divacky APInt q(BitWidth, 0);
1113f22ef01cSRoman Divacky
1114f22ef01cSRoman Divacky unsigned i;
1115f22ef01cSRoman Divacky for (i = 0; r[i^1] != 0; i ^= 1) {
1116f22ef01cSRoman Divacky // An overview of the math without the confusing bit-flipping:
1117f22ef01cSRoman Divacky // q = r[i-2] / r[i-1]
1118f22ef01cSRoman Divacky // r[i] = r[i-2] % r[i-1]
1119f22ef01cSRoman Divacky // t[i] = t[i-2] - t[i-1] * q
1120f22ef01cSRoman Divacky udivrem(r[i], r[i^1], q, r[i]);
1121f22ef01cSRoman Divacky t[i] -= t[i^1] * q;
1122f22ef01cSRoman Divacky }
1123f22ef01cSRoman Divacky
1124f22ef01cSRoman Divacky // If this APInt and the modulo are not coprime, there is no multiplicative
1125f22ef01cSRoman Divacky // inverse, so return 0. We check this by looking at the next-to-last
1126f22ef01cSRoman Divacky // remainder, which is the gcd(*this,modulo) as calculated by the Euclidean
1127f22ef01cSRoman Divacky // algorithm.
1128f22ef01cSRoman Divacky if (r[i] != 1)
1129f22ef01cSRoman Divacky return APInt(BitWidth, 0);
1130f22ef01cSRoman Divacky
1131f22ef01cSRoman Divacky // The next-to-last t is the multiplicative inverse. However, we are
11325517e702SDimitry Andric // interested in a positive inverse. Calculate a positive one from a negative
1133f22ef01cSRoman Divacky // one if necessary. A simple addition of the modulo suffices because
1134f22ef01cSRoman Divacky // abs(t[i]) is known to be less than *this/2 (see the link above).
11355517e702SDimitry Andric if (t[i].isNegative())
11365517e702SDimitry Andric t[i] += modulo;
11375517e702SDimitry Andric
11385517e702SDimitry Andric return std::move(t[i]);
1139f22ef01cSRoman Divacky }
1140f22ef01cSRoman Divacky
1141f22ef01cSRoman Divacky /// Calculate the magic numbers required to implement a signed integer division
1142f22ef01cSRoman Divacky /// by a constant as a sequence of multiplies, adds and shifts. Requires that
1143f22ef01cSRoman Divacky /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
1144f22ef01cSRoman Divacky /// Warren, Jr., chapter 10.
magic() const1145f22ef01cSRoman Divacky APInt::ms APInt::magic() const {
1146f22ef01cSRoman Divacky const APInt& d = *this;
1147f22ef01cSRoman Divacky unsigned p;
1148f22ef01cSRoman Divacky APInt ad, anc, delta, q1, r1, q2, r2, t;
1149f22ef01cSRoman Divacky APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1150f22ef01cSRoman Divacky struct ms mag;
1151f22ef01cSRoman Divacky
1152f22ef01cSRoman Divacky ad = d.abs();
1153f22ef01cSRoman Divacky t = signedMin + (d.lshr(d.getBitWidth() - 1));
1154f22ef01cSRoman Divacky anc = t - 1 - t.urem(ad); // absolute value of nc
1155f22ef01cSRoman Divacky p = d.getBitWidth() - 1; // initialize p
1156f22ef01cSRoman Divacky q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc)
1157f22ef01cSRoman Divacky r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc))
1158f22ef01cSRoman Divacky q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d)
1159f22ef01cSRoman Divacky r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d))
1160f22ef01cSRoman Divacky do {
1161f22ef01cSRoman Divacky p = p + 1;
1162f22ef01cSRoman Divacky q1 = q1<<1; // update q1 = 2p/abs(nc)
1163f22ef01cSRoman Divacky r1 = r1<<1; // update r1 = rem(2p/abs(nc))
1164f22ef01cSRoman Divacky if (r1.uge(anc)) { // must be unsigned comparison
1165f22ef01cSRoman Divacky q1 = q1 + 1;
1166f22ef01cSRoman Divacky r1 = r1 - anc;
1167f22ef01cSRoman Divacky }
1168f22ef01cSRoman Divacky q2 = q2<<1; // update q2 = 2p/abs(d)
1169f22ef01cSRoman Divacky r2 = r2<<1; // update r2 = rem(2p/abs(d))
1170f22ef01cSRoman Divacky if (r2.uge(ad)) { // must be unsigned comparison
1171f22ef01cSRoman Divacky q2 = q2 + 1;
1172f22ef01cSRoman Divacky r2 = r2 - ad;
1173f22ef01cSRoman Divacky }
1174f22ef01cSRoman Divacky delta = ad - r2;
1175dd6029ffSDimitry Andric } while (q1.ult(delta) || (q1 == delta && r1 == 0));
1176f22ef01cSRoman Divacky
1177f22ef01cSRoman Divacky mag.m = q2 + 1;
1178f22ef01cSRoman Divacky if (d.isNegative()) mag.m = -mag.m; // resulting magic number
1179f22ef01cSRoman Divacky mag.s = p - d.getBitWidth(); // resulting shift
1180f22ef01cSRoman Divacky return mag;
1181f22ef01cSRoman Divacky }
1182f22ef01cSRoman Divacky
1183f22ef01cSRoman Divacky /// Calculate the magic numbers required to implement an unsigned integer
1184f22ef01cSRoman Divacky /// division by a constant as a sequence of multiplies, adds and shifts.
1185f22ef01cSRoman Divacky /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
1186f22ef01cSRoman Divacky /// S. Warren, Jr., chapter 10.
11873b0f4066SDimitry Andric /// LeadingZeros can be used to simplify the calculation if the upper bits
11883b0f4066SDimitry Andric /// of the divided value are known zero.
magicu(unsigned LeadingZeros) const11893b0f4066SDimitry Andric APInt::mu APInt::magicu(unsigned LeadingZeros) const {
1190f22ef01cSRoman Divacky const APInt& d = *this;
1191f22ef01cSRoman Divacky unsigned p;
1192f22ef01cSRoman Divacky APInt nc, delta, q1, r1, q2, r2;
1193f22ef01cSRoman Divacky struct mu magu;
1194f22ef01cSRoman Divacky magu.a = 0; // initialize "add" indicator
11953b0f4066SDimitry Andric APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros);
1196f22ef01cSRoman Divacky APInt signedMin = APInt::getSignedMinValue(d.getBitWidth());
1197f22ef01cSRoman Divacky APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth());
1198f22ef01cSRoman Divacky
11997ae0e2c9SDimitry Andric nc = allOnes - (allOnes - d).urem(d);
1200f22ef01cSRoman Divacky p = d.getBitWidth() - 1; // initialize p
1201f22ef01cSRoman Divacky q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc
1202f22ef01cSRoman Divacky r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc)
1203f22ef01cSRoman Divacky q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d
1204f22ef01cSRoman Divacky r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d)
1205f22ef01cSRoman Divacky do {
1206f22ef01cSRoman Divacky p = p + 1;
1207f22ef01cSRoman Divacky if (r1.uge(nc - r1)) {
1208f22ef01cSRoman Divacky q1 = q1 + q1 + 1; // update q1
1209f22ef01cSRoman Divacky r1 = r1 + r1 - nc; // update r1
1210f22ef01cSRoman Divacky }
1211f22ef01cSRoman Divacky else {
1212f22ef01cSRoman Divacky q1 = q1+q1; // update q1
1213f22ef01cSRoman Divacky r1 = r1+r1; // update r1
1214f22ef01cSRoman Divacky }
1215f22ef01cSRoman Divacky if ((r2 + 1).uge(d - r2)) {
1216f22ef01cSRoman Divacky if (q2.uge(signedMax)) magu.a = 1;
1217f22ef01cSRoman Divacky q2 = q2+q2 + 1; // update q2
1218f22ef01cSRoman Divacky r2 = r2+r2 + 1 - d; // update r2
1219f22ef01cSRoman Divacky }
1220f22ef01cSRoman Divacky else {
1221f22ef01cSRoman Divacky if (q2.uge(signedMin)) magu.a = 1;
1222f22ef01cSRoman Divacky q2 = q2+q2; // update q2
1223f22ef01cSRoman Divacky r2 = r2+r2 + 1; // update r2
1224f22ef01cSRoman Divacky }
1225f22ef01cSRoman Divacky delta = d - 1 - r2;
1226f22ef01cSRoman Divacky } while (p < d.getBitWidth()*2 &&
1227f22ef01cSRoman Divacky (q1.ult(delta) || (q1 == delta && r1 == 0)));
1228f22ef01cSRoman Divacky magu.m = q2 + 1; // resulting magic number
1229f22ef01cSRoman Divacky magu.s = p - d.getBitWidth(); // resulting shift
1230f22ef01cSRoman Divacky return magu;
1231f22ef01cSRoman Divacky }
1232f22ef01cSRoman Divacky
1233f22ef01cSRoman Divacky /// Implementation of Knuth's Algorithm D (Division of nonnegative integers)
1234f22ef01cSRoman Divacky /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The
1235f22ef01cSRoman Divacky /// variables here have the same names as in the algorithm. Comments explain
1236f22ef01cSRoman Divacky /// the algorithm and any deviation from it.
KnuthDiv(uint32_t * u,uint32_t * v,uint32_t * q,uint32_t * r,unsigned m,unsigned n)12375517e702SDimitry Andric static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r,
1238f22ef01cSRoman Divacky unsigned m, unsigned n) {
1239f22ef01cSRoman Divacky assert(u && "Must provide dividend");
1240f22ef01cSRoman Divacky assert(v && "Must provide divisor");
1241f22ef01cSRoman Divacky assert(q && "Must provide quotient");
1242ff0cc061SDimitry Andric assert(u != v && u != q && v != q && "Must use different memory");
1243f22ef01cSRoman Divacky assert(n>1 && "n must be > 1");
1244f22ef01cSRoman Divacky
1245ff0cc061SDimitry Andric // b denotes the base of the number system. In our case b is 2^32.
1246d88c1a5aSDimitry Andric const uint64_t b = uint64_t(1) << 32;
1247f22ef01cSRoman Divacky
12482cab237bSDimitry Andric // The DEBUG macros here tend to be spam in the debug output if you're not
12492cab237bSDimitry Andric // debugging this code. Disable them unless KNUTH_DEBUG is defined.
1250*b5893f02SDimitry Andric #ifdef KNUTH_DEBUG
1251*b5893f02SDimitry Andric #define DEBUG_KNUTH(X) LLVM_DEBUG(X)
1252*b5893f02SDimitry Andric #else
1253*b5893f02SDimitry Andric #define DEBUG_KNUTH(X) do {} while(false)
12542cab237bSDimitry Andric #endif
12552cab237bSDimitry Andric
1256*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n');
1257*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: original:");
1258*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]);
1259*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << " by");
1260*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]);
1261*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1262f22ef01cSRoman Divacky // D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
1263f22ef01cSRoman Divacky // u and v by d. Note that we have taken Knuth's advice here to use a power
1264f22ef01cSRoman Divacky // of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
1265f22ef01cSRoman Divacky // 2 allows us to shift instead of multiply and it is easy to determine the
1266f22ef01cSRoman Divacky // shift amount from the leading zeros. We are basically normalizing the u
1267f22ef01cSRoman Divacky // and v so that its high bits are shifted to the top of v's range without
1268f22ef01cSRoman Divacky // overflow. Note that this can require an extra word in u so that u must
1269f22ef01cSRoman Divacky // be of length m+n+1.
1270f785676fSDimitry Andric unsigned shift = countLeadingZeros(v[n-1]);
12715517e702SDimitry Andric uint32_t v_carry = 0;
12725517e702SDimitry Andric uint32_t u_carry = 0;
1273f22ef01cSRoman Divacky if (shift) {
1274f22ef01cSRoman Divacky for (unsigned i = 0; i < m+n; ++i) {
12755517e702SDimitry Andric uint32_t u_tmp = u[i] >> (32 - shift);
1276f22ef01cSRoman Divacky u[i] = (u[i] << shift) | u_carry;
1277f22ef01cSRoman Divacky u_carry = u_tmp;
1278f22ef01cSRoman Divacky }
1279f22ef01cSRoman Divacky for (unsigned i = 0; i < n; ++i) {
12805517e702SDimitry Andric uint32_t v_tmp = v[i] >> (32 - shift);
1281f22ef01cSRoman Divacky v[i] = (v[i] << shift) | v_carry;
1282f22ef01cSRoman Divacky v_carry = v_tmp;
1283f22ef01cSRoman Divacky }
1284f22ef01cSRoman Divacky }
1285f22ef01cSRoman Divacky u[m+n] = u_carry;
1286ff0cc061SDimitry Andric
1287*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: normal:");
1288*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]);
1289*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << " by");
1290*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = n; i > 0; i--) dbgs() << " " << v[i - 1]);
1291*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1292f22ef01cSRoman Divacky
1293f22ef01cSRoman Divacky // D2. [Initialize j.] Set j to m. This is the loop counter over the places.
1294f22ef01cSRoman Divacky int j = m;
1295f22ef01cSRoman Divacky do {
1296*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: quotient digit #" << j << '\n');
1297f22ef01cSRoman Divacky // D3. [Calculate q'.].
1298f22ef01cSRoman Divacky // Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
1299f22ef01cSRoman Divacky // Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
1300f22ef01cSRoman Divacky // Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
13015517e702SDimitry Andric // qp by 1, increase rp by v[n-1], and repeat this test if rp < b. The test
1302f22ef01cSRoman Divacky // on v[n-2] determines at high speed most of the cases in which the trial
1303f22ef01cSRoman Divacky // value qp is one too large, and it eliminates all cases where qp is two
1304f22ef01cSRoman Divacky // too large.
13055517e702SDimitry Andric uint64_t dividend = Make_64(u[j+n], u[j+n-1]);
1306*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: dividend == " << dividend << '\n');
1307f22ef01cSRoman Divacky uint64_t qp = dividend / v[n-1];
1308f22ef01cSRoman Divacky uint64_t rp = dividend % v[n-1];
1309f22ef01cSRoman Divacky if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) {
1310f22ef01cSRoman Divacky qp--;
1311f22ef01cSRoman Divacky rp += v[n-1];
1312f22ef01cSRoman Divacky if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2]))
1313f22ef01cSRoman Divacky qp--;
1314f22ef01cSRoman Divacky }
1315*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n');
1316f22ef01cSRoman Divacky
1317f22ef01cSRoman Divacky // D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
1318f22ef01cSRoman Divacky // (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
1319f22ef01cSRoman Divacky // consists of a simple multiplication by a one-place number, combined with
1320f22ef01cSRoman Divacky // a subtraction.
1321f22ef01cSRoman Divacky // The digits (u[j+n]...u[j]) should be kept positive; if the result of
1322f22ef01cSRoman Divacky // this step is actually negative, (u[j+n]...u[j]) should be left as the
1323f22ef01cSRoman Divacky // true value plus b**(n+1), namely as the b's complement of
1324f22ef01cSRoman Divacky // the true value, and a "borrow" to the left should be remembered.
1325ff0cc061SDimitry Andric int64_t borrow = 0;
1326ff0cc061SDimitry Andric for (unsigned i = 0; i < n; ++i) {
1327ff0cc061SDimitry Andric uint64_t p = uint64_t(qp) * uint64_t(v[i]);
13285517e702SDimitry Andric int64_t subres = int64_t(u[j+i]) - borrow - Lo_32(p);
13295517e702SDimitry Andric u[j+i] = Lo_32(subres);
13305517e702SDimitry Andric borrow = Hi_32(p) - Hi_32(subres);
1331*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: u[j+i] = " << u[j + i]
1332ff0cc061SDimitry Andric << ", borrow = " << borrow << '\n');
1333f22ef01cSRoman Divacky }
1334ff0cc061SDimitry Andric bool isNeg = u[j+n] < borrow;
13355517e702SDimitry Andric u[j+n] -= Lo_32(borrow);
1336ff0cc061SDimitry Andric
1337*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: after subtraction:");
1338*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]);
1339*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1340f22ef01cSRoman Divacky
1341f22ef01cSRoman Divacky // D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
1342f22ef01cSRoman Divacky // negative, go to step D6; otherwise go on to step D7.
13435517e702SDimitry Andric q[j] = Lo_32(qp);
1344f22ef01cSRoman Divacky if (isNeg) {
1345f22ef01cSRoman Divacky // D6. [Add back]. The probability that this step is necessary is very
1346f22ef01cSRoman Divacky // small, on the order of only 2/b. Make sure that test data accounts for
1347f22ef01cSRoman Divacky // this possibility. Decrease q[j] by 1
1348f22ef01cSRoman Divacky q[j]--;
1349f22ef01cSRoman Divacky // and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
1350f22ef01cSRoman Divacky // A carry will occur to the left of u[j+n], and it should be ignored
1351f22ef01cSRoman Divacky // since it cancels with the borrow that occurred in D4.
1352f22ef01cSRoman Divacky bool carry = false;
1353f22ef01cSRoman Divacky for (unsigned i = 0; i < n; i++) {
13545517e702SDimitry Andric uint32_t limit = std::min(u[j+i],v[i]);
1355f22ef01cSRoman Divacky u[j+i] += v[i] + carry;
1356f22ef01cSRoman Divacky carry = u[j+i] < limit || (carry && u[j+i] == limit);
1357f22ef01cSRoman Divacky }
1358f22ef01cSRoman Divacky u[j+n] += carry;
1359f22ef01cSRoman Divacky }
1360*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: after correction:");
1361*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]);
1362*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "\nKnuthDiv: digit result = " << q[j] << '\n');
1363f22ef01cSRoman Divacky
1364f22ef01cSRoman Divacky // D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
1365f22ef01cSRoman Divacky } while (--j >= 0);
1366f22ef01cSRoman Divacky
1367*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: quotient:");
1368*b5893f02SDimitry Andric DEBUG_KNUTH(for (int i = m; i >= 0; i--) dbgs() << " " << q[i]);
1369*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1370f22ef01cSRoman Divacky
1371f22ef01cSRoman Divacky // D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
1372f22ef01cSRoman Divacky // remainder may be obtained by dividing u[...] by d. If r is non-null we
1373f22ef01cSRoman Divacky // compute the remainder (urem uses this).
1374f22ef01cSRoman Divacky if (r) {
1375f22ef01cSRoman Divacky // The value d is expressed by the "shift" value above since we avoided
1376f22ef01cSRoman Divacky // multiplication by d by using a shift left. So, all we have to do is
13777a7e6055SDimitry Andric // shift right here.
1378f22ef01cSRoman Divacky if (shift) {
13795517e702SDimitry Andric uint32_t carry = 0;
1380*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << "KnuthDiv: remainder:");
1381f22ef01cSRoman Divacky for (int i = n-1; i >= 0; i--) {
1382f22ef01cSRoman Divacky r[i] = (u[i] >> shift) | carry;
1383f22ef01cSRoman Divacky carry = u[i] << (32 - shift);
1384*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << " " << r[i]);
1385f22ef01cSRoman Divacky }
1386f22ef01cSRoman Divacky } else {
1387f22ef01cSRoman Divacky for (int i = n-1; i >= 0; i--) {
1388f22ef01cSRoman Divacky r[i] = u[i];
1389*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << " " << r[i]);
1390f22ef01cSRoman Divacky }
1391f22ef01cSRoman Divacky }
1392*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1393f22ef01cSRoman Divacky }
1394*b5893f02SDimitry Andric DEBUG_KNUTH(dbgs() << '\n');
1395f22ef01cSRoman Divacky }
1396f22ef01cSRoman Divacky
divide(const WordType * LHS,unsigned lhsWords,const WordType * RHS,unsigned rhsWords,WordType * Quotient,WordType * Remainder)1397d8866befSDimitry Andric void APInt::divide(const WordType *LHS, unsigned lhsWords, const WordType *RHS,
1398d8866befSDimitry Andric unsigned rhsWords, WordType *Quotient, WordType *Remainder) {
1399f22ef01cSRoman Divacky assert(lhsWords >= rhsWords && "Fractional result");
1400f22ef01cSRoman Divacky
1401f22ef01cSRoman Divacky // First, compose the values into an array of 32-bit words instead of
1402f22ef01cSRoman Divacky // 64-bit words. This is a necessity of both the "short division" algorithm
1403f22ef01cSRoman Divacky // and the Knuth "classical algorithm" which requires there to be native
1404f22ef01cSRoman Divacky // operations for +, -, and * on an m bit value with an m*2 bit result. We
1405f22ef01cSRoman Divacky // can't use 64-bit operands here because we don't have native results of
1406f22ef01cSRoman Divacky // 128-bits. Furthermore, casting the 64-bit values to 32-bit values won't
1407f22ef01cSRoman Divacky // work on large-endian machines.
1408f22ef01cSRoman Divacky unsigned n = rhsWords * 2;
1409f22ef01cSRoman Divacky unsigned m = (lhsWords * 2) - n;
1410f22ef01cSRoman Divacky
1411f22ef01cSRoman Divacky // Allocate space for the temporary values we need either on the stack, if
1412f22ef01cSRoman Divacky // it will fit, or on the heap if it won't.
14135517e702SDimitry Andric uint32_t SPACE[128];
14145517e702SDimitry Andric uint32_t *U = nullptr;
14155517e702SDimitry Andric uint32_t *V = nullptr;
14165517e702SDimitry Andric uint32_t *Q = nullptr;
14175517e702SDimitry Andric uint32_t *R = nullptr;
1418f22ef01cSRoman Divacky if ((Remainder?4:3)*n+2*m+1 <= 128) {
1419f22ef01cSRoman Divacky U = &SPACE[0];
1420f22ef01cSRoman Divacky V = &SPACE[m+n+1];
1421f22ef01cSRoman Divacky Q = &SPACE[(m+n+1) + n];
1422f22ef01cSRoman Divacky if (Remainder)
1423f22ef01cSRoman Divacky R = &SPACE[(m+n+1) + n + (m+n)];
1424f22ef01cSRoman Divacky } else {
14255517e702SDimitry Andric U = new uint32_t[m + n + 1];
14265517e702SDimitry Andric V = new uint32_t[n];
14275517e702SDimitry Andric Q = new uint32_t[m+n];
1428f22ef01cSRoman Divacky if (Remainder)
14295517e702SDimitry Andric R = new uint32_t[n];
1430f22ef01cSRoman Divacky }
1431f22ef01cSRoman Divacky
1432f22ef01cSRoman Divacky // Initialize the dividend
14335517e702SDimitry Andric memset(U, 0, (m+n+1)*sizeof(uint32_t));
1434f22ef01cSRoman Divacky for (unsigned i = 0; i < lhsWords; ++i) {
1435d8866befSDimitry Andric uint64_t tmp = LHS[i];
14365517e702SDimitry Andric U[i * 2] = Lo_32(tmp);
14375517e702SDimitry Andric U[i * 2 + 1] = Hi_32(tmp);
1438f22ef01cSRoman Divacky }
1439f22ef01cSRoman Divacky U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm.
1440f22ef01cSRoman Divacky
1441f22ef01cSRoman Divacky // Initialize the divisor
14425517e702SDimitry Andric memset(V, 0, (n)*sizeof(uint32_t));
1443f22ef01cSRoman Divacky for (unsigned i = 0; i < rhsWords; ++i) {
1444d8866befSDimitry Andric uint64_t tmp = RHS[i];
14455517e702SDimitry Andric V[i * 2] = Lo_32(tmp);
14465517e702SDimitry Andric V[i * 2 + 1] = Hi_32(tmp);
1447f22ef01cSRoman Divacky }
1448f22ef01cSRoman Divacky
1449f22ef01cSRoman Divacky // initialize the quotient and remainder
14505517e702SDimitry Andric memset(Q, 0, (m+n) * sizeof(uint32_t));
1451f22ef01cSRoman Divacky if (Remainder)
14525517e702SDimitry Andric memset(R, 0, n * sizeof(uint32_t));
1453f22ef01cSRoman Divacky
1454f22ef01cSRoman Divacky // Now, adjust m and n for the Knuth division. n is the number of words in
1455f22ef01cSRoman Divacky // the divisor. m is the number of words by which the dividend exceeds the
1456f22ef01cSRoman Divacky // divisor (i.e. m+n is the length of the dividend). These sizes must not
1457f22ef01cSRoman Divacky // contain any zero words or the Knuth algorithm fails.
1458f22ef01cSRoman Divacky for (unsigned i = n; i > 0 && V[i-1] == 0; i--) {
1459f22ef01cSRoman Divacky n--;
1460f22ef01cSRoman Divacky m++;
1461f22ef01cSRoman Divacky }
1462f22ef01cSRoman Divacky for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--)
1463f22ef01cSRoman Divacky m--;
1464f22ef01cSRoman Divacky
1465f22ef01cSRoman Divacky // If we're left with only a single word for the divisor, Knuth doesn't work
1466f22ef01cSRoman Divacky // so we implement the short division algorithm here. This is much simpler
1467f22ef01cSRoman Divacky // and faster because we are certain that we can divide a 64-bit quantity
1468f22ef01cSRoman Divacky // by a 32-bit quantity at hardware speed and short division is simply a
1469f22ef01cSRoman Divacky // series of such operations. This is just like doing short division but we
1470f22ef01cSRoman Divacky // are using base 2^32 instead of base 10.
1471f22ef01cSRoman Divacky assert(n != 0 && "Divide by zero?");
1472f22ef01cSRoman Divacky if (n == 1) {
14735517e702SDimitry Andric uint32_t divisor = V[0];
14745517e702SDimitry Andric uint32_t remainder = 0;
14755517e702SDimitry Andric for (int i = m; i >= 0; i--) {
14765517e702SDimitry Andric uint64_t partial_dividend = Make_64(remainder, U[i]);
1477f22ef01cSRoman Divacky if (partial_dividend == 0) {
1478f22ef01cSRoman Divacky Q[i] = 0;
1479f22ef01cSRoman Divacky remainder = 0;
1480f22ef01cSRoman Divacky } else if (partial_dividend < divisor) {
1481f22ef01cSRoman Divacky Q[i] = 0;
14825517e702SDimitry Andric remainder = Lo_32(partial_dividend);
1483f22ef01cSRoman Divacky } else if (partial_dividend == divisor) {
1484f22ef01cSRoman Divacky Q[i] = 1;
1485f22ef01cSRoman Divacky remainder = 0;
1486f22ef01cSRoman Divacky } else {
14875517e702SDimitry Andric Q[i] = Lo_32(partial_dividend / divisor);
14885517e702SDimitry Andric remainder = Lo_32(partial_dividend - (Q[i] * divisor));
1489f22ef01cSRoman Divacky }
1490f22ef01cSRoman Divacky }
1491f22ef01cSRoman Divacky if (R)
1492f22ef01cSRoman Divacky R[0] = remainder;
1493f22ef01cSRoman Divacky } else {
1494f22ef01cSRoman Divacky // Now we're ready to invoke the Knuth classical divide algorithm. In this
1495f22ef01cSRoman Divacky // case n > 1.
1496f22ef01cSRoman Divacky KnuthDiv(U, V, Q, R, m, n);
1497f22ef01cSRoman Divacky }
1498f22ef01cSRoman Divacky
1499f22ef01cSRoman Divacky // If the caller wants the quotient
1500f22ef01cSRoman Divacky if (Quotient) {
1501f22ef01cSRoman Divacky for (unsigned i = 0; i < lhsWords; ++i)
1502d8866befSDimitry Andric Quotient[i] = Make_64(Q[i*2+1], Q[i*2]);
1503f22ef01cSRoman Divacky }
1504f22ef01cSRoman Divacky
1505f22ef01cSRoman Divacky // If the caller wants the remainder
1506f22ef01cSRoman Divacky if (Remainder) {
1507f22ef01cSRoman Divacky for (unsigned i = 0; i < rhsWords; ++i)
1508d8866befSDimitry Andric Remainder[i] = Make_64(R[i*2+1], R[i*2]);
1509f22ef01cSRoman Divacky }
1510f22ef01cSRoman Divacky
1511f22ef01cSRoman Divacky // Clean up the memory we allocated.
1512f22ef01cSRoman Divacky if (U != &SPACE[0]) {
1513f22ef01cSRoman Divacky delete [] U;
1514f22ef01cSRoman Divacky delete [] V;
1515f22ef01cSRoman Divacky delete [] Q;
1516f22ef01cSRoman Divacky delete [] R;
1517f22ef01cSRoman Divacky }
1518f22ef01cSRoman Divacky }
1519f22ef01cSRoman Divacky
udiv(const APInt & RHS) const1520f22ef01cSRoman Divacky APInt APInt::udiv(const APInt &RHS) const {
1521f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1522f22ef01cSRoman Divacky
1523f22ef01cSRoman Divacky // First, deal with the easy case
1524f22ef01cSRoman Divacky if (isSingleWord()) {
1525f37b6182SDimitry Andric assert(RHS.U.VAL != 0 && "Divide by zero?");
1526f37b6182SDimitry Andric return APInt(BitWidth, U.VAL / RHS.U.VAL);
1527f22ef01cSRoman Divacky }
1528f22ef01cSRoman Divacky
1529f22ef01cSRoman Divacky // Get some facts about the LHS and RHS number of bits and words
15305517e702SDimitry Andric unsigned lhsWords = getNumWords(getActiveBits());
1531f22ef01cSRoman Divacky unsigned rhsBits = RHS.getActiveBits();
15325517e702SDimitry Andric unsigned rhsWords = getNumWords(rhsBits);
1533f22ef01cSRoman Divacky assert(rhsWords && "Divided by zero???");
1534f22ef01cSRoman Divacky
1535f22ef01cSRoman Divacky // Deal with some degenerate cases
1536f22ef01cSRoman Divacky if (!lhsWords)
1537f22ef01cSRoman Divacky // 0 / X ===> 0
1538f22ef01cSRoman Divacky return APInt(BitWidth, 0);
15395517e702SDimitry Andric if (rhsBits == 1)
15405517e702SDimitry Andric // X / 1 ===> X
15415517e702SDimitry Andric return *this;
15425517e702SDimitry Andric if (lhsWords < rhsWords || this->ult(RHS))
1543f22ef01cSRoman Divacky // X / Y ===> 0, iff X < Y
1544f22ef01cSRoman Divacky return APInt(BitWidth, 0);
15455517e702SDimitry Andric if (*this == RHS)
1546f22ef01cSRoman Divacky // X / X ===> 1
1547f22ef01cSRoman Divacky return APInt(BitWidth, 1);
15485517e702SDimitry Andric if (lhsWords == 1) // rhsWords is 1 if lhsWords is 1.
1549f22ef01cSRoman Divacky // All high words are zero, just use native divide
1550f37b6182SDimitry Andric return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]);
1551f22ef01cSRoman Divacky
1552f22ef01cSRoman Divacky // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1553d8866befSDimitry Andric APInt Quotient(BitWidth, 0); // to hold result.
1554d8866befSDimitry Andric divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, nullptr);
1555d8866befSDimitry Andric return Quotient;
1556d8866befSDimitry Andric }
1557d8866befSDimitry Andric
udiv(uint64_t RHS) const1558d8866befSDimitry Andric APInt APInt::udiv(uint64_t RHS) const {
1559d8866befSDimitry Andric assert(RHS != 0 && "Divide by zero?");
1560d8866befSDimitry Andric
1561d8866befSDimitry Andric // First, deal with the easy case
1562d8866befSDimitry Andric if (isSingleWord())
1563d8866befSDimitry Andric return APInt(BitWidth, U.VAL / RHS);
1564d8866befSDimitry Andric
1565d8866befSDimitry Andric // Get some facts about the LHS words.
1566d8866befSDimitry Andric unsigned lhsWords = getNumWords(getActiveBits());
1567d8866befSDimitry Andric
1568d8866befSDimitry Andric // Deal with some degenerate cases
1569d8866befSDimitry Andric if (!lhsWords)
1570d8866befSDimitry Andric // 0 / X ===> 0
1571d8866befSDimitry Andric return APInt(BitWidth, 0);
1572d8866befSDimitry Andric if (RHS == 1)
1573d8866befSDimitry Andric // X / 1 ===> X
1574d8866befSDimitry Andric return *this;
1575d8866befSDimitry Andric if (this->ult(RHS))
1576d8866befSDimitry Andric // X / Y ===> 0, iff X < Y
1577d8866befSDimitry Andric return APInt(BitWidth, 0);
1578d8866befSDimitry Andric if (*this == RHS)
1579d8866befSDimitry Andric // X / X ===> 1
1580d8866befSDimitry Andric return APInt(BitWidth, 1);
1581d8866befSDimitry Andric if (lhsWords == 1) // rhsWords is 1 if lhsWords is 1.
1582d8866befSDimitry Andric // All high words are zero, just use native divide
1583d8866befSDimitry Andric return APInt(BitWidth, this->U.pVal[0] / RHS);
1584d8866befSDimitry Andric
1585d8866befSDimitry Andric // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1586d8866befSDimitry Andric APInt Quotient(BitWidth, 0); // to hold result.
1587d8866befSDimitry Andric divide(U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, nullptr);
1588f22ef01cSRoman Divacky return Quotient;
1589f22ef01cSRoman Divacky }
1590f22ef01cSRoman Divacky
sdiv(const APInt & RHS) const1591139f7f9bSDimitry Andric APInt APInt::sdiv(const APInt &RHS) const {
1592139f7f9bSDimitry Andric if (isNegative()) {
1593139f7f9bSDimitry Andric if (RHS.isNegative())
1594139f7f9bSDimitry Andric return (-(*this)).udiv(-RHS);
1595139f7f9bSDimitry Andric return -((-(*this)).udiv(RHS));
1596139f7f9bSDimitry Andric }
1597139f7f9bSDimitry Andric if (RHS.isNegative())
1598139f7f9bSDimitry Andric return -(this->udiv(-RHS));
1599139f7f9bSDimitry Andric return this->udiv(RHS);
1600139f7f9bSDimitry Andric }
1601139f7f9bSDimitry Andric
sdiv(int64_t RHS) const1602d8866befSDimitry Andric APInt APInt::sdiv(int64_t RHS) const {
1603d8866befSDimitry Andric if (isNegative()) {
1604d8866befSDimitry Andric if (RHS < 0)
1605d8866befSDimitry Andric return (-(*this)).udiv(-RHS);
1606d8866befSDimitry Andric return -((-(*this)).udiv(RHS));
1607d8866befSDimitry Andric }
1608d8866befSDimitry Andric if (RHS < 0)
1609d8866befSDimitry Andric return -(this->udiv(-RHS));
1610d8866befSDimitry Andric return this->udiv(RHS);
1611d8866befSDimitry Andric }
1612d8866befSDimitry Andric
urem(const APInt & RHS) const1613f22ef01cSRoman Divacky APInt APInt::urem(const APInt &RHS) const {
1614f22ef01cSRoman Divacky assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1615f22ef01cSRoman Divacky if (isSingleWord()) {
1616f37b6182SDimitry Andric assert(RHS.U.VAL != 0 && "Remainder by zero?");
1617f37b6182SDimitry Andric return APInt(BitWidth, U.VAL % RHS.U.VAL);
1618f22ef01cSRoman Divacky }
1619f22ef01cSRoman Divacky
1620f22ef01cSRoman Divacky // Get some facts about the LHS
16215517e702SDimitry Andric unsigned lhsWords = getNumWords(getActiveBits());
1622f22ef01cSRoman Divacky
1623f22ef01cSRoman Divacky // Get some facts about the RHS
1624f22ef01cSRoman Divacky unsigned rhsBits = RHS.getActiveBits();
16255517e702SDimitry Andric unsigned rhsWords = getNumWords(rhsBits);
1626f22ef01cSRoman Divacky assert(rhsWords && "Performing remainder operation by zero ???");
1627f22ef01cSRoman Divacky
1628f22ef01cSRoman Divacky // Check the degenerate cases
16295517e702SDimitry Andric if (lhsWords == 0)
1630f22ef01cSRoman Divacky // 0 % Y ===> 0
1631f22ef01cSRoman Divacky return APInt(BitWidth, 0);
16325517e702SDimitry Andric if (rhsBits == 1)
16335517e702SDimitry Andric // X % 1 ===> 0
16345517e702SDimitry Andric return APInt(BitWidth, 0);
16355517e702SDimitry Andric if (lhsWords < rhsWords || this->ult(RHS))
1636f22ef01cSRoman Divacky // X % Y ===> X, iff X < Y
1637f22ef01cSRoman Divacky return *this;
16385517e702SDimitry Andric if (*this == RHS)
1639f22ef01cSRoman Divacky // X % X == 0;
1640f22ef01cSRoman Divacky return APInt(BitWidth, 0);
16415517e702SDimitry Andric if (lhsWords == 1)
1642f22ef01cSRoman Divacky // All high words are zero, just use native remainder
1643f37b6182SDimitry Andric return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]);
1644f22ef01cSRoman Divacky
1645f22ef01cSRoman Divacky // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1646d8866befSDimitry Andric APInt Remainder(BitWidth, 0);
1647d8866befSDimitry Andric divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, nullptr, Remainder.U.pVal);
1648d8866befSDimitry Andric return Remainder;
1649d8866befSDimitry Andric }
1650d8866befSDimitry Andric
urem(uint64_t RHS) const1651d8866befSDimitry Andric uint64_t APInt::urem(uint64_t RHS) const {
1652d8866befSDimitry Andric assert(RHS != 0 && "Remainder by zero?");
1653d8866befSDimitry Andric
1654d8866befSDimitry Andric if (isSingleWord())
1655d8866befSDimitry Andric return U.VAL % RHS;
1656d8866befSDimitry Andric
1657d8866befSDimitry Andric // Get some facts about the LHS
1658d8866befSDimitry Andric unsigned lhsWords = getNumWords(getActiveBits());
1659d8866befSDimitry Andric
1660d8866befSDimitry Andric // Check the degenerate cases
1661d8866befSDimitry Andric if (lhsWords == 0)
1662d8866befSDimitry Andric // 0 % Y ===> 0
1663d8866befSDimitry Andric return 0;
1664d8866befSDimitry Andric if (RHS == 1)
1665d8866befSDimitry Andric // X % 1 ===> 0
1666d8866befSDimitry Andric return 0;
1667d8866befSDimitry Andric if (this->ult(RHS))
1668d8866befSDimitry Andric // X % Y ===> X, iff X < Y
1669d8866befSDimitry Andric return getZExtValue();
1670d8866befSDimitry Andric if (*this == RHS)
1671d8866befSDimitry Andric // X % X == 0;
1672d8866befSDimitry Andric return 0;
1673d8866befSDimitry Andric if (lhsWords == 1)
1674d8866befSDimitry Andric // All high words are zero, just use native remainder
1675d8866befSDimitry Andric return U.pVal[0] % RHS;
1676d8866befSDimitry Andric
1677d8866befSDimitry Andric // We have to compute it the hard way. Invoke the Knuth divide algorithm.
1678d8866befSDimitry Andric uint64_t Remainder;
1679d8866befSDimitry Andric divide(U.pVal, lhsWords, &RHS, 1, nullptr, &Remainder);
1680f22ef01cSRoman Divacky return Remainder;
1681f22ef01cSRoman Divacky }
1682f22ef01cSRoman Divacky
srem(const APInt & RHS) const1683139f7f9bSDimitry Andric APInt APInt::srem(const APInt &RHS) const {
1684139f7f9bSDimitry Andric if (isNegative()) {
1685139f7f9bSDimitry Andric if (RHS.isNegative())
1686139f7f9bSDimitry Andric return -((-(*this)).urem(-RHS));
1687139f7f9bSDimitry Andric return -((-(*this)).urem(RHS));
1688139f7f9bSDimitry Andric }
1689139f7f9bSDimitry Andric if (RHS.isNegative())
1690139f7f9bSDimitry Andric return this->urem(-RHS);
1691139f7f9bSDimitry Andric return this->urem(RHS);
1692139f7f9bSDimitry Andric }
1693139f7f9bSDimitry Andric
srem(int64_t RHS) const1694d8866befSDimitry Andric int64_t APInt::srem(int64_t RHS) const {
1695d8866befSDimitry Andric if (isNegative()) {
1696d8866befSDimitry Andric if (RHS < 0)
1697d8866befSDimitry Andric return -((-(*this)).urem(-RHS));
1698d8866befSDimitry Andric return -((-(*this)).urem(RHS));
1699d8866befSDimitry Andric }
1700d8866befSDimitry Andric if (RHS < 0)
1701d8866befSDimitry Andric return this->urem(-RHS);
1702d8866befSDimitry Andric return this->urem(RHS);
1703d8866befSDimitry Andric }
1704d8866befSDimitry Andric
udivrem(const APInt & LHS,const APInt & RHS,APInt & Quotient,APInt & Remainder)1705f22ef01cSRoman Divacky void APInt::udivrem(const APInt &LHS, const APInt &RHS,
1706f22ef01cSRoman Divacky APInt &Quotient, APInt &Remainder) {
170739d628a0SDimitry Andric assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same");
17085517e702SDimitry Andric unsigned BitWidth = LHS.BitWidth;
170939d628a0SDimitry Andric
171039d628a0SDimitry Andric // First, deal with the easy case
171139d628a0SDimitry Andric if (LHS.isSingleWord()) {
1712f37b6182SDimitry Andric assert(RHS.U.VAL != 0 && "Divide by zero?");
1713f37b6182SDimitry Andric uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL;
1714f37b6182SDimitry Andric uint64_t RemVal = LHS.U.VAL % RHS.U.VAL;
17155517e702SDimitry Andric Quotient = APInt(BitWidth, QuotVal);
17165517e702SDimitry Andric Remainder = APInt(BitWidth, RemVal);
171739d628a0SDimitry Andric return;
171839d628a0SDimitry Andric }
171939d628a0SDimitry Andric
1720f22ef01cSRoman Divacky // Get some size facts about the dividend and divisor
17215517e702SDimitry Andric unsigned lhsWords = getNumWords(LHS.getActiveBits());
1722f22ef01cSRoman Divacky unsigned rhsBits = RHS.getActiveBits();
17235517e702SDimitry Andric unsigned rhsWords = getNumWords(rhsBits);
17245517e702SDimitry Andric assert(rhsWords && "Performing divrem operation by zero ???");
1725f22ef01cSRoman Divacky
1726f22ef01cSRoman Divacky // Check the degenerate cases
1727f22ef01cSRoman Divacky if (lhsWords == 0) {
17284ba319b5SDimitry Andric Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0
17294ba319b5SDimitry Andric Remainder = APInt(BitWidth, 0); // 0 % Y ===> 0
1730f22ef01cSRoman Divacky return;
1731f22ef01cSRoman Divacky }
1732f22ef01cSRoman Divacky
17335517e702SDimitry Andric if (rhsBits == 1) {
17345517e702SDimitry Andric Quotient = LHS; // X / 1 ===> X
17354ba319b5SDimitry Andric Remainder = APInt(BitWidth, 0); // X % 1 ===> 0
17365517e702SDimitry Andric }
17375517e702SDimitry Andric
1738f22ef01cSRoman Divacky if (lhsWords < rhsWords || LHS.ult(RHS)) {
1739f22ef01cSRoman Divacky Remainder = LHS; // X % Y ===> X, iff X < Y
17404ba319b5SDimitry Andric Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y
1741f22ef01cSRoman Divacky return;
1742f22ef01cSRoman Divacky }
1743f22ef01cSRoman Divacky
1744f22ef01cSRoman Divacky if (LHS == RHS) {
17454ba319b5SDimitry Andric Quotient = APInt(BitWidth, 1); // X / X ===> 1
17464ba319b5SDimitry Andric Remainder = APInt(BitWidth, 0); // X % X ===> 0;
1747f22ef01cSRoman Divacky return;
1748f22ef01cSRoman Divacky }
1749f22ef01cSRoman Divacky
1750d8866befSDimitry Andric // Make sure there is enough space to hold the results.
1751d8866befSDimitry Andric // NOTE: This assumes that reallocate won't affect any bits if it doesn't
1752d8866befSDimitry Andric // change the size. This is necessary if Quotient or Remainder is aliased
1753d8866befSDimitry Andric // with LHS or RHS.
1754d8866befSDimitry Andric Quotient.reallocate(BitWidth);
1755d8866befSDimitry Andric Remainder.reallocate(BitWidth);
1756d8866befSDimitry Andric
17575517e702SDimitry Andric if (lhsWords == 1) { // rhsWords is 1 if lhsWords is 1.
1758f22ef01cSRoman Divacky // There is only one word to consider so use the native versions.
17595517e702SDimitry Andric uint64_t lhsValue = LHS.U.pVal[0];
17605517e702SDimitry Andric uint64_t rhsValue = RHS.U.pVal[0];
17615517e702SDimitry Andric Quotient = lhsValue / rhsValue;
17625517e702SDimitry Andric Remainder = lhsValue % rhsValue;
1763f22ef01cSRoman Divacky return;
1764f22ef01cSRoman Divacky }
1765f22ef01cSRoman Divacky
1766f22ef01cSRoman Divacky // Okay, lets do it the long way
1767d8866befSDimitry Andric divide(LHS.U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal,
1768d8866befSDimitry Andric Remainder.U.pVal);
1769d8866befSDimitry Andric // Clear the rest of the Quotient and Remainder.
1770d8866befSDimitry Andric std::memset(Quotient.U.pVal + lhsWords, 0,
1771d8866befSDimitry Andric (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE);
1772d8866befSDimitry Andric std::memset(Remainder.U.pVal + rhsWords, 0,
1773d8866befSDimitry Andric (getNumWords(BitWidth) - rhsWords) * APINT_WORD_SIZE);
1774d8866befSDimitry Andric }
1775d8866befSDimitry Andric
udivrem(const APInt & LHS,uint64_t RHS,APInt & Quotient,uint64_t & Remainder)1776d8866befSDimitry Andric void APInt::udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient,
1777d8866befSDimitry Andric uint64_t &Remainder) {
1778d8866befSDimitry Andric assert(RHS != 0 && "Divide by zero?");
1779d8866befSDimitry Andric unsigned BitWidth = LHS.BitWidth;
1780d8866befSDimitry Andric
1781d8866befSDimitry Andric // First, deal with the easy case
1782d8866befSDimitry Andric if (LHS.isSingleWord()) {
1783d8866befSDimitry Andric uint64_t QuotVal = LHS.U.VAL / RHS;
1784d8866befSDimitry Andric Remainder = LHS.U.VAL % RHS;
1785d8866befSDimitry Andric Quotient = APInt(BitWidth, QuotVal);
1786d8866befSDimitry Andric return;
1787d8866befSDimitry Andric }
1788d8866befSDimitry Andric
1789d8866befSDimitry Andric // Get some size facts about the dividend and divisor
1790d8866befSDimitry Andric unsigned lhsWords = getNumWords(LHS.getActiveBits());
1791d8866befSDimitry Andric
1792d8866befSDimitry Andric // Check the degenerate cases
1793d8866befSDimitry Andric if (lhsWords == 0) {
17944ba319b5SDimitry Andric Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0
1795d8866befSDimitry Andric Remainder = 0; // 0 % Y ===> 0
1796d8866befSDimitry Andric return;
1797d8866befSDimitry Andric }
1798d8866befSDimitry Andric
1799d8866befSDimitry Andric if (RHS == 1) {
1800d8866befSDimitry Andric Quotient = LHS; // X / 1 ===> X
1801d8866befSDimitry Andric Remainder = 0; // X % 1 ===> 0
18024ba319b5SDimitry Andric return;
1803d8866befSDimitry Andric }
1804d8866befSDimitry Andric
1805d8866befSDimitry Andric if (LHS.ult(RHS)) {
1806d8866befSDimitry Andric Remainder = LHS.getZExtValue(); // X % Y ===> X, iff X < Y
18074ba319b5SDimitry Andric Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y
1808d8866befSDimitry Andric return;
1809d8866befSDimitry Andric }
1810d8866befSDimitry Andric
1811d8866befSDimitry Andric if (LHS == RHS) {
18124ba319b5SDimitry Andric Quotient = APInt(BitWidth, 1); // X / X ===> 1
1813d8866befSDimitry Andric Remainder = 0; // X % X ===> 0;
1814d8866befSDimitry Andric return;
1815d8866befSDimitry Andric }
1816d8866befSDimitry Andric
1817d8866befSDimitry Andric // Make sure there is enough space to hold the results.
1818d8866befSDimitry Andric // NOTE: This assumes that reallocate won't affect any bits if it doesn't
1819d8866befSDimitry Andric // change the size. This is necessary if Quotient is aliased with LHS.
1820d8866befSDimitry Andric Quotient.reallocate(BitWidth);
1821d8866befSDimitry Andric
1822d8866befSDimitry Andric if (lhsWords == 1) { // rhsWords is 1 if lhsWords is 1.
1823d8866befSDimitry Andric // There is only one word to consider so use the native versions.
1824d8866befSDimitry Andric uint64_t lhsValue = LHS.U.pVal[0];
1825d8866befSDimitry Andric Quotient = lhsValue / RHS;
1826d8866befSDimitry Andric Remainder = lhsValue % RHS;
1827d8866befSDimitry Andric return;
1828d8866befSDimitry Andric }
1829d8866befSDimitry Andric
1830d8866befSDimitry Andric // Okay, lets do it the long way
1831d8866befSDimitry Andric divide(LHS.U.pVal, lhsWords, &RHS, 1, Quotient.U.pVal, &Remainder);
1832d8866befSDimitry Andric // Clear the rest of the Quotient.
1833d8866befSDimitry Andric std::memset(Quotient.U.pVal + lhsWords, 0,
1834d8866befSDimitry Andric (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE);
1835f22ef01cSRoman Divacky }
1836f22ef01cSRoman Divacky
sdivrem(const APInt & LHS,const APInt & RHS,APInt & Quotient,APInt & Remainder)1837139f7f9bSDimitry Andric void APInt::sdivrem(const APInt &LHS, const APInt &RHS,
1838139f7f9bSDimitry Andric APInt &Quotient, APInt &Remainder) {
1839139f7f9bSDimitry Andric if (LHS.isNegative()) {
1840139f7f9bSDimitry Andric if (RHS.isNegative())
1841139f7f9bSDimitry Andric APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
1842139f7f9bSDimitry Andric else {
1843139f7f9bSDimitry Andric APInt::udivrem(-LHS, RHS, Quotient, Remainder);
18445517e702SDimitry Andric Quotient.negate();
1845139f7f9bSDimitry Andric }
18465517e702SDimitry Andric Remainder.negate();
1847139f7f9bSDimitry Andric } else if (RHS.isNegative()) {
1848139f7f9bSDimitry Andric APInt::udivrem(LHS, -RHS, Quotient, Remainder);
18495517e702SDimitry Andric Quotient.negate();
1850139f7f9bSDimitry Andric } else {
1851139f7f9bSDimitry Andric APInt::udivrem(LHS, RHS, Quotient, Remainder);
1852139f7f9bSDimitry Andric }
1853139f7f9bSDimitry Andric }
1854139f7f9bSDimitry Andric
sdivrem(const APInt & LHS,int64_t RHS,APInt & Quotient,int64_t & Remainder)1855d8866befSDimitry Andric void APInt::sdivrem(const APInt &LHS, int64_t RHS,
1856d8866befSDimitry Andric APInt &Quotient, int64_t &Remainder) {
1857d8866befSDimitry Andric uint64_t R = Remainder;
1858d8866befSDimitry Andric if (LHS.isNegative()) {
1859d8866befSDimitry Andric if (RHS < 0)
1860d8866befSDimitry Andric APInt::udivrem(-LHS, -RHS, Quotient, R);
1861d8866befSDimitry Andric else {
1862d8866befSDimitry Andric APInt::udivrem(-LHS, RHS, Quotient, R);
1863d8866befSDimitry Andric Quotient.negate();
1864d8866befSDimitry Andric }
1865d8866befSDimitry Andric R = -R;
1866d8866befSDimitry Andric } else if (RHS < 0) {
1867d8866befSDimitry Andric APInt::udivrem(LHS, -RHS, Quotient, R);
1868d8866befSDimitry Andric Quotient.negate();
1869d8866befSDimitry Andric } else {
1870d8866befSDimitry Andric APInt::udivrem(LHS, RHS, Quotient, R);
1871d8866befSDimitry Andric }
1872d8866befSDimitry Andric Remainder = R;
1873d8866befSDimitry Andric }
1874d8866befSDimitry Andric
sadd_ov(const APInt & RHS,bool & Overflow) const18752754fe60SDimitry Andric APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const {
18762754fe60SDimitry Andric APInt Res = *this+RHS;
18772754fe60SDimitry Andric Overflow = isNonNegative() == RHS.isNonNegative() &&
18782754fe60SDimitry Andric Res.isNonNegative() != isNonNegative();
18792754fe60SDimitry Andric return Res;
18802754fe60SDimitry Andric }
18812754fe60SDimitry Andric
uadd_ov(const APInt & RHS,bool & Overflow) const18822754fe60SDimitry Andric APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const {
18832754fe60SDimitry Andric APInt Res = *this+RHS;
18842754fe60SDimitry Andric Overflow = Res.ult(RHS);
18852754fe60SDimitry Andric return Res;
18862754fe60SDimitry Andric }
18872754fe60SDimitry Andric
ssub_ov(const APInt & RHS,bool & Overflow) const18882754fe60SDimitry Andric APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) const {
18892754fe60SDimitry Andric APInt Res = *this - RHS;
18902754fe60SDimitry Andric Overflow = isNonNegative() != RHS.isNonNegative() &&
18912754fe60SDimitry Andric Res.isNonNegative() != isNonNegative();
18922754fe60SDimitry Andric return Res;
18932754fe60SDimitry Andric }
18942754fe60SDimitry Andric
usub_ov(const APInt & RHS,bool & Overflow) const18952754fe60SDimitry Andric APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const {
18962754fe60SDimitry Andric APInt Res = *this-RHS;
18972754fe60SDimitry Andric Overflow = Res.ugt(*this);
18982754fe60SDimitry Andric return Res;
18992754fe60SDimitry Andric }
19002754fe60SDimitry Andric
sdiv_ov(const APInt & RHS,bool & Overflow) const19012754fe60SDimitry Andric APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) const {
19022754fe60SDimitry Andric // MININT/-1 --> overflow.
19032754fe60SDimitry Andric Overflow = isMinSignedValue() && RHS.isAllOnesValue();
19042754fe60SDimitry Andric return sdiv(RHS);
19052754fe60SDimitry Andric }
19062754fe60SDimitry Andric
smul_ov(const APInt & RHS,bool & Overflow) const19072754fe60SDimitry Andric APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const {
19082754fe60SDimitry Andric APInt Res = *this * RHS;
19092754fe60SDimitry Andric
19102754fe60SDimitry Andric if (*this != 0 && RHS != 0)
19112754fe60SDimitry Andric Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS;
19122754fe60SDimitry Andric else
19132754fe60SDimitry Andric Overflow = false;
19142754fe60SDimitry Andric return Res;
19152754fe60SDimitry Andric }
19162754fe60SDimitry Andric
umul_ov(const APInt & RHS,bool & Overflow) const19173b0f4066SDimitry Andric APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
19183b0f4066SDimitry Andric APInt Res = *this * RHS;
19193b0f4066SDimitry Andric
19203b0f4066SDimitry Andric if (*this != 0 && RHS != 0)
19213b0f4066SDimitry Andric Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS;
19223b0f4066SDimitry Andric else
19233b0f4066SDimitry Andric Overflow = false;
19243b0f4066SDimitry Andric return Res;
19253b0f4066SDimitry Andric }
19263b0f4066SDimitry Andric
sshl_ov(const APInt & ShAmt,bool & Overflow) const192739d628a0SDimitry Andric APInt APInt::sshl_ov(const APInt &ShAmt, bool &Overflow) const {
192839d628a0SDimitry Andric Overflow = ShAmt.uge(getBitWidth());
19292754fe60SDimitry Andric if (Overflow)
193039d628a0SDimitry Andric return APInt(BitWidth, 0);
19312754fe60SDimitry Andric
19322754fe60SDimitry Andric if (isNonNegative()) // Don't allow sign change.
193339d628a0SDimitry Andric Overflow = ShAmt.uge(countLeadingZeros());
19342754fe60SDimitry Andric else
193539d628a0SDimitry Andric Overflow = ShAmt.uge(countLeadingOnes());
193639d628a0SDimitry Andric
193739d628a0SDimitry Andric return *this << ShAmt;
193839d628a0SDimitry Andric }
193939d628a0SDimitry Andric
ushl_ov(const APInt & ShAmt,bool & Overflow) const194039d628a0SDimitry Andric APInt APInt::ushl_ov(const APInt &ShAmt, bool &Overflow) const {
194139d628a0SDimitry Andric Overflow = ShAmt.uge(getBitWidth());
194239d628a0SDimitry Andric if (Overflow)
194339d628a0SDimitry Andric return APInt(BitWidth, 0);
194439d628a0SDimitry Andric
194539d628a0SDimitry Andric Overflow = ShAmt.ugt(countLeadingZeros());
19462754fe60SDimitry Andric
19472754fe60SDimitry Andric return *this << ShAmt;
19482754fe60SDimitry Andric }
19492754fe60SDimitry Andric
sadd_sat(const APInt & RHS) const1950*b5893f02SDimitry Andric APInt APInt::sadd_sat(const APInt &RHS) const {
1951*b5893f02SDimitry Andric bool Overflow;
1952*b5893f02SDimitry Andric APInt Res = sadd_ov(RHS, Overflow);
1953*b5893f02SDimitry Andric if (!Overflow)
1954*b5893f02SDimitry Andric return Res;
19552754fe60SDimitry Andric
1956*b5893f02SDimitry Andric return isNegative() ? APInt::getSignedMinValue(BitWidth)
1957*b5893f02SDimitry Andric : APInt::getSignedMaxValue(BitWidth);
1958*b5893f02SDimitry Andric }
1959*b5893f02SDimitry Andric
uadd_sat(const APInt & RHS) const1960*b5893f02SDimitry Andric APInt APInt::uadd_sat(const APInt &RHS) const {
1961*b5893f02SDimitry Andric bool Overflow;
1962*b5893f02SDimitry Andric APInt Res = uadd_ov(RHS, Overflow);
1963*b5893f02SDimitry Andric if (!Overflow)
1964*b5893f02SDimitry Andric return Res;
1965*b5893f02SDimitry Andric
1966*b5893f02SDimitry Andric return APInt::getMaxValue(BitWidth);
1967*b5893f02SDimitry Andric }
1968*b5893f02SDimitry Andric
ssub_sat(const APInt & RHS) const1969*b5893f02SDimitry Andric APInt APInt::ssub_sat(const APInt &RHS) const {
1970*b5893f02SDimitry Andric bool Overflow;
1971*b5893f02SDimitry Andric APInt Res = ssub_ov(RHS, Overflow);
1972*b5893f02SDimitry Andric if (!Overflow)
1973*b5893f02SDimitry Andric return Res;
1974*b5893f02SDimitry Andric
1975*b5893f02SDimitry Andric return isNegative() ? APInt::getSignedMinValue(BitWidth)
1976*b5893f02SDimitry Andric : APInt::getSignedMaxValue(BitWidth);
1977*b5893f02SDimitry Andric }
1978*b5893f02SDimitry Andric
usub_sat(const APInt & RHS) const1979*b5893f02SDimitry Andric APInt APInt::usub_sat(const APInt &RHS) const {
1980*b5893f02SDimitry Andric bool Overflow;
1981*b5893f02SDimitry Andric APInt Res = usub_ov(RHS, Overflow);
1982*b5893f02SDimitry Andric if (!Overflow)
1983*b5893f02SDimitry Andric return Res;
1984*b5893f02SDimitry Andric
1985*b5893f02SDimitry Andric return APInt(BitWidth, 0);
1986*b5893f02SDimitry Andric }
19872754fe60SDimitry Andric
19882754fe60SDimitry Andric
fromString(unsigned numbits,StringRef str,uint8_t radix)1989ffd1746dSEd Schouten void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
1990f22ef01cSRoman Divacky // Check our assumptions here
1991f22ef01cSRoman Divacky assert(!str.empty() && "Invalid string length");
19926122f3e6SDimitry Andric assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 ||
19936122f3e6SDimitry Andric radix == 36) &&
19946122f3e6SDimitry Andric "Radix should be 2, 8, 10, 16, or 36!");
1995f22ef01cSRoman Divacky
1996f22ef01cSRoman Divacky StringRef::iterator p = str.begin();
1997f22ef01cSRoman Divacky size_t slen = str.size();
1998f22ef01cSRoman Divacky bool isNeg = *p == '-';
1999f22ef01cSRoman Divacky if (*p == '-' || *p == '+') {
2000f22ef01cSRoman Divacky p++;
2001f22ef01cSRoman Divacky slen--;
2002f22ef01cSRoman Divacky assert(slen && "String is only a sign, needs a value.");
2003f22ef01cSRoman Divacky }
2004f22ef01cSRoman Divacky assert((slen <= numbits || radix != 2) && "Insufficient bit width");
2005f22ef01cSRoman Divacky assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width");
2006f22ef01cSRoman Divacky assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width");
2007f22ef01cSRoman Divacky assert((((slen-1)*64)/22 <= numbits || radix != 10) &&
2008f22ef01cSRoman Divacky "Insufficient bit width");
2009f22ef01cSRoman Divacky
2010f37b6182SDimitry Andric // Allocate memory if needed
2011f37b6182SDimitry Andric if (isSingleWord())
2012f37b6182SDimitry Andric U.VAL = 0;
2013f37b6182SDimitry Andric else
2014f37b6182SDimitry Andric U.pVal = getClearedMemory(getNumWords());
2015f22ef01cSRoman Divacky
2016f22ef01cSRoman Divacky // Figure out if we can shift instead of multiply
2017f22ef01cSRoman Divacky unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);
2018f22ef01cSRoman Divacky
2019f22ef01cSRoman Divacky // Enter digit traversal loop
2020f22ef01cSRoman Divacky for (StringRef::iterator e = str.end(); p != e; ++p) {
2021f22ef01cSRoman Divacky unsigned digit = getDigit(*p, radix);
2022f22ef01cSRoman Divacky assert(digit < radix && "Invalid character in digit string");
2023f22ef01cSRoman Divacky
2024f22ef01cSRoman Divacky // Shift or multiply the value by the radix
2025f22ef01cSRoman Divacky if (slen > 1) {
2026f22ef01cSRoman Divacky if (shift)
2027f22ef01cSRoman Divacky *this <<= shift;
2028f22ef01cSRoman Divacky else
20290f5676f4SDimitry Andric *this *= radix;
2030f22ef01cSRoman Divacky }
2031f22ef01cSRoman Divacky
2032f22ef01cSRoman Divacky // Add in the digit we just interpreted
20337a7e6055SDimitry Andric *this += digit;
2034f22ef01cSRoman Divacky }
2035f22ef01cSRoman Divacky // If its negative, put it in two's complement form
20365517e702SDimitry Andric if (isNeg)
20375517e702SDimitry Andric this->negate();
2038f22ef01cSRoman Divacky }
2039f22ef01cSRoman Divacky
toString(SmallVectorImpl<char> & Str,unsigned Radix,bool Signed,bool formatAsCLiteral) const2040f22ef01cSRoman Divacky void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
204117a519f9SDimitry Andric bool Signed, bool formatAsCLiteral) const {
20426122f3e6SDimitry Andric assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
20436122f3e6SDimitry Andric Radix == 36) &&
2044dff0c46cSDimitry Andric "Radix should be 2, 8, 10, 16, or 36!");
2045f22ef01cSRoman Divacky
204617a519f9SDimitry Andric const char *Prefix = "";
204717a519f9SDimitry Andric if (formatAsCLiteral) {
204817a519f9SDimitry Andric switch (Radix) {
204917a519f9SDimitry Andric case 2:
205017a519f9SDimitry Andric // Binary literals are a non-standard extension added in gcc 4.3:
205117a519f9SDimitry Andric // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html
205217a519f9SDimitry Andric Prefix = "0b";
205317a519f9SDimitry Andric break;
205417a519f9SDimitry Andric case 8:
205517a519f9SDimitry Andric Prefix = "0";
205617a519f9SDimitry Andric break;
2057dff0c46cSDimitry Andric case 10:
2058dff0c46cSDimitry Andric break; // No prefix
205917a519f9SDimitry Andric case 16:
206017a519f9SDimitry Andric Prefix = "0x";
206117a519f9SDimitry Andric break;
2062dff0c46cSDimitry Andric default:
2063dff0c46cSDimitry Andric llvm_unreachable("Invalid radix!");
206417a519f9SDimitry Andric }
206517a519f9SDimitry Andric }
206617a519f9SDimitry Andric
2067f22ef01cSRoman Divacky // First, check for a zero value and just short circuit the logic below.
2068f22ef01cSRoman Divacky if (*this == 0) {
206917a519f9SDimitry Andric while (*Prefix) {
207017a519f9SDimitry Andric Str.push_back(*Prefix);
207117a519f9SDimitry Andric ++Prefix;
207217a519f9SDimitry Andric };
2073f22ef01cSRoman Divacky Str.push_back('0');
2074f22ef01cSRoman Divacky return;
2075f22ef01cSRoman Divacky }
2076f22ef01cSRoman Divacky
20776122f3e6SDimitry Andric static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2078f22ef01cSRoman Divacky
2079f22ef01cSRoman Divacky if (isSingleWord()) {
2080f22ef01cSRoman Divacky char Buffer[65];
2081302affcbSDimitry Andric char *BufPtr = std::end(Buffer);
2082f22ef01cSRoman Divacky
2083f22ef01cSRoman Divacky uint64_t N;
2084e580952dSDimitry Andric if (!Signed) {
2085e580952dSDimitry Andric N = getZExtValue();
2086e580952dSDimitry Andric } else {
2087f22ef01cSRoman Divacky int64_t I = getSExtValue();
2088e580952dSDimitry Andric if (I >= 0) {
2089f22ef01cSRoman Divacky N = I;
2090f22ef01cSRoman Divacky } else {
2091e580952dSDimitry Andric Str.push_back('-');
2092e580952dSDimitry Andric N = -(uint64_t)I;
2093e580952dSDimitry Andric }
2094f22ef01cSRoman Divacky }
2095f22ef01cSRoman Divacky
209617a519f9SDimitry Andric while (*Prefix) {
209717a519f9SDimitry Andric Str.push_back(*Prefix);
209817a519f9SDimitry Andric ++Prefix;
209917a519f9SDimitry Andric };
210017a519f9SDimitry Andric
2101f22ef01cSRoman Divacky while (N) {
2102f22ef01cSRoman Divacky *--BufPtr = Digits[N % Radix];
2103f22ef01cSRoman Divacky N /= Radix;
2104f22ef01cSRoman Divacky }
2105302affcbSDimitry Andric Str.append(BufPtr, std::end(Buffer));
2106f22ef01cSRoman Divacky return;
2107f22ef01cSRoman Divacky }
2108f22ef01cSRoman Divacky
2109f22ef01cSRoman Divacky APInt Tmp(*this);
2110f22ef01cSRoman Divacky
2111f22ef01cSRoman Divacky if (Signed && isNegative()) {
2112f22ef01cSRoman Divacky // They want to print the signed version and it is a negative value
2113f22ef01cSRoman Divacky // Flip the bits and add one to turn it into the equivalent positive
2114f22ef01cSRoman Divacky // value and put a '-' in the result.
21155517e702SDimitry Andric Tmp.negate();
2116f22ef01cSRoman Divacky Str.push_back('-');
2117f22ef01cSRoman Divacky }
2118f22ef01cSRoman Divacky
211917a519f9SDimitry Andric while (*Prefix) {
212017a519f9SDimitry Andric Str.push_back(*Prefix);
212117a519f9SDimitry Andric ++Prefix;
212217a519f9SDimitry Andric };
212317a519f9SDimitry Andric
2124f22ef01cSRoman Divacky // We insert the digits backward, then reverse them to get the right order.
2125f22ef01cSRoman Divacky unsigned StartDig = Str.size();
2126f22ef01cSRoman Divacky
2127f22ef01cSRoman Divacky // For the 2, 8 and 16 bit cases, we can just shift instead of divide
2128f22ef01cSRoman Divacky // because the number of bits per digit (1, 3 and 4 respectively) divides
21297a7e6055SDimitry Andric // equally. We just shift until the value is zero.
21306122f3e6SDimitry Andric if (Radix == 2 || Radix == 8 || Radix == 16) {
2131f22ef01cSRoman Divacky // Just shift tmp right for each digit width until it becomes zero
2132f22ef01cSRoman Divacky unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
2133f22ef01cSRoman Divacky unsigned MaskAmt = Radix - 1;
2134f22ef01cSRoman Divacky
21355517e702SDimitry Andric while (Tmp.getBoolValue()) {
2136f22ef01cSRoman Divacky unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
2137f22ef01cSRoman Divacky Str.push_back(Digits[Digit]);
21386bc11b14SDimitry Andric Tmp.lshrInPlace(ShiftAmt);
2139f22ef01cSRoman Divacky }
2140f22ef01cSRoman Divacky } else {
21415517e702SDimitry Andric while (Tmp.getBoolValue()) {
2142d8866befSDimitry Andric uint64_t Digit;
2143d8866befSDimitry Andric udivrem(Tmp, Radix, Tmp, Digit);
2144f22ef01cSRoman Divacky assert(Digit < Radix && "divide failed");
2145f22ef01cSRoman Divacky Str.push_back(Digits[Digit]);
2146f22ef01cSRoman Divacky }
2147f22ef01cSRoman Divacky }
2148f22ef01cSRoman Divacky
2149f22ef01cSRoman Divacky // Reverse the digits before returning.
2150f22ef01cSRoman Divacky std::reverse(Str.begin()+StartDig, Str.end());
2151f22ef01cSRoman Divacky }
2152f22ef01cSRoman Divacky
2153ff0cc061SDimitry Andric /// Returns the APInt as a std::string. Note that this is an inefficient method.
2154ff0cc061SDimitry Andric /// It is better to pass in a SmallVector/SmallString to the methods above.
toString(unsigned Radix=10,bool Signed=true) const2155f22ef01cSRoman Divacky std::string APInt::toString(unsigned Radix = 10, bool Signed = true) const {
2156f22ef01cSRoman Divacky SmallString<40> S;
215717a519f9SDimitry Andric toString(S, Radix, Signed, /* formatAsCLiteral = */false);
2158f22ef01cSRoman Divacky return S.str();
2159f22ef01cSRoman Divacky }
2160f22ef01cSRoman Divacky
21617a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const21623ca95b02SDimitry Andric LLVM_DUMP_METHOD void APInt::dump() const {
2163f22ef01cSRoman Divacky SmallString<40> S, U;
2164f22ef01cSRoman Divacky this->toStringUnsigned(U);
2165f22ef01cSRoman Divacky this->toStringSigned(S);
2166f22ef01cSRoman Divacky dbgs() << "APInt(" << BitWidth << "b, "
21677a7e6055SDimitry Andric << U << "u " << S << "s)\n";
2168f22ef01cSRoman Divacky }
21697a7e6055SDimitry Andric #endif
2170f22ef01cSRoman Divacky
print(raw_ostream & OS,bool isSigned) const2171f22ef01cSRoman Divacky void APInt::print(raw_ostream &OS, bool isSigned) const {
2172f22ef01cSRoman Divacky SmallString<40> S;
217317a519f9SDimitry Andric this->toString(S, 10, isSigned, /* formatAsCLiteral = */false);
2174ff0cc061SDimitry Andric OS << S;
2175f22ef01cSRoman Divacky }
2176f22ef01cSRoman Divacky
2177f22ef01cSRoman Divacky // This implements a variety of operations on a representation of
2178f22ef01cSRoman Divacky // arbitrary precision, two's-complement, bignum integer values.
2179f22ef01cSRoman Divacky
2180f22ef01cSRoman Divacky // Assumed by lowHalf, highHalf, partMSB and partLSB. A fairly safe
2181f22ef01cSRoman Divacky // and unrestricting assumption.
21827a7e6055SDimitry Andric static_assert(APInt::APINT_BITS_PER_WORD % 2 == 0,
21837a7e6055SDimitry Andric "Part width must be divisible by 2!");
2184f22ef01cSRoman Divacky
2185f22ef01cSRoman Divacky /* Some handy functions local to this file. */
2186f22ef01cSRoman Divacky
2187f22ef01cSRoman Divacky /* Returns the integer part with the least significant BITS set.
2188f22ef01cSRoman Divacky BITS cannot be zero. */
lowBitMask(unsigned bits)21897a7e6055SDimitry Andric static inline APInt::WordType lowBitMask(unsigned bits) {
21907a7e6055SDimitry Andric assert(bits != 0 && bits <= APInt::APINT_BITS_PER_WORD);
2191f22ef01cSRoman Divacky
21927a7e6055SDimitry Andric return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits);
2193f22ef01cSRoman Divacky }
2194f22ef01cSRoman Divacky
2195f22ef01cSRoman Divacky /* Returns the value of the lower half of PART. */
lowHalf(APInt::WordType part)21967a7e6055SDimitry Andric static inline APInt::WordType lowHalf(APInt::WordType part) {
21977a7e6055SDimitry Andric return part & lowBitMask(APInt::APINT_BITS_PER_WORD / 2);
2198f22ef01cSRoman Divacky }
2199f22ef01cSRoman Divacky
2200f22ef01cSRoman Divacky /* Returns the value of the upper half of PART. */
highHalf(APInt::WordType part)22017a7e6055SDimitry Andric static inline APInt::WordType highHalf(APInt::WordType part) {
22027a7e6055SDimitry Andric return part >> (APInt::APINT_BITS_PER_WORD / 2);
2203f22ef01cSRoman Divacky }
2204f22ef01cSRoman Divacky
2205f22ef01cSRoman Divacky /* Returns the bit number of the most significant set bit of a part.
2206f22ef01cSRoman Divacky If the input number has no bits set -1U is returned. */
partMSB(APInt::WordType value)22077a7e6055SDimitry Andric static unsigned partMSB(APInt::WordType value) {
2208f785676fSDimitry Andric return findLastSet(value, ZB_Max);
2209f22ef01cSRoman Divacky }
2210f22ef01cSRoman Divacky
2211f22ef01cSRoman Divacky /* Returns the bit number of the least significant set bit of a
2212f22ef01cSRoman Divacky part. If the input number has no bits set -1U is returned. */
partLSB(APInt::WordType value)22137a7e6055SDimitry Andric static unsigned partLSB(APInt::WordType value) {
2214f785676fSDimitry Andric return findFirstSet(value, ZB_Max);
2215f22ef01cSRoman Divacky }
2216f22ef01cSRoman Divacky
2217f22ef01cSRoman Divacky /* Sets the least significant part of a bignum to the input value, and
2218f22ef01cSRoman Divacky zeroes out higher parts. */
tcSet(WordType * dst,WordType part,unsigned parts)22197a7e6055SDimitry Andric void APInt::tcSet(WordType *dst, WordType part, unsigned parts) {
2220f22ef01cSRoman Divacky assert(parts > 0);
2221f22ef01cSRoman Divacky
2222f22ef01cSRoman Divacky dst[0] = part;
22237a7e6055SDimitry Andric for (unsigned i = 1; i < parts; i++)
2224f22ef01cSRoman Divacky dst[i] = 0;
2225f22ef01cSRoman Divacky }
2226f22ef01cSRoman Divacky
2227f22ef01cSRoman Divacky /* Assign one bignum to another. */
tcAssign(WordType * dst,const WordType * src,unsigned parts)22287a7e6055SDimitry Andric void APInt::tcAssign(WordType *dst, const WordType *src, unsigned parts) {
22297a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2230f22ef01cSRoman Divacky dst[i] = src[i];
2231f22ef01cSRoman Divacky }
2232f22ef01cSRoman Divacky
2233f22ef01cSRoman Divacky /* Returns true if a bignum is zero, false otherwise. */
tcIsZero(const WordType * src,unsigned parts)22347a7e6055SDimitry Andric bool APInt::tcIsZero(const WordType *src, unsigned parts) {
22357a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2236f22ef01cSRoman Divacky if (src[i])
2237f22ef01cSRoman Divacky return false;
2238f22ef01cSRoman Divacky
2239f22ef01cSRoman Divacky return true;
2240f22ef01cSRoman Divacky }
2241f22ef01cSRoman Divacky
2242f22ef01cSRoman Divacky /* Extract the given bit of a bignum; returns 0 or 1. */
tcExtractBit(const WordType * parts,unsigned bit)22437a7e6055SDimitry Andric int APInt::tcExtractBit(const WordType *parts, unsigned bit) {
22447a7e6055SDimitry Andric return (parts[whichWord(bit)] & maskBit(bit)) != 0;
2245f22ef01cSRoman Divacky }
2246f22ef01cSRoman Divacky
2247f22ef01cSRoman Divacky /* Set the given bit of a bignum. */
tcSetBit(WordType * parts,unsigned bit)22487a7e6055SDimitry Andric void APInt::tcSetBit(WordType *parts, unsigned bit) {
22497a7e6055SDimitry Andric parts[whichWord(bit)] |= maskBit(bit);
2250f22ef01cSRoman Divacky }
2251f22ef01cSRoman Divacky
2252f22ef01cSRoman Divacky /* Clears the given bit of a bignum. */
tcClearBit(WordType * parts,unsigned bit)22537a7e6055SDimitry Andric void APInt::tcClearBit(WordType *parts, unsigned bit) {
22547a7e6055SDimitry Andric parts[whichWord(bit)] &= ~maskBit(bit);
2255f22ef01cSRoman Divacky }
2256f22ef01cSRoman Divacky
2257f22ef01cSRoman Divacky /* Returns the bit number of the least significant set bit of a
2258f22ef01cSRoman Divacky number. If the input number has no bits set -1U is returned. */
tcLSB(const WordType * parts,unsigned n)22597a7e6055SDimitry Andric unsigned APInt::tcLSB(const WordType *parts, unsigned n) {
22607a7e6055SDimitry Andric for (unsigned i = 0; i < n; i++) {
2261f22ef01cSRoman Divacky if (parts[i] != 0) {
22627a7e6055SDimitry Andric unsigned lsb = partLSB(parts[i]);
2263f22ef01cSRoman Divacky
22647a7e6055SDimitry Andric return lsb + i * APINT_BITS_PER_WORD;
2265f22ef01cSRoman Divacky }
2266f22ef01cSRoman Divacky }
2267f22ef01cSRoman Divacky
2268f22ef01cSRoman Divacky return -1U;
2269f22ef01cSRoman Divacky }
2270f22ef01cSRoman Divacky
2271f22ef01cSRoman Divacky /* Returns the bit number of the most significant set bit of a number.
2272f22ef01cSRoman Divacky If the input number has no bits set -1U is returned. */
tcMSB(const WordType * parts,unsigned n)22737a7e6055SDimitry Andric unsigned APInt::tcMSB(const WordType *parts, unsigned n) {
2274f22ef01cSRoman Divacky do {
2275f22ef01cSRoman Divacky --n;
2276f22ef01cSRoman Divacky
2277f22ef01cSRoman Divacky if (parts[n] != 0) {
22787a7e6055SDimitry Andric unsigned msb = partMSB(parts[n]);
2279f22ef01cSRoman Divacky
22807a7e6055SDimitry Andric return msb + n * APINT_BITS_PER_WORD;
2281f22ef01cSRoman Divacky }
2282f22ef01cSRoman Divacky } while (n);
2283f22ef01cSRoman Divacky
2284f22ef01cSRoman Divacky return -1U;
2285f22ef01cSRoman Divacky }
2286f22ef01cSRoman Divacky
2287f22ef01cSRoman Divacky /* Copy the bit vector of width srcBITS from SRC, starting at bit
2288f22ef01cSRoman Divacky srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes
2289f22ef01cSRoman Divacky the least significant bit of DST. All high bits above srcBITS in
2290f22ef01cSRoman Divacky DST are zero-filled. */
2291f22ef01cSRoman Divacky void
tcExtract(WordType * dst,unsigned dstCount,const WordType * src,unsigned srcBits,unsigned srcLSB)22927a7e6055SDimitry Andric APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src,
22937a7e6055SDimitry Andric unsigned srcBits, unsigned srcLSB) {
22947a7e6055SDimitry Andric unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
2295f22ef01cSRoman Divacky assert(dstParts <= dstCount);
2296f22ef01cSRoman Divacky
22977a7e6055SDimitry Andric unsigned firstSrcPart = srcLSB / APINT_BITS_PER_WORD;
2298f22ef01cSRoman Divacky tcAssign (dst, src + firstSrcPart, dstParts);
2299f22ef01cSRoman Divacky
23007a7e6055SDimitry Andric unsigned shift = srcLSB % APINT_BITS_PER_WORD;
2301f22ef01cSRoman Divacky tcShiftRight (dst, dstParts, shift);
2302f22ef01cSRoman Divacky
23037a7e6055SDimitry Andric /* We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC
2304f22ef01cSRoman Divacky in DST. If this is less that srcBits, append the rest, else
2305f22ef01cSRoman Divacky clear the high bits. */
23067a7e6055SDimitry Andric unsigned n = dstParts * APINT_BITS_PER_WORD - shift;
2307f22ef01cSRoman Divacky if (n < srcBits) {
23087a7e6055SDimitry Andric WordType mask = lowBitMask (srcBits - n);
2309f22ef01cSRoman Divacky dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
23107a7e6055SDimitry Andric << n % APINT_BITS_PER_WORD);
2311f22ef01cSRoman Divacky } else if (n > srcBits) {
23127a7e6055SDimitry Andric if (srcBits % APINT_BITS_PER_WORD)
23137a7e6055SDimitry Andric dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD);
2314f22ef01cSRoman Divacky }
2315f22ef01cSRoman Divacky
2316f22ef01cSRoman Divacky /* Clear high parts. */
2317f22ef01cSRoman Divacky while (dstParts < dstCount)
2318f22ef01cSRoman Divacky dst[dstParts++] = 0;
2319f22ef01cSRoman Divacky }
2320f22ef01cSRoman Divacky
2321f22ef01cSRoman Divacky /* DST += RHS + C where C is zero or one. Returns the carry flag. */
tcAdd(WordType * dst,const WordType * rhs,WordType c,unsigned parts)23227a7e6055SDimitry Andric APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs,
23237a7e6055SDimitry Andric WordType c, unsigned parts) {
2324f22ef01cSRoman Divacky assert(c <= 1);
2325f22ef01cSRoman Divacky
23267a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++) {
23277a7e6055SDimitry Andric WordType l = dst[i];
2328f22ef01cSRoman Divacky if (c) {
2329f22ef01cSRoman Divacky dst[i] += rhs[i] + 1;
2330f22ef01cSRoman Divacky c = (dst[i] <= l);
2331f22ef01cSRoman Divacky } else {
2332f22ef01cSRoman Divacky dst[i] += rhs[i];
2333f22ef01cSRoman Divacky c = (dst[i] < l);
2334f22ef01cSRoman Divacky }
2335f22ef01cSRoman Divacky }
2336f22ef01cSRoman Divacky
2337f22ef01cSRoman Divacky return c;
2338f22ef01cSRoman Divacky }
2339f22ef01cSRoman Divacky
23407a7e6055SDimitry Andric /// This function adds a single "word" integer, src, to the multiple
23417a7e6055SDimitry Andric /// "word" integer array, dst[]. dst[] is modified to reflect the addition and
23427a7e6055SDimitry Andric /// 1 is returned if there is a carry out, otherwise 0 is returned.
23437a7e6055SDimitry Andric /// @returns the carry of the addition.
tcAddPart(WordType * dst,WordType src,unsigned parts)23447a7e6055SDimitry Andric APInt::WordType APInt::tcAddPart(WordType *dst, WordType src,
23457a7e6055SDimitry Andric unsigned parts) {
23467a7e6055SDimitry Andric for (unsigned i = 0; i < parts; ++i) {
23477a7e6055SDimitry Andric dst[i] += src;
23487a7e6055SDimitry Andric if (dst[i] >= src)
23497a7e6055SDimitry Andric return 0; // No need to carry so exit early.
23507a7e6055SDimitry Andric src = 1; // Carry one to next digit.
23517a7e6055SDimitry Andric }
2352f22ef01cSRoman Divacky
23537a7e6055SDimitry Andric return 1;
23547a7e6055SDimitry Andric }
23557a7e6055SDimitry Andric
23567a7e6055SDimitry Andric /* DST -= RHS + C where C is zero or one. Returns the carry flag. */
tcSubtract(WordType * dst,const WordType * rhs,WordType c,unsigned parts)23577a7e6055SDimitry Andric APInt::WordType APInt::tcSubtract(WordType *dst, const WordType *rhs,
23587a7e6055SDimitry Andric WordType c, unsigned parts) {
2359f22ef01cSRoman Divacky assert(c <= 1);
2360f22ef01cSRoman Divacky
23617a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++) {
23627a7e6055SDimitry Andric WordType l = dst[i];
2363f22ef01cSRoman Divacky if (c) {
2364f22ef01cSRoman Divacky dst[i] -= rhs[i] + 1;
2365f22ef01cSRoman Divacky c = (dst[i] >= l);
2366f22ef01cSRoman Divacky } else {
2367f22ef01cSRoman Divacky dst[i] -= rhs[i];
2368f22ef01cSRoman Divacky c = (dst[i] > l);
2369f22ef01cSRoman Divacky }
2370f22ef01cSRoman Divacky }
2371f22ef01cSRoman Divacky
2372f22ef01cSRoman Divacky return c;
2373f22ef01cSRoman Divacky }
2374f22ef01cSRoman Divacky
23757a7e6055SDimitry Andric /// This function subtracts a single "word" (64-bit word), src, from
23767a7e6055SDimitry Andric /// the multi-word integer array, dst[], propagating the borrowed 1 value until
23777a7e6055SDimitry Andric /// no further borrowing is needed or it runs out of "words" in dst. The result
23787a7e6055SDimitry Andric /// is 1 if "borrowing" exhausted the digits in dst, or 0 if dst was not
23797a7e6055SDimitry Andric /// exhausted. In other words, if src > dst then this function returns 1,
23807a7e6055SDimitry Andric /// otherwise 0.
23817a7e6055SDimitry Andric /// @returns the borrow out of the subtraction
tcSubtractPart(WordType * dst,WordType src,unsigned parts)23827a7e6055SDimitry Andric APInt::WordType APInt::tcSubtractPart(WordType *dst, WordType src,
23837a7e6055SDimitry Andric unsigned parts) {
23847a7e6055SDimitry Andric for (unsigned i = 0; i < parts; ++i) {
23857a7e6055SDimitry Andric WordType Dst = dst[i];
23867a7e6055SDimitry Andric dst[i] -= src;
23877a7e6055SDimitry Andric if (src <= Dst)
23887a7e6055SDimitry Andric return 0; // No need to borrow so exit early.
23897a7e6055SDimitry Andric src = 1; // We have to "borrow 1" from next "word"
23907a7e6055SDimitry Andric }
23917a7e6055SDimitry Andric
23927a7e6055SDimitry Andric return 1;
23937a7e6055SDimitry Andric }
23947a7e6055SDimitry Andric
2395f22ef01cSRoman Divacky /* Negate a bignum in-place. */
tcNegate(WordType * dst,unsigned parts)23967a7e6055SDimitry Andric void APInt::tcNegate(WordType *dst, unsigned parts) {
2397f22ef01cSRoman Divacky tcComplement(dst, parts);
2398f22ef01cSRoman Divacky tcIncrement(dst, parts);
2399f22ef01cSRoman Divacky }
2400f22ef01cSRoman Divacky
2401f22ef01cSRoman Divacky /* DST += SRC * MULTIPLIER + CARRY if add is true
2402f22ef01cSRoman Divacky DST = SRC * MULTIPLIER + CARRY if add is false
2403f22ef01cSRoman Divacky
2404f22ef01cSRoman Divacky Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC
2405f22ef01cSRoman Divacky they must start at the same point, i.e. DST == SRC.
2406f22ef01cSRoman Divacky
2407f22ef01cSRoman Divacky If DSTPARTS == SRCPARTS + 1 no overflow occurs and zero is
2408f22ef01cSRoman Divacky returned. Otherwise DST is filled with the least significant
2409f22ef01cSRoman Divacky DSTPARTS parts of the result, and if all of the omitted higher
2410f22ef01cSRoman Divacky parts were zero return zero, otherwise overflow occurred and
2411f22ef01cSRoman Divacky return one. */
tcMultiplyPart(WordType * dst,const WordType * src,WordType multiplier,WordType carry,unsigned srcParts,unsigned dstParts,bool add)24127a7e6055SDimitry Andric int APInt::tcMultiplyPart(WordType *dst, const WordType *src,
24137a7e6055SDimitry Andric WordType multiplier, WordType carry,
24147a7e6055SDimitry Andric unsigned srcParts, unsigned dstParts,
24157a7e6055SDimitry Andric bool add) {
2416f22ef01cSRoman Divacky /* Otherwise our writes of DST kill our later reads of SRC. */
2417f22ef01cSRoman Divacky assert(dst <= src || dst >= src + srcParts);
2418f22ef01cSRoman Divacky assert(dstParts <= srcParts + 1);
2419f22ef01cSRoman Divacky
2420f22ef01cSRoman Divacky /* N loops; minimum of dstParts and srcParts. */
24210f5676f4SDimitry Andric unsigned n = std::min(dstParts, srcParts);
2422f22ef01cSRoman Divacky
24230f5676f4SDimitry Andric for (unsigned i = 0; i < n; i++) {
24247a7e6055SDimitry Andric WordType low, mid, high, srcPart;
2425f22ef01cSRoman Divacky
2426f22ef01cSRoman Divacky /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
2427f22ef01cSRoman Divacky
2428f22ef01cSRoman Divacky This cannot overflow, because
2429f22ef01cSRoman Divacky
2430f22ef01cSRoman Divacky (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1)
2431f22ef01cSRoman Divacky
2432f22ef01cSRoman Divacky which is less than n^2. */
2433f22ef01cSRoman Divacky
2434f22ef01cSRoman Divacky srcPart = src[i];
2435f22ef01cSRoman Divacky
2436f22ef01cSRoman Divacky if (multiplier == 0 || srcPart == 0) {
2437f22ef01cSRoman Divacky low = carry;
2438f22ef01cSRoman Divacky high = 0;
2439f22ef01cSRoman Divacky } else {
2440f22ef01cSRoman Divacky low = lowHalf(srcPart) * lowHalf(multiplier);
2441f22ef01cSRoman Divacky high = highHalf(srcPart) * highHalf(multiplier);
2442f22ef01cSRoman Divacky
2443f22ef01cSRoman Divacky mid = lowHalf(srcPart) * highHalf(multiplier);
2444f22ef01cSRoman Divacky high += highHalf(mid);
24457a7e6055SDimitry Andric mid <<= APINT_BITS_PER_WORD / 2;
2446f22ef01cSRoman Divacky if (low + mid < low)
2447f22ef01cSRoman Divacky high++;
2448f22ef01cSRoman Divacky low += mid;
2449f22ef01cSRoman Divacky
2450f22ef01cSRoman Divacky mid = highHalf(srcPart) * lowHalf(multiplier);
2451f22ef01cSRoman Divacky high += highHalf(mid);
24527a7e6055SDimitry Andric mid <<= APINT_BITS_PER_WORD / 2;
2453f22ef01cSRoman Divacky if (low + mid < low)
2454f22ef01cSRoman Divacky high++;
2455f22ef01cSRoman Divacky low += mid;
2456f22ef01cSRoman Divacky
2457f22ef01cSRoman Divacky /* Now add carry. */
2458f22ef01cSRoman Divacky if (low + carry < low)
2459f22ef01cSRoman Divacky high++;
2460f22ef01cSRoman Divacky low += carry;
2461f22ef01cSRoman Divacky }
2462f22ef01cSRoman Divacky
2463f22ef01cSRoman Divacky if (add) {
2464f22ef01cSRoman Divacky /* And now DST[i], and store the new low part there. */
2465f22ef01cSRoman Divacky if (low + dst[i] < low)
2466f22ef01cSRoman Divacky high++;
2467f22ef01cSRoman Divacky dst[i] += low;
2468f22ef01cSRoman Divacky } else
2469f22ef01cSRoman Divacky dst[i] = low;
2470f22ef01cSRoman Divacky
2471f22ef01cSRoman Divacky carry = high;
2472f22ef01cSRoman Divacky }
2473f22ef01cSRoman Divacky
24740f5676f4SDimitry Andric if (srcParts < dstParts) {
2475f22ef01cSRoman Divacky /* Full multiplication, there is no overflow. */
24760f5676f4SDimitry Andric assert(srcParts + 1 == dstParts);
24770f5676f4SDimitry Andric dst[srcParts] = carry;
2478f22ef01cSRoman Divacky return 0;
24790f5676f4SDimitry Andric }
24800f5676f4SDimitry Andric
2481f22ef01cSRoman Divacky /* We overflowed if there is carry. */
2482f22ef01cSRoman Divacky if (carry)
2483f22ef01cSRoman Divacky return 1;
2484f22ef01cSRoman Divacky
2485f22ef01cSRoman Divacky /* We would overflow if any significant unwritten parts would be
2486f22ef01cSRoman Divacky non-zero. This is true if any remaining src parts are non-zero
2487f22ef01cSRoman Divacky and the multiplier is non-zero. */
2488f22ef01cSRoman Divacky if (multiplier)
24890f5676f4SDimitry Andric for (unsigned i = dstParts; i < srcParts; i++)
2490f22ef01cSRoman Divacky if (src[i])
2491f22ef01cSRoman Divacky return 1;
2492f22ef01cSRoman Divacky
2493f22ef01cSRoman Divacky /* We fitted in the narrow destination. */
2494f22ef01cSRoman Divacky return 0;
2495f22ef01cSRoman Divacky }
2496f22ef01cSRoman Divacky
2497f22ef01cSRoman Divacky /* DST = LHS * RHS, where DST has the same width as the operands and
2498f22ef01cSRoman Divacky is filled with the least significant parts of the result. Returns
2499f22ef01cSRoman Divacky one if overflow occurred, otherwise zero. DST must be disjoint
2500f22ef01cSRoman Divacky from both operands. */
tcMultiply(WordType * dst,const WordType * lhs,const WordType * rhs,unsigned parts)25017a7e6055SDimitry Andric int APInt::tcMultiply(WordType *dst, const WordType *lhs,
25027a7e6055SDimitry Andric const WordType *rhs, unsigned parts) {
2503f22ef01cSRoman Divacky assert(dst != lhs && dst != rhs);
2504f22ef01cSRoman Divacky
25057a7e6055SDimitry Andric int overflow = 0;
2506f22ef01cSRoman Divacky tcSet(dst, 0, parts);
2507f22ef01cSRoman Divacky
25087a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2509f22ef01cSRoman Divacky overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
2510f22ef01cSRoman Divacky parts - i, true);
2511f22ef01cSRoman Divacky
2512f22ef01cSRoman Divacky return overflow;
2513f22ef01cSRoman Divacky }
2514f22ef01cSRoman Divacky
25155517e702SDimitry Andric /// DST = LHS * RHS, where DST has width the sum of the widths of the
25165517e702SDimitry Andric /// operands. No overflow occurs. DST must be disjoint from both operands.
tcFullMultiply(WordType * dst,const WordType * lhs,const WordType * rhs,unsigned lhsParts,unsigned rhsParts)25175517e702SDimitry Andric void APInt::tcFullMultiply(WordType *dst, const WordType *lhs,
25187a7e6055SDimitry Andric const WordType *rhs, unsigned lhsParts,
25197a7e6055SDimitry Andric unsigned rhsParts) {
2520f22ef01cSRoman Divacky /* Put the narrower number on the LHS for less loops below. */
25210f5676f4SDimitry Andric if (lhsParts > rhsParts)
2522f22ef01cSRoman Divacky return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts);
25230f5676f4SDimitry Andric
2524f22ef01cSRoman Divacky assert(dst != lhs && dst != rhs);
2525f22ef01cSRoman Divacky
2526f22ef01cSRoman Divacky tcSet(dst, 0, rhsParts);
2527f22ef01cSRoman Divacky
25287a7e6055SDimitry Andric for (unsigned i = 0; i < lhsParts; i++)
25297a7e6055SDimitry Andric tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, true);
2530f22ef01cSRoman Divacky }
2531f22ef01cSRoman Divacky
2532f22ef01cSRoman Divacky /* If RHS is zero LHS and REMAINDER are left unchanged, return one.
2533f22ef01cSRoman Divacky Otherwise set LHS to LHS / RHS with the fractional part discarded,
2534f22ef01cSRoman Divacky set REMAINDER to the remainder, return zero. i.e.
2535f22ef01cSRoman Divacky
2536f22ef01cSRoman Divacky OLD_LHS = RHS * LHS + REMAINDER
2537f22ef01cSRoman Divacky
2538f22ef01cSRoman Divacky SCRATCH is a bignum of the same size as the operands and result for
2539f22ef01cSRoman Divacky use by the routine; its contents need not be initialized and are
2540f22ef01cSRoman Divacky destroyed. LHS, REMAINDER and SCRATCH must be distinct.
2541f22ef01cSRoman Divacky */
tcDivide(WordType * lhs,const WordType * rhs,WordType * remainder,WordType * srhs,unsigned parts)25427a7e6055SDimitry Andric int APInt::tcDivide(WordType *lhs, const WordType *rhs,
25437a7e6055SDimitry Andric WordType *remainder, WordType *srhs,
25447a7e6055SDimitry Andric unsigned parts) {
2545f22ef01cSRoman Divacky assert(lhs != remainder && lhs != srhs && remainder != srhs);
2546f22ef01cSRoman Divacky
25477a7e6055SDimitry Andric unsigned shiftCount = tcMSB(rhs, parts) + 1;
2548f22ef01cSRoman Divacky if (shiftCount == 0)
2549f22ef01cSRoman Divacky return true;
2550f22ef01cSRoman Divacky
25517a7e6055SDimitry Andric shiftCount = parts * APINT_BITS_PER_WORD - shiftCount;
25527a7e6055SDimitry Andric unsigned n = shiftCount / APINT_BITS_PER_WORD;
25537a7e6055SDimitry Andric WordType mask = (WordType) 1 << (shiftCount % APINT_BITS_PER_WORD);
2554f22ef01cSRoman Divacky
2555f22ef01cSRoman Divacky tcAssign(srhs, rhs, parts);
2556f22ef01cSRoman Divacky tcShiftLeft(srhs, parts, shiftCount);
2557f22ef01cSRoman Divacky tcAssign(remainder, lhs, parts);
2558f22ef01cSRoman Divacky tcSet(lhs, 0, parts);
2559f22ef01cSRoman Divacky
2560f22ef01cSRoman Divacky /* Loop, subtracting SRHS if REMAINDER is greater and adding that to
2561f22ef01cSRoman Divacky the total. */
2562f22ef01cSRoman Divacky for (;;) {
25635517e702SDimitry Andric int compare = tcCompare(remainder, srhs, parts);
2564f22ef01cSRoman Divacky if (compare >= 0) {
2565f22ef01cSRoman Divacky tcSubtract(remainder, srhs, 0, parts);
2566f22ef01cSRoman Divacky lhs[n] |= mask;
2567f22ef01cSRoman Divacky }
2568f22ef01cSRoman Divacky
2569f22ef01cSRoman Divacky if (shiftCount == 0)
2570f22ef01cSRoman Divacky break;
2571f22ef01cSRoman Divacky shiftCount--;
2572f22ef01cSRoman Divacky tcShiftRight(srhs, parts, 1);
25733ca95b02SDimitry Andric if ((mask >>= 1) == 0) {
25747a7e6055SDimitry Andric mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1);
25753ca95b02SDimitry Andric n--;
25763ca95b02SDimitry Andric }
2577f22ef01cSRoman Divacky }
2578f22ef01cSRoman Divacky
2579f22ef01cSRoman Divacky return false;
2580f22ef01cSRoman Divacky }
2581f22ef01cSRoman Divacky
25826bc11b14SDimitry Andric /// Shift a bignum left Cound bits in-place. Shifted in bits are zero. There are
25836bc11b14SDimitry Andric /// no restrictions on Count.
tcShiftLeft(WordType * Dst,unsigned Words,unsigned Count)25846bc11b14SDimitry Andric void APInt::tcShiftLeft(WordType *Dst, unsigned Words, unsigned Count) {
25856bc11b14SDimitry Andric // Don't bother performing a no-op shift.
25866bc11b14SDimitry Andric if (!Count)
25876bc11b14SDimitry Andric return;
2588f22ef01cSRoman Divacky
258951690af2SDimitry Andric // WordShift is the inter-part shift; BitShift is the intra-part shift.
25906bc11b14SDimitry Andric unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words);
25916bc11b14SDimitry Andric unsigned BitShift = Count % APINT_BITS_PER_WORD;
2592f22ef01cSRoman Divacky
25936bc11b14SDimitry Andric // Fastpath for moving by whole words.
25946bc11b14SDimitry Andric if (BitShift == 0) {
25956bc11b14SDimitry Andric std::memmove(Dst + WordShift, Dst, (Words - WordShift) * APINT_WORD_SIZE);
2596f22ef01cSRoman Divacky } else {
25976bc11b14SDimitry Andric while (Words-- > WordShift) {
25986bc11b14SDimitry Andric Dst[Words] = Dst[Words - WordShift] << BitShift;
25996bc11b14SDimitry Andric if (Words > WordShift)
26006bc11b14SDimitry Andric Dst[Words] |=
26016bc11b14SDimitry Andric Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift);
2602f22ef01cSRoman Divacky }
2603f22ef01cSRoman Divacky }
2604f22ef01cSRoman Divacky
26056bc11b14SDimitry Andric // Fill in the remainder with 0s.
26066bc11b14SDimitry Andric std::memset(Dst, 0, WordShift * APINT_WORD_SIZE);
26076bc11b14SDimitry Andric }
26086bc11b14SDimitry Andric
26096bc11b14SDimitry Andric /// Shift a bignum right Count bits in-place. Shifted in bits are zero. There
26106bc11b14SDimitry Andric /// are no restrictions on Count.
tcShiftRight(WordType * Dst,unsigned Words,unsigned Count)26116bc11b14SDimitry Andric void APInt::tcShiftRight(WordType *Dst, unsigned Words, unsigned Count) {
26126bc11b14SDimitry Andric // Don't bother performing a no-op shift.
26136bc11b14SDimitry Andric if (!Count)
26146bc11b14SDimitry Andric return;
26156bc11b14SDimitry Andric
261651690af2SDimitry Andric // WordShift is the inter-part shift; BitShift is the intra-part shift.
26176bc11b14SDimitry Andric unsigned WordShift = std::min(Count / APINT_BITS_PER_WORD, Words);
26186bc11b14SDimitry Andric unsigned BitShift = Count % APINT_BITS_PER_WORD;
26196bc11b14SDimitry Andric
26206bc11b14SDimitry Andric unsigned WordsToMove = Words - WordShift;
26216bc11b14SDimitry Andric // Fastpath for moving by whole words.
26226bc11b14SDimitry Andric if (BitShift == 0) {
26236bc11b14SDimitry Andric std::memmove(Dst, Dst + WordShift, WordsToMove * APINT_WORD_SIZE);
26246bc11b14SDimitry Andric } else {
26256bc11b14SDimitry Andric for (unsigned i = 0; i != WordsToMove; ++i) {
26266bc11b14SDimitry Andric Dst[i] = Dst[i + WordShift] >> BitShift;
26276bc11b14SDimitry Andric if (i + 1 != WordsToMove)
26286bc11b14SDimitry Andric Dst[i] |= Dst[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift);
2629f22ef01cSRoman Divacky }
2630f22ef01cSRoman Divacky }
26316bc11b14SDimitry Andric
26326bc11b14SDimitry Andric // Fill in the remainder with 0s.
26336bc11b14SDimitry Andric std::memset(Dst + WordsToMove, 0, WordShift * APINT_WORD_SIZE);
2634f22ef01cSRoman Divacky }
2635f22ef01cSRoman Divacky
2636f22ef01cSRoman Divacky /* Bitwise and of two bignums. */
tcAnd(WordType * dst,const WordType * rhs,unsigned parts)26377a7e6055SDimitry Andric void APInt::tcAnd(WordType *dst, const WordType *rhs, unsigned parts) {
26387a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2639f22ef01cSRoman Divacky dst[i] &= rhs[i];
2640f22ef01cSRoman Divacky }
2641f22ef01cSRoman Divacky
2642f22ef01cSRoman Divacky /* Bitwise inclusive or of two bignums. */
tcOr(WordType * dst,const WordType * rhs,unsigned parts)26437a7e6055SDimitry Andric void APInt::tcOr(WordType *dst, const WordType *rhs, unsigned parts) {
26447a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2645f22ef01cSRoman Divacky dst[i] |= rhs[i];
2646f22ef01cSRoman Divacky }
2647f22ef01cSRoman Divacky
2648f22ef01cSRoman Divacky /* Bitwise exclusive or of two bignums. */
tcXor(WordType * dst,const WordType * rhs,unsigned parts)26497a7e6055SDimitry Andric void APInt::tcXor(WordType *dst, const WordType *rhs, unsigned parts) {
26507a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2651f22ef01cSRoman Divacky dst[i] ^= rhs[i];
2652f22ef01cSRoman Divacky }
2653f22ef01cSRoman Divacky
2654f22ef01cSRoman Divacky /* Complement a bignum in-place. */
tcComplement(WordType * dst,unsigned parts)26557a7e6055SDimitry Andric void APInt::tcComplement(WordType *dst, unsigned parts) {
26567a7e6055SDimitry Andric for (unsigned i = 0; i < parts; i++)
2657f22ef01cSRoman Divacky dst[i] = ~dst[i];
2658f22ef01cSRoman Divacky }
2659f22ef01cSRoman Divacky
2660f22ef01cSRoman Divacky /* Comparison (unsigned) of two bignums. */
tcCompare(const WordType * lhs,const WordType * rhs,unsigned parts)26617a7e6055SDimitry Andric int APInt::tcCompare(const WordType *lhs, const WordType *rhs,
26627a7e6055SDimitry Andric unsigned parts) {
2663f22ef01cSRoman Divacky while (parts) {
2664f22ef01cSRoman Divacky parts--;
266551690af2SDimitry Andric if (lhs[parts] != rhs[parts])
26667a7e6055SDimitry Andric return (lhs[parts] > rhs[parts]) ? 1 : -1;
2667f22ef01cSRoman Divacky }
2668f22ef01cSRoman Divacky
2669f22ef01cSRoman Divacky return 0;
2670f22ef01cSRoman Divacky }
2671f22ef01cSRoman Divacky
2672f22ef01cSRoman Divacky /* Set the least significant BITS bits of a bignum, clear the
2673f22ef01cSRoman Divacky rest. */
tcSetLeastSignificantBits(WordType * dst,unsigned parts,unsigned bits)26747a7e6055SDimitry Andric void APInt::tcSetLeastSignificantBits(WordType *dst, unsigned parts,
26757a7e6055SDimitry Andric unsigned bits) {
26767a7e6055SDimitry Andric unsigned i = 0;
26777a7e6055SDimitry Andric while (bits > APINT_BITS_PER_WORD) {
26787a7e6055SDimitry Andric dst[i++] = ~(WordType) 0;
26797a7e6055SDimitry Andric bits -= APINT_BITS_PER_WORD;
2680f22ef01cSRoman Divacky }
2681f22ef01cSRoman Divacky
2682f22ef01cSRoman Divacky if (bits)
26837a7e6055SDimitry Andric dst[i++] = ~(WordType) 0 >> (APINT_BITS_PER_WORD - bits);
2684f22ef01cSRoman Divacky
2685f22ef01cSRoman Divacky while (i < parts)
2686f22ef01cSRoman Divacky dst[i++] = 0;
2687f22ef01cSRoman Divacky }
26884ba319b5SDimitry Andric
RoundingUDiv(const APInt & A,const APInt & B,APInt::Rounding RM)26894ba319b5SDimitry Andric APInt llvm::APIntOps::RoundingUDiv(const APInt &A, const APInt &B,
26904ba319b5SDimitry Andric APInt::Rounding RM) {
26914ba319b5SDimitry Andric // Currently udivrem always rounds down.
26924ba319b5SDimitry Andric switch (RM) {
26934ba319b5SDimitry Andric case APInt::Rounding::DOWN:
26944ba319b5SDimitry Andric case APInt::Rounding::TOWARD_ZERO:
26954ba319b5SDimitry Andric return A.udiv(B);
26964ba319b5SDimitry Andric case APInt::Rounding::UP: {
26974ba319b5SDimitry Andric APInt Quo, Rem;
26984ba319b5SDimitry Andric APInt::udivrem(A, B, Quo, Rem);
26994ba319b5SDimitry Andric if (Rem == 0)
27004ba319b5SDimitry Andric return Quo;
27014ba319b5SDimitry Andric return Quo + 1;
27024ba319b5SDimitry Andric }
27034ba319b5SDimitry Andric }
27044ba319b5SDimitry Andric llvm_unreachable("Unknown APInt::Rounding enum");
27054ba319b5SDimitry Andric }
27064ba319b5SDimitry Andric
RoundingSDiv(const APInt & A,const APInt & B,APInt::Rounding RM)27074ba319b5SDimitry Andric APInt llvm::APIntOps::RoundingSDiv(const APInt &A, const APInt &B,
27084ba319b5SDimitry Andric APInt::Rounding RM) {
27094ba319b5SDimitry Andric switch (RM) {
27104ba319b5SDimitry Andric case APInt::Rounding::DOWN:
27114ba319b5SDimitry Andric case APInt::Rounding::UP: {
27124ba319b5SDimitry Andric APInt Quo, Rem;
27134ba319b5SDimitry Andric APInt::sdivrem(A, B, Quo, Rem);
27144ba319b5SDimitry Andric if (Rem == 0)
27154ba319b5SDimitry Andric return Quo;
27164ba319b5SDimitry Andric // This algorithm deals with arbitrary rounding mode used by sdivrem.
27174ba319b5SDimitry Andric // We want to check whether the non-integer part of the mathematical value
27184ba319b5SDimitry Andric // is negative or not. If the non-integer part is negative, we need to round
27194ba319b5SDimitry Andric // down from Quo; otherwise, if it's positive or 0, we return Quo, as it's
27204ba319b5SDimitry Andric // already rounded down.
27214ba319b5SDimitry Andric if (RM == APInt::Rounding::DOWN) {
27224ba319b5SDimitry Andric if (Rem.isNegative() != B.isNegative())
27234ba319b5SDimitry Andric return Quo - 1;
27244ba319b5SDimitry Andric return Quo;
27254ba319b5SDimitry Andric }
27264ba319b5SDimitry Andric if (Rem.isNegative() != B.isNegative())
27274ba319b5SDimitry Andric return Quo;
27284ba319b5SDimitry Andric return Quo + 1;
27294ba319b5SDimitry Andric }
27304ba319b5SDimitry Andric // Currently sdiv rounds twards zero.
27314ba319b5SDimitry Andric case APInt::Rounding::TOWARD_ZERO:
27324ba319b5SDimitry Andric return A.sdiv(B);
27334ba319b5SDimitry Andric }
27344ba319b5SDimitry Andric llvm_unreachable("Unknown APInt::Rounding enum");
27354ba319b5SDimitry Andric }
2736*b5893f02SDimitry Andric
2737*b5893f02SDimitry Andric Optional<APInt>
SolveQuadraticEquationWrap(APInt A,APInt B,APInt C,unsigned RangeWidth)2738*b5893f02SDimitry Andric llvm::APIntOps::SolveQuadraticEquationWrap(APInt A, APInt B, APInt C,
2739*b5893f02SDimitry Andric unsigned RangeWidth) {
2740*b5893f02SDimitry Andric unsigned CoeffWidth = A.getBitWidth();
2741*b5893f02SDimitry Andric assert(CoeffWidth == B.getBitWidth() && CoeffWidth == C.getBitWidth());
2742*b5893f02SDimitry Andric assert(RangeWidth <= CoeffWidth &&
2743*b5893f02SDimitry Andric "Value range width should be less than coefficient width");
2744*b5893f02SDimitry Andric assert(RangeWidth > 1 && "Value range bit width should be > 1");
2745*b5893f02SDimitry Andric
2746*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": solving " << A << "x^2 + " << B
2747*b5893f02SDimitry Andric << "x + " << C << ", rw:" << RangeWidth << '\n');
2748*b5893f02SDimitry Andric
2749*b5893f02SDimitry Andric // Identify 0 as a (non)solution immediately.
2750*b5893f02SDimitry Andric if (C.sextOrTrunc(RangeWidth).isNullValue() ) {
2751*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": zero solution\n");
2752*b5893f02SDimitry Andric return APInt(CoeffWidth, 0);
2753*b5893f02SDimitry Andric }
2754*b5893f02SDimitry Andric
2755*b5893f02SDimitry Andric // The result of APInt arithmetic has the same bit width as the operands,
2756*b5893f02SDimitry Andric // so it can actually lose high bits. A product of two n-bit integers needs
2757*b5893f02SDimitry Andric // 2n-1 bits to represent the full value.
2758*b5893f02SDimitry Andric // The operation done below (on quadratic coefficients) that can produce
2759*b5893f02SDimitry Andric // the largest value is the evaluation of the equation during bisection,
2760*b5893f02SDimitry Andric // which needs 3 times the bitwidth of the coefficient, so the total number
2761*b5893f02SDimitry Andric // of required bits is 3n.
2762*b5893f02SDimitry Andric //
2763*b5893f02SDimitry Andric // The purpose of this extension is to simulate the set Z of all integers,
2764*b5893f02SDimitry Andric // where n+1 > n for all n in Z. In Z it makes sense to talk about positive
2765*b5893f02SDimitry Andric // and negative numbers (not so much in a modulo arithmetic). The method
2766*b5893f02SDimitry Andric // used to solve the equation is based on the standard formula for real
2767*b5893f02SDimitry Andric // numbers, and uses the concepts of "positive" and "negative" with their
2768*b5893f02SDimitry Andric // usual meanings.
2769*b5893f02SDimitry Andric CoeffWidth *= 3;
2770*b5893f02SDimitry Andric A = A.sext(CoeffWidth);
2771*b5893f02SDimitry Andric B = B.sext(CoeffWidth);
2772*b5893f02SDimitry Andric C = C.sext(CoeffWidth);
2773*b5893f02SDimitry Andric
2774*b5893f02SDimitry Andric // Make A > 0 for simplicity. Negate cannot overflow at this point because
2775*b5893f02SDimitry Andric // the bit width has increased.
2776*b5893f02SDimitry Andric if (A.isNegative()) {
2777*b5893f02SDimitry Andric A.negate();
2778*b5893f02SDimitry Andric B.negate();
2779*b5893f02SDimitry Andric C.negate();
2780*b5893f02SDimitry Andric }
2781*b5893f02SDimitry Andric
2782*b5893f02SDimitry Andric // Solving an equation q(x) = 0 with coefficients in modular arithmetic
2783*b5893f02SDimitry Andric // is really solving a set of equations q(x) = kR for k = 0, 1, 2, ...,
2784*b5893f02SDimitry Andric // and R = 2^BitWidth.
2785*b5893f02SDimitry Andric // Since we're trying not only to find exact solutions, but also values
2786*b5893f02SDimitry Andric // that "wrap around", such a set will always have a solution, i.e. an x
2787*b5893f02SDimitry Andric // that satisfies at least one of the equations, or such that |q(x)|
2788*b5893f02SDimitry Andric // exceeds kR, while |q(x-1)| for the same k does not.
2789*b5893f02SDimitry Andric //
2790*b5893f02SDimitry Andric // We need to find a value k, such that Ax^2 + Bx + C = kR will have a
2791*b5893f02SDimitry Andric // positive solution n (in the above sense), and also such that the n
2792*b5893f02SDimitry Andric // will be the least among all solutions corresponding to k = 0, 1, ...
2793*b5893f02SDimitry Andric // (more precisely, the least element in the set
2794*b5893f02SDimitry Andric // { n(k) | k is such that a solution n(k) exists }).
2795*b5893f02SDimitry Andric //
2796*b5893f02SDimitry Andric // Consider the parabola (over real numbers) that corresponds to the
2797*b5893f02SDimitry Andric // quadratic equation. Since A > 0, the arms of the parabola will point
2798*b5893f02SDimitry Andric // up. Picking different values of k will shift it up and down by R.
2799*b5893f02SDimitry Andric //
2800*b5893f02SDimitry Andric // We want to shift the parabola in such a way as to reduce the problem
2801*b5893f02SDimitry Andric // of solving q(x) = kR to solving shifted_q(x) = 0.
2802*b5893f02SDimitry Andric // (The interesting solutions are the ceilings of the real number
2803*b5893f02SDimitry Andric // solutions.)
2804*b5893f02SDimitry Andric APInt R = APInt::getOneBitSet(CoeffWidth, RangeWidth);
2805*b5893f02SDimitry Andric APInt TwoA = 2 * A;
2806*b5893f02SDimitry Andric APInt SqrB = B * B;
2807*b5893f02SDimitry Andric bool PickLow;
2808*b5893f02SDimitry Andric
2809*b5893f02SDimitry Andric auto RoundUp = [] (const APInt &V, const APInt &A) -> APInt {
2810*b5893f02SDimitry Andric assert(A.isStrictlyPositive());
2811*b5893f02SDimitry Andric APInt T = V.abs().urem(A);
2812*b5893f02SDimitry Andric if (T.isNullValue())
2813*b5893f02SDimitry Andric return V;
2814*b5893f02SDimitry Andric return V.isNegative() ? V+T : V+(A-T);
2815*b5893f02SDimitry Andric };
2816*b5893f02SDimitry Andric
2817*b5893f02SDimitry Andric // The vertex of the parabola is at -B/2A, but since A > 0, it's negative
2818*b5893f02SDimitry Andric // iff B is positive.
2819*b5893f02SDimitry Andric if (B.isNonNegative()) {
2820*b5893f02SDimitry Andric // If B >= 0, the vertex it at a negative location (or at 0), so in
2821*b5893f02SDimitry Andric // order to have a non-negative solution we need to pick k that makes
2822*b5893f02SDimitry Andric // C-kR negative. To satisfy all the requirements for the solution
2823*b5893f02SDimitry Andric // that we are looking for, it needs to be closest to 0 of all k.
2824*b5893f02SDimitry Andric C = C.srem(R);
2825*b5893f02SDimitry Andric if (C.isStrictlyPositive())
2826*b5893f02SDimitry Andric C -= R;
2827*b5893f02SDimitry Andric // Pick the greater solution.
2828*b5893f02SDimitry Andric PickLow = false;
2829*b5893f02SDimitry Andric } else {
2830*b5893f02SDimitry Andric // If B < 0, the vertex is at a positive location. For any solution
2831*b5893f02SDimitry Andric // to exist, the discriminant must be non-negative. This means that
2832*b5893f02SDimitry Andric // C-kR <= B^2/4A is a necessary condition for k, i.e. there is a
2833*b5893f02SDimitry Andric // lower bound on values of k: kR >= C - B^2/4A.
2834*b5893f02SDimitry Andric APInt LowkR = C - SqrB.udiv(2*TwoA); // udiv because all values > 0.
2835*b5893f02SDimitry Andric // Round LowkR up (towards +inf) to the nearest kR.
2836*b5893f02SDimitry Andric LowkR = RoundUp(LowkR, R);
2837*b5893f02SDimitry Andric
2838*b5893f02SDimitry Andric // If there exists k meeting the condition above, and such that
2839*b5893f02SDimitry Andric // C-kR > 0, there will be two positive real number solutions of
2840*b5893f02SDimitry Andric // q(x) = kR. Out of all such values of k, pick the one that makes
2841*b5893f02SDimitry Andric // C-kR closest to 0, (i.e. pick maximum k such that C-kR > 0).
2842*b5893f02SDimitry Andric // In other words, find maximum k such that LowkR <= kR < C.
2843*b5893f02SDimitry Andric if (C.sgt(LowkR)) {
2844*b5893f02SDimitry Andric // If LowkR < C, then such a k is guaranteed to exist because
2845*b5893f02SDimitry Andric // LowkR itself is a multiple of R.
2846*b5893f02SDimitry Andric C -= -RoundUp(-C, R); // C = C - RoundDown(C, R)
2847*b5893f02SDimitry Andric // Pick the smaller solution.
2848*b5893f02SDimitry Andric PickLow = true;
2849*b5893f02SDimitry Andric } else {
2850*b5893f02SDimitry Andric // If C-kR < 0 for all potential k's, it means that one solution
2851*b5893f02SDimitry Andric // will be negative, while the other will be positive. The positive
2852*b5893f02SDimitry Andric // solution will shift towards 0 if the parabola is moved up.
2853*b5893f02SDimitry Andric // Pick the kR closest to the lower bound (i.e. make C-kR closest
2854*b5893f02SDimitry Andric // to 0, or in other words, out of all parabolas that have solutions,
2855*b5893f02SDimitry Andric // pick the one that is the farthest "up").
2856*b5893f02SDimitry Andric // Since LowkR is itself a multiple of R, simply take C-LowkR.
2857*b5893f02SDimitry Andric C -= LowkR;
2858*b5893f02SDimitry Andric // Pick the greater solution.
2859*b5893f02SDimitry Andric PickLow = false;
2860*b5893f02SDimitry Andric }
2861*b5893f02SDimitry Andric }
2862*b5893f02SDimitry Andric
2863*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": updated coefficients " << A << "x^2 + "
2864*b5893f02SDimitry Andric << B << "x + " << C << ", rw:" << RangeWidth << '\n');
2865*b5893f02SDimitry Andric
2866*b5893f02SDimitry Andric APInt D = SqrB - 4*A*C;
2867*b5893f02SDimitry Andric assert(D.isNonNegative() && "Negative discriminant");
2868*b5893f02SDimitry Andric APInt SQ = D.sqrt();
2869*b5893f02SDimitry Andric
2870*b5893f02SDimitry Andric APInt Q = SQ * SQ;
2871*b5893f02SDimitry Andric bool InexactSQ = Q != D;
2872*b5893f02SDimitry Andric // The calculated SQ may actually be greater than the exact (non-integer)
2873*b5893f02SDimitry Andric // value. If that's the case, decremement SQ to get a value that is lower.
2874*b5893f02SDimitry Andric if (Q.sgt(D))
2875*b5893f02SDimitry Andric SQ -= 1;
2876*b5893f02SDimitry Andric
2877*b5893f02SDimitry Andric APInt X;
2878*b5893f02SDimitry Andric APInt Rem;
2879*b5893f02SDimitry Andric
2880*b5893f02SDimitry Andric // SQ is rounded down (i.e SQ * SQ <= D), so the roots may be inexact.
2881*b5893f02SDimitry Andric // When using the quadratic formula directly, the calculated low root
2882*b5893f02SDimitry Andric // may be greater than the exact one, since we would be subtracting SQ.
2883*b5893f02SDimitry Andric // To make sure that the calculated root is not greater than the exact
2884*b5893f02SDimitry Andric // one, subtract SQ+1 when calculating the low root (for inexact value
2885*b5893f02SDimitry Andric // of SQ).
2886*b5893f02SDimitry Andric if (PickLow)
2887*b5893f02SDimitry Andric APInt::sdivrem(-B - (SQ+InexactSQ), TwoA, X, Rem);
2888*b5893f02SDimitry Andric else
2889*b5893f02SDimitry Andric APInt::sdivrem(-B + SQ, TwoA, X, Rem);
2890*b5893f02SDimitry Andric
2891*b5893f02SDimitry Andric // The updated coefficients should be such that the (exact) solution is
2892*b5893f02SDimitry Andric // positive. Since APInt division rounds towards 0, the calculated one
2893*b5893f02SDimitry Andric // can be 0, but cannot be negative.
2894*b5893f02SDimitry Andric assert(X.isNonNegative() && "Solution should be non-negative");
2895*b5893f02SDimitry Andric
2896*b5893f02SDimitry Andric if (!InexactSQ && Rem.isNullValue()) {
2897*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": solution (root): " << X << '\n');
2898*b5893f02SDimitry Andric return X;
2899*b5893f02SDimitry Andric }
2900*b5893f02SDimitry Andric
2901*b5893f02SDimitry Andric assert((SQ*SQ).sle(D) && "SQ = |_sqrt(D)_|, so SQ*SQ <= D");
2902*b5893f02SDimitry Andric // The exact value of the square root of D should be between SQ and SQ+1.
2903*b5893f02SDimitry Andric // This implies that the solution should be between that corresponding to
2904*b5893f02SDimitry Andric // SQ (i.e. X) and that corresponding to SQ+1.
2905*b5893f02SDimitry Andric //
2906*b5893f02SDimitry Andric // The calculated X cannot be greater than the exact (real) solution.
2907*b5893f02SDimitry Andric // Actually it must be strictly less than the exact solution, while
2908*b5893f02SDimitry Andric // X+1 will be greater than or equal to it.
2909*b5893f02SDimitry Andric
2910*b5893f02SDimitry Andric APInt VX = (A*X + B)*X + C;
2911*b5893f02SDimitry Andric APInt VY = VX + TwoA*X + A + B;
2912*b5893f02SDimitry Andric bool SignChange = VX.isNegative() != VY.isNegative() ||
2913*b5893f02SDimitry Andric VX.isNullValue() != VY.isNullValue();
2914*b5893f02SDimitry Andric // If the sign did not change between X and X+1, X is not a valid solution.
2915*b5893f02SDimitry Andric // This could happen when the actual (exact) roots don't have an integer
2916*b5893f02SDimitry Andric // between them, so they would both be contained between X and X+1.
2917*b5893f02SDimitry Andric if (!SignChange) {
2918*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": no valid solution\n");
2919*b5893f02SDimitry Andric return None;
2920*b5893f02SDimitry Andric }
2921*b5893f02SDimitry Andric
2922*b5893f02SDimitry Andric X += 1;
2923*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": solution (wrap): " << X << '\n');
2924*b5893f02SDimitry Andric return X;
2925*b5893f02SDimitry Andric }
2926