-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
uint128_t.hpp
1107 lines (1005 loc) · 34.6 KB
/
uint128_t.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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
*
* @details Implementation of 128-bit unsigned integers.
* @note The implementation can be flagged as not completed. This header is used
* with enough operations as a part of bigger integer types 256-bit integer.
* @author [Ashish Daulatabad](https://github.com/AshishYUO)
*/
#include <algorithm> /// for `std::reverse` and other operations
#include <cstdint>
#include <ostream> /// for `std::cout` overload
#include <string> /// for `std::string`
#include <utility> /// for `std::pair` library
#ifdef _MSC_VER
#include <intrin.h> /// for _BitScanForward64 and __BitScanReverse64 operation
#endif
#ifndef CIPHERS_UINT128_T_HPP_
#define CIPHERS_UINT128_T_HPP_
class uint128_t;
template <>
struct std::is_integral<uint128_t> : std::true_type {};
template <>
struct std::is_arithmetic<uint128_t> : std::true_type {};
template <>
struct std::is_unsigned<uint128_t> : std::true_type {};
/**
* @brief Adding two string
* @details Adds two long integer, only used for printing numbers
* @param first First integer string
* @param second Second integer string
* @returns string denoting the addition of both the strings
*/
std::string add(const std::string &first, const std::string &second) {
std::string third;
int16_t sum = 0, carry = 0;
for (int32_t i = static_cast<int32_t>(first.size()) - 1,
j = static_cast<int32_t>(second.size()) - 1;
i >= 0 || j >= 0; --i, --j) {
sum = ((i >= 0 ? first[i] - '0' : 0) + (j >= 0 ? second[j] - '0' : 0) +
carry);
carry = sum / 10;
sum %= 10;
third.push_back(sum + '0');
}
if (carry) {
third.push_back('1');
}
std::reverse(third.begin(), third.end());
return third;
}
/**
* @class uint128_t
* @brief class for 128-bit unsigned integer
*/
class uint128_t {
uint64_t f{}, s{}; /// First and second half of 128 bit number
/**
* @brief Get integer from given string.
* @details Create an integer from a given string
* @param str integer string, can be hexadecimal (starting on 0x... or
* number)
* @returns void
*/
void __get_integer_from_string(const std::string &str) {
this->f = this->s = 0;
if (str.size() > 1 && str[1] == 'x') { // if hexadecimal
for (auto i = 2; i < str.size(); ++i) {
*this *= 16LL;
if (str[i] >= '0' && str[i] <= '9') {
*this += (str[i] - '0');
} else if (str[i] >= 'A' && str[i] <= 'F') {
*this += (str[i] - 'A' + 10);
} else if (str[i] >= 'a' && str[i] <= 'f') {
*this += (str[i] - 'a' + 10);
}
}
} else { // if decimal
for (auto &x : str) {
*this *= 10LL;
*this += (x - '0');
}
}
}
public:
uint128_t() = default;
/**
* @brief Parameterized constructor
* @tparam T integral type
* @param low lower part 8-bit unisgned integer
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
explicit uint128_t(T low) : s(low) {}
/**
* @brief Parameterized constructor
* @param str Integer string (hexadecimal starting with 0x.. or decimal)
*/
explicit uint128_t(const std::string &str) {
__get_integer_from_string(str);
}
/**
* @brief Parameterized constructor
* @param high higher part 64-bit unsigned integer
* @param low lower part 64-bit unsigned integer
*/
uint128_t(const uint64_t high, const uint64_t low) : f(high), s(low) {}
/**
* @brief Copy constructor
* @param num 128-bit unsigned integer
*/
uint128_t(const uint128_t &num) = default;
/**
* @brief Move constructor
* @param num 128-bit unsigned integer
*/
uint128_t(uint128_t &&num) noexcept : f(num.f), s(num.s) {}
/**
* @brief Destructor for uint128_t
*/
~uint128_t() = default;
/**
* @brief Leading zeroes in binary
* @details Calculates leading zeros in 128-bit integer
* @returns Integer denoting leading zeroes
*/
inline uint32_t _lez() {
#ifndef _MSC_VER
if (f) {
return __builtin_clzll(f);
}
return 64 + __builtin_clzll(s);
#else
unsigned long r = 0;
_BitScanForward64(&r, f);
if (r == 64) {
unsigned long l = 0;
_BitScanForward64(&l, s);
return 64 + l;
}
return r;
#endif
}
/**
* @brief Trailing zeroes in binary
* @details Calculates leading zeros in 128-bit integer
* @returns Integer denoting Trailing zeroes
*/
inline uint32_t _trz() {
#ifndef _MSC_VER
if (f) {
return __builtin_ctzll(f);
}
return 64 + __builtin_ctzll(s);
#else
unsigned long r = 0;
_BitScanReverse64(&r, s);
if (r == 64) {
unsigned long l = 0;
_BitScanReverse64(&l, f);
return 64 + l;
}
return r;
#endif
}
/**
* @brief casting operator to boolean value
* @returns true if value of this is non-zero, else false
*/
inline explicit operator bool() const { return (f || s); }
/**
* @brief casting operator to any integer valu
* @tparam T any integer type
* @returns integer value casted to mentioned type
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline explicit operator T() const {
return static_cast<T>(s);
}
/**
* @brief returns lower 64-bit integer part
* @returns returns lower 64-bit integer part
*/
inline uint64_t lower() const { return s; }
/**
* @brief returns upper 64-bit integer part
* @returns returns upper 64-bit integer part
*/
inline uint64_t upper() const { return f; }
/**
* @brief operator = for other types
* @tparam T denoting any integer type
* @param p an integer to assign it's value
* @returns this pointer with it's value equal to `p`
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator=(const T &p) {
this->s = p;
return *this;
}
/**
* @brief operator = for type string
* @param p a string to assign it's value to equivalent integer
* @returns this pointer with it's value equal to `p`
*/
inline uint128_t &operator=(const std::string &p) {
this->__get_integer_from_string(p);
return *this;
}
/**
* @brief operator = for uint128_t
* @param p an 128-bit integer to assign it's value
* @returns this pointer with it's value equal to `p`
*/
inline uint128_t &operator=(const uint128_t &p) = default;
/**
* @brief Move assignment operator
*/
inline uint128_t &operator=(uint128_t &&p) = default;
/**
* @brief operator + for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns addition of this and p, returning uint128_t integer
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator+(const T p) {
return uint128_t(f + (p + s < s), p + s);
}
/**
* @brief operator + for uint128_t and other integer types.
* @param p 128-bit unsigned integer
* @returns addition of this and p, returning uint128_t integer
*/
inline uint128_t operator+(const uint128_t &p) {
return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
}
/**
* @brief operator += for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns addition of this and p, returning this
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator+=(const T p) {
bool app = p + s < s;
this->f += app;
this->s += p;
return *this;
}
/**
* @brief operator += for uint128_t
* @param p 128-bit unsigned integer
* @returns addition of this and p, returning this
*/
uint128_t &operator+=(const uint128_t &p) {
bool app = p.s + s < s;
f = f + app + p.f;
s = p.s + s;
return *this;
}
/**
* @brief pre-increment operator
* @returns incremented value of this.
*/
inline uint128_t &operator++() {
*this += 1;
return *this;
}
/**
* @brief post-increment operator
* @returns incremented value of this.
*/
inline uint128_t operator++(int) {
++*this;
return *this;
}
/**
* @brief operator - for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns subtraction of this and p, returning uint128_t integer
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator-(const T &p) {
bool app = p > s;
return uint128_t(f - app, s - p);
}
/**
* @brief operator - for uint128_t
* @param p a type of integer variable
* @returns subtraction of this and p, returning uint128_t integer
*/
inline uint128_t operator-(const uint128_t &p) {
bool app = p.s > s;
return uint128_t(f - p.f - app, s - p.s);
}
/**
* @brief operator - using twos complement
* @returns 2's complement of this.
*/
inline uint128_t operator-() { return ~*this + uint128_t(1); }
/**
* @brief operator -- (pre-decrement)
* @returns decremented value of this
*/
inline uint128_t &operator--() {
*this -= 1;
return *this;
}
/**
* @brief operator -- (post-decrement)
* @returns decremented value of this
*/
inline uint128_t operator--(int p) {
--*this;
return *this;
}
/**
* @brief operator -= for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns subtraction of this and p, returning this
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t &operator-=(const T &p) {
bool app = p > s;
f -= app;
s -= p;
return *this;
}
/**
* @brief operator -= for uint128_t
* @param p 128-bit unsigned integer
* @returns subtraction of this and p, returning this
*/
uint128_t &operator-=(const uint128_t &p) {
bool app = p.s > s;
f = f - p.f - app;
s = s - p.s;
return *this;
}
/**
* @brief operator * for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns multiplication of this and p, returning uint128_t integer
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator*(const T p) {
return *this * uint128_t(p);
}
/**
* @brief operator * for uint128_t and other integer types.
* @param p 128-bit unsigned integer
* @returns multiplication of this and p, returning uint128_t integer
*/
uint128_t operator*(const uint128_t &p) {
uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
uint64_t fi = f_first * s_first, se = f_first * s_second,
th = s_first * f_second, fo = s_second * f_second;
uint64_t tmp = ((se & 0xFFFFFFFF) << 32), tmp2 = (th & 0xFFFFFFFF)
<< 32;
int cc = (tmp + tmp2 < tmp);
tmp += tmp2;
cc += (tmp + fo < tmp);
uint64_t carry = fi + (se >> 32) + (th >> 32);
return uint128_t(this->f * p.s + this->s * p.f + carry + cc, tmp + fo);
}
/**
* @brief operator *= for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns multiplication of this and p, returning this
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator*=(const T p) {
*this *= uint128_t(p);
return *this;
}
/**
* @brief operator *= for uint128_t and other integer types.
* @param p 128-bit unsigned integer
* @returns multiplication of this and p, returning this
*/
uint128_t &operator*=(const uint128_t &p) {
uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
uint64_t fi = f_first * s_first, se = f_first * s_second,
th = s_first * f_second, fo = s_second * f_second;
uint64_t tmp = (se << 32), tmp2 = (th << 32);
int cc = (tmp + tmp2 < tmp);
tmp += tmp2;
cc += (tmp + fo < tmp);
uint64_t carry = fi + (se >> 32) + (th >> 32);
f = this->f * p.s + this->s * p.f + carry + cc;
s = tmp + fo;
return *this;
}
/**
* @brief divide function for uint128_t and other integer types.
* @details divide this value and
* @param p 128-bit unsigned integer
* @returns pair denoting quotient and remainder.
*/
std::pair<uint128_t, uint128_t> divide(const uint128_t &p) {
if (*this < p) { // if this is less than divisor
return {uint128_t(0), *this};
} else if (*this == p) { // if this is equal to divisor
return {uint128_t(1), uint128_t(0)};
}
uint128_t tmp = p, tmp2 = *this;
uint16_t left = tmp._lez() - _lez();
tmp <<= left;
uint128_t quotient(0);
uint128_t zero(0);
while (tmp2 >= p) {
uint16_t shf = tmp2._lez() - tmp._lez();
if (shf) {
tmp >>= shf;
quotient <<= shf;
left -= shf;
}
if (tmp2 < tmp) {
tmp >>= 1;
quotient <<= 1;
--left;
}
tmp2 -= tmp;
++quotient;
}
return {quotient << left, tmp2};
}
/**
* @brief operator / for uint128_t and other integer types.
* @param p 128-bit unsigned integer
* @returns unsigned 128-bit quotient.
*/
inline uint128_t operator/(const uint128_t &p) { return divide(p).first; }
/**
* @brief operator / for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns unsigned 128-bit quotient.
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator/(const T p) {
uint128_t tmp = *this;
tmp /= uint128_t(0, p);
return tmp;
}
/**
* @brief operator /= for uint128_t
* @param p 128-bit unsigned integer
* @returns this set as unsigned 128-bit quotient.
*/
inline uint128_t &operator/=(const uint128_t &p) {
*this = divide(p).first;
return *this;
}
/**
* @brief operator /= for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns this set as unsigned 128-bit quotient.
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator/=(const T p) {
*this /= uint128_t(0, p);
return *this;
}
/**
* @brief operator % for uint128_t
* @param p 128-bit unsigned integer
* @returns unsigned 128-bit remainder.
*/
inline uint128_t operator%(const uint128_t &p) { return divide(p).second; }
/**
* @brief operator % for uint128_t and other integer types.
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns unsigned 128-bit remainder.
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator%(const T &p) {
return *this % uint128_t(p);
}
/**
* @brief operator %= for uint128_t
* @param p 128-bit unsigned integer
* @returns this set as unsigned 128-bit remainder.
*/
inline uint128_t &operator%=(const uint128_t &p) {
*this = divide(p).second;
return *this;
}
/**
* @brief operator %= for uint128_t
* @tparam T denoting integral type
* @param p a type of integer variable
* @returns this set as unsigned 128-bit remainder.
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator%=(const T &p) {
*this %= uint128_t(p);
return *this;
}
/**
* @brief operator < for uint128_t
* @param other number to be compared with this
* @returns true if this is less than other, else false
*/
inline bool operator<(const uint128_t &other) {
return f < other.f || (f == other.f && s < other.s);
}
/**
* @brief operator <= for uint128_t
* @param other number to be compared with this
* @returns true if this is less than or equal to other, else false
*/
inline bool operator<=(const uint128_t &other) {
return f < other.f || (f == other.f && s <= other.s);
}
/**
* @brief operator > for uint128_t
* @param other number to be compared with this
* @returns true if this is greater than other, else false
*/
inline bool operator>(const uint128_t &other) {
return f > other.f || (f == other.f && s > other.s);
}
/**
* @brief operator >= for uint128_t
* @param other number to be compared with this
* @returns true if this is greater than or equal than other, else false
*/
inline bool operator>=(const uint128_t &other) {
return (f > other.f) || (f == other.f && s >= other.s);
}
/**
* @brief operator == for uint128_t
* @param other number to be compared with this
* @returns true if this is equal than other, else false
*/
inline bool operator==(const uint128_t &other) {
return f == other.f && s == other.s;
}
/**
* @brief operator != for uint128_t
* @param other number to be compared with this
* @returns true if this is not equal than other, else false
*/
inline bool operator!=(const uint128_t &other) {
return f != other.f || s != other.s;
}
/**
* @brief operator ! for uint128_t
* @returns true if this has zero value, else false
*/
inline bool operator!() { return !f && !s; }
/**
* @brief operator && for uint128_t
* @param b number to be compared with this
* @returns true if both of the values are not zero, else false
*/
inline bool operator&&(const uint128_t &b) {
return (s || f) && (b.s || b.f);
}
/**
* @brief operator || for uint128_t
* @param b number to be compared with this
* @returns true if one of the values are not zero, else false
*/
inline bool operator||(const uint128_t &b) {
return (s || f) || (b.s || b.f);
}
/**
* @brief operator () for uint128_t
* @returns true if this value is non-zero, else false
*/
inline bool operator()() { return s || f; }
/**
* @brief operator < for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is less than other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator<(const T other) {
return *this < uint128_t(other);
}
/**
* @brief operator <= for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is less than or equal to other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator<=(const T other) {
return *this <= uint128_t(other);
}
/**
* @brief operator > for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is greater than other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator>(const T other) {
return *this > uint128_t(other);
}
/**
* @brief operator >= for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is greater than or equal other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator>=(const T other) {
return *this >= uint128_t(other);
}
/**
* @brief operator == for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is equal to other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator==(const T other) {
return *this == uint128_t(other);
}
/**
* @brief operator != for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is not equal to other, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator!=(const T other) {
return *this != uint128_t(other);
}
/**
* @brief operator && for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is both values are non-zero, else false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator&&(const T b) {
return (f || s) && b;
}
/**
* @brief operator || for other types
* @tparam T integral type
* @param other number to be compared with this
* @returns true if this is either one of the values are non-zero, else
* false
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline bool operator||(const T b) {
return (f || s) || b;
}
/**
* @brief operator ~ for uint128_t
* @returns 1's complement of this number
*/
uint128_t operator~() { return uint128_t(~this->f, ~this->s); }
/**
* @brief operator << for uint128_t
* @tparam T integral type
* @param p number denoting number of shifts
* @returns value of this shifted by p to left
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t operator<<(const T p) {
if (!p) {
return uint128_t(f, s);
} else if (p >= 64 && p <= 128) {
return uint128_t((this->s << (p - 64)), 0);
} else if (p < 64 && p > 0) {
return uint128_t((this->f << p) + ((this->s >> (64 - p))),
this->s << p);
}
return uint128_t(0);
}
/**
* @brief operator <<= for uint128_t
* @tparam T integral type
* @param p number denoting number of shifts
* @returns this shifted by p to left
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t &operator<<=(const T p) {
if (p) {
if (p >= 64 && p <= 128) {
this->f = (this->s << (p - 64));
this->s = 0;
} else {
f = ((this->f << p) + (this->s >> (64 - p)));
s = (this->s << p);
}
}
return *this;
}
/**
* @brief operator >> for uint128_t
* @tparam T integral type
* @param p number denoting number of shifts
* @returns value of this shifted by p to right
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t operator>>(const T p) {
if (!p) {
return uint128_t(this->f, this->s);
} else if (p >= 64 && p <= 128) {
return uint128_t(0, (this->f >> (p - 64)));
} else if (p < 64 && p > 0) {
return uint128_t((this->f >> p),
(this->s >> p) + (this->f << (64 - p)));
}
return uint128_t(0);
}
/**
* @brief operator >>= for uint128_t
* @tparam T integral type
* @param p number denoting number of shifts
* @returns this shifted by p to right
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t &operator>>=(const T p) {
if (p) {
if (p >= 64) {
f = 0;
s = (this->f >> (p - 64));
} else {
s = (this->s >> p) + (this->f << (64 - p));
f = (this->f >> p);
}
}
return *this;
}
/**
* @brief operator & for uint128_t (bitwise operator)
* @param p number to be operated
* @returns value of this & p (& is bit-wise operator)
*/
inline uint128_t operator&(const uint128_t &p) {
return uint128_t(this->f & p.f, this->s & p.s);
}
/**
* @brief operator & for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns value of this & p (& is bit-wise operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t operator&(const T p) {
uint128_t tmp = *this;
return tmp & uint128_t(p);
}
/**
* @brief operator &= for uint128_t (bitwise operator)
* @param p number to be operated
* @returns this = this & p (& is bit-wise operator)
*/
uint128_t &operator&=(const uint128_t &p) {
this->f &= p.f;
this->s &= p.s;
return *this;
}
/**
* @brief operator &= for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns this = this & p (& is bit-wise operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
uint128_t &operator&=(const T p) {
*this &= uint128_t(p);
return *this;
}
/**
* @brief operator | for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns value of this | p (| is bit-wise operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator|(const T p) {
return uint128_t(p | s);
}
/**
* @brief operator | for uint128_t (bitwise operator)
* @param p number to be operated
* @returns value of this | p (| is bit-wise OR operator)
*/
inline uint128_t operator|(const uint128_t &p) {
return uint128_t(this->f | p.f, this->s | p.s);
}
/**
* @brief operator |= for uint128_t (bitwise operator)
* @param p number to be operated
* @returns this = this | p (| is bit-wise OR operator)
*/
uint128_t &operator|=(const uint128_t &p) {
f |= p.f;
s |= p.s;
return *this;
}
/**
* @brief operator |= for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns this = this | p (| is bit-wise OR operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator|=(const T p) {
s |= p.s;
return *this;
}
/**
* @brief operator ^ for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns value of this ^ p (^ is bit-wise XOR operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t operator^(const T p) {
return uint128_t(this->f, this->s ^ p);
}
/**
* @brief operator ^ for uint128_t (bitwise operator)
* @param p number to be operated
* @returns value of this ^ p (^ is bit-wise XOR operator)
*/
inline uint128_t operator^(const uint128_t &p) {
return uint128_t(this->f ^ p.f, this->s ^ p.s);
}
/**
* @brief operator ^= for uint128_t (bitwise operator)
* @param p number to be operated
* @returns this = this ^ p (^ is bit-wise XOR operator)
*/
uint128_t &operator^=(const uint128_t &p) {
f ^= p.f;
s ^= p.s;
return *this;
}
/**
* @brief operator ^= for other types (bitwise operator)
* @tparam T integral type
* @param p number to be operated
* @returns this = this ^ p (^ is bit-wise XOR operator)
*/
template <typename T, typename = typename std::enable_if<
std::is_integral<T>::value, T>::type>
inline uint128_t &operator^=(const T &p) {
s ^= p;
return *this;
}
/**
* @brief operator << for printing uint128_t integer
* @details Prints the uint128_t integer in decimal form
* @note Note that this operator is costly since it uses strings to print
* the value
* @param op ostream object
* @param p 128-bit integer
* @returns op, ostream object.
*/
friend std::ostream &operator<<(std::ostream &op, const uint128_t &p) {
if (!p.f) {
op << p.s;
} else {
std::string out = "0", p_2 = "1";
for (int i = 0; i < 64; ++i) {
if (p.s & (1LL << i)) {
out = add(out, p_2);
}
p_2 = add(p_2, p_2);
}
for (int i = 0; i < 64; ++i) {
if (p.f & (1LL << i)) {
out = add(out, p_2);
}
p_2 = add(p_2, p_2);
}