From 00771aed2daa3cd9463c76b2e52f355c7bd1547c Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 10 Apr 2015 18:12:42 -1000 Subject: [PATCH 1/2] inlined byte-swapping utilities --- include/Athena/Utility.hpp | 202 ++++++++++++++++++++++++++++++----- src/Athena/Utility.cpp | 213 ------------------------------------- 2 files changed, 174 insertions(+), 241 deletions(-) diff --git a/include/Athena/Utility.hpp b/include/Athena/Utility.hpp index 74c9edf..476cf1c 100644 --- a/include/Athena/Utility.hpp +++ b/include/Athena/Utility.hpp @@ -27,35 +27,181 @@ namespace Athena { namespace utility { -bool isEmpty(atInt8*, atUint32); - -atUint16 swapU16(atUint16 val ); -atInt16 swap16 (atInt16 val ); -atUint32 swapU32(atUint32 val); -atInt32 swap32 (atInt32 val ); -atUint64 swapU64(atUint64 val); -atInt64 swap64 (atInt64 val); -float swapFloat(float val); -double swapDouble(double val); -atInt16 LittleInt16(atInt16& val); -atUint16 LittleUint16(atUint16& val); -atInt16 BigInt16(atInt16& val); -atUint16 BigUint16(atUint16& val); -atInt32 LittleInt32(atInt32& val); -atUint32 LittleUint32(atUint32& val); -atInt32 BigInt32(atInt32& val); -atUint32 BigUint32(atUint32& val); -atInt64 LittleInt64(atInt64& val); -atUint64 LittleUint64(atUint64& val); -atInt64 BigInt64(atInt64& val); -atUint64 BigUint64(atUint64& val); - -float LittleFloat(float& val); -float BigFloat(float& val); -double LittleDouble(double& val); -double BigDouble(double& val); - +inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);} bool isSystemBigEndian(); + +inline atInt16 swap16 (atInt16 val) +{ +#if __GNUC__ + return __builtin_bswap16(val); +#elif _WIN32 + return _byteswap_ushort(val); +#else + return (val = (val << 8) | ((val >> 8) & 0xFF)); +#endif +} +inline atUint16 swapU16(atUint16 val) {return (atUint16)swap16(val);} +inline atInt32 swap32 (atInt32 val) +{ +#if __GNUC__ + return __builtin_bswap32(val); +#elif _WIN32 + return _byteswap_ulong(val); +#else + val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16; + val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8; + return val; +#endif +} +inline atUint32 swapU32(atUint32 val) {return (atUint32)swap32(val);} +inline atInt64 swap64 (atInt64 val) +{ +#if __GNUC__ + return __builtin_bswap64(val); +#elif _WIN32 + return _byteswap_uint64(val); +#else + return (val = ((atInt64)((((atInt64)(val) & 0xFF00000000000000ULL) >> 56) | + (((atInt64)(val) & 0x00FF000000000000ULL) >> 40) | + (((atInt64)(val) & 0x0000FF0000000000ULL) >> 24) | + (((atInt64)(val) & 0x000000FF00000000ULL) >> 8) | + (((atInt64)(val) & 0x00000000FF000000ULL) << 8) | + (((atInt64)(val) & 0x0000000000FF0000ULL) << 24) | + (((atInt64)(val) & 0x000000000000FF00ULL) << 40) | + (((atInt64)(val) & 0x00000000000000FFULL) << 56)))); +#endif +} +inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);} +inline float swapFloat(float val) +{ + atInt32 ival = swap32(*((atInt32*)(&val))); + return *((float*)(&ival)); +} +inline double swapDouble(double val) +{ + atInt64 ival = swap64(*((atInt64*)(&val))); + return *((double*)(&ival)); +} +inline atInt16 LittleInt16(atInt16& val) +{ + if (Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap16(val); + + return val; +} +inline atUint16 LittleUint16(atUint16& val) +{ + atInt16 ret = val; + LittleInt16(ret); + val = ret; + + return val; +} +inline atInt16 BigInt16(atInt16& val) +{ + if (!Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap16(val); + + return val; +} +inline atUint16 BigUint16(atUint16& val) +{ + atInt16 ret = val; + BigInt16(ret); + val = ret; + + return val; +} +inline atInt32 LittleInt32(atInt32& val) +{ + if (Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap32(val); + + return val; +} +inline atUint32 LittleUint32(atUint32& val) +{ + atInt32 ret = val; + LittleInt32(ret); + val = ret; + + return val; +} +inline atInt32 BigInt32(atInt32& val) +{ + if (!Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap32(val); + + return val; +} +inline atUint32 BigUint32(atUint32& val) +{ + atInt32 ret = val; + BigInt32(ret); + val = ret; + + return val; +} +inline atInt64 LittleInt64(atInt64& val) +{ + if (Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap64(val); + + return val; +} +inline atUint64 LittleUint64(atUint64& val) +{ + atInt64 ret = val; + LittleInt64(ret); + val = ret; + + return val; +} +inline atInt64 BigInt64(atInt64& val) +{ + if (!Athena::utility::isSystemBigEndian()) + val = Athena::utility::swap64(val); + + return val; +} +inline atUint64 BigUint64(atUint64& val) +{ + atInt64 ret = val; + BigInt64(ret); + val = ret; + + return val; +} + +inline float LittleFloat(float& val) +{ + if (Athena::utility::isSystemBigEndian()) + val = Athena::utility::swapFloat(val); + + return val; +} +inline float BigFloat(float& val) +{ + if (!Athena::utility::isSystemBigEndian()) + val = Athena::utility::swapFloat(val); + + return val; +} +inline double LittleDouble(double& val) +{ + if (Athena::utility::isSystemBigEndian()) + val = Athena::utility::swapDouble(val); + + return val; +} +inline double BigDouble(double& val) +{ + if (!Athena::utility::isSystemBigEndian()) + val = Athena::utility::swapDouble(val); + + return val; +} + void fillRandom(atUint8 * rndArea, atUint64 count); std::vector split(const std::string &s, char delim); std::string join(const std::vector& elems, const std::string& delims); diff --git a/src/Athena/Utility.cpp b/src/Athena/Utility.cpp index eca57f5..b5045a3 100644 --- a/src/Athena/Utility.cpp +++ b/src/Athena/Utility.cpp @@ -34,49 +34,6 @@ namespace Athena { namespace utility { -bool isEmpty(atInt8* buf, atUint32 size) -{ - return !memcmp(buf, buf + 1, size - 1); -} - -atUint16 swapU16(atUint16 val ) -{ - return (atUint16)swap16(val); -} - -atInt16 swap16(atInt16 val ) -{ - return (val = (val << 8) | ((val >> 8) & 0xFF)); -} - -atUint32 swapU32(atUint32 val) -{ - return (atUint32)swap32(val); -} - -int swap32(atInt32 val ) -{ - val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16; - val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8; - return val; -} - -atUint64 swapU64(atUint64 val) -{ - return (atUint64)swap64(val); -} - -atInt64 swap64(atInt64 val) -{ - return (val = ((atInt64)((((atInt64)(val) & 0xFF00000000000000ULL) >> 56) | - (((atInt64)(val) & 0x00FF000000000000ULL) >> 40) | - (((atInt64)(val) & 0x0000FF0000000000ULL) >> 24) | - (((atInt64)(val) & 0x000000FF00000000ULL) >> 8) | - (((atInt64)(val) & 0x00000000FF000000ULL) << 8) | - (((atInt64)(val) & 0x0000000000FF0000ULL) << 24) | - (((atInt64)(val) & 0x000000000000FF00ULL) << 40) | - (((atInt64)(val) & 0x00000000000000FFULL) << 56)))); -} bool isSystemBigEndian() { @@ -84,182 +41,12 @@ bool isSystemBigEndian() return (*(atUint16*)test == 0xFEFF); } - -atInt16 LittleInt16(atInt16& val) -{ - if (Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap16(val); - - return val; -} - -atUint16 LittleUint16(atUint16& val) -{ - atInt16 ret = val; - LittleInt16(ret); - val = ret; - - return val; -} - -atInt16 BigInt16(atInt16& val) -{ - if (!Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap16(val); - - return val; -} - -atUint16 BigUint16(atUint16& val) -{ - atInt16 ret = val; - BigInt16(ret); - val = ret; - - return val; -} - -atInt32 LittleInt32(atInt32& val) -{ - if (Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap32(val); - - return val; -} - -atUint32 LittleUint32(atUint32& val) -{ - atInt32 ret = val; - LittleInt32(ret); - val = ret; - - return val; -} - - -atInt32 BigInt32(atInt32& val) -{ - if (!Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap32(val); - - return val; -} - -atUint32 BigUint32(atUint32& val) -{ - atInt32 ret = val; - BigInt32(ret); - val = ret; - - return val; -} - -atInt64 LittleInt64(atInt64& val) -{ - if (Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap64(val); - - return val; -} - -atUint64 LittleUint64(atUint64& val) -{ - atInt64 ret = val; - LittleInt64(ret); - val = ret; - - return val; -} - -atInt64 BigInt64(atInt64& val) -{ - if (!Athena::utility::isSystemBigEndian()) - val = Athena::utility::swap64(val); - - return val; -} - -atUint64 BigUint64(atUint64& val) -{ - atInt64 ret = val; - BigInt64(ret); - val = ret; - - return val; -} - -float LittleFloat(float& val) -{ - if (Athena::utility::isSystemBigEndian()) - val = Athena::utility::swapFloat(val); - - return val; -} - -float BigFloat(float& val) -{ - if (!Athena::utility::isSystemBigEndian()) - val = Athena::utility::swapFloat(val); - - return val; -} - -double LittleDouble(double& val) -{ - if (Athena::utility::isSystemBigEndian()) - val = Athena::utility::swapDouble(val); - - return val; -} - -double BigDouble(double& val) -{ - if (!Athena::utility::isSystemBigEndian()) - val = Athena::utility::swapDouble(val); - - return val; -} - - void fillRandom(atUint8 * rndArea, atUint64 count) { for(atUint64 i = 0; i < count; i++) rndArea[i]=rand(); } -float swapFloat(float val) -{ - float retVal; - char* convFloat = (char*) &val; - char* retFloat = (char*) &retVal; - - retFloat[0] = convFloat[3]; - retFloat[1] = convFloat[2]; - retFloat[2] = convFloat[1]; - retFloat[3] = convFloat[0]; - - return retVal; -} - -double swapDouble(double val) -{ - double retVal; - char* convFloat = (char*) &val; - char* retFloat = (char*) &retVal; - - retFloat[0] = convFloat[7]; - retFloat[1] = convFloat[6]; - retFloat[2] = convFloat[5]; - retFloat[3] = convFloat[4]; - retFloat[4] = convFloat[3]; - retFloat[5] = convFloat[2]; - retFloat[6] = convFloat[1]; - retFloat[7] = convFloat[0]; - - return (double)((atUint64)retVal); -} - - std::vector &split(const std::string &s, char delim, std::vector &elems) { std::stringstream ss(s); From 7890a1be70f2336a813f6a6d64d66707641ceaec Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 10 Apr 2015 18:20:18 -1000 Subject: [PATCH 2/2] fixed header non-inclusion --- include/Athena/Utility.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/Athena/Utility.hpp b/include/Athena/Utility.hpp index 476cf1c..02eff72 100644 --- a/include/Athena/Utility.hpp +++ b/include/Athena/Utility.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "Athena/Global.hpp" #include "Athena/Types.hpp"