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