[libc-commits] [libc] [libc] Make BigInt bit_cast-able to compatible types (PR #75063)
Clement Courbet via libc-commits
libc-commits at lists.llvm.org
Wed Dec 20 02:32:39 PST 2023
================
@@ -952,6 +952,49 @@ struct make_signed<UInt<Bits>> : type_identity<Int<Bits>> {
"Number of bits in Int should be a multiple of 64.");
};
+namespace internal {
+template <typename T> struct is_custom_uint : cpp::false_type {};
+template <size_t Bits> struct is_custom_uint<UInt<Bits>> : cpp::true_type {};
+} // namespace internal
+
+// bit_cast to UInt
+// Note: The standard scheme for SFINAE selection is to have exactly one
+// function instanciation valid at a time. This is usually done by having a
+// predicate in one function and the negated predicate in the other one.
+// e.g.
+// template<typename = cpp::enable_if_t< is_custom_uint<To>::value == true> ...
+// template<typename = cpp::enable_if_t< is_custom_uint<To>::value == false> ...
+//
+// Unfortunately this would make the default 'cpp::bit_cast' aware of
+// 'is_custom_uint' (or any other customization). To prevent exposing all
+// customizations in the original function, we create a different function with
+// four 'typename's instead of three - otherwise it would be considered as a
+// redeclaration of the same function leading to "error: template parameter
+// redefines default argument".
+template <typename To, typename From,
+ typename = cpp::enable_if_t<sizeof(To) == sizeof(From) &&
+ cpp::is_trivially_copyable<To>::value &&
+ cpp::is_trivially_copyable<From>::value>,
+ typename = cpp::enable_if_t<internal::is_custom_uint<To>::value>>
+LIBC_INLINE constexpr To bit_cast(const From &from) {
+ To out;
+ cpp::memcpy_inline<sizeof(out)>(out.val, &from);
----------------
legrosbuffle wrote:
I would suggest changing `BigInt` to make the storage a value type rather than an array type:
```
template <size_t Bits, bool Signed>
struct BigInt {
struct Storage {
uint64_t words[WORDCOUNT];
};
Storage val{};
};
```
Then all storage manipulations can be done with value semantics.
In particular this can be written `out.val = bit_cast<Storage>(from);`
https://github.com/llvm/llvm-project/pull/75063
More information about the libc-commits
mailing list