From 078e9bdab4370736eb961434938faa7491fe51e0 Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 29 May 2021 01:11:36 +0100 Subject: [PATCH 1/5] Clean up bitops.hpp --- include/gp/math/boolean/bitops.hpp | 108 +++++++++++------------------ 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/include/gp/math/boolean/bitops.hpp b/include/gp/math/boolean/bitops.hpp index 0e6fedc..a92043e 100644 --- a/include/gp/math/boolean/bitops.hpp +++ b/include/gp/math/boolean/bitops.hpp @@ -2,79 +2,55 @@ #include "gp/containers/array.hpp" +#include + #include #include -namespace gp{ - namespace _impl{ - constexpr uint32_t uint32_ = 0x01020304; - constexpr uint8_t magic_ = (const uint8_t&)uint32_; - } +namespace gp { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wfour-char-constants" -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmultichar" - enum class endian : uint16_t - { -#ifdef _WIN32 - little = 0, - big = 1, - native = little -#elseif defined(__BYTE_ORDER__) - little = __ORDER_LITTLE_ENDIAN__, - big = __ORDER_BIG_ENDIAN__, - native = __BYTE_ORDER__ -#else - little = true, - big = false, - native = ('ABCD'==0x41424344UL) - }; -#endif -#pragma GCC diagnostic pop -#pragma clang diagnostic pop - +using endian = std::endian; - template - T swap_endian(T value) - { - union - { - T v; - uint8_t u8[sizeof(T)]; - } src, dest; +template +T swap_endian(T value) { + union { + T v; + uint8_t u8[sizeof(T)]; + } src, dest; + + src.v = value; - src.v = value; - - for (size_t idx = 0;idx - struct endian_wrapper { - union{ - T value; - gp::array bytes; - }; - endian_wrapper(){}; - endian_wrapper(T v) : value{ - mode == endian::native ? v : swap_endian(v) - }{} - endian_wrapper(endian_wrapper& v) : value{ - v.value - }{} - - endian_wrapper& operator=(T p) { - value = mode == endian::native ? p : swap_endian(p); - return *this; - } +template +struct endian_wrapper { - operator T(){ - return mode == endian::native ? value : swap_endian(value); - } + union { + T value; + gp::array bytes; }; -} \ No newline at end of file + + endian_wrapper() {} + + endian_wrapper(T v) + : value{mode == endian::native ? v : swap_endian(v)} {} + + endian_wrapper(endian_wrapper& v) + : value{v.value} {} + + endian_wrapper& operator=(T p) { + value = mode == endian::native ? p : swap_endian(p); + return *this; + } + + operator T() { + return mode == endian::native ? value : swap_endian(value); + } +}; + +} From 275e85d5e2cff09ed6cc2b0c80cd161539797ec5 Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 29 May 2021 02:14:57 +0100 Subject: [PATCH 2/5] Make endian_wrapper conversion operator const --- include/gp/math/boolean/bitops.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gp/math/boolean/bitops.hpp b/include/gp/math/boolean/bitops.hpp index a92043e..ce03019 100644 --- a/include/gp/math/boolean/bitops.hpp +++ b/include/gp/math/boolean/bitops.hpp @@ -12,7 +12,7 @@ namespace gp { using endian = std::endian; template -T swap_endian(T value) { +T swap_endian(T value) noexcept { union { T v; uint8_t u8[sizeof(T)]; @@ -48,7 +48,7 @@ struct endian_wrapper { return *this; } - operator T() { + operator T() const noexcept { return mode == endian::native ? value : swap_endian(value); } }; From de8f097d70cbde6cc637df978ffdb03a7ca82e60 Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 29 May 2021 16:15:31 +0100 Subject: [PATCH 3/5] Add copy assignment operator for endian_wrapper --- include/gp/math/boolean/bitops.hpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/include/gp/math/boolean/bitops.hpp b/include/gp/math/boolean/bitops.hpp index ce03019..7b73303 100644 --- a/include/gp/math/boolean/bitops.hpp +++ b/include/gp/math/boolean/bitops.hpp @@ -35,19 +35,25 @@ struct endian_wrapper { gp::array bytes; }; - endian_wrapper() {} + endian_wrapper() noexcept + : value{0} {} - endian_wrapper(T v) + endian_wrapper(T v) noexcept : value{mode == endian::native ? v : swap_endian(v)} {} - endian_wrapper(endian_wrapper& v) + endian_wrapper(const endian_wrapper& v) noexcept : value{v.value} {} - endian_wrapper& operator=(T p) { + endian_wrapper& operator=(T p) noexcept { value = mode == endian::native ? p : swap_endian(p); return *this; } + endian_wrapper& operator=(const endian_wrapper& v) noexcept { + value = v.value; + return *this; + } + operator T() const noexcept { return mode == endian::native ? value : swap_endian(value); } From b864f3a3c16404070164c7d8fe3d4acb9a576eb8 Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 29 May 2021 18:05:13 +0100 Subject: [PATCH 4/5] Add arithmetic assign operators for endian_wrapper --- include/gp/math/boolean/bitops.hpp | 36 ++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/include/gp/math/boolean/bitops.hpp b/include/gp/math/boolean/bitops.hpp index 7b73303..9972742 100644 --- a/include/gp/math/boolean/bitops.hpp +++ b/include/gp/math/boolean/bitops.hpp @@ -39,13 +39,13 @@ struct endian_wrapper { : value{0} {} endian_wrapper(T v) noexcept - : value{mode == endian::native ? v : swap_endian(v)} {} + : value{adjust(v)} {} endian_wrapper(const endian_wrapper& v) noexcept : value{v.value} {} - endian_wrapper& operator=(T p) noexcept { - value = mode == endian::native ? p : swap_endian(p); + endian_wrapper& operator=(T v) noexcept { + value = adjust(v); return *this; } @@ -55,7 +55,35 @@ struct endian_wrapper { } operator T() const noexcept { - return mode == endian::native ? value : swap_endian(value); + return adjust(value); + } + + // arithmetic assignment operators + + endian_wrapper& operator+=(T rhs) noexcept { + value = adjust(adjust(value) + rhs); + return *this; + } + + endian_wrapper& operator+=(endian_wrapper rhs) noexcept { + value = adjust(adjust(value) + adjust(rhs.value)); + return *this; + } + + endian_wrapper& operator-=(T rhs) noexcept { + value = adjust(adjust(value) - rhs); + return *this; + } + + endian_wrapper& operator-=(endian_wrapper rhs) noexcept { + value = adjust(adjust(value) - adjust(rhs.value)); + return *this; + } + +private: + + T adjust(T x) const noexcept { + return (mode == endian::native ? x : swap_endian(x)); } }; From d76ee8c6a9ae83917824f38de0e99a58bfd564cf Mon Sep 17 00:00:00 2001 From: Emil-Jarosz Date: Sat, 29 May 2021 18:29:10 +0100 Subject: [PATCH 5/5] Make endian_wrapper copy ctor/assignment trivial --- include/gp/ipc/envelope/cbor.hpp | 7 +++---- include/gp/math/boolean/bitops.hpp | 26 +++++++++++--------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/include/gp/ipc/envelope/cbor.hpp b/include/gp/ipc/envelope/cbor.hpp index 818ce19..d23bda6 100644 --- a/include/gp/ipc/envelope/cbor.hpp +++ b/include/gp/ipc/envelope/cbor.hpp @@ -126,19 +126,19 @@ namespace gp { } else if(norm_v < (1ll<<16ll)) { endian_wrapper wrapper = norm_v; src.push_back(header+25); - for(auto byte : wrapper.bytes) { + for(auto byte : wrapper.bytes()) { src.push_back(byte); } } else if(norm_v < (1ll<<32ll)) { endian_wrapper wrapper = norm_v; src.push_back(header+26); - for(auto byte : wrapper.bytes) { + for(auto byte : wrapper.bytes()) { src.push_back(byte); } } else { endian_wrapper wrapper = norm_v; src.push_back(header+27); - for(auto byte : wrapper.bytes) { + for(auto byte : wrapper.bytes()) { src.push_back(byte); } } @@ -477,4 +477,3 @@ namespace gp { } } } - diff --git a/include/gp/math/boolean/bitops.hpp b/include/gp/math/boolean/bitops.hpp index 9972742..5ffcf1b 100644 --- a/include/gp/math/boolean/bitops.hpp +++ b/include/gp/math/boolean/bitops.hpp @@ -29,35 +29,31 @@ T swap_endian(T value) noexcept { template struct endian_wrapper { + T value = 0; - union { - T value; - gp::array bytes; - }; - - endian_wrapper() noexcept - : value{0} {} + constexpr endian_wrapper() noexcept = default; endian_wrapper(T v) noexcept : value{adjust(v)} {} - endian_wrapper(const endian_wrapper& v) noexcept - : value{v.value} {} - endian_wrapper& operator=(T v) noexcept { value = adjust(v); return *this; } - endian_wrapper& operator=(const endian_wrapper& v) noexcept { - value = v.value; - return *this; - } - operator T() const noexcept { return adjust(value); } + auto bytes() const noexcept -> gp::array { + union { + T t; + gp::array bytes; + } tmp {.t = value}; + + return tmp.bytes; + } + // arithmetic assignment operators endian_wrapper& operator+=(T rhs) noexcept {