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