ZenLib
int128s.h
Go to the documentation of this file.
1 // int128s - integer 8 bytes
2 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 //
20 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 // based on http://Tringi.Mx-3.cz
24 // Only adapted for ZenLib:
25 // - .hpp --> .h
26 // - Namespace
27 // - int128s alias
28 //
29 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30 
31 #ifndef INT128_HPP
32 #define INT128_HPP
33 
34 /*
35  Name: int128.hpp
36  Copyright: Copyright (C) 2005, Jan Ringos
37  Author: Jan Ringos, http://Tringi.Mx-3.cz
38 
39  Version: 1.1
40 */
41 
42 #include <exception>
43 #include <cstdlib>
44 #include <cstdio>
45 #include <new>
46 #include "ZenLib/Conf.h"
47 
48 namespace ZenLib
49 {
50 
51 // CLASS
52 
53 class int128 {
54  private:
55  // Binary correct representation of signed 128bit integer
56  int64u lo;
57  int64s hi;
58 
59  protected:
60  // Some global operator functions must be friends
61  friend bool operator < (const int128 &, const int128 &) throw ();
62  friend bool operator == (const int128 &, const int128 &) throw ();
63  friend bool operator || (const int128 &, const int128 &) throw ();
64  friend bool operator && (const int128 &, const int128 &) throw ();
65 
66  public:
67  // Constructors
68  inline int128 () throw () {};
69  inline int128 (const int128 & a) throw () : lo (a.lo), hi (a.hi) {};
70 
71  inline int128 (const unsigned int & a) throw () : lo (a), hi (0ll) {};
72  inline int128 (const signed int & a) throw () : lo (a), hi (0ll) {
73  if (a < 0) this->hi = -1ll;
74  };
75 
76  inline int128 (const int64u & a) throw () : lo (a), hi (0ll) {};
77  inline int128 (const int64s & a) throw () : lo (a), hi (0ll) {
78  if (a < 0) this->hi = -1ll;
79  };
80 
81  int128 (const float a) throw ();
82  int128 (const double & a) throw ();
83  int128 (const long double & a) throw ();
84 
85  int128 (const char * sz) throw ();
86 
87  // TODO: Consider creation of operator= to eliminate
88  // the need of intermediate objects during assignments.
89 
90  private:
91  // Special internal constructors
92  int128 (const int64u & a, const int64s & b) throw ()
93  : lo (a), hi (b) {};
94 
95  public:
96  // Operators
97  bool operator ! () const throw ();
98 
99  int128 operator - () const throw ();
100  int128 operator ~ () const throw ();
101 
102  int128 & operator ++ ();
103  int128 & operator -- ();
104  int128 operator ++ (int);
105  int128 operator -- (int);
106 
107  int128 & operator += (const int128 & b) throw ();
108  int128 & operator *= (const int128 & b) throw ();
109 
110  int128 & operator >>= (unsigned int n) throw ();
111  int128 & operator <<= (unsigned int n) throw ();
112 
113  int128 & operator |= (const int128 & b) throw ();
114  int128 & operator &= (const int128 & b) throw ();
115  int128 & operator ^= (const int128 & b) throw ();
116 
117  // Inline simple operators
118  inline const int128 & operator + () const throw () { return *this; };
119 
120  // Rest of inline operators
121  inline int128 & operator -= (const int128 & b) throw () {
122  return *this += (-b);
123  };
124  inline int128 & operator /= (const int128 & b) throw () {
125  int128 dummy;
126  *this = this->div (b, dummy);
127  return *this;
128  };
129  inline int128 & operator %= (const int128 & b) throw () {
130  this->div (b, *this);
131  return *this;
132  };
133 
134  // Common methods
135  int toInt () const throw () { return (int) this->lo; };
136  int64s toInt64 () const throw () { return (int64s) this->lo; };
137 
138  const char * toString (unsigned int radix = 10) const throw ();
139  float toFloat () const throw ();
140  double toDouble () const throw ();
141  long double toLongDouble () const throw ();
142 
143  // Arithmetic methods
144  int128 div (const int128 &, int128 &) const throw ();
145 
146  // Bit operations
147  bool bit (unsigned int n) const throw ();
148  void bit (unsigned int n, bool val) throw ();
149 }
150 #ifdef __GNUC__
151  __attribute__ ((__aligned__ (16), __packed__))
152 #endif
153 ;
154 
155 
156 // GLOBAL OPERATORS
157 
158 bool operator < (const int128 & a, const int128 & b) throw ();
159 bool operator == (const int128 & a, const int128 & b) throw ();
160 bool operator || (const int128 & a, const int128 & b) throw ();
161 bool operator && (const int128 & a, const int128 & b) throw ();
162 
163 // GLOBAL OPERATOR INLINES
164 
165 inline int128 operator + (const int128 & a, const int128 & b) throw () {
166  return int128 (a) += b; };
167 inline int128 operator - (const int128 & a, const int128 & b) throw () {
168  return int128 (a) -= b; };
169 inline int128 operator * (const int128 & a, const int128 & b) throw () {
170  return int128 (a) *= b; };
171 inline int128 operator / (const int128 & a, const int128 & b) throw () {
172  return int128 (a) /= b; };
173 inline int128 operator % (const int128 & a, const int128 & b) throw () {
174  return int128 (a) %= b; };
175 
176 inline int128 operator >> (const int128 & a, unsigned int n) throw () {
177  return int128 (a) >>= n; };
178 inline int128 operator << (const int128 & a, unsigned int n) throw () {
179  return int128 (a) <<= n; };
180 
181 inline int128 operator & (const int128 & a, const int128 & b) throw () {
182  return int128 (a) &= b; };
183 inline int128 operator | (const int128 & a, const int128 & b) throw () {
184  return int128 (a) |= b; };
185 inline int128 operator ^ (const int128 & a, const int128 & b) throw () {
186  return int128 (a) ^= b; };
187 
188 inline bool operator > (const int128 & a, const int128 & b) throw () {
189  return b < a; };
190 inline bool operator <= (const int128 & a, const int128 & b) throw () {
191  return !(b < a); };
192 inline bool operator >= (const int128 & a, const int128 & b) throw () {
193  return !(a < b); };
194 inline bool operator != (const int128 & a, const int128 & b) throw () {
195  return !(a == b); };
196 
197 
198 // MISC
199 
200 //typedef int128 __int128;
201 
202 typedef int128 int128s;
203 } //NameSpace
204 
205 #endif