ZenLib
int128s.h
Go to the documentation of this file.
00001 // int128s - 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 // - int128s alias
00028 //
00029 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00030 
00031 #ifndef INT128_HPP
00032 #define INT128_HPP
00033 
00034 /*
00035   Name: int128.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 #include <exception>
00043 #include <cstdlib>
00044 #include <cstdio>
00045 #include <new>
00046 #include "ZenLib/Conf.h"
00047 
00048 namespace ZenLib
00049 {
00050 
00051 // CLASS
00052 
00053 class int128 {
00054     private:
00055         // Binary correct representation of signed 128bit integer
00056         int64u lo;
00057         int64s hi;
00058 
00059     protected:
00060         // Some global operator functions must be friends
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         // Constructors
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         // TODO: Consider creation of operator= to eliminate
00088         //       the need of intermediate objects during assignments.
00089 
00090     private:
00091         // Special internal constructors
00092         int128 (const int64u & a, const int64s & b) throw ()
00093             : lo (a), hi (b) {};
00094 
00095     public:
00096         // Operators
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         // Inline simple operators
00118         inline const int128 & operator + () const throw () { return *this; };
00119 
00120         // Rest of inline operators
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         // Common methods
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         // Arithmetic methods
00144         int128  div (const int128 &, int128 &) const throw ();
00145 
00146         // Bit operations
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 // GLOBAL OPERATORS
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 // GLOBAL OPERATOR INLINES
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 // MISC
00199 
00200 //typedef int128 __int128;
00201 
00202 typedef int128 int128s;
00203 } //NameSpace
00204 
00205 #endif