Changeset 52 for sources/src/sc_biguint.h
- Timestamp:
- Jan 22, 2013, 4:23:22 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sources/src/sc_biguint.h
r27 r52 22 22 namespace sc_dt { 23 23 24 //25 24 #define MASK32(W) ((~ (const uint32)0) >> (sizeof (uint32) * 8 - W)) 26 25 #define MASK64(W) ((~ (const uint64)0) >> (sizeof (uint64) * 8 - W)) 27 //28 26 29 27 template<int W> struct s_biguint_type { typedef uint64 uint_type;}; 28 30 29 #define DECLAR_BIGUINT_TYPE(W) template<> struct s_biguint_type<W> { typedef uint16 uint_type; }// not declared as uint16 because << operator threats like a character 31 DECLAR_BIGUINT_TYPE( 32 DECLAR_BIGUINT_TYPE( 33 DECLAR_BIGUINT_TYPE( 34 DECLAR_BIGUINT_TYPE( 35 DECLAR_BIGUINT_TYPE( 36 DECLAR_BIGUINT_TYPE( 37 DECLAR_BIGUINT_TYPE( 38 DECLAR_BIGUINT_TYPE( 30 DECLAR_BIGUINT_TYPE(1); 31 DECLAR_BIGUINT_TYPE(2); 32 DECLAR_BIGUINT_TYPE(3); 33 DECLAR_BIGUINT_TYPE(4); 34 DECLAR_BIGUINT_TYPE(5); 35 DECLAR_BIGUINT_TYPE(6); 36 DECLAR_BIGUINT_TYPE(7); 37 DECLAR_BIGUINT_TYPE(8); 39 38 #undef DECLAR_BIGUINT_TYPE 39 40 40 #define DECLAR_BIGUINT_TYPE(W) template<> struct s_biguint_type<W> { typedef uint16 uint_type; } 41 DECLAR_BIGUINT_TYPE( 41 DECLAR_BIGUINT_TYPE(9); 42 42 DECLAR_BIGUINT_TYPE(10); 43 43 DECLAR_BIGUINT_TYPE(11); … … 48 48 DECLAR_BIGUINT_TYPE(16); 49 49 #undef DECLAR_BIGUINT_TYPE 50 50 51 #define DECLAR_BIGUINT_TYPE(W) template<> struct s_biguint_type<W> { typedef uint32 uint_type; } 51 52 DECLAR_BIGUINT_TYPE(17); … … 67 68 #undef DECLAR_BIGUINT_TYPE 68 69 70 69 71 template< int W /* = SC_INTWIDTH */> 70 class sc_biguint 71 { 72 typedef sc_biguint<W> this_type; 73 typedef typename s_int_type<W>::int_type data_type; 74 75 typedef data_type sc_uint_subref; 76 typedef data_type sc_uint_subref_r; 77 78 union { 79 data_type valW:W; 80 bool valB[W]; 81 data_type val; 82 }; 83 void check () 84 { if (W > 64) { std::cerr << "sc_biguint with W > 64 is not supported.\n"; exit (20040528); } } 85 public: 86 sc_biguint() { check (); val = 0; } 87 sc_biguint(data_type val_) { check (); val = 0; write (val_); } 88 template <int W2> explicit sc_biguint (const sc_biguint<W2> &val_) { write (val_.read());} 89 90 inline const data_type& read() const { return val; } 91 inline operator const data_type& () const { return read (); } 92 // inline void write(int64 val_) { val = val_ & MASK64(W); } 93 // inline void write(signed int val_) { val = val_ & MASK32(W); } 94 inline void write(data_type val_) { valW = val_; } 95 template <int W2> inline void write (const sc_biguint<W2> val_) { write (val_.read ()); } 96 inline void write (const sc_biguint<W> val_) { write (val_.read()); }; 97 template <typename T> inline sc_biguint& operator = (const T& val_) { write (val_); return *this; } 98 99 inline uint32 to_uint () const {return val & MASK32(W);}; 100 inline int32 to_int () const {return val & MASK32(W);}; 101 inline uint64 to_uint64 () const {return val & MASK64(W);}; 102 inline int64 to_int64 () const {return val & MASK64(W);}; 103 104 template <typename T> 105 inline sc_biguint& operator <<= (T v) 106 { valW <<= v; return *this; } 107 template <typename T> 108 inline sc_biguint& operator >>= (T v) 109 { valW >>= v; return *this; } 110 template <typename T> 111 inline sc_biguint& operator += (T v) 112 { valW += v; return *this; } 113 template <typename T> 114 inline sc_biguint& operator -= (T v) 115 { valW -= v; return *this; } 116 template <typename T> 117 inline sc_biguint& operator *= (T v) 118 { valW *= v; return *this; } 119 template <typename T> 120 inline sc_biguint& operator /= (T v) 121 { valW /= v; return *this; } 122 template <typename T> 123 inline sc_biguint& operator %= (T v) 124 { valW %= v; return *this; } 125 template <typename T> 126 inline sc_biguint& operator &= (T v) 127 { valW &= v; return *this; } 128 template <typename T> 129 inline sc_biguint& operator |= (T v) 130 { valW |= v; return *this; } 131 template <typename T> 132 inline sc_biguint& operator ^= (T v) 133 { valW ^= v; return *this; } 134 inline sc_uint_bit_ref& operator [] (int v) 135 { return valB[v]; } 136 inline sc_uint_bit_ref_r& operator [] (int v) const 137 { return valB[v]; } 138 inline sc_uint_subref range (int left, int right) 139 { return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right; } 140 inline sc_uint_subref_r range (int left, int right) const 141 { return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right; } 72 class sc_biguint { 73 typedef sc_biguint<W> this_type; 74 typedef typename s_int_type<W>::int_type data_type; 75 76 typedef data_type sc_uint_subref; 77 typedef data_type sc_uint_subref_r; 78 79 union { 80 data_type valW:W; 81 bool valB[W]; 82 data_type val; 83 }; 84 85 void check () { 86 if (W > 64) { 87 std::cerr << "sc_biguint with W > 64 is not supported.\n"; 88 exit (20040528); 89 } 90 } 91 92 public: 93 94 sc_biguint() { 95 check (); 96 val = 0; 97 } 98 99 sc_biguint(data_type val_) { 100 check (); 101 val = 0; 102 write (val_); 103 } 104 105 template <int W2> explicit sc_biguint (const sc_biguint<W2> & val_) { 106 write (val_.read()); 107 } 108 109 inline const data_type & read() const { 110 return val; 111 } 112 113 inline operator const data_type & () const { 114 return read (); 115 } 116 117 inline void write(data_type val_) { 118 valW = val_; 119 } 120 121 template <int W2> inline void write (const sc_biguint<W2> val_) { 122 write (val_.read ()); 123 } 124 125 inline void write (const sc_biguint<W> val_) { 126 write (val_.read()); 127 } 128 129 template <typename T> inline sc_biguint & operator = (const T& val_) { 130 write (val_); 131 return *this; 132 } 133 134 inline uint32 to_uint() const { return val & MASK32(W); } 135 inline int32 to_int() const { return val & MASK32(W); } 136 inline uint64 to_uint64() const { return val & MASK64(W); } 137 inline int64 to_int64() const { return val & MASK64(W); } 138 139 template <typename T> 140 inline sc_biguint & operator <<= (T v) { 141 valW <<= v; 142 return *this; 143 } 144 145 template <typename T> 146 inline sc_biguint & operator >>= (T v) { 147 valW >>= v; 148 return *this; 149 } 150 151 template <typename T> 152 inline sc_biguint & operator += (T v) { 153 valW += v; 154 return *this; 155 } 156 157 template <typename T> 158 inline sc_biguint & operator -= (T v) { 159 valW -= v; 160 return *this; 161 } 162 163 template <typename T> 164 inline sc_biguint & operator *= (T v) { 165 valW *= v; 166 return *this; 167 } 168 169 template <typename T> 170 inline sc_biguint & operator /= (T v) { 171 valW /= v; 172 return *this; 173 } 174 175 template <typename T> 176 inline sc_biguint & operator %= (T v) { 177 valW %= v; 178 return *this; 179 } 180 181 template <typename T> 182 inline sc_biguint & operator &= (T v) { 183 valW &= v; 184 return *this; 185 } 186 187 template <typename T> 188 inline sc_biguint & operator |= (T v) { 189 valW |= v; 190 return *this; 191 } 192 193 template <typename T> 194 inline sc_biguint & operator ^= (T v) { 195 valW ^= v; 196 return *this; 197 } 198 199 inline sc_uint_bit_ref & operator [] (int v) { return valB[v]; } 200 inline sc_uint_bit_ref_r & operator [] (int v) const { return valB[v]; } 201 202 inline sc_uint_subref range (int left, int right) { 203 return (((~0) >> (sizeof(data_type) * 8 - left)) & val) >> right; 204 } 205 206 inline sc_uint_subref_r range (int left, int right) const { 207 return (((~0) >> (sizeof(data_type) * 8 - left)) & val) >> right; 208 } 209 142 210 }; 143 211 … … 145 213 146 214 #endif /* __SC_BIGUINT_H__ */ 215 216 /* 217 # Local Variables: 218 # tab-width: 4; 219 # c-basic-offset: 4; 220 # c-file-offsets:((innamespace . 0)(inline-open . 0)); 221 # indent-tabs-mode: nil; 222 # End: 223 # 224 # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 225 */ 226
Note: See TracChangeset
for help on using the changeset viewer.