Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git

fixed.h (3378B) · raw


      1 /// @file core/fixed.h
      2 /// @brief Fixed point numbers with 16 fractional bits, for the simulation.
      3 ///
      4 /// Positions and speeds of the simulation are held as integers, because sums
      5 /// of integers come out the same on every machine. A fixed is an integer count
      6 /// of 1/65536, so it steps much finer than a pixel and a speed of a fraction of
      7 /// a pixel per step is not rounded away. The raster code has its own 28.4
      8 /// format, Point, for screen positions; convert with floor() or round() before
      9 /// drawing.
     10 ///
     11 /// The range is -32768 up to (but not including) 32768, and nothing checks it:
     12 /// a sum or product outside of it overflows the 32 bits, as the plain integers
     13 /// do.
     14 #pragma once
     15 
     16 #include <compare>
     17 
     18 #include "core/types.h"
     19 
     20 namespace kumo {
     21 
     22 /// @brief A number with 16 fractional bits, stored in a 32-bit integer.
     23 struct Fixed {
     24   /// @brief Number of fractional bits.
     25   static constexpr int kFractionBits = 16;
     26 
     27   /// @brief The raw value of 1.
     28   static constexpr int32_t kOne = 1 << kFractionBits;
     29 
     30   int32_t raw = 0;  ///< The value times 65536.
     31 
     32   /// @brief The Fixed whose raw value is @p raw.
     33   static constexpr Fixed from_raw(int32_t raw) { return Fixed{raw}; }
     34 
     35   /// @brief The whole number @p value, which must be within -32768 to 32767.
     36   static constexpr Fixed from_int(int value) { return Fixed{value * kOne}; }
     37 
     38   /// @brief The fraction @p numerator / @p denominator, rounded toward zero.
     39   ///
     40   /// Within two integers, so that no floating point enters: a speed of one and
     41   /// a half pixels per step is `Fixed::ratio(3, 2)`.
     42   ///
     43   /// @param numerator   Any value that keeps the result in range.
     44   /// @param denominator Not zero.
     45   static constexpr Fixed ratio(int64_t numerator, int64_t denominator) {
     46     return Fixed(static_cast<int32_t>(numerator * kOne / denominator));
     47   }
     48 
     49   /// @brief The largest whole number that is not above this one.
     50   ///
     51   /// -0.5 gives -1, not 0. The shift of a negative number is arithmetic in
     52   /// C++20, so this holds on every platform.
     53   constexpr int floor() const { return raw >> kFractionBits; }
     54 
     55   /// @brief The nearest whole number; halves round up, so -0.5 gives 0.
     56   constexpr int round() const { return (raw + kOne / 2) >> kFractionBits; }
     57 
     58   constexpr Fixed operator-() const { return Fixed{-raw}; }
     59   constexpr Fixed operator+(Fixed other) const {
     60     return Fixed{raw + other.raw};
     61   }
     62   constexpr Fixed operator-(Fixed other) const {
     63     return Fixed{raw - other.raw};
     64   }
     65 
     66   /// @brief The product, rounded toward minus infinity.
     67   ///
     68   /// The 64-bit product keeps every bit until the shift.
     69   constexpr Fixed operator*(Fixed other) const {
     70     const int64_t product = static_cast<int64_t>(raw) * other.raw;
     71     return Fixed{static_cast<int32_t>(product >> kFractionBits)};
     72   }
     73 
     74   constexpr Fixed& operator+=(Fixed other) { return *this = *this + other; }
     75   constexpr Fixed& operator-=(Fixed other) { return *this = *this - other; }
     76 
     77   constexpr bool operator==(const Fixed&) const = default;
     78   constexpr auto operator<=>(const Fixed&) const = default;
     79 };
     80 
     81 /// @brief The point between @p a and @p b that is @p t of the way from a to b.
     82 ///
     83 /// Exact at both ends: lerp(a, b, 0) is a and lerp(a, b, 1) is b.
     84 /// @param t Usually from 0 to 1; outside of it the result goes past an end.
     85 constexpr Fixed lerp(Fixed a, Fixed b, Fixed t) { return a + (b - a) * t; }
     86 
     87 }  // namespace kumo