From 4984100559a00e055a502fc8fe85d7ca66589b36 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 7 Aug 2018 17:29:35 +0200 Subject: Clean up bit vector code --- chilbert/FixBitVec.hpp | 331 ++++++++++++++++--------------------------------- 1 file changed, 107 insertions(+), 224 deletions(-) (limited to 'chilbert/FixBitVec.hpp') 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< 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< 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<>=( - 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<>(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 <> -- cgit v1.2.1