Fix Endian issue in BitHelpers

This commit is contained in:
HarryLeeIBM 2022-07-27 06:56:04 -07:00
parent 288b73035f
commit 3639ecfc56

View File

@ -1,5 +1,6 @@
#pragma once
#include <bit>
#include <base/types.h>
#include <Common/BitHelpers.h>
#include <Common/Exception.h>
@ -140,7 +141,8 @@ private:
memcpy(&tmp_buffer, source_current, bytes_to_read);
source_current += bytes_to_read;
tmp_buffer = __builtin_bswap64(tmp_buffer);
if constexpr (std::endian::native == std::endian::little)
tmp_buffer = __builtin_bswap64(tmp_buffer);
bits_buffer |= BufferType(tmp_buffer) << ((sizeof(BufferType) - sizeof(tmp_buffer)) * 8 - bits_count);
bits_count += static_cast<UInt8>(bytes_to_read) * 8;
@ -223,8 +225,11 @@ private:
"Can not write past end of buffer. Space available {} bytes, required to write {} bytes.",
available, to_write);
}
const auto tmp_buffer = __builtin_bswap64(static_cast<UInt64>(bits_buffer >> (sizeof(bits_buffer) - sizeof(UInt64)) * 8));
UInt64 tmp_buffer = 0;
if constexpr (std::endian::native == std::endian::little)
tmp_buffer = __builtin_bswap64(static_cast<UInt64>(bits_buffer >> (sizeof(bits_buffer) - sizeof(UInt64)) * 8));
else
tmp_buffer = static_cast<UInt64>(bits_buffer >> (sizeof(bits_buffer) - sizeof(UInt64)) * 8);
memcpy(dest_current, &tmp_buffer, to_write);
dest_current += to_write;