/// @file core/fixed.h /// @brief Fixed point numbers with 16 fractional bits, for the simulation. /// /// Positions and speeds of the simulation are held as integers, because sums /// of integers come out the same on every machine. A fixed is an integer count /// of 1/65536, so it steps much finer than a pixel and a speed of a fraction of /// a pixel per step is not rounded away. The raster code has its own 28.4 /// format, Point, for screen positions; convert with floor() or round() before /// drawing. /// /// The range is -32768 up to (but not including) 32768, and nothing checks it: /// a sum or product outside of it overflows the 32 bits, as the plain integers /// do. #pragma once #include #include "core/types.h" namespace kumo { /// @brief A number with 16 fractional bits, stored in a 32-bit integer. struct Fixed { /// @brief Number of fractional bits. static constexpr int kFractionBits = 16; /// @brief The raw value of 1. static constexpr int32_t kOne = 1 << kFractionBits; int32_t raw = 0; ///< The value times 65536. /// @brief The Fixed whose raw value is @p raw. static constexpr Fixed from_raw(int32_t raw) { return Fixed{raw}; } /// @brief The whole number @p value, which must be within -32768 to 32767. static constexpr Fixed from_int(int value) { return Fixed{value * kOne}; } /// @brief The fraction @p numerator / @p denominator, rounded toward zero. /// /// Within two integers, so that no floating point enters: a speed of one and /// a half pixels per step is `Fixed::ratio(3, 2)`. /// /// @param numerator Any value that keeps the result in range. /// @param denominator Not zero. static constexpr Fixed ratio(int64_t numerator, int64_t denominator) { return Fixed(static_cast(numerator * kOne / denominator)); } /// @brief The largest whole number that is not above this one. /// /// -0.5 gives -1, not 0. The shift of a negative number is arithmetic in /// C++20, so this holds on every platform. constexpr int floor() const { return raw >> kFractionBits; } /// @brief The nearest whole number; halves round up, so -0.5 gives 0. constexpr int round() const { return (raw + kOne / 2) >> kFractionBits; } constexpr Fixed operator-() const { return Fixed{-raw}; } constexpr Fixed operator+(Fixed other) const { return Fixed{raw + other.raw}; } constexpr Fixed operator-(Fixed other) const { return Fixed{raw - other.raw}; } /// @brief The product, rounded toward minus infinity. /// /// The 64-bit product keeps every bit until the shift. constexpr Fixed operator*(Fixed other) const { const int64_t product = static_cast(raw) * other.raw; return Fixed{static_cast(product >> kFractionBits)}; } constexpr Fixed& operator+=(Fixed other) { return *this = *this + other; } constexpr Fixed& operator-=(Fixed other) { return *this = *this - other; } constexpr bool operator==(const Fixed&) const = default; constexpr auto operator<=>(const Fixed&) const = default; }; /// @brief The point between @p a and @p b that is @p t of the way from a to b. /// /// Exact at both ends: lerp(a, b, 0) is a and lerp(a, b, 1) is b. /// @param t Usually from 0 to 1; outside of it the result goes past an end. constexpr Fixed lerp(Fixed a, Fixed b, Fixed t) { return a + (b - a) * t; } } // namespace kumo