source: sources/src/sc_bigint.h @ 65

Last change on this file since 65 was 59, checked in by meunier, 7 years ago
  • Fixed memory leaks
  • Fixed indentation in some files
File size: 6.0 KB
Line 
1/*------------------------------------------------------------\
2  |                                                             |
3  | Tool    :                  systemcass                       |
4  |                                                             |
5  | File    :                 sc_bigint.h                      |
6  |                                                             |
7  | Author  :                 Buchmann Richard                  |
8  |                                                             |
9  | Date    :                   09_07_2004                      |
10  |                                                             |
11  \------------------------------------------------------------*/
12#ifndef __SC_BIGINT_H__
13#define __SC_BIGINT_H__
14
15#include <sc_bit.h>
16#include <sc_logic.h>
17#include <sc_bv.h>
18//#include <stdint.h>
19
20// ----------------------------------------------------------------------------
21//  CLASS : sc_bigint<W>
22//
23// ----------------------------------------------------------------------------
24
25#include "sc_nbdefs.h"
26
27namespace sc_dt {
28
29#define MASK32(W) ((~ (const uint32)0) >> (sizeof (uint32) * 8 - W))
30#define MASK64(W) ((~ (const uint64)0) >> (sizeof (uint64) * 8 - W))
31
32template<int W> struct s_bigint_type { typedef uint64 uint_type;};
33#define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint16 uint_type; }// not declared as uint16 because << operator threats like a character
34DECLAR_BIGINT_TYPE(1);
35DECLAR_BIGINT_TYPE(2);
36DECLAR_BIGINT_TYPE(3);
37DECLAR_BIGINT_TYPE(4);
38DECLAR_BIGINT_TYPE(5);
39DECLAR_BIGINT_TYPE(6);
40DECLAR_BIGINT_TYPE(7);
41DECLAR_BIGINT_TYPE(8);
42#undef DECLAR_BIGINT_TYPE
43
44#define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint16 uint_type; }
45DECLAR_BIGINT_TYPE(9);
46DECLAR_BIGINT_TYPE(10);
47DECLAR_BIGINT_TYPE(11);
48DECLAR_BIGINT_TYPE(12);
49DECLAR_BIGINT_TYPE(13);
50DECLAR_BIGINT_TYPE(14);
51DECLAR_BIGINT_TYPE(15);
52DECLAR_BIGINT_TYPE(16);
53#undef DECLAR_BIGINT_TYPE
54
55#define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint32 uint_type; }
56DECLAR_BIGINT_TYPE(17);
57DECLAR_BIGINT_TYPE(18);
58DECLAR_BIGINT_TYPE(19);
59DECLAR_BIGINT_TYPE(20);
60DECLAR_BIGINT_TYPE(21);
61DECLAR_BIGINT_TYPE(22);
62DECLAR_BIGINT_TYPE(23);
63DECLAR_BIGINT_TYPE(24);
64DECLAR_BIGINT_TYPE(25);
65DECLAR_BIGINT_TYPE(26);
66DECLAR_BIGINT_TYPE(27);
67DECLAR_BIGINT_TYPE(28);
68DECLAR_BIGINT_TYPE(29);
69DECLAR_BIGINT_TYPE(30);
70DECLAR_BIGINT_TYPE(31);
71DECLAR_BIGINT_TYPE(32);
72#undef DECLAR_BIGINT_TYPE
73
74template< int W /* = SC_INTWIDTH */>
75class sc_bigint {
76    typedef sc_bigint<W> this_type;
77    typedef typename s_int_type<W>::int_type data_type;
78
79    typedef data_type sc_uint_subref;
80    typedef data_type sc_uint_subref_r;
81
82    union {
83        data_type valW:W;
84        bool valB[W];
85        data_type val;
86    };
87   
88    void check() {
89        if (W > 64) {
90            std::cerr << "sc_bigint with W > 64 is not supported.\n";
91            exit (20040528);
92        }
93    }
94
95    public:
96    sc_bigint() {
97        check ();
98        val = 0;
99    }
100
101    sc_bigint(data_type val_) {
102        check ();
103        val = 0;
104        write (val_);
105    }
106
107    template <int W2> explicit sc_bigint(const sc_bigint<W2> & val_) {
108        write (val_.read());
109    }
110
111    inline const data_type & read() const {
112        return val;
113    }
114
115    inline operator const data_type & () const {
116        return read ();
117    }
118
119    //  inline void write(int64 val_) { val = val_ & MASK64(W); }
120    //  inline void write(signed int val_)   { val = val_ & MASK32(W); }
121   
122    inline void write(data_type val_) {
123        valW = val_;
124    }
125   
126    template <int W2> inline void write (const sc_bigint<W2> val_) {
127        write (val_.read ());
128    }
129
130    inline void write (const sc_bigint<W> val_) {
131        write (val_.read());
132    }
133
134    template <typename T> inline sc_bigint & operator = (const T & val_) {
135        write (val_);
136        return *this;
137    }
138
139    inline uint32 to_uint() const {
140        return val & MASK32(W);
141    }
142
143    inline int32 to_int() const {
144        return val & MASK32(W);
145    }
146
147    inline uint64 to_uint64() const {
148        return val & MASK64(W);
149    }
150
151    inline int64 to_int64() const {
152        return val & MASK64(W);
153    }
154
155    template <typename T>
156    inline sc_bigint & operator <<= (T v) {
157        valW <<= v;
158        return *this;
159    }
160
161    template <typename T>
162    inline sc_bigint & operator >>= (T v) {
163        valW >>= v;
164        return *this;
165    }
166
167    template <typename T>
168    inline sc_bigint & operator += (T v) {
169        valW += v;
170        return *this;
171    }
172
173    template <typename T>
174    inline sc_bigint & operator -= (T v) {
175        valW -= v;
176        return *this;
177    }
178
179    template <typename T>
180    inline sc_bigint & operator *= (T v) {
181        valW *= v;
182        return *this;
183    }
184
185    template <typename T>
186    inline sc_bigint & operator /= (T v) {
187        valW /= v;
188        return *this;
189    }
190
191    template <typename T>
192    inline sc_bigint & operator %= (T v) {
193        valW %= v;
194        return *this;
195    }
196
197    template <typename T>
198    inline sc_bigint & operator &= (T v) {
199        valW &= v;
200        return *this;
201    }
202
203    template <typename T>
204    inline sc_bigint & operator |= (T v) {
205        valW |= v;
206        return *this;
207    }
208
209    template <typename T>
210    inline sc_bigint & operator ^= (T v) {
211        valW ^= v;
212        return *this;
213    }
214
215    inline sc_uint_bit_ref & operator [] (int v) {
216        return valB[v];
217    }
218
219    inline sc_uint_bit_ref_r& operator [] (int v) const {
220        return valB[v];
221    }
222
223    inline sc_uint_subref range(int left, int right) {
224        return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right;
225    }
226
227    inline sc_uint_subref_r range(int left, int right) const {
228        return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right;
229    }
230
231};
232
233} /* end of sc_dt namespace */
234
235#endif /* __SC_BIGINT_H__ */
236
237
238/*
239# Local Variables:
240# tab-width: 4;
241# c-basic-offset: 4;
242# c-file-offsets:((innamespace . 0)(inline-open . 0));
243# indent-tabs-mode: nil;
244# End:
245#
246# vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
247*/
248
Note: See TracBrowser for help on using the repository browser.