diff options
| author | 2023-03-07 19:53:32 -0500 | |
|---|---|---|
| committer | 2023-03-07 20:26:56 -0500 | |
| commit | 64dcb40db139ce1aa79dff16ce863a8d5389199f (patch) | |
| tree | 4b58da704b35a1a221f5d83a8d0cf8dce495a3f1 /src | |
| parent | Merge pull request #9889 from Morph1984/time-is-ticking (diff) | |
| download | yuzu-64dcb40db139ce1aa79dff16ce863a8d5389199f.tar.gz yuzu-64dcb40db139ce1aa79dff16ce863a8d5389199f.tar.xz yuzu-64dcb40db139ce1aa79dff16ce863a8d5389199f.zip | |
common: make BitCast constexpr
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/bit_cast.h | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/common/bit_cast.h b/src/common/bit_cast.h index 535148b4d..c6110c542 100644 --- a/src/common/bit_cast.h +++ b/src/common/bit_cast.h | |||
| @@ -3,19 +3,21 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <cstring> | 6 | #include <version> |
| 7 | #include <type_traits> | 7 | |
| 8 | #ifdef __cpp_lib_bit_cast | ||
| 9 | #include <bit> | ||
| 10 | #endif | ||
| 8 | 11 | ||
| 9 | namespace Common { | 12 | namespace Common { |
| 10 | 13 | ||
| 11 | template <typename To, typename From> | 14 | template <typename To, typename From> |
| 12 | [[nodiscard]] std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> && | 15 | constexpr inline To BitCast(const From& from) { |
| 13 | std::is_trivially_copyable_v<To>, | 16 | #ifdef __cpp_lib_bit_cast |
| 14 | To> | 17 | return std::bit_cast<To>(from); |
| 15 | BitCast(const From& src) noexcept { | 18 | #else |
| 16 | To dst; | 19 | return __builtin_bit_cast(To, from); |
| 17 | std::memcpy(&dst, &src, sizeof(To)); | 20 | #endif |
| 18 | return dst; | ||
| 19 | } | 21 | } |
| 20 | 22 | ||
| 21 | } // namespace Common | 23 | } // namespace Common |