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