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