From c9b5ba4a5c7eab652c6685eb0cdee0b4af1489fc Mon Sep 17 00:00:00 2001 From: Eladash Date: Tue, 17 Mar 2020 15:05:42 +0200 Subject: [PATCH] BEType.h: use common initial sequance in v128 Partially obey 'strict type aliasing' rule. --- Utilities/BEType.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Utilities/BEType.h b/Utilities/BEType.h index 67918fcf16..bf8ce55398 100644 --- a/Utilities/BEType.h +++ b/Utilities/BEType.h @@ -18,17 +18,17 @@ union alignas(16) v128 template struct masked_array_t // array type accessed as (index ^ M) { - T m_data[N]; + char m_data[16]; public: T& operator[](std::size_t index) { - return m_data[index ^ M]; + return reinterpret_cast(m_data)[index ^ M]; } const T& operator[](std::size_t index) const { - return m_data[index ^ M]; + return reinterpret_cast(m_data)[index ^ M]; } }; @@ -68,7 +68,7 @@ union alignas(16) v128 struct bit_array_128 { - u64 m_data[2]; + char m_data[16]; public: class bit_element @@ -118,26 +118,30 @@ union alignas(16) v128 // Index 0 returns the MSB and index 127 returns the LSB bit_element operator[](u32 index) { + const auto data_ptr = reinterpret_cast(m_data); + if constexpr (std::endian::little == std::endian::native) { - return bit_element(m_data[1 - (index >> 6)], 0x8000000000000000ull >> (index & 0x3F)); + return bit_element(data_ptr[1 - (index >> 6)], 0x8000000000000000ull >> (index & 0x3F)); } else { - return bit_element(m_data[index >> 6], 0x8000000000000000ull >> (index & 0x3F)); + return bit_element(data_ptr[index >> 6], 0x8000000000000000ull >> (index & 0x3F)); } } // Index 0 returns the MSB and index 127 returns the LSB bool operator[](u32 index) const { + const auto data_ptr = reinterpret_cast(m_data); + if constexpr (std::endian::little == std::endian::native) { - return (m_data[1 - (index >> 6)] & (0x8000000000000000ull >> (index & 0x3F))) != 0; + return (data_ptr[1 - (index >> 6)] & (0x8000000000000000ull >> (index & 0x3F))) != 0; } else { - return (m_data[index >> 6] & (0x8000000000000000ull >> (index & 0x3F))) != 0; + return (data_ptr[index >> 6] & (0x8000000000000000ull >> (index & 0x3F))) != 0; } } } _bit;