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 #ifdef __cplusplus
32 #include <new> //for size_t
33 #else /* __cplusplus */
34 #include <stddef.h> //for size_t
35 #endif /* __cplusplus */
36 #include "ZenLib/Conf.h"
37
38 namespace ZenLib
39 {
40
41 // CLASS
42
43 class int128 {
44 private:
45 // Binary correct representation of signed 128bit integer
46 int64u lo;
47 int64s hi;
48
49 protected:
50 // Some global operator functions must be friends
51 friend bool operator < (const int128 &, const int128 &) throw ();
52 friend bool operator == (const int128 &, const int128 &) throw ();
53 friend bool operator || (const int128 &, const int128 &) throw ();
54 friend bool operator && (const int128 &, const int128 &) throw ();
55
56 public:
57 // Constructors
58 inline int128 () throw () : lo(0), hi(0) {};
59 inline int128 (const int128 & a) throw () : lo (a.lo), hi (a.hi) {};
60
61 inline int128 (const unsigned int & a) throw () : lo (a), hi (0ll) {};
62 inline int128 (const signed int & a) throw () : lo (a), hi (0ll) {
63 if (a < 0) this->hi = -1ll;
64 };
65
66 inline int128 (const int64u & a) throw () : lo (a), hi (0ll) {};
67 inline int128 (const int64s & a) throw () : lo (a), hi (0ll) {
68 if (a < 0) this->hi = -1ll;
69 };
70
71 int128 (const float a) throw ();
72 int128 (const double & a) throw ();
73 int128 (const long double & a) throw ();
74
75 int128 (const char * sz) throw ();
76
77 // TODO: Consider creation of operator= to eliminate
78 // the need of intermediate objects during assignments.
79
80 private:
81 // Special internal constructors
82 int128 (const int64u & a, const int64s & b) throw ()
83 : lo (a), hi (b) {};
84
85 public:
86 // Operators
87 bool operator ! () const throw ();
88
89 int128 operator - () const throw ();
90 int128 operator ~ () const throw ();
91
92 int128 & operator ++ ();
93 int128 & operator -- ();
94 int128 operator ++ (int);
95 int128 operator -- (int);
96
97 int128 & operator += (const int128 & b) throw ();
98 int128 & operator *= (const int128 & b) throw ();
99
100 int128 & operator >>= (unsigned int n) throw ();
101 int128 & operator <<= (unsigned int n) throw ();
102
103 int128 & operator |= (const int128 & b) throw ();
104 int128 & operator &= (const int128 & b) throw ();
105 int128 & operator ^= (const int128 & b) throw ();
106
107 // Inline simple operators
108 inline const int128 & operator + () const throw () { return *this; };
109
110 // Rest of inline operators
111 inline int128 & operator -= (const int128 & b) throw () {
112 return *this += (-b);
113 };
114 inline int128 & operator /= (const int128 & b) throw () {
115 int128 dummy;
116 *this = this->div (b, dummy);
117 return *this;
118 };
119 inline int128 & operator %= (const int128 & b) throw () {
120 this->div (b, *this);
121 return *this;
122 };
123
124 // Common methods
125 int toInt () const throw () { return (int) this->lo; };
126 int64s toInt64 () const throw () { return (int64s) this->lo; };
127
128 const char * toString (unsigned int radix = 10) const throw ();
129 float toFloat () const throw ();
130 double toDouble () const throw ();
131 long double toLongDouble () const throw ();
132
133 // Arithmetic methods
134 int128 div (const int128 &, int128 &) const throw ();
135
136 // Bit operations
137 bool bit (unsigned int n) const throw ();
138 void bit (unsigned int n, bool val) throw ();
139 }
140 #ifdef __GNUC__
141 __attribute__ ((__aligned__ (16), __packed__))
142 #endif
143 ;
144
145
146 // GLOBAL OPERATORS
147
148 bool operator < (const int128 & a, const int128 & b) throw ();
149 bool operator == (const int128 & a, const int128 & b) throw ();
150 bool operator || (const int128 & a, const int128 & b) throw ();
151 bool operator && (const int128 & a, const int128 & b) throw ();
152
153 // GLOBAL OPERATOR INLINES
154
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 inline int128 operator / (const int128 & a, const int128 & b) throw () {
162 return int128 (a) /= b; };
163 inline int128 operator % (const int128 & a, const int128 & b) throw () {
164 return int128 (a) %= b; };
165
166 inline int128 operator >> (const int128 & a, unsigned int n) throw () {
167 return int128 (a) >>= n; };
168 inline int128 operator << (const int128 & a, unsigned int n) throw () {
169 return int128 (a) <<= n; };
170
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 inline int128 operator ^ (const int128 & a, const int128 & b) throw () {
176 return int128 (a) ^= b; };
177
178 inline bool operator > (const int128 & a, const int128 & b) throw () {
179 return b < a; };
180 inline bool operator <= (const int128 & a, const int128 & b) throw () {
181 return !(b < a); };
182 inline bool operator >= (const int128 & a, const int128 & b) throw () {
183 return !(a < b); };
184 inline bool operator != (const int128 & a, const int128 & b) throw () {
185 return !(a == b); };
186
187
188 // MISC
189
190 //typedef int128 __int128;
191
192 typedef int128 int128s;
193 } //NameSpace
194
195 #endif
ZenLib::int128::toDouble
double toDouble() const
ZenLib::operator!=
bool operator!=(const int128 &a, const int128 &b)
Definition: int128s.h:184
ZenLib::int128::operator%=
int128 & operator%=(const int128 &b)
Definition: int128s.h:119
ZenLib::operator*
int128 operator*(const int128 &a, const int128 &b)
Definition: int128s.h:159
ZenLib::operator%
int128 operator%(const int128 &a, const int128 &b)
Definition: int128s.h:163
ZenLib::int128::int128
int128()
Definition: int128s.h:58
ZenLib::int128
Definition: int128s.h:43
Conf.h
ZenLib::int128s
int128 int128s
Definition: int128s.h:185
ZenLib::int128::operator||
friend bool operator||(const int128 &, const int128 &)
ZenLib::int128::int128
int128(const int64u &a)
Definition: int128s.h:66
ZenLib::operator/
int128 operator/(const int128 &a, const int128 &b)
Definition: int128s.h:161
ZenLib::operator>
bool operator>(const int128 &a, const int128 &b)
Definition: int128s.h:178
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
Definition: BitStream.h:23
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:180
ZenLib::int128::int128
int128(const int128 &a)
Definition: int128s.h:59
ZenLib::operator&
int128 operator&(const int128 &a, const int128 &b)
Definition: int128s.h:171
ZenLib::operator>>
int128 operator>>(const int128 &a, unsigned int n)
Definition: int128s.h:166
ZenLib::int128::operator&&
friend bool operator&&(const int128 &, const int128 &)
ZenLib::operator|
int128 operator|(const int128 &a, const int128 &b)
Definition: int128s.h:173
ZenLib::int128::operator-
int128 operator-() const
ZenLib::int128::int128
int128(const int64s &a)
Definition: int128s.h:67
ZenLib::int128::operator-=
int128 & operator-=(const int128 &b)
Definition: int128s.h:111
ZenLib::int128::toString
const char * toString(unsigned int radix=10) const
ZenLib::int128::toInt
int toInt() const
Definition: int128s.h:125
ZenLib::int128::operator!
bool operator!() const
ZenLib::operator^
int128 operator^(const int128 &a, const int128 &b)
Definition: int128s.h:175
ZenLib::int128::int128
int128(const signed int &a)
Definition: int128s.h:62
ZenLib::operator<<
int128 operator<<(const int128 &a, unsigned int n)
Definition: int128s.h:168
ZenLib::int128::operator+
const int128 & operator+() const
Definition: int128s.h:108
ZenLib::int128::operator/=
int128 & operator/=(const int128 &b)
Definition: int128s.h:114
ZenLib::operator>=
bool operator>=(const int128 &a, const int128 &b)
Definition: int128s.h:182
ZenLib::int128::toInt64
int64s toInt64() const
Definition: int128s.h:126
ZenLib::int128::int128
int128(const unsigned int &a)
Definition: int128s.h:61
ZenLib::int128::operator<
friend bool operator<(const int128 &, const int128 &)