|
1 // uint64.h |
|
2 // minimal double precision unsigned int arithmetics for numeric_facets support. |
|
3 // Written by Tsutomu Yoshida, Minokamo, Japan. 03/25/2000 |
|
4 |
|
5 #ifndef _UINT64_H |
|
6 #define _UINT64_H |
|
7 |
|
8 |
|
9 |
|
10 template <class _Tp> |
|
11 class _compound_int : public _Tp |
|
12 { |
|
13 public: |
|
14 typedef _compound_int<_Tp> _Self; |
|
15 |
|
16 // Constructors, destructor, assignment operator. |
|
17 _compound_int(); // platform specific |
|
18 _compound_int(unsigned long); // platform specific |
|
19 _compound_int(unsigned long hi, unsigned long lo); // platform specific |
|
20 _compound_int(const _Tp& rhs) : _Tp(rhs) {} |
|
21 |
|
22 // Arithmetic op= operations involving two _compound_int. |
|
23 _Self& operator+= (const _Self&); // platform specific |
|
24 _Self& operator-= (const _Self&); // platform specific |
|
25 _Self& operator*= (const _Self&); // platform specific |
|
26 _Self& operator/= (const _Self&); // platform specific |
|
27 _Self& operator%= (const _Self&); // platform specific |
|
28 _Self& operator&= (const _Self&); // platform specific |
|
29 _Self& operator|= (const _Self&); // platform specific |
|
30 _Self& operator^= (const _Self&); // platform specific |
|
31 |
|
32 // Arithmetic op= operations involving built-in integer. |
|
33 _Self& operator<<= (unsigned int); // platform specific |
|
34 _Self& operator>>= (unsigned int); // platform specific |
|
35 |
|
36 _Self& operator= (unsigned long rhs) { return *this = _Self(rhs); } |
|
37 _Self& operator+= (unsigned long rhs) { return *this += _Self(rhs); } |
|
38 _Self& operator-= (unsigned long rhs) { return *this -= _Self(rhs); } |
|
39 _Self& operator*= (unsigned long rhs) { return *this *= _Self(rhs); } |
|
40 _Self& operator/= (unsigned long rhs) { return *this /= _Self(rhs); } |
|
41 _Self& operator%= (unsigned long rhs) { return *this %= _Self(rhs); } |
|
42 _Self& operator&= (unsigned long rhs) { return *this &= _Self(rhs); } |
|
43 _Self& operator|= (unsigned long rhs) { return *this |= _Self(rhs); } |
|
44 _Self& operator^= (unsigned long rhs) { return *this ^= _Self(rhs); } |
|
45 |
|
46 // Increment and decrement |
|
47 _Self& operator++() { return (*this) += 1; } |
|
48 _Self& operator--() { return (*this) -= 1; } |
|
49 _Self operator++(int) { _Self temp(*this); ++(*this); return temp; } |
|
50 _Self operator--(int) { _Self temp(*this); --(*this); return temp; } |
|
51 }; |
|
52 |
|
53 // Comparison operators. |
|
54 template <class _Tp> bool operator==(const _compound_int<_Tp>&, const _compound_int<_Tp>&); // platform specific |
|
55 template <class _Tp> bool operator<(const _compound_int<_Tp>&, const _compound_int<_Tp>&); // platform specific |
|
56 |
|
57 template <class _Tp> inline bool operator==(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs == _compound_int<_Tp>(rhs); } |
|
58 template <class _Tp> inline bool operator==(unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs == lhs; } |
|
59 |
|
60 template <class _Tp> inline bool operator< (const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs < _compound_int<_Tp>(rhs); } |
|
61 template <class _Tp> inline bool operator< (unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) < rhs; } |
|
62 |
|
63 template <class _Tp> inline bool operator!=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs == rhs); } |
|
64 template <class _Tp> inline bool operator!=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs == rhs); } |
|
65 |
|
66 template <class _Tp> inline bool operator> (const _compound_int<_Tp>& lhs, unsigned long rhs) { return rhs < lhs; } |
|
67 template <class _Tp> inline bool operator> (unsigned long lhs, const _compound_int<_Tp>& rhs) { return rhs < lhs; } |
|
68 |
|
69 template <class _Tp> inline bool operator<=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs > rhs); } |
|
70 template <class _Tp> inline bool operator<=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs > rhs); } |
|
71 |
|
72 template <class _Tp> inline bool operator>=(const _compound_int<_Tp>& lhs, unsigned long rhs) { return !(lhs < rhs); } |
|
73 template <class _Tp> inline bool operator>=(unsigned long lhs, const _compound_int<_Tp>& rhs) { return !(lhs < rhs); } |
|
74 |
|
75 // Unary non-member arithmetic operators. |
|
76 template <class _Tp> unsigned long to_ulong(const _compound_int<_Tp>&); // platform specific |
|
77 template <class _Tp> _compound_int<_Tp> operator~(const _compound_int<_Tp>&); // platform specific |
|
78 |
|
79 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& val) {return val;} |
|
80 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& val) {return 0 - val;} |
|
81 template <class _Tp> inline bool operator!(const _compound_int<_Tp>& val) {return val==0;} |
|
82 |
|
83 // Non-member arithmetic operations involving two _compound_int arguments |
|
84 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp += rhs; } |
|
85 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp -= rhs; } |
|
86 template <class _Tp> inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp *= rhs; } |
|
87 template <class _Tp> inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp /= rhs; } |
|
88 template <class _Tp> inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp %= rhs; } |
|
89 template <class _Tp> inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp &= rhs; } |
|
90 template <class _Tp> inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp |= rhs; } |
|
91 template <class _Tp> inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, const _compound_int<_Tp>& rhs) { _compound_int<_Tp> temp(lhs); return temp ^= rhs; } |
|
92 |
|
93 // Non-member arithmetic operations involving one built-in integer argument. |
|
94 template <class _Tp> inline _compound_int<_Tp> operator+(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs + _compound_int<_Tp>(rhs); } |
|
95 template <class _Tp> inline _compound_int<_Tp> operator+(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) + rhs; } |
|
96 |
|
97 template <class _Tp> inline _compound_int<_Tp> operator-(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs - _compound_int<_Tp>(rhs); } |
|
98 template <class _Tp> inline _compound_int<_Tp> operator-(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) - rhs; } |
|
99 |
|
100 template <class _Tp> inline _compound_int<_Tp> operator*(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs * _compound_int<_Tp>(rhs); } |
|
101 template <class _Tp> inline _compound_int<_Tp> operator*(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) * rhs; } |
|
102 |
|
103 template <class _Tp> inline _compound_int<_Tp> operator/(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs / _compound_int<_Tp>(rhs); } |
|
104 template <class _Tp> inline _compound_int<_Tp> operator/(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) / rhs; } |
|
105 |
|
106 template <class _Tp> inline _compound_int<_Tp> operator%(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs % _compound_int<_Tp>(rhs); } |
|
107 template <class _Tp> inline _compound_int<_Tp> operator%(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) % rhs; } |
|
108 |
|
109 template <class _Tp> inline _compound_int<_Tp> operator&(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs & _compound_int<_Tp>(rhs); } |
|
110 template <class _Tp> inline _compound_int<_Tp> operator&(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) & rhs; } |
|
111 |
|
112 template <class _Tp> inline _compound_int<_Tp> operator|(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs | _compound_int<_Tp>(rhs); } |
|
113 template <class _Tp> inline _compound_int<_Tp> operator|(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) | rhs; } |
|
114 |
|
115 template <class _Tp> inline _compound_int<_Tp> operator^(const _compound_int<_Tp>& lhs, unsigned long rhs) { return lhs ^ _compound_int<_Tp>(rhs); } |
|
116 template <class _Tp> inline _compound_int<_Tp> operator^(unsigned long lhs, const _compound_int<_Tp>& rhs) { return _compound_int<_Tp>(lhs) ^ rhs; } |
|
117 |
|
118 template <class _Tp> inline _compound_int<_Tp> operator<<(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp <<= rhs; } |
|
119 template <class _Tp> inline _compound_int<_Tp> operator>>(const _compound_int<_Tp>& lhs, unsigned int rhs) { _compound_int<_Tp> temp(lhs); return temp >>= rhs; } |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 // platform specific specializations |
|
125 |
|
126 #if defined(__MRC__)||defined(__SC__) |
|
127 |
|
128 _STLP_END_NAMESPACE // ugly! |
|
129 # include <Math64.h> |
|
130 # include <utility> |
|
131 # undef modff //*TY 04/06/2000 - defined in <math.h> which conflicts with <fp.h> definition |
|
132 # include <fp.h> |
|
133 |
|
134 _STLP_BEGIN_NAMESPACE |
|
135 |
|
136 # if TYPE_LONGLONG |
|
137 typedef UInt64 uint64; |
|
138 # define ULL(x) (U64SetU(x)) |
|
139 |
|
140 # else |
|
141 // Apple's mpw sc compiler for 68k macintosh does not support native 64bit integer type. |
|
142 // Instead, it comes with external support library and struct data type representing 64bit int: |
|
143 // |
|
144 // struct UnsignedWide { |
|
145 // UInt32 hi; |
|
146 // UInt32 lo; |
|
147 // }; |
|
148 |
|
149 typedef _compound_int<UnsignedWide> uint64; |
|
150 # define ULL(x) uint64(x) |
|
151 # define ULL2(hi,lo) {hi,lo} |
|
152 |
|
153 // Constructors, destructor, assignment operator. |
|
154 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int() { hi = 0; lo = 0; } |
|
155 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long val) { hi = 0; lo = val; } |
|
156 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>::_compound_int(unsigned long h, unsigned long l) { hi = h; lo = l; } |
|
157 |
|
158 // Arithmetic op= operations involving two _compound_int. |
|
159 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator+= (const _compound_int<UnsignedWide>& rhs) { *this = U64Add( *this, rhs ); return *this; } |
|
160 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator-= (const _compound_int<UnsignedWide>& rhs) { *this = U64Subtract( *this, rhs ); return *this; } |
|
161 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator*= (const _compound_int<UnsignedWide>& rhs) { *this = U64Multiply( *this, rhs ); return *this; } |
|
162 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator/= (const _compound_int<UnsignedWide>& rhs) { *this = U64Divide( *this, rhs, NULL ); return *this; } |
|
163 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator%= (const _compound_int<UnsignedWide>& rhs) { U64Divide( *this, rhs, this ); return *this; } |
|
164 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator&= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseAnd( *this, rhs ); return *this; } |
|
165 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator|= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseOr( *this, rhs ); return *this; } |
|
166 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator^= (const _compound_int<UnsignedWide>& rhs) { *this = U64BitwiseEor( *this, rhs ); return *this; } |
|
167 |
|
168 // Arithmetic op= operations involving built-in integer. |
|
169 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator<<= (unsigned int rhs) { *this = U64ShiftLeft( *this, rhs ); return *this; } |
|
170 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide>& _compound_int<UnsignedWide>::operator>>= (unsigned int rhs) { *this = U64ShiftRight( *this, rhs ); return *this; } |
|
171 |
|
172 // Comparison operators. |
|
173 _STLP_TEMPLATE_NULL inline bool operator==(const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs) { return (lhs.hi == rhs.hi) && (lhs.lo == rhs.lo); } |
|
174 _STLP_TEMPLATE_NULL inline bool operator< (const _compound_int<UnsignedWide>& lhs, const _compound_int<UnsignedWide>& rhs) { return U64Compare( lhs, rhs ) < 0; } |
|
175 _STLP_TEMPLATE_NULL inline bool operator==(const _compound_int<UnsignedWide>& lhs, unsigned long rhs) { return (lhs.hi == 0) && (lhs.lo == rhs); } |
|
176 |
|
177 // Unary non-member arithmetic operators. |
|
178 _STLP_TEMPLATE_NULL inline unsigned long to_ulong(const _compound_int<UnsignedWide>& val) { return val.lo; } |
|
179 _STLP_TEMPLATE_NULL inline _compound_int<UnsignedWide> operator~(const _compound_int<UnsignedWide>& val) { return U64BitwiseNot( val ); } |
|
180 _STLP_TEMPLATE_NULL inline bool operator!(const _compound_int<UnsignedWide>& val) { return !((val.hi == 0) && (val.lo == 0)); } |
|
181 |
|
182 # endif // TYPE_LONGLONG |
|
183 #endif // __MRC__ |
|
184 #endif // _UINT64_H |