Changeset 52 for sources/src/sc_int.h
- Timestamp:
- Jan 22, 2013, 4:23:22 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sources/src/sc_int.h
r27 r52 36 36 typedef sc_int<1> sc_int_bit_ref_r; 37 37 38 // 38 39 39 #define MASK32(W) ((~ (const uint32)0) >> (sizeof (uint32) * 8 - W)) 40 40 #define MASK64(W) ((~ (const uint64)0) >> (sizeof (uint64) * 8 - W)) 41 // 42 43 template<int W> struct s_int_type { typedef int64 int_type;}; 41 42 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 in8 because << operator threats like a character 45 DECLAR_INT_TYPE( 46 DECLAR_INT_TYPE( 47 DECLAR_INT_TYPE( 48 DECLAR_INT_TYPE( 49 DECLAR_INT_TYPE( 50 DECLAR_INT_TYPE( 51 DECLAR_INT_TYPE( 52 DECLAR_INT_TYPE( 45 DECLAR_INT_TYPE(1); 46 DECLAR_INT_TYPE(2); 47 DECLAR_INT_TYPE(3); 48 DECLAR_INT_TYPE(4); 49 DECLAR_INT_TYPE(5); 50 DECLAR_INT_TYPE(6); 51 DECLAR_INT_TYPE(7); 52 DECLAR_INT_TYPE(8); 53 53 #undef DECLAR_INT_TYPE 54 54 55 #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int16 int_type; } 55 56 DECLAR_INT_TYPE( 9); … … 62 63 DECLAR_INT_TYPE(16); 63 64 #undef DECLAR_INT_TYPE 65 64 66 #define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int32 int_type; } 65 67 DECLAR_INT_TYPE(17); … … 86 88 // Used for range, concat functions 87 89 88 class sc_int_subref_r 89 { 90 template <int W> friend class sc_dt::sc_int; 91 int left, right; 92 public : int64 val; 93 sc_int_subref_r (int64 val_, int left_, int right_) 94 { 95 val = val_; left = left_; right = right_; 96 } 97 public: 98 inline int64 read () const { return val; } 99 inline const sc_int_subref_r& operator | (const sc_int_subref_r &v) const 100 { print_warning (); return *this; } 101 102 private :void print_warning () const; 90 class sc_int_subref_r { 91 92 template <int W> friend class sc_dt::sc_int; 93 int left, right; 94 95 public : 96 int64 val; 97 sc_int_subref_r(int64 val_, int left_, int right_) { 98 val = val_; left = left_; right = right_; 99 } 100 101 inline int64 read () const { return val; } 102 inline const sc_int_subref_r& operator | (const sc_int_subref_r &v) const { 103 print_warning (); 104 return *this; 105 } 106 107 private : 108 void print_warning () const; 109 103 110 }; 104 111 … … 110 117 111 118 template< int W /* = SC_INTWIDTH */> 112 class sc_int 113 { 114 /***********************/ 115 /* SYSTEMCASS SPECIFIC */ 116 /***********************/ 117 typedef sc_int<W> this_type; 118 typedef typename s_int_type<W>::int_type data_type; 119 120 typedef data_type sc_int_subref; 121 //typedef data_type sc_int_subref_r; 122 /* removed since "operator ," was going wrong */ 123 124 // internal 125 union { 126 val_field<W,(sizeof (data_type) * 8) - W,data_type> vf; /* To compute */ 127 data_type val; /* To return an int reference (read function) */ 128 // data_type valW:W; /* works with little endianess only */ 129 // bool valB[W]; /* removed since 1 bool takes 1 byte */ 130 }; 131 132 /***********************/ 133 /* L R M */ 134 /***********************/ 135 public: 136 sc_int() { val = 0; } 137 // sc_int(data_type val_) { val = 0; write (val_); } 138 sc_int (const char *a) { val = 0; write (std::atoi (a)); } 139 sc_int (unsigned short a){ val = 0; write (a); } 140 sc_int (short a) { val = 0; write (a); } 141 sc_int (unsigned long a) { val = 0; write (a); } 142 sc_int (long a) { val = 0; write (a); } 143 sc_int (unsigned int a) { val = 0; write (a); } 144 sc_int (int a) { val = 0; write (a); } 145 sc_int (int64 a) { val = 0; write (a); } 146 sc_int (uint64 a) { val = 0; write (a); } 147 sc_int (double a) { val = 0; write (a); } 148 149 template <int W2> sc_int (const sc_int<W2> &val_) { val = 0; write (val_.read());} 150 /* this template doesn't redefine default copy constructor of sc_int. 151 * So, we define by this way 152 */ 153 154 sc_int (const sc_int &val_) { val = val_.val; } 155 sc_int (const sc_int_subref_r &a) { val = 0; write (a); } 119 class sc_int { 120 121 /***********************/ 122 /* SYSTEMCASS SPECIFIC */ 123 /***********************/ 124 typedef sc_int<W> this_type; 125 typedef typename s_int_type<W>::int_type data_type; 126 127 typedef data_type sc_int_subref; 128 129 // internal 130 union { 131 val_field<W, (sizeof (data_type) * 8) - W,data_type> vf; /* To compute */ 132 data_type val; /* To return an int reference (read function) */ 133 }; 134 135 136 /***********************/ 137 /* L R M */ 138 /***********************/ 139 public: 140 sc_int() { val = 0; } 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); } 151 152 template <int W2> sc_int (const sc_int<W2> &val_) { val = 0; write (val_.read());} 153 /* this template doesn't redefine default copy constructor of sc_int. 154 * So, we define by this way 155 */ 156 157 sc_int (const sc_int &val_) { val = val_.val; } 158 sc_int (const sc_int_subref_r & a) { val = 0; write (a); } 156 159 /* the user needs to cast explicitly result of range () method. */ 157 160 158 /***********************/ 159 /* SYSTEMCASS SPECIFIC */ 160 /***********************/ 161 162 /***********************/ 163 /* SYSTEMCASS SPECIFIC */ 164 /***********************/ 165 166 // 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 172 /***********************/ 173 /* L R M */ 174 /***********************/ 175 176 // operators 177 inline operator const data_type & () const { 178 return read (); 179 } 180 181 template <typename T> inline sc_int& operator = (const T& val_) { 182 write (val_); 183 return *this; 184 } 185 186 inline sc_int & operator = (const sc_int_subref_r& a) { 187 write (a); 188 return *this; 189 } 190 191 // explicit conversions 192 inline uint32 to_uint() const { return val & MASK32(W); } 193 inline int32 to_int() const { return val & MASK32(W); } 194 inline uint64 to_uint64() const { return val & MASK64(W); } 195 inline int64 to_int64() const { return val & MASK64(W); } 196 197 // explicit conversion to character string 198 const sc_string to_string(sc_numrep numrep = SC_DEC) const { 199 return sc_dt::to_string (val, W, numrep); 200 } 201 const sc_string to_dec() const { return to_string (SC_DEC); } 202 const sc_string to_bin() const { return to_string (SC_BIN); } 203 const sc_string to_oct() const { return to_string (SC_OCT); } 204 const sc_string to_hex() const { return to_string (SC_HEX); } 205 206 // arithmetic 207 #define DEFINE_OPERATOR(OPER) \ 208 template <typename T> \ 209 inline sc_int & operator OPER (T v) \ 210 { vf.valW OPER v; return *this; } 211 212 DEFINE_OPERATOR(<<=) 213 DEFINE_OPERATOR(>>=) 214 DEFINE_OPERATOR(+=) 215 DEFINE_OPERATOR(-=) 216 DEFINE_OPERATOR(*=) 217 DEFINE_OPERATOR(/=) 218 DEFINE_OPERATOR(%=) 219 DEFINE_OPERATOR(&=) 220 DEFINE_OPERATOR(|=) 221 DEFINE_OPERATOR(^=) 222 #undef DEFINE_OPERATOR 223 224 inline sc_int_bit_ref & operator [] (int v) { 225 return (vf.valW >> v) & 1; 226 } 227 inline sc_int_bit_ref_r & operator [] (int v) const { 228 return (vf.valW >> v) & 1; 229 } 230 231 template <int W2> 232 inline sc_int<W + W2> operator , (const sc_int<W2> & b) const { 233 sc_int<W + W2> res = read() << W2; res += b.read(); 234 return res; 235 } 236 237 inline sc_int<W + 1> operator , (bool b) const { 238 sc_int<W + 1> res = read(); res <<= 1; res += b; 239 return res; 240 } 241 242 template <int W2> 243 inline sc_int<W2> operator , (const sc_int_subref_r &v) const { 244 std::cerr << "Warning : \n"; 245 return sc_int<W2> (v.read()); 246 } 247 248 inline sc_int_subref range (int left, int right) { 249 return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right; 250 } 251 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 } 161 255 #if 0 162 bool is_negative () const { 163 return (valW >> (W-1)) == 1; 164 } 256 std::cerr << "range(" << left << "," << right << ")\n"; 257 std::cerr << "val = " << val << "\n"; 258 std::cerr << "~0 >> " << (sizeof (data_type) * 8 - left - 1) << " = " << (data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) << "\n"; 259 std::cerr << ((data_type) ((((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right) << "\n"; 260 std::cerr << "data_type = " << sizeof (data_type) << "\n"; 165 261 #endif 166 // read/write167 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<W> val_) { write (val_.read()); };171 inline void write (const sc_int_subref_r& s) { write (s.read()); }172 173 /***********************/174 /* L R M */175 /***********************/176 // operators177 inline operator const data_type& () const { return read (); }178 // inline void write(int64 val_) { val = val_ & MASK64(W); }179 // inline void write(signed int val_) { val = val_ & MASK32(W); }180 template <typename T> inline sc_int& operator = (const T& val_)181 { write (val_); return *this; }182 inline sc_int& operator = (const sc_int_subref_r& a)183 { write (a); return *this; }184 185 // explicit conversions186 inline uint32 to_uint () const {return val & MASK32(W);};187 inline int32 to_int () const {return val & MASK32(W);};188 inline uint64 to_uint64 () const {return val & MASK64(W);};189 inline int64 to_int64 () const {return val & MASK64(W);};190 191 // explicit conversion to character string192 const sc_string to_string ( sc_numrep numrep = SC_DEC ) const193 { return sc_dt::to_string (val, W, numrep); }194 const sc_string to_dec() const { return to_string (SC_DEC); }195 const sc_string to_bin() const { return to_string (SC_BIN); }196 const sc_string to_oct() const { return to_string (SC_OCT); }197 const sc_string to_hex() const { return to_string (SC_HEX); }198 199 // arithmetic200 #define DEFINE_OPERATOR(OPER) \201 template <typename T> \202 inline sc_int& operator OPER (T v) \203 { vf.valW OPER v; return *this; }204 205 DEFINE_OPERATOR(<<=)206 DEFINE_OPERATOR(>>=)207 DEFINE_OPERATOR(+=)208 DEFINE_OPERATOR(-=)209 DEFINE_OPERATOR(*=)210 DEFINE_OPERATOR(/=)211 DEFINE_OPERATOR(%=)212 DEFINE_OPERATOR(&=)213 DEFINE_OPERATOR(|=)214 DEFINE_OPERATOR(^=)215 #undef DEFINE_OPERATOR216 217 inline sc_int_bit_ref& operator [] (int v)218 { return (vf.valW >> v) & 1; }219 inline sc_int_bit_ref_r& operator [] (int v) const220 { return (vf.valW >> v) & 1; }221 222 template <int W2>223 inline sc_int<W+W2> operator , (const sc_int<W2> &b) const224 { sc_int<W+W2> res = read() << W2; res += b.read(); return res; }225 #if 0226 std::cerr << "[" << to_bin() << "," << b.to_bin() << " = " << res.to_bin() << "]\n";227 #endif228 229 inline sc_int<W+1> operator , (bool b) const230 { sc_int<W+1> res = read(); res <<= 1; res += b; return res; }231 #if 0232 std::cerr << "[" << to_bin() << "," << b.to_bin() << " = " << res.to_bin() << "]\n";233 #endif234 235 template <int W2>236 inline sc_int<W2> operator , (const sc_int_subref_r &v) const237 { std::cerr << "Warning : \n"; return sc_int<W2> (v.read()); }238 239 inline sc_int_subref range (int left, int right)240 { return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right; }241 242 inline sc_int_subref_r range (int left, int right) const243 { return sc_int_subref_r (((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right, left, right); }244 #if 0245 std::cerr << "range(" << left << "," << right << ")\n";246 std::cerr << "val = " << val << "\n";247 std::cerr << "~0 >> " << (sizeof (data_type) * 8 - left - 1) << " = " << (data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) << "\n";248 std::cerr << ((data_type) ((((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right) << "\n";249 std::cerr << "data_type = " << sizeof (data_type) << "\n";250 #endif251 262 252 263 }; 253 264 254 inline std::ostream& operator << (std::ostream &o, const sc_int_subref_r& s) 255 { return o << s.val; } 265 inline std::ostream & operator << (std::ostream &o, const sc_int_subref_r & s) { 266 return o << s.val; 267 } 268 269 #undef MASK32 270 #undef MASK64 256 271 257 272 } /* end of sc_dt namespace */ … … 259 274 #endif /* __SC_INT_H__ */ 260 275 276 /* 277 # Local Variables: 278 # tab-width: 4; 279 # c-basic-offset: 4; 280 # c-file-offsets:((innamespace . 0)(inline-open . 0)); 281 # indent-tabs-mode: nil; 282 # End: 283 # 284 # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 285 */ 286
Note: See TracChangeset
for help on using the changeset viewer.