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