-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathint128.hpp
930 lines (758 loc) · 18.6 KB
/
int128.hpp
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/* int128.hpp - a small 128 bit datatype consisting of one 64 bit integer
Author: Robert "burner" Schadek [email protected] License: LGPL 3 or higher
*/
#pragma once
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-conversion"
//#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <ostream>
#include <cstdint>
#include <limits>
#include <math.h>
// Assignment and Assignment-Conversion operators
// Infix addition
#define BITS62_0 0x7FFFFFFFFFFFFFFFLL
#define BIT_52 0x0010000000000000LL
#define BITS51_0 0x000FFFFFFFFFFFFFLL
#define HIGHBIT 0x8000000000000000LL
#define ALLBITS 0xFFFFFFFFFFFFFFFFLL
#define SIGBITS_HI 0x7FFFFFFF00000000LL
#define HI_WORD 0xFFFFFFFF00000000LL
#define LO_WORD 0x00000000FFFFFFFFLL
struct int128;
int128 operator * ( const int128 & lhs, const int128 & rhs );
int128 operator * ( const int128 & lhs, uint64_t rhs );
int128 operator * ( const int128 & lhs, int32_t rhs );
int128 operator + ( const int128 & lhs, const int128 & rhs );
int128 operator + ( const int128 & lhs, uint64_t rhs );
int128 operator + ( const int128 & lhs, int32_t rhs );
int128 operator - ( const int128 & lhs, const int128 & rhs );
int128 operator - ( const int128 & lhs, uint64_t rhs );
int128 operator - ( const int128 & lhs, int32_t rhs );
int128 operator / ( const int128 & x, const int128 & d );
int128 operator / ( const int128 & x, uint64_t d );
int128 operator / ( const int128 & x, int32_t d );
int128 operator % ( const int128 & x, const int128 & d );
int128 operator % ( const int128 & x, uint64_t d );
int128 operator % ( const int128 & x, int64_t d );
int128 operator % ( const int128 & x, int32_t d );
int128 operator % ( const int128 & x, uint32_t d );
int128 operator += ( int128 & lhs, const int128 & rhs ) ;
int128 operator += ( int128 & lhs, const int64_t & rhs ) ;
int128 operator -= ( int128 & lhs, const int128 & rhs ) ;
int128 operator -= ( int128 & lhs, const int64_t & rhs ) ;
int128 operator *= ( int128 & lhs, const int128 & rhs ) ;
int128 operator *= ( int128 & lhs, const int64_t & rhs ) ;
int128 operator /= ( int128 & lhs, const int128 & rhs ) ;
int128 operator /= ( int128 & lhs, const int64_t & rhs ) ;
int operator < ( const int128 & lhs, const int128 & rhs );
inline int operator < ( const int128 & lhs, int32_t rhs );
inline int operator <= ( const int128 & lhs, const int128 & rhs );
inline int operator <= ( const int128 & lhs, int32_t rhs );
inline int operator == ( const int128 & lhs, const int128 & rhs );
inline int operator == ( const int128 & lhs, int32_t rhs );
inline int operator != ( const int128 & lhs, const int128 & rhs );
inline int operator != ( const int128 & lhs, int32_t rhs );
inline int operator > ( const int128 & lhs, const int128 & rhs );
inline int operator > ( const int128 & lhs, int32_t rhs );
inline int operator >= ( const int128 & lhs, const int128 & rhs );
struct int128 {
int64_t high;
uint64_t low;
inline int128(): high(0), low(0) { }
inline int128(int64_t h, uint64_t l) : high(h), low(l) {}
inline int128(uint8_t x): high(0), low(x) { }
inline int128(uint16_t x): high(0), low(x) { }
inline int128(uint32_t x): high(0), low(x) { }
inline int128(uint64_t x): high(0), low(x) { }
//inline int128(const int128& x): high(x.high), low(x.low) { }
inline int128(const std::string& str) : high(0), low(0) {
int128 t;
for(const auto& c : str) {
t = t * static_cast<uint64_t>(10);
t = t + static_cast<int32_t>(c + '0');
}
this->low = t.low;
this->high = t.high;
}
template<typename T>
void set(T t,
typename std::enable_if<std::is_integral<T>::value >::type* = 0)
{
this->low = t;
this->high = 0;
}
template<typename T>
int128 operator=(T t) {
this->set(t);
return *this;
}
explicit inline operator unsigned char() const {
return this->low;
}
explicit inline operator char() const {
return this->low | (this->high & HIGHBIT);
}
explicit inline operator unsigned short() const {
return this->low;
}
explicit inline operator short() const {
return this->low | (this->high & HIGHBIT);
}
explicit inline operator unsigned int() const {
return this->low;
}
explicit inline operator int() const {
int32_t result;
result = (low & LO_WORD) | (high & HIGHBIT);
return result;
}
explicit inline operator uint64_t() const {
return low;
}
explicit inline operator int64_t() const {
int64_t result;
result = (low & BITS62_0) | (high & HIGHBIT);
return result;
}
explicit inline operator double () const {
double result;
if(high >= 0) {
result = static_cast<double>(high) * (static_cast<double>(18446744073709551616.0)) +
static_cast<double>(low);
} else {
int64_t h;
uint64_t l;
h = high; l = low;
h = ~h;
l = ~l;
l += 1;
if(l == 0) {
h += 1;
}
result = -static_cast<double>(h) * (static_cast<double>(18446744073709551616.0)) +
(static_cast<double>(l));
}
return result;
}
inline int128& operator++() {
*this += 1;
return *this;
}
inline int128& operator--() {
*this -= 1;
return *this;
}
inline int128(int8_t x) {
this->low = x;
if(x < 0) {
this->high = -1;
} else {
this->high = 0;
}
}
inline int128(int16_t x) {
this->low = x;
if(x < 0) {
this->high = -1;
} else {
this->high = 0;
}
}
inline int128(int32_t x) {
this->low = x;
if(x < 0) {
this->high = -1;
} else {
this->high = 0;
}
}
/*explicit int128(long long int x) {
this->low = x;
if(x < 0) {
this->high = -1;
} else {
this->high = 0;
}
}*/
inline int128(int64_t x) {
this->low = x;
if(x < 0) {
this->high = -1;
} else {
this->high = 0;
}
}
union DoubleUint64 {
double d;
uint64_t i;
};
inline int128(double x) {
uint64_t t, m, h, l;
if(x < -1.7014118346046e38) {
// overflow negative
this->high = HIGHBIT;
this->low = 0;
} else if(x < -9.2233720368547e18) {
// take the 54 mantissa bits and shift into position
//DoubleUint64 di;
//di.d = x;
//t = *(reinterpret_cast<uint64_t *>(&x));
auto t2 = (reinterpret_cast<uint64_t *>(&x));
t = *t2;
m = (t & BITS51_0) | BIT_52;
t = (t & BITS62_0) >> 52;
//t = *(reinterpret_cast<uint64_t*>(reinterpret_cast<char*>(&x)));
//t = di.i;
// if x is 1.5 * 2^1, t will be 1024
// if x is 1.5 * 2^52, t will be 1024+51 = 1075
t = t - 1075;
if(t > 64) {
l = 0;
h = m << (t-64);
} else {
l = m << t;
h = m >> (64 - t);
}
this->low = ~l;
this->high = ~h;
} else if(x < 9.2233720368547e18) {
// it will fit in a uint64_t
this->low = static_cast<uint64_t>(x);
this->high = ((x<0) ? -1 : 0);
} else if(x < 1.7014118346046e38) {
// take the 54 mantissa bits and shift into position
//t = *(reinterpret_cast<uint64_t *>(&x));
auto t2 = (reinterpret_cast<uint64_t *>(&x));
t = *t2;
//DoubleUint64 di;
//di.d = x;
//t = *(reinterpret_cast<uint64_t*>(reinterpret_cast<char*>(&x)));
//t = di.i;
m = (t & BITS51_0) | BIT_52;
t = (t & BITS62_0) >> 52;
// if x is 1.5 * 2^1, t will be 1024
// if x is 1.5 * 2^52, t will be 1024+51 = 1075
t = t - 1075;
if(t > 64) {
this->low = 0;
this->high = m << (t-64);
} else {
this->low = m << t;
this->high = m >> (64 - t);
}
} else {
// overflow positive
this->high = BITS62_0;
this->low = ALLBITS;
}
}
inline size_t digits() const {
size_t l(log10(this->low));
size_t h(log10(std::abs(this->high)));
return l+h;
}
template<typename T>
inline int128 pow(const T& t) const {
bool pos(true);
int128 r(*this);
int128 ti(t);
if(r < 0) {
r = r * -1;
pos = false;
}
while(ti > 1) {
r *= *this;
--ti;
}
if(!pos) {
return r * -1;
} else {
return r;
}
}
};
inline int operator < ( const int128 & lhs, const int128 & rhs ) {
if(lhs.high < rhs.high)
return 1;
if(rhs.high < lhs.high)
return 0;
// high components are equal
if(lhs.low < rhs.low)
return 1;
return 0;
}
inline int operator < ( const int128 & lhs, int32_t rhs ) {
int128 r(rhs);
if(lhs.high < r.high)
return 1;
if(r.high < lhs.high)
return 0;
// high components are equal
if(lhs.low < r.low)
return 1;
return 0;
}
inline int operator <= ( const int128 & lhs, const int128 & rhs ) {
if((lhs.high == rhs.high) && (lhs.low == rhs.low))
return 1;
if(lhs.high < rhs.high)
return 1;
if(rhs.high < lhs.high)
return 0;
// high components are equal
if(lhs.low < rhs.low)
return 1;
return 0;
}
inline int operator <= ( const int128 & lhs, int32_t rhs ) {
int128 t(rhs);
if((lhs.high == t.high) && (lhs.low == t.low))
return 1;
if(lhs.high < t.high)
return 1;
if(t.high < lhs.high)
return 0;
// high components are equal
if(lhs.low < t.low)
return 1;
return 0;
}
inline int operator == ( const int128 & lhs, const int128 & rhs ) {
if(lhs.high != rhs.high)
return 0;
if(lhs.low != rhs.low)
return 0;
return 1;
}
inline int operator == ( const int128 & lhs, int32_t rhs ) {
int128 t(rhs);
return ((lhs.high == t.high) && (lhs.low == t.low));
}
inline int operator != ( const int128 & lhs, const int128 & rhs ) {
if(lhs.high != rhs.high)
return 1;
if(lhs.low != rhs.low)
return 1;
return 0;
}
inline int operator != ( const int128 & lhs, int32_t rhs ) {
int128 t(rhs);
return ( (lhs.high != t.high) || (lhs.low != t.low) );
}
inline int operator > ( const int128 & lhs, const int128 & rhs ) {
if(lhs.high > rhs.high)
return 1;
if(rhs.high > lhs.high)
return 0;
// high components are equal
if(lhs.low > rhs.low)
return 1;
return 0;
}
inline int operator > ( const int128 & lhs, int32_t rhs ) {
int128 t(rhs);
if(lhs.high > t.high)
return 1;
if(t.high > lhs.high)
return 0;
// high components are equal
if(lhs.low > t.low)
return 1;
return 0;
}
inline int operator >= ( const int128 & lhs, const int128 & rhs ) {
if((lhs.high == rhs.high) && (lhs.low == rhs.low))
return 1;
if(lhs.high > rhs.high)
return 1;
if(rhs.high > lhs.high)
return 0;
// high components are equal
if(lhs.low > rhs.low)
return 1;
return 0;
}
inline int128 operator + ( const int128 & lhs, const int128 & rhs ) {
int128 res;
res.high = lhs.high + rhs.high;
res.low = lhs.low + rhs.low;
if(res.low < rhs.low) {
(res.high)++;
}
return res;
}
inline int128 operator + ( const int128 & lhs, int64_t rhs ) {
int128 res;
if(rhs < 0) {
res.high = lhs.high - 1;
res.low = lhs.low + rhs;
if(res.low < rhs) {
(res.high)++;
}
} else {
res.high = lhs.high;
res.low = lhs.low + rhs;
if(res.low < rhs) {
(res.high)++;
}
}
return res;
}
inline int128 operator + ( const int128 & lhs, uint64_t rhs ) {
int128 res;
res.high = lhs.high;
res.low = lhs.low + rhs;
if(res.low < rhs) {
(res.high)++;
}
return res;
}
inline int128 operator + ( const int128 & lhs, int32_t rhs ) {
int128 res;
if(rhs < 0) {
res.high = lhs.high - 1;
res.low = lhs.low + rhs;
if(res.low < rhs) {
(res.high)++;
}
} else {
res.high = lhs.high;
res.low = lhs.low + rhs;
if(res.low < rhs) {
(res.high)++;
}
}
return res;
}
inline int128 operator + ( int64_t lhs, const int128 & rhs ) {
int128 res;
int128 a;
a.low = lhs;
a.high = (lhs < 0) ? -1 : 0;
res.high = a.high + rhs.high;
res.low = a.low + rhs.low;
if(res.low < rhs.low) {
(res.high)++;
}
return res;
}
// Infix subtraction
inline int128 operator - ( const int128 & lhs, const int128 & rhs ) {
int128 res;
res.high = lhs.high - rhs.high;
res.low = lhs.low - rhs.low;
if(res.low > lhs.low) {
// borrow
(res.high)--;
}
return res;
}
inline int128 operator - ( const int128 & lhs, int32_t rhs ) {
int128 res;
int128 b;
b.low = rhs;
b.high = (rhs < 0) ? -1 : 0;
res.high = lhs.high - b.high;
res.low = lhs.low - b.low;
if(res.low > lhs.low) {
(res.high)--;
}
return res;
}
// Unary minus
inline int128 operator - ( const int128 & x ) {
int128 res;
res.high = ~(x.high);
res.low = ~(x.low);
res.low += 1;
if(res.low == 0) {
res.high += 1;
}
return res;
}
// Support routines for multiply, divide and modulo
static int128 int128_shr(int128 x) {
int128 rv;
// printf("%.16llX %016llX >> ", x.high, x.low);
rv.low = (x.low >> 1) | (x.high << 63);
rv.high = x.high >> 1;
// printf("%.16llX %016llX\n", rv.high, rv.low);
return rv;
}
static int128 int128_shl(int128 x) {
int128 rv;
rv.high = (x.high << 1) | (x.low >> 63);
rv.low = x.low << 1;
return rv;
}
// Multiplication
static int128 mult1(int128 xi, int128 yi) {
int128 rv2;
uint64_t acc, ac2, carry, o1, o2;
uint64_t a, b, c, d, e, f, g, h, ah, bh, ch, dh, bg, cg, dg, cf, df, de;
/* x a b c d
y e f g h
---------------
ah bh ch dh
bg cg dg
cf df
de
--------------------------
-o2-- -o1--
*/
d = xi.low & LO_WORD;
c = (xi.low & HI_WORD) >> 32LL;
b = xi.high & LO_WORD;
a = (xi.high & HI_WORD) >> 32LL;
h = yi.low & LO_WORD;
g = (yi.low & HI_WORD) >> 32LL;
f = yi.high & LO_WORD;
e = (yi.high & HI_WORD) >> 32LL;
acc = d * h;
o1 = acc & LO_WORD;
acc >>= 32LL;
carry = 0;
ac2 = acc + c * h; if(ac2 < acc) { carry++; }
acc = ac2 + d * g; if(acc < ac2) { carry++; }
rv2.low = o1 | (acc << 32LL);
ac2 = (acc >> 32LL) | (carry << 32LL); carry = 0;
acc = ac2 + b * h; if(acc < ac2) { carry++; }
ac2 = acc + c * g; if(ac2 < acc) { carry++; }
acc = ac2 + d * f; if(acc < ac2) { carry++; }
o2 = acc & LO_WORD;
ac2 = (acc >> 32LL) | (carry << 32LL);
acc = ac2 + a * h;
ac2 = acc + b * g;
acc = ac2 + c * f;
ac2 = acc + d * e;
rv2.high = (ac2 << 32LL) | o2;
return rv2;
}
inline int128 operator * ( const int128 & lhs, const int128 & rhs ) {
int128 rv;
rv = mult1(lhs, rhs);
return rv;
}
inline int128 operator * ( const int128 & lhs, uint64_t rhs ) {
int128 t;
int128 rv;
t = rhs;
rv = mult1(lhs, int128(rhs));
return rv;
}
inline int128 operator * ( const int128 & lhs, int32_t rhs ) {
int128 t;
int128 rv;
t = rhs;
rv = mult1(lhs, int128(rhs));
return rv;
}
// Division
static int128 div1(int128 x, int128 d, int128 *r) {
int s;
int128 d1, p2, rv;
//printf("divide %.16llX %016llX / %.16llX %016llX\n", x.high, x.low, d.high, d.low);
/* check for divide by zero */
if((d.low == 0) && (d.high == 0)) {
rv.low = x.low / d.low; /* This will cause runtime error */
}
s = 1;
if(x < 0) {
// notice that MININT will be unchanged, this is used below.
s = - s;
x = - x;
}
if(d < 0) {
s = - s;
d = - d;
}
if(d == 1) {
/* This includes the overflow case MININT/-1 */
rv = x;
x = 0;
//} else if(x < ((int128) d)) {
} else if(x < d) {
/* x < d, so quotient is 0 and x is remainder */
rv = 0;
} else {
rv = 0;
/* calculate biggest power of 2 times d that's <= x */
p2 = 1; d1 = d;
x = x - d1;
while(x >= d1) {
x = x - d1;
d1 = d1 + d1;
p2 = p2 + p2;
}
x = x + d1;
while(p2 != 0) {
//printf("x %.16llX %016llX d1 %.16llX %016llX\n", x.high, x.low, d1.high, d1.low);
if(x >= d1) {
x = x - d1;
rv = rv + p2;
//printf("`.. %.16llX %016llX\n", rv.high, rv.low);
}
p2 = int128_shr(p2);
d1 = int128_shr(d1);
}
/* whatever is left in x is the remainder */
}
/* Put sign in result */
if(s < 0) {
rv = - rv;
}
/* return remainder if they asked for it */
if(r) {
*r = x;
}
return rv;
}
inline int128 operator / ( const int128 & x, const int128 & d ) {
int128 rv;
rv = div1(x, d, 0);
return rv;
}
inline int128 operator / ( const int128 & x, uint64_t d ) {
int128 t;
int128 rv;
t.high = 0; t.low = d;
rv = div1(x, t, 0);
return rv;
}
// Modulo operator
inline int128 operator % ( const int128 & x, const int128 & d) {
int128 rv;
//rv = rv - (rv / ( d * d));
rv = x - (d * (x / d));
return rv;
}
inline int128 operator % ( const int128 & x, uint64_t d ) {
return x % int128(d);
}
inline int128 operator % ( const int128 & x, int32_t d ) {
return x % int128(d);
}
inline int128 operator % ( const int128 & x, int64_t d ) {
return x % int128(d);
}
inline int128 operator % ( const int128 & x, uint32_t d ) {
return x % int128(d);
}
// Comparison operators
// Conversion for output
static uint64_t int128_u64(int128 x) {
return(x.low);
}
static uint32_t int128_uint32_t(int128 x) {
uint32_t rv;
rv = x.low;
return(rv);
}
static void int128_str(int128 x, char *s) {
int d, nd, going;
int128 t, p10;
//if(x < ((int128) 0)) {
if(x < 0) {
*s = '-'; s++;
x = - x;
}
//if(x == ((int128) 0)) {
if(x == 0) {
*s = '0'; s++;
*s = 0; return;
}
// Count number of digits in x
p10 = 1; nd = 0; going = 1;
while((p10 <= x) && (going)) {
//p10 = p10 + p10; if(p10 < ((int128) 0)) { going = 0; }
p10 = p10 + p10; if(p10 < 0) { going = 0; }
t = p10;
//p10 = p10 + p10; if(p10 < ((int128) 0)) { going = 0; }
//p10 = p10 + p10; if(p10 < ((int128) 0)) { going = 0; }
//p10 = p10 + t; if(p10 < ((int128) 0)) { going = 0; }
p10 = p10 + p10; if(p10 < 0) { going = 0; }
p10 = p10 + p10; if(p10 < 0) { going = 0; }
p10 = p10 + t; if(p10 < 0) { going = 0; }
nd++;
}
// Extract each digit
while(nd > 0) {
int i;
nd--;
p10 = 1;
for(i=0; i<nd; i++) {
p10 = p10 + p10;
t = p10;
p10 = p10 + p10;
p10 = p10 + p10;
p10 = p10 + t;
}
d = 0;
while(x >= p10) {
x = x - p10;
d++;
}
*s=('0' + d); s++;
}
*s=0;
}
inline std::ostream& operator<<(std::ostream& os, int128 i) {
char out[128] = {'\0'};
int128_str(i, out);
os<<out;
return os;
}
inline int128 operator += ( int128 & lhs, const int128 & rhs ) {
lhs = lhs + rhs;
return lhs;
}
inline int128 operator += ( int128 & lhs, const int64_t & rhs ) {
lhs = lhs + rhs;
return lhs;
}
inline int128 operator -= ( int128 & lhs, const int128 & rhs ) {
lhs = lhs - rhs;
return lhs;
}
inline int128 operator -= ( int128 & lhs, const int64_t & rhs ) {
lhs = lhs - int128(rhs);
return lhs;
}
inline int128 operator *= ( int128 & lhs, const int128 & rhs ) {
lhs = lhs * rhs;
return lhs;
}
inline int128 operator *= ( int128 & lhs, const int64_t & rhs ) {
lhs = lhs * int128(rhs);
return lhs;
}
inline int128 operator /= ( int128 & lhs, const int128 & rhs ) {
lhs = lhs / rhs;
return lhs;
}
inline int128 operator /= ( int128 & lhs, const int64_t & rhs ) {
lhs = lhs / int128(rhs);
return lhs;
}
template<typename T>
struct is_sweet_int128 : std::false_type {};
template<>
struct is_sweet_int128<int128> : std::true_type {};
namespace std {
template<> // define your specialization
class numeric_limits<int128> {
public:
static unsigned long long max() {
return std::numeric_limits<uint64_t>::max();
}
static long long min() {
return std::numeric_limits<int64_t>::max();
}
};
}