diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2007-03-24 23:05:35 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2007-03-24 23:05:35 +0000 |
commit | 30e83dfc4b9dee311751f7740a7a5ed7e5467818 (patch) | |
tree | 6433b62afc155540bfb298897008efe9a52ad080 | |
parent | 41eadeb49d044a54a34af193353a682f1e1a2926 (diff) | |
download | bcm5719-llvm-30e83dfc4b9dee311751f7740a7a5ed7e5467818.tar.gz bcm5719-llvm-30e83dfc4b9dee311751f7740a7a5ed7e5467818.zip |
Implement the getHighBitsSet and getLowBitsSet functions.
llvm-svn: 35308
-rw-r--r-- | llvm/include/llvm/ADT/APInt.h | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 90564ef3f8b..31fac9608f8 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -346,13 +346,27 @@ public: /// @param numBits the bitwidth of the result /// @param hiBitsSet the number of high-order bits set in the result. /// @brief Get a value with high bits set - static APInt getHighBitsSet(uint32_t numBits, uint32_t hiBitsSet); + static APInt getHighBitsSet(uint32_t numBits, uint32_t hiBitsSet) { + assert(hiBitsSet <= numBits && "Too many bits to set!"); + uint32_t mvBits = numBits - hiBitsSet; + // For small values, return quickly + if (numBits <= APINT_BITS_PER_WORD) + return APInt(numBits, ((1ULL << hiBitsSet) - 1) << mvBits); + APInt Result(numBits, 1); + return (APInt(numBits, 1).shl(hiBitsSet) - APInt(numBits, 1)).shl(mvBits); + } /// Constructs an APInt value that has the bottom loBitsSet bits set. /// @param numBits the bitwidth of the result /// @param loBitsSet the number of low-order bits set in the result. /// @brief Get a value with low bits set - static APInt getLowBitsSet(uint32_t numBits, uint32_t loBitsSet); + static APInt getLowBitsSet(uint32_t numBits, uint32_t loBitsSet) { + assert(loBitsSet <= numBits && "Too many bits to set!"); + // For small values, return quickly + if (numBits <= APINT_BITS_PER_WORD) + return APInt(numBits, (1ULL << loBitsSet) - 1ULL); + return APInt(numBits, 1).shl(loBitsSet) - APInt(numBits, 1); + } /// The hash value is computed as the sum of the words and the bit width. /// @returns A hash value computed from the sum of the APInt words. |