14 template<
unsigned FRACTION_BITS_>
28 static constexpr
float INV_ONE_F = 1.0f / ONE;
32 static constexpr
double INV_ONE_D = 1.0 / ONE;
37 static constexpr
int FRACTION_MASK = ONE - 1;
57 explicit FixedPoint(
double d) : value(lrint (d * ONE)) {}
64 return (s >= 0) ? (
x >> s) : (
x << -s);
66 template<
unsigned BITS2>
76 [[nodiscard]] constexpr
int toInt()
const {
83 [[nodiscard]] constexpr
float toFloat()
const {
84 return value * INV_ONE_F;
90 [[nodiscard]] constexpr
double toDouble()
const {
91 return value * INV_ONE_D;
101 return (value & FRACTION_MASK) * INV_ONE_F;
111 return (value & FRACTION_MASK) * INV_ONE_D;
121 return value / other.value;
129 return create(value & ~FRACTION_MASK);
137 return create(value & FRACTION_MASK);
145 return value & FRACTION_MASK;
151 return create(
x.value + y.value);
154 return create(
x.value - y.value);
191 value += other.value;
194 value -= other.value;
210 template<
typename Archive>
213 ar.serialize(
"value", value);
217 struct CreateRawTag {};
218 constexpr
FixedPoint(
int raw_value, CreateRawTag)
219 : value(raw_value) {}
A fixed point number, implemented by a 32-bit signed integer.
static constexpr FixedPoint create(int value)
Create new fixed point object from given representation.
constexpr bool operator==(const FixedPoint &) const =default
constexpr friend FixedPoint operator-(FixedPoint x, FixedPoint y)
constexpr void operator-=(FixedPoint other)
static constexpr FixedPoint roundRatioDown(unsigned n, unsigned d)
constexpr int divAsInt(FixedPoint other) const
Returns the result of a division between this fixed point number and another, rounded towards zero.
static constexpr unsigned FRACTION_BITS
Number of fractional bits (export template parameter as a constant so that external code can use it m...
constexpr friend FixedPoint operator*(FixedPoint x, int y)
constexpr double fractionAsDouble() const
Returns the fractional part of this fixed point number as a double.
constexpr FixedPoint(unsigned i)
constexpr double toDouble() const
Returns the double value that corresponds to this fixed point number.
constexpr friend FixedPoint operator/(FixedPoint x, FixedPoint y)
Divides two fixed point numbers.
constexpr friend FixedPoint operator*(FixedPoint x, FixedPoint y)
constexpr friend FixedPoint operator>>(FixedPoint x, int y)
constexpr FixedPoint fract() const
Returns the fractional part of this value.
constexpr int toInt() const
Returns the integer part (rounded down) of this fixed point number.
constexpr friend FixedPoint operator*(int x, FixedPoint y)
constexpr FixedPoint(int i)
constexpr int getRawValue() const
constexpr float toFloat() const
Returns the float value that corresponds to this fixed point number.
static constexpr int shiftHelper(int x, int s)
constexpr unsigned fractAsInt() const
Returns the fractional part of this value as an integer.
constexpr float fractionAsFloat() const
Returns the fractional part of this fixed point number as a float.
constexpr friend FixedPoint operator<<(FixedPoint x, int y)
constexpr FixedPoint(FixedPoint< BITS2 > other)
constexpr friend FixedPoint operator+(FixedPoint x, FixedPoint y)
constexpr void addQuantum()
Increase this value with the smallest possible amount.
constexpr friend FixedPoint operator/(FixedPoint x, int y)
void serialize(Archive &ar, unsigned)
constexpr FixedPoint()=default
Creates a zero-initialized fixed point object.
constexpr auto operator<=>(const FixedPoint &) const =default
constexpr void operator+=(FixedPoint other)
constexpr FixedPoint floor() const
Returns this value rounded down.
This file implemented 3 utility functions:
constexpr KeyMatrixPosition x
Keyboard bindings.