BitOverlay.h

00001 # ifndef BitOverlay_h
00002 # define BitOverlay_h BitInfo_h
00003 
00004 // # Created 2006/06/09 by wade
00005 
00006 # include "Root.h"  // parent
00007 # include <string>
00008 
00009 namespace Conjecture {
00010     
00011     /********************************************************************
00012      * \class BitOverlay
00013      *
00014      * \brief Hyper-low-level bit analysis
00015      *
00016      * This class provides a variety of routines for analyzing the
00017      * bits within a 32-bit value.
00018      *
00019      * There are both static and instance versions of each method.
00020      *
00021      * An "on" bit is 1, an "off" bit is 0.
00022      *
00023      * IMPORTANT: This class can be used as simply a collection of
00024      * static methods, OR it can be used as an "overlay", a means of
00025      * adding semantics and functionality to an arbitrary integer.
00026      * However, in order for it to be an overlay, NO VIRTUAL METHODS!
00027      * We do not want a vtbl here!  Includes parents, of course.
00028      * Because of this, statically typing a variable as BitOverlay
00029      * and assigning it an instance of the subclass BitInfo is NOT
00030      * recommended.
00031      *
00032      * FUTURE WORK: Analyze 
00033      *     http://graphics.stanford.edu/~seander/bithacks.html
00034      * and incorporate useful methods from there into this class!
00035      *
00036      * See Also: BitOverlay64
00037      *
00038      *******************************************************************/
00039     class BitOverlay : public Root {
00040       public:
00049         typedef unsigned int Layout;
00050 
00068         typedef int BitPos;
00069         static const BitPos npos = 255;
00070 
00075         typedef unsigned BitCount;
00076 
00077         inline BitOverlay(Layout data) { this->bitsIs(data); }
00078         
00079         // Accessors
00080         inline Layout bits() const { return this->_bits; }
00081         
00082         // Interface
00083 
00092         static BitPos FindFirstHigh(Layout val);
00093         inline BitPos findFirstHigh() const { return FindFirstHigh(this->_bits); }
00094         static BitPos FindFirstHighSlow(Layout val);
00095 
00100         static BitPos FindFirstLow(Layout val);
00101         inline BitPos findFirstLow() const { return FindFirstLow(this->_bits); }
00102 
00107         static inline int _bx(Layout x)             { return (x - ((x>>1)&0x77777777) - ((x>>2)&0x33333333) - ((x>>3)&0x11111111)); }
00108         static inline BitCount PopCount(Layout val) { return (((_bx(val)+(_bx(val)>>4)) & 0x0F0F0F0F) % 255); }
00109         inline BitCount popc() const                { return PopCount(this->_bits); }
00110         static BitCount PopCountSlow(Layout val);
00111 
00120         static std::string Str(Layout val);
00121         inline std::string str() const { return Str(this->_bits); }
00122 
00131         static BitCount Regions(Layout val, int* largest = NULL, int* all = NULL);
00132         inline BitCount regions(int* largest = NULL, int* all = NULL) const { return Regions(this->_bits, largest, all); }
00133 
00138         static void Test(int argc = 0, const char* argv[] = NULL);
00139         
00140       protected:
00141         // Accessors
00142         inline void bitsIs(const Layout & bits) { this->_bits = bits; }
00143         
00144         // Methods 
00145         
00146       private: 
00147         // Accessors
00148         inline Layout &  bitsRef() { return this->_bits; }
00149         
00150         // Methods 
00151         
00152         // State
00153         Layout    _bits;
00154     };
00155 }
00156 
00157 # endif // BitOverlay_h
00158 

Generated on Wed Jun 14 15:08:02 2006 for Conjecture by  doxygen 1.4.6