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