Skip to content

Commit cdbc3ab

Browse files
committed
[libc] Add support for cpp::make_signed
1 parent eb2558c commit cdbc3ab

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

libc/src/__support/CPP/type_traits.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ template <> struct make_unsigned<__uint128_t> : type_identity<__uint128_t> {};
163163
#endif
164164
template <typename T> using make_unsigned_t = typename make_unsigned<T>::type;
165165

166+
template <typename T> struct make_signed;
167+
template <> struct make_signed<char> : type_identity<char> {};
168+
template <> struct make_signed<signed char> : type_identity<char> {};
169+
template <> struct make_signed<short> : type_identity<short> {};
170+
template <> struct make_signed<int> : type_identity<int> {};
171+
template <> struct make_signed<long> : type_identity<long> {};
172+
template <> struct make_signed<long long> : type_identity<long long> {};
173+
template <> struct make_signed<unsigned char> : type_identity<char> {};
174+
template <> struct make_signed<unsigned short> : type_identity<short> {};
175+
template <> struct make_signed<unsigned int> : type_identity<int> {};
176+
template <> struct make_signed<unsigned long> : type_identity<long> {};
177+
template <>
178+
struct make_signed<unsigned long long> : type_identity<long long> {};
179+
#ifdef __SIZEOF_INT128__
180+
template <> struct make_signed<__int128_t> : type_identity<__int128_t> {};
181+
template <> struct make_signed<__uint128_t> : type_identity<__int128_t> {};
182+
#endif
183+
template <typename T> using make_signed_t = typename make_signed<T>::type;
184+
166185
// Compile time type selection.
167186
template <bool B, typename T, typename F>
168187
struct conditional : type_identity<T> {};

libc/src/__support/UInt.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,24 @@ struct make_unsigned<Int<Bits>> : type_identity<UInt<Bits>> {
909909
"Number of bits in Int should be a multiple of 64.");
910910
};
911911

912+
template <size_t Bits>
913+
struct make_unsigned<UInt<Bits>> : type_identity<UInt<Bits>> {
914+
static_assert(Bits > 0 && Bits % 64 == 0,
915+
"Number of bits in Int should be a multiple of 64.");
916+
};
917+
918+
template <size_t Bits>
919+
struct make_signed<Int<Bits>> : type_identity<Int<Bits>> {
920+
static_assert(Bits > 0 && Bits % 64 == 0,
921+
"Number of bits in Int should be a multiple of 64.");
922+
};
923+
924+
template <size_t Bits>
925+
struct make_signed<UInt<Bits>> : type_identity<Int<Bits>> {
926+
static_assert(Bits > 0 && Bits % 64 == 0,
927+
"Number of bits in Int should be a multiple of 64.");
928+
};
929+
912930
} // namespace __llvm_libc::cpp
913931

914932
#endif // LLVM_LIBC_SRC_SUPPORT_UINT_H

0 commit comments

Comments
 (0)