mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-09 10:19:01 +08:00
Fix TODO: Use std::bit_cast or __builtin_bit_cast if available.
This commit is contained in:
parent
4be7e6b4e0
commit
975a5aba4f
@ -95,9 +95,19 @@ struct default_max_digits10_impl<T, false, true> // Integer
|
||||
} // end namespace internal
|
||||
|
||||
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>
|
||||
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
|
||||
// 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));
|
||||
return tgt;
|
||||
}
|
||||
#endif
|
||||
} // namespace numext
|
||||
|
||||
// clang-format off
|
||||
|
Loading…
x
Reference in New Issue
Block a user