SaturnRingLibrary 0.9.1
SGL wrapper
 
Loading...
Searching...
No Matches
SRL::Math::Types::Fxp Class Reference

Detailed Description

Fixed-point arithmetic optimized for Saturn hardware.

Uses a 16.16 fixed-point representation where:

  • Upper 16 bits: Integer part
  • Lower 16 bits: Fractional part (1/65536 units)

Value range:

  • Minimum: -32768 (0x80000000)
  • Maximum: 32767.99998474 (0x7FFFFFFF)
  • Resolution: ~0000152587 (1/65536)

Performance features:

  • Compile-time evaluation with constexpr/consteval
  • Hardware-optimized multiplication using dmuls.l
  • Efficient division through hardware divider unit
  • Zero runtime floating-point operations

Example:

// Compile-time conversion (preferred)
constexpr Fxp a = 5; // 5 (0x00050000)
constexpr Fxp b = 2.5; // 2.5 (0x00028000)
// Runtime conversion
Fxp c = Fxp::Convert(someInt); // With range checking
Fxp d = Fxp::Convert(someFloat); // With performance warning
// Arithmetic operations
Fxp result = a * b; // 12.5 (0x000C8000)
int16_t i = result.As<int16_t>(); // 12
static constexpr Fxp Convert(const T &value)
Convert integral type to fixed-point with compile-time range validation.
Definition fxp.hpp:156
constexpr T As() const
Converts to the specified integer type.
Definition fxp.hpp:441
Note
All operations are designed for maximum efficiency on Saturn hardware. Avoid runtime floating-point conversions in performance-critical code.
The #pragma GCC optimize("O2") directive is used to enable optimizations that improve performance, specifically for fixed-point arithmetic operations. It ensures that the compiler generates efficient code that takes full advantage of the hardware capabilities, particularly in performance-critical sections. Without this optimization, the generated code may not perform as expected, leading to slower execution. This optimization is crucial for achieving optimal performance in the Fxp class, as it allows the compiler to apply various optimizations, such as dead code elimination, register allocation, and instruction scheduling. These optimizations can significantly improve the execution speed of the Fxp class, making it suitable for performance-critical applications.

#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 Fxpoperator%= (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 Fxpoperator*= (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 Fxpoperator*= (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 Fxpoperator+= (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 Fxpoperator-= (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 Fxpoperator/= (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 Fxpoperator/= (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 Fxpoperator<<= (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 Fxpoperator= (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 Fxpoperator>>= (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 &dividend, 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 ()