Skip to content

Commit 23c397c

Browse files
authored
[libc] Provide LIBC_TYPES_HAS_INT128 (#84149)
Umbrella bug #83182
1 parent 01e5d46 commit 23c397c

File tree

22 files changed

+72
-36
lines changed

22 files changed

+72
-36
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ add_header_library(
205205
libc.src.__support.CPP.bit
206206
libc.src.__support.CPP.type_traits
207207
libc.src.__support.macros.optimization
208+
libc.src.__support.macros.properties.types
208209
)
209210

210211
add_header_library(
@@ -213,6 +214,7 @@ add_header_library(
213214
UInt128.h
214215
DEPENDS
215216
.uint
217+
libc.src.__support.macros.properties.types
216218
)
217219

218220
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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#include "src/__support/CPP/limits.h"
1515
#include "src/__support/CPP/optional.h"
1616
#include "src/__support/CPP/type_traits.h"
17-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
18-
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
19-
#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
17+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
18+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
19+
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
20+
#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
2021
#include "src/__support/number_pair.h"
2122

2223
#include <stddef.h> // For size_t
@@ -30,9 +31,9 @@ template <typename T> struct half_width;
3031
template <> struct half_width<uint64_t> : cpp::type_identity<uint32_t> {};
3132
template <> struct half_width<uint32_t> : cpp::type_identity<uint16_t> {};
3233
template <> struct half_width<uint16_t> : cpp::type_identity<uint8_t> {};
33-
#ifdef __SIZEOF_INT128__
34+
#ifdef LIBC_TYPES_HAS_INT128
3435
template <> struct half_width<__uint128_t> : cpp::type_identity<uint64_t> {};
35-
#endif // __SIZEOF_INT128__
36+
#endif // LIBC_TYPES_HAS_INT128
3637

3738
template <typename T> using half_width_t = typename half_width<T>::type;
3839

@@ -69,7 +70,7 @@ LIBC_INLINE constexpr NumberPair<uint32_t> full_mul<uint32_t>(uint32_t a,
6970
return result;
7071
}
7172

72-
#ifdef __SIZEOF_INT128__
73+
#ifdef LIBC_TYPES_HAS_INT128
7374
template <>
7475
LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
7576
uint64_t b) {
@@ -79,7 +80,7 @@ LIBC_INLINE constexpr NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a,
7980
result.hi = uint64_t(prod >> 64);
8081
return result;
8182
}
82-
#endif // __SIZEOF_INT128__
83+
#endif // LIBC_TYPES_HAS_INT128
8384

8485
} // namespace internal
8586

@@ -682,7 +683,7 @@ struct BigInt {
682683
val[1] = uint32_t(tmp >> 32);
683684
return;
684685
}
685-
#ifdef __SIZEOF_INT128__
686+
#ifdef LIBC_TYPES_HAS_INT128
686687
if constexpr ((Bits == 128) && (WORD_SIZE == 64)) {
687688
// Use builtin 128 bits if available;
688689
if (s >= 128) {
@@ -696,7 +697,7 @@ struct BigInt {
696697
val[1] = uint64_t(tmp >> 64);
697698
return;
698699
}
699-
#endif // __SIZEOF_INT128__
700+
#endif // LIBC_TYPES_HAS_INT128
700701
if (LIBC_UNLIKELY(s == 0))
701702
return;
702703

@@ -753,7 +754,7 @@ struct BigInt {
753754
val[1] = uint32_t(tmp >> 32);
754755
return;
755756
}
756-
#ifdef __SIZEOF_INT128__
757+
#ifdef LIBC_TYPES_HAS_INT128
757758
if constexpr ((Bits == 128) && (WORD_SIZE == 64)) {
758759
// Use builtin 128 bits if available;
759760
if (s >= 128) {
@@ -771,7 +772,7 @@ struct BigInt {
771772
val[1] = uint64_t(tmp >> 64);
772773
return;
773774
}
774-
#endif // __SIZEOF_INT128__
775+
#endif // LIBC_TYPES_HAS_INT128
775776

776777
if (LIBC_UNLIKELY(s == 0))
777778
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::UInt<128>;
1920
using Int128 = LIBC_NAMESPACE::Int<128>;
20-
#endif
21+
#endif // LIBC_TYPES_HAS_INT128
2122

2223
#endif // LLVM_LIBC_SRC___SUPPORT_UINT128_H

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.uint
7879
libc.src.__support.uint128

0 commit comments

Comments
 (0)