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