From cc95cde45001c9440dc259a1c67eb2f315ff8244 Mon Sep 17 00:00:00 2001 From: Ced2911 Date: Tue, 30 Jul 2013 18:38:00 +0200 Subject: [PATCH] fix swap type --- Common/Common.h | 27 +++++++++++++++++++++++++++ Common/Swap.h | 30 ++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Common/Common.h b/Common/Common.h index e12d240a4c..5dafca92bc 100644 --- a/Common/Common.h +++ b/Common/Common.h @@ -185,6 +185,33 @@ inline unsigned int bswap32(unsigned int x) { return (x >> 24) | ((x & 0xFF0000) inline unsigned long long bswap64(unsigned long long x) {return ((unsigned long long)bswap32(x) << 32) | bswap32(x >> 32); } #endif +inline float bswapf( float f ) +{ + union + { + float f; + unsigned int u32; + } dat1, dat2; + + dat1.f = f; + dat2.u32 = bswap32(dat1.u32); + + return dat2.f; +} + +inline double bswapd( double f ) +{ + union + { + double f; + unsigned long long u64; + } dat1, dat2; + + dat1.f = f; + dat2.u64 = bswap64(dat1.u64); + + return dat2.f; +} // Host communication. enum HOST_COMM diff --git a/Common/Swap.h b/Common/Swap.h index 021406e868..2becbc82ab 100644 --- a/Common/Swap.h +++ b/Common/Swap.h @@ -469,6 +469,20 @@ struct swap_16_t { } }; +template +struct swap_float_t { + static T swap(T x) { + return (T)bswapf(*(float *)&x); + } +}; + +template +struct swap_double_t { + static T swap(T x) { + return (T)bswapd(*(double *)&x); + } +}; + #if COMMON_LITTLE_ENDIAN typedef u32 u32_le; typedef u16 u16_le; @@ -487,11 +501,11 @@ typedef swap_struct_t> s64_be; typedef swap_struct_t> u32_be; typedef swap_struct_t> s32_be; -typedef swap_struct_t> u16_be; -typedef swap_struct_t> s16_be; +typedef swap_struct_t> u16_be; +typedef swap_struct_t> s16_be; -typedef swap_struct_t > float_be; -typedef swap_struct_t > double_be; +typedef swap_struct_t > float_be; +typedef swap_struct_t > double_be; #else typedef swap_struct_t> u64_le; @@ -500,11 +514,11 @@ typedef swap_struct_t> s64_le; typedef swap_struct_t> u32_le; typedef swap_struct_t> s32_le; -typedef swap_struct_t> u16_le; -typedef swap_struct_t> s16_le; +typedef swap_struct_t> u16_le; +typedef swap_struct_t> s16_le; -typedef swap_struct_t > float_le; -typedef swap_struct_t > double_le; +typedef swap_struct_t > float_le; +typedef swap_struct_t > double_le; typedef u32 u32_be; typedef u16 u16_be;