Changeset 59 for sources/src/sc_int.h
- Timestamp:
- Feb 6, 2017, 11:35:42 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sources/src/sc_int.h
r52 r59 19 19 #include <sc_bv.h> 20 20 #include <cstdlib> 21 //#include <stdint.h> 21 22 22 23 // ---------------------------------------------------------------------------- … … 37 38 38 39 39 #define MASK32(W) ((~ (const uint32) 0) >> (sizeof (uint32) * 8 - W))40 #define MASK64(W) ((~ (const uint64) 0) >> (sizeof (uint64) * 8 - W))40 #define MASK32(W) ((~ (const uint32) 0) >> (sizeof (uint32) * 8 - W)) 41 #define MASK64(W) ((~ (const uint64) 0) >> (sizeof (uint64) * 8 - W)) 41 42 42 43 template<int W> struct s_int_type { typedef int64 int_type; }; 43 44 44 #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef smallest_int int_type; } // not declared as in 8 because << operator threats like a character45 #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef smallest_int int_type; } // not declared as int8 because << operator threats like a character 45 46 DECLAR_INT_TYPE(1); 46 47 DECLAR_INT_TYPE(2); … … 54 55 55 56 #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int16 int_type; } 56 DECLAR_INT_TYPE( 57 DECLAR_INT_TYPE(9); 57 58 DECLAR_INT_TYPE(10); 58 59 DECLAR_INT_TYPE(11); … … 96 97 int64 val; 97 98 sc_int_subref_r(int64 val_, int left_, int right_) { 98 val = val_; left = left_; right = right_; 99 val = val_; 100 left = left_; 101 right = right_; 99 102 } 100 103 101 inline int64 read 102 inline const sc_int_subref_r & operator | (const sc_int_subref_r &v) const {103 print_warning 104 inline int64 read() const { return val; } 105 inline const sc_int_subref_r & operator | (const sc_int_subref_r & v) const { 106 print_warning(); 104 107 return *this; 105 108 } 106 109 107 110 private : 108 void print_warning 111 void print_warning() const; 109 112 110 113 }; … … 116 119 class sc_int_subref_r; 117 120 118 template < int W /* = SC_INTWIDTH */>121 template < int W /* = SC_INTWIDTH */> 119 122 class sc_int { 120 123 … … 123 126 /***********************/ 124 127 typedef sc_int<W> this_type; 128 public: 125 129 typedef typename s_int_type<W>::int_type data_type; 126 130 private: 127 131 typedef data_type sc_int_subref; 128 132 129 133 // internal 130 134 union { 131 val_field<W, (sizeof 135 val_field<W, (sizeof(data_type) * 8) - W,data_type> vf; /* To compute */ 132 136 data_type val; /* To return an int reference (read function) */ 133 137 }; … … 138 142 /***********************/ 139 143 public: 140 sc_int() 141 sc_int (const char * a) { val = 0; write (std::atoi(a)); }142 sc_int (unsigned short a) { val = 0; write(a); }143 sc_int (short a) { val = 0; write(a); }144 sc_int (unsigned long a) { val = 0; write(a); }145 sc_int (long a) { val = 0; write(a); }146 sc_int (unsigned int a) { val = 0; write(a); }147 sc_int (int a) { val = 0; write(a); }148 sc_int (int64 a) { val = 0; write(a); }149 sc_int (uint64 a) { val = 0; write(a); }150 sc_int (double a) { val = 0; write(a); }144 sc_int() { val = 0; } 145 sc_int(const char * a) { val = 0; write(std::atoi(a)); } 146 sc_int(unsigned short a) { val = 0; write(a); } 147 sc_int(short a) { val = 0; write(a); } 148 sc_int(unsigned long a) { val = 0; write(a); } 149 sc_int(long a) { val = 0; write(a); } 150 sc_int(unsigned int a) { val = 0; write(a); } 151 sc_int(int a) { val = 0; write(a); } 152 sc_int(int64 a) { val = 0; write(a); } 153 sc_int(uint64 a) { val = 0; write(a); } 154 sc_int(double a) { val = 0; write(a); } 151 155 152 156 template <int W2> sc_int (const sc_int<W2> &val_) { val = 0; write (val_.read());} … … 155 159 */ 156 160 157 sc_int (const sc_int &val_) { val = val_.val; }158 sc_int (const sc_int_subref_r & a) { val = 0; write(a); }161 sc_int(const sc_int & val_) { val = val_.val; } 162 sc_int(const sc_int_subref_r & a) { val = 0; write(a); } 159 163 /* the user needs to cast explicitly result of range () method. */ 160 164 … … 165 169 166 170 // read/write 167 inline const data_type& read() const { return val; } 168 inline void write(data_type val_) { vf.valW = val_; } 169 template <int W2> inline void write (const sc_int<W2> val_) { write (val_.read ()); } 170 inline void write (const sc_int_subref_r& s) { write (s.read()); } 171 inline const data_type & read() const { return val; } 172 inline void write(data_type val_) { vf.valW = val_; } 173 template <int W2> void write(const sc_int<W2> val_) { write (val_.read ()); } 174 inline void write(const sc_int_subref_r & s) { 175 write (s.read()); 176 } 171 177 172 178 /***********************/ … … 176 182 // operators 177 183 inline operator const data_type & () const { 178 return read 179 } 180 181 template < typename T> inline sc_int& operator = (const T& val_) {182 write 184 return read(); 185 } 186 187 template < typename T > inline sc_int & operator = (const T & val_) { 188 write(val_); 183 189 return *this; 184 190 } 185 191 186 inline sc_int & operator = (const sc_int_subref_r & a) {187 write 192 inline sc_int & operator = (const sc_int_subref_r & a) { 193 write(a); 188 194 return *this; 189 195 } … … 199 205 return sc_dt::to_string (val, W, numrep); 200 206 } 201 const sc_string to_dec() const { return to_string 202 const sc_string to_bin() const { return to_string 203 const sc_string to_oct() const { return to_string 204 const sc_string to_hex() const { return to_string 207 const sc_string to_dec() const { return to_string(SC_DEC); } 208 const sc_string to_bin() const { return to_string(SC_BIN); } 209 const sc_string to_oct() const { return to_string(SC_OCT); } 210 const sc_string to_hex() const { return to_string(SC_HEX); } 205 211 206 212 // arithmetic … … 231 237 template <int W2> 232 238 inline sc_int<W + W2> operator , (const sc_int<W2> & b) const { 233 sc_int<W + W2> res = read() << W2; res += b.read(); 239 sc_int<W + W2> res = read() << W2; 240 res += b.read(); 234 241 return res; 235 242 } … … 241 248 242 249 template <int W2> 243 inline sc_int<W2> operator , (const sc_int_subref_r & v) const {250 inline sc_int<W2> operator , (const sc_int_subref_r & v) const { 244 251 std::cerr << "Warning : \n"; 245 252 return sc_int<W2> (v.read()); 246 253 } 247 254 248 inline sc_int_subref range 255 inline sc_int_subref range(int left, int right) { 249 256 return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right; 250 257 } 251 258 252 inline sc_int_subref_r range (int left, int right) const { 253 return sc_int_subref_r (((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right, left, right); 254 } 259 inline sc_int_subref_r range(int left, int right) const { 260 return sc_int_subref_r(((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right, left, right); 261 } 262 255 263 #if 0 256 264 std::cerr << "range(" << left << "," << right << ")\n"; … … 263 271 }; 264 272 265 inline std::ostream & operator << (std::ostream & o, const sc_int_subref_r & s) {273 inline std::ostream & operator << (std::ostream & o, const sc_int_subref_r & s) { 266 274 return o << s.val; 267 275 }
Note: See TracChangeset
for help on using the changeset viewer.