From a1c1ad096d23d76de2924ce299ecd49e66674e77 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 9 Jul 2022 20:33:03 -0400 Subject: common: fix bitfield aliasing on GCC/Clang --- src/common/bit_field.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 16d805694..7e1df62b1 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -146,7 +146,16 @@ public: } constexpr void Assign(const T& value) { +#ifdef _MSC_VER storage = static_cast((storage & ~mask) | FormatValue(value)); +#else + // Explicitly reload with memcpy to avoid compiler aliasing quirks + // regarding optimization: GCC/Clang clobber chained stores to + // different bitfields in the same struct with the last value. + StorageTypeWithEndian storage_; + std::memcpy(&storage_, &storage, sizeof(storage_)); + storage = static_cast((storage_ & ~mask) | FormatValue(value)); +#endif } [[nodiscard]] constexpr T Value() const { -- cgit v1.2.3