ZenLib
|
00001 // int128u - integer 8 bytes 00002 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net 00003 // 00004 // This software is provided 'as-is', without any express or implied 00005 // warranty. In no event will the authors be held liable for any damages 00006 // arising from the use of this software. 00007 // 00008 // Permission is granted to anyone to use this software for any purpose, 00009 // including commercial applications, and to alter it and redistribute it 00010 // freely, subject to the following restrictions: 00011 // 00012 // 1. The origin of this software must not be misrepresented; you must not 00013 // claim that you wrote the original software. If you use this software 00014 // in a product, an acknowledgment in the product documentation would be 00015 // appreciated but is not required. 00016 // 2. Altered source versions must be plainly marked as such, and must not be 00017 // misrepresented as being the original software. 00018 // 3. This notice may not be removed or altered from any source distribution. 00019 // 00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 // 00023 // based on http://Tringi.Mx-3.cz 00024 // Only adapted for ZenLib: 00025 // - .hpp --> .h 00026 // - Namespace 00027 // - int128u alias 00028 // 00029 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00030 00031 #ifndef UINT128_HPP 00032 #define UINT128_HPP 00033 00034 /* 00035 Name: uint128.hpp 00036 Copyright: Copyright (C) 2005, Jan Ringos 00037 Author: Jan Ringos, http://Tringi.Mx-3.cz 00038 00039 Version: 1.1 00040 */ 00041 00042 00043 #include <exception> 00044 #include <cstdlib> 00045 #include <cstdio> 00046 #include <new> 00047 #include "ZenLib/Conf.h" 00048 00049 // CLASS 00050 00051 namespace ZenLib 00052 { 00053 00054 class uint128 { 00055 public://private: 00056 // Binary correct representation of signed 128bit integer 00057 int64u lo; 00058 int64u hi; 00059 00060 protected: 00061 // Some global operator functions must be friends 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 // Constructors 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 // TODO: Consider creation of operator= to eliminate 00083 // the need of intermediate objects during assignments. 00084 00085 private: 00086 // Special internal constructors 00087 uint128 (const int64u & a, const int64u & b) throw () 00088 : lo (a), hi (b) {}; 00089 00090 public: 00091 // Operators 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 // Inline simple operators 00113 inline const uint128 & operator + () const throw () { return *this; }; 00114 00115 // Rest of inline operators 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 // Common methods 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 // Arithmetic methods 00140 uint128 div (const uint128 &, uint128 &) const throw (); 00141 00142 // Bit operations 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 // GLOBAL OPERATORS 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 // GLOBAL OPERATOR INLINES 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 // MISC 00195 00196 typedef uint128 __uint128; 00197 00198 typedef uint128 int128u; 00199 } //NameSpace 00200 00201 #endif