mirror of
https://github.com/yuzu-mirror/yuzu.git
synced 2024-11-03 06:10:00 +00:00
Merge pull request #9920 from liamwhite/constexpr-bit-cast
common: make BitCast constexpr
This commit is contained in:
commit
b014fdacdb
1 changed files with 11 additions and 9 deletions
|
@ -3,19 +3,21 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstring>
|
#include <version>
|
||||||
#include <type_traits>
|
|
||||||
|
#ifdef __cpp_lib_bit_cast
|
||||||
|
#include <bit>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
template <typename To, typename From>
|
template <typename To, typename From>
|
||||||
[[nodiscard]] std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
|
constexpr inline To BitCast(const From& from) {
|
||||||
std::is_trivially_copyable_v<To>,
|
#ifdef __cpp_lib_bit_cast
|
||||||
To>
|
return std::bit_cast<To>(from);
|
||||||
BitCast(const From& src) noexcept {
|
#else
|
||||||
To dst;
|
return __builtin_bit_cast(To, from);
|
||||||
std::memcpy(&dst, &src, sizeof(To));
|
#endif
|
||||||
return dst;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
Loading…
Reference in a new issue