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