summaryrefslogtreecommitdiff
path: root/numpy/core/src/common/utils.hpp
blob: f847cab44832d4a0ad81f159c2724fea2c3e258d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef NUMPY_CORE_SRC_COMMON_UTILS_HPP
#define NUMPY_CORE_SRC_COMMON_UTILS_HPP

#include "npdef.hpp"

#if NP_HAS_CPP20
    #include <bit>
#endif

#include <type_traits>
#include <string.h>

namespace np {

/** Create a value of type `To` from the bits of `from`.
 *
 * similar to `std::bit_cast` but compatible with C++17,
 * should perform similar to `*reinterpret_cast<To*>(&from)`
 * or through punning without expecting any undefined behaviors.
 */
template<typename To, typename From>
#if NP_HAS_BUILTIN(__builtin_bit_cast) || NP_HAS_CPP20
[[nodiscard]] constexpr
#else
inline
#endif
To BitCast(const From &from) noexcept
{
    static_assert(
        sizeof(To) == sizeof(From),
        "both data types must have the same size");

    static_assert(
        std::is_trivially_copyable_v<To> &&
        std::is_trivially_copyable_v<From>,
        "both data types must be trivially copyable");

#if NP_HAS_CPP20
    return std::bit_cast<To>(from);
#elif NP_HAS_BUILTIN(__builtin_bit_cast)
    return __builtin_bit_cast(To, from);
#else
    To to;
    memcpy(&to, &from, sizeof(from));
    return to;
#endif
}

} // namespace np
#endif // NUMPY_CORE_SRC_COMMON_UTILS_HPP