3
0
Fork 0
forked from suyu/suyu

Merge pull request #8560 from liamwhite/bitfield-may-alias

common: fix bitfield aliasing on GCC/Clang
This commit is contained in:
bunnei 2022-07-15 22:16:52 -07:00 committed by GitHub
commit 93a4ca11fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {