Merge pull request #3008 from Ced2911/swap_fixes

fix swap type
This commit is contained in:
Henrik Rydgård
2013-07-30 11:20:15 -07:00
2 changed files with 49 additions and 8 deletions
+27
View File
@@ -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
+22 -8
View File
@@ -469,6 +469,20 @@ struct swap_16_t {
}
};
template <typename T>
struct swap_float_t {
static T swap(T x) {
return (T)bswapf(*(float *)&x);
}
};
template <typename T>
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, swap_64_t<s64>> s64_be;
typedef swap_struct_t<u32, swap_32_t<u32>> u32_be;
typedef swap_struct_t<s32, swap_32_t<s32>> s32_be;
typedef swap_struct_t<u16, swap_16_t<u32>> u16_be;
typedef swap_struct_t<s16, swap_16_t<s32>> s16_be;
typedef swap_struct_t<u16, swap_16_t<u16>> u16_be;
typedef swap_struct_t<s16, swap_16_t<s16>> s16_be;
typedef swap_struct_t<float, swap_32_t<float> > float_be;
typedef swap_struct_t<double, swap_64_t<double> > double_be;
typedef swap_struct_t<float, swap_float_t<float> > float_be;
typedef swap_struct_t<double, swap_double_t<double> > double_be;
#else
typedef swap_struct_t<u64, swap_64_t<u64>> u64_le;
@@ -500,11 +514,11 @@ typedef swap_struct_t<s64, swap_64_t<s64>> s64_le;
typedef swap_struct_t<u32, swap_32_t<u32>> u32_le;
typedef swap_struct_t<s32, swap_32_t<s32>> s32_le;
typedef swap_struct_t<u16, swap_16_t<u32>> u16_le;
typedef swap_struct_t<s16, swap_16_t<s32>> s16_le;
typedef swap_struct_t<u16, swap_16_t<u16>> u16_le;
typedef swap_struct_t<s16, swap_16_t<s16>> s16_le;
typedef swap_struct_t<float, swap_32_t<float> > float_le;
typedef swap_struct_t<double, swap_64_t<double> > double_le;
typedef swap_struct_t<float, swap_float_t<float> > float_le;
typedef swap_struct_t<double, swap_double_t<double> > double_le;
typedef u32 u32_be;
typedef u16 u16_be;