00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef LLVM_APINT_H
00016 #define LLVM_APINT_H
00017
00018 #include "llvm/ADT/ArrayRef.h"
00019 #include "llvm/Support/MathExtras.h"
00020 #include <cassert>
00021 #include <climits>
00022 #include <cstring>
00023 #include <string>
00024
00025 namespace llvm {
00026 class Serializer;
00027 class Deserializer;
00028 class FoldingSetNodeID;
00029 class StringRef;
00030
00031 template<typename T>
00032 class SmallVectorImpl;
00033
00034
00035
00036 typedef uint64_t integerPart;
00037
00038 const unsigned int host_char_bit = 8;
00039 const unsigned int integerPartWidth = host_char_bit *
00040 static_cast<unsigned int>(sizeof(integerPart));
00041
00042
00043
00044
00045
00072 class APInt {
00073 unsigned BitWidth;
00074
00077 union {
00078 uint64_t VAL;
00079 uint64_t *pVal;
00080 };
00081
00083 enum {
00085 APINT_BITS_PER_WORD = static_cast<unsigned int>(sizeof(uint64_t)) *
00086 CHAR_BIT,
00088 APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
00089 };
00090
00094 APInt(uint64_t* val, unsigned bits) : BitWidth(bits), pVal(val) { }
00095
00098 bool isSingleWord() const {
00099 return BitWidth <= APINT_BITS_PER_WORD;
00100 }
00101
00104 static unsigned whichWord(unsigned bitPosition) {
00105 return bitPosition / APINT_BITS_PER_WORD;
00106 }
00107
00111 static unsigned whichBit(unsigned bitPosition) {
00112 return bitPosition % APINT_BITS_PER_WORD;
00113 }
00114
00120 static uint64_t maskBit(unsigned bitPosition) {
00121 return 1ULL << whichBit(bitPosition);
00122 }
00123
00129 APInt& clearUnusedBits() {
00130
00131 unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
00132 if (wordBits == 0)
00133
00134
00135
00136 return *this;
00137
00138
00139 uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
00140 if (isSingleWord())
00141 VAL &= mask;
00142 else
00143 pVal[getNumWords() - 1] &= mask;
00144 return *this;
00145 }
00146
00149 uint64_t getWord(unsigned bitPosition) const {
00150 return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
00151 }
00152
00165 void fromString(unsigned numBits, StringRef str, uint8_t radix);
00166
00172 static void divide(const APInt LHS, unsigned lhsWords,
00173 const APInt &RHS, unsigned rhsWords,
00174 APInt *Quotient, APInt *Remainder);
00175
00177 void initSlowCase(unsigned numBits, uint64_t val, bool isSigned);
00178
00180 void initFromArray(ArrayRef<uint64_t> array);
00181
00183 void initSlowCase(const APInt& that);
00184
00186 APInt shlSlowCase(unsigned shiftAmt) const;
00187
00189 APInt AndSlowCase(const APInt& RHS) const;
00190
00192 APInt OrSlowCase(const APInt& RHS) const;
00193
00195 APInt XorSlowCase(const APInt& RHS) const;
00196
00198 APInt& AssignSlowCase(const APInt& RHS);
00199
00201 bool EqualSlowCase(const APInt& RHS) const;
00202
00204 bool EqualSlowCase(uint64_t Val) const;
00205
00207 unsigned countLeadingZerosSlowCase() const;
00208
00210 unsigned countTrailingOnesSlowCase() const;
00211
00213 unsigned countPopulationSlowCase() const;
00214
00215 public:
00226 APInt(unsigned numBits, uint64_t val, bool isSigned = false)
00227 : BitWidth(numBits), VAL(0) {
00228 assert(BitWidth && "bitwidth too small");
00229 if (isSingleWord())
00230 VAL = val;
00231 else
00232 initSlowCase(numBits, val, isSigned);
00233 clearUnusedBits();
00234 }
00235
00241 APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
00249 APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
00250
00261 APInt(unsigned numBits, StringRef str, uint8_t radix);
00262
00265 APInt(const APInt& that)
00266 : BitWidth(that.BitWidth), VAL(0) {
00267 assert(BitWidth && "bitwidth too small");
00268 if (isSingleWord())
00269 VAL = that.VAL;
00270 else
00271 initSlowCase(that);
00272 }
00273
00275 ~APInt() {
00276 if (!isSingleWord())
00277 delete [] pVal;
00278 }
00279
00282 explicit APInt() : BitWidth(1) {}
00283
00286 void Profile(FoldingSetNodeID& id) const;
00287
00294 bool isNegative() const {
00295 return (*this)[BitWidth - 1];
00296 }
00297
00300 bool isNonNegative() const {
00301 return !isNegative();
00302 }
00303
00308 bool isStrictlyPositive() const {
00309 return isNonNegative() && !!*this;
00310 }
00311
00314 bool isAllOnesValue() const {
00315 return countPopulation() == BitWidth;
00316 }
00317
00321 bool isMaxValue() const {
00322 return countPopulation() == BitWidth;
00323 }
00324
00328 bool isMaxSignedValue() const {
00329 return BitWidth == 1 ? VAL == 0 :
00330 !isNegative() && countPopulation() == BitWidth - 1;
00331 }
00332
00336 bool isMinValue() const {
00337 return !*this;
00338 }
00339
00343 bool isMinSignedValue() const {
00344 return BitWidth == 1 ? VAL == 1 : isNegative() && isPowerOf2();
00345 }
00346
00348 bool isIntN(unsigned N) const {
00349 assert(N && "N == 0 ???");
00350 if (N >= getBitWidth())
00351 return true;
00352
00353 if (isSingleWord())
00354 return isUIntN(N, VAL);
00355 return APInt(N, makeArrayRef(pVal, getNumWords())).zext(getBitWidth())
00356 == (*this);
00357 }
00358
00360 bool isSignedIntN(unsigned N) const {
00361 assert(N && "N == 0 ???");
00362 return getMinSignedBits() <= N;
00363 }
00364
00366 bool isPowerOf2() const {
00367 if (isSingleWord())
00368 return isPowerOf2_64(VAL);
00369 return countPopulationSlowCase() == 1;
00370 }
00371
00373 bool isSignBit() const { return isMinSignedValue(); }
00374
00377 bool getBoolValue() const {
00378 return !!*this;
00379 }
00380
00384 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
00385 return (getActiveBits() > 64 || getZExtValue() > Limit) ?
00386 Limit : getZExtValue();
00387 }
00388
00393 static APInt getMaxValue(unsigned numBits) {
00394 return getAllOnesValue(numBits);
00395 }
00396
00398 static APInt getSignedMaxValue(unsigned numBits) {
00399 APInt API = getAllOnesValue(numBits);
00400 API.clearBit(numBits - 1);
00401 return API;
00402 }
00403
00405 static APInt getMinValue(unsigned numBits) {
00406 return APInt(numBits, 0);
00407 }
00408
00410 static APInt getSignedMinValue(unsigned numBits) {
00411 APInt API(numBits, 0);
00412 API.setBit(numBits - 1);
00413 return API;
00414 }
00415
00419 static APInt getSignBit(unsigned BitWidth) {
00420 return getSignedMinValue(BitWidth);
00421 }
00422
00425 static APInt getAllOnesValue(unsigned numBits) {
00426 return APInt(numBits, -1ULL, true);
00427 }
00428
00431 static APInt getNullValue(unsigned numBits) {
00432 return APInt(numBits, 0);
00433 }
00434
00438 APInt getHiBits(unsigned numBits) const;
00439
00443 APInt getLoBits(unsigned numBits) const;
00444
00446 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
00447 APInt Res(numBits, 0);
00448 Res.setBit(BitNo);
00449 return Res;
00450 }
00451
00462 static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
00463 assert(hiBit <= numBits && "hiBit out of range");
00464 assert(loBit < numBits && "loBit out of range");
00465 if (hiBit < loBit)
00466 return getLowBitsSet(numBits, hiBit) |
00467 getHighBitsSet(numBits, numBits-loBit);
00468 return getLowBitsSet(numBits, hiBit-loBit).shl(loBit);
00469 }
00470
00475 static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
00476 assert(hiBitsSet <= numBits && "Too many bits to set!");
00477
00478 if (hiBitsSet == 0)
00479 return APInt(numBits, 0);
00480 unsigned shiftAmt = numBits - hiBitsSet;
00481
00482 if (numBits <= APINT_BITS_PER_WORD)
00483 return APInt(numBits, ~0ULL << shiftAmt);
00484 return getAllOnesValue(numBits).shl(shiftAmt);
00485 }
00486
00491 static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
00492 assert(loBitsSet <= numBits && "Too many bits to set!");
00493
00494 if (loBitsSet == 0)
00495 return APInt(numBits, 0);
00496 if (loBitsSet == APINT_BITS_PER_WORD)
00497 return APInt(numBits, -1ULL);
00498
00499 if (numBits < APINT_BITS_PER_WORD)
00500 return APInt(numBits, (1ULL << loBitsSet) - 1);
00501 return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
00502 }
00503
00507 uint64_t getHashValue() const;
00508
00512 const uint64_t* getRawData() const {
00513 if (isSingleWord())
00514 return &VAL;
00515 return &pVal[0];
00516 }
00517
00523 const APInt operator++(int) {
00524 APInt API(*this);
00525 ++(*this);
00526 return API;
00527 }
00528
00531 APInt& operator++();
00532
00535 const APInt operator--(int) {
00536 APInt API(*this);
00537 --(*this);
00538 return API;
00539 }
00540
00543 APInt& operator--();
00544
00548 APInt operator~() const {
00549 APInt Result(*this);
00550 Result.flipAllBits();
00551 return Result;
00552 }
00553
00557 APInt operator-() const {
00558 return APInt(BitWidth, 0) - (*this);
00559 }
00560
00564 bool operator!() const;
00565
00571 APInt& operator=(const APInt& RHS) {
00572
00573 if (isSingleWord() && RHS.isSingleWord()) {
00574 VAL = RHS.VAL;
00575 BitWidth = RHS.BitWidth;
00576 return clearUnusedBits();
00577 }
00578
00579 return AssignSlowCase(RHS);
00580 }
00581
00587 APInt& operator=(uint64_t RHS);
00588
00593 APInt& operator&=(const APInt& RHS);
00594
00599 APInt& operator|=(const APInt& RHS);
00600
00606 APInt& operator|=(uint64_t RHS) {
00607 if (isSingleWord()) {
00608 VAL |= RHS;
00609 clearUnusedBits();
00610 } else {
00611 pVal[0] |= RHS;
00612 }
00613 return *this;
00614 }
00615
00620 APInt& operator^=(const APInt& RHS);
00621
00625 APInt& operator*=(const APInt& RHS);
00626
00630 APInt& operator+=(const APInt& RHS);
00631
00635 APInt& operator-=(const APInt& RHS);
00636
00640 APInt& operator<<=(unsigned shiftAmt) {
00641 *this = shl(shiftAmt);
00642 return *this;
00643 }
00644
00651 APInt operator&(const APInt& RHS) const {
00652 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00653 if (isSingleWord())
00654 return APInt(getBitWidth(), VAL & RHS.VAL);
00655 return AndSlowCase(RHS);
00656 }
00657 APInt And(const APInt& RHS) const {
00658 return this->operator&(RHS);
00659 }
00660
00664 APInt operator|(const APInt& RHS) const {
00665 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00666 if (isSingleWord())
00667 return APInt(getBitWidth(), VAL | RHS.VAL);
00668 return OrSlowCase(RHS);
00669 }
00670 APInt Or(const APInt& RHS) const {
00671 return this->operator|(RHS);
00672 }
00673
00677 APInt operator^(const APInt& RHS) const {
00678 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
00679 if (isSingleWord())
00680 return APInt(BitWidth, VAL ^ RHS.VAL);
00681 return XorSlowCase(RHS);
00682 }
00683 APInt Xor(const APInt& RHS) const {
00684 return this->operator^(RHS);
00685 }
00686
00689 APInt operator*(const APInt& RHS) const;
00690
00693 APInt operator+(const APInt& RHS) const;
00694 APInt operator+(uint64_t RHS) const {
00695 return (*this) + APInt(BitWidth, RHS);
00696 }
00697
00700 APInt operator-(const APInt& RHS) const;
00701 APInt operator-(uint64_t RHS) const {
00702 return (*this) - APInt(BitWidth, RHS);
00703 }
00704
00705 APInt operator<<(unsigned Bits) const {
00706 return shl(Bits);
00707 }
00708
00709 APInt operator<<(const APInt &Bits) const {
00710 return shl(Bits);
00711 }
00712
00715 APInt ashr(unsigned shiftAmt) const;
00716
00719 APInt lshr(unsigned shiftAmt) const;
00720
00723 APInt shl(unsigned shiftAmt) const {
00724 assert(shiftAmt <= BitWidth && "Invalid shift amount");
00725 if (isSingleWord()) {
00726 if (shiftAmt == BitWidth)
00727 return APInt(BitWidth, 0);
00728 return APInt(BitWidth, VAL << shiftAmt);
00729 }
00730 return shlSlowCase(shiftAmt);
00731 }
00732
00734 APInt rotl(unsigned rotateAmt) const;
00735
00737 APInt rotr(unsigned rotateAmt) const;
00738
00741 APInt ashr(const APInt &shiftAmt) const;
00742
00745 APInt lshr(const APInt &shiftAmt) const;
00746
00749 APInt shl(const APInt &shiftAmt) const;
00750
00752 APInt rotl(const APInt &rotateAmt) const;
00753
00755 APInt rotr(const APInt &rotateAmt) const;
00756
00761 APInt udiv(const APInt &RHS) const;
00762
00765 APInt sdiv(const APInt &RHS) const {
00766 if (isNegative())
00767 if (RHS.isNegative())
00768 return (-(*this)).udiv(-RHS);
00769 else
00770 return -((-(*this)).udiv(RHS));
00771 else if (RHS.isNegative())
00772 return -(this->udiv(-RHS));
00773 return this->udiv(RHS);
00774 }
00775
00783 APInt urem(const APInt &RHS) const;
00784
00787 APInt srem(const APInt &RHS) const {
00788 if (isNegative())
00789 if (RHS.isNegative())
00790 return -((-(*this)).urem(-RHS));
00791 else
00792 return -((-(*this)).urem(RHS));
00793 else if (RHS.isNegative())
00794 return this->urem(-RHS);
00795 return this->urem(RHS);
00796 }
00797
00804 static void udivrem(const APInt &LHS, const APInt &RHS,
00805 APInt &Quotient, APInt &Remainder);
00806
00807 static void sdivrem(const APInt &LHS, const APInt &RHS,
00808 APInt &Quotient, APInt &Remainder) {
00809 if (LHS.isNegative()) {
00810 if (RHS.isNegative())
00811 APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
00812 else
00813 APInt::udivrem(-LHS, RHS, Quotient, Remainder);
00814 Quotient = -Quotient;
00815 Remainder = -Remainder;
00816 } else if (RHS.isNegative()) {
00817 APInt::udivrem(LHS, -RHS, Quotient, Remainder);
00818 Quotient = -Quotient;
00819 } else {
00820 APInt::udivrem(LHS, RHS, Quotient, Remainder);
00821 }
00822 }
00823
00824
00825
00826 APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
00827 APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
00828 APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
00829 APInt usub_ov(const APInt &RHS, bool &Overflow) const;
00830 APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
00831 APInt smul_ov(const APInt &RHS, bool &Overflow) const;
00832 APInt umul_ov(const APInt &RHS, bool &Overflow) const;
00833 APInt sshl_ov(unsigned Amt, bool &Overflow) const;
00834
00837 bool operator[](unsigned bitPosition) const;
00838
00845 bool operator==(const APInt& RHS) const {
00846 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
00847 if (isSingleWord())
00848 return VAL == RHS.VAL;
00849 return EqualSlowCase(RHS);
00850 }
00851
00856 bool operator==(uint64_t Val) const {
00857 if (isSingleWord())
00858 return VAL == Val;
00859 return EqualSlowCase(Val);
00860 }
00861
00866 bool eq(const APInt &RHS) const {
00867 return (*this) == RHS;
00868 }
00869
00874 bool operator!=(const APInt& RHS) const {
00875 return !((*this) == RHS);
00876 }
00877
00882 bool operator!=(uint64_t Val) const {
00883 return !((*this) == Val);
00884 }
00885
00890 bool ne(const APInt &RHS) const {
00891 return !((*this) == RHS);
00892 }
00893
00898 bool ult(const APInt &RHS) const;
00899
00904 bool ult(uint64_t RHS) const {
00905 return ult(APInt(getBitWidth(), RHS));
00906 }
00907
00912 bool slt(const APInt& RHS) const;
00913
00918 bool slt(uint64_t RHS) const {
00919 return slt(APInt(getBitWidth(), RHS));
00920 }
00921
00926 bool ule(const APInt& RHS) const {
00927 return ult(RHS) || eq(RHS);
00928 }
00929
00934 bool ule(uint64_t RHS) const {
00935 return ule(APInt(getBitWidth(), RHS));
00936 }
00937
00942 bool sle(const APInt& RHS) const {
00943 return slt(RHS) || eq(RHS);
00944 }
00945
00950 bool sle(uint64_t RHS) const {
00951 return sle(APInt(getBitWidth(), RHS));
00952 }
00953
00958 bool ugt(const APInt& RHS) const {
00959 return !ult(RHS) && !eq(RHS);
00960 }
00961
00966 bool ugt(uint64_t RHS) const {
00967 return ugt(APInt(getBitWidth(), RHS));
00968 }
00969
00974 bool sgt(const APInt& RHS) const {
00975 return !slt(RHS) && !eq(RHS);
00976 }
00977
00982 bool sgt(uint64_t RHS) const {
00983 return sgt(APInt(getBitWidth(), RHS));
00984 }
00985
00990 bool uge(const APInt& RHS) const {
00991 return !ult(RHS);
00992 }
00993
00998 bool uge(uint64_t RHS) const {
00999 return uge(APInt(getBitWidth(), RHS));
01000 }
01001
01006 bool sge(const APInt& RHS) const {
01007 return !slt(RHS);
01008 }
01009
01014 bool sge(uint64_t RHS) const {
01015 return sge(APInt(getBitWidth(), RHS));
01016 }
01017
01018
01019
01020
01023 bool intersects(const APInt &RHS) const {
01024 return (*this & RHS) != 0;
01025 }
01026
01033 APInt trunc(unsigned width) const;
01034
01040 APInt sext(unsigned width) const;
01041
01046 APInt zext(unsigned width) const;
01047
01051 APInt sextOrTrunc(unsigned width) const;
01052
01056 APInt zextOrTrunc(unsigned width) const;
01057
01062 void setAllBits() {
01063 if (isSingleWord())
01064 VAL = -1ULL;
01065 else {
01066
01067 for (unsigned i = 0; i < getNumWords(); ++i)
01068 pVal[i] = -1ULL;
01069 }
01070
01071 clearUnusedBits();
01072 }
01073
01076 void setBit(unsigned bitPosition);
01077
01079 void clearAllBits() {
01080 if (isSingleWord())
01081 VAL = 0;
01082 else
01083 memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
01084 }
01085
01088 void clearBit(unsigned bitPosition);
01089
01091 void flipAllBits() {
01092 if (isSingleWord())
01093 VAL ^= -1ULL;
01094 else {
01095 for (unsigned i = 0; i < getNumWords(); ++i)
01096 pVal[i] ^= -1ULL;
01097 }
01098 clearUnusedBits();
01099 }
01100
01104 void flipBit(unsigned bitPosition);
01105
01109
01111 unsigned getBitWidth() const {
01112 return BitWidth;
01113 }
01114
01118 unsigned getNumWords() const {
01119 return getNumWords(BitWidth);
01120 }
01121
01126 static unsigned getNumWords(unsigned BitWidth) {
01127 return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
01128 }
01129
01134 unsigned getActiveBits() const {
01135 return BitWidth - countLeadingZeros();
01136 }
01137
01141 unsigned getActiveWords() const {
01142 return whichWord(getActiveBits()-1) + 1;
01143 }
01144
01152 unsigned getMinSignedBits() const {
01153 if (isNegative())
01154 return BitWidth - countLeadingOnes() + 1;
01155 return getActiveBits()+1;
01156 }
01157
01162 uint64_t getZExtValue() const {
01163 if (isSingleWord())
01164 return VAL;
01165 assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
01166 return pVal[0];
01167 }
01168
01173 int64_t getSExtValue() const {
01174 if (isSingleWord())
01175 return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
01176 (APINT_BITS_PER_WORD - BitWidth);
01177 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
01178 return int64_t(pVal[0]);
01179 }
01180
01184 static unsigned getBitsNeeded(StringRef str, uint8_t radix);
01185
01192 unsigned countLeadingZeros() const {
01193 if (isSingleWord()) {
01194 unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
01195 return CountLeadingZeros_64(VAL) - unusedBits;
01196 }
01197 return countLeadingZerosSlowCase();
01198 }
01199
01206 unsigned countLeadingOnes() const;
01207
01210 unsigned getNumSignBits() const {
01211 return isNegative() ? countLeadingOnes() : countLeadingZeros();
01212 }
01213
01221 unsigned countTrailingZeros() const;
01222
01230 unsigned countTrailingOnes() const {
01231 if (isSingleWord())
01232 return CountTrailingOnes_64(VAL);
01233 return countTrailingOnesSlowCase();
01234 }
01235
01242 unsigned countPopulation() const {
01243 if (isSingleWord())
01244 return CountPopulation_64(VAL);
01245 return countPopulationSlowCase();
01246 }
01247
01251 void print(std::ostream &OS, bool isSigned) const;
01252
01255 void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
01256 bool formatAsCLiteral = false) const;
01257
01260 void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
01261 toString(Str, Radix, false, false);
01262 }
01263
01266 void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
01267 toString(Str, Radix, true, false);
01268 }
01269
01273 std::string toString(unsigned Radix, bool Signed) const;
01274
01275
01277 APInt byteSwap() const;
01278
01280 double roundToDouble(bool isSigned) const;
01281
01283 double roundToDouble() const {
01284 return roundToDouble(false);
01285 }
01286
01288 double signedRoundToDouble() const {
01289 return roundToDouble(true);
01290 }
01291
01296 double bitsToDouble() const {
01297 union {
01298 uint64_t I;
01299 double D;
01300 } T;
01301 T.I = (isSingleWord() ? VAL : pVal[0]);
01302 return T.D;
01303 }
01304
01309 float bitsToFloat() const {
01310 union {
01311 unsigned I;
01312 float F;
01313 } T;
01314 T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
01315 return T.F;
01316 }
01317
01321 static APInt doubleToBits(double V) {
01322 union {
01323 uint64_t I;
01324 double D;
01325 } T;
01326 T.D = V;
01327 return APInt(sizeof T * CHAR_BIT, T.I);
01328 }
01329
01333 static APInt floatToBits(float V) {
01334 union {
01335 unsigned I;
01336 float F;
01337 } T;
01338 T.F = V;
01339 return APInt(sizeof T * CHAR_BIT, T.I);
01340 }
01341
01345
01347 unsigned logBase2() const {
01348 return BitWidth - 1 - countLeadingZeros();
01349 }
01350
01352 unsigned ceilLogBase2() const {
01353 return BitWidth - (*this - 1).countLeadingZeros();
01354 }
01355
01358 int32_t exactLogBase2() const {
01359 if (!isPowerOf2())
01360 return -1;
01361 return logBase2();
01362 }
01363
01365 APInt sqrt() const;
01366
01369 APInt abs() const {
01370 if (isNegative())
01371 return -(*this);
01372 return *this;
01373 }
01374
01376 APInt multiplicativeInverse(const APInt& modulo) const;
01377
01381
01383 struct ms;
01384 ms magic() const;
01385
01387 struct mu;
01388 mu magicu(unsigned LeadingZeros = 0) const;
01389
01393
01394
01395
01396
01397
01398
01399
01400
01403 static void tcSet(integerPart *, integerPart, unsigned int);
01404
01406 static void tcAssign(integerPart *, const integerPart *, unsigned int);
01407
01409 static bool tcIsZero(const integerPart *, unsigned int);
01410
01412 static int tcExtractBit(const integerPart *, unsigned int bit);
01413
01418 static void tcExtract(integerPart *, unsigned int dstCount,
01419 const integerPart *,
01420 unsigned int srcBits, unsigned int srcLSB);
01421
01423 static void tcSetBit(integerPart *, unsigned int bit);
01424
01426 static void tcClearBit(integerPart *, unsigned int bit);
01427
01431 static unsigned int tcLSB(const integerPart *, unsigned int);
01432 static unsigned int tcMSB(const integerPart *parts, unsigned int n);
01433
01435 static void tcNegate(integerPart *, unsigned int);
01436
01439 static integerPart tcAdd(integerPart *, const integerPart *,
01440 integerPart carry, unsigned);
01441
01444 static integerPart tcSubtract(integerPart *, const integerPart *,
01445 integerPart carry, unsigned);
01446
01458 static int tcMultiplyPart(integerPart *dst, const integerPart *src,
01459 integerPart multiplier, integerPart carry,
01460 unsigned int srcParts, unsigned int dstParts,
01461 bool add);
01462
01467 static int tcMultiply(integerPart *, const integerPart *,
01468 const integerPart *, unsigned);
01469
01474 static unsigned int tcFullMultiply(integerPart *, const integerPart *,
01475 const integerPart *, unsigned, unsigned);
01476
01487 static int tcDivide(integerPart *lhs, const integerPart *rhs,
01488 integerPart *remainder, integerPart *scratch,
01489 unsigned int parts);
01490
01493 static void tcShiftLeft(integerPart *, unsigned int parts,
01494 unsigned int count);
01495
01498 static void tcShiftRight(integerPart *, unsigned int parts,
01499 unsigned int count);
01500
01502 static void tcAnd(integerPart *, const integerPart *, unsigned int);
01503 static void tcOr(integerPart *, const integerPart *, unsigned int);
01504 static void tcXor(integerPart *, const integerPart *, unsigned int);
01505 static void tcComplement(integerPart *, unsigned int);
01506
01508 static int tcCompare(const integerPart *, const integerPart *,
01509 unsigned int);
01510
01512 static integerPart tcIncrement(integerPart *, unsigned int);
01513
01515 static void tcSetLeastSignificantBits(integerPart *, unsigned int,
01516 unsigned int bits);
01517
01519 void dump() const;
01520
01522 };
01523
01525 struct APInt::ms {
01526 APInt m;
01527 unsigned s;
01528 };
01529
01531 struct APInt::mu {
01532 APInt m;
01533 bool a;
01534 unsigned s;
01535 };
01536
01537 inline bool operator==(uint64_t V1, const APInt& V2) {
01538 return V2 == V1;
01539 }
01540
01541 inline bool operator!=(uint64_t V1, const APInt& V2) {
01542 return V2 != V1;
01543 }
01544
01545 inline std::ostream &operator<<(std::ostream &OS, const APInt &I) {
01546 I.print(OS, true);
01547 return OS;
01548 }
01549
01550 namespace APIntOps {
01551
01553 inline APInt smin(const APInt &A, const APInt &B) {
01554 return A.slt(B) ? A : B;
01555 }
01556
01558 inline APInt smax(const APInt &A, const APInt &B) {
01559 return A.sgt(B) ? A : B;
01560 }
01561
01563 inline APInt umin(const APInt &A, const APInt &B) {
01564 return A.ult(B) ? A : B;
01565 }
01566
01568 inline APInt umax(const APInt &A, const APInt &B) {
01569 return A.ugt(B) ? A : B;
01570 }
01571
01573 inline bool isIntN(unsigned N, const APInt& APIVal) {
01574 return APIVal.isIntN(N);
01575 }
01576
01578 inline bool isSignedIntN(unsigned N, const APInt& APIVal) {
01579 return APIVal.isSignedIntN(N);
01580 }
01581
01584 inline bool isMask(unsigned numBits, const APInt& APIVal) {
01585 return numBits <= APIVal.getBitWidth() &&
01586 APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
01587 }
01588
01591 inline bool isShiftedMask(unsigned numBits, const APInt& APIVal) {
01592 return isMask(numBits, (APIVal - APInt(numBits,1)) | APIVal);
01593 }
01594
01596 inline APInt byteSwap(const APInt& APIVal) {
01597 return APIVal.byteSwap();
01598 }
01599
01601 inline unsigned logBase2(const APInt& APIVal) {
01602 return APIVal.logBase2();
01603 }
01604
01609 APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
01610
01613 inline double RoundAPIntToDouble(const APInt& APIVal) {
01614 return APIVal.roundToDouble();
01615 }
01616
01619 inline double RoundSignedAPIntToDouble(const APInt& APIVal) {
01620 return APIVal.signedRoundToDouble();
01621 }
01622
01624 inline float RoundAPIntToFloat(const APInt& APIVal) {
01625 return float(RoundAPIntToDouble(APIVal));
01626 }
01627
01630 inline float RoundSignedAPIntToFloat(const APInt& APIVal) {
01631 return float(APIVal.signedRoundToDouble());
01632 }
01633
01636 APInt RoundDoubleToAPInt(double Double, unsigned width);
01637
01640 inline APInt RoundFloatToAPInt(float Float, unsigned width) {
01641 return RoundDoubleToAPInt(double(Float), width);
01642 }
01643
01646 inline APInt ashr(const APInt& LHS, unsigned shiftAmt) {
01647 return LHS.ashr(shiftAmt);
01648 }
01649
01652 inline APInt lshr(const APInt& LHS, unsigned shiftAmt) {
01653 return LHS.lshr(shiftAmt);
01654 }
01655
01658 inline APInt shl(const APInt& LHS, unsigned shiftAmt) {
01659 return LHS.shl(shiftAmt);
01660 }
01661
01664 inline APInt sdiv(const APInt& LHS, const APInt& RHS) {
01665 return LHS.sdiv(RHS);
01666 }
01667
01670 inline APInt udiv(const APInt& LHS, const APInt& RHS) {
01671 return LHS.udiv(RHS);
01672 }
01673
01676 inline APInt srem(const APInt& LHS, const APInt& RHS) {
01677 return LHS.srem(RHS);
01678 }
01679
01682 inline APInt urem(const APInt& LHS, const APInt& RHS) {
01683 return LHS.urem(RHS);
01684 }
01685
01688 inline APInt mul(const APInt& LHS, const APInt& RHS) {
01689 return LHS * RHS;
01690 }
01691
01694 inline APInt add(const APInt& LHS, const APInt& RHS) {
01695 return LHS + RHS;
01696 }
01697
01700 inline APInt sub(const APInt& LHS, const APInt& RHS) {
01701 return LHS - RHS;
01702 }
01703
01707 inline APInt And(const APInt& LHS, const APInt& RHS) {
01708 return LHS & RHS;
01709 }
01710
01713 inline APInt Or(const APInt& LHS, const APInt& RHS) {
01714 return LHS | RHS;
01715 }
01716
01719 inline APInt Xor(const APInt& LHS, const APInt& RHS) {
01720 return LHS ^ RHS;
01721 }
01722
01725 inline APInt Not(const APInt& APIVal) {
01726 return ~APIVal;
01727 }
01728
01729 }
01730
01731 }
01732
01733 #endif