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