Skip to content

Commit

Permalink
[poincare/preferences] Fix alignment for emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielNumworks committed Apr 16, 2024
1 parent fbff382 commit 5c64ac4
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 66 deletions.
8 changes: 6 additions & 2 deletions apps/global_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GlobalPreferences {
static GlobalPreferences* SharedGlobalPreferences();

GlobalPreferences()
: m_brightnessLevel(Ion::Backlight::MaxBrightness),
: m_version(k_version),
m_brightnessLevel(Ion::Backlight::MaxBrightness),
m_showPopUp(true),
m_font(KDFont::Size::Large) {
setLanguage(I18n::Language::EN);
Expand Down Expand Up @@ -106,6 +107,8 @@ class GlobalPreferences {
void setFont(KDFont::Size font) { m_font = font; }

private:
constexpr static int k_version = 0x20240401;

const CountryPreferences& preferences() const {
return I18n::CountryPreferencesArray[static_cast<uint8_t>(m_country)];
}
Expand All @@ -119,10 +122,11 @@ class GlobalPreferences {
static_assert(I18n::NumberOfCountries > 0,
"I18n::NumberOfCountries is not superior to 0");

int m_version = 0x20240401;
#if __EMSCRIPTEN__
emscripten_align1_int m_version;
emscripten_align1_int m_brightnessLevel;
#else
int m_version;
int m_brightnessLevel;
#endif
I18n::Language m_language;
Expand Down
15 changes: 11 additions & 4 deletions ion/include/ion/exam_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Configuration {
Configuration() : Configuration(-1) {}
Configuration(Int raw) : m_bits(raw) {}

bool operator==(const Configuration& other) const { return m_bits == m_bits; }
bool operator==(const Configuration& other) const {
return m_bits == other.m_bits;
}
bool operator!=(const Configuration& other) const {
return !(*this == other);
}
Expand All @@ -70,10 +72,15 @@ class Configuration {
static_assert(static_cast<int>(Bits::NumberOfBits) ==
OMG::BitHelper::numberOfBitsIn<Int>());

bool configurable() const { return m_bits.get(Bits::Configurable); }
Int data() const { return m_bits.get(Bits::DataLast, Bits::DataFirst); }
bool configurable() const {
return OMG::BitHelper::bitAtIndex(m_bits, Bits::Configurable);
}
Int data() const {
return OMG::BitHelper::bitsBetweenIndexes(m_bits, Bits::DataLast,
Bits::DataFirst);
}

OMG::BitHelper::BitField<Int> m_bits;
Int m_bits;
};

static_assert(
Expand Down
11 changes: 6 additions & 5 deletions ion/src/shared/exam_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ void set(Configuration config) {

// Class Configuration

Configuration::Configuration(Ruleset rules, Int flags) {
Configuration::Configuration(Ruleset rules, Int flags) : m_bits(0) {
bool configurable = rules == Ruleset::PressToTest;
m_bits.set(Bits::Configurable, Bits::Configurable, configurable)
.set(Bits::DataLast, Bits::DataFirst,
configurable ? flags : static_cast<Int>(rules));
OMG::BitHelper::setBitAtIndex(m_bits, Bits::Configurable, configurable);
OMG::BitHelper::setBitsBetweenIndexes(
m_bits, Bits::DataLast, Bits::DataFirst,
configurable ? flags : static_cast<Int>(rules));

assert(!isUninitialized());
}
Expand All @@ -77,7 +78,7 @@ Int Configuration::flags() const {
}

bool Configuration::isUninitialized() const {
bool clearBit = m_bits.get(Bits::Cleared);
bool clearBit = OMG::BitHelper::bitAtIndex(m_bits, Bits::Cleared);
return clearBit || (!configurable() &&
data() >= static_cast<Int>(Ruleset::NumberOfRulesets));
}
Expand Down
72 changes: 30 additions & 42 deletions omg/include/omg/bit_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ constexpr bool bitAtIndex(T mask, size_t i) {
assert(i >= 0 && i < numberOfBitsIn<T>());
return (mask >> i) & 1U;
}
template <typename T, typename I>
constexpr bool bitAtIndex(T mask, I i) {
return bitAtIndex(mask, static_cast<size_t>(i));
}

template <typename T>
constexpr void setBitAtIndex(T& mask, size_t i, bool b) {
Expand All @@ -40,6 +44,32 @@ constexpr void setBitAtIndex(T& mask, size_t i, bool b) {
mask &= ~(one << i);
}
}
template <typename T, typename I>
constexpr void setBitAtIndex(T& mask, I i, bool b) {
setBitAtIndex(mask, static_cast<size_t>(i), b);
}

template <typename T>
constexpr T bitsBetweenIndexes(T bits, size_t high, size_t low) {
return (bits >> low) & ((static_cast<T>(1) << (high - low + 1)) - 1);
}
template <typename T, typename I>
constexpr T bitsBetweenIndexes(T bits, I high, I low) {
return bitsBetweenIndexes<T>(bits, static_cast<size_t>(high),
static_cast<size_t>(low));
}

template <typename T>
constexpr void setBitsBetweenIndexes(T& bits, size_t high, size_t low,
T value) {
T mask = ((static_cast<T>(1) << (high - low + 1)) - static_cast<T>(1)) << low;
bits = (bits & ~mask) | ((value << low) & mask);
}
template <typename T, typename I>
constexpr void setBitsBetweenIndexes(T& bits, I high, I low, T value) {
setBitsBetweenIndexes<T>(bits, static_cast<size_t>(high),
static_cast<size_t>(low), value);
}

constexpr inline size_t countLeadingZeros(uint32_t i) {
return __builtin_clz(i);
Expand Down Expand Up @@ -76,48 +106,6 @@ uint8_t log2(T v) {
return numberOfBitsIn<T>();
}

/* FIXME This could be the base class for Ion::Device::Regs::Register, but
* Register gets a little more specific with inline and volatile. This should be
* factorised still. */
template <typename T>
class BitField {
public:
constexpr BitField(T bits) : m_bits(bits) {}
constexpr BitField() : BitField(0) {}

operator T() const { return m_bits; }

constexpr T get() const { return m_bits; }

template <typename K>
bool get(K bit) const {
return get(bit, bit);
}

template <typename K>
T get(K high, K low) const {
return get(static_cast<size_t>(high), static_cast<size_t>(low));
}
constexpr T get(size_t high, size_t low) const {
return (m_bits >> low) & ((k_one << (high - low + 1)) - 1);
}

template <typename K>
constexpr BitField& set(K high, K low, T value) {
return set(static_cast<size_t>(high), static_cast<size_t>(low), value);
}
constexpr BitField& set(size_t high, size_t low, T value) {
T mask = ((k_one << (high - low + 1)) - 1) << low;
m_bits = (m_bits & ~mask) | ((value << low) & mask);
return *this;
}

protected:
constexpr static T k_one = static_cast<T>(1);

T m_bits;
};

} // namespace BitHelper
} // namespace OMG

Expand Down
12 changes: 9 additions & 3 deletions poincare/include/poincare/exam_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExamMode : public Ion::ExamMode::Configuration {
public:
using Ruleset = Ion::ExamMode::Ruleset;

struct PressToTestFlags : OMG::BitHelper::BitField<Ion::ExamMode::Int> {
struct PressToTestFlags {
CODE_GUARD(press_to_test_flags, 2156906052, //
enum class Flags
: size_t{
Expand All @@ -35,17 +35,23 @@ class ExamMode : public Ion::ExamMode::Configuration {
static_assert(static_cast<size_t>(Flags::NumberOfFlags) <=
Ion::ExamMode::Configuration::k_dataSize);

bool operator==(const PressToTestFlags& other) {
return m_bits == other.m_bits;
}

bool getFlag(Flags flag) const {
assert(static_cast<size_t>(flag) <
static_cast<size_t>(Flags::NumberOfFlags));
return BitField::get(flag);
return OMG::BitHelper::bitAtIndex(m_bits, flag);
}
PressToTestFlags& setFlag(Flags flag, bool value = true) {
assert(static_cast<size_t>(flag) <
static_cast<size_t>(Flags::NumberOfFlags));
BitField::set(flag, flag, 1);
OMG::BitHelper::setBitAtIndex(m_bits, flag, value);
return *this;
}

Ion::ExamMode::Int m_bits;
};
static_assert(sizeof(PressToTestFlags) == sizeof(Ion::ExamMode::Int));

Expand Down
12 changes: 10 additions & 2 deletions poincare/include/poincare/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,15 @@ class Preferences final {
}

private:
CODE_GUARD(poincare_preferences, 4011524516, //
int m_version = 0x20240401;
constexpr static int k_version = 0x20240401;

#if __EMSCRIPTEN__
emscripten_align1_int m_version;
#else
int m_version;
#endif

CODE_GUARD(poincare_preferences, 4027384180, //
CalculationPreferences m_calculationPreferences;
mutable ExamMode m_examMode;
/* This flag can only be asserted by writing it via DFU. When set,
Expand All @@ -178,6 +185,7 @@ class Preferences final {
mutable LogarithmBasePosition m_logarithmBasePosition;
mutable LogarithmKeyEvent m_logarithmKeyEvent;
mutable ParabolaParameter m_parabolaParameter;)

#if PLATFORM_DEVICE
/* Explicitly declare padding to ensure the structure of the class
* stays consistent across versions. */
Expand Down
16 changes: 8 additions & 8 deletions poincare/src/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ constexpr int Preferences::ShortNumberOfSignificantDigits;
constexpr int Preferences::VeryShortNumberOfSignificantDigits;

Preferences::Preferences()
: m_calculationPreferences{.angleUnit = AngleUnit::Radian,
.displayMode =
Preferences::PrintFloatMode::Decimal,
.editionMode = EditionMode::Edition2D,
.complexFormat =
Preferences::ComplexFormat::Real,
.numberOfSignificantDigits = Preferences::
DefaultNumberOfPrintedSignificantDigits},
: m_version(k_version),
m_calculationPreferences{
.angleUnit = AngleUnit::Radian,
.displayMode = Preferences::PrintFloatMode::Decimal,
.editionMode = EditionMode::Edition2D,
.complexFormat = Preferences::ComplexFormat::Real,
.numberOfSignificantDigits =
Preferences::DefaultNumberOfPrintedSignificantDigits},
m_forceExamModeReload(false) {}

Preferences::ComplexFormat Preferences::UpdatedComplexFormatWithExpressionInput(
Expand Down

0 comments on commit 5c64ac4

Please sign in to comment.