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