diff options
| author | Liam <byteslice@airmail.cc> | 2022-07-09 20:33:03 -0400 |
|---|---|---|
| committer | Liam <byteslice@airmail.cc> | 2022-07-09 22:43:45 -0400 |
| commit | b7b77ea2ac28c6fa5afeda1dcbbba33b016732b7 (patch) | |
| tree | 2da5d6548a80d05831010160182d07a2f6546aa2 /src/common | |
| parent | 485473f1181a4902f2996cde236dd8a4685836f7 (diff) | |
common: fix bitfield aliasing on GCC/Clang
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/bit_field.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 16d805694e..7e1df62b1c 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<StorageType>((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<StorageType>((storage_ & ~mask) | FormatValue(value)); +#endif } [[nodiscard]] constexpr T Value() const { |
