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