From 19dc32a2d422ac0aafd047514e3e5e727796696e Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Sun, 13 May 2007 23:44:59 +0000 Subject: Add some things needed by the llvm-gcc version supporting bit accurate integer types: 1. Functions to compute div/rem at the same time. 2. Further assurance that an APInt with 0 bitwidth cannot be constructed. 3. Left and right rotate operations. 4. An exactLogBase2 function which requires an exact power of two or it returns -1. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37025 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/APInt.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'include/llvm/ADT') diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index 36ced6f..535e5db 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -567,6 +567,12 @@ public: /// @brief Left-shift function. APInt shl(uint32_t shiftAmt) const; + /// @brief Rotate left by rotateAmt. + APInt rotl(uint32_t rotateAmt) const; + + /// @brief Rotate right by rotateAmt. + APInt rotr(uint32_t rotateAmt) const; + /// Perform an unsigned divide operation on this APInt by RHS. Both this and /// RHS are treated as unsigned quantities for purposes of this division. /// @returns a new APInt value containing the division result @@ -608,6 +614,31 @@ public: return this->urem(RHS); } + /// Sometimes it is convenient to divide two APInt values and obtain both + /// the quotient and remainder. This function does both operations in the + /// same computation making it a little more efficient. + /// @brief Dual division/remainder interface. + static void udivrem(const APInt &LHS, const APInt &RHS, + APInt &Quotient, APInt &Remainder); + + static void sdivrem(const APInt &LHS, const APInt &RHS, + APInt &Quotient, APInt &Remainder) + { + if (LHS.isNegative()) { + if (RHS.isNegative()) + APInt::udivrem(-LHS, -RHS, Quotient, Remainder); + else + APInt::udivrem(-LHS, RHS, Quotient, Remainder); + Quotient = -Quotient; + Remainder = -Remainder; + } else if (RHS.isNegative()) { + APInt::udivrem(LHS, -RHS, Quotient, Remainder); + Quotient = -Quotient; + } else { + APInt::udivrem(LHS, RHS, Quotient, Remainder); + } + } + /// @returns the bit value at bitPosition /// @brief Array-indexing support. bool operator[](uint32_t bitPosition) const; @@ -988,6 +1019,14 @@ public: return BitWidth - 1 - countLeadingZeros(); } + /// @returns the log base 2 of this APInt if its an exact power of two, -1 + /// otherwise + inline int32_t exactLogBase2() const { + if (!isPowerOf2()) + return -1; + return logBase2(); + } + /// @brief Compute the square root APInt sqrt() const; -- cgit v1.1