Browse Source
Merge pull request #4952 from ReinUsesLisp/bit-cast
common/bit_cast: Add function matching std::bit_cast without constexpr
pull/15/merge
LC
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
23 additions and
0 deletions
-
src/common/CMakeLists.txt
-
src/common/bit_cast.h
|
|
|
@ -102,6 +102,7 @@ add_library(common STATIC |
|
|
|
atomic_ops.h |
|
|
|
detached_tasks.cpp |
|
|
|
detached_tasks.h |
|
|
|
bit_cast.h |
|
|
|
bit_field.h |
|
|
|
bit_util.h |
|
|
|
cityhash.cpp |
|
|
|
|
|
|
|
@ -0,0 +1,22 @@ |
|
|
|
// Copyright 2020 yuzu emulator team |
|
|
|
// Licensed under GPLv2 or any later version |
|
|
|
// Refer to the license.txt file included. |
|
|
|
|
|
|
|
#pragma once |
|
|
|
|
|
|
|
#include <cstring> |
|
|
|
#include <type_traits> |
|
|
|
|
|
|
|
namespace Common { |
|
|
|
|
|
|
|
template <typename To, typename From> |
|
|
|
[[nodiscard]] std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> && |
|
|
|
std::is_trivially_copyable_v<To>, |
|
|
|
To> |
|
|
|
BitCast(const From& src) noexcept { |
|
|
|
To dst; |
|
|
|
std::memcpy(&dst, &src, sizeof(To)); |
|
|
|
return dst; |
|
|
|
} |
|
|
|
|
|
|
|
} // namespace Common |