From: <man...@us...> - 2013-06-14 10:18:25
|
Revision: 2366 http://sourceforge.net/p/modplug/code/2366 Author: manxorist Date: 2013-06-14 10:18:18 +0000 (Fri, 14 Jun 2013) Log Message: ----------- [Ref] Add DecodeFloat?E and EncodeFloat?E to aid conversion between float and its endianess dependend bit pattern. [Ref] Add test cases that check for general float bit pattern sanity. Modified Paths: -------------- trunk/OpenMPT/common/typedefs.h trunk/OpenMPT/mptrack/VstPresets.cpp trunk/OpenMPT/soundlib/Endianness.h trunk/OpenMPT/soundlib/FileReader.h trunk/OpenMPT/test/test.cpp Modified: trunk/OpenMPT/common/typedefs.h =================================================================== --- trunk/OpenMPT/common/typedefs.h 2013-06-14 07:21:12 UTC (rev 2365) +++ trunk/OpenMPT/common/typedefs.h 2013-06-14 10:18:18 UTC (rev 2366) @@ -241,6 +241,18 @@ #define int24_min (0-0x00800000) #define int24_max (0+0x007fffff) +struct uint8_4 +{ + uint8 x[4]; + uint8_4() { } + uint8_4(uint8 a, uint8 b, uint8 c, uint8 d) { x[0] = a; x[1] = b; x[2] = c; x[3] = d; } + uint32 GetBE() const { return (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | (x[3] << 0); } + uint32 GetLE() const { return (x[0] << 0) | (x[1] << 8) | (x[2] << 16) | (x[3] << 24); } + uint8_4 & SetBE(uint32 y) { x[0] = (y >> 24)&0xff; x[1] = (y >> 16)&0xff; x[2] = (y >> 8)&0xff; x[3] = (y >> 0)&0xff; return *this; } + uint8_4 & SetLE(uint32 y) { x[0] = (y >> 0)&0xff; x[1] = (y >> 8)&0xff; x[2] = (y >> 16)&0xff; x[3] = (y >> 24)&0xff; return *this; } +}; +STATIC_ASSERT(sizeof(uint8_4) == 4); + typedef float float32; STATIC_ASSERT(sizeof(float32) == 4); Modified: trunk/OpenMPT/mptrack/VstPresets.cpp =================================================================== --- trunk/OpenMPT/mptrack/VstPresets.cpp 2013-06-14 07:21:12 UTC (rev 2365) +++ trunk/OpenMPT/mptrack/VstPresets.cpp 2013-06-14 10:18:18 UTC (rev 2366) @@ -245,9 +245,7 @@ void VSTPresets::WriteBE(float v, std::ostream &f) //------------------------------------------------ { - FloatInt32 u; - u.f = v; - WriteBE(u.i, f); + Write(EncodeFloatBE(v), f); } Modified: trunk/OpenMPT/soundlib/Endianness.h =================================================================== --- trunk/OpenMPT/soundlib/Endianness.h 2013-06-14 07:21:12 UTC (rev 2365) +++ trunk/OpenMPT/soundlib/Endianness.h 2013-06-14 10:18:18 UTC (rev 2366) @@ -96,3 +96,48 @@ #undef bswap16 #undef bswap32 + +static forceinline float DecodeFloatNE(uint32 i) +{ + FloatInt32 conv; + conv.i = i; + return conv.f; +} +static forceinline uint32 EncodeFloatNE(float f) +{ + FloatInt32 conv; + conv.f = f; + return conv.i; +} +static forceinline float DecodeFloatBE(uint8_4 x) +{ + #if defined(MPT_PLATFORM_FLIPPED_FLOAT_ENDIAN) + return DecodeFloatNE(x.GetLE()); + #else + return DecodeFloatNE(x.GetBE()); + #endif +} +static forceinline uint8_4 EncodeFloatBE(float f) +{ + #if defined(MPT_PLATFORM_FLIPPED_FLOAT_ENDIAN) + return uint8_4().SetLE(EncodeFloatNE(f)); + #else + return uint8_4().SetBE(EncodeFloatNE(f)); + #endif +} +static forceinline float DecodeFloatLE(uint8_4 x) +{ + #if defined(MPT_PLATFORM_FLIPPED_FLOAT_ENDIAN) + return DecodeFloatNE(x.GetBE()); + #else + return DecodeFloatNE(x.GetLE()); + #endif +} +static forceinline uint8_4 EncodeFloatLE(float f) +{ + #if defined(MPT_PLATFORM_FLIPPED_FLOAT_ENDIAN) + return uint8_4().SetBE(EncodeFloatNE(f)); + #else + return uint8_4().SetLE(EncodeFloatNE(f)); + #endif +} Modified: trunk/OpenMPT/soundlib/FileReader.h =================================================================== --- trunk/OpenMPT/soundlib/FileReader.h 2013-06-14 07:21:12 UTC (rev 2365) +++ trunk/OpenMPT/soundlib/FileReader.h 2013-06-14 10:18:18 UTC (rev 2366) @@ -728,11 +728,10 @@ // If successful, the file cursor is advanced by the size of the float. float ReadFloatLE() { - FloatInt32 target; + uint8_4 target; if(Read(target)) { - SwapBytesLE(target.i); - return target.f; + return DecodeFloatLE(target); } else { return 0.0f; @@ -743,11 +742,10 @@ // If successful, the file cursor is advanced by the size of the float. float ReadFloatBE() { - FloatInt32 target; + uint8_4 target; if(Read(target)) { - SwapBytesBE(target.i); - return target.f; + return DecodeFloatBE(target); } else { return 0.0f; Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2013-06-14 07:21:12 UTC (rev 2365) +++ trunk/OpenMPT/test/test.cpp 2013-06-14 10:18:18 UTC (rev 2366) @@ -423,9 +423,38 @@ } +static float AsFloat(uint32 x) +//---------------------------- +{ + FloatInt32 conv; + conv.i = x; + return conv.f; +} + +static uint32 AsInt(float x) +//-------------------------- +{ + FloatInt32 conv; + conv.f = x; + return conv.i; +} + void TestMisc() //------------- { + + VERIFY_EQUAL(AsFloat(0x3f800000u), 1.0f); + VERIFY_EQUAL(AsFloat(0x00000000u), 0.0f); + VERIFY_EQUAL(AsFloat(0xbf800000u), -1.0f); + VERIFY_EQUAL(DecodeFloatNE(0x3f800000u), 1.0f); + VERIFY_EQUAL(DecodeFloatLE(uint8_4(0x00,0x00,0x80,0x3f)), 1.0f); + VERIFY_EQUAL(DecodeFloatBE(uint8_4(0x3f,0x80,0x00,0x00)), 1.0f); + VERIFY_EQUAL(EncodeFloatNE(1.0f), 0x3f800000u); + VERIFY_EQUAL(EncodeFloatBE(1.0f).GetBE(), 0x3f800000u); + VERIFY_EQUAL(EncodeFloatLE(1.0f).GetBE(), 0x0000803fu); + VERIFY_EQUAL(EncodeFloatLE(1.0f).GetLE(), 0x3f800000u); + VERIFY_EQUAL(EncodeFloatBE(1.0f).GetLE(), 0x0000803fu); + VERIFY_EQUAL(ConvertStrTo<uint32>("586"), 586); VERIFY_EQUAL(ConvertStrTo<uint32>("2147483647"), (uint32)int32_max); VERIFY_EQUAL(ConvertStrTo<uint32>("4294967295"), uint32_max); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |