00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __DYNINT_H__
00012 #define __DYNINT_H__
00013
00014 #include <shogun/lib/common.h>
00015 #include <shogun/lib/io.h>
00016 #include <shogun/lib/Mathematics.h>
00017
00034 template <class T, int sz> class CDynInt
00035 {
00036 public:
00041 CDynInt()
00042 {
00043 for (int i=0; i<sz; i++)
00044 integer[i]=0;
00045 }
00046
00053 CDynInt(uint8_t x)
00054 {
00055 for (int i=0; i<sz-1; i++)
00056 integer[i]=0;
00057 integer[sz-1]= (T) x;
00058 }
00059
00066 CDynInt(uint16_t x)
00067 {
00068 for (int i=0; i<sz-1; i++)
00069 integer[i]=0;
00070 integer[sz-1]= (T) x;
00071 }
00072
00079 CDynInt(uint32_t x)
00080 {
00081 for (int i=0; i<sz-1; i++)
00082 integer[i]=0;
00083 integer[sz-1]= (T) x;
00084 }
00085
00092 CDynInt(int32_t x)
00093 {
00094 for (int i=0; i<sz-1; i++)
00095 integer[i]=0;
00096 integer[sz-1]= (T) x;
00097 }
00098
00105 CDynInt(int64_t x)
00106 {
00107 for (int i=0; i<sz-1; i++)
00108 integer[i]=0;
00109 integer[sz-1]=(T) x;
00110 }
00111
00118 CDynInt(uint64_t x)
00119 {
00120 for (int i=0; i<sz-1; i++)
00121 integer[i]=0;
00122 integer[sz-1]=(T) x;
00123 }
00124
00131 CDynInt(const T x[sz])
00132 {
00133 for (int i=0; i<sz; i++)
00134 integer[i]=x[i];
00135 }
00136
00138 CDynInt(const CDynInt<T,sz> &x)
00139 {
00140 for (int i=0; i<sz; i++)
00141 integer[i]=x.integer[i];
00142 }
00143
00145 ~CDynInt()
00146 {
00147 }
00148
00152 CDynInt<T,sz>& operator=(const CDynInt<T,sz>& x)
00153 {
00154 for (int i=0; i<sz; i++)
00155 integer[i]=x.integer[i];
00156 return *this;
00157 }
00158
00163 const CDynInt<T,sz> operator|(const CDynInt<T,sz>& x) const
00164 {
00165 CDynInt<T,sz> r;
00166
00167 for (int i=sz-1; i>=0; i--)
00168 r.integer[i]=integer[i] | x.integer[i];
00169
00170 return r;
00171 }
00172
00177 const CDynInt<T,sz> operator&(const CDynInt<T,sz>& x) const
00178 {
00179 CDynInt<T,sz> r;
00180
00181 for (int i=sz-1; i>=0; i--)
00182 r.integer[i]=integer[i] & x.integer[i];
00183
00184 return r;
00185 }
00186
00193 CDynInt<T,sz> operator<<(int shift)
00194 {
00195 CDynInt<T,sz> r=*this;
00196
00197 while (shift>0)
00198 {
00199 int s=CMath::min(shift, 8*((int) sizeof(T))-1);
00200
00201 for (int i=0; i<sz; i++)
00202 {
00203 T overflow=0;
00204 if (i<sz-1)
00205 overflow = r.integer[i+1] >> (sizeof(T)*8 - s);
00206 r.integer[i]= (r.integer[i] << s) | overflow;
00207 }
00208
00209 shift-=s;
00210 }
00211
00212 return r;
00213 }
00214
00221 CDynInt<T,sz> operator>>(int shift)
00222 {
00223 CDynInt<T,sz> r=*this;
00224
00225 while (shift>0)
00226 {
00227 int s=CMath::min(shift, 8*((int) sizeof(T))-1);
00228
00229 for (int i=sz-1; i>=0; i--)
00230 {
00231 T overflow=0;
00232 if (i>0)
00233 overflow = (r.integer[i-1] << (sizeof(T)*8 - s));
00234 r.integer[i]= (r.integer[i] >> s) | overflow;
00235 }
00236
00237 shift-=s;
00238 }
00239
00240 return r;
00241 }
00242
00247 const CDynInt<T,sz> operator^(const CDynInt<T,sz>& x) const
00248 {
00249 CDynInt<T,sz> r;
00250
00251 for (int i=sz-1; i>=0; i--)
00252 r.integer[i]=integer[i] ^ x.integer[i];
00253
00254 return r;
00255 }
00256
00261 const CDynInt<T,sz> operator+(const CDynInt<T,sz> &x) const
00262 {
00263 CDynInt<T,sz> r;
00264
00265 T overflow=0;
00266 for (int i=sz-1; i>=0; i--)
00267 {
00268 r.integer[i]=integer[i]+x.integer[i]+overflow;
00269 if (r.integer[i] < CMath::max(integer[i], x.integer[i]))
00270 overflow=1;
00271 else
00272 overflow=0;
00273 }
00274
00275 return x;
00276 }
00277
00282 const CDynInt<T,sz> operator-(const CDynInt<T,sz> &x) const
00283 {
00284 return NULL;
00285 }
00286
00291 const CDynInt<T,sz> operator/(const CDynInt<T,sz> &x) const
00292 {
00293 return NULL;
00294 }
00295
00300 const CDynInt<T,sz> operator*(const CDynInt<T,sz> &x) const
00301 {
00302 return NULL;
00303 }
00304
00309 CDynInt<T,sz>& operator+=(const CDynInt<T,sz> &x)
00310 {
00311 return NULL;
00312 }
00313
00318 CDynInt<T,sz>& operator-=(const CDynInt<T,sz> &x)
00319 {
00320 return NULL;
00321 }
00322
00327 CDynInt<T,sz>& operator*=(const CDynInt<T,sz> &x)
00328 {
00329 return NULL;
00330 }
00331
00336 CDynInt<T,sz>& operator/=(const CDynInt<T,sz> &x)
00337 {
00338 return NULL;
00339 }
00340
00345 bool operator==(const CDynInt<T,sz> &x) const
00346 {
00347 for (int i=sz-1; i>=0; i--)
00348 {
00349 if (integer[i]!=x.integer[i])
00350 return false;
00351 }
00352
00353 return true;
00354 }
00355
00360 bool operator>=(const CDynInt<T,sz> &x) const
00361 {
00362 for (int i=0; i<sz; i++)
00363 {
00364 if (integer[i]>x.integer[i])
00365 return true;
00366 if (integer[i]<x.integer[i])
00367 return false;
00368 }
00369 return true;
00370 }
00371
00376 bool operator<=(const CDynInt<T,sz> &x) const
00377 {
00378 for (int i=0; i<sz; i++)
00379 {
00380 if (integer[i]<x.integer[i])
00381 return true;
00382 if (integer[i]>x.integer[i])
00383 return false;
00384 }
00385 return true;
00386 }
00387
00392 bool operator>(const CDynInt<T,sz> &x) const
00393 {
00394 for (int i=0; i<sz; i++)
00395 {
00396 if (integer[i]>x.integer[i])
00397 return true;
00398 if (integer[i]<x.integer[i])
00399 return false;
00400 }
00401 return false;
00402 }
00403
00408 bool operator<(const CDynInt<T,sz> &x) const
00409 {
00410 for (int i=0; i<sz; i++)
00411 {
00412 if (integer[i]<x.integer[i])
00413 return true;
00414 if (integer[i]>x.integer[i])
00415 return false;
00416 }
00417 return false;
00418 }
00419
00424 bool operator!=(const CDynInt<T,sz> &x) const
00425 {
00426 for (int i=sz-1; i>=0; i--)
00427 {
00428 if (integer[i]!=x.integer[i])
00429 return true;
00430 }
00431 return false;
00432 }
00433
00440 CDynInt<T,sz>& operator|=(const CDynInt<T,sz>& x)
00441 {
00442 for (int i=sz-1; i>=0; i--)
00443 integer[i]|=x.integer[i];
00444
00445 return *this;
00446 }
00447
00454 CDynInt<T,sz>& operator&=(const CDynInt<T,sz>& x)
00455 {
00456 for (int i=sz-1; i>=0; i--)
00457 integer[i]&=x.integer[i];
00458
00459 return *this;
00460 }
00461
00468 CDynInt<T,sz>& operator^=(const CDynInt<T,sz>& x)
00469 {
00470 for (int i=sz-1; i>=0; i--)
00471 integer[i]^=x.integer[i];
00472
00473 return *this;
00474 }
00475
00482 CDynInt<T,sz>& operator<<=(int shift)
00483 {
00484 *this=*this<<shift;
00485 return *this;
00486 }
00487
00494 CDynInt<T,sz>& operator>>=(int shift)
00495 {
00496 *this=*this>>shift;
00497 return *this;
00498 }
00499
00501 CDynInt<T,sz>& operator~()
00502 {
00503 for (int i=sz-1; i>=0; i--)
00504 integer[i]= ~integer[i];
00505 return *this;
00506 }
00507
00509 operator T() { return integer[sz-1]; }
00510
00512 CDynInt<T,sz>& operator--()
00513 {
00514 T overflow=0;
00515 for (int i=sz-1; i>=0; i--)
00516 {
00517 T x = integer[i]-1-overflow;
00518 overflow=0;
00519 if (integer[i]>x)
00520 overflow=1;
00521 integer[i]=x;
00522 }
00523 return *this;
00524 }
00525
00527 CDynInt<T,sz>& operator++()
00528 {
00529 T overflow=0;
00530 for (int i=sz-1; i>=0; i--)
00531 {
00532 T x = integer[i]+1+overflow;
00533 overflow=0;
00534 if (integer[i]>x)
00535 overflow=1;
00536 integer[i]=x;
00537 }
00538 return *this;
00539 }
00540
00542 void print_hex() const
00543 {
00544 for (int i=0; i<sz; i++)
00545 SG_SPRINT("%.16llx", (uint64_t) integer[i]);
00546 }
00547
00549 void print_bits() const
00550 {
00551 CMath::display_bits(integer, sz);
00552 }
00553
00554 private:
00556 T integer[sz];
00557 };
00558
00560 typedef CDynInt<uint64_t,3> uint192_t;
00561 typedef CDynInt<uint64_t,3> uint256_t;
00562 typedef CDynInt<uint64_t,3> uint512_t;
00563 typedef CDynInt<uint64_t,3> uint1024_t;
00564 #endif // __DYNINT_H__