|
|
|||
File indexing completed on 2026-05-10 08:43:02
0001 //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 /// 0009 /// \file 0010 /// This file implements a class to represent arbitrary precision 0011 /// integral constant values and operations on them. 0012 /// 0013 //===----------------------------------------------------------------------===// 0014 0015 #ifndef LLVM_ADT_APINT_H 0016 #define LLVM_ADT_APINT_H 0017 0018 #include "llvm/Support/Compiler.h" 0019 #include "llvm/Support/MathExtras.h" 0020 #include "llvm/Support/float128.h" 0021 #include <cassert> 0022 #include <climits> 0023 #include <cstring> 0024 #include <optional> 0025 #include <utility> 0026 0027 namespace llvm { 0028 class FoldingSetNodeID; 0029 class StringRef; 0030 class hash_code; 0031 class raw_ostream; 0032 struct Align; 0033 class DynamicAPInt; 0034 0035 template <typename T> class SmallVectorImpl; 0036 template <typename T> class ArrayRef; 0037 template <typename T, typename Enable> struct DenseMapInfo; 0038 0039 class APInt; 0040 0041 inline APInt operator-(APInt); 0042 0043 //===----------------------------------------------------------------------===// 0044 // APInt Class 0045 //===----------------------------------------------------------------------===// 0046 0047 /// Class for arbitrary precision integers. 0048 /// 0049 /// APInt is a functional replacement for common case unsigned integer type like 0050 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width 0051 /// integer sizes and large integer value types such as 3-bits, 15-bits, or more 0052 /// than 64-bits of precision. APInt provides a variety of arithmetic operators 0053 /// and methods to manipulate integer values of any bit-width. It supports both 0054 /// the typical integer arithmetic and comparison operations as well as bitwise 0055 /// manipulation. 0056 /// 0057 /// The class has several invariants worth noting: 0058 /// * All bit, byte, and word positions are zero-based. 0059 /// * Once the bit width is set, it doesn't change except by the Truncate, 0060 /// SignExtend, or ZeroExtend operations. 0061 /// * All binary operators must be on APInt instances of the same bit width. 0062 /// Attempting to use these operators on instances with different bit 0063 /// widths will yield an assertion. 0064 /// * The value is stored canonically as an unsigned value. For operations 0065 /// where it makes a difference, there are both signed and unsigned variants 0066 /// of the operation. For example, sdiv and udiv. However, because the bit 0067 /// widths must be the same, operations such as Mul and Add produce the same 0068 /// results regardless of whether the values are interpreted as signed or 0069 /// not. 0070 /// * In general, the class tries to follow the style of computation that LLVM 0071 /// uses in its IR. This simplifies its use for LLVM. 0072 /// * APInt supports zero-bit-width values, but operations that require bits 0073 /// are not defined on it (e.g. you cannot ask for the sign of a zero-bit 0074 /// integer). This means that operations like zero extension and logical 0075 /// shifts are defined, but sign extension and ashr is not. Zero bit values 0076 /// compare and hash equal to themselves, and countLeadingZeros returns 0. 0077 /// 0078 class [[nodiscard]] APInt { 0079 public: 0080 typedef uint64_t WordType; 0081 0082 /// Byte size of a word. 0083 static constexpr unsigned APINT_WORD_SIZE = sizeof(WordType); 0084 0085 /// Bits in a word. 0086 static constexpr unsigned APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT; 0087 0088 enum class Rounding { 0089 DOWN, 0090 TOWARD_ZERO, 0091 UP, 0092 }; 0093 0094 static constexpr WordType WORDTYPE_MAX = ~WordType(0); 0095 0096 /// \name Constructors 0097 /// @{ 0098 0099 /// Create a new APInt of numBits width, initialized as val. 0100 /// 0101 /// If isSigned is true then val is treated as if it were a signed value 0102 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width 0103 /// will be done. Otherwise, no sign extension occurs (high order bits beyond 0104 /// the range of val are zero filled). 0105 /// 0106 /// \param numBits the bit width of the constructed APInt 0107 /// \param val the initial value of the APInt 0108 /// \param isSigned how to treat signedness of val 0109 /// \param implicitTrunc allow implicit truncation of non-zero/sign bits of 0110 /// val beyond the range of numBits 0111 APInt(unsigned numBits, uint64_t val, bool isSigned = false, 0112 bool implicitTrunc = false) 0113 : BitWidth(numBits) { 0114 if (!implicitTrunc) { 0115 if (isSigned) { 0116 if (BitWidth == 0) { 0117 assert((val == 0 || val == uint64_t(-1)) && 0118 "Value must be 0 or -1 for signed 0-bit APInt"); 0119 } else { 0120 assert(llvm::isIntN(BitWidth, val) && 0121 "Value is not an N-bit signed value"); 0122 } 0123 } else { 0124 if (BitWidth == 0) { 0125 assert(val == 0 && "Value must be zero for unsigned 0-bit APInt"); 0126 } else { 0127 assert(llvm::isUIntN(BitWidth, val) && 0128 "Value is not an N-bit unsigned value"); 0129 } 0130 } 0131 } 0132 if (isSingleWord()) { 0133 U.VAL = val; 0134 if (implicitTrunc || isSigned) 0135 clearUnusedBits(); 0136 } else { 0137 initSlowCase(val, isSigned); 0138 } 0139 } 0140 0141 /// Construct an APInt of numBits width, initialized as bigVal[]. 0142 /// 0143 /// Note that bigVal.size() can be smaller or larger than the corresponding 0144 /// bit width but any extraneous bits will be dropped. 0145 /// 0146 /// \param numBits the bit width of the constructed APInt 0147 /// \param bigVal a sequence of words to form the initial value of the APInt 0148 APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); 0149 0150 /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but 0151 /// deprecated because this constructor is prone to ambiguity with the 0152 /// APInt(unsigned, uint64_t, bool) constructor. 0153 /// 0154 /// If this overload is ever deleted, care should be taken to prevent calls 0155 /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool) 0156 /// constructor. 0157 APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); 0158 0159 /// Construct an APInt from a string representation. 0160 /// 0161 /// This constructor interprets the string \p str in the given radix. The 0162 /// interpretation stops when the first character that is not suitable for the 0163 /// radix is encountered, or the end of the string. Acceptable radix values 0164 /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the 0165 /// string to require more bits than numBits. 0166 /// 0167 /// \param numBits the bit width of the constructed APInt 0168 /// \param str the string to be interpreted 0169 /// \param radix the radix to use for the conversion 0170 APInt(unsigned numBits, StringRef str, uint8_t radix); 0171 0172 /// Default constructor that creates an APInt with a 1-bit zero value. 0173 explicit APInt() { U.VAL = 0; } 0174 0175 /// Copy Constructor. 0176 APInt(const APInt &that) : BitWidth(that.BitWidth) { 0177 if (isSingleWord()) 0178 U.VAL = that.U.VAL; 0179 else 0180 initSlowCase(that); 0181 } 0182 0183 /// Move Constructor. 0184 APInt(APInt &&that) : BitWidth(that.BitWidth) { 0185 memcpy(&U, &that.U, sizeof(U)); 0186 that.BitWidth = 0; 0187 } 0188 0189 /// Destructor. 0190 ~APInt() { 0191 if (needsCleanup()) 0192 delete[] U.pVal; 0193 } 0194 0195 /// @} 0196 /// \name Value Generators 0197 /// @{ 0198 0199 /// Get the '0' value for the specified bit-width. 0200 static APInt getZero(unsigned numBits) { return APInt(numBits, 0); } 0201 0202 /// Return an APInt zero bits wide. 0203 static APInt getZeroWidth() { return getZero(0); } 0204 0205 /// Gets maximum unsigned value of APInt for specific bit width. 0206 static APInt getMaxValue(unsigned numBits) { return getAllOnes(numBits); } 0207 0208 /// Gets maximum signed value of APInt for a specific bit width. 0209 static APInt getSignedMaxValue(unsigned numBits) { 0210 APInt API = getAllOnes(numBits); 0211 API.clearBit(numBits - 1); 0212 return API; 0213 } 0214 0215 /// Gets minimum unsigned value of APInt for a specific bit width. 0216 static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } 0217 0218 /// Gets minimum signed value of APInt for a specific bit width. 0219 static APInt getSignedMinValue(unsigned numBits) { 0220 APInt API(numBits, 0); 0221 API.setBit(numBits - 1); 0222 return API; 0223 } 0224 0225 /// Get the SignMask for a specific bit width. 0226 /// 0227 /// This is just a wrapper function of getSignedMinValue(), and it helps code 0228 /// readability when we want to get a SignMask. 0229 static APInt getSignMask(unsigned BitWidth) { 0230 return getSignedMinValue(BitWidth); 0231 } 0232 0233 /// Return an APInt of a specified width with all bits set. 0234 static APInt getAllOnes(unsigned numBits) { 0235 return APInt(numBits, WORDTYPE_MAX, true); 0236 } 0237 0238 /// Return an APInt with exactly one bit set in the result. 0239 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { 0240 APInt Res(numBits, 0); 0241 Res.setBit(BitNo); 0242 return Res; 0243 } 0244 0245 /// Get a value with a block of bits set. 0246 /// 0247 /// Constructs an APInt value that has a contiguous range of bits set. The 0248 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other 0249 /// bits will be zero. For example, with parameters(32, 0, 16) you would get 0250 /// 0x0000FFFF. Please call getBitsSetWithWrap if \p loBit may be greater than 0251 /// \p hiBit. 0252 /// 0253 /// \param numBits the intended bit width of the result 0254 /// \param loBit the index of the lowest bit set. 0255 /// \param hiBit the index of the highest bit set. 0256 /// 0257 /// \returns An APInt value with the requested bits set. 0258 static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { 0259 APInt Res(numBits, 0); 0260 Res.setBits(loBit, hiBit); 0261 return Res; 0262 } 0263 0264 /// Wrap version of getBitsSet. 0265 /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet. 0266 /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example, 0267 /// with parameters (32, 28, 4), you would get 0xF000000F. 0268 /// If \p hiBit is equal to \p loBit, you would get a result with all bits 0269 /// set. 0270 static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, 0271 unsigned hiBit) { 0272 APInt Res(numBits, 0); 0273 Res.setBitsWithWrap(loBit, hiBit); 0274 return Res; 0275 } 0276 0277 /// Constructs an APInt value that has a contiguous range of bits set. The 0278 /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other 0279 /// bits will be zero. For example, with parameters(32, 12) you would get 0280 /// 0xFFFFF000. 0281 /// 0282 /// \param numBits the intended bit width of the result 0283 /// \param loBit the index of the lowest bit to set. 0284 /// 0285 /// \returns An APInt value with the requested bits set. 0286 static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { 0287 APInt Res(numBits, 0); 0288 Res.setBitsFrom(loBit); 0289 return Res; 0290 } 0291 0292 /// Constructs an APInt value that has the top hiBitsSet bits set. 0293 /// 0294 /// \param numBits the bitwidth of the result 0295 /// \param hiBitsSet the number of high-order bits set in the result. 0296 static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { 0297 APInt Res(numBits, 0); 0298 Res.setHighBits(hiBitsSet); 0299 return Res; 0300 } 0301 0302 /// Constructs an APInt value that has the bottom loBitsSet bits set. 0303 /// 0304 /// \param numBits the bitwidth of the result 0305 /// \param loBitsSet the number of low-order bits set in the result. 0306 static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { 0307 APInt Res(numBits, 0); 0308 Res.setLowBits(loBitsSet); 0309 return Res; 0310 } 0311 0312 /// Return a value containing V broadcasted over NewLen bits. 0313 static APInt getSplat(unsigned NewLen, const APInt &V); 0314 0315 /// @} 0316 /// \name Value Tests 0317 /// @{ 0318 0319 /// Determine if this APInt just has one word to store value. 0320 /// 0321 /// \returns true if the number of bits <= 64, false otherwise. 0322 bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } 0323 0324 /// Determine sign of this APInt. 0325 /// 0326 /// This tests the high bit of this APInt to determine if it is set. 0327 /// 0328 /// \returns true if this APInt is negative, false otherwise 0329 bool isNegative() const { return (*this)[BitWidth - 1]; } 0330 0331 /// Determine if this APInt Value is non-negative (>= 0) 0332 /// 0333 /// This tests the high bit of the APInt to determine if it is unset. 0334 bool isNonNegative() const { return !isNegative(); } 0335 0336 /// Determine if sign bit of this APInt is set. 0337 /// 0338 /// This tests the high bit of this APInt to determine if it is set. 0339 /// 0340 /// \returns true if this APInt has its sign bit set, false otherwise. 0341 bool isSignBitSet() const { return (*this)[BitWidth - 1]; } 0342 0343 /// Determine if sign bit of this APInt is clear. 0344 /// 0345 /// This tests the high bit of this APInt to determine if it is clear. 0346 /// 0347 /// \returns true if this APInt has its sign bit clear, false otherwise. 0348 bool isSignBitClear() const { return !isSignBitSet(); } 0349 0350 /// Determine if this APInt Value is positive. 0351 /// 0352 /// This tests if the value of this APInt is positive (> 0). Note 0353 /// that 0 is not a positive value. 0354 /// 0355 /// \returns true if this APInt is positive. 0356 bool isStrictlyPositive() const { return isNonNegative() && !isZero(); } 0357 0358 /// Determine if this APInt Value is non-positive (<= 0). 0359 /// 0360 /// \returns true if this APInt is non-positive. 0361 bool isNonPositive() const { return !isStrictlyPositive(); } 0362 0363 /// Determine if this APInt Value only has the specified bit set. 0364 /// 0365 /// \returns true if this APInt only has the specified bit set. 0366 bool isOneBitSet(unsigned BitNo) const { 0367 return (*this)[BitNo] && popcount() == 1; 0368 } 0369 0370 /// Determine if all bits are set. This is true for zero-width values. 0371 bool isAllOnes() const { 0372 if (BitWidth == 0) 0373 return true; 0374 if (isSingleWord()) 0375 return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); 0376 return countTrailingOnesSlowCase() == BitWidth; 0377 } 0378 0379 /// Determine if this value is zero, i.e. all bits are clear. 0380 bool isZero() const { 0381 if (isSingleWord()) 0382 return U.VAL == 0; 0383 return countLeadingZerosSlowCase() == BitWidth; 0384 } 0385 0386 /// Determine if this is a value of 1. 0387 /// 0388 /// This checks to see if the value of this APInt is one. 0389 bool isOne() const { 0390 if (isSingleWord()) 0391 return U.VAL == 1; 0392 return countLeadingZerosSlowCase() == BitWidth - 1; 0393 } 0394 0395 /// Determine if this is the largest unsigned value. 0396 /// 0397 /// This checks to see if the value of this APInt is the maximum unsigned 0398 /// value for the APInt's bit width. 0399 bool isMaxValue() const { return isAllOnes(); } 0400 0401 /// Determine if this is the largest signed value. 0402 /// 0403 /// This checks to see if the value of this APInt is the maximum signed 0404 /// value for the APInt's bit width. 0405 bool isMaxSignedValue() const { 0406 if (isSingleWord()) { 0407 assert(BitWidth && "zero width values not allowed"); 0408 return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); 0409 } 0410 return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; 0411 } 0412 0413 /// Determine if this is the smallest unsigned value. 0414 /// 0415 /// This checks to see if the value of this APInt is the minimum unsigned 0416 /// value for the APInt's bit width. 0417 bool isMinValue() const { return isZero(); } 0418 0419 /// Determine if this is the smallest signed value. 0420 /// 0421 /// This checks to see if the value of this APInt is the minimum signed 0422 /// value for the APInt's bit width. 0423 bool isMinSignedValue() const { 0424 if (isSingleWord()) { 0425 assert(BitWidth && "zero width values not allowed"); 0426 return U.VAL == (WordType(1) << (BitWidth - 1)); 0427 } 0428 return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; 0429 } 0430 0431 /// Check if this APInt has an N-bits unsigned integer value. 0432 bool isIntN(unsigned N) const { return getActiveBits() <= N; } 0433 0434 /// Check if this APInt has an N-bits signed integer value. 0435 bool isSignedIntN(unsigned N) const { return getSignificantBits() <= N; } 0436 0437 /// Check if this APInt's value is a power of two greater than zero. 0438 /// 0439 /// \returns true if the argument APInt value is a power of two > 0. 0440 bool isPowerOf2() const { 0441 if (isSingleWord()) { 0442 assert(BitWidth && "zero width values not allowed"); 0443 return isPowerOf2_64(U.VAL); 0444 } 0445 return countPopulationSlowCase() == 1; 0446 } 0447 0448 /// Check if this APInt's negated value is a power of two greater than zero. 0449 bool isNegatedPowerOf2() const { 0450 assert(BitWidth && "zero width values not allowed"); 0451 if (isNonNegative()) 0452 return false; 0453 // NegatedPowerOf2 - shifted mask in the top bits. 0454 unsigned LO = countl_one(); 0455 unsigned TZ = countr_zero(); 0456 return (LO + TZ) == BitWidth; 0457 } 0458 0459 /// Checks if this APInt -interpreted as an address- is aligned to the 0460 /// provided value. 0461 bool isAligned(Align A) const; 0462 0463 /// Check if the APInt's value is returned by getSignMask. 0464 /// 0465 /// \returns true if this is the value returned by getSignMask. 0466 bool isSignMask() const { return isMinSignedValue(); } 0467 0468 /// Convert APInt to a boolean value. 0469 /// 0470 /// This converts the APInt to a boolean value as a test against zero. 0471 bool getBoolValue() const { return !isZero(); } 0472 0473 /// If this value is smaller than the specified limit, return it, otherwise 0474 /// return the limit value. This causes the value to saturate to the limit. 0475 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) const { 0476 return ugt(Limit) ? Limit : getZExtValue(); 0477 } 0478 0479 /// Check if the APInt consists of a repeated bit pattern. 0480 /// 0481 /// e.g. 0x01010101 satisfies isSplat(8). 0482 /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit 0483 /// width without remainder. 0484 bool isSplat(unsigned SplatSizeInBits) const; 0485 0486 /// \returns true if this APInt value is a sequence of \param numBits ones 0487 /// starting at the least significant bit with the remainder zero. 0488 bool isMask(unsigned numBits) const { 0489 assert(numBits != 0 && "numBits must be non-zero"); 0490 assert(numBits <= BitWidth && "numBits out of range"); 0491 if (isSingleWord()) 0492 return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); 0493 unsigned Ones = countTrailingOnesSlowCase(); 0494 return (numBits == Ones) && 0495 ((Ones + countLeadingZerosSlowCase()) == BitWidth); 0496 } 0497 0498 /// \returns true if this APInt is a non-empty sequence of ones starting at 0499 /// the least significant bit with the remainder zero. 0500 /// Ex. isMask(0x0000FFFFU) == true. 0501 bool isMask() const { 0502 if (isSingleWord()) 0503 return isMask_64(U.VAL); 0504 unsigned Ones = countTrailingOnesSlowCase(); 0505 return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); 0506 } 0507 0508 /// Return true if this APInt value contains a non-empty sequence of ones with 0509 /// the remainder zero. 0510 bool isShiftedMask() const { 0511 if (isSingleWord()) 0512 return isShiftedMask_64(U.VAL); 0513 unsigned Ones = countPopulationSlowCase(); 0514 unsigned LeadZ = countLeadingZerosSlowCase(); 0515 return (Ones + LeadZ + countTrailingZerosSlowCase()) == BitWidth; 0516 } 0517 0518 /// Return true if this APInt value contains a non-empty sequence of ones with 0519 /// the remainder zero. If true, \p MaskIdx will specify the index of the 0520 /// lowest set bit and \p MaskLen is updated to specify the length of the 0521 /// mask, else neither are updated. 0522 bool isShiftedMask(unsigned &MaskIdx, unsigned &MaskLen) const { 0523 if (isSingleWord()) 0524 return isShiftedMask_64(U.VAL, MaskIdx, MaskLen); 0525 unsigned Ones = countPopulationSlowCase(); 0526 unsigned LeadZ = countLeadingZerosSlowCase(); 0527 unsigned TrailZ = countTrailingZerosSlowCase(); 0528 if ((Ones + LeadZ + TrailZ) != BitWidth) 0529 return false; 0530 MaskLen = Ones; 0531 MaskIdx = TrailZ; 0532 return true; 0533 } 0534 0535 /// Compute an APInt containing numBits highbits from this APInt. 0536 /// 0537 /// Get an APInt with the same BitWidth as this APInt, just zero mask the low 0538 /// bits and right shift to the least significant bit. 0539 /// 0540 /// \returns the high "numBits" bits of this APInt. 0541 APInt getHiBits(unsigned numBits) const; 0542 0543 /// Compute an APInt containing numBits lowbits from this APInt. 0544 /// 0545 /// Get an APInt with the same BitWidth as this APInt, just zero mask the high 0546 /// bits. 0547 /// 0548 /// \returns the low "numBits" bits of this APInt. 0549 APInt getLoBits(unsigned numBits) const; 0550 0551 /// Determine if two APInts have the same value, after zero-extending 0552 /// one of them (if needed!) to ensure that the bit-widths match. 0553 static bool isSameValue(const APInt &I1, const APInt &I2) { 0554 if (I1.getBitWidth() == I2.getBitWidth()) 0555 return I1 == I2; 0556 0557 if (I1.getBitWidth() > I2.getBitWidth()) 0558 return I1 == I2.zext(I1.getBitWidth()); 0559 0560 return I1.zext(I2.getBitWidth()) == I2; 0561 } 0562 0563 /// Overload to compute a hash_code for an APInt value. 0564 friend hash_code hash_value(const APInt &Arg); 0565 0566 /// This function returns a pointer to the internal storage of the APInt. 0567 /// This is useful for writing out the APInt in binary form without any 0568 /// conversions. 0569 const uint64_t *getRawData() const { 0570 if (isSingleWord()) 0571 return &U.VAL; 0572 return &U.pVal[0]; 0573 } 0574 0575 /// @} 0576 /// \name Unary Operators 0577 /// @{ 0578 0579 /// Postfix increment operator. Increment *this by 1. 0580 /// 0581 /// \returns a new APInt value representing the original value of *this. 0582 APInt operator++(int) { 0583 APInt API(*this); 0584 ++(*this); 0585 return API; 0586 } 0587 0588 /// Prefix increment operator. 0589 /// 0590 /// \returns *this incremented by one 0591 APInt &operator++(); 0592 0593 /// Postfix decrement operator. Decrement *this by 1. 0594 /// 0595 /// \returns a new APInt value representing the original value of *this. 0596 APInt operator--(int) { 0597 APInt API(*this); 0598 --(*this); 0599 return API; 0600 } 0601 0602 /// Prefix decrement operator. 0603 /// 0604 /// \returns *this decremented by one. 0605 APInt &operator--(); 0606 0607 /// Logical negation operation on this APInt returns true if zero, like normal 0608 /// integers. 0609 bool operator!() const { return isZero(); } 0610 0611 /// @} 0612 /// \name Assignment Operators 0613 /// @{ 0614 0615 /// Copy assignment operator. 0616 /// 0617 /// \returns *this after assignment of RHS. 0618 APInt &operator=(const APInt &RHS) { 0619 // The common case (both source or dest being inline) doesn't require 0620 // allocation or deallocation. 0621 if (isSingleWord() && RHS.isSingleWord()) { 0622 U.VAL = RHS.U.VAL; 0623 BitWidth = RHS.BitWidth; 0624 return *this; 0625 } 0626 0627 assignSlowCase(RHS); 0628 return *this; 0629 } 0630 0631 /// Move assignment operator. 0632 APInt &operator=(APInt &&that) { 0633 #ifdef EXPENSIVE_CHECKS 0634 // Some std::shuffle implementations still do self-assignment. 0635 if (this == &that) 0636 return *this; 0637 #endif 0638 assert(this != &that && "Self-move not supported"); 0639 if (!isSingleWord()) 0640 delete[] U.pVal; 0641 0642 // Use memcpy so that type based alias analysis sees both VAL and pVal 0643 // as modified. 0644 memcpy(&U, &that.U, sizeof(U)); 0645 0646 BitWidth = that.BitWidth; 0647 that.BitWidth = 0; 0648 return *this; 0649 } 0650 0651 /// Assignment operator. 0652 /// 0653 /// The RHS value is assigned to *this. If the significant bits in RHS exceed 0654 /// the bit width, the excess bits are truncated. If the bit width is larger 0655 /// than 64, the value is zero filled in the unspecified high order bits. 0656 /// 0657 /// \returns *this after assignment of RHS value. 0658 APInt &operator=(uint64_t RHS) { 0659 if (isSingleWord()) { 0660 U.VAL = RHS; 0661 return clearUnusedBits(); 0662 } 0663 U.pVal[0] = RHS; 0664 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); 0665 return *this; 0666 } 0667 0668 /// Bitwise AND assignment operator. 0669 /// 0670 /// Performs a bitwise AND operation on this APInt and RHS. The result is 0671 /// assigned to *this. 0672 /// 0673 /// \returns *this after ANDing with RHS. 0674 APInt &operator&=(const APInt &RHS) { 0675 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 0676 if (isSingleWord()) 0677 U.VAL &= RHS.U.VAL; 0678 else 0679 andAssignSlowCase(RHS); 0680 return *this; 0681 } 0682 0683 /// Bitwise AND assignment operator. 0684 /// 0685 /// Performs a bitwise AND operation on this APInt and RHS. RHS is 0686 /// logically zero-extended or truncated to match the bit-width of 0687 /// the LHS. 0688 APInt &operator&=(uint64_t RHS) { 0689 if (isSingleWord()) { 0690 U.VAL &= RHS; 0691 return *this; 0692 } 0693 U.pVal[0] &= RHS; 0694 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); 0695 return *this; 0696 } 0697 0698 /// Bitwise OR assignment operator. 0699 /// 0700 /// Performs a bitwise OR operation on this APInt and RHS. The result is 0701 /// assigned *this; 0702 /// 0703 /// \returns *this after ORing with RHS. 0704 APInt &operator|=(const APInt &RHS) { 0705 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 0706 if (isSingleWord()) 0707 U.VAL |= RHS.U.VAL; 0708 else 0709 orAssignSlowCase(RHS); 0710 return *this; 0711 } 0712 0713 /// Bitwise OR assignment operator. 0714 /// 0715 /// Performs a bitwise OR operation on this APInt and RHS. RHS is 0716 /// logically zero-extended or truncated to match the bit-width of 0717 /// the LHS. 0718 APInt &operator|=(uint64_t RHS) { 0719 if (isSingleWord()) { 0720 U.VAL |= RHS; 0721 return clearUnusedBits(); 0722 } 0723 U.pVal[0] |= RHS; 0724 return *this; 0725 } 0726 0727 /// Bitwise XOR assignment operator. 0728 /// 0729 /// Performs a bitwise XOR operation on this APInt and RHS. The result is 0730 /// assigned to *this. 0731 /// 0732 /// \returns *this after XORing with RHS. 0733 APInt &operator^=(const APInt &RHS) { 0734 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 0735 if (isSingleWord()) 0736 U.VAL ^= RHS.U.VAL; 0737 else 0738 xorAssignSlowCase(RHS); 0739 return *this; 0740 } 0741 0742 /// Bitwise XOR assignment operator. 0743 /// 0744 /// Performs a bitwise XOR operation on this APInt and RHS. RHS is 0745 /// logically zero-extended or truncated to match the bit-width of 0746 /// the LHS. 0747 APInt &operator^=(uint64_t RHS) { 0748 if (isSingleWord()) { 0749 U.VAL ^= RHS; 0750 return clearUnusedBits(); 0751 } 0752 U.pVal[0] ^= RHS; 0753 return *this; 0754 } 0755 0756 /// Multiplication assignment operator. 0757 /// 0758 /// Multiplies this APInt by RHS and assigns the result to *this. 0759 /// 0760 /// \returns *this 0761 APInt &operator*=(const APInt &RHS); 0762 APInt &operator*=(uint64_t RHS); 0763 0764 /// Addition assignment operator. 0765 /// 0766 /// Adds RHS to *this and assigns the result to *this. 0767 /// 0768 /// \returns *this 0769 APInt &operator+=(const APInt &RHS); 0770 APInt &operator+=(uint64_t RHS); 0771 0772 /// Subtraction assignment operator. 0773 /// 0774 /// Subtracts RHS from *this and assigns the result to *this. 0775 /// 0776 /// \returns *this 0777 APInt &operator-=(const APInt &RHS); 0778 APInt &operator-=(uint64_t RHS); 0779 0780 /// Left-shift assignment function. 0781 /// 0782 /// Shifts *this left by shiftAmt and assigns the result to *this. 0783 /// 0784 /// \returns *this after shifting left by ShiftAmt 0785 APInt &operator<<=(unsigned ShiftAmt) { 0786 assert(ShiftAmt <= BitWidth && "Invalid shift amount"); 0787 if (isSingleWord()) { 0788 if (ShiftAmt == BitWidth) 0789 U.VAL = 0; 0790 else 0791 U.VAL <<= ShiftAmt; 0792 return clearUnusedBits(); 0793 } 0794 shlSlowCase(ShiftAmt); 0795 return *this; 0796 } 0797 0798 /// Left-shift assignment function. 0799 /// 0800 /// Shifts *this left by shiftAmt and assigns the result to *this. 0801 /// 0802 /// \returns *this after shifting left by ShiftAmt 0803 APInt &operator<<=(const APInt &ShiftAmt); 0804 0805 /// @} 0806 /// \name Binary Operators 0807 /// @{ 0808 0809 /// Multiplication operator. 0810 /// 0811 /// Multiplies this APInt by RHS and returns the result. 0812 APInt operator*(const APInt &RHS) const; 0813 0814 /// Left logical shift operator. 0815 /// 0816 /// Shifts this APInt left by \p Bits and returns the result. 0817 APInt operator<<(unsigned Bits) const { return shl(Bits); } 0818 0819 /// Left logical shift operator. 0820 /// 0821 /// Shifts this APInt left by \p Bits and returns the result. 0822 APInt operator<<(const APInt &Bits) const { return shl(Bits); } 0823 0824 /// Arithmetic right-shift function. 0825 /// 0826 /// Arithmetic right-shift this APInt by shiftAmt. 0827 APInt ashr(unsigned ShiftAmt) const { 0828 APInt R(*this); 0829 R.ashrInPlace(ShiftAmt); 0830 return R; 0831 } 0832 0833 /// Arithmetic right-shift this APInt by ShiftAmt in place. 0834 void ashrInPlace(unsigned ShiftAmt) { 0835 assert(ShiftAmt <= BitWidth && "Invalid shift amount"); 0836 if (isSingleWord()) { 0837 int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); 0838 if (ShiftAmt == BitWidth) 0839 U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit. 0840 else 0841 U.VAL = SExtVAL >> ShiftAmt; 0842 clearUnusedBits(); 0843 return; 0844 } 0845 ashrSlowCase(ShiftAmt); 0846 } 0847 0848 /// Logical right-shift function. 0849 /// 0850 /// Logical right-shift this APInt by shiftAmt. 0851 APInt lshr(unsigned shiftAmt) const { 0852 APInt R(*this); 0853 R.lshrInPlace(shiftAmt); 0854 return R; 0855 } 0856 0857 /// Logical right-shift this APInt by ShiftAmt in place. 0858 void lshrInPlace(unsigned ShiftAmt) { 0859 assert(ShiftAmt <= BitWidth && "Invalid shift amount"); 0860 if (isSingleWord()) { 0861 if (ShiftAmt == BitWidth) 0862 U.VAL = 0; 0863 else 0864 U.VAL >>= ShiftAmt; 0865 return; 0866 } 0867 lshrSlowCase(ShiftAmt); 0868 } 0869 0870 /// Left-shift function. 0871 /// 0872 /// Left-shift this APInt by shiftAmt. 0873 APInt shl(unsigned shiftAmt) const { 0874 APInt R(*this); 0875 R <<= shiftAmt; 0876 return R; 0877 } 0878 0879 /// relative logical shift right 0880 APInt relativeLShr(int RelativeShift) const { 0881 return RelativeShift > 0 ? lshr(RelativeShift) : shl(-RelativeShift); 0882 } 0883 0884 /// relative logical shift left 0885 APInt relativeLShl(int RelativeShift) const { 0886 return relativeLShr(-RelativeShift); 0887 } 0888 0889 /// relative arithmetic shift right 0890 APInt relativeAShr(int RelativeShift) const { 0891 return RelativeShift > 0 ? ashr(RelativeShift) : shl(-RelativeShift); 0892 } 0893 0894 /// relative arithmetic shift left 0895 APInt relativeAShl(int RelativeShift) const { 0896 return relativeAShr(-RelativeShift); 0897 } 0898 0899 /// Rotate left by rotateAmt. 0900 APInt rotl(unsigned rotateAmt) const; 0901 0902 /// Rotate right by rotateAmt. 0903 APInt rotr(unsigned rotateAmt) const; 0904 0905 /// Arithmetic right-shift function. 0906 /// 0907 /// Arithmetic right-shift this APInt by shiftAmt. 0908 APInt ashr(const APInt &ShiftAmt) const { 0909 APInt R(*this); 0910 R.ashrInPlace(ShiftAmt); 0911 return R; 0912 } 0913 0914 /// Arithmetic right-shift this APInt by shiftAmt in place. 0915 void ashrInPlace(const APInt &shiftAmt); 0916 0917 /// Logical right-shift function. 0918 /// 0919 /// Logical right-shift this APInt by shiftAmt. 0920 APInt lshr(const APInt &ShiftAmt) const { 0921 APInt R(*this); 0922 R.lshrInPlace(ShiftAmt); 0923 return R; 0924 } 0925 0926 /// Logical right-shift this APInt by ShiftAmt in place. 0927 void lshrInPlace(const APInt &ShiftAmt); 0928 0929 /// Left-shift function. 0930 /// 0931 /// Left-shift this APInt by shiftAmt. 0932 APInt shl(const APInt &ShiftAmt) const { 0933 APInt R(*this); 0934 R <<= ShiftAmt; 0935 return R; 0936 } 0937 0938 /// Rotate left by rotateAmt. 0939 APInt rotl(const APInt &rotateAmt) const; 0940 0941 /// Rotate right by rotateAmt. 0942 APInt rotr(const APInt &rotateAmt) const; 0943 0944 /// Concatenate the bits from "NewLSB" onto the bottom of *this. This is 0945 /// equivalent to: 0946 /// (this->zext(NewWidth) << NewLSB.getBitWidth()) | NewLSB.zext(NewWidth) 0947 APInt concat(const APInt &NewLSB) const { 0948 /// If the result will be small, then both the merged values are small. 0949 unsigned NewWidth = getBitWidth() + NewLSB.getBitWidth(); 0950 if (NewWidth <= APINT_BITS_PER_WORD) 0951 return APInt(NewWidth, (U.VAL << NewLSB.getBitWidth()) | NewLSB.U.VAL); 0952 return concatSlowCase(NewLSB); 0953 } 0954 0955 /// Unsigned division operation. 0956 /// 0957 /// Perform an unsigned divide operation on this APInt by RHS. Both this and 0958 /// RHS are treated as unsigned quantities for purposes of this division. 0959 /// 0960 /// \returns a new APInt value containing the division result, rounded towards 0961 /// zero. 0962 APInt udiv(const APInt &RHS) const; 0963 APInt udiv(uint64_t RHS) const; 0964 0965 /// Signed division function for APInt. 0966 /// 0967 /// Signed divide this APInt by APInt RHS. 0968 /// 0969 /// The result is rounded towards zero. 0970 APInt sdiv(const APInt &RHS) const; 0971 APInt sdiv(int64_t RHS) const; 0972 0973 /// Unsigned remainder operation. 0974 /// 0975 /// Perform an unsigned remainder operation on this APInt with RHS being the 0976 /// divisor. Both this and RHS are treated as unsigned quantities for purposes 0977 /// of this operation. 0978 /// 0979 /// \returns a new APInt value containing the remainder result 0980 APInt urem(const APInt &RHS) const; 0981 uint64_t urem(uint64_t RHS) const; 0982 0983 /// Function for signed remainder operation. 0984 /// 0985 /// Signed remainder operation on APInt. 0986 /// 0987 /// Note that this is a true remainder operation and not a modulo operation 0988 /// because the sign follows the sign of the dividend which is *this. 0989 APInt srem(const APInt &RHS) const; 0990 int64_t srem(int64_t RHS) const; 0991 0992 /// Dual division/remainder interface. 0993 /// 0994 /// Sometimes it is convenient to divide two APInt values and obtain both the 0995 /// quotient and remainder. This function does both operations in the same 0996 /// computation making it a little more efficient. The pair of input arguments 0997 /// may overlap with the pair of output arguments. It is safe to call 0998 /// udivrem(X, Y, X, Y), for example. 0999 static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, 1000 APInt &Remainder); 1001 static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, 1002 uint64_t &Remainder); 1003 1004 static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, 1005 APInt &Remainder); 1006 static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, 1007 int64_t &Remainder); 1008 1009 // Operations that return overflow indicators. 1010 APInt sadd_ov(const APInt &RHS, bool &Overflow) const; 1011 APInt uadd_ov(const APInt &RHS, bool &Overflow) const; 1012 APInt ssub_ov(const APInt &RHS, bool &Overflow) const; 1013 APInt usub_ov(const APInt &RHS, bool &Overflow) const; 1014 APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; 1015 APInt smul_ov(const APInt &RHS, bool &Overflow) const; 1016 APInt umul_ov(const APInt &RHS, bool &Overflow) const; 1017 APInt sshl_ov(const APInt &Amt, bool &Overflow) const; 1018 APInt sshl_ov(unsigned Amt, bool &Overflow) const; 1019 APInt ushl_ov(const APInt &Amt, bool &Overflow) const; 1020 APInt ushl_ov(unsigned Amt, bool &Overflow) const; 1021 1022 /// Signed integer floor division operation. 1023 /// 1024 /// Rounds towards negative infinity, i.e. 5 / -2 = -3. Iff minimum value 1025 /// divided by -1 set Overflow to true. 1026 APInt sfloordiv_ov(const APInt &RHS, bool &Overflow) const; 1027 1028 // Operations that saturate 1029 APInt sadd_sat(const APInt &RHS) const; 1030 APInt uadd_sat(const APInt &RHS) const; 1031 APInt ssub_sat(const APInt &RHS) const; 1032 APInt usub_sat(const APInt &RHS) const; 1033 APInt smul_sat(const APInt &RHS) const; 1034 APInt umul_sat(const APInt &RHS) const; 1035 APInt sshl_sat(const APInt &RHS) const; 1036 APInt sshl_sat(unsigned RHS) const; 1037 APInt ushl_sat(const APInt &RHS) const; 1038 APInt ushl_sat(unsigned RHS) const; 1039 1040 /// Array-indexing support. 1041 /// 1042 /// \returns the bit value at bitPosition 1043 bool operator[](unsigned bitPosition) const { 1044 assert(bitPosition < getBitWidth() && "Bit position out of bounds!"); 1045 return (maskBit(bitPosition) & getWord(bitPosition)) != 0; 1046 } 1047 1048 /// @} 1049 /// \name Comparison Operators 1050 /// @{ 1051 1052 /// Equality operator. 1053 /// 1054 /// Compares this APInt with RHS for the validity of the equality 1055 /// relationship. 1056 bool operator==(const APInt &RHS) const { 1057 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); 1058 if (isSingleWord()) 1059 return U.VAL == RHS.U.VAL; 1060 return equalSlowCase(RHS); 1061 } 1062 1063 /// Equality operator. 1064 /// 1065 /// Compares this APInt with a uint64_t for the validity of the equality 1066 /// relationship. 1067 /// 1068 /// \returns true if *this == Val 1069 bool operator==(uint64_t Val) const { 1070 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; 1071 } 1072 1073 /// Equality comparison. 1074 /// 1075 /// Compares this APInt with RHS for the validity of the equality 1076 /// relationship. 1077 /// 1078 /// \returns true if *this == Val 1079 bool eq(const APInt &RHS) const { return (*this) == RHS; } 1080 1081 /// Inequality operator. 1082 /// 1083 /// Compares this APInt with RHS for the validity of the inequality 1084 /// relationship. 1085 /// 1086 /// \returns true if *this != Val 1087 bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } 1088 1089 /// Inequality operator. 1090 /// 1091 /// Compares this APInt with a uint64_t for the validity of the inequality 1092 /// relationship. 1093 /// 1094 /// \returns true if *this != Val 1095 bool operator!=(uint64_t Val) const { return !((*this) == Val); } 1096 1097 /// Inequality comparison 1098 /// 1099 /// Compares this APInt with RHS for the validity of the inequality 1100 /// relationship. 1101 /// 1102 /// \returns true if *this != Val 1103 bool ne(const APInt &RHS) const { return !((*this) == RHS); } 1104 1105 /// Unsigned less than comparison 1106 /// 1107 /// Regards both *this and RHS as unsigned quantities and compares them for 1108 /// the validity of the less-than relationship. 1109 /// 1110 /// \returns true if *this < RHS when both are considered unsigned. 1111 bool ult(const APInt &RHS) const { return compare(RHS) < 0; } 1112 1113 /// Unsigned less than comparison 1114 /// 1115 /// Regards both *this as an unsigned quantity and compares it with RHS for 1116 /// the validity of the less-than relationship. 1117 /// 1118 /// \returns true if *this < RHS when considered unsigned. 1119 bool ult(uint64_t RHS) const { 1120 // Only need to check active bits if not a single word. 1121 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; 1122 } 1123 1124 /// Signed less than comparison 1125 /// 1126 /// Regards both *this and RHS as signed quantities and compares them for 1127 /// validity of the less-than relationship. 1128 /// 1129 /// \returns true if *this < RHS when both are considered signed. 1130 bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } 1131 1132 /// Signed less than comparison 1133 /// 1134 /// Regards both *this as a signed quantity and compares it with RHS for 1135 /// the validity of the less-than relationship. 1136 /// 1137 /// \returns true if *this < RHS when considered signed. 1138 bool slt(int64_t RHS) const { 1139 return (!isSingleWord() && getSignificantBits() > 64) 1140 ? isNegative() 1141 : getSExtValue() < RHS; 1142 } 1143 1144 /// Unsigned less or equal comparison 1145 /// 1146 /// Regards both *this and RHS as unsigned quantities and compares them for 1147 /// validity of the less-or-equal relationship. 1148 /// 1149 /// \returns true if *this <= RHS when both are considered unsigned. 1150 bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } 1151 1152 /// Unsigned less or equal comparison 1153 /// 1154 /// Regards both *this as an unsigned quantity and compares it with RHS for 1155 /// the validity of the less-or-equal relationship. 1156 /// 1157 /// \returns true if *this <= RHS when considered unsigned. 1158 bool ule(uint64_t RHS) const { return !ugt(RHS); } 1159 1160 /// Signed less or equal comparison 1161 /// 1162 /// Regards both *this and RHS as signed quantities and compares them for 1163 /// validity of the less-or-equal relationship. 1164 /// 1165 /// \returns true if *this <= RHS when both are considered signed. 1166 bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } 1167 1168 /// Signed less or equal comparison 1169 /// 1170 /// Regards both *this as a signed quantity and compares it with RHS for the 1171 /// validity of the less-or-equal relationship. 1172 /// 1173 /// \returns true if *this <= RHS when considered signed. 1174 bool sle(uint64_t RHS) const { return !sgt(RHS); } 1175 1176 /// Unsigned greater than comparison 1177 /// 1178 /// Regards both *this and RHS as unsigned quantities and compares them for 1179 /// the validity of the greater-than relationship. 1180 /// 1181 /// \returns true if *this > RHS when both are considered unsigned. 1182 bool ugt(const APInt &RHS) const { return !ule(RHS); } 1183 1184 /// Unsigned greater than comparison 1185 /// 1186 /// Regards both *this as an unsigned quantity and compares it with RHS for 1187 /// the validity of the greater-than relationship. 1188 /// 1189 /// \returns true if *this > RHS when considered unsigned. 1190 bool ugt(uint64_t RHS) const { 1191 // Only need to check active bits if not a single word. 1192 return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; 1193 } 1194 1195 /// Signed greater than comparison 1196 /// 1197 /// Regards both *this and RHS as signed quantities and compares them for the 1198 /// validity of the greater-than relationship. 1199 /// 1200 /// \returns true if *this > RHS when both are considered signed. 1201 bool sgt(const APInt &RHS) const { return !sle(RHS); } 1202 1203 /// Signed greater than comparison 1204 /// 1205 /// Regards both *this as a signed quantity and compares it with RHS for 1206 /// the validity of the greater-than relationship. 1207 /// 1208 /// \returns true if *this > RHS when considered signed. 1209 bool sgt(int64_t RHS) const { 1210 return (!isSingleWord() && getSignificantBits() > 64) 1211 ? !isNegative() 1212 : getSExtValue() > RHS; 1213 } 1214 1215 /// Unsigned greater or equal comparison 1216 /// 1217 /// Regards both *this and RHS as unsigned quantities and compares them for 1218 /// validity of the greater-or-equal relationship. 1219 /// 1220 /// \returns true if *this >= RHS when both are considered unsigned. 1221 bool uge(const APInt &RHS) const { return !ult(RHS); } 1222 1223 /// Unsigned greater or equal comparison 1224 /// 1225 /// Regards both *this as an unsigned quantity and compares it with RHS for 1226 /// the validity of the greater-or-equal relationship. 1227 /// 1228 /// \returns true if *this >= RHS when considered unsigned. 1229 bool uge(uint64_t RHS) const { return !ult(RHS); } 1230 1231 /// Signed greater or equal comparison 1232 /// 1233 /// Regards both *this and RHS as signed quantities and compares them for 1234 /// validity of the greater-or-equal relationship. 1235 /// 1236 /// \returns true if *this >= RHS when both are considered signed. 1237 bool sge(const APInt &RHS) const { return !slt(RHS); } 1238 1239 /// Signed greater or equal comparison 1240 /// 1241 /// Regards both *this as a signed quantity and compares it with RHS for 1242 /// the validity of the greater-or-equal relationship. 1243 /// 1244 /// \returns true if *this >= RHS when considered signed. 1245 bool sge(int64_t RHS) const { return !slt(RHS); } 1246 1247 /// This operation tests if there are any pairs of corresponding bits 1248 /// between this APInt and RHS that are both set. 1249 bool intersects(const APInt &RHS) const { 1250 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1251 if (isSingleWord()) 1252 return (U.VAL & RHS.U.VAL) != 0; 1253 return intersectsSlowCase(RHS); 1254 } 1255 1256 /// This operation checks that all bits set in this APInt are also set in RHS. 1257 bool isSubsetOf(const APInt &RHS) const { 1258 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); 1259 if (isSingleWord()) 1260 return (U.VAL & ~RHS.U.VAL) == 0; 1261 return isSubsetOfSlowCase(RHS); 1262 } 1263 1264 /// @} 1265 /// \name Resizing Operators 1266 /// @{ 1267 1268 /// Truncate to new width. 1269 /// 1270 /// Truncate the APInt to a specified width. It is an error to specify a width 1271 /// that is greater than the current width. 1272 APInt trunc(unsigned width) const; 1273 1274 /// Truncate to new width with unsigned saturation. 1275 /// 1276 /// If the APInt, treated as unsigned integer, can be losslessly truncated to 1277 /// the new bitwidth, then return truncated APInt. Else, return max value. 1278 APInt truncUSat(unsigned width) const; 1279 1280 /// Truncate to new width with signed saturation. 1281 /// 1282 /// If this APInt, treated as signed integer, can be losslessly truncated to 1283 /// the new bitwidth, then return truncated APInt. Else, return either 1284 /// signed min value if the APInt was negative, or signed max value. 1285 APInt truncSSat(unsigned width) const; 1286 1287 /// Sign extend to a new width. 1288 /// 1289 /// This operation sign extends the APInt to a new width. If the high order 1290 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. 1291 /// It is an error to specify a width that is less than the 1292 /// current width. 1293 APInt sext(unsigned width) const; 1294 1295 /// Zero extend to a new width. 1296 /// 1297 /// This operation zero extends the APInt to a new width. The high order bits 1298 /// are filled with 0 bits. It is an error to specify a width that is less 1299 /// than the current width. 1300 APInt zext(unsigned width) const; 1301 1302 /// Sign extend or truncate to width 1303 /// 1304 /// Make this APInt have the bit width given by \p width. The value is sign 1305 /// extended, truncated, or left alone to make it that width. 1306 APInt sextOrTrunc(unsigned width) const; 1307 1308 /// Zero extend or truncate to width 1309 /// 1310 /// Make this APInt have the bit width given by \p width. The value is zero 1311 /// extended, truncated, or left alone to make it that width. 1312 APInt zextOrTrunc(unsigned width) const; 1313 1314 /// @} 1315 /// \name Bit Manipulation Operators 1316 /// @{ 1317 1318 /// Set every bit to 1. 1319 void setAllBits() { 1320 if (isSingleWord()) 1321 U.VAL = WORDTYPE_MAX; 1322 else 1323 // Set all the bits in all the words. 1324 memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); 1325 // Clear the unused ones 1326 clearUnusedBits(); 1327 } 1328 1329 /// Set the given bit to 1 whose position is given as "bitPosition". 1330 void setBit(unsigned BitPosition) { 1331 assert(BitPosition < BitWidth && "BitPosition out of range"); 1332 WordType Mask = maskBit(BitPosition); 1333 if (isSingleWord()) 1334 U.VAL |= Mask; 1335 else 1336 U.pVal[whichWord(BitPosition)] |= Mask; 1337 } 1338 1339 /// Set the sign bit to 1. 1340 void setSignBit() { setBit(BitWidth - 1); } 1341 1342 /// Set a given bit to a given value. 1343 void setBitVal(unsigned BitPosition, bool BitValue) { 1344 if (BitValue) 1345 setBit(BitPosition); 1346 else 1347 clearBit(BitPosition); 1348 } 1349 1350 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1. 1351 /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls 1352 /// setBits when \p loBit < \p hiBit. 1353 /// For \p loBit == \p hiBit wrap case, set every bit to 1. 1354 void setBitsWithWrap(unsigned loBit, unsigned hiBit) { 1355 assert(hiBit <= BitWidth && "hiBit out of range"); 1356 assert(loBit <= BitWidth && "loBit out of range"); 1357 if (loBit < hiBit) { 1358 setBits(loBit, hiBit); 1359 return; 1360 } 1361 setLowBits(hiBit); 1362 setHighBits(BitWidth - loBit); 1363 } 1364 1365 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1. 1366 /// This function handles case when \p loBit <= \p hiBit. 1367 void setBits(unsigned loBit, unsigned hiBit) { 1368 assert(hiBit <= BitWidth && "hiBit out of range"); 1369 assert(loBit <= BitWidth && "loBit out of range"); 1370 assert(loBit <= hiBit && "loBit greater than hiBit"); 1371 if (loBit == hiBit) 1372 return; 1373 if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { 1374 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); 1375 mask <<= loBit; 1376 if (isSingleWord()) 1377 U.VAL |= mask; 1378 else 1379 U.pVal[0] |= mask; 1380 } else { 1381 setBitsSlowCase(loBit, hiBit); 1382 } 1383 } 1384 1385 /// Set the top bits starting from loBit. 1386 void setBitsFrom(unsigned loBit) { return setBits(loBit, BitWidth); } 1387 1388 /// Set the bottom loBits bits. 1389 void setLowBits(unsigned loBits) { return setBits(0, loBits); } 1390 1391 /// Set the top hiBits bits. 1392 void setHighBits(unsigned hiBits) { 1393 return setBits(BitWidth - hiBits, BitWidth); 1394 } 1395 1396 /// Set every bit to 0. 1397 void clearAllBits() { 1398 if (isSingleWord()) 1399 U.VAL = 0; 1400 else 1401 memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); 1402 } 1403 1404 /// Set a given bit to 0. 1405 /// 1406 /// Set the given bit to 0 whose position is given as "bitPosition". 1407 void clearBit(unsigned BitPosition) { 1408 assert(BitPosition < BitWidth && "BitPosition out of range"); 1409 WordType Mask = ~maskBit(BitPosition); 1410 if (isSingleWord()) 1411 U.VAL &= Mask; 1412 else 1413 U.pVal[whichWord(BitPosition)] &= Mask; 1414 } 1415 1416 /// Set bottom loBits bits to 0. 1417 void clearLowBits(unsigned loBits) { 1418 assert(loBits <= BitWidth && "More bits than bitwidth"); 1419 APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits); 1420 *this &= Keep; 1421 } 1422 1423 /// Set top hiBits bits to 0. 1424 void clearHighBits(unsigned hiBits) { 1425 assert(hiBits <= BitWidth && "More bits than bitwidth"); 1426 APInt Keep = getLowBitsSet(BitWidth, BitWidth - hiBits); 1427 *this &= Keep; 1428 } 1429 1430 /// Set the sign bit to 0. 1431 void clearSignBit() { clearBit(BitWidth - 1); } 1432 1433 /// Toggle every bit to its opposite value. 1434 void flipAllBits() { 1435 if (isSingleWord()) { 1436 U.VAL ^= WORDTYPE_MAX; 1437 clearUnusedBits(); 1438 } else { 1439 flipAllBitsSlowCase(); 1440 } 1441 } 1442 1443 /// Toggles a given bit to its opposite value. 1444 /// 1445 /// Toggle a given bit to its opposite value whose position is given 1446 /// as "bitPosition". 1447 void flipBit(unsigned bitPosition); 1448 1449 /// Negate this APInt in place. 1450 void negate() { 1451 flipAllBits(); 1452 ++(*this); 1453 } 1454 1455 /// Insert the bits from a smaller APInt starting at bitPosition. 1456 void insertBits(const APInt &SubBits, unsigned bitPosition); 1457 void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits); 1458 1459 /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits). 1460 APInt extractBits(unsigned numBits, unsigned bitPosition) const; 1461 uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const; 1462 1463 /// @} 1464 /// \name Value Characterization Functions 1465 /// @{ 1466 1467 /// Return the number of bits in the APInt. 1468 unsigned getBitWidth() const { return BitWidth; } 1469 1470 /// Get the number of words. 1471 /// 1472 /// Here one word's bitwidth equals to that of uint64_t. 1473 /// 1474 /// \returns the number of words to hold the integer value of this APInt. 1475 unsigned getNumWords() const { return getNumWords(BitWidth); } 1476 1477 /// Get the number of words. 1478 /// 1479 /// *NOTE* Here one word's bitwidth equals to that of uint64_t. 1480 /// 1481 /// \returns the number of words to hold the integer value with a given bit 1482 /// width. 1483 static unsigned getNumWords(unsigned BitWidth) { 1484 return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; 1485 } 1486 1487 /// Compute the number of active bits in the value 1488 /// 1489 /// This function returns the number of active bits which is defined as the 1490 /// bit width minus the number of leading zeros. This is used in several 1491 /// computations to see how "wide" the value is. 1492 unsigned getActiveBits() const { return BitWidth - countl_zero(); } 1493 1494 /// Compute the number of active words in the value of this APInt. 1495 /// 1496 /// This is used in conjunction with getActiveData to extract the raw value of 1497 /// the APInt. 1498 unsigned getActiveWords() const { 1499 unsigned numActiveBits = getActiveBits(); 1500 return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; 1501 } 1502 1503 /// Get the minimum bit size for this signed APInt 1504 /// 1505 /// Computes the minimum bit width for this APInt while considering it to be a 1506 /// signed (and probably negative) value. If the value is not negative, this 1507 /// function returns the same value as getActiveBits()+1. Otherwise, it 1508 /// returns the smallest bit width that will retain the negative value. For 1509 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so 1510 /// for -1, this function will always return 1. 1511 unsigned getSignificantBits() const { 1512 return BitWidth - getNumSignBits() + 1; 1513 } 1514 1515 /// Get zero extended value 1516 /// 1517 /// This method attempts to return the value of this APInt as a zero extended 1518 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a 1519 /// uint64_t. Otherwise an assertion will result. 1520 uint64_t getZExtValue() const { 1521 if (isSingleWord()) 1522 return U.VAL; 1523 assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); 1524 return U.pVal[0]; 1525 } 1526 1527 /// Get zero extended value if possible 1528 /// 1529 /// This method attempts to return the value of this APInt as a zero extended 1530 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a 1531 /// uint64_t. Otherwise no value is returned. 1532 std::optional<uint64_t> tryZExtValue() const { 1533 return (getActiveBits() <= 64) ? std::optional<uint64_t>(getZExtValue()) 1534 : std::nullopt; 1535 }; 1536 1537 /// Get sign extended value 1538 /// 1539 /// This method attempts to return the value of this APInt as a sign extended 1540 /// int64_t. The bit width must be <= 64 or the value must fit within an 1541 /// int64_t. Otherwise an assertion will result. 1542 int64_t getSExtValue() const { 1543 if (isSingleWord()) 1544 return SignExtend64(U.VAL, BitWidth); 1545 assert(getSignificantBits() <= 64 && "Too many bits for int64_t"); 1546 return int64_t(U.pVal[0]); 1547 } 1548 1549 /// Get sign extended value if possible 1550 /// 1551 /// This method attempts to return the value of this APInt as a sign extended 1552 /// int64_t. The bitwidth must be <= 64 or the value must fit within an 1553 /// int64_t. Otherwise no value is returned. 1554 std::optional<int64_t> trySExtValue() const { 1555 return (getSignificantBits() <= 64) ? std::optional<int64_t>(getSExtValue()) 1556 : std::nullopt; 1557 }; 1558 1559 /// Get bits required for string value. 1560 /// 1561 /// This method determines how many bits are required to hold the APInt 1562 /// equivalent of the string given by \p str. 1563 static unsigned getBitsNeeded(StringRef str, uint8_t radix); 1564 1565 /// Get the bits that are sufficient to represent the string value. This may 1566 /// over estimate the amount of bits required, but it does not require 1567 /// parsing the value in the string. 1568 static unsigned getSufficientBitsNeeded(StringRef Str, uint8_t Radix); 1569 1570 /// The APInt version of std::countl_zero. 1571 /// 1572 /// It counts the number of zeros from the most significant bit to the first 1573 /// one bit. 1574 /// 1575 /// \returns BitWidth if the value is zero, otherwise returns the number of 1576 /// zeros from the most significant bit to the first one bits. 1577 unsigned countl_zero() const { 1578 if (isSingleWord()) { 1579 unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; 1580 return llvm::countl_zero(U.VAL) - unusedBits; 1581 } 1582 return countLeadingZerosSlowCase(); 1583 } 1584 1585 unsigned countLeadingZeros() const { return countl_zero(); } 1586 1587 /// Count the number of leading one bits. 1588 /// 1589 /// This function is an APInt version of std::countl_one. It counts the number 1590 /// of ones from the most significant bit to the first zero bit. 1591 /// 1592 /// \returns 0 if the high order bit is not set, otherwise returns the number 1593 /// of 1 bits from the most significant to the least 1594 unsigned countl_one() const { 1595 if (isSingleWord()) { 1596 if (LLVM_UNLIKELY(BitWidth == 0)) 1597 return 0; 1598 return llvm::countl_one(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); 1599 } 1600 return countLeadingOnesSlowCase(); 1601 } 1602 1603 unsigned countLeadingOnes() const { return countl_one(); } 1604 1605 /// Computes the number of leading bits of this APInt that are equal to its 1606 /// sign bit. 1607 unsigned getNumSignBits() const { 1608 return isNegative() ? countl_one() : countl_zero(); 1609 } 1610 1611 /// Count the number of trailing zero bits. 1612 /// 1613 /// This function is an APInt version of std::countr_zero. It counts the 1614 /// number of zeros from the least significant bit to the first set bit. 1615 /// 1616 /// \returns BitWidth if the value is zero, otherwise returns the number of 1617 /// zeros from the least significant bit to the first one bit. 1618 unsigned countr_zero() const { 1619 if (isSingleWord()) { 1620 unsigned TrailingZeros = llvm::countr_zero(U.VAL); 1621 return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros); 1622 } 1623 return countTrailingZerosSlowCase(); 1624 } 1625 1626 unsigned countTrailingZeros() const { return countr_zero(); } 1627 1628 /// Count the number of trailing one bits. 1629 /// 1630 /// This function is an APInt version of std::countr_one. It counts the number 1631 /// of ones from the least significant bit to the first zero bit. 1632 /// 1633 /// \returns BitWidth if the value is all ones, otherwise returns the number 1634 /// of ones from the least significant bit to the first zero bit. 1635 unsigned countr_one() const { 1636 if (isSingleWord()) 1637 return llvm::countr_one(U.VAL); 1638 return countTrailingOnesSlowCase(); 1639 } 1640 1641 unsigned countTrailingOnes() const { return countr_one(); } 1642 1643 /// Count the number of bits set. 1644 /// 1645 /// This function is an APInt version of std::popcount. It counts the number 1646 /// of 1 bits in the APInt value. 1647 /// 1648 /// \returns 0 if the value is zero, otherwise returns the number of set bits. 1649 unsigned popcount() const { 1650 if (isSingleWord()) 1651 return llvm::popcount(U.VAL); 1652 return countPopulationSlowCase(); 1653 } 1654 1655 /// @} 1656 /// \name Conversion Functions 1657 /// @{ 1658 void print(raw_ostream &OS, bool isSigned) const; 1659 1660 /// Converts an APInt to a string and append it to Str. Str is commonly a 1661 /// SmallString. If Radix > 10, UpperCase determine the case of letter 1662 /// digits. 1663 void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, 1664 bool formatAsCLiteral = false, bool UpperCase = true, 1665 bool InsertSeparators = false) const; 1666 1667 /// Considers the APInt to be unsigned and converts it into a string in the 1668 /// radix given. The radix can be 2, 8, 10 16, or 36. 1669 void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { 1670 toString(Str, Radix, false, false); 1671 } 1672 1673 /// Considers the APInt to be signed and converts it into a string in the 1674 /// radix given. The radix can be 2, 8, 10, 16, or 36. 1675 void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { 1676 toString(Str, Radix, true, false); 1677 } 1678 1679 /// \returns a byte-swapped representation of this APInt Value. 1680 APInt byteSwap() const; 1681 1682 /// \returns the value with the bit representation reversed of this APInt 1683 /// Value. 1684 APInt reverseBits() const; 1685 1686 /// Converts this APInt to a double value. 1687 double roundToDouble(bool isSigned) const; 1688 1689 /// Converts this unsigned APInt to a double value. 1690 double roundToDouble() const { return roundToDouble(false); } 1691 1692 /// Converts this signed APInt to a double value. 1693 double signedRoundToDouble() const { return roundToDouble(true); } 1694 1695 /// Converts APInt bits to a double 1696 /// 1697 /// The conversion does not do a translation from integer to double, it just 1698 /// re-interprets the bits as a double. Note that it is valid to do this on 1699 /// any bit width. Exactly 64 bits will be translated. 1700 double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); } 1701 1702 #ifdef HAS_IEE754_FLOAT128 1703 float128 bitsToQuad() const { 1704 __uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0]; 1705 return llvm::bit_cast<float128>(ul); 1706 } 1707 #endif 1708 1709 /// Converts APInt bits to a float 1710 /// 1711 /// The conversion does not do a translation from integer to float, it just 1712 /// re-interprets the bits as a float. Note that it is valid to do this on 1713 /// any bit width. Exactly 32 bits will be translated. 1714 float bitsToFloat() const { 1715 return llvm::bit_cast<float>(static_cast<uint32_t>(getWord(0))); 1716 } 1717 1718 /// Converts a double to APInt bits. 1719 /// 1720 /// The conversion does not do a translation from double to integer, it just 1721 /// re-interprets the bits of the double. 1722 static APInt doubleToBits(double V) { 1723 return APInt(sizeof(double) * CHAR_BIT, llvm::bit_cast<uint64_t>(V)); 1724 } 1725 1726 /// Converts a float to APInt bits. 1727 /// 1728 /// The conversion does not do a translation from float to integer, it just 1729 /// re-interprets the bits of the float. 1730 static APInt floatToBits(float V) { 1731 return APInt(sizeof(float) * CHAR_BIT, llvm::bit_cast<uint32_t>(V)); 1732 } 1733 1734 /// @} 1735 /// \name Mathematics Operations 1736 /// @{ 1737 1738 /// \returns the floor log base 2 of this APInt. 1739 unsigned logBase2() const { return getActiveBits() - 1; } 1740 1741 /// \returns the ceil log base 2 of this APInt. 1742 unsigned ceilLogBase2() const { 1743 APInt temp(*this); 1744 --temp; 1745 return temp.getActiveBits(); 1746 } 1747 1748 /// \returns the nearest log base 2 of this APInt. Ties round up. 1749 /// 1750 /// NOTE: When we have a BitWidth of 1, we define: 1751 /// 1752 /// log2(0) = UINT32_MAX 1753 /// log2(1) = 0 1754 /// 1755 /// to get around any mathematical concerns resulting from 1756 /// referencing 2 in a space where 2 does no exist. 1757 unsigned nearestLogBase2() const; 1758 1759 /// \returns the log base 2 of this APInt if its an exact power of two, -1 1760 /// otherwise 1761 int32_t exactLogBase2() const { 1762 if (!isPowerOf2()) 1763 return -1; 1764 return logBase2(); 1765 } 1766 1767 /// Compute the square root. 1768 APInt sqrt() const; 1769 1770 /// Get the absolute value. If *this is < 0 then return -(*this), otherwise 1771 /// *this. Note that the "most negative" signed number (e.g. -128 for 8 bit 1772 /// wide APInt) is unchanged due to how negation works. 1773 APInt abs() const { 1774 if (isNegative()) 1775 return -(*this); 1776 return *this; 1777 } 1778 1779 /// \returns the multiplicative inverse of an odd APInt modulo 2^BitWidth. 1780 APInt multiplicativeInverse() const; 1781 1782 /// @} 1783 /// \name Building-block Operations for APInt and APFloat 1784 /// @{ 1785 1786 // These building block operations operate on a representation of arbitrary 1787 // precision, two's-complement, bignum integer values. They should be 1788 // sufficient to implement APInt and APFloat bignum requirements. Inputs are 1789 // generally a pointer to the base of an array of integer parts, representing 1790 // an unsigned bignum, and a count of how many parts there are. 1791 1792 /// Sets the least significant part of a bignum to the input value, and zeroes 1793 /// out higher parts. 1794 static void tcSet(WordType *, WordType, unsigned); 1795 1796 /// Assign one bignum to another. 1797 static void tcAssign(WordType *, const WordType *, unsigned); 1798 1799 /// Returns true if a bignum is zero, false otherwise. 1800 static bool tcIsZero(const WordType *, unsigned); 1801 1802 /// Extract the given bit of a bignum; returns 0 or 1. Zero-based. 1803 static int tcExtractBit(const WordType *, unsigned bit); 1804 1805 /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to 1806 /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least 1807 /// significant bit of DST. All high bits above srcBITS in DST are 1808 /// zero-filled. 1809 static void tcExtract(WordType *, unsigned dstCount, const WordType *, 1810 unsigned srcBits, unsigned srcLSB); 1811 1812 /// Set the given bit of a bignum. Zero-based. 1813 static void tcSetBit(WordType *, unsigned bit); 1814 1815 /// Clear the given bit of a bignum. Zero-based. 1816 static void tcClearBit(WordType *, unsigned bit); 1817 1818 /// Returns the bit number of the least or most significant set bit of a 1819 /// number. If the input number has no bits set -1U is returned. 1820 static unsigned tcLSB(const WordType *, unsigned n); 1821 static unsigned tcMSB(const WordType *parts, unsigned n); 1822 1823 /// Negate a bignum in-place. 1824 static void tcNegate(WordType *, unsigned); 1825 1826 /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag. 1827 static WordType tcAdd(WordType *, const WordType *, WordType carry, unsigned); 1828 /// DST += RHS. Returns the carry flag. 1829 static WordType tcAddPart(WordType *, WordType, unsigned); 1830 1831 /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag. 1832 static WordType tcSubtract(WordType *, const WordType *, WordType carry, 1833 unsigned); 1834 /// DST -= RHS. Returns the carry flag. 1835 static WordType tcSubtractPart(WordType *, WordType, unsigned); 1836 1837 /// DST += SRC * MULTIPLIER + PART if add is true 1838 /// DST = SRC * MULTIPLIER + PART if add is false 1839 /// 1840 /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must 1841 /// start at the same point, i.e. DST == SRC. 1842 /// 1843 /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned. 1844 /// Otherwise DST is filled with the least significant DSTPARTS parts of the 1845 /// result, and if all of the omitted higher parts were zero return zero, 1846 /// otherwise overflow occurred and return one. 1847 static int tcMultiplyPart(WordType *dst, const WordType *src, 1848 WordType multiplier, WordType carry, 1849 unsigned srcParts, unsigned dstParts, bool add); 1850 1851 /// DST = LHS * RHS, where DST has the same width as the operands and is 1852 /// filled with the least significant parts of the result. Returns one if 1853 /// overflow occurred, otherwise zero. DST must be disjoint from both 1854 /// operands. 1855 static int tcMultiply(WordType *, const WordType *, const WordType *, 1856 unsigned); 1857 1858 /// DST = LHS * RHS, where DST has width the sum of the widths of the 1859 /// operands. No overflow occurs. DST must be disjoint from both operands. 1860 static void tcFullMultiply(WordType *, const WordType *, const WordType *, 1861 unsigned, unsigned); 1862 1863 /// If RHS is zero LHS and REMAINDER are left unchanged, return one. 1864 /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set 1865 /// REMAINDER to the remainder, return zero. i.e. 1866 /// 1867 /// OLD_LHS = RHS * LHS + REMAINDER 1868 /// 1869 /// SCRATCH is a bignum of the same size as the operands and result for use by 1870 /// the routine; its contents need not be initialized and are destroyed. LHS, 1871 /// REMAINDER and SCRATCH must be distinct. 1872 static int tcDivide(WordType *lhs, const WordType *rhs, WordType *remainder, 1873 WordType *scratch, unsigned parts); 1874 1875 /// Shift a bignum left Count bits. Shifted in bits are zero. There are no 1876 /// restrictions on Count. 1877 static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); 1878 1879 /// Shift a bignum right Count bits. Shifted in bits are zero. There are no 1880 /// restrictions on Count. 1881 static void tcShiftRight(WordType *, unsigned Words, unsigned Count); 1882 1883 /// Comparison (unsigned) of two bignums. 1884 static int tcCompare(const WordType *, const WordType *, unsigned); 1885 1886 /// Increment a bignum in-place. Return the carry flag. 1887 static WordType tcIncrement(WordType *dst, unsigned parts) { 1888 return tcAddPart(dst, 1, parts); 1889 } 1890 1891 /// Decrement a bignum in-place. Return the borrow flag. 1892 static WordType tcDecrement(WordType *dst, unsigned parts) { 1893 return tcSubtractPart(dst, 1, parts); 1894 } 1895 1896 /// Used to insert APInt objects, or objects that contain APInt objects, into 1897 /// FoldingSets. 1898 void Profile(FoldingSetNodeID &id) const; 1899 1900 /// debug method 1901 void dump() const; 1902 1903 /// Returns whether this instance allocated memory. 1904 bool needsCleanup() const { return !isSingleWord(); } 1905 1906 private: 1907 /// This union is used to store the integer value. When the 1908 /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal. 1909 union { 1910 uint64_t VAL; ///< Used to store the <= 64 bits integer value. 1911 uint64_t *pVal; ///< Used to store the >64 bits integer value. 1912 } U; 1913 1914 unsigned BitWidth = 1; ///< The number of bits in this APInt. 1915 1916 friend struct DenseMapInfo<APInt, void>; 1917 friend class APSInt; 1918 1919 // Make DynamicAPInt a friend so it can access BitWidth directly. 1920 friend DynamicAPInt; 1921 1922 /// This constructor is used only internally for speed of construction of 1923 /// temporaries. It is unsafe since it takes ownership of the pointer, so it 1924 /// is not public. 1925 APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { U.pVal = val; } 1926 1927 /// Determine which word a bit is in. 1928 /// 1929 /// \returns the word position for the specified bit position. 1930 static unsigned whichWord(unsigned bitPosition) { 1931 return bitPosition / APINT_BITS_PER_WORD; 1932 } 1933 1934 /// Determine which bit in a word the specified bit position is in. 1935 static unsigned whichBit(unsigned bitPosition) { 1936 return bitPosition % APINT_BITS_PER_WORD; 1937 } 1938 1939 /// Get a single bit mask. 1940 /// 1941 /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set 1942 /// This method generates and returns a uint64_t (word) mask for a single 1943 /// bit at a specific bit position. This is used to mask the bit in the 1944 /// corresponding word. 1945 static uint64_t maskBit(unsigned bitPosition) { 1946 return 1ULL << whichBit(bitPosition); 1947 } 1948 1949 /// Clear unused high order bits 1950 /// 1951 /// This method is used internally to clear the top "N" bits in the high order 1952 /// word that are not used by the APInt. This is needed after the most 1953 /// significant word is assigned a value to ensure that those bits are 1954 /// zero'd out. 1955 APInt &clearUnusedBits() { 1956 // Compute how many bits are used in the final word. 1957 unsigned WordBits = ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1; 1958 1959 // Mask out the high bits. 1960 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); 1961 if (LLVM_UNLIKELY(BitWidth == 0)) 1962 mask = 0; 1963 1964 if (isSingleWord()) 1965 U.VAL &= mask; 1966 else 1967 U.pVal[getNumWords() - 1] &= mask; 1968 return *this; 1969 } 1970 1971 /// Get the word corresponding to a bit position 1972 /// \returns the corresponding word for the specified bit position. 1973 uint64_t getWord(unsigned bitPosition) const { 1974 return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; 1975 } 1976 1977 /// Utility method to change the bit width of this APInt to new bit width, 1978 /// allocating and/or deallocating as necessary. There is no guarantee on the 1979 /// value of any bits upon return. Caller should populate the bits after. 1980 void reallocate(unsigned NewBitWidth); 1981 1982 /// Convert a char array into an APInt 1983 /// 1984 /// \param radix 2, 8, 10, 16, or 36 1985 /// Converts a string into a number. The string must be non-empty 1986 /// and well-formed as a number of the given base. The bit-width 1987 /// must be sufficient to hold the result. 1988 /// 1989 /// This is used by the constructors that take string arguments. 1990 /// 1991 /// StringRef::getAsInteger is superficially similar but (1) does 1992 /// not assume that the string is well-formed and (2) grows the 1993 /// result to hold the input. 1994 void fromString(unsigned numBits, StringRef str, uint8_t radix); 1995 1996 /// An internal division function for dividing APInts. 1997 /// 1998 /// This is used by the toString method to divide by the radix. It simply 1999 /// provides a more convenient form of divide for internal use since KnuthDiv 2000 /// has specific constraints on its inputs. If those constraints are not met 2001 /// then it provides a simpler form of divide. 2002 static void divide(const WordType *LHS, unsigned lhsWords, 2003 const WordType *RHS, unsigned rhsWords, WordType *Quotient, 2004 WordType *Remainder); 2005 2006 /// out-of-line slow case for inline constructor 2007 void initSlowCase(uint64_t val, bool isSigned); 2008 2009 /// shared code between two array constructors 2010 void initFromArray(ArrayRef<uint64_t> array); 2011 2012 /// out-of-line slow case for inline copy constructor 2013 void initSlowCase(const APInt &that); 2014 2015 /// out-of-line slow case for shl 2016 void shlSlowCase(unsigned ShiftAmt); 2017 2018 /// out-of-line slow case for lshr. 2019 void lshrSlowCase(unsigned ShiftAmt); 2020 2021 /// out-of-line slow case for ashr. 2022 void ashrSlowCase(unsigned ShiftAmt); 2023 2024 /// out-of-line slow case for operator= 2025 void assignSlowCase(const APInt &RHS); 2026 2027 /// out-of-line slow case for operator== 2028 bool equalSlowCase(const APInt &RHS) const LLVM_READONLY; 2029 2030 /// out-of-line slow case for countLeadingZeros 2031 unsigned countLeadingZerosSlowCase() const LLVM_READONLY; 2032 2033 /// out-of-line slow case for countLeadingOnes. 2034 unsigned countLeadingOnesSlowCase() const LLVM_READONLY; 2035 2036 /// out-of-line slow case for countTrailingZeros. 2037 unsigned countTrailingZerosSlowCase() const LLVM_READONLY; 2038 2039 /// out-of-line slow case for countTrailingOnes 2040 unsigned countTrailingOnesSlowCase() const LLVM_READONLY; 2041 2042 /// out-of-line slow case for countPopulation 2043 unsigned countPopulationSlowCase() const LLVM_READONLY; 2044 2045 /// out-of-line slow case for intersects. 2046 bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY; 2047 2048 /// out-of-line slow case for isSubsetOf. 2049 bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY; 2050 2051 /// out-of-line slow case for setBits. 2052 void setBitsSlowCase(unsigned loBit, unsigned hiBit); 2053 2054 /// out-of-line slow case for flipAllBits. 2055 void flipAllBitsSlowCase(); 2056 2057 /// out-of-line slow case for concat. 2058 APInt concatSlowCase(const APInt &NewLSB) const; 2059 2060 /// out-of-line slow case for operator&=. 2061 void andAssignSlowCase(const APInt &RHS); 2062 2063 /// out-of-line slow case for operator|=. 2064 void orAssignSlowCase(const APInt &RHS); 2065 2066 /// out-of-line slow case for operator^=. 2067 void xorAssignSlowCase(const APInt &RHS); 2068 2069 /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal 2070 /// to, or greater than RHS. 2071 int compare(const APInt &RHS) const LLVM_READONLY; 2072 2073 /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal 2074 /// to, or greater than RHS. 2075 int compareSigned(const APInt &RHS) const LLVM_READONLY; 2076 2077 /// @} 2078 }; 2079 2080 inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } 2081 2082 inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } 2083 2084 /// Unary bitwise complement operator. 2085 /// 2086 /// \returns an APInt that is the bitwise complement of \p v. 2087 inline APInt operator~(APInt v) { 2088 v.flipAllBits(); 2089 return v; 2090 } 2091 2092 inline APInt operator&(APInt a, const APInt &b) { 2093 a &= b; 2094 return a; 2095 } 2096 2097 inline APInt operator&(const APInt &a, APInt &&b) { 2098 b &= a; 2099 return std::move(b); 2100 } 2101 2102 inline APInt operator&(APInt a, uint64_t RHS) { 2103 a &= RHS; 2104 return a; 2105 } 2106 2107 inline APInt operator&(uint64_t LHS, APInt b) { 2108 b &= LHS; 2109 return b; 2110 } 2111 2112 inline APInt operator|(APInt a, const APInt &b) { 2113 a |= b; 2114 return a; 2115 } 2116 2117 inline APInt operator|(const APInt &a, APInt &&b) { 2118 b |= a; 2119 return std::move(b); 2120 } 2121 2122 inline APInt operator|(APInt a, uint64_t RHS) { 2123 a |= RHS; 2124 return a; 2125 } 2126 2127 inline APInt operator|(uint64_t LHS, APInt b) { 2128 b |= LHS; 2129 return b; 2130 } 2131 2132 inline APInt operator^(APInt a, const APInt &b) { 2133 a ^= b; 2134 return a; 2135 } 2136 2137 inline APInt operator^(const APInt &a, APInt &&b) { 2138 b ^= a; 2139 return std::move(b); 2140 } 2141 2142 inline APInt operator^(APInt a, uint64_t RHS) { 2143 a ^= RHS; 2144 return a; 2145 } 2146 2147 inline APInt operator^(uint64_t LHS, APInt b) { 2148 b ^= LHS; 2149 return b; 2150 } 2151 2152 inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { 2153 I.print(OS, true); 2154 return OS; 2155 } 2156 2157 inline APInt operator-(APInt v) { 2158 v.negate(); 2159 return v; 2160 } 2161 2162 inline APInt operator+(APInt a, const APInt &b) { 2163 a += b; 2164 return a; 2165 } 2166 2167 inline APInt operator+(const APInt &a, APInt &&b) { 2168 b += a; 2169 return std::move(b); 2170 } 2171 2172 inline APInt operator+(APInt a, uint64_t RHS) { 2173 a += RHS; 2174 return a; 2175 } 2176 2177 inline APInt operator+(uint64_t LHS, APInt b) { 2178 b += LHS; 2179 return b; 2180 } 2181 2182 inline APInt operator-(APInt a, const APInt &b) { 2183 a -= b; 2184 return a; 2185 } 2186 2187 inline APInt operator-(const APInt &a, APInt &&b) { 2188 b.negate(); 2189 b += a; 2190 return std::move(b); 2191 } 2192 2193 inline APInt operator-(APInt a, uint64_t RHS) { 2194 a -= RHS; 2195 return a; 2196 } 2197 2198 inline APInt operator-(uint64_t LHS, APInt b) { 2199 b.negate(); 2200 b += LHS; 2201 return b; 2202 } 2203 2204 inline APInt operator*(APInt a, uint64_t RHS) { 2205 a *= RHS; 2206 return a; 2207 } 2208 2209 inline APInt operator*(uint64_t LHS, APInt b) { 2210 b *= LHS; 2211 return b; 2212 } 2213 2214 namespace APIntOps { 2215 2216 /// Determine the smaller of two APInts considered to be signed. 2217 inline const APInt &smin(const APInt &A, const APInt &B) { 2218 return A.slt(B) ? A : B; 2219 } 2220 2221 /// Determine the larger of two APInts considered to be signed. 2222 inline const APInt &smax(const APInt &A, const APInt &B) { 2223 return A.sgt(B) ? A : B; 2224 } 2225 2226 /// Determine the smaller of two APInts considered to be unsigned. 2227 inline const APInt &umin(const APInt &A, const APInt &B) { 2228 return A.ult(B) ? A : B; 2229 } 2230 2231 /// Determine the larger of two APInts considered to be unsigned. 2232 inline const APInt &umax(const APInt &A, const APInt &B) { 2233 return A.ugt(B) ? A : B; 2234 } 2235 2236 /// Determine the absolute difference of two APInts considered to be signed. 2237 inline const APInt abds(const APInt &A, const APInt &B) { 2238 return A.sge(B) ? (A - B) : (B - A); 2239 } 2240 2241 /// Determine the absolute difference of two APInts considered to be unsigned. 2242 inline const APInt abdu(const APInt &A, const APInt &B) { 2243 return A.uge(B) ? (A - B) : (B - A); 2244 } 2245 2246 /// Compute the floor of the signed average of C1 and C2 2247 APInt avgFloorS(const APInt &C1, const APInt &C2); 2248 2249 /// Compute the floor of the unsigned average of C1 and C2 2250 APInt avgFloorU(const APInt &C1, const APInt &C2); 2251 2252 /// Compute the ceil of the signed average of C1 and C2 2253 APInt avgCeilS(const APInt &C1, const APInt &C2); 2254 2255 /// Compute the ceil of the unsigned average of C1 and C2 2256 APInt avgCeilU(const APInt &C1, const APInt &C2); 2257 2258 /// Performs (2*N)-bit multiplication on sign-extended operands. 2259 /// Returns the high N bits of the multiplication result. 2260 APInt mulhs(const APInt &C1, const APInt &C2); 2261 2262 /// Performs (2*N)-bit multiplication on zero-extended operands. 2263 /// Returns the high N bits of the multiplication result. 2264 APInt mulhu(const APInt &C1, const APInt &C2); 2265 2266 /// Compute X^N for N>=0. 2267 /// 0^0 is supported and returns 1. 2268 APInt pow(const APInt &X, int64_t N); 2269 2270 /// Compute GCD of two unsigned APInt values. 2271 /// 2272 /// This function returns the greatest common divisor of the two APInt values 2273 /// using Stein's algorithm. 2274 /// 2275 /// \returns the greatest common divisor of A and B. 2276 APInt GreatestCommonDivisor(APInt A, APInt B); 2277 2278 /// Converts the given APInt to a double value. 2279 /// 2280 /// Treats the APInt as an unsigned value for conversion purposes. 2281 inline double RoundAPIntToDouble(const APInt &APIVal) { 2282 return APIVal.roundToDouble(); 2283 } 2284 2285 /// Converts the given APInt to a double value. 2286 /// 2287 /// Treats the APInt as a signed value for conversion purposes. 2288 inline double RoundSignedAPIntToDouble(const APInt &APIVal) { 2289 return APIVal.signedRoundToDouble(); 2290 } 2291 2292 /// Converts the given APInt to a float value. 2293 inline float RoundAPIntToFloat(const APInt &APIVal) { 2294 return float(RoundAPIntToDouble(APIVal)); 2295 } 2296 2297 /// Converts the given APInt to a float value. 2298 /// 2299 /// Treats the APInt as a signed value for conversion purposes. 2300 inline float RoundSignedAPIntToFloat(const APInt &APIVal) { 2301 return float(APIVal.signedRoundToDouble()); 2302 } 2303 2304 /// Converts the given double value into a APInt. 2305 /// 2306 /// This function convert a double value to an APInt value. 2307 APInt RoundDoubleToAPInt(double Double, unsigned width); 2308 2309 /// Converts a float value into a APInt. 2310 /// 2311 /// Converts a float value into an APInt value. 2312 inline APInt RoundFloatToAPInt(float Float, unsigned width) { 2313 return RoundDoubleToAPInt(double(Float), width); 2314 } 2315 2316 /// Return A unsign-divided by B, rounded by the given rounding mode. 2317 APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); 2318 2319 /// Return A sign-divided by B, rounded by the given rounding mode. 2320 APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); 2321 2322 /// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range 2323 /// (e.g. 32 for i32). 2324 /// This function finds the smallest number n, such that 2325 /// (a) n >= 0 and q(n) = 0, or 2326 /// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all 2327 /// integers, belong to two different intervals [Rk, Rk+R), 2328 /// where R = 2^BW, and k is an integer. 2329 /// The idea here is to find when q(n) "overflows" 2^BW, while at the 2330 /// same time "allowing" subtraction. In unsigned modulo arithmetic a 2331 /// subtraction (treated as addition of negated numbers) would always 2332 /// count as an overflow, but here we want to allow values to decrease 2333 /// and increase as long as they are within the same interval. 2334 /// Specifically, adding of two negative numbers should not cause an 2335 /// overflow (as long as the magnitude does not exceed the bit width). 2336 /// On the other hand, given a positive number, adding a negative 2337 /// number to it can give a negative result, which would cause the 2338 /// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is 2339 /// treated as a special case of an overflow. 2340 /// 2341 /// This function returns std::nullopt if after finding k that minimizes the 2342 /// positive solution to q(n) = kR, both solutions are contained between 2343 /// two consecutive integers. 2344 /// 2345 /// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation 2346 /// in arithmetic modulo 2^BW, and treating the values as signed) by the 2347 /// virtue of *signed* overflow. This function will *not* find such an n, 2348 /// however it may find a value of n satisfying the inequalities due to 2349 /// an *unsigned* overflow (if the values are treated as unsigned). 2350 /// To find a solution for a signed overflow, treat it as a problem of 2351 /// finding an unsigned overflow with a range with of BW-1. 2352 /// 2353 /// The returned value may have a different bit width from the input 2354 /// coefficients. 2355 std::optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, 2356 unsigned RangeWidth); 2357 2358 /// Compare two values, and if they are different, return the position of the 2359 /// most significant bit that is different in the values. 2360 std::optional<unsigned> GetMostSignificantDifferentBit(const APInt &A, 2361 const APInt &B); 2362 2363 /// Splat/Merge neighboring bits to widen/narrow the bitmask represented 2364 /// by \param A to \param NewBitWidth bits. 2365 /// 2366 /// MatchAnyBits: (Default) 2367 /// e.g. ScaleBitMask(0b0101, 8) -> 0b00110011 2368 /// e.g. ScaleBitMask(0b00011011, 4) -> 0b0111 2369 /// 2370 /// MatchAllBits: 2371 /// e.g. ScaleBitMask(0b0101, 8) -> 0b00110011 2372 /// e.g. ScaleBitMask(0b00011011, 4) -> 0b0001 2373 /// A.getBitwidth() or NewBitWidth must be a whole multiples of the other. 2374 APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, 2375 bool MatchAllBits = false); 2376 } // namespace APIntOps 2377 2378 // See friend declaration above. This additional declaration is required in 2379 // order to compile LLVM with IBM xlC compiler. 2380 hash_code hash_value(const APInt &Arg); 2381 2382 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst 2383 /// with the integer held in IntVal. 2384 void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes); 2385 2386 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting 2387 /// from Src into IntVal, which is assumed to be wide enough and to hold zero. 2388 void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes); 2389 2390 /// Provide DenseMapInfo for APInt. 2391 template <> struct DenseMapInfo<APInt, void> { 2392 static inline APInt getEmptyKey() { 2393 APInt V(nullptr, 0); 2394 V.U.VAL = ~0ULL; 2395 return V; 2396 } 2397 2398 static inline APInt getTombstoneKey() { 2399 APInt V(nullptr, 0); 2400 V.U.VAL = ~1ULL; 2401 return V; 2402 } 2403 2404 static unsigned getHashValue(const APInt &Key); 2405 2406 static bool isEqual(const APInt &LHS, const APInt &RHS) { 2407 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; 2408 } 2409 }; 2410 2411 } // namespace llvm 2412 2413 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|