| 21 | template<typename To, typename From> |
| 22 | #if NP_HAS_BUILTIN(__builtin_bit_cast) || NP_HAS_CPP20 |
| 23 | [[nodiscard]] constexpr |
| 24 | #else |
| 25 | inline |
| 26 | #endif |
| 27 | To BitCast(const From &from) noexcept |
| 28 | { |
| 29 | static_assert( |
| 30 | sizeof(To) == sizeof(From), |
| 31 | "both data types must have the same size"); |
| 32 | |
| 33 | static_assert( |
| 34 | std::is_trivially_copyable_v<To> && |
| 35 | std::is_trivially_copyable_v<From>, |
| 36 | "both data types must be trivially copyable"); |
| 37 | |
| 38 | #if NP_HAS_CPP20 |
| 39 | return std::bit_cast<To>(from); |
| 40 | #elif NP_HAS_BUILTIN(__builtin_bit_cast) |
| 41 | return __builtin_bit_cast(To, from); |
| 42 | #else |
| 43 | To to; |
| 44 | memcpy(&to, &from, sizeof(from)); |
| 45 | return to; |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | } // namespace np |
| 50 | #endif // NUMPY_CORE_SRC_COMMON_UTILS_HPP |
nothing calls this directly
no outgoing calls
no test coverage detected