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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
|
//===-- A class to manipulate wide integers. --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_UINT_H
#define LLVM_LIBC_SRC___SUPPORT_UINT_H
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/bit.h" // countl_zero
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128, LIBC_TYPES_HAS_INT64
#include "src/__support/math_extras.h" // add_with_carry, sub_with_borrow
#include "src/__support/number_pair.h"
#include <stddef.h> // For size_t
#include <stdint.h>
namespace LIBC_NAMESPACE {
namespace multiword {
// A type trait mapping unsigned integers to their half-width unsigned
// counterparts.
template <typename T> struct half_width;
template <> struct half_width<uint16_t> : cpp::type_identity<uint8_t> {};
template <> struct half_width<uint32_t> : cpp::type_identity<uint16_t> {};
#ifdef LIBC_TYPES_HAS_INT64
template <> struct half_width<uint64_t> : cpp::type_identity<uint32_t> {};
#ifdef LIBC_TYPES_HAS_INT128
template <> struct half_width<__uint128_t> : cpp::type_identity<uint64_t> {};
#endif // LIBC_TYPES_HAS_INT128
#endif // LIBC_TYPES_HAS_INT64
template <typename T> using half_width_t = typename half_width<T>::type;
// An array of two elements that can be used in multiword operations.
template <typename T> struct DoubleWide final : cpp::array<T, 2> {
using UP = cpp::array<T, 2>;
using UP::UP;
LIBC_INLINE constexpr DoubleWide(T lo, T hi) : UP({lo, hi}) {}
};
// Converts an unsigned value into a DoubleWide<half_width_t<T>>.
template <typename T> LIBC_INLINE constexpr auto split(T value) {
static_assert(cpp::is_unsigned_v<T>);
using half_type = half_width_t<T>;
return DoubleWide<half_type>(
half_type(value),
half_type(value >> cpp::numeric_limits<half_type>::digits));
}
// The low part of a DoubleWide value.
template <typename T> LIBC_INLINE constexpr T lo(const DoubleWide<T> &value) {
return value[0];
}
// The high part of a DoubleWide value.
template <typename T> LIBC_INLINE constexpr T hi(const DoubleWide<T> &value) {
return value[1];
}
// The low part of an unsigned value.
template <typename T> LIBC_INLINE constexpr half_width_t<T> lo(T value) {
return lo(split(value));
}
// The high part of an unsigned value.
template <typename T> LIBC_INLINE constexpr half_width_t<T> hi(T value) {
return hi(split(value));
}
// Returns 'a' times 'b' in a DoubleWide<word>. Cannot overflow by construction.
template <typename word>
LIBC_INLINE constexpr DoubleWide<word> mul2(word a, word b) {
if constexpr (cpp::is_same_v<word, uint8_t>) {
return split<uint16_t>(uint16_t(a) * uint16_t(b));
} else if constexpr (cpp::is_same_v<word, uint16_t>) {
return split<uint32_t>(uint32_t(a) * uint32_t(b));
}
#ifdef LIBC_TYPES_HAS_INT64
else if constexpr (cpp::is_same_v<word, uint32_t>) {
return split<uint64_t>(uint64_t(a) * uint64_t(b));
}
#endif
#ifdef LIBC_TYPES_HAS_INT128
else if constexpr (cpp::is_same_v<word, uint64_t>) {
return split<__uint128_t>(__uint128_t(a) * __uint128_t(b));
}
#endif
else {
using half_word = half_width_t<word>;
const auto shiftl = [](word value) -> word {
return value << cpp::numeric_limits<half_word>::digits;
};
const auto shiftr = [](word value) -> word {
return value >> cpp::numeric_limits<half_word>::digits;
};
// Here we do a one digit multiplication where 'a' and 'b' are of type
// word. We split 'a' and 'b' into half words and perform the classic long
// multiplication with 'a' and 'b' being two-digit numbers.
// a a_hi a_lo
// x b => x b_hi b_lo
// ---- -----------
// c result
// We convert 'lo' and 'hi' from 'half_word' to 'word' so multiplication
// doesn't overflow.
const word a_lo = lo(a);
const word b_lo = lo(b);
const word a_hi = hi(a);
const word b_hi = hi(b);
const word step1 = b_lo * a_lo; // no overflow;
const word step2 = b_lo * a_hi; // no overflow;
const word step3 = b_hi * a_lo; // no overflow;
const word step4 = b_hi * a_hi; // no overflow;
word lo_digit = step1;
word hi_digit = step4;
const word no_carry = 0;
word carry;
word _; // unused carry variable.
lo_digit = add_with_carry<word>(lo_digit, shiftl(step2), no_carry, carry);
hi_digit = add_with_carry<word>(hi_digit, shiftr(step2), carry, _);
lo_digit = add_with_carry<word>(lo_digit, shiftl(step3), no_carry, carry);
hi_digit = add_with_carry<word>(hi_digit, shiftr(step3), carry, _);
return DoubleWide<word>(lo_digit, hi_digit);
}
}
// In-place 'dst op= rhs' with operation with carry propagation. Returns carry.
template <typename Function, typename word, size_t N, size_t M>
LIBC_INLINE constexpr word inplace_binop(Function op_with_carry,
cpp::array<word, N> &dst,
const cpp::array<word, M> &rhs) {
static_assert(N >= M);
word carry_out = 0;
for (size_t i = 0; i < N; ++i) {
const bool has_rhs_value = i < M;
const word rhs_value = has_rhs_value ? rhs[i] : 0;
const word carry_in = carry_out;
dst[i] = op_with_carry(dst[i], rhs_value, carry_in, carry_out);
// stop early when rhs is over and no carry is to be propagated.
if (!has_rhs_value && carry_out == 0)
break;
}
return carry_out;
}
// In-place addition. Returns carry.
template <typename word, size_t N, size_t M>
LIBC_INLINE constexpr word add_with_carry(cpp::array<word, N> &dst,
const cpp::array<word, M> &rhs) {
return inplace_binop(LIBC_NAMESPACE::add_with_carry<word>, dst, rhs);
}
// In-place subtraction. Returns borrow.
template <typename word, size_t N, size_t M>
LIBC_INLINE constexpr word sub_with_borrow(cpp::array<word, N> &dst,
const cpp::array<word, M> &rhs) {
return inplace_binop(LIBC_NAMESPACE::sub_with_borrow<word>, dst, rhs);
}
// In-place multiply-add. Returns carry.
// i.e., 'dst += b * c'
template <typename word, size_t N>
LIBC_INLINE constexpr word mul_add_with_carry(cpp::array<word, N> &dst, word b,
word c) {
return add_with_carry(dst, mul2(b, c));
}
// An array of two elements serving as an accumulator during multiword
// computations.
template <typename T> struct Accumulator final : cpp::array<T, 2> {
using UP = cpp::array<T, 2>;
LIBC_INLINE constexpr Accumulator() : UP({0, 0}) {}
LIBC_INLINE constexpr T advance(T carry_in) {
auto result = UP::front();
UP::front() = UP::back();
UP::back() = carry_in;
return result;
}
LIBC_INLINE constexpr T sum() const { return UP::front(); }
LIBC_INLINE constexpr T carry() const { return UP::back(); }
};
// In-place multiplication by a single word. Returns carry.
template <typename word, size_t N>
LIBC_INLINE constexpr word scalar_multiply_with_carry(cpp::array<word, N> &dst,
word x) {
Accumulator<word> acc;
for (auto &val : dst) {
const word carry = mul_add_with_carry(acc, val, x);
val = acc.advance(carry);
}
return acc.carry();
}
// Multiplication of 'lhs' by 'rhs' into 'dst'. Returns carry.
// This function is safe to use for signed numbers.
// https://stackoverflow.com/a/20793834
// https://pages.cs.wisc.edu/%7Emarkhill/cs354/Fall2008/beyond354/int.mult.html
template <typename word, size_t O, size_t M, size_t N>
LIBC_INLINE constexpr word multiply_with_carry(cpp::array<word, O> &dst,
const cpp::array<word, M> &lhs,
const cpp::array<word, N> &rhs) {
static_assert(O >= M + N);
Accumulator<word> acc;
for (size_t i = 0; i < O; ++i) {
const size_t lower_idx = i < N ? 0 : i - N + 1;
const size_t upper_idx = i < M ? i : M - 1;
word carry = 0;
for (size_t j = lower_idx; j <= upper_idx; ++j)
carry += mul_add_with_carry(acc, lhs[j], rhs[i - j]);
dst[i] = acc.advance(carry);
}
return acc.carry();
}
template <typename word, size_t N>
LIBC_INLINE constexpr void quick_mul_hi(cpp::array<word, N> &dst,
const cpp::array<word, N> &lhs,
const cpp::array<word, N> &rhs) {
Accumulator<word> acc;
word carry = 0;
// First round of accumulation for those at N - 1 in the full product.
for (size_t i = 0; i < N; ++i)
carry += mul_add_with_carry(acc, lhs[i], rhs[N - 1 - i]);
for (size_t i = N; i < 2 * N - 1; ++i) {
acc.advance(carry);
carry = 0;
for (size_t j = i - N + 1; j < N; ++j)
carry += mul_add_with_carry(acc, lhs[j], rhs[i - j]);
dst[i - N] = acc.sum();
}
dst.back() = acc.carry();
}
template <typename word, size_t N>
LIBC_INLINE constexpr bool is_negative(cpp::array<word, N> &array) {
using signed_word = cpp::make_signed_t<word>;
return cpp::bit_cast<signed_word>(array.back()) < 0;
}
// An enum for the shift function below.
enum Direction { LEFT, RIGHT };
// A bitwise shift on an array of elements.
// 'offset' must be less than TOTAL_BITS (i.e., sizeof(word) * CHAR_BIT * N)
// otherwise the behavior is undefined.
template <Direction direction, bool is_signed, typename word, size_t N>
LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
size_t offset) {
static_assert(direction == LEFT || direction == RIGHT);
constexpr size_t WORD_BITS = cpp::numeric_limits<word>::digits;
#ifdef LIBC_TYPES_HAS_INT128
constexpr size_t TOTAL_BITS = N * WORD_BITS;
if constexpr (TOTAL_BITS == 128) {
using type = cpp::conditional_t<is_signed, __int128_t, __uint128_t>;
auto tmp = cpp::bit_cast<type>(array);
if constexpr (direction == LEFT)
tmp <<= offset;
else
tmp >>= offset;
return cpp::bit_cast<cpp::array<word, N>>(tmp);
}
#endif
if (LIBC_UNLIKELY(offset == 0))
return array;
const bool is_neg = is_signed && is_negative(array);
constexpr auto at = [](size_t index) -> int {
// reverse iteration when direction == LEFT.
if constexpr (direction == LEFT)
return int(N) - int(index) - 1;
return int(index);
};
const auto safe_get_at = [&](size_t index) -> word {
// return appropriate value when accessing out of bound elements.
const int i = at(index);
if (i < 0)
return 0;
if (i >= int(N))
return is_neg ? -1 : 0;
return array[i];
};
const size_t index_offset = offset / WORD_BITS;
const size_t bit_offset = offset % WORD_BITS;
#ifdef LIBC_COMPILER_IS_CLANG
__builtin_assume(index_offset < N);
#endif
cpp::array<word, N> out = {};
for (size_t index = 0; index < N; ++index) {
const word part1 = safe_get_at(index + index_offset);
const word part2 = safe_get_at(index + index_offset + 1);
word &dst = out[at(index)];
if (bit_offset == 0)
dst = part1; // no crosstalk between parts.
else if constexpr (direction == LEFT)
dst = static_cast<word>((part1 << bit_offset) |
(part2 >> (WORD_BITS - bit_offset)));
else
dst = static_cast<word>((part1 >> bit_offset) |
(part2 << (WORD_BITS - bit_offset)));
}
return out;
}
#define DECLARE_COUNTBIT(NAME, INDEX_EXPR) \
template <typename word, size_t N> \
LIBC_INLINE constexpr int NAME(const cpp::array<word, N> &val) { \
int bit_count = 0; \
for (size_t i = 0; i < N; ++i) { \
const int word_count = cpp::NAME<word>(val[INDEX_EXPR]); \
bit_count += word_count; \
if (word_count != cpp::numeric_limits<word>::digits) \
break; \
} \
return bit_count; \
}
DECLARE_COUNTBIT(countr_zero, i) // iterating forward
DECLARE_COUNTBIT(countr_one, i) // iterating forward
DECLARE_COUNTBIT(countl_zero, N - i - 1) // iterating backward
DECLARE_COUNTBIT(countl_one, N - i - 1) // iterating backward
} // namespace multiword
template <size_t Bits, bool Signed, typename WordType = uint64_t>
struct BigInt {
private:
static_assert(cpp::is_integral_v<WordType> && cpp::is_unsigned_v<WordType>,
"WordType must be unsigned integer.");
struct Division {
BigInt quotient;
BigInt remainder;
};
public:
using word_type = WordType;
using unsigned_type = BigInt<Bits, false, word_type>;
using signed_type = BigInt<Bits, true, word_type>;
LIBC_INLINE_VAR static constexpr bool SIGNED = Signed;
LIBC_INLINE_VAR static constexpr size_t BITS = Bits;
LIBC_INLINE_VAR
static constexpr size_t WORD_SIZE = sizeof(WordType) * CHAR_BIT;
static_assert(Bits > 0 && Bits % WORD_SIZE == 0,
"Number of bits in BigInt should be a multiple of WORD_SIZE.");
LIBC_INLINE_VAR static constexpr size_t WORD_COUNT = Bits / WORD_SIZE;
cpp::array<WordType, WORD_COUNT> val{}; // zero initialized.
LIBC_INLINE constexpr BigInt() = default;
LIBC_INLINE constexpr BigInt(const BigInt &other) = default;
template <size_t OtherBits, bool OtherSigned>
LIBC_INLINE constexpr BigInt(
const BigInt<OtherBits, OtherSigned, WordType> &other) {
if (OtherBits >= Bits) { // truncate
for (size_t i = 0; i < WORD_COUNT; ++i)
val[i] = other[i];
} else { // zero or sign extend
size_t i = 0;
for (; i < OtherBits / WORD_SIZE; ++i)
val[i] = other[i];
extend(i, Signed && other.is_neg());
}
}
// Construct a BigInt from a C array.
template <size_t N> LIBC_INLINE constexpr BigInt(const WordType (&nums)[N]) {
static_assert(N == WORD_COUNT);
for (size_t i = 0; i < WORD_COUNT; ++i)
val[i] = nums[i];
}
LIBC_INLINE constexpr explicit BigInt(
const cpp::array<WordType, WORD_COUNT> &words) {
val = words;
}
// Initialize the first word to |v| and the rest to 0.
template <typename T, typename = cpp::enable_if_t<cpp::is_integral_v<T>>>
LIBC_INLINE constexpr BigInt(T v) {
constexpr size_t T_SIZE = sizeof(T) * CHAR_BIT;
const bool is_neg = Signed && (v < 0);
for (size_t i = 0; i < WORD_COUNT; ++i) {
if (v == 0) {
extend(i, is_neg);
return;
}
val[i] = static_cast<WordType>(v);
if constexpr (T_SIZE > WORD_SIZE)
v >>= WORD_SIZE;
else
v = 0;
}
}
LIBC_INLINE constexpr BigInt &operator=(const BigInt &other) = default;
// constants
LIBC_INLINE static constexpr BigInt zero() { return BigInt(); }
LIBC_INLINE static constexpr BigInt one() { return BigInt(1); }
LIBC_INLINE static constexpr BigInt all_ones() { return ~zero(); }
LIBC_INLINE static constexpr BigInt min() {
BigInt out;
if constexpr (SIGNED)
out.set_msb();
return out;
}
LIBC_INLINE static constexpr BigInt max() {
BigInt out = all_ones();
if constexpr (SIGNED)
out.clear_msb();
return out;
}
// TODO: Reuse the Sign type.
LIBC_INLINE constexpr bool is_neg() const { return SIGNED && get_msb(); }
template <typename T> LIBC_INLINE constexpr explicit operator T() const {
return to<T>();
}
template <typename T>
LIBC_INLINE constexpr cpp::enable_if_t<
cpp::is_integral_v<T> && !cpp::is_same_v<T, bool>, T>
to() const {
constexpr size_t T_SIZE = sizeof(T) * CHAR_BIT;
T lo = static_cast<T>(val[0]);
if constexpr (T_SIZE <= WORD_SIZE)
return lo;
constexpr size_t MAX_COUNT =
T_SIZE > Bits ? WORD_COUNT : T_SIZE / WORD_SIZE;
for (size_t i = 1; i < MAX_COUNT; ++i)
lo += static_cast<T>(val[i]) << (WORD_SIZE * i);
if constexpr (Signed && (T_SIZE > Bits)) {
// Extend sign for negative numbers.
constexpr T MASK = (~T(0) << Bits);
if (is_neg())
lo |= MASK;
}
return lo;
}
LIBC_INLINE constexpr explicit operator bool() const { return !is_zero(); }
LIBC_INLINE constexpr bool is_zero() const {
for (auto part : val)
if (part != 0)
return false;
return true;
}
// Add 'rhs' to this number and store the result in this number.
// Returns the carry value produced by the addition operation.
LIBC_INLINE constexpr WordType add_overflow(const BigInt &rhs) {
return multiword::add_with_carry(val, rhs.val);
}
LIBC_INLINE constexpr BigInt operator+(const BigInt &other) const {
BigInt result = *this;
result.add_overflow(other);
return result;
}
// This will only apply when initializing a variable from constant values, so
// it will always use the constexpr version of add_with_carry.
LIBC_INLINE constexpr BigInt operator+(BigInt &&other) const {
// We use addition commutativity to reuse 'other' and prevent allocation.
other.add_overflow(*this); // Returned carry value is ignored.
return other;
}
LIBC_INLINE constexpr BigInt &operator+=(const BigInt &other) {
add_overflow(other); // Returned carry value is ignored.
return *this;
}
// Subtract 'rhs' to this number and store the result in this number.
// Returns the carry value produced by the subtraction operation.
LIBC_INLINE constexpr WordType sub_overflow(const BigInt &rhs) {
return multiword::sub_with_borrow(val, rhs.val);
}
LIBC_INLINE constexpr BigInt operator-(const BigInt &other) const {
BigInt result = *this;
result.sub_overflow(other); // Returned carry value is ignored.
return result;
}
LIBC_INLINE constexpr BigInt operator-(BigInt &&other) const {
BigInt result = *this;
result.sub_overflow(other); // Returned carry value is ignored.
return result;
}
LIBC_INLINE constexpr BigInt &operator-=(const BigInt &other) {
// TODO(lntue): Set overflow flag / errno when carry is true.
sub_overflow(other); // Returned carry value is ignored.
return *this;
}
// Multiply this number with x and store the result in this number.
LIBC_INLINE constexpr WordType mul(WordType x) {
return multiword::scalar_multiply_with_carry(val, x);
}
// Return the full product.
template <size_t OtherBits>
LIBC_INLINE constexpr auto
ful_mul(const BigInt<OtherBits, Signed, WordType> &other) const {
BigInt<Bits + OtherBits, Signed, WordType> result;
multiword::multiply_with_carry(result.val, val, other.val);
return result;
}
LIBC_INLINE constexpr BigInt operator*(const BigInt &other) const {
// Perform full mul and truncate.
return BigInt(ful_mul(other));
}
// Fast hi part of the full product. The normal product `operator*` returns
// `Bits` least significant bits of the full product, while this function will
// approximate `Bits` most significant bits of the full product with errors
// bounded by:
// 0 <= (a.full_mul(b) >> Bits) - a.quick_mul_hi(b)) <= WORD_COUNT - 1.
//
// An example usage of this is to quickly (but less accurately) compute the
// product of (normalized) mantissas of floating point numbers:
// (mant_1, mant_2) -> quick_mul_hi -> normalize leading bit
// is much more efficient than:
// (mant_1, mant_2) -> ful_mul -> normalize leading bit
// -> convert back to same Bits width by shifting/rounding,
// especially for higher precisions.
//
// Performance summary:
// Number of 64-bit x 64-bit -> 128-bit multiplications performed.
// Bits WORD_COUNT ful_mul quick_mul_hi Error bound
// 128 2 4 3 1
// 196 3 9 6 2
// 256 4 16 10 3
// 512 8 64 36 7
LIBC_INLINE constexpr BigInt quick_mul_hi(const BigInt &other) const {
BigInt result;
multiword::quick_mul_hi(result.val, val, other.val);
return result;
}
// BigInt(x).pow_n(n) computes x ^ n.
// Note 0 ^ 0 == 1.
LIBC_INLINE constexpr void pow_n(uint64_t power) {
static_assert(!Signed);
BigInt result = one();
BigInt cur_power = *this;
while (power > 0) {
if ((power % 2) > 0)
result *= cur_power;
power >>= 1;
cur_power *= cur_power;
}
*this = result;
}
// Performs inplace signed / unsigned division. Returns remainder if not
// dividing by zero.
// For signed numbers it behaves like C++ signed integer division.
// That is by truncating the fractionnal part
// https://stackoverflow.com/a/3602857
LIBC_INLINE constexpr cpp::optional<BigInt> div(const BigInt ÷r) {
if (LIBC_UNLIKELY(divider.is_zero()))
return cpp::nullopt;
if (LIBC_UNLIKELY(divider == BigInt::one()))
return BigInt::zero();
Division result;
if constexpr (SIGNED)
result = divide_signed(*this, divider);
else
result = divide_unsigned(*this, divider);
*this = result.quotient;
return result.remainder;
}
// Efficiently perform BigInt / (x * 2^e), where x is a half-word-size
// unsigned integer, and return the remainder. The main idea is as follow:
// Let q = y / (x * 2^e) be the quotient, and
// r = y % (x * 2^e) be the remainder.
// First, notice that:
// r % (2^e) = y % (2^e),
// so we just need to focus on all the bits of y that is >= 2^e.
// To speed up the shift-and-add steps, we only use x as the divisor, and
// performing 32-bit shiftings instead of bit-by-bit shiftings.
// Since the remainder of each division step < x < 2^(WORD_SIZE / 2), the
// computation of each step is now properly contained within WordType.
// And finally we perform some extra alignment steps for the remaining bits.
LIBC_INLINE constexpr cpp::optional<BigInt>
div_uint_half_times_pow_2(multiword::half_width_t<WordType> x, size_t e) {
BigInt remainder;
if (x == 0)
return cpp::nullopt;
if (e >= Bits) {
remainder = *this;
*this = BigInt<Bits, false, WordType>();
return remainder;
}
BigInt quotient;
WordType x_word = static_cast<WordType>(x);
constexpr size_t LOG2_WORD_SIZE = cpp::bit_width(WORD_SIZE) - 1;
constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1;
constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1);
// lower = smallest multiple of WORD_SIZE that is >= e.
size_t lower = ((e >> LOG2_WORD_SIZE) + ((e & (WORD_SIZE - 1)) != 0))
<< LOG2_WORD_SIZE;
// lower_pos is the index of the closest WORD_SIZE-bit chunk >= 2^e.
size_t lower_pos = lower / WORD_SIZE;
// Keep track of current remainder mod x * 2^(32*i)
WordType rem = 0;
// pos is the index of the current 64-bit chunk that we are processing.
size_t pos = WORD_COUNT;
// TODO: look into if constexpr(Bits > 256) skip leading zeroes.
for (size_t q_pos = WORD_COUNT - lower_pos; q_pos > 0; --q_pos) {
// q_pos is 1 + the index of the current WORD_SIZE-bit chunk of the
// quotient being processed. Performing the division / modulus with
// divisor:
// x * 2^(WORD_SIZE*q_pos - WORD_SIZE/2),
// i.e. using the upper (WORD_SIZE/2)-bit of the current WORD_SIZE-bit
// chunk.
rem <<= HALF_WORD_SIZE;
rem += val[--pos] >> HALF_WORD_SIZE;
WordType q_tmp = rem / x_word;
rem %= x_word;
// Performing the division / modulus with divisor:
// x * 2^(WORD_SIZE*(q_pos - 1)),
// i.e. using the lower (WORD_SIZE/2)-bit of the current WORD_SIZE-bit
// chunk.
rem <<= HALF_WORD_SIZE;
rem += val[pos] & HALF_MASK;
quotient.val[q_pos - 1] = (q_tmp << HALF_WORD_SIZE) + rem / x_word;
rem %= x_word;
}
// So far, what we have is:
// quotient = y / (x * 2^lower), and
// rem = (y % (x * 2^lower)) / 2^lower.
// If (lower > e), we will need to perform an extra adjustment of the
// quotient and remainder, namely:
// y / (x * 2^e) = [ y / (x * 2^lower) ] * 2^(lower - e) +
// + (rem * 2^(lower - e)) / x
// (y % (x * 2^e)) / 2^e = (rem * 2^(lower - e)) % x
size_t last_shift = lower - e;
if (last_shift > 0) {
// quotient * 2^(lower - e)
quotient <<= last_shift;
WordType q_tmp = 0;
WordType d = val[--pos];
if (last_shift >= HALF_WORD_SIZE) {
// The shifting (rem * 2^(lower - e)) might overflow WordTyoe, so we
// perform a HALF_WORD_SIZE-bit shift first.
rem <<= HALF_WORD_SIZE;
rem += d >> HALF_WORD_SIZE;
d &= HALF_MASK;
q_tmp = rem / x_word;
rem %= x_word;
last_shift -= HALF_WORD_SIZE;
} else {
// Only use the upper HALF_WORD_SIZE-bit of the current WORD_SIZE-bit
// chunk.
d >>= HALF_WORD_SIZE;
}
if (last_shift > 0) {
rem <<= HALF_WORD_SIZE;
rem += d;
q_tmp <<= last_shift;
x_word <<= HALF_WORD_SIZE - last_shift;
q_tmp += rem / x_word;
rem %= x_word;
}
quotient.val[0] += q_tmp;
if (lower - e <= HALF_WORD_SIZE) {
// The remainder rem * 2^(lower - e) might overflow to the higher
// WORD_SIZE-bit chunk.
if (pos < WORD_COUNT - 1) {
remainder[pos + 1] = rem >> HALF_WORD_SIZE;
}
remainder[pos] = (rem << HALF_WORD_SIZE) + (val[pos] & HALF_MASK);
} else {
remainder[pos] = rem;
}
} else {
remainder[pos] = rem;
}
// Set the remaining lower bits of the remainder.
for (; pos > 0; --pos) {
remainder[pos - 1] = val[pos - 1];
}
*this = quotient;
return remainder;
}
LIBC_INLINE constexpr BigInt operator/(const BigInt &other) const {
BigInt result(*this);
result.div(other);
return result;
}
LIBC_INLINE constexpr BigInt &operator/=(const BigInt &other) {
div(other);
return *this;
}
LIBC_INLINE constexpr BigInt operator%(const BigInt &other) const {
BigInt result(*this);
return *result.div(other);
}
LIBC_INLINE constexpr BigInt &operator*=(const BigInt &other) {
*this = *this * other;
return *this;
}
LIBC_INLINE constexpr BigInt &operator<<=(size_t s) {
val = multiword::shift<multiword::LEFT, SIGNED>(val, s);
return *this;
}
LIBC_INLINE constexpr BigInt operator<<(size_t s) const {
return BigInt(multiword::shift<multiword::LEFT, SIGNED>(val, s));
}
LIBC_INLINE constexpr BigInt &operator>>=(size_t s) {
val = multiword::shift<multiword::RIGHT, SIGNED>(val, s);
return *this;
}
LIBC_INLINE constexpr BigInt operator>>(size_t s) const {
return BigInt(multiword::shift<multiword::RIGHT, SIGNED>(val, s));
}
#define DEFINE_BINOP(OP) \
LIBC_INLINE friend constexpr BigInt operator OP(const BigInt &lhs, \
const BigInt &rhs) { \
BigInt result; \
for (size_t i = 0; i < WORD_COUNT; ++i) \
result[i] = lhs[i] OP rhs[i]; \
return result; \
} \
LIBC_INLINE friend constexpr BigInt operator OP##=(BigInt &lhs, \
const BigInt &rhs) { \
for (size_t i = 0; i < WORD_COUNT; ++i) \
lhs[i] OP## = rhs[i]; \
return lhs; \
}
DEFINE_BINOP(&) // & and &=
DEFINE_BINOP(|) // | and |=
DEFINE_BINOP(^) // ^ and ^=
#undef DEFINE_BINOP
LIBC_INLINE constexpr BigInt operator~() const {
BigInt result;
for (size_t i = 0; i < WORD_COUNT; ++i)
result[i] = ~val[i];
return result;
}
LIBC_INLINE constexpr BigInt operator-() const {
BigInt result(*this);
result.negate();
return result;
}
LIBC_INLINE friend constexpr bool operator==(const BigInt &lhs,
const BigInt &rhs) {
for (size_t i = 0; i < WORD_COUNT; ++i)
if (lhs.val[i] != rhs.val[i])
return false;
return true;
}
LIBC_INLINE friend constexpr bool operator!=(const BigInt &lhs,
const BigInt &rhs) {
return !(lhs == rhs);
}
LIBC_INLINE friend constexpr bool operator>(const BigInt &lhs,
const BigInt &rhs) {
return cmp(lhs, rhs) > 0;
}
LIBC_INLINE friend constexpr bool operator>=(const BigInt &lhs,
const BigInt &rhs) {
return cmp(lhs, rhs) >= 0;
}
LIBC_INLINE friend constexpr bool operator<(const BigInt &lhs,
const BigInt &rhs) {
return cmp(lhs, rhs) < 0;
}
LIBC_INLINE friend constexpr bool operator<=(const BigInt &lhs,
const BigInt &rhs) {
return cmp(lhs, rhs) <= 0;
}
LIBC_INLINE constexpr BigInt &operator++() {
increment();
return *this;
}
LIBC_INLINE constexpr BigInt operator++(int) {
BigInt oldval(*this);
increment();
return oldval;
}
LIBC_INLINE constexpr BigInt &operator--() {
decrement();
return *this;
}
LIBC_INLINE constexpr BigInt operator--(int) {
BigInt oldval(*this);
decrement();
return oldval;
}
// Return the i-th word of the number.
LIBC_INLINE constexpr const WordType &operator[](size_t i) const {
return val[i];
}
// Return the i-th word of the number.
LIBC_INLINE constexpr WordType &operator[](size_t i) { return val[i]; }
private:
LIBC_INLINE friend constexpr int cmp(const BigInt &lhs, const BigInt &rhs) {
constexpr auto compare = [](WordType a, WordType b) {
return a == b ? 0 : a > b ? 1 : -1;
};
if constexpr (Signed) {
const bool lhs_is_neg = lhs.is_neg();
const bool rhs_is_neg = rhs.is_neg();
if (lhs_is_neg != rhs_is_neg)
return rhs_is_neg ? 1 : -1;
}
for (size_t i = WORD_COUNT; i-- > 0;)
if (auto cmp = compare(lhs[i], rhs[i]); cmp != 0)
return cmp;
return 0;
}
LIBC_INLINE constexpr void bitwise_not() {
for (auto &part : val)
part = ~part;
}
LIBC_INLINE constexpr void negate() {
bitwise_not();
increment();
}
LIBC_INLINE constexpr void increment() {
multiword::add_with_carry(val, cpp::array<WordType, 1>{1});
}
LIBC_INLINE constexpr void decrement() {
multiword::add_with_carry(val, cpp::array<WordType, 1>{1});
}
LIBC_INLINE constexpr void extend(size_t index, bool is_neg) {
const WordType value = is_neg ? cpp::numeric_limits<WordType>::max()
: cpp::numeric_limits<WordType>::min();
for (size_t i = index; i < WORD_COUNT; ++i)
val[i] = value;
}
LIBC_INLINE constexpr bool get_msb() const {
return val.back() >> (WORD_SIZE - 1);
}
LIBC_INLINE constexpr void set_msb() {
val.back() |= mask_leading_ones<WordType, 1>();
}
LIBC_INLINE constexpr void clear_msb() {
val.back() &= mask_trailing_ones<WordType, WORD_SIZE - 1>();
}
LIBC_INLINE constexpr void set_bit(size_t i) {
const size_t word_index = i / WORD_SIZE;
val[word_index] |= WordType(1) << (i % WORD_SIZE);
}
LIBC_INLINE constexpr static Division divide_unsigned(const BigInt ÷nd,
const BigInt ÷r) {
BigInt remainder = dividend;
BigInt quotient;
if (remainder >= divider) {
BigInt subtractor = divider;
int cur_bit = multiword::countl_zero(subtractor.val) -
multiword::countl_zero(remainder.val);
subtractor <<= cur_bit;
for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) {
if (remainder < subtractor)
continue;
remainder -= subtractor;
quotient.set_bit(cur_bit);
}
}
return Division{quotient, remainder};
}
LIBC_INLINE constexpr static Division divide_signed(const BigInt ÷nd,
const BigInt ÷r) {
// Special case because it is not possible to negate the min value of a
// signed integer.
if (dividend == min() && divider == min())
return Division{one(), zero()};
// 1. Convert the dividend and divisor to unsigned representation.
unsigned_type udividend(dividend);
unsigned_type udivider(divider);
// 2. Negate the dividend if it's negative, and similarly for the divisor.
const bool dividend_is_neg = dividend.is_neg();
const bool divider_is_neg = divider.is_neg();
if (dividend_is_neg)
udividend.negate();
if (divider_is_neg)
udivider.negate();
// 3. Use unsigned multiword division algorithm.
const auto unsigned_result = divide_unsigned(udividend, udivider);
// 4. Convert the quotient and remainder to signed representation.
Division result;
result.quotient = signed_type(unsigned_result.quotient);
result.remainder = signed_type(unsigned_result.remainder);
// 5. Negate the quotient if the dividend and divisor had opposite signs.
if (dividend_is_neg != divider_is_neg)
result.quotient.negate();
// 6. Negate the remainder if the dividend was negative.
if (dividend_is_neg)
result.remainder.negate();
return result;
}
friend signed_type;
friend unsigned_type;
};
namespace internal {
// We default BigInt's WordType to 'uint64_t' or 'uint32_t' depending on type
// availability.
template <size_t Bits>
struct WordTypeSelector : cpp::type_identity<
#ifdef LIBC_TYPES_HAS_INT64
uint64_t
#else
uint32_t
#endif // LIBC_TYPES_HAS_INT64
> {
};
// Except if we request 16 or 32 bits explicitly.
template <> struct WordTypeSelector<16> : cpp::type_identity<uint16_t> {};
template <> struct WordTypeSelector<32> : cpp::type_identity<uint32_t> {};
template <size_t Bits>
using WordTypeSelectorT = typename WordTypeSelector<Bits>::type;
} // namespace internal
template <size_t Bits>
using UInt = BigInt<Bits, false, internal::WordTypeSelectorT<Bits>>;
template <size_t Bits>
using Int = BigInt<Bits, true, internal::WordTypeSelectorT<Bits>>;
// Provides limits of BigInt.
template <size_t Bits, bool Signed, typename T>
struct cpp::numeric_limits<BigInt<Bits, Signed, T>> {
LIBC_INLINE static constexpr BigInt<Bits, Signed, T> max() {
return BigInt<Bits, Signed, T>::max();
}
LIBC_INLINE static constexpr BigInt<Bits, Signed, T> min() {
return BigInt<Bits, Signed, T>::min();
}
// Meant to match std::numeric_limits interface.
// NOLINTNEXTLINE(readability-identifier-naming)
LIBC_INLINE_VAR static constexpr int digits = Bits - Signed;
};
// type traits to determine whether a T is a BigInt.
template <typename T> struct is_big_int : cpp::false_type {};
template <size_t Bits, bool Signed, typename T>
struct is_big_int<BigInt<Bits, Signed, T>> : cpp::true_type {};
template <class T>
LIBC_INLINE_VAR constexpr bool is_big_int_v = is_big_int<T>::value;
// extensions of type traits to include BigInt
// is_integral_or_big_int
template <typename T>
struct is_integral_or_big_int
: cpp::bool_constant<(cpp::is_integral_v<T> || is_big_int_v<T>)> {};
template <typename T>
LIBC_INLINE_VAR constexpr bool is_integral_or_big_int_v =
is_integral_or_big_int<T>::value;
// make_big_int_unsigned
template <typename T> struct make_big_int_unsigned;
template <size_t Bits, bool Signed, typename T>
struct make_big_int_unsigned<BigInt<Bits, Signed, T>>
: cpp::type_identity<BigInt<Bits, false, T>> {};
template <typename T>
using make_big_int_unsigned_t = typename make_big_int_unsigned<T>::type;
// make_big_int_signed
template <typename T> struct make_big_int_signed;
template <size_t Bits, bool Signed, typename T>
struct make_big_int_signed<BigInt<Bits, Signed, T>>
: cpp::type_identity<BigInt<Bits, true, T>> {};
template <typename T>
using make_big_int_signed_t = typename make_big_int_signed<T>::type;
// make_integral_or_big_int_unsigned
template <typename T, class = void> struct make_integral_or_big_int_unsigned;
template <typename T>
struct make_integral_or_big_int_unsigned<
T, cpp::enable_if_t<cpp::is_integral_v<T>>> : cpp::make_unsigned<T> {};
template <typename T>
struct make_integral_or_big_int_unsigned<T, cpp::enable_if_t<is_big_int_v<T>>>
: make_big_int_unsigned<T> {};
template <typename T>
using make_integral_or_big_int_unsigned_t =
typename make_integral_or_big_int_unsigned<T>::type;
// make_integral_or_big_int_signed
template <typename T, class = void> struct make_integral_or_big_int_signed;
template <typename T>
struct make_integral_or_big_int_signed<T,
cpp::enable_if_t<cpp::is_integral_v<T>>>
: cpp::make_signed<T> {};
template <typename T>
struct make_integral_or_big_int_signed<T, cpp::enable_if_t<is_big_int_v<T>>>
: make_big_int_signed<T> {};
template <typename T>
using make_integral_or_big_int_signed_t =
typename make_integral_or_big_int_signed<T>::type;
// is_unsigned_integral_or_big_int
template <typename T>
struct is_unsigned_integral_or_big_int
: cpp::bool_constant<
cpp::is_same_v<T, make_integral_or_big_int_unsigned_t<T>>> {};
template <typename T>
// Meant to look like <type_traits> helper variable templates.
// NOLINTNEXTLINE(readability-identifier-naming)
LIBC_INLINE_VAR constexpr bool is_unsigned_integral_or_big_int_v =
is_unsigned_integral_or_big_int<T>::value;
namespace cpp {
// Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
template <typename To, typename From>
LIBC_INLINE constexpr cpp::enable_if_t<
(sizeof(To) == sizeof(From)) && cpp::is_trivially_copyable<To>::value &&
cpp::is_trivially_copyable<From>::value && is_big_int<To>::value,
To>
bit_cast(const From &from) {
To out;
using Storage = decltype(out.val);
out.val = cpp::bit_cast<Storage>(from);
return out;
}
// Specialization of cpp::bit_cast ('bit.h') from BigInt to T.
template <typename To, size_t Bits>
LIBC_INLINE constexpr cpp::enable_if_t<
sizeof(To) == sizeof(UInt<Bits>) &&
cpp::is_trivially_constructible<To>::value &&
cpp::is_trivially_copyable<To>::value &&
cpp::is_trivially_copyable<UInt<Bits>>::value,
To>
bit_cast(const UInt<Bits> &from) {
return cpp::bit_cast<To>(from.val);
}
// Specialization of cpp::popcount ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
popcount(T value) {
int bits = 0;
for (auto word : value.val)
if (word)
bits += popcount(word);
return bits;
}
// Specialization of cpp::has_single_bit ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, bool>
has_single_bit(T value) {
int bits = 0;
for (auto word : value.val) {
if (word == 0)
continue;
bits += popcount(word);
if (bits > 1)
return false;
}
return bits == 1;
}
// Specialization of cpp::countr_zero ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
countr_zero(const T &value) {
return multiword::countr_zero(value.val);
}
// Specialization of cpp::countl_zero ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
countl_zero(const T &value) {
return multiword::countl_zero(value.val);
}
// Specialization of cpp::countl_one ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
countl_one(T value) {
return multiword::countl_one(value.val);
}
// Specialization of cpp::countr_one ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
countr_one(T value) {
return multiword::countr_one(value.val);
}
// Specialization of cpp::bit_width ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
bit_width(T value) {
return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
}
// Forward-declare rotr so that rotl can use it.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
rotr(T value, int rotate);
// Specialization of cpp::rotl ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
rotl(T value, int rotate) {
constexpr unsigned N = cpp::numeric_limits<T>::digits;
rotate = rotate % N;
if (!rotate)
return value;
if (rotate < 0)
return cpp::rotr<T>(value, -rotate);
return (value << rotate) | (value >> (N - rotate));
}
// Specialization of cpp::rotr ('bit.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
rotr(T value, int rotate) {
constexpr unsigned N = cpp::numeric_limits<T>::digits;
rotate = rotate % N;
if (!rotate)
return value;
if (rotate < 0)
return cpp::rotl<T>(value, -rotate);
return (value >> rotate) | (value << (N - rotate));
}
} // namespace cpp
// Specialization of mask_trailing_ones ('math_extras.h') for BigInt.
template <typename T, size_t count>
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
mask_trailing_ones() {
static_assert(!T::SIGNED && count <= T::BITS);
if (count == T::BITS)
return T::all_ones();
constexpr size_t QUOTIENT = count / T::WORD_SIZE;
constexpr size_t REMAINDER = count % T::WORD_SIZE;
T out; // zero initialized
for (size_t i = 0; i <= QUOTIENT; ++i)
out[i] = i < QUOTIENT
? -1
: mask_trailing_ones<typename T::word_type, REMAINDER>();
return out;
}
// Specialization of mask_leading_ones ('math_extras.h') for BigInt.
template <typename T, size_t count>
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() {
static_assert(!T::SIGNED && count <= T::BITS);
if (count == T::BITS)
return T::all_ones();
constexpr size_t QUOTIENT = (T::BITS - count - 1U) / T::WORD_SIZE;
constexpr size_t REMAINDER = count % T::WORD_SIZE;
T out; // zero initialized
for (size_t i = QUOTIENT; i < T::WORD_COUNT; ++i)
out[i] = i > QUOTIENT
? -1
: mask_leading_ones<typename T::word_type, REMAINDER>();
return out;
}
// Specialization of mask_trailing_zeros ('math_extras.h') for BigInt.
template <typename T, size_t count>
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
mask_trailing_zeros() {
return mask_leading_ones<T, T::BITS - count>();
}
// Specialization of mask_leading_zeros ('math_extras.h') for BigInt.
template <typename T, size_t count>
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
mask_leading_zeros() {
return mask_trailing_ones<T, T::BITS - count>();
}
// Specialization of count_zeros ('math_extras.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
count_zeros(T value) {
return cpp::popcount(~value);
}
// Specialization of first_leading_zero ('math_extras.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
first_leading_zero(T value) {
return value == cpp::numeric_limits<T>::max() ? 0
: cpp::countl_one(value) + 1;
}
// Specialization of first_leading_one ('math_extras.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
first_leading_one(T value) {
return first_leading_zero(~value);
}
// Specialization of first_trailing_zero ('math_extras.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
first_trailing_zero(T value) {
return value == cpp::numeric_limits<T>::max() ? 0
: cpp::countr_zero(~value) + 1;
}
// Specialization of first_trailing_one ('math_extras.h') for BigInt.
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
first_trailing_one(T value) {
return value == cpp::numeric_limits<T>::max() ? 0
: cpp::countr_zero(value) + 1;
}
} // namespace LIBC_NAMESPACE
#endif // LLVM_LIBC_SRC___SUPPORT_UINT_H
|