Changeset 59 for sources/src/sc_int.h


Ignore:
Timestamp:
Feb 6, 2017, 11:35:42 AM (7 years ago)
Author:
meunier
Message:
  • Fixed memory leaks
  • Fixed indentation in some files
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sources/src/sc_int.h

    r52 r59  
    1919#include <sc_bv.h>
    2020#include <cstdlib>
     21//#include <stdint.h>
    2122
    2223// ----------------------------------------------------------------------------
     
    3738
    3839
    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))
    4142
    4243template<int W> struct s_int_type { typedef int64 int_type; };
    4344
    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#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
    4546DECLAR_INT_TYPE(1);
    4647DECLAR_INT_TYPE(2);
     
    5455
    5556#define DECLAR_INT_TYPE(W) template<> struct s_int_type<W> { typedef int16 int_type; }
    56 DECLAR_INT_TYPE( 9);
     57DECLAR_INT_TYPE(9);
    5758DECLAR_INT_TYPE(10);
    5859DECLAR_INT_TYPE(11);
     
    9697    int64 val;
    9798    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_;
    99102    }
    100103   
    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    inline int64 read() const { return val; }
     105    inline const sc_int_subref_r & operator | (const sc_int_subref_r & v) const {
     106        print_warning();
    104107        return *this;
    105108    }
    106109
    107110    private :
    108     void print_warning () const;
     111    void print_warning() const;
    109112
    110113};
     
    116119class sc_int_subref_r;
    117120
    118 template< int W /* = SC_INTWIDTH */>
     121template < int W /* = SC_INTWIDTH */>
    119122class sc_int {
    120123
     
    123126    /***********************/
    124127    typedef sc_int<W> this_type;
     128    public:
    125129    typedef typename s_int_type<W>::int_type data_type;
    126 
     130    private:
    127131    typedef data_type sc_int_subref;
    128132
    129133    // internal
    130134    union {
    131         val_field<W, (sizeof (data_type) * 8) - W,data_type> vf; /* To compute */
     135        val_field<W, (sizeof(data_type) * 8) - W,data_type> vf; /* To compute */
    132136        data_type val;          /* To return an int reference (read function) */
    133137    };
     
    138142    /***********************/
    139143    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); }
     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); }
    151155
    152156    template <int W2> sc_int (const sc_int<W2> &val_) { val = 0; write (val_.read());}
     
    155159     */
    156160
    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); }
    159163    /* the user needs to cast explicitly result of range () method. */
    160164
     
    165169
    166170    // 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    }
    171177
    172178    /***********************/
     
    176182    // operators
    177183    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_);
     184        return read();
     185    }
     186
     187    template < typename T > inline sc_int & operator = (const T & val_) {
     188        write(val_);
    183189        return *this;
    184190    }
    185191
    186     inline sc_int & operator = (const sc_int_subref_r& a) {
    187         write (a);
     192    inline sc_int & operator = (const sc_int_subref_r & a) {
     193        write(a);
    188194        return *this;
    189195    }
     
    199205        return sc_dt::to_string (val, W, numrep);
    200206    }
    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); }
     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); }
    205211
    206212    // arithmetic
     
    231237    template <int W2>
    232238    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();
    234241        return res;
    235242    }
     
    241248
    242249    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 {
    244251        std::cerr << "Warning : \n";
    245252        return sc_int<W2> (v.read());
    246253    }
    247254
    248     inline sc_int_subref range (int left, int right) {
     255    inline sc_int_subref range(int left, int right) {
    249256        return (data_type)((data_type) (((data_type)~(0)) >> (sizeof (data_type) * 8 - left - 1)) & val) >> right;
    250257    }
    251258
    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
    255263#if 0
    256264    std::cerr << "range(" << left << "," << right << ")\n";
     
    263271};
    264272
    265 inline std::ostream & operator << (std::ostream &o, const sc_int_subref_r & s) {
     273inline std::ostream & operator << (std::ostream & o, const sc_int_subref_r & s) {
    266274    return o << s.val;
    267275}
Note: See TracChangeset for help on using the changeset viewer.