From 1c598695e433b9ac65887d156fcd78b10e6269de Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 16 May 2018 00:21:24 +0200 Subject: Optimize for little endian instead of big endian --- project.conf | 4 ++-- sibs/SafeDeserializer.hpp | 4 ++-- sibs/SafeSerializer.hpp | 4 ++-- tests/main.cpp | 9 +++++++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/project.conf b/project.conf index ca50d38..0fd1d74 100644 --- a/project.conf +++ b/project.conf @@ -1,9 +1,9 @@ [package] name = "sibs-serializer" -version = "0.2.0" +version = "1.0.0" type = "static" platforms = ["any"] tests = "tests" [config] -expose_include_dirs = ["."] \ No newline at end of file +expose_include_dirs = ["."] diff --git a/sibs/SafeDeserializer.hpp b/sibs/SafeDeserializer.hpp index 7ef6b3f..0110920 100644 --- a/sibs/SafeDeserializer.hpp +++ b/sibs/SafeDeserializer.hpp @@ -38,7 +38,7 @@ namespace sibs verifyExtractSize(typeSize); size -= typeSize; T result; - #ifdef LITTLE_ENDIAN + #if BYTE_ORDER == BIG_ENDIAN switch(typeSize) { case 1: @@ -63,7 +63,7 @@ namespace sibs } } #else - memcpy(&result, data, typeSize); + result = *(T*)data; #endif data += typeSize; return result; diff --git a/sibs/SafeSerializer.hpp b/sibs/SafeSerializer.hpp index 72ef5a3..379b7c8 100644 --- a/sibs/SafeSerializer.hpp +++ b/sibs/SafeSerializer.hpp @@ -22,7 +22,7 @@ namespace sibs { usize offset = buffer.size(); buffer.resize(buffer.size() + sizeof(T)); - #ifdef LITTLE_ENDIAN + #if BYTE_ORDER == BIG_ENDIAN switch(sizeof(T)) { case 1: @@ -47,7 +47,7 @@ namespace sibs } } #else - memcpy(&buffer[offset], &data, sizeof(T)); + *(T*)&buffer[offset] = data; #endif } diff --git a/tests/main.cpp b/tests/main.cpp index 4b6c7a7..e879e6a 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -15,6 +15,15 @@ struct TestStruct int main() { +#if BYTE_ORDER == BIG_ENDIAN + const char *endianessStr = "big"; +#elif BYTE_ORDER == LITTLE_ENDIAN + const char *endianessStr = "little"; +#else + const char *endianessStr = "Unknown"; +#endif + printf("System endianess: %s\n", endianessStr); + TestStruct expectedTestStruct; expectedTestStruct.a = 0x38956326; expectedTestStruct.b = 0x34; -- cgit v1.2.3