Fix implementation of Misc::swapEndiannessInplace

pull/3046/head^2
Petr Mikheev 4 years ago
parent dc31e1c680
commit 31b5150e0d

@ -2,6 +2,8 @@
#define COMPONENTS_MISC_ENDIANNESS_H #define COMPONENTS_MISC_ENDIANNESS_H
#include <cstdint> #include <cstdint>
#include <cstring>
#include <type_traits>
namespace Misc namespace Misc
{ {
@ -15,20 +17,26 @@ namespace Misc
if constexpr (sizeof(T) == 2) if constexpr (sizeof(T) == 2)
{ {
uint16_t& v16 = *reinterpret_cast<uint16_t*>(&v); uint16_t v16;
std::memcpy(&v16, &v, sizeof(T));
v16 = (v16 >> 8) | (v16 << 8); v16 = (v16 >> 8) | (v16 << 8);
std::memcpy(&v, &v16, sizeof(T));
} }
if constexpr (sizeof(T) == 4) if constexpr (sizeof(T) == 4)
{ {
uint32_t& v32 = *reinterpret_cast<uint32_t*>(&v); uint32_t v32;
v32 = (v32 >> 24) | ((v32 >> 8) & 0xff00) | ((v32 & 0xff00) << 8) || v32 << 24; std::memcpy(&v32, &v, sizeof(T));
v32 = (v32 >> 24) | ((v32 >> 8) & 0xff00) | ((v32 & 0xff00) << 8) | v32 << 24;
std::memcpy(&v, &v32, sizeof(T));
} }
if constexpr (sizeof(T) == 8) if constexpr (sizeof(T) == 8)
{ {
uint64_t& v64 = *reinterpret_cast<uint64_t*>(&v); uint64_t v64;
std::memcpy(&v64, &v, sizeof(T));
v64 = (v64 >> 56) | ((v64 & 0x00ff'0000'0000'0000) >> 40) | ((v64 & 0x0000'ff00'0000'0000) >> 24) v64 = (v64 >> 56) | ((v64 & 0x00ff'0000'0000'0000) >> 40) | ((v64 & 0x0000'ff00'0000'0000) >> 24)
| ((v64 & 0x0000'00ff'0000'0000) >> 8) | ((v64 & 0x0000'0000'ff00'0000) << 8) | ((v64 & 0x0000'00ff'0000'0000) >> 8) | ((v64 & 0x0000'0000'ff00'0000) << 8)
| ((v64 & 0x0000'0000'00ff'0000) << 24) | ((v64 & 0x0000'0000'0000'ff00) << 40) | (v64 << 56); | ((v64 & 0x0000'0000'00ff'0000) << 24) | ((v64 & 0x0000'0000'0000'ff00) << 40) | (v64 << 56);
std::memcpy(&v, &v64, sizeof(T));
} }
} }

Loading…
Cancel
Save