00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef UINT128_HPP
00032 #define UINT128_HPP
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <exception>
00044 #include <cstdlib>
00045 #include <cstdio>
00046 #include <new>
00047 #include "ZenLib/Conf.h"
00048
00049
00050
00051 namespace ZenLib
00052 {
00053
00054 class uint128 {
00055 public:
00056
00057 int64u lo;
00058 int64u hi;
00059
00060 protected:
00061
00062 friend bool operator < (const uint128 &, const uint128 &) throw ();
00063 friend bool operator == (const uint128 &, const uint128 &) throw ();
00064 friend bool operator || (const uint128 &, const uint128 &) throw ();
00065 friend bool operator && (const uint128 &, const uint128 &) throw ();
00066
00067 public:
00068
00069 inline uint128 () throw () {};
00070 inline uint128 (const uint128 & a) throw () : lo (a.lo), hi (a.hi) {};
00071
00072 inline uint128 (const int & a) throw () : lo (a), hi (0ull) {};
00073 inline uint128 (const unsigned int & a) throw () : lo (a), hi (0ull) {};
00074 inline uint128 (const int64u & a) throw () : lo (a), hi (0ull) {};
00075
00076 uint128 (const float a) throw ();
00077 uint128 (const double & a) throw ();
00078 uint128 (const long double & a) throw ();
00079
00080 uint128 (const char * sz) throw ();
00081
00082
00083
00084
00085 private:
00086
00087 uint128 (const int64u & a, const int64u & b) throw ()
00088 : lo (a), hi (b) {};
00089
00090 public:
00091
00092 bool operator ! () const throw ();
00093
00094 uint128 operator - () const throw ();
00095 uint128 operator ~ () const throw ();
00096
00097 uint128 & operator ++ ();
00098 uint128 & operator -- ();
00099 uint128 operator ++ (int);
00100 uint128 operator -- (int);
00101
00102 uint128 & operator += (const uint128 & b) throw ();
00103 uint128 & operator *= (const uint128 & b) throw ();
00104
00105 uint128 & operator >>= (unsigned int n) throw ();
00106 uint128 & operator <<= (unsigned int n) throw ();
00107
00108 uint128 & operator |= (const uint128 & b) throw ();
00109 uint128 & operator &= (const uint128 & b) throw ();
00110 uint128 & operator ^= (const uint128 & b) throw ();
00111
00112
00113 inline const uint128 & operator + () const throw () { return *this; };
00114
00115
00116 inline uint128 & operator -= (const uint128 & b) throw () {
00117 return *this += (-b);
00118 };
00119 inline uint128 & operator /= (const uint128 & b) throw () {
00120 uint128 dummy;
00121 *this = this->div (b, dummy);
00122 return *this;
00123 };
00124 inline uint128 & operator %= (const uint128 & b) throw () {
00125 this->div (b, *this);
00126 return *this;
00127 };
00128
00129
00130 unsigned int toUint () const throw () {
00131 return (unsigned int) this->lo; };
00132 int64u toUint64 () const throw () {
00133 return (int64u) this->lo; };
00134 const char * toString (unsigned int radix = 10) const throw ();
00135 float toFloat () const throw ();
00136 double toDouble () const throw ();
00137 long double toLongDouble () const throw ();
00138
00139
00140 uint128 div (const uint128 &, uint128 &) const throw ();
00141
00142
00143 bool bit (unsigned int n) const throw ();
00144 void bit (unsigned int n, bool val) throw ();
00145 }
00146 #ifdef __GNUC__
00147 __attribute__ ((__aligned__ (16), __packed__))
00148 #endif
00149 ;
00150
00151
00152
00153
00154 bool operator < (const uint128 & a, const uint128 & b) throw ();
00155 bool operator == (const uint128 & a, const uint128 & b) throw ();
00156 bool operator || (const uint128 & a, const uint128 & b) throw ();
00157 bool operator && (const uint128 & a, const uint128 & b) throw ();
00158
00159
00160
00161 inline uint128 operator + (const uint128 & a, const uint128 & b) throw () {
00162 return uint128 (a) += b; };
00163 inline uint128 operator - (const uint128 & a, const uint128 & b) throw () {
00164 return uint128 (a) -= b; };
00165 inline uint128 operator * (const uint128 & a, const uint128 & b) throw () {
00166 return uint128 (a) *= b; };
00167 inline uint128 operator / (const uint128 & a, const uint128 & b) throw () {
00168 return uint128 (a) /= b; };
00169 inline uint128 operator % (const uint128 & a, const uint128 & b) throw () {
00170 return uint128 (a) %= b; };
00171
00172 inline uint128 operator >> (const uint128 & a, unsigned int n) throw () {
00173 return uint128 (a) >>= n; };
00174 inline uint128 operator << (const uint128 & a, unsigned int n) throw () {
00175 return uint128 (a) <<= n; };
00176
00177 inline uint128 operator & (const uint128 & a, const uint128 & b) throw () {
00178 return uint128 (a) &= b; };
00179 inline uint128 operator | (const uint128 & a, const uint128 & b) throw () {
00180 return uint128 (a) |= b; };
00181 inline uint128 operator ^ (const uint128 & a, const uint128 & b) throw () {
00182 return uint128 (a) ^= b; };
00183
00184 inline bool operator > (const uint128 & a, const uint128 & b) throw () {
00185 return b < a; };
00186 inline bool operator <= (const uint128 & a, const uint128 & b) throw () {
00187 return !(b < a); };
00188 inline bool operator >= (const uint128 & a, const uint128 & b) throw () {
00189 return !(a < b); };
00190 inline bool operator != (const uint128 & a, const uint128 & b) throw () {
00191 return !(a == b); };
00192
00193
00194
00195
00196 typedef uint128 __uint128;
00197
00198 typedef uint128 int128u;
00199 }
00200
00201 #endif