[libc-commits] [libc] cdbc3ab - [libc] Add support for cpp::make_signed

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Thu Aug 3 06:45:17 PDT 2023


Author: Guillaume Chatelet
Date: 2023-08-03T13:45:04Z
New Revision: cdbc3ab7d644533e89e5045b02ddae5608f29828

URL: https://github.com/llvm/llvm-project/commit/cdbc3ab7d644533e89e5045b02ddae5608f29828
DIFF: https://github.com/llvm/llvm-project/commit/cdbc3ab7d644533e89e5045b02ddae5608f29828.diff

LOG: [libc] Add support for cpp::make_signed

Added: 
    

Modified: 
    libc/src/__support/CPP/type_traits.h
    libc/src/__support/UInt.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index 5808e92f6816a5..1b58c95cf29fdd 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -163,6 +163,25 @@ template <> struct make_unsigned<__uint128_t> : type_identity<__uint128_t> {};
 #endif
 template <typename T> using make_unsigned_t = typename make_unsigned<T>::type;
 
+template <typename T> struct make_signed;
+template <> struct make_signed<char> : type_identity<char> {};
+template <> struct make_signed<signed char> : type_identity<char> {};
+template <> struct make_signed<short> : type_identity<short> {};
+template <> struct make_signed<int> : type_identity<int> {};
+template <> struct make_signed<long> : type_identity<long> {};
+template <> struct make_signed<long long> : type_identity<long long> {};
+template <> struct make_signed<unsigned char> : type_identity<char> {};
+template <> struct make_signed<unsigned short> : type_identity<short> {};
+template <> struct make_signed<unsigned int> : type_identity<int> {};
+template <> struct make_signed<unsigned long> : type_identity<long> {};
+template <>
+struct make_signed<unsigned long long> : type_identity<long long> {};
+#ifdef __SIZEOF_INT128__
+template <> struct make_signed<__int128_t> : type_identity<__int128_t> {};
+template <> struct make_signed<__uint128_t> : type_identity<__int128_t> {};
+#endif
+template <typename T> using make_signed_t = typename make_signed<T>::type;
+
 // Compile time type selection.
 template <bool B, typename T, typename F>
 struct conditional : type_identity<T> {};

diff  --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 2b187586c3ba4c..ffb3a12e2a8d34 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -909,6 +909,24 @@ struct make_unsigned<Int<Bits>> : type_identity<UInt<Bits>> {
                 "Number of bits in Int should be a multiple of 64.");
 };
 
+template <size_t Bits>
+struct make_unsigned<UInt<Bits>> : type_identity<UInt<Bits>> {
+  static_assert(Bits > 0 && Bits % 64 == 0,
+                "Number of bits in Int should be a multiple of 64.");
+};
+
+template <size_t Bits>
+struct make_signed<Int<Bits>> : type_identity<Int<Bits>> {
+  static_assert(Bits > 0 && Bits % 64 == 0,
+                "Number of bits in Int should be a multiple of 64.");
+};
+
+template <size_t Bits>
+struct make_signed<UInt<Bits>> : type_identity<Int<Bits>> {
+  static_assert(Bits > 0 && Bits % 64 == 0,
+                "Number of bits in Int should be a multiple of 64.");
+};
+
 } // namespace __llvm_libc::cpp
 
 #endif // LLVM_LIBC_SRC_SUPPORT_UINT_H


        


More information about the libc-commits mailing list