Skip to content

Commit 6a8e6c9

Browse files
authored
[libc][NFC] Move BigInt out of the cpp namespace (llvm#84445)
As noted in llvm#84035 (comment) only files under the CPP folder should be in the `cpp` namespace.
1 parent 4b1910b commit 6a8e6c9

17 files changed

+119
-123
lines changed

libc/src/__support/FPUtil/dyadic_float.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE::fputil {
3131
// To simplify and improve the efficiency, many functions will assume that the
3232
// inputs are normal.
3333
template <size_t Bits> struct DyadicFloat {
34-
using MantissaType = LIBC_NAMESPACE::cpp::UInt<Bits>;
34+
using MantissaType = LIBC_NAMESPACE::UInt<Bits>;
3535

3636
Sign sign = Sign::POS;
3737
int exponent = 0;

libc/src/__support/UInt.h

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
#include <stddef.h> // For size_t
2424
#include <stdint.h>
2525

26-
namespace LIBC_NAMESPACE::cpp {
26+
namespace LIBC_NAMESPACE {
2727

2828
namespace internal {
2929
template <typename T> struct half_width;
3030

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> {};
3434
#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> {};
3636
#endif // __SIZEOF_INT128__
3737

3838
template <typename T> using half_width_t = typename half_width<T>::type;
3939
} // namespace internal
4040

4141
template <size_t Bits, bool Signed, typename WordType = uint64_t>
4242
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>,
4444
"WordType must be unsigned integer.");
4545

4646
using word_type = WordType;
@@ -76,15 +76,15 @@ struct BigInt {
7676
WordType sign = 0;
7777
if constexpr (Signed && OtherSigned) {
7878
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()));
8080
}
8181
for (; i < WORD_COUNT; ++i)
8282
val[i] = sign;
8383
}
8484
}
8585

8686
// 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>
8888
LIBC_INLINE constexpr BigInt(const WordType (&nums)[N]) {
8989
size_t min_wordcount = N < WORD_COUNT ? N : WORD_COUNT;
9090
size_t i = 0;
@@ -97,7 +97,7 @@ struct BigInt {
9797
}
9898

9999
// 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>>>
101101
LIBC_INLINE constexpr BigInt(T v) {
102102
val[0] = static_cast<WordType>(v);
103103

@@ -406,7 +406,7 @@ struct BigInt {
406406

407407
// div takes another BigInt of the same size and divides this by it. The value
408408
// 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) {
410410
BigInt remainder(0);
411411
if (*this < other) {
412412
remainder = *this;
@@ -417,7 +417,7 @@ struct BigInt {
417417
return remainder;
418418
}
419419
if (other == 0) {
420-
return nullopt;
420+
return cpp::nullopt;
421421
}
422422

423423
BigInt quotient(0);
@@ -448,12 +448,12 @@ struct BigInt {
448448
// Since the remainder of each division step < x < 2^(WORD_SIZE / 2), the
449449
// computation of each step is now properly contained within WordType.
450450
// 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>
452452
div_uint_half_times_pow_2(internal::half_width_t<WordType> x, size_t e) {
453453
BigInt remainder(0);
454454

455455
if (x == 0) {
456-
return nullopt;
456+
return cpp::nullopt;
457457
}
458458
if (e >= Bits) {
459459
remainder = *this;
@@ -463,7 +463,7 @@ struct BigInt {
463463

464464
BigInt quotient(0);
465465
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;
467467
constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1;
468468
constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1);
469469
// lower = smallest multiple of WORD_SIZE that is >= e.
@@ -592,7 +592,7 @@ struct BigInt {
592592
int leading_zeroes = 0;
593593
for (auto i = val.size(); i > 0;) {
594594
--i;
595-
const int zeroes = countl_zero(val[i]);
595+
const int zeroes = cpp::countl_zero(val[i]);
596596
leading_zeroes += zeroes;
597597
if (zeroes != word_digits)
598598
break;
@@ -605,7 +605,7 @@ struct BigInt {
605605
constexpr int word_digits = cpp::numeric_limits<word_type>::digits;
606606
int trailing_zeroes = 0;
607607
for (auto word : val) {
608-
const int zeroes = countr_zero(word);
608+
const int zeroes = cpp::countr_zero(word);
609609
trailing_zeroes += zeroes;
610610
if (zeroes != word_digits)
611611
break;
@@ -913,7 +913,7 @@ template <size_t Bits>
913913
using Int = BigInt<Bits, true, internal::WordTypeSelectorT<Bits>>;
914914

915915
// Provides limits of U/Int<128>.
916-
template <> class numeric_limits<UInt<128>> {
916+
template <> class cpp::numeric_limits<UInt<128>> {
917917
public:
918918
LIBC_INLINE static constexpr UInt<128> max() {
919919
return UInt<128>({0xffff'ffff'ffff'ffff, 0xffff'ffff'ffff'ffff});
@@ -924,7 +924,7 @@ template <> class numeric_limits<UInt<128>> {
924924
LIBC_INLINE_VAR static constexpr int digits = 128;
925925
};
926926

927-
template <> class numeric_limits<Int<128>> {
927+
template <> class cpp::numeric_limits<Int<128>> {
928928
public:
929929
LIBC_INLINE static constexpr Int<128> max() {
930930
return Int<128>({0xffff'ffff'ffff'ffff, 0x7fff'ffff'ffff'ffff});
@@ -937,7 +937,7 @@ template <> class numeric_limits<Int<128>> {
937937
LIBC_INLINE_VAR static constexpr int digits = 128;
938938
};
939939

940-
// type traits to determine whether a T is a cpp::BigInt.
940+
// type traits to determine whether a T is a BigInt.
941941
template <typename T> struct is_big_int : cpp::false_type {};
942942

943943
template <size_t Bits, bool Signed, typename T>
@@ -946,6 +946,8 @@ struct is_big_int<BigInt<Bits, Signed, T>> : cpp::true_type {};
946946
template <class T>
947947
LIBC_INLINE_VAR constexpr bool is_big_int_v = is_big_int<T>::value;
948948

949+
namespace cpp {
950+
949951
// Specialization of cpp::bit_cast ('bit.h') from T to BigInt.
950952
template <typename To, typename From>
951953
LIBC_INLINE constexpr cpp::enable_if_t<
@@ -973,7 +975,7 @@ bit_cast(const UInt<Bits> &from) {
973975

974976
// Specialization of cpp::has_single_bit ('bit.h') for BigInt.
975977
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>
977979
has_single_bit(T value) {
978980
int bits = 0;
979981
for (auto word : value.val) {
@@ -988,49 +990,49 @@ has_single_bit(T value) {
988990

989991
// Specialization of cpp::countr_zero ('bit.h') for BigInt.
990992
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>
992994
countr_zero(const T &value) {
993995
return value.ctz();
994996
}
995997

996998
// Specialization of cpp::countl_zero ('bit.h') for BigInt.
997999
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>
9991001
countl_zero(const T &value) {
10001002
return value.clz();
10011003
}
10021004

10031005
// Specialization of cpp::countl_one ('bit.h') for BigInt.
10041006
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>
10061008
countl_one(T value) {
10071009
// TODO : Implement a faster version not involving operator~.
10081010
return cpp::countl_zero<T>(~value);
10091011
}
10101012

10111013
// Specialization of cpp::countr_one ('bit.h') for BigInt.
10121014
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>
10141016
countr_one(T value) {
10151017
// TODO : Implement a faster version not involving operator~.
10161018
return cpp::countr_zero<T>(~value);
10171019
}
10181020

10191021
// Specialization of cpp::bit_width ('bit.h') for BigInt.
10201022
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>
10221024
bit_width(T value) {
10231025
return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
10241026
}
10251027

10261028
// Forward-declare rotr so that rotl can use it.
10271029
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>
10291031
rotr(T value, int rotate);
10301032

10311033
// Specialization of cpp::rotl ('bit.h') for BigInt.
10321034
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>
10341036
rotl(T value, int rotate) {
10351037
constexpr unsigned N = cpp::numeric_limits<T>::digits;
10361038
rotate = rotate % N;
@@ -1043,7 +1045,7 @@ rotl(T value, int rotate) {
10431045

10441046
// Specialization of cpp::rotr ('bit.h') for BigInt.
10451047
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>
10471049
rotr(T value, int rotate) {
10481050
constexpr unsigned N = cpp::numeric_limits<T>::digits;
10491051
rotate = rotate % N;
@@ -1054,13 +1056,11 @@ rotr(T value, int rotate) {
10541056
return (value >> rotate) | (value << (N - rotate));
10551057
}
10561058

1057-
} // namespace LIBC_NAMESPACE::cpp
1058-
1059-
namespace LIBC_NAMESPACE {
1059+
} // namespace cpp
10601060

10611061
// Specialization of mask_trailing_ones ('math_extras.h') for BigInt.
10621062
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>
10641064
mask_trailing_ones() {
10651065
static_assert(!T::SIGNED);
10661066
if (count == 0)
@@ -1086,8 +1086,7 @@ mask_trailing_ones() {
10861086

10871087
// Specialization of mask_leading_ones ('math_extras.h') for BigInt.
10881088
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() {
10911090
static_assert(!T::SIGNED);
10921091
if (count == 0)
10931092
return T();

libc/src/__support/UInt128.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
using UInt128 = __uint128_t;
1616
using Int128 = __int128_t;
1717
#else
18-
using UInt128 = LIBC_NAMESPACE::cpp::UInt<128>;
19-
using Int128 = LIBC_NAMESPACE::cpp::Int<128>;
18+
using UInt128 = LIBC_NAMESPACE::UInt<128>;
19+
using Int128 = LIBC_NAMESPACE::Int<128>;
2020
#endif
2121

2222
#endif // LLVM_LIBC_SRC___SUPPORT_UINT128_H

0 commit comments

Comments
 (0)