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