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 | |
---|
26 | namespace sc_dt { |
---|
27 | |
---|
28 | // |
---|
29 | #define MASK32(W) ((~ (const uint32)0) >> (sizeof (uint32) * 8 - W)) |
---|
30 | #define MASK64(W) ((~ (const uint64)0) >> (sizeof (uint64) * 8 - W)) |
---|
31 | // |
---|
32 | |
---|
33 | template<int W> struct s_bigint_type { typedef uint64 uint_type;}; |
---|
34 | #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 |
---|
35 | DECLAR_BIGINT_TYPE( 1); |
---|
36 | DECLAR_BIGINT_TYPE( 2); |
---|
37 | DECLAR_BIGINT_TYPE( 3); |
---|
38 | DECLAR_BIGINT_TYPE( 4); |
---|
39 | DECLAR_BIGINT_TYPE( 5); |
---|
40 | DECLAR_BIGINT_TYPE( 6); |
---|
41 | DECLAR_BIGINT_TYPE( 7); |
---|
42 | DECLAR_BIGINT_TYPE( 8); |
---|
43 | #undef DECLAR_BIGINT_TYPE |
---|
44 | #define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint16 uint_type; } |
---|
45 | DECLAR_BIGINT_TYPE( 9); |
---|
46 | DECLAR_BIGINT_TYPE(10); |
---|
47 | DECLAR_BIGINT_TYPE(11); |
---|
48 | DECLAR_BIGINT_TYPE(12); |
---|
49 | DECLAR_BIGINT_TYPE(13); |
---|
50 | DECLAR_BIGINT_TYPE(14); |
---|
51 | DECLAR_BIGINT_TYPE(15); |
---|
52 | DECLAR_BIGINT_TYPE(16); |
---|
53 | #undef DECLAR_BIGINT_TYPE |
---|
54 | #define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint32 uint_type; } |
---|
55 | DECLAR_BIGINT_TYPE(17); |
---|
56 | DECLAR_BIGINT_TYPE(18); |
---|
57 | DECLAR_BIGINT_TYPE(19); |
---|
58 | DECLAR_BIGINT_TYPE(20); |
---|
59 | DECLAR_BIGINT_TYPE(21); |
---|
60 | DECLAR_BIGINT_TYPE(22); |
---|
61 | DECLAR_BIGINT_TYPE(23); |
---|
62 | DECLAR_BIGINT_TYPE(24); |
---|
63 | DECLAR_BIGINT_TYPE(25); |
---|
64 | DECLAR_BIGINT_TYPE(26); |
---|
65 | DECLAR_BIGINT_TYPE(27); |
---|
66 | DECLAR_BIGINT_TYPE(28); |
---|
67 | DECLAR_BIGINT_TYPE(29); |
---|
68 | DECLAR_BIGINT_TYPE(30); |
---|
69 | DECLAR_BIGINT_TYPE(31); |
---|
70 | DECLAR_BIGINT_TYPE(32); |
---|
71 | #undef DECLAR_BIGINT_TYPE |
---|
72 | |
---|
73 | template< int W /* = SC_INTWIDTH */> |
---|
74 | class sc_bigint |
---|
75 | { |
---|
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 | void check () |
---|
88 | { if (W > 64) { std::cerr << "sc_bigint with W > 64 is not supported.\n"; exit (20040528); } } |
---|
89 | public: |
---|
90 | sc_bigint() { check (); val = 0; } |
---|
91 | sc_bigint(data_type val_) { check (); val = 0; write (val_); } |
---|
92 | template <int W2> explicit sc_bigint (const sc_bigint<W2> &val_) { write (val_.read());} |
---|
93 | |
---|
94 | inline const data_type& read() const { return val; } |
---|
95 | inline operator const data_type& () const { return read (); } |
---|
96 | // inline void write(int64 val_) { val = val_ & MASK64(W); } |
---|
97 | // inline void write(signed int val_) { val = val_ & MASK32(W); } |
---|
98 | inline void write(data_type val_) { valW = val_; } |
---|
99 | template <int W2> inline void write (const sc_bigint<W2> val_) { write (val_.read ()); } |
---|
100 | inline void write (const sc_bigint<W> val_) { write (val_.read()); }; |
---|
101 | template <typename T> inline sc_bigint& operator = (const T& val_) { write (val_); return *this; } |
---|
102 | |
---|
103 | inline uint32 to_uint () const {return val & MASK32(W);}; |
---|
104 | inline int32 to_int () const {return val & MASK32(W);}; |
---|
105 | inline uint64 to_uint64 () const {return val & MASK64(W);}; |
---|
106 | inline int64 to_int64 () const {return val & MASK64(W);}; |
---|
107 | |
---|
108 | template <typename T> |
---|
109 | inline sc_bigint& operator <<= (T v) |
---|
110 | { valW <<= v; return *this; } |
---|
111 | template <typename T> |
---|
112 | inline sc_bigint& operator >>= (T v) |
---|
113 | { valW >>= v; return *this; } |
---|
114 | template <typename T> |
---|
115 | inline sc_bigint& operator += (T v) |
---|
116 | { valW += v; return *this; } |
---|
117 | template <typename T> |
---|
118 | inline sc_bigint& operator -= (T v) |
---|
119 | { valW -= v; return *this; } |
---|
120 | template <typename T> |
---|
121 | inline sc_bigint& operator *= (T v) |
---|
122 | { valW *= v; return *this; } |
---|
123 | template <typename T> |
---|
124 | inline sc_bigint& operator /= (T v) |
---|
125 | { valW /= v; return *this; } |
---|
126 | template <typename T> |
---|
127 | inline sc_bigint& operator %= (T v) |
---|
128 | { valW %= v; return *this; } |
---|
129 | template <typename T> |
---|
130 | inline sc_bigint& operator &= (T v) |
---|
131 | { valW &= v; return *this; } |
---|
132 | template <typename T> |
---|
133 | inline sc_bigint& operator |= (T v) |
---|
134 | { valW |= v; return *this; } |
---|
135 | template <typename T> |
---|
136 | inline sc_bigint& operator ^= (T v) |
---|
137 | { valW ^= v; return *this; } |
---|
138 | inline sc_uint_bit_ref& operator [] (int v) |
---|
139 | { return valB[v]; } |
---|
140 | inline sc_uint_bit_ref_r& operator [] (int v) const |
---|
141 | { return valB[v]; } |
---|
142 | inline sc_uint_subref range (int left, int right) |
---|
143 | { return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right; } |
---|
144 | inline sc_uint_subref_r range (int left, int right) const |
---|
145 | { return (((~0) >> (sizeof (data_type) * 8 - left)) & val) >> right; } |
---|
146 | }; |
---|
147 | |
---|
148 | } /* end of sc_dt namespace */ |
---|
149 | |
---|
150 | #endif /* __SC_BIGINT_H__ */ |
---|