ZenLib

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

- File List
- File Members

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