23
23
#include < stddef.h> // For size_t
24
24
#include < stdint.h>
25
25
26
- namespace LIBC_NAMESPACE ::cpp {
26
+ namespace LIBC_NAMESPACE {
27
27
28
28
namespace internal {
29
29
template <typename T> struct half_width ;
30
30
31
- template <> struct half_width <uint64_t > : type_identity<uint32_t > {};
32
- template <> struct half_width <uint32_t > : type_identity<uint16_t > {};
33
- template <> struct half_width <uint16_t > : type_identity<uint8_t > {};
31
+ template <> struct half_width <uint64_t > : cpp:: type_identity<uint32_t > {};
32
+ template <> struct half_width <uint32_t > : cpp:: type_identity<uint16_t > {};
33
+ template <> struct half_width <uint16_t > : cpp:: type_identity<uint8_t > {};
34
34
#ifdef __SIZEOF_INT128__
35
- template <> struct half_width <__uint128_t > : type_identity<uint64_t > {};
35
+ template <> struct half_width <__uint128_t > : cpp:: type_identity<uint64_t > {};
36
36
#endif // __SIZEOF_INT128__
37
37
38
38
template <typename T> using half_width_t = typename half_width<T>::type;
39
39
} // namespace internal
40
40
41
41
template <size_t Bits, bool Signed, typename WordType = uint64_t >
42
42
struct BigInt {
43
- static_assert (is_integral_v<WordType> && is_unsigned_v<WordType>,
43
+ static_assert (cpp:: is_integral_v<WordType> && cpp:: is_unsigned_v<WordType>,
44
44
" WordType must be unsigned integer." );
45
45
46
46
using word_type = WordType;
@@ -76,15 +76,15 @@ struct BigInt {
76
76
WordType sign = 0 ;
77
77
if constexpr (Signed && OtherSigned) {
78
78
sign = static_cast <WordType>(
79
- -static_cast <make_signed_t <WordType>>(other.is_neg ()));
79
+ -static_cast <cpp:: make_signed_t <WordType>>(other.is_neg ()));
80
80
}
81
81
for (; i < WORD_COUNT; ++i)
82
82
val[i] = sign;
83
83
}
84
84
}
85
85
86
86
// Construct a BigInt from a C array.
87
- template <size_t N, enable_if_t <N <= WORD_COUNT, int > = 0 >
87
+ template <size_t N, cpp:: enable_if_t <N <= WORD_COUNT, int > = 0 >
88
88
LIBC_INLINE constexpr BigInt (const WordType (&nums)[N]) {
89
89
size_t min_wordcount = N < WORD_COUNT ? N : WORD_COUNT;
90
90
size_t i = 0 ;
@@ -97,7 +97,7 @@ struct BigInt {
97
97
}
98
98
99
99
// Initialize the first word to |v| and the rest to 0.
100
- template <typename T, typename = cpp::enable_if_t <is_integral_v<T>>>
100
+ template <typename T, typename = cpp::enable_if_t <cpp:: is_integral_v<T>>>
101
101
LIBC_INLINE constexpr BigInt (T v) {
102
102
val[0 ] = static_cast <WordType>(v);
103
103
@@ -406,7 +406,7 @@ struct BigInt {
406
406
407
407
// div takes another BigInt of the same size and divides this by it. The value
408
408
// of this will be set to the quotient, and the return value is the remainder.
409
- LIBC_INLINE constexpr optional<BigInt> div (const BigInt &other) {
409
+ LIBC_INLINE constexpr cpp:: optional<BigInt> div (const BigInt &other) {
410
410
BigInt remainder (0 );
411
411
if (*this < other) {
412
412
remainder = *this ;
@@ -417,7 +417,7 @@ struct BigInt {
417
417
return remainder ;
418
418
}
419
419
if (other == 0 ) {
420
- return nullopt;
420
+ return cpp:: nullopt;
421
421
}
422
422
423
423
BigInt quotient (0 );
@@ -448,12 +448,12 @@ struct BigInt {
448
448
// Since the remainder of each division step < x < 2^(WORD_SIZE / 2), the
449
449
// computation of each step is now properly contained within WordType.
450
450
// And finally we perform some extra alignment steps for the remaining bits.
451
- LIBC_INLINE constexpr optional<BigInt>
451
+ LIBC_INLINE constexpr cpp:: optional<BigInt>
452
452
div_uint_half_times_pow_2 (internal::half_width_t <WordType> x, size_t e) {
453
453
BigInt remainder (0 );
454
454
455
455
if (x == 0 ) {
456
- return nullopt;
456
+ return cpp:: nullopt;
457
457
}
458
458
if (e >= Bits) {
459
459
remainder = *this ;
@@ -463,7 +463,7 @@ struct BigInt {
463
463
464
464
BigInt quotient (0 );
465
465
WordType x_word = static_cast <WordType>(x);
466
- constexpr size_t LOG2_WORD_SIZE = bit_width (WORD_SIZE) - 1 ;
466
+ constexpr size_t LOG2_WORD_SIZE = cpp:: bit_width (WORD_SIZE) - 1 ;
467
467
constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1 ;
468
468
constexpr WordType HALF_MASK = ((WordType (1 ) << HALF_WORD_SIZE) - 1 );
469
469
// lower = smallest multiple of WORD_SIZE that is >= e.
@@ -592,7 +592,7 @@ struct BigInt {
592
592
int leading_zeroes = 0 ;
593
593
for (auto i = val.size (); i > 0 ;) {
594
594
--i;
595
- const int zeroes = countl_zero (val[i]);
595
+ const int zeroes = cpp:: countl_zero (val[i]);
596
596
leading_zeroes += zeroes;
597
597
if (zeroes != word_digits)
598
598
break ;
@@ -605,7 +605,7 @@ struct BigInt {
605
605
constexpr int word_digits = cpp::numeric_limits<word_type>::digits;
606
606
int trailing_zeroes = 0 ;
607
607
for (auto word : val) {
608
- const int zeroes = countr_zero (word);
608
+ const int zeroes = cpp:: countr_zero (word);
609
609
trailing_zeroes += zeroes;
610
610
if (zeroes != word_digits)
611
611
break ;
@@ -913,7 +913,7 @@ template <size_t Bits>
913
913
using Int = BigInt<Bits, true , internal::WordTypeSelectorT<Bits>>;
914
914
915
915
// Provides limits of U/Int<128>.
916
- template <> class numeric_limits <UInt<128 >> {
916
+ template <> class cpp :: numeric_limits<UInt<128 >> {
917
917
public:
918
918
LIBC_INLINE static constexpr UInt<128 > max () {
919
919
return UInt<128 >({0xffff'ffff'ffff'ffff , 0xffff'ffff'ffff'ffff });
@@ -924,7 +924,7 @@ template <> class numeric_limits<UInt<128>> {
924
924
LIBC_INLINE_VAR static constexpr int digits = 128 ;
925
925
};
926
926
927
- template <> class numeric_limits <Int<128 >> {
927
+ template <> class cpp :: numeric_limits<Int<128 >> {
928
928
public:
929
929
LIBC_INLINE static constexpr Int<128 > max () {
930
930
return Int<128 >({0xffff'ffff'ffff'ffff , 0x7fff'ffff'ffff'ffff });
@@ -937,7 +937,7 @@ template <> class numeric_limits<Int<128>> {
937
937
LIBC_INLINE_VAR static constexpr int digits = 128 ;
938
938
};
939
939
940
- // type traits to determine whether a T is a cpp:: BigInt.
940
+ // type traits to determine whether a T is a BigInt.
941
941
template <typename T> struct is_big_int : cpp::false_type {};
942
942
943
943
template <size_t Bits, bool Signed, typename T>
@@ -946,6 +946,8 @@ struct is_big_int<BigInt<Bits, Signed, T>> : cpp::true_type {};
946
946
template <class T >
947
947
LIBC_INLINE_VAR constexpr bool is_big_int_v = is_big_int<T>::value;
948
948
949
+ namespace cpp {
950
+
949
951
// Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
950
952
template <typename To, typename From>
951
953
LIBC_INLINE constexpr cpp::enable_if_t <
@@ -973,7 +975,7 @@ bit_cast(const UInt<Bits> &from) {
973
975
974
976
// Specialization of cpp::has_single_bit ('bit.h') for BigInt.
975
977
template <typename T>
976
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, bool >
978
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, bool >
977
979
has_single_bit (T value) {
978
980
int bits = 0 ;
979
981
for (auto word : value.val ) {
@@ -988,49 +990,49 @@ has_single_bit(T value) {
988
990
989
991
// Specialization of cpp::countr_zero ('bit.h') for BigInt.
990
992
template <typename T>
991
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, int >
993
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, int >
992
994
countr_zero (const T &value) {
993
995
return value.ctz ();
994
996
}
995
997
996
998
// Specialization of cpp::countl_zero ('bit.h') for BigInt.
997
999
template <typename T>
998
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, int >
1000
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, int >
999
1001
countl_zero (const T &value) {
1000
1002
return value.clz ();
1001
1003
}
1002
1004
1003
1005
// Specialization of cpp::countl_one ('bit.h') for BigInt.
1004
1006
template <typename T>
1005
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, int >
1007
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, int >
1006
1008
countl_one (T value) {
1007
1009
// TODO : Implement a faster version not involving operator~.
1008
1010
return cpp::countl_zero<T>(~value);
1009
1011
}
1010
1012
1011
1013
// Specialization of cpp::countr_one ('bit.h') for BigInt.
1012
1014
template <typename T>
1013
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, int >
1015
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, int >
1014
1016
countr_one (T value) {
1015
1017
// TODO : Implement a faster version not involving operator~.
1016
1018
return cpp::countr_zero<T>(~value);
1017
1019
}
1018
1020
1019
1021
// Specialization of cpp::bit_width ('bit.h') for BigInt.
1020
1022
template <typename T>
1021
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, int >
1023
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, int >
1022
1024
bit_width (T value) {
1023
1025
return cpp::numeric_limits<T>::digits - cpp::countl_zero (value);
1024
1026
}
1025
1027
1026
1028
// Forward-declare rotr so that rotl can use it.
1027
1029
template <typename T>
1028
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, T>
1030
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, T>
1029
1031
rotr (T value, int rotate);
1030
1032
1031
1033
// Specialization of cpp::rotl ('bit.h') for BigInt.
1032
1034
template <typename T>
1033
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, T>
1035
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, T>
1034
1036
rotl (T value, int rotate) {
1035
1037
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1036
1038
rotate = rotate % N;
@@ -1043,7 +1045,7 @@ rotl(T value, int rotate) {
1043
1045
1044
1046
// Specialization of cpp::rotr ('bit.h') for BigInt.
1045
1047
template <typename T>
1046
- [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, T>
1048
+ [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, T>
1047
1049
rotr (T value, int rotate) {
1048
1050
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1049
1051
rotate = rotate % N;
@@ -1054,13 +1056,11 @@ rotr(T value, int rotate) {
1054
1056
return (value >> rotate) | (value << (N - rotate));
1055
1057
}
1056
1058
1057
- } // namespace LIBC_NAMESPACE::cpp
1058
-
1059
- namespace LIBC_NAMESPACE {
1059
+ } // namespace cpp
1060
1060
1061
1061
// Specialization of mask_trailing_ones ('math_extras.h') for BigInt.
1062
1062
template <typename T, size_t count>
1063
- LIBC_INLINE constexpr cpp::enable_if_t <cpp:: is_big_int_v<T>, T>
1063
+ LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, T>
1064
1064
mask_trailing_ones () {
1065
1065
static_assert (!T::SIGNED);
1066
1066
if (count == 0 )
@@ -1086,8 +1086,7 @@ mask_trailing_ones() {
1086
1086
1087
1087
// Specialization of mask_leading_ones ('math_extras.h') for BigInt.
1088
1088
template <typename T, size_t count>
1089
- LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, T>
1090
- mask_leading_ones () {
1089
+ LIBC_INLINE constexpr cpp::enable_if_t <is_big_int_v<T>, T> mask_leading_ones () {
1091
1090
static_assert (!T::SIGNED);
1092
1091
if (count == 0 )
1093
1092
return T ();
0 commit comments