int128s.h

Go to the documentation of this file.
00001 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
00002  *
00003  *  Use of this source code is governed by a zlib-style license that can
00004  *  be found in the License.txt file in the root of the source tree.
00005  */
00006 
00007 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00008 //
00009 // based on http://Tringi.Mx-3.cz
00010 // Only adapted for ZenLib:
00011 // - .hpp --> .h
00012 // - Namespace
00013 // - int128s alias
00014 //
00015 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016 
00017 #ifndef INT128_HPP
00018 #define INT128_HPP
00019 
00020 /*
00021   Name: int128.hpp
00022   Copyright: Copyright (C) 2005, Jan Ringos
00023   Author: Jan Ringos, http://Tringi.Mx-3.cz
00024 
00025   Version: 1.1
00026 */
00027 
00028 #include <exception>
00029 #include <cstdlib>
00030 #include <cstdio>
00031 #ifdef __cplusplus
00032     #include <new> //for size_t
00033 #else /* __cplusplus */
00034     #include <stddef.h> //for size_t
00035 #endif /* __cplusplus */
00036 #include "ZenLib/Conf.h"
00037 
00038 namespace ZenLib
00039 {
00040 
00041 // CLASS
00042 
00043 class int128 {
00044     private:
00045         // Binary correct representation of signed 128bit integer
00046         int64u lo;
00047         int64s hi;
00048 
00049     protected:
00050         // Some global operator functions must be friends
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         // Constructors
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         // TODO: Consider creation of operator= to eliminate
00078         //       the need of intermediate objects during assignments.
00079 
00080     private:
00081         // Special internal constructors
00082         int128 (const int64u & a, const int64s & b) throw ()
00083             : lo (a), hi (b) {};
00084 
00085     public:
00086         // Operators
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         // Inline simple operators
00108         inline const int128 & operator + () const throw () { return *this; };
00109 
00110         // Rest of inline operators
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         // Common methods
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         // Arithmetic methods
00134         int128  div (const int128 &, int128 &) const throw ();
00135 
00136         // Bit operations
00137         bool    bit (unsigned int n) const throw ();
00138         void    bit (unsigned int n, bool val) throw ();
00139 }
00140 #ifdef __GNUC__
00141     __attribute__ ((__aligned__ (16), __packed__))
00142 #endif
00143 ;
00144 
00145 
00146 // GLOBAL OPERATORS
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 // GLOBAL OPERATOR INLINES
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 // MISC
00189 
00190 //typedef int128 __int128;
00191 
00192 typedef int128 int128s;
00193 } //NameSpace
00194 
00195 #endif

Generated on Thu Dec 1 09:55:27 2016 for ZenLib by  doxygen 1.4.7