[1] | 1 | /*------------------------------------------------------------\ |
---|
[52] | 2 | | | |
---|
| 3 | | Tool : systemcass | |
---|
| 4 | | | |
---|
| 5 | | File : sc_bigint.h | |
---|
| 6 | | | |
---|
| 7 | | Author : Buchmann Richard | |
---|
| 8 | | | |
---|
| 9 | | Date : 09_07_2004 | |
---|
| 10 | | | |
---|
| 11 | \------------------------------------------------------------*/ |
---|
[1] | 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 | |
---|
[27] | 24 | #include "sc_nbdefs.h" |
---|
[1] | 25 | |
---|
| 26 | namespace 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 | |
---|
| 31 | template<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 |
---|
[52] | 33 | DECLAR_BIGINT_TYPE(1); |
---|
| 34 | DECLAR_BIGINT_TYPE(2); |
---|
| 35 | DECLAR_BIGINT_TYPE(3); |
---|
| 36 | DECLAR_BIGINT_TYPE(4); |
---|
| 37 | DECLAR_BIGINT_TYPE(5); |
---|
| 38 | DECLAR_BIGINT_TYPE(6); |
---|
| 39 | DECLAR_BIGINT_TYPE(7); |
---|
| 40 | DECLAR_BIGINT_TYPE(8); |
---|
[1] | 41 | #undef DECLAR_BIGINT_TYPE |
---|
[52] | 42 | |
---|
[1] | 43 | #define DECLAR_BIGINT_TYPE(W) template<> struct s_bigint_type<W> { typedef uint16 uint_type; } |
---|
[52] | 44 | DECLAR_BIGINT_TYPE(9); |
---|
[1] | 45 | DECLAR_BIGINT_TYPE(10); |
---|
| 46 | DECLAR_BIGINT_TYPE(11); |
---|
| 47 | DECLAR_BIGINT_TYPE(12); |
---|
| 48 | DECLAR_BIGINT_TYPE(13); |
---|
| 49 | DECLAR_BIGINT_TYPE(14); |
---|
| 50 | DECLAR_BIGINT_TYPE(15); |
---|
| 51 | DECLAR_BIGINT_TYPE(16); |
---|
| 52 | #undef DECLAR_BIGINT_TYPE |
---|
[52] | 53 | |
---|
[1] | 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 */> |
---|
[52] | 74 | class sc_bigint { |
---|
| 75 | typedef sc_bigint<W> this_type; |
---|
| 76 | typedef typename s_int_type<W>::int_type data_type; |
---|
[1] | 77 | |
---|
[52] | 78 | typedef data_type sc_uint_subref; |
---|
| 79 | typedef data_type sc_uint_subref_r; |
---|
[1] | 80 | |
---|
[52] | 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 | } |
---|
[1] | 93 | |
---|
[52] | 94 | public: |
---|
| 95 | sc_bigint() { |
---|
| 96 | check (); |
---|
| 97 | val = 0; |
---|
| 98 | } |
---|
[1] | 99 | |
---|
[52] | 100 | sc_bigint(data_type val_) { |
---|
| 101 | check (); |
---|
| 102 | val = 0; |
---|
| 103 | write (val_); |
---|
| 104 | } |
---|
[1] | 105 | |
---|
[52] | 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 | |
---|
[1] | 230 | }; |
---|
| 231 | |
---|
| 232 | } /* end of sc_dt namespace */ |
---|
| 233 | |
---|
| 234 | #endif /* __SC_BIGINT_H__ */ |
---|
[52] | 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 | |
---|