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