ZenLib

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

int128s.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2 *
3 * Use of this source code is governed by a zlib-style license that can
4 * be found in the License.txt file in the root of the source tree.
5 */
6
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // based on http://Tringi.Mx-3.cz
10 // Only adapted for ZenLib:
11 // - .hpp --> .h
12 // - Namespace
13 // - int128s alias
14 //
15 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
17 #ifndef INT128_HPP
18 #define INT128_HPP
19
20 /*
21 Name: int128.hpp
22 Copyright: Copyright (C) 2005, Jan Ringos
23 Author: Jan Ringos, http://Tringi.Mx-3.cz
24
25 Version: 1.1
26 */
27
28 #include <exception>
29 #include <cstdlib>
30 #include <cstdio>
31 #include <new>
32 #include "ZenLib/Conf.h"
33
34 namespace ZenLib
35 {
36
37 // CLASS
38
39 class int128 {
40 private:
41 // Binary correct representation of signed 128bit integer
42 int64u lo;
43 int64s hi;
44
45 protected:
46 // Some global operator functions must be friends
47 friend bool operator < (const int128 &, const int128 &) throw ();
48 friend bool operator == (const int128 &, const int128 &) throw ();
49 friend bool operator || (const int128 &, const int128 &) throw ();
50 friend bool operator && (const int128 &, const int128 &) throw ();
51
52 public:
53 // Constructors
54 inline int128 () throw () : lo(0), hi(0) {};
55 inline int128 (const int128 & a) throw () : lo (a.lo), hi (a.hi) {};
56
57 inline int128 (const unsigned int & a) throw () : lo (a), hi (0ll) {};
58 inline int128 (const signed int & a) throw () : lo (a), hi (0ll) {
59 if (a < 0) this->hi = -1ll;
60 };
61
62 inline int128 (const int64u & a) throw () : lo (a), hi (0ll) {};
63 inline int128 (const int64s & a) throw () : lo (a), hi (0ll) {
64 if (a < 0) this->hi = -1ll;
65 };
66
67 int128 (const float a) throw ();
68 int128 (const double & a) throw ();
69 int128 (const long double & a) throw ();
70
71 int128 (const char * sz) throw ();
72
73 // TODO: Consider creation of operator= to eliminate
74 // the need of intermediate objects during assignments.
75
76 private:
77 // Special internal constructors
78 int128 (const int64u & a, const int64s & b) throw ()
79 : lo (a), hi (b) {};
80
81 public:
82 // Operators
83 bool operator ! () const throw ();
84
85 int128 operator - () const throw ();
86 int128 operator ~ () const throw ();
87
88 int128 & operator ++ ();
89 int128 & operator -- ();
90 int128 operator ++ (int);
91 int128 operator -- (int);
92
93 int128 & operator += (const int128 & b) throw ();
94 int128 & operator *= (const int128 & b) throw ();
95
96 int128 & operator >>= (unsigned int n) throw ();
97 int128 & operator <<= (unsigned int n) throw ();
98
99 int128 & operator |= (const int128 & b) throw ();
100 int128 & operator &= (const int128 & b) throw ();
101 int128 & operator ^= (const int128 & b) throw ();
102
103 // Inline simple operators
104 inline const int128 & operator + () const throw () { return *this; };
105
106 // Rest of inline operators
107 inline int128 & operator -= (const int128 & b) throw () {
108 return *this += (-b);
109 };
110 inline int128 & operator /= (const int128 & b) throw () {
111 int128 dummy;
112 *this = this->div (b, dummy);
113 return *this;
114 };
115 inline int128 & operator %= (const int128 & b) throw () {
116 this->div (b, *this);
117 return *this;
118 };
119
120 // Common methods
121 int toInt () const throw () { return (int) this->lo; };
122 int64s toInt64 () const throw () { return (int64s) this->lo; };
123
124 const char * toString (unsigned int radix = 10) const throw ();
125 float toFloat () const throw ();
126 double toDouble () const throw ();
127 long double toLongDouble () const throw ();
128
129 // Arithmetic methods
130 int128 div (const int128 &, int128 &) const throw ();
131
132 // Bit operations
133 bool bit (unsigned int n) const throw ();
134 void bit (unsigned int n, bool val) throw ();
135 }
136 #ifdef __GNUC__
137 __attribute__ ((__aligned__ (16), __packed__))
138 #endif
139 ;
140
141
142 // GLOBAL OPERATORS
143
144 bool operator < (const int128 & a, const int128 & b) throw ();
145 bool operator == (const int128 & a, const int128 & b) throw ();
146 bool operator || (const int128 & a, const int128 & b) throw ();
147 bool operator && (const int128 & a, const int128 & b) throw ();
148
149 // GLOBAL OPERATOR INLINES
150
151 inline int128 operator + (const int128 & a, const int128 & b) throw () {
152 return int128 (a) += b; };
153 inline int128 operator - (const int128 & a, const int128 & b) throw () {
154 return int128 (a) -= b; };
155 inline int128 operator * (const int128 & a, const int128 & b) throw () {
156 return int128 (a) *= b; };
157 inline int128 operator / (const int128 & a, const int128 & b) throw () {
158 return int128 (a) /= b; };
159 inline int128 operator % (const int128 & a, const int128 & b) throw () {
160 return int128 (a) %= b; };
161
162 inline int128 operator >> (const int128 & a, unsigned int n) throw () {
163 return int128 (a) >>= n; };
164 inline int128 operator << (const int128 & a, unsigned int n) throw () {
165 return int128 (a) <<= n; };
166
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
174 inline bool operator > (const int128 & a, const int128 & b) throw () {
175 return b < a; };
176 inline bool operator <= (const int128 & a, const int128 & b) throw () {
177 return !(b < a); };
178 inline bool operator >= (const int128 & a, const int128 & b) throw () {
179 return !(a < b); };
180 inline bool operator != (const int128 & a, const int128 & b) throw () {
181 return !(a == b); };
182
183
184 // MISC
185
186 //typedef int128 __int128;
187
188 typedef int128 int128s;
189 } //NameSpace
190
191 #endif
ZenLib::int128::toDouble
double toDouble() const
ZenLib::operator!=
bool operator!=(const int128 &a, const int128 &b)
Definition: int128s.h:180
ZenLib::int128::operator%=
int128 & operator%=(const int128 &b)
Definition: int128s.h:115
ZenLib::operator*
int128 operator*(const int128 &a, const int128 &b)
Definition: int128s.h:155
ZenLib::operator%
int128 operator%(const int128 &a, const int128 &b)
Definition: int128s.h:159
ZenLib::int128::int128
int128()
Definition: int128s.h:54
ZenLib::int128
Definition: int128s.h:39
Conf.h
ZenLib::int128s
int128 int128s
Definition: int128s.h:181
ZenLib::int128::operator||
friend bool operator||(const int128 &, const int128 &)
ZenLib::int128::int128
int128(const int64u &a)
Definition: int128s.h:62
ZenLib::operator/
int128 operator/(const int128 &a, const int128 &b)
Definition: int128s.h:157
ZenLib::operator>
bool operator>(const int128 &a, const int128 &b)
Definition: int128s.h:174
ZenLib::int128::operator==
friend bool operator==(const int128 &, const int128 &)
ZenLib::int128::div
int128 div(const int128 &, int128 &) const
ZenLib::int128::toLongDouble
long double toLongDouble() const
ZenLib::int128::bit
bool bit(unsigned int n) const
ZenLib::int128::toFloat
float toFloat() const
ZenLib::operator<=
bool operator<=(const int128 &a, const int128 &b)
Definition: int128s.h:176
ZenLib::int128::int128
int128(const int128 &a)
Definition: int128s.h:55
ZenLib::operator&
int128 operator&(const int128 &a, const int128 &b)
Definition: int128s.h:167
ZenLib::operator>>
int128 operator>>(const int128 &a, unsigned int n)
Definition: int128s.h:162
ZenLib::int128::operator&&
friend bool operator&&(const int128 &, const int128 &)
ZenLib::operator|
int128 operator|(const int128 &a, const int128 &b)
Definition: int128s.h:169
ZenLib::int128::operator-
int128 operator-() const
ZenLib::int128::int128
int128(const int64s &a)
Definition: int128s.h:63
ZenLib::int128::operator-=
int128 & operator-=(const int128 &b)
Definition: int128s.h:107
ZenLib::int128::toString
const char * toString(unsigned int radix=10) const
ZenLib::int128::toInt
int toInt() const
Definition: int128s.h:121
ZenLib::int128::operator!
bool operator!() const
ZenLib::operator^
int128 operator^(const int128 &a, const int128 &b)
Definition: int128s.h:171
ZenLib::int128::int128
int128(const signed int &a)
Definition: int128s.h:58
ZenLib::operator<<
int128 operator<<(const int128 &a, unsigned int n)
Definition: int128s.h:164
ZenLib::int128::operator+
const int128 & operator+() const
Definition: int128s.h:104
ZenLib::int128::operator/=
int128 & operator/=(const int128 &b)
Definition: int128s.h:110
ZenLib::operator>=
bool operator>=(const int128 &a, const int128 &b)
Definition: int128s.h:178
ZenLib::int128::toInt64
int64s toInt64() const
Definition: int128s.h:122
ZenLib::int128::int128
int128(const unsigned int &a)
Definition: int128s.h:57
ZenLib::int128::operator<
friend bool operator<(const int128 &, const int128 &)