source: latest/src/sc_biguint.h @ 1

Last change on this file since 1 was 1, checked in by buchmann, 17 years ago

Initial import from CVS repository

File size: 5.2 KB
Line 
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
22namespace 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
29template<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
31DECLAR_BIGUINT_TYPE( 1);
32DECLAR_BIGUINT_TYPE( 2);
33DECLAR_BIGUINT_TYPE( 3);
34DECLAR_BIGUINT_TYPE( 4);
35DECLAR_BIGUINT_TYPE( 5);
36DECLAR_BIGUINT_TYPE( 6);
37DECLAR_BIGUINT_TYPE( 7);
38DECLAR_BIGUINT_TYPE( 8);
39#undef DECLAR_BIGUINT_TYPE
40#define DECLAR_BIGUINT_TYPE(W) template<> struct s_biguint_type<W> { typedef uint16 uint_type; }
41DECLAR_BIGUINT_TYPE( 9);
42DECLAR_BIGUINT_TYPE(10);
43DECLAR_BIGUINT_TYPE(11);
44DECLAR_BIGUINT_TYPE(12);
45DECLAR_BIGUINT_TYPE(13);
46DECLAR_BIGUINT_TYPE(14);
47DECLAR_BIGUINT_TYPE(15);
48DECLAR_BIGUINT_TYPE(16);
49#undef DECLAR_BIGUINT_TYPE
50#define DECLAR_BIGUINT_TYPE(W) template<> struct s_biguint_type<W> { typedef uint32 uint_type; }
51DECLAR_BIGUINT_TYPE(17);
52DECLAR_BIGUINT_TYPE(18);
53DECLAR_BIGUINT_TYPE(19);
54DECLAR_BIGUINT_TYPE(20);
55DECLAR_BIGUINT_TYPE(21);
56DECLAR_BIGUINT_TYPE(22);
57DECLAR_BIGUINT_TYPE(23);
58DECLAR_BIGUINT_TYPE(24);
59DECLAR_BIGUINT_TYPE(25);
60DECLAR_BIGUINT_TYPE(26);
61DECLAR_BIGUINT_TYPE(27);
62DECLAR_BIGUINT_TYPE(28);
63DECLAR_BIGUINT_TYPE(29);
64DECLAR_BIGUINT_TYPE(30);
65DECLAR_BIGUINT_TYPE(31);
66DECLAR_BIGUINT_TYPE(32);
67#undef DECLAR_BIGUINT_TYPE
68
69template< int W /* = SC_INTWIDTH */>
70class 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); } }
85public:
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__ */
Note: See TracBrowser for help on using the repository browser.