summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2022-07-15 22:16:52 -0700
committerGitHub <noreply@github.com>2022-07-15 22:16:52 -0700
commit93a4ca11fa976a71cfd4912900b5f98993c930d4 (patch)
tree4faff3821fd0905e05de813b7f70cdf6b550f3ff
parentd2c0b45bca8a42f30001e0deaf4e0921a7efbee4 (diff)
parenta1c1ad096d23d76de2924ce299ecd49e66674e77 (diff)
Merge pull request #8560 from liamwhite/bitfield-may-alias
common: fix bitfield aliasing on GCC/Clang
-rw-r--r--src/common/bit_field.h9
1 files changed, 9 insertions, 0 deletions
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<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 {