26 #ifndef COM_SAXBOPHONE_UNMOVING_PSX_FIXED_HPP
27 #define COM_SAXBOPHONE_UNMOVING_PSX_FIXED_HPP
35 #include <sys/types.h>
57 constexpr
PSXFixed operator"" _fx(
long double literal);
73 constexpr
PSXFixed operator"" _fx(
unsigned long long int literal);
169 double remainder = scaled - integral;
171 if (remainder <= -0.5 or remainder >= 0.5) {
172 integral += (integral < 0 ? -1 : 1);
174 this->_raw_value = integral;
191 return this->_raw_value;
202 explicit constexpr
operator double()
const {
216 explicit constexpr
operator float()
const {
218 return (
float)(double)*
this;
242 constexpr
bool to_c_str(
char* buffer,
size_t buffer_size)
const {
244 if (buffer ==
nullptr) {
return false; }
246 if (buffer_size < 15) {
return false; }
247 int decimal_part = this->_raw_value / 4096;
249 unsigned int remainder = ((
unsigned int)abs(this->_raw_value)) % 4096;
251 unsigned int fractional_part = (remainder * 1'000'000) / 4096;
253 if (this->_raw_value < 0 and decimal_part == 0) {
254 snprintf(buffer, buffer_size,
"-0.%06u", fractional_part);
256 snprintf(buffer, buffer_size,
"%d.%06u", decimal_part, fractional_part);
294 this->_raw_value += rhs._raw_value;
301 this->_raw_value -= rhs._raw_value;
314 int64_t result = (int64_t)this->_raw_value * rhs._raw_value;
324 this->_raw_value *= rhs;
346 this->_raw_value /= rhs;
408 constexpr
PSXFixed operator"" _fx(
long double literal) {
412 constexpr
PSXFixed operator"" _fx(
unsigned long long int literal) {
PSXFixed-point arithmetic value type for Sony PlayStation.
Definition: PSXFixed.hpp:85
static constexpr double FRACTIONAL_MAX
Largest real value representable by the fixed-point type.
Definition: PSXFixed.hpp:123
constexpr UnderlyingType to_integer() const
Definition: PSXFixed.hpp:223
constexpr PSXFixed & operator-=(const PSXFixed &rhs)
Compound assignment subtraction operator.
Definition: PSXFixed.hpp:300
int32_t UnderlyingType
Underlying base type the fixed-point integer is stored as.
Definition: PSXFixed.hpp:92
constexpr PSXFixed & operator*=(const PSXFixed &rhs)
Compound assignment multiplication operator.
Definition: PSXFixed.hpp:312
static constexpr PSXFixed MIN()
Smallest PSXFixed value.
Definition: PSXFixed.hpp:137
constexpr bool to_c_str(char *buffer, size_t buffer_size) const
Stringifies the PSXFixed-point value to a C-string.
Definition: PSXFixed.hpp:242
static constexpr double FRACTIONAL_MIN
Smallest real value representable by the fixed-point type.
Definition: PSXFixed.hpp:127
static constexpr PSXFixed MAX()
Largest PSXFixed value.
Definition: PSXFixed.hpp:131
constexpr PSXFixed(double value)
Implicit converting constructor from float/double.
Definition: PSXFixed.hpp:165
static constexpr UnderlyingType DECIMAL_MAX
Largest integer value representable by the fixed-point type.
Definition: PSXFixed.hpp:115
constexpr friend PSXFixed operator*(PSXFixed lhs, const UnderlyingType &rhs)
Integer multiplication operator.
Definition: PSXFixed.hpp:379
constexpr PSXFixed operator++(int)
Postfix increment operator.
Definition: PSXFixed.hpp:277
constexpr friend PSXFixed operator-(PSXFixed lhs, const PSXFixed &rhs)
Subtraction operator.
Definition: PSXFixed.hpp:365
constexpr friend PSXFixed operator/(PSXFixed lhs, const PSXFixed &rhs)
Division operator.
Definition: PSXFixed.hpp:392
static constexpr double ACCURACY
The largest difference between a fixed-point value and the "true" value it represents.
Definition: PSXFixed.hpp:111
constexpr PSXFixed(UnderlyingType raw_value)
Implicit converting constructor from fixed-point integer.
Definition: PSXFixed.hpp:152
constexpr friend PSXFixed operator+(PSXFixed lhs, const PSXFixed &rhs)
Addition operator.
Definition: PSXFixed.hpp:358
static constexpr double PRECISION
How far apart two adjacent fixed-point values are.
Definition: PSXFixed.hpp:106
constexpr friend PSXFixed operator*(UnderlyingType lhs, const PSXFixed &rhs)
Integer multiplication operator.
Definition: PSXFixed.hpp:386
constexpr PSXFixed & operator++()
Prefix increment operator.
Definition: PSXFixed.hpp:263
static constexpr size_t DECIMAL_BITS
How many bits are used for the integer part of the fixed-point integer.
Definition: PSXFixed.hpp:94
constexpr PSXFixed & operator+=(const PSXFixed &rhs)
Compound assignment addition operator.
Definition: PSXFixed.hpp:293
static constexpr size_t FRACTION_BITS
How many bits are used for the fraction part of the fixed-point integer.
Definition: PSXFixed.hpp:96
constexpr PSXFixed()
Default constructor, creates a PSXFixed instance with value 0.0_fx
Definition: PSXFixed.hpp:143
constexpr PSXFixed operator--(int)
Postfix decrement operator.
Definition: PSXFixed.hpp:285
constexpr friend PSXFixed operator*(PSXFixed lhs, const PSXFixed &rhs)
Multiplication operator.
Definition: PSXFixed.hpp:372
constexpr PSXFixed & operator/=(const PSXFixed &rhs)
Compound assignment division operator.
Definition: PSXFixed.hpp:335
static constexpr PSXFixed from_integer(int value)
Definition: PSXFixed.hpp:184
static constexpr UnderlyingType SCALE
The scale used for the fixed-point integer.
Definition: PSXFixed.hpp:102
constexpr PSXFixed & operator--()
Prefix decrement operator.
Definition: PSXFixed.hpp:270
constexpr friend PSXFixed operator/(PSXFixed lhs, const UnderlyingType &rhs)
Integer division operator.
Definition: PSXFixed.hpp:399
static constexpr UnderlyingType DECIMAL_MIN
Smallest integer value representable by the fixed-point type.
Definition: PSXFixed.hpp:119
constexpr PSXFixed operator-() const
Unary minus (negation) operator.
Definition: PSXFixed.hpp:352
Definition: PSXFixed.hpp:44