summaryrefslogtreecommitdiff
path: root/Source/Core/Common
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Core/Common')
-rw-r--r--Source/Core/Common/BitUtils.h47
1 files changed, 4 insertions, 43 deletions
diff --git a/Source/Core/Common/BitUtils.h b/Source/Core/Common/BitUtils.h
index b20baa20f2..8b1b196c24 100644
--- a/Source/Core/Common/BitUtils.h
+++ b/Source/Core/Common/BitUtils.h
@@ -4,6 +4,7 @@
#pragma once
#include <array>
+#include <bit>
#include <climits>
#include <cstddef>
#include <cstdint>
@@ -166,50 +167,10 @@ inline auto BitCastPtr(PtrType* ptr) noexcept -> BitCastPtrType<T, PtrType>
}
// Similar to BitCastPtr, but specifically for aliasing structs to arrays.
-template <typename ArrayType, typename T,
- typename Container = std::array<ArrayType, sizeof(T) / sizeof(ArrayType)>>
-inline auto BitCastToArray(const T& obj) noexcept -> Container
+template <typename ValueType, typename From>
+[[nodiscard]] constexpr auto BitCastToArray(const From& obj) noexcept
{
- static_assert(sizeof(T) % sizeof(ArrayType) == 0,
- "Size of array type must be a factor of size of source type.");
- static_assert(std::is_trivially_copyable<T>(),
- "BitCastToArray source type must be trivially copyable.");
- static_assert(std::is_trivially_copyable<Container>(),
- "BitCastToArray array type must be trivially copyable.");
-
- Container result;
- std::memcpy(result.data(), &obj, sizeof(T));
- return result;
-}
-
-template <typename ArrayType, typename T,
- typename Container = std::array<ArrayType, sizeof(T) / sizeof(ArrayType)>>
-inline void BitCastFromArray(const Container& array, T& obj) noexcept
-{
- static_assert(sizeof(T) % sizeof(ArrayType) == 0,
- "Size of array type must be a factor of size of destination type.");
- static_assert(std::is_trivially_copyable<Container>(),
- "BitCastFromArray array type must be trivially copyable.");
- static_assert(std::is_trivially_copyable<T>(),
- "BitCastFromArray destination type must be trivially copyable.");
-
- std::memcpy(&obj, array.data(), sizeof(T));
-}
-
-template <typename ArrayType, typename T,
- typename Container = std::array<ArrayType, sizeof(T) / sizeof(ArrayType)>>
-inline auto BitCastFromArray(const Container& array) noexcept -> T
-{
- static_assert(sizeof(T) % sizeof(ArrayType) == 0,
- "Size of array type must be a factor of size of destination type.");
- static_assert(std::is_trivially_copyable<Container>(),
- "BitCastFromArray array type must be trivially copyable.");
- static_assert(std::is_trivially_copyable<T>(),
- "BitCastFromArray destination type must be trivially copyable.");
-
- T obj;
- std::memcpy(&obj, array.data(), sizeof(T));
- return obj;
+ return std::bit_cast<std::array<ValueType, sizeof(From) / sizeof(ValueType)>>(obj);
}
template <typename T>