mycpu/cdatn.h

00001 
00008 #ifndef CDATN_H
00009 #define CDATN_H 1
00010 
00011 #include <boost/operators.hpp>
00012 #include <iostream>
00013 
00019 class CDatN
00020   : public boost::operators<CDatN>
00021 {
00022   private:
00033     CDatN()
00034     {}
00035 
00036   public:
00047     virtual ~CDatN()
00048     {}
00049 
00060     CDatN(const CDatN& other)
00061       : m_value(other.m_value), m_width(other.m_width)
00062     {}
00063 
00075     CDatN(const int newval, unsigned width = 31)
00076       : m_value(((1 << width) - 1) & newval), m_width(width)
00077     {
00078       if (width < 2 || width > 32)
00079         throw std::runtime_error("width must be between 2 and 32");
00080     }
00081 
00092     int getValue() const
00093     {
00094       return m_value;
00095     }
00096 
00107     operator int()
00108     {
00109       return m_value;
00110     }
00111 
00122     bool operator<(const CDatN& x) const
00123     {
00124       return m_value < x.m_value;
00125     }
00126 
00137     bool operator==(const CDatN& x) const
00138     {
00139       return m_value == x.m_value;
00140     }
00141 
00152     CDatN &operator=(const int& newval)
00153     {
00154       m_value = ((1 << m_width) - 1) & newval;
00155       return *this;
00156     }
00157 
00168     CDatN& operator+=(const CDatN& x)
00169     {
00170       m_value = ((1 << m_width) - 1) & (m_value + x.m_value);
00171       return *this;
00172     }
00173 
00184     CDatN& operator-=(const CDatN& x)
00185     {
00186       m_value = ((1 << m_width) - 1) & (m_value - x.m_value);
00187       return *this;
00188     }
00189 
00200     CDatN& operator*=(const CDatN& x)
00201     {
00202       m_value = ((1 << m_width) - 1) & (m_value * x.m_value);
00203       return *this;
00204     }
00205 
00216     CDatN& operator/=(const CDatN& x)
00217     {
00218       m_value = ((1 << m_width) - 1) & (m_value / x.m_value);
00219       return *this;
00220     }
00221 
00232     CDatN& operator%=(const CDatN& x)
00233     {
00234       m_value = ((1 << m_width) - 1) & (m_value % x.m_value);
00235       return *this;
00236     }
00237 
00248     CDatN& operator|=(const CDatN& x)
00249     {
00250       m_value = ((1 << m_width) - 1) & (m_value | x.m_value);
00251       return *this;
00252     }
00253 
00264     CDatN& operator&=(const CDatN& x)
00265     {
00266       m_value = ((1 << m_width) - 1) & (m_value & x.m_value);
00267       return *this;
00268     }
00269 
00280     CDatN& operator^=(const CDatN& x)
00281     {
00282       m_value = ((1 << m_width) - 1) & (m_value ^ x.m_value);
00283       return *this;
00284     }
00285 
00296     CDatN& operator++()
00297     {
00298       m_value = ((1 << m_width) - 1) & (m_value + 1);
00299       return *this;
00300     }
00301 
00312     CDatN& operator--()
00313     {
00314       m_value--;
00315       return *this;
00316     }
00317 
00329     friend std::ostream& operator<<(std::ostream& stream, CDatN cdat)
00330     {
00331       stream << cdat.m_value;
00332       return stream;
00333     }
00334 
00346     friend std::istream& operator>>(std::istream & stream, CDatN& cdat)
00347     {
00348       stream >> cdat.m_value;
00349       cdat.m_value = ((1 << cdat.m_width) - 1) & cdat.m_value;
00350       return stream;
00351     }
00352 
00353   protected:
00354     /* members */
00356     int m_value;
00358     unsigned m_width;
00359 };
00360 
00361 #endif
00362 
00363 /* vim: set et sw=2 ts=2: */

Generated on Sat May 30 16:32:35 2009 for mycpu by  doxygen 1.5.3