diff options
author | David Robillard <d@drobilla.net> | 2018-08-07 17:29:35 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-09-29 14:46:14 +0200 |
commit | 4984100559a00e055a502fc8fe85d7ca66589b36 (patch) | |
tree | caf0a0a9e3e3b4e4bee1d1a43b9a9beb30d7610e /chilbert/FixBitVec.hpp | |
parent | 0bcaa46d0fb0af324fc2991ddcdf3d34c594f741 (diff) | |
download | chilbert-4984100559a00e055a502fc8fe85d7ca66589b36.tar.gz chilbert-4984100559a00e055a502fc8fe85d7ca66589b36.tar.bz2 chilbert-4984100559a00e055a502fc8fe85d7ca66589b36.zip |
Clean up bit vector code
Diffstat (limited to 'chilbert/FixBitVec.hpp')
-rw-r--r-- | chilbert/FixBitVec.hpp | 331 |
1 files changed, 107 insertions, 224 deletions
diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp index d8fee9a..bec5ba1 100644 --- a/chilbert/FixBitVec.hpp +++ b/chilbert/FixBitVec.hpp @@ -26,316 +26,199 @@ namespace chilbert { -// This must be an unsigned integer that is either -// 32 or 64 bits. Otherwise, there are places in the -// code that simply will not work. -// For speed, this should be the native word size. +/* This must be an unsigned integer that is either 32 or 64 bits. Otherwise, + there are places in the code that simply will not work. For speed, this + should be the native word size. */ typedef uint64_t FBV_UINT; -#define FBV_BITS 64 -#define FBV0 ((FBV_UINT)0) -#define FBV1 ((FBV_UINT)1) -#define FBV1S (~FBV0) -#define FBVN1S(n) (n==FBV_BITS?FBV1S:(FBV1<<n)-1) +#define FBV_BITS 64 + +#define FBV1 ((FBV_UINT)1) +#define FBV1S (~(FBV_UINT)0) +#define FBVN1S(n) (n == FBV_BITS ? FBV1S : (FBV1 << n) - 1) class CFixBitVec { public: + /// Return the size in bits + int size() const { return FBV_BITS; } - // Default constructor. The bits parameter - // is completely ignored, but accepted in order - // to look and feel the same as a BigBitVec. - CFixBitVec( - int iBits = FBV_BITS - ) - : m_uiRack{} - { - } - - // Copy constructor. - CFixBitVec( - const CFixBitVec &cFBV - ) - : m_uiRack{cFBV.m_uiRack} - { - } - - // Returns the current size in bits. - int - size() const + /// Set all bits to zero + CFixBitVec& reset() { - return FBV_BITS; + m_rack = 0; + return *this; } - // Zeros the bit-vector. - CFixBitVec & - reset() + /// Return the value of the `index`th bit + bool test(const int index) const { - m_uiRack = 0; - return (*this); + assert(0 <= index && index < FBV_BITS); + return ((m_rack & (FBV1 << index)) > 0); } - // Assignment operator. - CFixBitVec & - operator=( - const CFixBitVec &cFBV - ) + /// Set the `index`th bit to 1 + CFixBitVec& set(const int index) { - m_uiRack = cFBV.m_uiRack; - return (*this); + assert(0 <= index && index < FBV_BITS); + m_rack |= ((FBV_UINT)1 << index); + return *this; } - // Assignment operator. - CFixBitVec & - operator=( - FBV_UINT i - ) + /// Reset the `index`th bit to 0 + CFixBitVec& reset(const int index) { - m_uiRack = i; - return (*this); + assert(0 <= index && index < FBV_BITS); + m_rack &= ~((FBV_UINT)1 << index); + return *this; } - // Returns the value of the nth bit. - bool - test( - int iIndex - ) const + /// Set the `index`th bit to `value` + CFixBitVec& set(const int index, const bool value) { - assert( 0 <= iIndex && iIndex < FBV_BITS ); - return ( (m_uiRack & (FBV1<<iIndex)) > 0 ); + assert(0 <= index && index < FBV_BITS); + m_rack ^= (-value ^ m_rack) & ((FBV_UINT)1 << index); + return *this; } - // Sets the value of the nth bit. - CFixBitVec & - set( - int iIndex, - bool bBit = true - ) + /// Toggle the value of the `index`th bit + CFixBitVec& toggle(const int index) { - assert( 0 <= iIndex && iIndex < FBV_BITS ); - FBV_UINT m = (FBV1<<iIndex); - m_uiRack |= m; - if ( !bBit ) m_uiRack ^= m; - return (*this); + assert(0 <= index && index < FBV_BITS); + m_rack ^= (FBV1 << index); + return *this; } - // Toggles the value of the nth bit. - CFixBitVec & - toggle( - int iIndex - ) + CFixBitVec& operator=(const FBV_UINT i) { - assert( 0 <= iIndex && iIndex < FBV_BITS ); - m_uiRack ^= (FBV1<<iIndex); - return (*this); + m_rack = i; + return *this; } - // AND operation in place. - CFixBitVec & - operator&=( - const CFixBitVec &cFBV - ) + CFixBitVec& operator&=(const CFixBitVec& vec) { - m_uiRack &= cFBV.m_uiRack; - return (*this); + m_rack &= vec.m_rack; + return *this; } - // AND operation. - CFixBitVec - operator&( - const CFixBitVec &cFBV - ) const + CFixBitVec operator&(const CFixBitVec& vec) const { CFixBitVec t(*this); - t &= cFBV; + t &= vec; return t; } - // OR operation in place. - CFixBitVec & - operator|=( - const CFixBitVec &cFBV - ) + CFixBitVec& operator|=(const CFixBitVec& vec) { - m_uiRack |= cFBV.m_uiRack; - return (*this); + m_rack |= vec.m_rack; + return *this; } - // OR operation. - CFixBitVec - operator|( - const CFixBitVec &cFBV - ) const + CFixBitVec operator|(const CFixBitVec& vec) const { CFixBitVec t(*this); - t |= cFBV; + t |= vec; return t; } - - // XOR operation in place. - CFixBitVec & - operator^=( - const CFixBitVec &cFBV - ) + CFixBitVec& operator^=(const CFixBitVec& vec) { - m_uiRack ^= cFBV.m_uiRack; - return (*this); + m_rack ^= vec.m_rack; + return *this; } - CFixBitVec & - operator^=( - FBV_UINT i - ) + + CFixBitVec& operator^=(const FBV_UINT i) { - m_uiRack ^= i; - return (*this); + m_rack ^= i; + return *this; } - // XOR operation. - CFixBitVec - operator^( - const CFixBitVec &cFBV - ) const + CFixBitVec operator^(const CFixBitVec& vec) const { CFixBitVec t(*this); - t ^= cFBV; + t ^= vec; return t; } - // Shift left operation, in place. - CFixBitVec & - operator<<=( - int iBits - ) + CFixBitVec& operator<<=(const int bits) { - m_uiRack <<= iBits; - return (*this); + m_rack <<= bits; + return *this; } - // Shift left operation. - CFixBitVec - operator<<( - int iBits - ) const + CFixBitVec operator<<(const int bits) const { CFixBitVec t(*this); - t <<= iBits; + t <<= bits; return t; } - // Shift right operation, in place. - CFixBitVec & - operator>>=( - int iBits - ) + CFixBitVec& operator>>=(const int bits) { - m_uiRack >>= iBits; - return (*this); + m_rack >>= bits; + return *this; } - // Shift right operation. - CFixBitVec - operator>>( - int iBits - ) const + CFixBitVec operator>>(const int bits) const { CFixBitVec t(*this); - t >>= iBits; + t >>= bits; return t; } - // Right rotation, in place. - CFixBitVec & - rotr( - int iBits, - int iWidth - ) - { - assert( iBits >= 0 ); - assert( iWidth > 0 ); - assert( iBits < iWidth ); - m_uiRack &= FBVN1S(iWidth); - m_uiRack = (m_uiRack>>iBits) | (m_uiRack<<(iWidth-iBits)); - m_uiRack &= FBVN1S(iWidth); - return (*this); - } - - // Left rotation, in place. - CFixBitVec & - rotl( - int iBits, - int iWidth - ) + /// Right-rotate the least significant `width` bits by `bits` positions + CFixBitVec& rotr(const int bits, const int width) { - assert( iBits >= 0 ); - assert( iWidth > 0 ); - assert( iBits < iWidth ); - m_uiRack &= FBVN1S(iWidth); - m_uiRack = (m_uiRack<<iBits) | (m_uiRack>>(iWidth-iBits)); - m_uiRack &= FBVN1S(iWidth); - return (*this); + assert(bits >= 0); + assert(width > 0); + assert(bits < width); + m_rack &= FBVN1S(width); + m_rack = (m_rack >> bits) | (m_rack << (width - bits)); + m_rack &= FBVN1S(width); + return *this; } - // Is the bit rack zero valued? - bool - none() const + /// Left-rotate the least significant `width` bits by `bits` positions + CFixBitVec& rotl(int bits, int width) { - return m_uiRack == 0; + assert(bits >= 0); + assert(width > 0); + assert(bits < width); + m_rack &= FBVN1S(width); + m_rack = (m_rack << bits) | (m_rack >> (width - bits)); + m_rack &= FBVN1S(width); + return *this; } - // Returns the index of the first set bit, numbered from - // 1 to n. 0 means there were no set bits. - int - fsb() const - { - return chilbert::ffs(m_uiRack); - } + /// Return true iff all bits are zero + bool none() const { return m_rack == 0; } + /// Return 1 + the index of the first set bit, or 0 if there are none + int fsb() const { return chilbert::ffs(m_rack); } - // Ones-complements the rack - CFixBitVec & - flip() + /// Flip all bits (one's complement) + CFixBitVec& flip() { - m_uiRack = ~m_uiRack; - return (*this); + m_rack = ~m_rack; + return *this; } - // Returns the first rack. - FBV_UINT & - rack() - { - return m_uiRack; - } - FBV_UINT - rack() const - { - return m_uiRack; - } + /// Return the first rack + FBV_UINT& rack() { return m_rack; } + FBV_UINT rack() const { return m_rack; } - // Return a pointer to the racks - FBV_UINT * - racks() - { - return &m_uiRack; - } - const FBV_UINT * - racks() const - { - return &m_uiRack; - } + /// Return a pointer to the racks + FBV_UINT* racks() { return &m_rack; } + const FBV_UINT* racks() const { return &m_rack; } - // Returns the number of racks. - int - rackCount() - { - return 1; - } + /// Return the number of racks + int rackCount() { return 1; } private: - static_assert( 8*sizeof(FBV_UINT) == FBV_BITS, "" ); - static_assert( (sizeof(FBV_UINT) == 4) || (sizeof(FBV_UINT) == 8), "" ); + static_assert(8 * sizeof(FBV_UINT) == FBV_BITS, ""); + static_assert((sizeof(FBV_UINT) == 4) || (sizeof(FBV_UINT) == 8), ""); - FBV_UINT m_uiRack; + FBV_UINT m_rack{}; }; template <> |