source: sources/src/sc_bigint.h @ 52

Last change on this file since 52 was 52, checked in by meunier, 11 years ago

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