Fix TODO: Use std::bit_cast or __builtin_bit_cast if available.

This commit is contained in:
Rasmus Munk Larsen 2025-08-06 19:00:08 +00:00
parent 4be7e6b4e0
commit 975a5aba4f

View File

@ -95,9 +95,19 @@ struct default_max_digits10_impl<T, false, true> // Integer
} // end namespace internal } // end namespace internal
namespace numext { namespace numext {
/** \internal bit-wise cast without changing the underlying bit representation. */
// TODO: Replace by std::bit_cast (available in C++20) /** \internal bit-wise cast without changing the underlying bit representation. */
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) { return std::bit_cast<Tgt>(src); }
#elif EIGEN_HAS_BUILTIN(__builtin_bit_cast)
template <typename Tgt, typename Src>
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC constexpr Tgt bit_cast(const Src& src) {
EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED)
EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value, THIS_TYPE_IS_NOT_SUPPORTED)
EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED)
return __builtin_bit_cast(Tgt, src);
}
#else
template <typename Tgt, typename Src> template <typename Tgt, typename Src>
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) { EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
// The behaviour of memcpy is not specified for non-trivially copyable types // The behaviour of memcpy is not specified for non-trivially copyable types
@ -113,6 +123,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
memcpy(static_cast<void*>(&tgt), static_cast<const void*>(&staged), sizeof(Tgt)); memcpy(static_cast<void*>(&tgt), static_cast<const void*>(&staged), sizeof(Tgt));
return tgt; return tgt;
} }
#endif
} // namespace numext } // namespace numext
// clang-format off // clang-format off