-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuinteger_t.hh
2546 lines (2152 loc) · 69.6 KB
/
uinteger_t.hh
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
/*
uinteger_t.hh
An arbitrary precision unsigned integer type for C++
Copyright (c) 2017 German Mendez Bravo (Kronuz) @ german dot mb at gmail.com
Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com
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.
With much help from Auston Sterling
Thanks to Stefan Deigmüller for finding
a bug in operator*.
Thanks to François Dessenne for convincing me
to do a general rewrite of this class.
Germán Mández Bravo (Kronuz) converted Jason Lee's uint128_t
to header-only and extended to arbitrary bit length.
*/
#ifndef __uint_t__
#define __uint_t__
#include <vector>
#include <string>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdint>
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <functional>
#include <type_traits>
#define ASSERT assert
// Compatibility inlines
#ifndef __has_builtin // Optional of course
#define __has_builtin(x) 0 // Compatibility with non-clang compilers
#endif
#if defined _MSC_VER
# define HAVE___ADDCARRY_U64
# define HAVE___SUBBORROW_U64
# define HAVE___ADDCARRY_U32
# define HAVE___SUBBORROW_U32
# define HAVE___ADDCARRY_U16
# define HAVE___SUBBORROW_U16
# define HAVE___UMUL128
# define HAVE___UMUL64
# define HAVE___UMUL32
# include <intrin.h>
#endif
#if (defined(__clang__) && __has_builtin(__builtin_clzll)) || (defined(__GNUC__ ) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)))
# define HAVE____BUILTIN_CLZLL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_clzl)) || (defined(__GNUC__ ) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)))
# define HAVE____BUILTIN_CLZL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_clz)) || (defined(__GNUC__ ) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)))
# define HAVE____BUILTIN_CLZ
#endif
#if (defined(__clang__) && __has_builtin(__builtin_addcll))
# define HAVE____BUILTIN_ADDCLL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_addcl))
# define HAVE____BUILTIN_ADDCL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_addc))
# define HAVE____BUILTIN_ADDC
#endif
#if (defined(__clang__) && __has_builtin(__builtin_subcll))
# define HAVE____BUILTIN_SUBCLL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_subcl))
# define HAVE____BUILTIN_SUBCL
#endif
#if (defined(__clang__) && __has_builtin(__builtin_subc))
# define HAVE____BUILTIN_SUBC
#endif
#if defined __SIZEOF_INT128__
#define HAVE____INT128_T
#endif
#ifndef DIGIT_T
#define DIGIT_T std::uint64_t
#endif
#ifndef HALF_DIGIT_T
#define HALF_DIGIT_T std::uint32_t
#endif
class uinteger_t;
namespace std { // This is probably not a good idea
// Give uinteger_t type traits
template <> struct is_arithmetic <uinteger_t> : std::true_type {};
template <> struct is_integral <uinteger_t> : std::true_type {};
template <> struct is_unsigned <uinteger_t> : std::true_type {};
}
class uinteger_t {
public:
using digit = DIGIT_T;
using half_digit = HALF_DIGIT_T;
static constexpr std::size_t digit_octets = sizeof(digit); // number of octets per digit
static constexpr std::size_t digit_bits = digit_octets * 8; // number of bits per digit
static constexpr std::size_t half_digit_octets = sizeof(half_digit); // number of octets per half_digit
static constexpr std::size_t half_digit_bits = half_digit_octets * 8; // number of bits per half_digit
using container = std::vector<digit>;
template <typename T>
struct is_result {
static const bool value = false;
};
template <typename T, typename Alloc>
struct is_result<std::vector<T, Alloc>> {
static const bool value = true;
};
template <typename charT, typename traits, typename Alloc>
struct is_result<std::basic_string<charT, traits, Alloc>> {
static const bool value = true;
};
private:
static_assert(digit_octets == half_digit_octets * 2, "half_digit must be exactly half the size of digit");
static constexpr std::size_t karatsuba_cutoff = 1024 / digit_bits;
static constexpr double growth_factor = 1.5;
std::size_t _begin;
std::size_t _end;
container _value_instance;
container& _value;
bool _carry;
public:
// Window to vector (uses _begin and _end)
void reserve(std::size_t sz) {
_value.reserve(sz + _begin);
}
std::size_t grow(std::size_t n) {
// expands the vector using a growth factor
// and returns the new capacity.
auto cc = _value.capacity();
if (n >= cc) {
cc = n * growth_factor;
_value.reserve(cc);
}
return cc;
}
void resize(std::size_t sz) {
grow(sz + _begin);
_value.resize(sz + _begin);
}
void resize(std::size_t sz, const digit& c) {
grow(sz + _begin);
_value.resize(sz + _begin, c);
}
void clear() {
_value.clear();
_begin = 0;
_end = 0;
_carry = false;
}
digit* data() noexcept {
return _value.data() + _begin;
}
const digit* data() const noexcept {
return _value.data() + _begin;
}
std::size_t size() const noexcept {
return _end ? _end - _begin : _value.size() - _begin;
}
void prepend(std::size_t sz, const digit& c) {
// Efficiently prepend by growing backwards by growth factor
auto min = std::min(_begin, sz);
if (min) {
// If there is some space before `_begin`, we try using it first:
_begin -= min;
std::fill_n(_value.begin() + _begin, min, c);
sz -= min;
}
if (sz) {
ASSERT(_begin == 0); // _begin should be 0 in here
// If there's still more room needed, we grow the vector:
// Ex.: grow using prepend(3, y)
// sz = 3
// _begin = 0 (B)
// _end = 1 (E)
// initially (capacity == 12):
// |xxxxxxxxxx- |
// B E
// after reclaiming space after `_end` (same capacity == 12):
// |xxxxxxxxxx |
// B
// _end = 0
// csz = 10
// grow returns the new capacity (22)
// isz = 12 (22 - 10)
// _begin = 9 (12 - 3)
// after (capacity == (12 + 3) * 1.5 == 22):
// |---------yyyxxxxxxxxxx|
// B
if (_end) {
// reclaim space after `_end`
_value.resize(_end);
_end = 0;
}
auto csz = _value.size();
auto isz = grow(csz + sz) - csz;
_value.insert(_value.begin(), isz, c);
_begin = isz - sz;
}
}
void prepend(const digit& c) {
prepend(1, c);
}
void prepend(const uinteger_t& num) {
prepend(num.size(), 0);
std::copy(num.begin(), num.end(), begin());
}
void append(std::size_t sz, const digit& c) {
// Efficiently append by growing by growth factor
if (_end) {
// reclaim space after `_end`
_value.resize(_end);
_end = 0;
}
auto nsz = _value.size() + sz;
grow(nsz);
_value.resize(nsz, c);
}
void append(const digit& c) {
append(1, c);
}
void append(const uinteger_t& num) {
auto sz = num.size();
append(sz, 0);
std::copy(num.begin(), num.end(), end() - sz);
}
container::iterator begin() noexcept {
return _value.begin() + _begin;
}
container::const_iterator begin() const noexcept {
return _value.cbegin() + _begin;
}
container::iterator end() noexcept {
return _end ? _value.begin() + _end : _value.end();
}
container::const_iterator end() const noexcept {
return _end ? _value.cbegin() + _end : _value.cend();
}
container::reverse_iterator rbegin() noexcept {
return _end ? container::reverse_iterator(_value.begin() + _end) : _value.rbegin();
}
container::const_reverse_iterator rbegin() const noexcept {
return _end ? container::const_reverse_iterator(_value.cbegin() + _end) : _value.crbegin();
}
container::reverse_iterator rend() noexcept {
return container::reverse_iterator(_value.begin() + _begin);
}
container::const_reverse_iterator rend() const noexcept {
return container::const_reverse_iterator(_value.cbegin() + _begin);
}
container::reference front() {
return *begin();
}
container::const_reference front() const {
return *begin();
}
container::reference back() {
return *rbegin();
}
container::const_reference back() const {
return *rbegin();
}
private:
// Optimized primitives for operations
static digit _bits(digit x) {
#if defined HAVE____BUILTIN_CLZLL
if (digit_octets == sizeof(unsigned long long)) {
return x ? digit_bits - __builtin_clzll(x) : 1;
}
#endif
#if defined HAVE____BUILTIN_CLZL
if (digit_octets == sizeof(unsigned long)) {
return x ? digit_bits - __builtin_clzl(x) : 1;
}
#endif
#if defined HAVE____BUILTIN_CLZ
if (digit_octets == sizeof(unsigned)) {
return x ? digit_bits - __builtin_clz(x) : 1;
}
#endif
{
digit c = x ? 0 : 1;
while (x) {
x >>= 1;
++c;
}
return c;
}
}
static digit _mult(digit x, digit y, digit* lo) {
#if defined HAVE___UMUL128
if (digit_bits == 64) {
digit h;
digit l = _umul128(x, y, &h); // _umul128(x, y, *hi) -> lo
return h;
}
#endif
#if defined HAVE___UMUL64
if (digit_bits == 32) {
digit h;
digit l = _umul64(x, y, &h); // _umul64(x, y, *hi) -> lo
return h;
}
#endif
#if defined HAVE___UMUL32
if (digit_bits == 16) {
digit h;
digit l = _umul32(x, y, &h); // _umul32(x, y, *hi) -> lo
return h;
}
#endif
#if defined HAVE____INT128_T
if (digit_bits == 64) {
auto r = static_cast<__uint128_t>(x) * static_cast<__uint128_t>(y);
*lo = r;
return r >> digit_bits;
}
#endif
if (digit_bits == 64) {
digit x0 = x & 0xffffffffUL;
digit x1 = x >> 32;
digit y0 = y & 0xffffffffUL;
digit y1 = y >> 32;
digit u = (x0 * y0);
digit v = (x1 * y0) + (u >> 32);
digit w = (x0 * y1) + (v & 0xffffffffUL);
*lo = (w << 32) + (u & 0xffffffffUL); // low
return (x1 * y1) + (v >> 32) + (w >> 32); // high
} if (digit_bits == 32) {
auto r = static_cast<std::uint64_t>(x) * static_cast<std::uint64_t>(y);
*lo = r;
return r >> 32;
} if (digit_bits == 16) {
auto r = static_cast<std::uint32_t>(x) * static_cast<std::uint32_t>(y);
*lo = r;
return r >> 16;
} if (digit_bits == 8) {
auto r = static_cast<std::uint16_t>(x) * static_cast<std::uint16_t>(y);
*lo = r;
return r >> 8;
}
}
static digit _multadd(digit x, digit y, digit a, digit c, digit* lo) {
#if defined HAVE___UMUL128 && defined HAVE___ADDCARRY_U64
if (digit_bits == 64) {
digit h;
digit l = _umul128(x, y, &h); // _umul128(x, y, *hi) -> lo
return h + _addcarry_u64(c, l, a, lo); // _addcarry_u64(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___UMUL64 && defined HAVE___ADDCARRY_U32
if (digit_bits == 32) {
digit h;
digit l = _umul64(x, y, &h); // _umul64(x, y, *hi) -> lo
return h + _addcarry_u32(c, l, a, lo); // _addcarry_u32(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___UMUL32 && defined HAVE___ADDCARRY_U16
if (digit_bits == 16) {
digit h;
digit l = _umul32(x, y, &h); // _umul32(x, y, *hi) -> lo
return h + _addcarry_u16(c, l, a, lo); // _addcarry_u16(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE____INT128_T
if (digit_bits == 64) {
auto r = static_cast<__uint128_t>(x) * static_cast<__uint128_t>(y) + static_cast<__uint128_t>(a) + static_cast<__uint128_t>(c);
*lo = r;
return r >> digit_bits;
}
#endif
if (digit_bits == 64) {
digit x0 = x & 0xffffffffUL;
digit x1 = x >> 32;
digit y0 = y & 0xffffffffUL;
digit y1 = y >> 32;
digit u = (x0 * y0) + (a & 0xffffffffUL) + (c & 0xffffffffUL);
digit v = (x1 * y0) + (u >> 32) + (a >> 32) + (c >> 32);
digit w = (x0 * y1) + (v & 0xffffffffUL);
*lo = (w << 32) + (u & 0xffffffffUL); // low
return (x1 * y1) + (v >> 32) + (w >> 32); // high
}
if (digit_bits == 32) {
auto r = static_cast<std::uint64_t>(x) * static_cast<std::uint64_t>(y) + static_cast<std::uint64_t>(a) + static_cast<std::uint64_t>(c);
*lo = r;
return r >> 32;
}
if (digit_bits == 16) {
auto r = static_cast<std::uint32_t>(x) * static_cast<std::uint32_t>(y) + static_cast<std::uint32_t>(a) + static_cast<std::uint32_t>(c);
*lo = r;
return r >> 16;
}
if (digit_bits == 8) {
auto r = static_cast<std::uint16_t>(x) * static_cast<std::uint16_t>(y) + static_cast<std::uint16_t>(a) + static_cast<std::uint16_t>(c);
*lo = r;
return r >> 8;
}
}
static digit _divmod(digit x_hi, digit x_lo, digit y, digit* result) {
#if defined HAVE____INT128_T
if (digit_bits == 64) {
auto x = static_cast<__uint128_t>(x_hi) << digit_bits | static_cast<__uint128_t>(x_lo);
digit q = x / y;
digit r = x % y;
*result = q;
return r;
}
#endif
if (digit_bits == 64) {
// quotient
digit q = x_lo << 1;
// remainder
digit r = x_hi;
digit carry = x_lo >> 63;
int i;
for (i = 0; i < 64; i++) {
auto tmp = r >> 63;
r <<= 1;
r |= carry;
carry = tmp;
if (carry == 0) {
if (r >= y) {
carry = 1;
} else {
tmp = q >> 63;
q <<= 1;
q |= carry;
carry = tmp;
continue;
}
}
r -= y;
r -= (1 - carry);
carry = 1;
tmp = q >> 63;
q <<= 1;
q |= carry;
carry = tmp;
}
*result = q;
return r;
}
if (digit_bits == 32) {
auto x = static_cast<std::uint64_t>(x_hi) << 32 | static_cast<std::uint64_t>(x_lo);
digit q = x / y;
digit r = x % y;
*result = q;
return r;
}
if (digit_bits == 16) {
auto x = static_cast<std::uint32_t>(x_hi) << 16 | static_cast<std::uint32_t>(x_lo);
digit q = x / y;
digit r = x % y;
*result = q;
return r;
}
if (digit_bits == 8) {
auto x = static_cast<std::uint16_t>(x_hi) << 8 | static_cast<std::uint16_t>(x_lo);
digit q = x / y;
digit r = x % y;
*result = q;
return r;
}
}
static digit _addcarry(digit x, digit y, digit c, digit* result) {
#if defined HAVE___ADDCARRY_U64
if (digit_bits == 64) {
return _addcarry_u64(c, x, y, result); // _addcarry_u64(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___ADDCARRY_U32
if (digit_bits == 32) {
return _addcarry_u32(c, x, y, result); // _addcarry_u32(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___ADDCARRY_U16
if (digit_bits == 16) {
return _addcarry_u16(c, x, y, result); // _addcarry_u16(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE____BUILTIN_ADDCLL
if (digit_octets == sizeof(unsigned long long)) {
unsigned long long carryout;
*result = __builtin_addcll(x, y, c, &carryout); // __builtin_addcll(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____BUILTIN_ADDCL
if (digit_octets == sizeof(unsigned long)) {
unsigned long carryout;
*result = __builtin_addcl(x, y, c, &carryout); // __builtin_addcl(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____BUILTIN_ADDC
if (digit_octets == sizeof(unsigned)) {
unsigned carryout;
*result = __builtin_addc(x, y, c, &carryout); // __builtin_addc(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____INT128_T
if (digit_bits == 64) {
auto r = static_cast<__uint128_t>(x) + static_cast<__uint128_t>(y) + static_cast<__uint128_t>(c);
*result = r;
return static_cast<bool>(r >> digit_bits);
}
#endif
if (digit_bits == 64) {
digit x0 = x & 0xffffffffUL;
digit x1 = x >> 32;
digit y0 = y & 0xffffffffUL;
digit y1 = y >> 32;
auto u = x0 + y0 + c;
auto v = x1 + y1 + static_cast<bool>(u >> 32);
*result = (v << 32) + (u & 0xffffffffUL);
return static_cast<bool>(v >> 32);
}
if (digit_bits == 32) {
auto r = static_cast<std::uint64_t>(x) + static_cast<std::uint64_t>(y) + static_cast<std::uint64_t>(c);
*result = r;
return static_cast<bool>(r >> 32);
}
if (digit_bits == 16) {
auto r = static_cast<std::uint32_t>(x) + static_cast<std::uint32_t>(y) + static_cast<std::uint32_t>(c);
*result = r;
return static_cast<bool>(r >> 16);
}
if (digit_bits == 8) {
auto r = static_cast<std::uint16_t>(x) + static_cast<std::uint16_t>(y) + static_cast<std::uint16_t>(c);
*result = r;
return static_cast<bool>(r >> 8);
}
}
static digit _subborrow(digit x, digit y, digit c, digit* result) {
#if defined HAVE___SUBBORROW_U64
if (digit_bits == 64) {
return _subborrow_u64(c, x, y, result); // _subborrow_u64(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___SUBBORROW_U32
if (digit_bits == 64) {
return _subborrow_u32(c, x, y, result); // _subborrow_u32(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE___SUBBORROW_U16
if (digit_bits == 64) {
return _subborrow_u16(c, x, y, result); // _subborrow_u16(carryin, x, y, *sum) -> carryout
}
#endif
#if defined HAVE____BUILTIN_SUBCLL
if (digit_octets == sizeof(unsigned long long)) {
unsigned long long carryout;
*result = __builtin_subcll(x, y, c, &carryout); // __builtin_subcll(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____BUILTIN_SUBCL
if (digit_octets == sizeof(unsigned long)) {
unsigned long carryout;
*result = __builtin_subcl(x, y, c, &carryout); // __builtin_subcl(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____BUILTIN_SUBC
if (digit_octets == sizeof(unsigned)) {
unsigned carryout;
*result = __builtin_subc(x, y, c, &carryout); // __builtin_subc(x, y, carryin, *carryout) -> sum
return carryout;
}
#endif
#if defined HAVE____INT128_T
if (digit_bits == 64) {
auto r = static_cast<__uint128_t>(x) - static_cast<__uint128_t>(y) - static_cast<__uint128_t>(c);
*result = r;
return static_cast<bool>(r >> 64);
}
#endif
if (digit_bits == 64) {
digit x0 = x & 0xffffffffUL;
digit x1 = x >> 32;
digit y0 = y & 0xffffffffUL;
digit y1 = y >> 32;
auto u = x0 - y0 - c;
auto v = x1 - y1 - static_cast<bool>(u >> 32);
*result = (v << 32) + (u & 0xffffffffUL);
return static_cast<bool>(v >> 32);
}
if (digit_bits == 32) {
auto r = static_cast<std::uint64_t>(x) - static_cast<std::uint64_t>(y) - static_cast<std::uint64_t>(c);
*result = r;
return static_cast<bool>(r >> 32);
}
if (digit_bits == 16) {
auto r = static_cast<std::uint32_t>(x) - static_cast<std::uint32_t>(y) - static_cast<std::uint32_t>(c);
*result = r;
return static_cast<bool>(r >> 16);
}
if (digit_bits == 8) {
auto r = static_cast<std::uint16_t>(x) - static_cast<std::uint16_t>(y) - static_cast<std::uint16_t>(c);
*result = r;
return static_cast<bool>(r >> 8);
}
}
// Helper functions
void trim(digit mask = 0) {
auto rit = rbegin();
auto rit_e = rend();
// Masks the last value of internal vector
mask &= (digit_bits - 1);
if (mask && rit != rit_e) {
*rit &= (static_cast<digit>(1) << mask) - 1;
}
// Removes all unused zeros from the internal vector
auto rit_f = std::find_if(rit, rit_e, [](const digit& c) { return c; });
resize(rit_e - rit_f); // shrink
}
static constexpr char chr(int ord) {
constexpr const char _[256] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
};
return _[ord];
}
static constexpr int ord(int chr) {
constexpr const int _[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
return _[chr];
}
public:
static constexpr unsigned base_bits(int base) {
constexpr const unsigned _[256] = {
0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8,
};
return _[base - 1];
}
static constexpr unsigned base_size(int base) {
constexpr const unsigned _[256] = {
0, 64, 41, 32, 28, 25, 23, 22, 21, 20, 19, 18, 18, 17, 17, 16,
16, 16, 16, 15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8,
};
return _[base - 1];
}
static const uinteger_t uint_0() {
static uinteger_t uint_0(0);
return uint_0;
}
static const uinteger_t uint_1() {
static uinteger_t uint_1(1);
return uint_1;
}
private:
// Public Implementation
#ifdef UINT_T_PUBLIC_IMPLEMENTATION
public:
#endif
static uinteger_t& bitwise_and(uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
if (lhs_sz > rhs_sz) {
lhs.resize(rhs_sz); // shrink
}
auto lhs_it = lhs.begin();
auto lhs_it_e = lhs.end();
auto rhs_it = rhs.begin();
for (; lhs_it != lhs_it_e; ++lhs_it, ++rhs_it) {
*lhs_it &= *rhs_it;
}
// Finish up
lhs.trim();
return lhs;
}
static uinteger_t& bitwise_and(uinteger_t& result, const uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
auto result_sz = std::max(lhs_sz, rhs_sz);
result.resize(result_sz);
// not using `end()` because resize of `result.resize()` could have
// resized `lhs` or `rhs` if `result` is also either `rhs` or `lhs`.
auto lhs_it = lhs.begin();
auto lhs_it_e = lhs_it + lhs_sz;
auto rhs_it = rhs.begin();
auto rhs_it_e = rhs_it + rhs_sz;
auto it = result.begin();
if (lhs_sz < rhs_sz) {
for (; lhs_it != lhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it & *rhs_it;
}
for (; rhs_it != rhs_it_e; ++rhs_it, ++it) {
*it = 0;
}
} else {
for (; rhs_it != rhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it & *rhs_it;
}
for (; lhs_it != lhs_it_e; ++lhs_it, ++it) {
*it = 0;
}
}
// Finish up
result.trim();
return result;
}
static uinteger_t bitwise_and(const uinteger_t& lhs, const uinteger_t& rhs) {
uinteger_t result;
bitwise_and(result, lhs, rhs);
return result;
}
static uinteger_t& bitwise_or(uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
if (lhs_sz < rhs_sz) {
lhs.resize(rhs_sz, 0); // grow
}
auto lhs_it = lhs.begin();
auto rhs_it = rhs.begin();
auto rhs_it_e = rhs.end();
for (; rhs_it != rhs_it_e; ++lhs_it, ++rhs_it) {
*lhs_it |= *rhs_it;
}
// Finish up
lhs.trim();
return lhs;
}
static uinteger_t& bitwise_or(uinteger_t& result, const uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
auto result_sz = std::max(lhs_sz, rhs_sz);
result.resize(result_sz);
// not using `end()` because resize of `result.resize()` could have
// resized `lhs` or `rhs` if `result` is also either `rhs` or `lhs`.
auto lhs_it = lhs.begin();
auto lhs_it_e = lhs_it + lhs_sz;
auto rhs_it = rhs.begin();
auto rhs_it_e = rhs_it + rhs_sz;
auto it = result.begin();
if (lhs_sz < rhs_sz) {
for (; lhs_it != lhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it | *rhs_it;
}
for (; rhs_it != rhs_it_e; ++rhs_it, ++it) {
*it = *rhs_it;
}
} else {
for (; rhs_it != rhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it | *rhs_it;
}
for (; lhs_it != lhs_it_e; ++lhs_it, ++it) {
*it = *lhs_it;
}
}
// Finish up
result.trim();
return result;
}
static uinteger_t bitwise_or(const uinteger_t& lhs, const uinteger_t& rhs) {
uinteger_t result;
bitwise_or(result, lhs, rhs);
return result;
}
static uinteger_t& bitwise_xor(uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
if (lhs_sz < rhs_sz) {
lhs.resize(rhs_sz, 0); // grow
}
auto lhs_it = lhs.begin();
auto rhs_it = rhs.begin();
auto rhs_it_e = rhs.end();
for (; rhs_it != rhs_it_e; ++lhs_it, ++rhs_it) {
*lhs_it ^= *rhs_it;
}
// Finish up
lhs.trim();
return lhs;
}
static uinteger_t& bitwise_xor(uinteger_t& result, const uinteger_t& lhs, const uinteger_t& rhs) {
auto lhs_sz = lhs.size();
auto rhs_sz = rhs.size();
auto result_sz = std::max(lhs_sz, rhs_sz);
result.resize(result_sz);
// not using `end()` because resize of `result.resize()` could have
// resized `lhs` or `rhs` if `result` is also either `rhs` or `lhs`.
auto lhs_it = lhs.begin();
auto lhs_it_e = lhs_it + lhs_sz;
auto rhs_it = rhs.begin();
auto rhs_it_e = rhs_it + rhs_sz;
auto it = result.begin();
if (lhs_sz < rhs_sz) {
for (; lhs_it != lhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it ^ *rhs_it;
}
for (; rhs_it != rhs_it_e; ++rhs_it, ++it) {
*it = *rhs_it;
}
} else {
for (; rhs_it != rhs_it_e; ++lhs_it, ++rhs_it, ++it) {
*it = *lhs_it ^ *rhs_it;
}
for (; lhs_it != lhs_it_e; ++lhs_it, ++it) {
*it = *lhs_it;
}
}
// Finish up
result.trim();
return result;