From aef463010282d2daac4ce4aead1f20a8fa588ae3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 8 May 2016 23:11:59 -0400 Subject: [PATCH 1/3] swap: Remove unused methods Also gets rid of pointer data variants as this prevents the use of the regular swapping routines as unary predicates in std lib functions. They also cast to stricter alignment types, which is undefined behavior. --- src/common/swap.h | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/common/swap.h b/src/common/swap.h index a7c37bc44f..f51751d29a 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -58,9 +58,6 @@ namespace Common { -inline u8 swap8(u8 _data) {return _data;} -inline u32 swap24(const u8* _data) {return (_data[0] << 16) | (_data[1] << 8) | _data[2];} - #ifdef _MSC_VER inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);} inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);} @@ -115,31 +112,6 @@ inline double swapd(double f) { return dat2.f; } -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); -} - } // Namespace Common From 47ca79ba4bc7e5c18cf9427c975a789efd65a414 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 8 May 2016 23:21:44 -0400 Subject: [PATCH 2/3] swap: Get rid of undefined behavior in swapf and swapd This isn't well-defined in C++. --- src/common/swap.h | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/common/swap.h b/src/common/swap.h index f51751d29a..beedd6c7ed 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -25,6 +25,8 @@ #include #endif +#include + #include "common/common_types.h" // GCC 4.6+ @@ -89,27 +91,29 @@ inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 3 #endif inline float swapf(float f) { - union { - float f; - unsigned int u32; - } dat1, dat2; + static_assert(sizeof(u32) == sizeof(float), + "float must be the same size as uint32_t."); - dat1.f = f; - dat2.u32 = swap32(dat1.u32); + u32 value; + std::memcpy(&value, &f, sizeof(u32)); - return dat2.f; + value = swap32(value); + std::memcpy(&f, &value, sizeof(u32)); + + return f; } inline double swapd(double f) { - union { - double f; - unsigned long long u64; - } dat1, dat2; + static_assert(sizeof(u64) == sizeof(double), + "double must be the same size as uint64_t."); - dat1.f = f; - dat2.u64 = swap64(dat1.u64); + u64 value; + std::memcpy(&value, &f, sizeof(u64)); - return dat2.f; + value = swap64(value); + std::memcpy(&f, &value, sizeof(u64)); + + return f; } } // Namespace Common From d5b983a8c000533ca17b727595e813113cd9d54c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 8 May 2016 23:33:46 -0400 Subject: [PATCH 3/3] swap: Get rid of pointer casting for swapping structs These shouldn't haphazardly convert types --- src/common/swap.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/common/swap.h b/src/common/swap.h index beedd6c7ed..1749bd7a46 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -510,35 +510,35 @@ bool operator==(const S &p, const swap_struct_t v) { template struct swap_64_t { static T swap(T x) { - return (T)Common::swap64(*(u64 *)&x); + return static_cast(Common::swap64(x)); } }; template struct swap_32_t { static T swap(T x) { - return (T)Common::swap32(*(u32 *)&x); + return static_cast(Common::swap32(x)); } }; template struct swap_16_t { static T swap(T x) { - return (T)Common::swap16(*(u16 *)&x); + return static_cast(Common::swap16(x)); } }; template struct swap_float_t { static T swap(T x) { - return (T)Common::swapf(*(float *)&x); + return static_cast(Common::swapf(x)); } }; template struct swap_double_t { static T swap(T x) { - return (T)Common::swapd(*(double *)&x); + return static_cast(Common::swapd(x)); } };