From f388fe7252f6e10b49578bb3bd8b2e4e30c89b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 4 Sep 2019 14:56:31 +0200 Subject: [PATCH] [apps] Use attribute "packed" on member variable directly instead of using "pragma pack" to allow compiler optimization on one-byte object accesses --- apps/calculation/calculation.h | 7 +++---- apps/sequence/sequence.h | 6 ++---- apps/shared/cartesian_function.h | 4 +--- apps/shared/function.h | 6 ++---- apps/shared/range_1D.h | 8 +++----- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/apps/calculation/calculation.h b/apps/calculation/calculation.h index 9e5b9b85a..89dbd52a1 100644 --- a/apps/calculation/calculation.h +++ b/apps/calculation/calculation.h @@ -16,7 +16,7 @@ class CalculationStore; * |m_displayOutput| m_height |m_expandedHeight|m_equalSign|m_inputText|m_exactOuputText|m_approximateOuputText| * * */ -#pragma pack(push,1) + class Calculation { public: enum class EqualSign : uint8_t { @@ -77,12 +77,11 @@ private: * by user (of maximum length TextField::maxBufferSize()) because when we * print an expression we add omitted signs (multiplications, parenthesis...) */ DisplayOutput m_displayOutput; - KDCoordinate m_height; - KDCoordinate m_expandedHeight; + KDCoordinate m_height __attribute__((packed)); + KDCoordinate m_expandedHeight __attribute__((packed)); EqualSign m_equalSign; char m_inputText[0]; // MUST be the last member variable }; -#pragma pack(pop) } diff --git a/apps/sequence/sequence.h b/apps/sequence/sequence.h index 99cd41d6e..408c11d4f 100644 --- a/apps/sequence/sequence.h +++ b/apps/sequence/sequence.h @@ -72,7 +72,6 @@ private: /* SequenceRecordDataBuffer is the layout of the data buffer of Record * representing a Sequence. See comment in * Shared::Function::FunctionRecordDataBuffer about packing. */ -#pragma pack(push,1) class SequenceRecordDataBuffer : public FunctionRecordDataBuffer { public: SequenceRecordDataBuffer(KDColor color) : @@ -100,12 +99,11 @@ private: #if __EMSCRIPTEN__ // See comment about emscripten alignement in Shared::Function::FunctionRecordDataBuffer static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t"); - emscripten_align1_short m_initialConditionSizes[2]; + emscripten_align1_short m_initialConditionSizes[2] __attribute__((packed)); #else - uint16_t m_initialConditionSizes[2]; + uint16_t m_initialConditionSizes[2] __attribute__((packed)); #endif }; -#pragma pack(pop) class SequenceModel : public Shared::ExpressionModel { public: diff --git a/apps/shared/cartesian_function.h b/apps/shared/cartesian_function.h index 62b3aa6d3..786b1c9fc 100644 --- a/apps/shared/cartesian_function.h +++ b/apps/shared/cartesian_function.h @@ -61,8 +61,7 @@ private: /* CartesianFunctionRecordDataBuffer is the layout of the data buffer of Record * representing a CartesianFunction. See comment on * Shared::Function::FunctionRecordDataBuffer about packing. */ -#pragma pack(push,1) - class CartesianFunctionRecordDataBuffer : public FunctionRecordDataBuffer { + class __attribute__((packed)) CartesianFunctionRecordDataBuffer : public FunctionRecordDataBuffer { public: CartesianFunctionRecordDataBuffer(KDColor color) : FunctionRecordDataBuffer(color), @@ -86,7 +85,6 @@ private: * the expression of the function, directly copied from the pool. */ //char m_expression[0]; }; -#pragma pack(pop) class Model : public ExpressionModel { public: void * expressionAddress(const Ion::Storage::Record * record) const override; diff --git a/apps/shared/function.h b/apps/shared/function.h index a986b963f..08cfcaade 100644 --- a/apps/shared/function.h +++ b/apps/shared/function.h @@ -54,7 +54,6 @@ protected: * - increase the size of the storage file * - introduce junk memory zone which are then crc-ed in Storage::checksum * creating dependency on uninitialized values. */ -#pragma pack(push,1) class FunctionRecordDataBuffer { public: FunctionRecordDataBuffer(KDColor color) : m_color(color), m_active(true) {} @@ -73,13 +72,12 @@ protected: * version of uint16_t type to avoid producing an alignment error on the * emscripten platform. */ static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t"); - emscripten_align1_short m_color; + emscripten_align1_short m_color __attribute__((packed)); #else - uint16_t m_color; + uint16_t m_color __attribute__((packed)); #endif bool m_active; }; -#pragma pack(pop) private: FunctionRecordDataBuffer * recordData() const; }; diff --git a/apps/shared/range_1D.h b/apps/shared/range_1D.h index e1d63c0ed..d732276f7 100644 --- a/apps/shared/range_1D.h +++ b/apps/shared/range_1D.h @@ -9,8 +9,7 @@ namespace Shared { /* This class is used in a DataBuffer of a Storage::Record. See comment in * Shared::Function::FunctionRecordDataBuffer about packing. */ -#pragma pack(push,1) -class Range1D final { +class __attribute__((packed)) Range1D final { public: constexpr static float k_minFloat = 1E-4f; Range1D(float min = -10.0f, float max = 10.0f) : @@ -27,14 +26,13 @@ private: #if __EMSCRIPTEN__ // See comment about emscripten alignement in Shared::Function::FunctionRecordDataBuffer static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t"); - emscripten_align1_float m_min; - emscripten_align1_float m_max; + emscripten_align1_float m_min __attribute__((packed)); + emscripten_align1_float m_max __attribute__((packed)); #else float m_min; float m_max; #endif }; -#pragma pack(pop) static_assert(Range1D::k_minFloat >= FLT_EPSILON, "InteractiveCurveViewRange's minimal float range is lower than float precision, it might draw uglily curves such as cos(x)^2+sin(x)^2");