Skip to content

Commit dd143c5

Browse files
committed
[libc] Provide LIBC_TYPES_HAS_INT128
Umbrella bug #83182
1 parent ac74d9e commit dd143c5

File tree

23 files changed

+73
-34
lines changed

23 files changed

+73
-34
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ add_header_library(
203203
libc.src.__support.common
204204
libc.src.__support.CPP.bit
205205
libc.src.__support.CPP.type_traits
206+
libc.src.__support.macros.properties.types
206207
)
207208

208209
add_header_library(
@@ -217,6 +218,7 @@ add_header_library(
217218
libc.src.__support.CPP.bit
218219
libc.src.__support.CPP.type_traits
219220
libc.src.__support.macros.optimization
221+
libc.src.__support.macros.properties.types
220222
)
221223

222224
add_header_library(
@@ -225,6 +227,7 @@ add_header_library(
225227
UInt128.h
226228
DEPENDS
227229
.uint
230+
libc.src.__support.macros.properties.types
228231
)
229232

230233
add_header_library(

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ add_header_library(
4949
DEPENDS
5050
.type_traits
5151
libc.include.llvm-libc-macros.limits_macros
52+
libc.src.__support.macros.properties.types
5253
)
5354

5455
add_header_library(

libc/src/__support/CPP/limits.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include "include/llvm-libc-macros/limits-macros.h" // CHAR_BIT
1313
#include "src/__support/CPP/type_traits/is_integral.h"
1414
#include "src/__support/CPP/type_traits/is_signed.h"
15-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
15+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
16+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1617

1718
namespace LIBC_NAMESPACE {
1819
namespace cpp {
@@ -76,7 +77,7 @@ template <>
7677
struct numeric_limits<unsigned char>
7778
: public internal::integer_impl<unsigned char, 0, UCHAR_MAX> {};
7879

79-
#ifdef __SIZEOF_INT128__
80+
#ifdef LIBC_TYPES_HAS_INT128
8081
// On platform where UInt128 resolves to __uint128_t, this specialization
8182
// provides the limits of UInt128.
8283
template <>

libc/src/__support/CPP/type_traits/is_integral.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/CPP/type_traits/is_same.h"
1212
#include "src/__support/CPP/type_traits/remove_cv.h"
1313
#include "src/__support/macros/attributes.h"
14+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1415

1516
namespace LIBC_NAMESPACE::cpp {
1617

@@ -25,7 +26,7 @@ template <typename T> struct is_integral {
2526
public:
2627
LIBC_INLINE_VAR static constexpr bool value = __is_unqualified_any_of<
2728
T,
28-
#ifdef __SIZEOF_INT128__
29+
#ifdef LIBC_TYPES_HAS_INT128
2930
__int128_t, __uint128_t,
3031
#endif
3132
char, signed char, unsigned char, short, unsigned short, int,

libc/src/__support/CPP/type_traits/make_signed.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_MAKE_SIGNED_H
1010

1111
#include "src/__support/CPP/type_traits/type_identity.h"
12+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1213

1314
namespace LIBC_NAMESPACE::cpp {
1415

@@ -26,7 +27,7 @@ template <> struct make_signed<unsigned int> : type_identity<int> {};
2627
template <> struct make_signed<unsigned long> : type_identity<long> {};
2728
template <>
2829
struct make_signed<unsigned long long> : type_identity<long long> {};
29-
#ifdef __SIZEOF_INT128__
30+
#ifdef LIBC_TYPES_HAS_INT128
3031
template <> struct make_signed<__int128_t> : type_identity<__int128_t> {};
3132
template <> struct make_signed<__uint128_t> : type_identity<__int128_t> {};
3233
#endif

libc/src/__support/CPP/type_traits/make_unsigned.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_MAKE_UNSIGNED_H
1010

1111
#include "src/__support/CPP/type_traits/type_identity.h"
12+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1213

1314
namespace LIBC_NAMESPACE::cpp {
1415

@@ -31,7 +32,7 @@ template <>
3132
struct make_unsigned<unsigned long> : type_identity<unsigned long> {};
3233
template <>
3334
struct make_unsigned<unsigned long long> : type_identity<unsigned long long> {};
34-
#ifdef __SIZEOF_INT128__
35+
#ifdef LIBC_TYPES_HAS_INT128
3536
template <> struct make_unsigned<__int128_t> : type_identity<__uint128_t> {};
3637
template <> struct make_unsigned<__uint128_t> : type_identity<__uint128_t> {};
3738
#endif

libc/src/__support/UInt.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
#include "src/__support/CPP/optional.h"
1616
#include "src/__support/CPP/type_traits.h"
1717
#include "src/__support/integer_utils.h"
18-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
19-
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
20-
#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
18+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
19+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
20+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
21+
#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
2122
#include "src/__support/number_pair.h"
2223

2324
#include <stddef.h> // For size_t
@@ -31,9 +32,9 @@ template <typename T> struct half_width;
3132
template <> struct half_width<uint64_t> : type_identity<uint32_t> {};
3233
template <> struct half_width<uint32_t> : type_identity<uint16_t> {};
3334
template <> struct half_width<uint16_t> : type_identity<uint8_t> {};
34-
#ifdef __SIZEOF_INT128__
35+
#ifdef LIBC_TYPES_HAS_INT128
3536
template <> struct half_width<__uint128_t> : type_identity<uint64_t> {};
36-
#endif // __SIZEOF_INT128__
37+
#endif // LIBC_TYPES_HAS_INT128
3738

3839
template <typename T> using half_width_t = typename half_width<T>::type;
3940
} // namespace internal
@@ -615,7 +616,7 @@ struct BigInt {
615616
val[1] = uint32_t(tmp >> 32);
616617
return;
617618
}
618-
#ifdef __SIZEOF_INT128__
619+
#ifdef LIBC_TYPES_HAS_INT128
619620
if constexpr ((Bits == 128) && (WORD_SIZE == 64)) {
620621
// Use builtin 128 bits if available;
621622
if (s >= 128) {
@@ -629,7 +630,7 @@ struct BigInt {
629630
val[1] = uint64_t(tmp >> 64);
630631
return;
631632
}
632-
#endif // __SIZEOF_INT128__
633+
#endif // LIBC_TYPES_HAS_INT128
633634
if (LIBC_UNLIKELY(s == 0))
634635
return;
635636

@@ -686,7 +687,7 @@ struct BigInt {
686687
val[1] = uint32_t(tmp >> 32);
687688
return;
688689
}
689-
#ifdef __SIZEOF_INT128__
690+
#ifdef LIBC_TYPES_HAS_INT128
690691
if constexpr ((Bits == 128) && (WORD_SIZE == 64)) {
691692
// Use builtin 128 bits if available;
692693
if (s >= 128) {
@@ -704,7 +705,7 @@ struct BigInt {
704705
val[1] = uint64_t(tmp >> 64);
705706
return;
706707
}
707-
#endif // __SIZEOF_INT128__
708+
#endif // LIBC_TYPES_HAS_INT128
708709

709710
if (LIBC_UNLIKELY(s == 0))
710711
return;

libc/src/__support/UInt128.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_UINT128_H
1111

1212
#include "UInt.h"
13+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1314

14-
#if defined(__SIZEOF_INT128__)
15+
#ifdef LIBC_TYPES_HAS_INT128
1516
using UInt128 = __uint128_t;
1617
using Int128 = __int128_t;
1718
#else
1819
using UInt128 = LIBC_NAMESPACE::cpp::UInt<128>;
1920
using Int128 = LIBC_NAMESPACE::cpp::Int<128>;
20-
#endif
21+
#endif // LIBC_TYPES_HAS_INT128
2122

2223
#endif // LLVM_LIBC_SRC___SUPPORT_UINT128_H

libc/src/__support/integer_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "src/__support/CPP/type_traits.h"
1313
#include "src/__support/common.h"
14+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1415

1516
#include "math_extras.h"
1617
#include "number_pair.h"
@@ -52,7 +53,7 @@ LIBC_INLINE constexpr NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a,
5253
return result;
5354
}
5455

55-
#ifdef __SIZEOF_INT128__
56+
#ifdef LIBC_TYPES_HAS_INT128
5657
template <>
5758
LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
5859
uint64_t b) {
@@ -62,7 +63,7 @@ LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
6263
result.hi = uint64_t(prod >> 64);
6364
return result;
6465
}
65-
#endif // __SIZEOF_INT128__
66+
#endif // LIBC_TYPES_HAS_INT128
6667

6768
} // namespace LIBC_NAMESPACE
6869

libc/src/__support/macros/properties/types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "src/__support/macros/properties/cpu_features.h"
1818
#include "src/__support/macros/properties/os.h"
1919

20+
#include <stdint.h> // __SIZEOF_INT128__
21+
2022
// 'long double' properties.
2123
#if (LDBL_MANT_DIG == 53)
2224
#define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
@@ -26,6 +28,11 @@
2628
#define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128
2729
#endif
2830

31+
// int128 / uint128 support
32+
#if defined(__SIZEOF_INT128__)
33+
#define LIBC_TYPES_HAS_INT128
34+
#endif // defined(__SIZEOF_INT128__)
35+
2936
// -- float16 support ---------------------------------------------------------
3037
// TODO: move this logic to "llvm-libc-types/float16.h"
3138
#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)

libc/test/UnitTest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ add_unittest_framework_library(
7373
libc.src.__support.CPP.string_view
7474
libc.src.__support.CPP.type_traits
7575
libc.src.__support.fixed_point.fx_rep
76+
libc.src.__support.macros.properties.types
7677
libc.src.__support.OSUtil.osutil
7778
libc.src.__support.uint128
7879
)

libc/test/UnitTest/LibcTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "src/__support/CPP/string_view.h"
1414
#include "src/__support/UInt128.h"
1515
#include "src/__support/fixed_point/fx_rep.h"
16+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1617
#include "test/UnitTest/TestLogger.h"
1718

1819
#if __STDC_HOSTED__
@@ -215,11 +216,11 @@ TEST_SPECIALIZATION(bool);
215216

216217
// We cannot just use a single UInt128 specialization as that resolves to only
217218
// one type, UInt<128> or __uint128_t. We want both overloads as we want to
218-
#ifdef __SIZEOF_INT128__
219+
#ifdef LIBC_TYPES_HAS_INT128
219220
// When builtin __uint128_t type is available, include its specialization
220221
// also.
221222
TEST_SPECIALIZATION(__uint128_t);
222-
#endif
223+
#endif // LIBC_TYPES_HAS_INT128
223224

224225
TEST_SPECIALIZATION(LIBC_NAMESPACE::cpp::Int<128>);
225226

libc/test/UnitTest/TestLogger.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "src/__support/CPP/string_view.h"
44
#include "src/__support/OSUtil/io.h" // write_to_stderr
55
#include "src/__support/UInt128.h"
6+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
67

78
#include <stdint.h>
89

@@ -68,11 +69,11 @@ template TestLogger &TestLogger::operator<< <unsigned short>(unsigned short);
6869
template TestLogger &TestLogger::operator<< <unsigned int>(unsigned int);
6970
template TestLogger &TestLogger::operator<< <unsigned long>(unsigned long);
7071
template TestLogger &
71-
TestLogger::operator<< <unsigned long long>(unsigned long long);
72+
TestLogger::operator<< <unsigned long long>(unsigned long long);
7273

73-
#ifdef __SIZEOF_INT128__
74+
#ifdef LIBC_TYPES_HAS_INT128
7475
template TestLogger &TestLogger::operator<< <__uint128_t>(__uint128_t);
75-
#endif
76+
#endif // LIBC_TYPES_HAS_INT128
7677
template TestLogger &TestLogger::operator<< <cpp::UInt<128>>(cpp::UInt<128>);
7778
template TestLogger &TestLogger::operator<< <cpp::UInt<192>>(cpp::UInt<192>);
7879
template TestLogger &TestLogger::operator<< <cpp::UInt<256>>(cpp::UInt<256>);

libc/test/src/__support/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
9191
SRCS
9292
uint_test.cpp
9393
DEPENDS
94-
libc.src.__support.uint
9594
libc.src.__support.CPP.optional
95+
libc.src.__support.macros.properties.types
96+
libc.src.__support.uint
9697
)
9798
endif()
9899

@@ -103,8 +104,9 @@ add_libc_test(
103104
SRCS
104105
integer_literals_test.cpp
105106
DEPENDS
106-
libc.src.__support.integer_literals
107107
libc.src.__support.CPP.optional
108+
libc.src.__support.integer_literals
109+
libc.src.__support.macros.properties.types
108110
)
109111

110112
add_libc_test(

libc/test/src/__support/CPP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_libc_test(
88
bit_test.cpp
99
DEPENDS
1010
libc.src.__support.CPP.bit
11+
libc.src.__support.macros.properties.types
1112
libc.src.__support.uint
1213
)
1314

@@ -49,6 +50,7 @@ add_libc_test(
4950
limits_test.cpp
5051
DEPENDS
5152
libc.src.__support.CPP.limits
53+
libc.src.__support.macros.properties.types
5254
libc.src.__support.uint
5355
)
5456

libc/test/src/__support/CPP/bit_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/__support/CPP/bit.h"
1010
#include "src/__support/UInt.h"
11+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1112
#include "test/UnitTest/Test.h"
1213

1314
#include <stdint.h>
@@ -17,7 +18,7 @@ namespace LIBC_NAMESPACE::cpp {
1718
using UnsignedTypes =
1819
testing::TypeList<unsigned char, unsigned short, unsigned int,
1920
unsigned long, unsigned long long,
20-
#if defined(__SIZEOF_INT128__)
21+
#ifdef LIBC_TYPES_HAS_INT128
2122
__uint128_t,
2223
#endif
2324
cpp::UInt<128>>;

libc/test/src/__support/CPP/limits_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/__support/CPP/limits.h"
1010
#include "src/__support/UInt.h"
11+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1112
#include "test/UnitTest/Test.h"
1213

1314
namespace LIBC_NAMESPACE {
@@ -37,9 +38,9 @@ TEST(LlvmLibcLimitsTest, UInt128Limits) {
3738
LIBC_NAMESPACE::cpp::UInt<128>(cpp::numeric_limits<uint64_t>::max());
3839
EXPECT_GT(umax128, umax64);
3940
ASSERT_EQ(~LIBC_NAMESPACE::cpp::UInt<128>(0), umax128);
40-
#ifdef __SIZEOF_INT128__
41+
#ifdef LIBC_TYPES_HAS_INT128
4142
ASSERT_EQ(~__uint128_t(0), cpp::numeric_limits<__uint128_t>::max());
42-
#endif
43+
#endif // LIBC_TYPES_HAS_INT128
4344
}
4445

4546
} // namespace LIBC_NAMESPACE

libc/test/src/__support/integer_literals_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "src/__support/integer_literals.h"
11+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
1112
#include "test/UnitTest/Test.h"
1213

1314
using LIBC_NAMESPACE::operator""_u8;
@@ -66,7 +67,7 @@ TEST(LlvmLibcIntegerLiteralTest, u64) {
6667
}
6768

6869
TEST(LlvmLibcIntegerLiteralTest, u128) {
69-
#if defined(__SIZEOF_INT128__)
70+
#ifdef LIBC_TYPES_HAS_INT128
7071
const __uint128_t ZERO = 0;
7172
const __uint128_t U8_MAX = UINT8_MAX;
7273
const __uint128_t U16_MAX = UINT16_MAX;
@@ -80,7 +81,7 @@ TEST(LlvmLibcIntegerLiteralTest, u128) {
8081
const UInt128 U32_MAX = UINT32_MAX;
8182
const UInt128 U64_MAX = UINT64_MAX;
8283
const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX;
83-
#endif
84+
#endif // LIBC_TYPES_HAS_INT128
8485
EXPECT_EQ(ZERO, 0_u128);
8586
EXPECT_EQ(U8_MAX, 255_u128);
8687
EXPECT_EQ(U8_MAX, 0xFF_u128);

0 commit comments

Comments
 (0)