From 4363fd64c4db21ddb97f3616efb2a20ba9814557 Mon Sep 17 00:00:00 2001 From: Michael Krasnyk Date: Sun, 24 Apr 2016 14:06:48 +0200 Subject: [PATCH] Fix for PhantomNode packing in MSVC Changed bool to uint32_t in bit fields to have 4-byte packings for 32 bits: "c++ compilers will allocate bit-fields in memory as follows: several consecutive bit-field members of the same type will be allocated sequentially. As soon as a new type needs to be allocated, it will be aligned with the beginning of the next logical memory block." References: - https://msdn.microsoft.com/en-us/library/ewwyfdbe.aspx - http://stackoverflow.com/questions/308364/c-bitfield-packing-with-bools/308383#308383 --- include/engine/hint.hpp | 11 +---------- include/engine/phantom_node.hpp | 12 +++--------- include/util/typedefs.hpp | 7 ++----- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/include/engine/hint.hpp b/include/engine/hint.hpp index 56dfbc4d6..c39b03c6a 100644 --- a/include/engine/hint.hpp +++ b/include/engine/hint.hpp @@ -63,18 +63,9 @@ struct Hint friend std::ostream &operator<<(std::ostream &, const Hint &); }; -#ifndef _MSC_VER static_assert(sizeof(Hint) == 60 + 4, "Hint is bigger than expected"); constexpr std::size_t ENCODED_HINT_SIZE = 88; -static_assert(ENCODED_HINT_SIZE / 4 * 3 >= sizeof(Hint), - "ENCODED_HINT_SIZE does not match size of Hint"); -#else -// PhantomNode is bigger under windows because MSVC does not support bit packing -static_assert(sizeof(Hint) == 72 + 4, "Hint is bigger than expected"); -constexpr std::size_t ENCODED_HINT_SIZE = 104; -static_assert(ENCODED_HINT_SIZE / 4 * 3 >= sizeof(Hint), - "ENCODED_HINT_SIZE does not match size of Hint"); -#endif +static_assert(ENCODED_HINT_SIZE / 4 * 3 >= sizeof(Hint), "ENCODED_HINT_SIZE does not match size of Hint"); } } diff --git a/include/engine/phantom_node.hpp b/include/engine/phantom_node.hpp index 88724b171..7f8c29d56 100644 --- a/include/engine/phantom_node.hpp +++ b/include/engine/phantom_node.hpp @@ -149,13 +149,11 @@ struct PhantomNode unsigned reverse_packed_geometry_id; struct ComponentType { - uint32_t id : 31; - bool is_tiny : 1; + std::uint32_t id : 31; + std::uint32_t is_tiny : 1; } component; -// bit-fields are broken on Windows -#ifndef _MSC_VER static_assert(sizeof(ComponentType) == 4, "ComponentType needs to be 4 bytes big"); -#endif + util::Coordinate location; util::Coordinate input_location; unsigned short fwd_segment_position; @@ -165,11 +163,7 @@ struct PhantomNode extractor::TravelMode backward_travel_mode; }; -#ifndef _MSC_VER static_assert(sizeof(PhantomNode) == 60, "PhantomNode has more padding then expected"); -#else -static_assert(sizeof(PhantomNode) == 72, "PhantomNode has more padding then expected"); -#endif using PhantomNodePair = std::pair; diff --git a/include/util/typedefs.hpp b/include/util/typedefs.hpp index b3404bc2c..f36a241ee 100644 --- a/include/util/typedefs.hpp +++ b/include/util/typedefs.hpp @@ -71,13 +71,10 @@ struct SegmentID BOOST_ASSERT(!enabled || id != SPECIAL_SEGMENTID); } - NodeID id : 31; - bool enabled : 1; + NodeID id : 31; + std::uint32_t enabled : 1; }; -// bit-fields are broken on Windows -#ifndef _MSC_VER static_assert(sizeof(SegmentID) == 4, "SegmentID needs to be 4 bytes big"); -#endif #endif /* TYPEDEFS_H */