bit_cast.h 574 B

12345678910111213141516171819202122
  1. // Copyright 2020 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstring>
  6. #include <type_traits>
  7. namespace Common {
  8. template <typename To, typename From>
  9. [[nodiscard]] std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
  10. std::is_trivially_copyable_v<To>,
  11. To>
  12. BitCast(const From& src) noexcept {
  13. To dst;
  14. std::memcpy(&dst, &src, sizeof(To));
  15. return dst;
  16. }
  17. } // namespace Common