[llvm-commits] [llvm] r164826 - /llvm/trunk/include/llvm/ADT/PackedVector.h

Sean Silva silvas at purdue.edu
Fri Sep 28 11:50:46 PDT 2012


Is it just me or does this seem really inefficient?:

     for (unsigned i = 0; i != BitNum; ++i)
       Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);

Extracting an element should be able to be done with one indexing +
one (variable) shift, right? Maybe building this on top of the
bitvector isn't be best way to do it?

It almost seems like BitVector could (should?) be implemented in terms
of PackedVector, rather than the other way around as it is currently.

--Sean Silva

On Fri, Sep 28, 2012 at 12:40 PM, Benjamin Kramer
<benny.kra at googlemail.com> wrote:
> Author: d0k
> Date: Fri Sep 28 11:40:29 2012
> New Revision: 164826
>
> URL: http://llvm.org/viewvc/llvm-project?rev=164826&view=rev
> Log:
> PackedVector: Make the BitVector implementation configurable.
>
> Modified:
>     llvm/trunk/include/llvm/ADT/PackedVector.h
>
> Modified: llvm/trunk/include/llvm/ADT/PackedVector.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/PackedVector.h?rev=164826&r1=164825&r2=164826&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/PackedVector.h (original)
> +++ llvm/trunk/include/llvm/ADT/PackedVector.h Fri Sep 28 11:40:29 2012
> @@ -19,32 +19,32 @@
>
>  namespace llvm {
>
> -template <typename T, unsigned BitNum, bool isSigned>
> +template <typename T, unsigned BitNum, typename BitVectorTy, bool isSigned>
>  class PackedVectorBase;
>
>  // This won't be necessary if we can specialize members without specializing
>  // the parent template.
> -template <typename T, unsigned BitNum>
> -class PackedVectorBase<T, BitNum, false> {
> +template <typename T, unsigned BitNum, typename BitVectorTy>
> +class PackedVectorBase<T, BitNum, BitVectorTy, false> {
>  protected:
> -  static T getValue(const llvm::BitVector &Bits, unsigned Idx) {
> +  static T getValue(const BitVectorTy &Bits, unsigned Idx) {
>      T val = T();
>      for (unsigned i = 0; i != BitNum; ++i)
>        val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
>      return val;
>    }
>
> -  static void setValue(llvm::BitVector &Bits, unsigned Idx, T val) {
> +  static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
>      assert((val >> BitNum) == 0 && "value is too big");
>      for (unsigned i = 0; i != BitNum; ++i)
>        Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
>    }
>  };
>
> -template <typename T, unsigned BitNum>
> -class PackedVectorBase<T, BitNum, true> {
> +template <typename T, unsigned BitNum, typename BitVectorTy>
> +class PackedVectorBase<T, BitNum, BitVectorTy, true> {
>  protected:
> -  static T getValue(const llvm::BitVector &Bits, unsigned Idx) {
> +  static T getValue(const BitVectorTy &Bits, unsigned Idx) {
>      T val = T();
>      for (unsigned i = 0; i != BitNum-1; ++i)
>        val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
> @@ -53,7 +53,7 @@
>      return val;
>    }
>
> -  static void setValue(llvm::BitVector &Bits, unsigned Idx, T val) {
> +  static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
>      if (val < 0) {
>        val = ~val;
>        Bits.set((Idx << (BitNum-1)) + BitNum-1);
> @@ -71,11 +71,12 @@
>  /// @endcode
>  /// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
>  /// an assertion.
> -template <typename T, unsigned BitNum>
> -class PackedVector : public PackedVectorBase<T, BitNum,
> +template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
> +class PackedVector : public PackedVectorBase<T, BitNum, BitVectorTy,
>                                              std::numeric_limits<T>::is_signed> {
> -  llvm::BitVector Bits;
> -  typedef PackedVectorBase<T, BitNum, std::numeric_limits<T>::is_signed> base;
> +  BitVectorTy Bits;
> +  typedef PackedVectorBase<T, BitNum, BitVectorTy,
> +                           std::numeric_limits<T>::is_signed> base;
>
>  public:
>    class reference {
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits



More information about the llvm-commits mailing list