15template<
unsigned FRACTION_BITS_>
29 static constexpr float INV_ONE_F = 1.0f / ONE;
33 static constexpr double INV_ONE_D = 1.0 / ONE;
38 static constexpr int FRACTION_MASK = ONE - 1;
64 [[nodiscard]]
static constexpr int shiftHelper(
int x,
int s) {
65 return (s >= 0) ? (x >> s) : (x << -s);
67 template<
unsigned BITS2>
77 [[nodiscard]]
constexpr int toInt()
const {
84 [[nodiscard]]
constexpr float toFloat()
const {
85 return narrow_cast<float>(value) * INV_ONE_F;
91 [[nodiscard]]
constexpr double toDouble()
const {
92 return narrow_cast<double>(value) * INV_ONE_D;
102 return narrow_cast<float>(value & FRACTION_MASK) * INV_ONE_F;
112 return narrow_cast<double>(value & FRACTION_MASK) * INV_ONE_D;
122 return value / other.value;
130 return create(value & ~FRACTION_MASK);
138 return create(value & FRACTION_MASK);
146 return value & FRACTION_MASK;
152 return create(x.value + y.value);
155 return create(x.value - y.value);
159 (
static_cast<int64_t
>(x.value) * y.value) >>
FRACTION_BITS));
162 return create(x.value * y);
165 return create(x * y.value);
173 (
static_cast<int64_t
>(x.value) <<
FRACTION_BITS) / y.value));
176 return create(x.value / y);
179 return create(x.value << y);
182 return create(x.value >> y);
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 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 To narrow_cast(From &&from) noexcept