[llvm-commits] CVS: llvm/include/llvm/ADT/APInt.h

Reid Spencer reid at x10sys.com
Sun Feb 25 11:26:17 PST 2007



Changes in directory llvm/include/llvm/ADT:

APInt.h updated: 1.18 -> 1.19
---
Log message:

Fix clearUnusedBits to not depend on "undefined behavior" of >> operator
when the bit size is equal to the word size. This happens to work out okay
on x86, but might not on other platforms. The change just detects when 
there are no bits to clear (because BitWidth is a multiple of the word size)
and returns early.

Also, move some comments from .cpp file into header.


---
Diffs of the changes:  (+40 -9)

 APInt.h |   49 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 40 insertions(+), 9 deletions(-)


Index: llvm/include/llvm/ADT/APInt.h
diff -u llvm/include/llvm/ADT/APInt.h:1.18 llvm/include/llvm/ADT/APInt.h:1.19
--- llvm/include/llvm/ADT/APInt.h:1.18	Sun Feb 25 01:29:03 2007
+++ llvm/include/llvm/ADT/APInt.h	Sun Feb 25 13:26:01 2007
@@ -114,12 +114,22 @@
   /// by the APInt. This is needed after the most significant word is assigned 
   /// a value to ensure that those bits are zero'd out.
   /// @brief Clear high order bits
-  inline void clearUnusedBits() {
+  inline APInt& clearUnusedBits() {
+    // Compute how many bits are used in the final word
+    uint32_t wordBits = BitWidth % APINT_BITS_PER_WORD;
+    if (wordBits == 0)
+      // If all bits are used, we want to leave the value alone. This also
+      // avoids the undefined behavior of >> when the shfit is the same size as
+      // the word size (64).
+      return *this;
+
+    // Mask out the hight bits.
+    uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
     if (isSingleWord())
-      VAL &= ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - BitWidth);
+      VAL &= mask;
     else
-      pVal[getNumWords() - 1] &= ~uint64_t(0ULL) >> 
-        (APINT_BITS_PER_WORD - (whichBit(BitWidth - 1) + 1));
+      pVal[getNumWords() - 1] &= mask;
+    return *this;
   }
 
   /// @returns the corresponding word for the specified bit position.
@@ -134,7 +144,9 @@
                   uint8_t radix);
 
   /// This is used by the toString method to divide by the radix. It simply
-  /// provides a more convenient form of divide for internal use.
+  /// provides a more convenient form of divide for internal use since KnuthDiv
+  /// has specific constraints on its inputs. If those constraints are not met
+  /// then it provides a simpler form of divide.
   /// @brief An internal division function for dividing APInts.
   static void divide(const APInt LHS, uint32_t lhsWords, 
                      const APInt &RHS, uint32_t rhsWords,
@@ -481,15 +493,30 @@
   /// @returns true if the argument APInt value is a power of two > 0.
   bool isPowerOf2() const; 
 
+  /// countLeadingZeros - This function is an APInt version of the
+  /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number
+  /// of zeros from the most significant bit to the first one bit.
+  /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
   /// @returns the number of zeros from the most significant bit to the first
   /// one bits.
+  /// @brief Count the number of trailing one bits.
   uint32_t countLeadingZeros() const;
 
+  /// countTrailingZeros - This function is an APInt version of the 
+  /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts 
+  /// the number of zeros from the least significant bit to the first one bit.
+  /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero.
   /// @returns the number of zeros from the least significant bit to the first
   /// one bit.
+  /// @brief Count the number of trailing zero bits.
   uint32_t countTrailingZeros() const;
 
+  /// countPopulation - This function is an APInt version of the
+  /// countPopulation_{32,64} functions in MathExtras.h. It counts the number
+  /// of 1 bits in the APInt value. 
+  /// @returns 0 if the value is zero.
   /// @returns the number of set bits.
+  /// @brief Count the number of bits set.
   uint32_t countPopulation() const; 
 
   /// @returns the total number of bits.
@@ -550,9 +577,11 @@
   return APIVal.logBase2(); 
 }
 
-/// @returns the greatest common divisor of the two values 
-/// using Euclid's algorithm.
-APInt GreatestCommonDivisor(const APInt& API1, const APInt& API2);
+/// GreatestCommonDivisor - This function returns the greatest common
+/// divisor of the two APInt values using Enclid's algorithm.
+/// @returns the greatest common divisor of Val1 and Val2
+/// @brief Compute GCD of two APInt values.
+APInt GreatestCommonDivisor(const APInt& Val1, const APInt& Val2);
 
 /// @brief Converts the given APInt to a double value.
 inline double RoundAPIntToDouble(const APInt& APIVal, bool isSigned = false) {
@@ -564,10 +593,12 @@
   return float(RoundAPIntToDouble(APIVal));
 }
 
+/// RoundDoubleToAPInt - This function convert a double value to an APInt value.
 /// @brief Converts the given double value into a APInt.
 APInt RoundDoubleToAPInt(double Double);
 
-/// @brief Converts the given float value into a APInt.
+/// RoundFloatToAPInt - Converts a float value into an APInt value.
+/// @brief Converts a float value into a APInt.
 inline APInt RoundFloatToAPInt(float Float) {
   return RoundDoubleToAPInt(double(Float));
 }






More information about the llvm-commits mailing list