Fixed-point arithmetic optimized for Saturn hardware.
Uses a 16.16 fixed-point representation where:
Value range:
Performance features:
Example:
#include <fxp.hpp>
Public Member Functions | |
| constexpr | Fxp () |
| Default constructor for Fxp class. Initializes the fixed-point number to zero. | |
| constexpr | Fxp (const Fxp &fxp) |
| Copy constructor for Fxp class. | |
| constexpr | Fxp (const int16_t &value) |
| Constructor for Fxp class from a 16-bit signed integer. | |
| template<typename T> requires (!std::is_same_v<T, int16_t>) | |
| consteval | Fxp (const T &value) |
| Compile-time constructor for numeric types other than int16_t. | |
| constexpr Fxp | Abs () const |
| Absolute value (|x|). | |
| template<typename T> requires std::integral<T> | |
| constexpr T | As () const |
| Converts to the specified integer type. | |
| template<typename T> requires std::floating_point<T> | |
| constexpr T | As () const |
| Converts to the specified floating-point type. | |
| constexpr Fxp | Ceil () const |
| Rounds up to the nearest integer. | |
| constexpr Fxp | Clamp (const Fxp &min, const Fxp &max) const |
| Clamps this value between minimum and maximum bounds. | |
| constexpr Fxp | Floor () const |
| Rounds down to the nearest integer. | |
| constexpr Fxp | GetFraction () const |
| Extracts the fractional part of the fixed-point value. | |
| constexpr bool | operator!= (const Fxp &fxp) const |
| Compare two Fxp objects for inequality. | |
| constexpr Fxp | operator% (const Fxp &fxp) const |
| Computes the modulo of the current fixed-point value with another fixed-point value (a % b). | |
| constexpr Fxp & | operator%= (const Fxp &fxp) |
| Computes the modulo of the current fixed-point value with another fixed-point value (a %= b). | |
| constexpr Fxp | operator* (const Fxp &fxp) const |
| Fixed-point multiplication (a * b). | |
| template<typename T> requires std::is_integral_v<T> | |
| constexpr Fxp | operator* (const T &value) const |
| Multiplies the current fixed-point value by an integer (a * b). | |
| constexpr Fxp & | operator*= (const Fxp &fxp) |
| Multiplies the current fixed-point value by another fixed-point value (a *= b). | |
| template<typename T> requires std::is_integral_v<T> | |
| constexpr Fxp & | operator*= (const T &value) |
| Multiplies the current fixed-point value by an integer (a *= b). | |
| constexpr Fxp | operator+ (const Fxp &fxp) const |
| Add another Fxp object to this object. | |
| constexpr Fxp & | operator+= (const Fxp &fxp) |
| Fixed-point addition (a += b). | |
| constexpr Fxp | operator- () const |
| Negate the value. | |
| constexpr Fxp | operator- (const Fxp &fxp) const |
| Subtract another Fxp object from this object. | |
| constexpr Fxp & | operator-= (const Fxp &fxp) |
| Fixed-point subtraction (a -= b). | |
| constexpr Fxp | operator/ (const Fxp &fxp) const |
| Divides the current fixed-point value by another fixed-point value (a / b). | |
| template<typename T> requires std::is_integral_v<T> | |
| constexpr Fxp | operator/ (const T &value) const |
| Divides the current fixed-point value by an integer (a / b). | |
| constexpr Fxp & | operator/= (const Fxp &fxp) |
| Divides the current fixed-point value by another fixed-point value (a /= b). | |
| template<typename T> requires std::is_integral_v<T> | |
| constexpr Fxp & | operator/= (const T &value) |
| Divides the current fixed-point value by an integer (a /= b). | |
| constexpr bool | operator< (const Fxp &fxp) const |
| Compare two Fxp objects for less than. | |
| constexpr Fxp | operator<< (const size_t &shiftAmount) const |
| Left shift operator for shifting the internal value by a specified number of bits. | |
| constexpr Fxp & | operator<<= (const size_t &shiftAmount) |
| In-place left shift operator for shifting the internal value by a specified number of bits. | |
| constexpr bool | operator<= (const Fxp &fxp) const |
| Compare two Fxp objects for less than or equal to. | |
| constexpr Fxp & | operator= (const Fxp &)=default |
| Copy assignment operator. | |
| constexpr bool | operator== (const Fxp &fxp) const |
| Compare two Fxp objects for equality. | |
| constexpr bool | operator> (const Fxp &fxp) const |
| Compare two Fxp objects for greater than. | |
| constexpr bool | operator>= (const Fxp &fxp) const |
| Compare two Fxp objects for greater than or equal to. | |
| constexpr Fxp | operator>> (const size_t &shiftAmount) const |
| Right shift operator for logical right shift. | |
| constexpr Fxp & | operator>>= (const size_t &shiftAmount) |
| Right shift and assign operator for logical right shift. | |
| constexpr Fxp | Pow (const Fxp &exponent) const |
| Power function for fixed-point numbers. | |
| constexpr const int32_t & | RawValue () const |
| Returns a const reference to the internal raw fixed-point value. | |
| constexpr Fxp | Round () const |
| Rounds to the nearest integer. | |
| template<Precision P = Precision::Default> | |
| constexpr Fxp | Sqrt () const |
| Calculate square root with configurable precision. | |
| constexpr Fxp | Square () const |
| Squares the value (x²). | |
| constexpr Fxp | TruncateFraction () const |
| Removes fractional part, keeping only integer portion. | |
Static Public Member Functions | |
| static Fxp | AsyncDivGetRemainder () |
| Retrieves remainder from hardware division unit. | |
| static Fxp | AsyncDivGetResult () |
| Retrieves result from hardware division unit. | |
| static void | AsyncDivSet (const Fxp ÷nd, const Fxp &divisor) |
| Sets up hardware division unit for fixed-point division. | |
| static constexpr Fxp | BuildRaw (const int32_t &rawValue) |
| Creates fixed-point from raw 16.16 value. | |
| static void | ClearMac () |
| Clears the MAC (Multiply-and-Accumulate) registers. | |
| template<std::integral T> | |
| static constexpr Fxp | Convert (const T &value) |
| Convert integral type to fixed-point with compile-time range validation. | |
| template<std::floating_point T> | |
| static Fxp | Convert (const T &value) |
| Convert floating-point to fixed-point with performance warning. | |
| static Fxp | ExtractMac () |
| Extracts the result from MAC registers into a fixed-point value. | |
| static constexpr Fxp | Max (const Fxp &a, const Fxp &b) |
| Returns the larger of two fixed-point values. | |
| static consteval Fxp | MaxValue () |
| static constexpr Fxp | Min (const Fxp &a, const Fxp &b) |
| Returns the smaller of two fixed-point values. | |
| static consteval Fxp | MinValue () |