Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 9cf998cea8f92693252af9e6a28a6b770bab6ac0
parent 242ce4fb8488980e61f0c038cebc56ae78f47958
Author: sentomk <sentomk@proton.me>
Date:   Tue, 27 Oct 2026 20:14:31 +0800

feat: add 16.16 fixed point numbers

Fixed is a 32-bit integer that counts in 1/65536, for the positions and
speeds of the simulation. Sums and products of integers come out the
same on every machine, which floating point does not promise, and
1/65536 of a pixel is fine enough that a speed of a fraction of a pixel
per step is not rounded away. The raster code keeps its 28.4 Point for
screen positions; floor() and round() convert between the two.

The rounding is fixed and tested, also for negative values: ratio()
rounds toward zero, floor() and the product round toward minus infinity
(an arithmetic shift, which C++20 defines), and round() takes halves up.
The product is taken in 64 bits, so no bit is lost before the shift.
lerp() is exact at both ends, so drawing between two states shows each
of them unchanged at t of 0 and 1.

The range is -32768 up to 32768 and noting checks it: leaving it
overflows as an int does.

Diffstat:
Asrc/core/fixed.h | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/unit/fixed_test.cpp | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/src/core/fixed.h b/src/core/fixed.h @@ -0,0 +1,87 @@ +/// @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 <compare> + +#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<int32_t>(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<int64_t>(raw) * other.raw; + return Fixed{static_cast<int32_t>(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 diff --git a/tests/unit/fixed_test.cpp b/tests/unit/fixed_test.cpp @@ -0,0 +1,115 @@ +#include "core/fixed.h" + +#include "check.h" +#include "core/types.h" + +using kumo::Fixed; +using kumo::lerp; + +TEST(fixed_whole_numbers_keep_their_value) { + CHECK_EQ(Fixed::from_int(0).raw, 0); + CHECK_EQ(Fixed::from_int(1).raw, 65536); + CHECK_EQ(Fixed::from_int(-3).raw, -3 * 65536); + CHECK_EQ(Fixed::from_int(32767).floor(), 32767); + CHECK_EQ(Fixed::from_int(-32768).floor(), -32768); +} + +TEST(fixed_ratio_is_exact_where_the_value_can_be) { + CHECK_EQ(Fixed::ratio(1, 2).raw, 32768); + CHECK_EQ(Fixed::ratio(3, 2).raw, 98304); + CHECK_EQ(Fixed::ratio(-5, 4).raw, -81920); + CHECK_EQ(Fixed::ratio(7, 1), Fixed::from_int(7)); +} + +TEST(fixed_ratio_rounds_toward_zero) { + CHECK_EQ(Fixed::ratio(1, 3).raw, 21845); + CHECK_EQ(Fixed::ratio(-1, 3).raw, -21845); + // Three thirds fall short of one by a little, and never go over. + CHECK_EQ((Fixed::ratio(1, 3) + Fixed::ratio(1, 3) + Fixed::ratio(1, 3)).raw, + 65535); +} + +TEST(fixed_floor_goes_toward_minus_infinity) { + CHECK_EQ(Fixed::from_raw(0).floor(), 0); + CHECK_EQ(Fixed::from_raw(65535).floor(), 0); + CHECK_EQ(Fixed::from_raw(65536).floor(), 1); + CHECK_EQ(Fixed::from_raw(-1).floor(), -1); + CHECK_EQ(Fixed::from_raw(-65536).floor(), -1); + CHECK_EQ(Fixed::from_raw(-65537).floor(), -2); +} + +TEST(fixed_round_takes_halves_up) { + CHECK_EQ(Fixed::from_raw(32767).round(), 0); + CHECK_EQ(Fixed::from_raw(32768).round(), 1); + CHECK_EQ(Fixed::from_raw(-32768).round(), 0); + CHECK_EQ(Fixed::from_raw(-32769).round(), -1); + CHECK_EQ(Fixed::from_int(-4).round(), -4); +} + +TEST(fixed_addition_and_subtraction_are_exact) { + const Fixed a = Fixed::ratio(5, 4); + const Fixed b = Fixed::ratio(-7, 8); + CHECK_EQ((a + b).raw, 24576); + CHECK_EQ((a - b).raw, 139264); + CHECK_EQ((a + b) - b, a); + CHECK_EQ((-a).raw, -a.raw); + Fixed c = a; + c += b; + c -= b; + CHECK_EQ(c, a); +} + +TEST(fixed_multiplication) { + CHECK_EQ((Fixed::ratio(3, 2) * Fixed::ratio(5, 2)).raw, 245760); // 3.75 + CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(5, 2)).raw, -245760); + CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(-5, 2)).raw, 245760); + CHECK_EQ(Fixed::from_int(123) * Fixed::from_int(-45), Fixed::from_int(-5535)); + // By one is the identity, for any value. + CHECK_EQ(Fixed::from_raw(12345) * Fixed::from_int(1), Fixed::from_raw(12345)); +} + +TEST(fixed_multiplication_rounds_toward_minus_infinity) { + // The smallest step times the smallest step is far below the smallest step. + CHECK_EQ((Fixed::from_raw(1) * Fixed::from_raw(1)).raw, 0); + CHECK_EQ((Fixed::from_raw(-1) * Fixed::from_raw(1)).raw, -1); +} + +TEST(fixed_multiplication_does_not_lose_bits_to_the_intermediate) { + // 30000 * 30000 does not fit in 32 bits before the shift: the product is + // taken in 64 bits. + CHECK_EQ(Fixed::from_int(180) * Fixed::from_int(180), Fixed::from_int(32400)); + CHECK_EQ(Fixed::from_int(-180) * Fixed::from_int(180), + Fixed::from_int(-32400)); +} + +TEST(fixed_comparisons_follow_the_value) { + CHECK(Fixed::from_int(-1) < Fixed::from_int(0)); + CHECK(Fixed::from_raw(1) > Fixed::from_int(0)); + CHECK(Fixed::from_int(2) >= Fixed::from_int(2)); + CHECK(Fixed::from_int(2) <= Fixed::from_int(2)); + CHECK(Fixed::from_int(2) != Fixed::from_int(3)); + CHECK(Fixed{} == Fixed::from_int(0)); +} + +TEST(fixed_lerp_is_exact_at_both_ends) { + const Fixed a = Fixed::ratio(-17, 3); + const Fixed b = Fixed::ratio(101, 7); + CHECK_EQ(lerp(a, b, Fixed{}), a); + CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b); +} + +TEST(fixed_lerp_in_between) { + CHECK_EQ(lerp(Fixed::from_int(10), Fixed::from_int(20), Fixed::ratio(1, 2)), + Fixed::from_int(15)); + CHECK_EQ(lerp(Fixed::from_int(20), Fixed::from_int(10), Fixed::ratio(1, 4)), + Fixed::ratio(35, 2)); + // The same value twice stays where it is, whatever t is. + CHECK_EQ(lerp(Fixed::from_int(7), Fixed::from_int(7), Fixed::ratio(2, 3)), + Fixed::from_int(7)); +} + +TEST(fixed_is_constexpr) { + constexpr Fixed value = Fixed::ratio(3, 2) * Fixed::from_int(2); + static_assert(value == Fixed::from_int(3)); + CHECK_EQ(value.floor(), 3); +}