forked from eteran/cpp-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint128.h
436 lines (347 loc) · 8.31 KB
/
uint128.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Evan Teran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef UINT128_20050119_H_
#define UINT128_20050119_H_
#include <climits>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include <boost/operators.hpp>
namespace numeric {
namespace detail {
template <typename T>
static void divide(const T &numerator, const T &denominator, T "ient, T &remainder) {
static const int bits = sizeof(T) * CHAR_BIT;
if (denominator == 0) {
throw std::domain_error("divide by zero");
} else {
T n = numerator;
T d = denominator;
T x = 1;
T answer = 0;
while ((n >= d) && (((d >> (bits - 1)) & 1) == 0)) {
x <<= 1;
d <<= 1;
}
while (x != 0) {
if (n >= d) {
n -= d;
answer |= x;
}
x >>= 1;
d >>= 1;
}
quotient = answer;
remainder = n;
}
}
}
// convinience macro
#define U128_C(s) uint128(#s)
class uint128 : public boost::shiftable<uint128, boost::totally_ordered<uint128, boost::integer_arithmetic<uint128, boost::bitwise<uint128, boost::unit_steppable<uint128 > > > > > {
public:
typedef uint64_t base_type;
public:
static const unsigned int size = (sizeof(base_type) + sizeof(base_type)) * CHAR_BIT;
private:
base_type lo;
base_type hi;
public:
// constructors for all basic types
uint128() : lo(0), hi(0) {
}
uint128(int value) : lo(static_cast<base_type>(value)), hi(0) {
if (value < 0)
hi = static_cast<base_type>(-1);
}
uint128(unsigned int value) : lo(static_cast<base_type>(value)), hi(0) {
}
uint128(float value) : lo(static_cast<base_type>(value)), hi(0) {
}
uint128(double value) : lo(static_cast<base_type>(value)), hi(0) {
}
uint128(const uint128 &value) : lo(value.lo), hi(value.hi) {
}
uint128(base_type value) : lo(value), hi(0) {
}
uint128(const std::string &sz) : lo(0), hi(0) {
// do we have at least one character?
if (!sz.empty()) {
// make some reasonable assumptions
int radix = 10;
bool minus = false;
auto i = sz.begin();
// check for minus sign, i suppose technically this should only apply
// to base 10, but who says that -0x1 should be invalid?
if (*i == '-') {
++i;
minus = true;
}
// check if there is radix changing prefix (0 or 0x)
if (i != sz.end()) {
if (*i == '0') {
radix = 8;
++i;
if (i != sz.end()) {
if (*i == 'x') {
radix = 16;
++i;
}
}
}
while (i != sz.end()) {
unsigned int n;
const char ch = *i;
if (ch >= 'A' && ch <= 'Z') {
if (((ch - 'A') + 10) < radix) {
n = (ch - 'A') + 10;
} else {
break;
}
} else if (ch >= 'a' && ch <= 'z') {
if (((ch - 'a') + 10) < radix) {
n = (ch - 'a') + 10;
} else {
break;
}
} else if (ch >= '0' && ch <= '9') {
if ((ch - '0') < radix) {
n = (ch - '0');
} else {
break;
}
} else {
/* completely invalid character */
break;
}
(*this) *= radix;
(*this) += n;
++i;
}
}
// if this was a negative number, do that two's compliment madness :-P
if (minus) {
*this = -*this;
}
}
}
uint128 &operator=(const uint128 &other) {
if (&other != this) {
lo = other.lo;
hi = other.hi;
}
return *this;
}
public: // comparison operators
bool operator==(const uint128 &o) const {
return hi == o.hi && lo == o.lo;
}
bool operator<(const uint128 &o) const {
return (hi == o.hi) ? lo < o.lo : hi < o.hi;
}
public: // unary operators
bool operator!() const {
return !(hi != 0 || lo != 0);
}
uint128 operator-() const {
// standard 2's compliment negation
return ~uint128(*this) + 1;
}
uint128 operator~() const {
uint128 t(*this);
t.lo = ~t.lo;
t.hi = ~t.hi;
return t;
}
uint128 &operator++() {
if (++lo == 0) {
++hi;
}
return *this;
}
uint128 &operator--() {
if (lo-- == 0) {
--hi;
}
return *this;
}
public: // basic math operators
uint128 &operator+=(const uint128 &b) {
const base_type old_lo = lo;
lo += b.lo;
hi += b.hi;
if (lo < old_lo) {
++hi;
}
return *this;
}
uint128 &operator-=(const uint128 &b) {
// it happens to be way easier to write it
// this way instead of make a subtraction algorithm
return *this += -b;
}
uint128 &operator*=(const uint128 &b) {
// check for multiply by 0
// result is always 0 :-P
if (b == 0) {
hi = 0;
lo = 0;
} else if (b != 1) {
// check we aren't multiplying by 1
uint128 a(*this);
uint128 t = b;
lo = 0;
hi = 0;
for (unsigned int i = 0; i < size; ++i) {
if ((t & 1) != 0) {
*this += (a << i);
}
t >>= 1;
}
}
return *this;
}
uint128 &operator|=(const uint128 &b) {
hi |= b.hi;
lo |= b.lo;
return *this;
}
uint128 &operator&=(const uint128 &b) {
hi &= b.hi;
lo &= b.lo;
return *this;
}
uint128 &operator^=(const uint128 &b) {
hi ^= b.hi;
lo ^= b.lo;
return *this;
}
uint128 &operator/=(const uint128 &b) {
uint128 remainder;
detail::divide(*this, b, *this, remainder);
return *this;
}
uint128 &operator%=(const uint128 &b) {
uint128 quotient;
detail::divide(*this, b, quotient, *this);
return *this;
}
uint128 &operator<<=(const uint128 &rhs) {
unsigned int n = rhs.to_integer();
if (n >= size) {
hi = 0;
lo = 0;
} else {
const unsigned int halfsize = size / 2;
if (n >= halfsize) {
n -= halfsize;
hi = lo;
lo = 0;
}
if (n != 0) {
// shift high half
hi <<= n;
const base_type mask(~(base_type(-1) >> n));
// and add them to high half
hi |= (lo & mask) >> (halfsize - n);
// and finally shift also low half
lo <<= n;
}
}
return *this;
}
uint128 &operator>>=(const uint128 &rhs) {
unsigned int n = rhs.to_integer();
if (n >= size) {
hi = 0;
lo = 0;
} else {
const unsigned int halfsize = size / 2;
if (n >= halfsize) {
n -= halfsize;
lo = hi;
hi = 0;
}
if (n != 0) {
// shift low half
lo >>= n;
// get lower N bits of high half
const base_type mask(~(base_type(-1) << n));
// and add them to low qword
lo |= (hi & mask) << (halfsize - n);
// and finally shift also high half
hi >>= n;
}
}
return *this;
}
public:
int to_integer() const {
return static_cast<int>(lo);
}
base_type to_base_type() const {
return lo;
}
std::string to_string(unsigned int radix = 10) const {
if (*this == 0) {
return "0";
}
if (radix < 2 || radix > 37) {
return "(invalid radix)";
}
// at worst it will be size digits (base 2) so make our buffer
// that plus room for null terminator
static char sz[size + 1];
sz[sizeof(sz) - 1] = '\0';
uint128 ii(*this);
int i = size - 1;
while (ii != 0 && i) {
uint128 remainder;
detail::divide(ii, uint128(radix), ii, remainder);
sz[--i] = "0123456789abcdefghijklmnopqrstuvwxyz"[remainder.to_integer()];
}
return &sz[i];
}
};
std::ostream &operator<<(std::ostream &o, const uint128 &n) {
switch (o.flags() & (std::ios_base::hex | std::ios_base::dec | std::ios_base::oct)) {
case std::ios_base::hex:
o << n.to_string(16);
break;
case std::ios_base::dec:
o << n.to_string(10);
break;
case std::ios_base::oct:
o << n.to_string(8);
break;
default:
o << n.to_string();
break;
}
return o;
}
typedef uint128 uint128_t;
}
#endif