MCPcopy Create free account
hub / github.com/numpy/numpy / BitCast

Function BitCast

numpy/core/src/common/utils.hpp:23–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21template<typename To, typename From>
22#if NP_HAS_BUILTIN(__builtin_bit_cast) || NP_HAS_CPP20
23[[nodiscard]] constexpr
24#else
25inline
26#endif
27To 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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected