From 6a13a4e5ed10aac3b80e996f5c9e94d000f484b2 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 20 Feb 2013 20:20:53 -0600 Subject: [PATCH] Add template "swap" function to CommonFuncs. --- Source/Core/Common/Src/CommonFuncs.h | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Source/Core/Common/Src/CommonFuncs.h b/Source/Core/Common/Src/CommonFuncs.h index 585fe0999a..18dc40e8c8 100644 --- a/Source/Core/Common/Src/CommonFuncs.h +++ b/Source/Core/Common/Src/CommonFuncs.h @@ -162,6 +162,40 @@ inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);} inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);} inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);} +template +void swap(u8*); + +template <> +inline void swap<1>(u8* data) +{} + +template <> +inline void swap<2>(u8* data) +{ + *reinterpret_cast(data) = swap16(data); +} + +template <> +inline void swap<4>(u8* data) +{ + *reinterpret_cast(data) = swap32(data); +} + +template <> +inline void swap<8>(u8* data) +{ + *reinterpret_cast(data) = swap64(data); +} + +template +inline T FromBigEndian(T data) +{ + //static_assert(std::is_arithmetic::value, "function only makes sense with arithmetic types"); + + swap(reinterpret_cast(&data)); + return data; +} + } // Namespace Common #endif // _COMMONFUNCS_H_