33#define USE_FIXED_OVERFLOW_CHECKS
40#define MUL_I64_I32_I32(a, b)\
42#define SQUARE_U64_FIXED(a)\
43 static_cast<std::uint64_t>(__emul((a).GetInternalValue(), (a).GetInternalValue()))
45#define MUL_I64_I32_I32(a, b)\
46 static_cast<std::int64_t>(a) * static_cast<std::int64_t>(b)
47#define SQUARE_U64_FIXED(a)\
48 static_cast<std::uint64_t>(static_cast<std::int64_t>((a).GetInternalValue()) * \
49 static_cast<std::int64_t>((a).GetInternalValue()))
53#ifndef USE_FIXED_OVERFLOW_CHECKS
55#define CheckSignedSubtractionOverflow(type, left, right, overflowWarning, underflowWarning)
56#define CheckSignedAdditionOverflow(type, left, right, overflowWarning, underflowWarning)
57#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning)
58#define CheckU32CastOverflow(var, targetType, overflowWarning)
59#define CheckUnsignedAdditionOverflow(result, operand, overflowWarning)
60#define CheckUnsignedSubtractionOverflow(result, operand, overflowWarning)
61#define CheckNegationOverflow(var, type, overflowWarning)
62#define CheckMultiplicationOverflow(type, left, right, overflowWarning, underflowWarning)
63#define CheckDivisionOverflow(type, left, right, overflowWarning)
67#define CheckSignedSubtractionOverflow(type, left, right, overflowWarning, underflowWarning) \
68 if(left > 0 && right < 0 && left > std::numeric_limits<type>::max() + right) \
69 debug_warn(overflowWarning); \
70 else if(left < 0 && right > 0 && left < std::numeric_limits<type>::min() + right) \
71 debug_warn(underflowWarning);
73#define CheckSignedAdditionOverflow(type, left, right, overflowWarning, underflowWarning) \
74 if(left > 0 && right > 0 && std::numeric_limits<type>::max() - left < right) \
75 debug_warn(overflowWarning); \
76 else if(left < 0 && right < 0 && std::numeric_limits<type>::min() - left > right) \
77 debug_warn(underflowWarning);
79#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning) \
80 if(var > std::numeric_limits<targetType>::max()) \
81 debug_warn(overflowWarning); \
82 else if(var < std::numeric_limits<targetType>::min()) \
83 debug_warn(underflowWarning);
85#define CheckU32CastOverflow(var, targetType, overflowWarning) \
86 if(var > static_cast<std::uint32_t>(std::numeric_limits<targetType>::max())) \
87 debug_warn(overflowWarning);
89#define CheckUnsignedAdditionOverflow(result, operand, overflowWarning) \
90 if(result < operand) \
91 debug_warn(overflowWarning);
93#define CheckUnsignedSubtractionOverflow(result, left, overflowWarning) \
95 debug_warn(overflowWarning);
97#define CheckNegationOverflow(var, type, overflowWarning) \
98 if(value == std::numeric_limits<type>::min()) \
99 debug_warn(overflowWarning);
101#define CheckMultiplicationOverflow(type, left, right, overflowWarning, underflowWarning) \
102 std::int64_t res##left = static_cast<std::int64_t>(left) * static_cast<std::int64_t>(right); \
103 CheckCastOverflow(res##left, type, overflowWarning, underflowWarning)
105#define CheckDivisionOverflow(type, left, right, overflowWarning) \
106 if(right == -1) { CheckNegationOverflow(left, type, overflowWarning) }
113 return (
T)(value >= 0 ? value + 0.5f : value - 0.5f);
119 return (
T)(value >= 0 ? value + 0.5 : value - 0.5);
127template<
typename T, T max_t,
int total_bits,
int int_bits,
int fract_bits_,
int fract_pow2>
157 return CFixed(
static_cast<int>(
static_cast<unsigned int>(n) <<
fract_bits) / d);
162 if (!std::isfinite(n))
164 float scaled = n * fract_pow2;
165 return CFixed(round_away_from_zero<T>(scaled));
170 if (!std::isfinite(n))
172 double scaled = n * fract_pow2;
173 return CFixed(round_away_from_zero<T>(scaled));
182 return (
float)
value / (float)fract_pow2;
188 return value / (double)fract_pow2;
284 std::int64_t result = t /
static_cast<std::int64_t
>(n.
value);
286 CheckCastOverflow(result,
T, L
"Overflow in CFixed::operator/(CFixed n)", L
"Underflow in CFixed::operator/(CFixed n)")
300 std::int64_t t =
static_cast<std::int64_t
>(
value) * n;
301 t = std::max(
static_cast<std::int64_t
>(std::numeric_limits<T>::min()),
302 std::min(
static_cast<std::int64_t
>(std::numeric_limits<T>::max()), t));
303 return CFixed(
static_cast<std::int32_t
>(t));
317 if (n.
value > 0 && t < 0)
319 else if (n.
value < 0 && t > 0)
336 CheckCastOverflow(t,
T, L
"Overflow in CFixed::Multiply(CFixed n)", L
"Underflow in CFixed::Multiply(CFixed n)")
354 CheckCastOverflow(t,
T, L
"Overflow in CFixed::Multiply(CFixed n)", L
"Underflow in CFixed::Multiply(CFixed n)")
387template<
typename T, T max_t,
int total_bits,
int int_bits,
int fract_bits_,
int fract_pow2>
388struct numeric_limits<
CFixed<
T, max_t, total_bits, int_bits, fract_bits_, fract_pow2> >
392 static const bool is_specialized =
true;
#define MUL_I64_I32_I32(a, b)
Definition: Fixed.h:45
#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning)
Definition: Fixed.h:79
T round_away_from_zero(float value)
Definition: Fixed.h:111
CFixed< std::int32_t, static_cast< std::int32_t >(0x7fffffff), 32, 15, 16, 65536 > CFixed_15_16
A fixed-point number class with 1-bit sign, 15-bit integral part, 16-bit fractional part.
Definition: Fixed.h:375
CFixed_15_16 atan2_approx(CFixed_15_16 y, CFixed_15_16 x)
Inaccurate approximation of atan2 over fixed-point numbers.
Definition: Fixed.cpp:149
#define CheckNegationOverflow(var, type, overflowWarning)
Definition: Fixed.h:97
#define CheckSignedAdditionOverflow(type, left, right, overflowWarning, underflowWarning)
Definition: Fixed.h:73
void sincos_approx(CFixed_15_16 a, CFixed_15_16 &sin_out, CFixed_15_16 &cos_out)
Compute sin(a) and cos(a).
Definition: Fixed.cpp:189
#define CheckSignedSubtractionOverflow(type, left, right, overflowWarning, underflowWarning)
Definition: Fixed.h:67
CFixed_15_16 fixed
Default fixed-point type used by the engine.
Definition: Fixed.h:380
#define CheckMultiplicationOverflow(type, left, right, overflowWarning, underflowWarning)
Definition: Fixed.h:101
#define CheckDivisionOverflow(type, left, right, overflowWarning)
Definition: Fixed.h:105
std::uint32_t isqrt64(std::uint64_t n)
64-bit integer square root.
Definition: Sqrt.cpp:23
A simple fixed-point number class.
Definition: Fixed.h:129
CFixed operator-(CFixed n) const
Subtract a CFixed. Might overflow.
Definition: Fixed.h:248
void SetInternalValue(T n)
Definition: Fixed.h:145
static constexpr CFixed FromFraction(int n, int d)
Definition: Fixed.h:155
double ToDouble() const
Convert to double. Won't be lossy - double can precisely represent all values.
Definition: Fixed.h:186
constexpr CFixed Absolute() const
Definition: Fixed.h:325
CStr8 ToString() const
Returns the shortest string such that FromString will parse to the correct value.
Definition: Fixed.cpp:97
constexpr CFixed MultiplyClamp(int n) const
Multiply by an integer. Avoids overflow by clamping to min/max representable value.
Definition: Fixed.h:298
CFixed operator/(int n) const
Divide by an integer. Must not have n == 0. Cannot overflow unless n == -1.
Definition: Fixed.h:307
static CFixed Zero()
Definition: Fixed.h:140
constexpr bool operator<(CFixed n) const
Numeric comparison.
Definition: Fixed.h:230
float ToFloat() const
Convert to float. May be lossy - float can't represent all values.
Definition: Fixed.h:180
@ fract_bits
Definition: Fixed.h:136
static constexpr CFixed FromFloat(float n)
Definition: Fixed.h:160
CFixed()
Definition: Fixed.h:138
constexpr CFixed(T v)
Definition: Fixed.h:133
constexpr CFixed & operator+=(CFixed n)
Add a CFixed. Might overflow.
Definition: Fixed.h:255
constexpr bool operator==(CFixed n) const
Equality.
Definition: Fixed.h:221
constexpr bool operator<=(CFixed n) const
Numeric comparison.
Definition: Fixed.h:227
constexpr CFixed Square() const
Multiply the value by itself.
Definition: Fixed.h:343
constexpr CFixed & operator-=(CFixed n)
Subtract a CFixed. Might overflow.
Definition: Fixed.h:258
constexpr int ToInt_RoundToInfinity() const
Definition: Fixed.h:199
CFixed Multiply(CFixed n) const
Multiply by a CFixed.
Definition: Fixed.h:331
CFixed operator<<(int n) const
Definition: Fixed.h:273
constexpr CFixed Sqrt() const
Definition: Fixed.h:358
constexpr int ToInt_RoundToNearest() const
Definition: Fixed.h:209
CFixed operator-() const
Negate a CFixed.
Definition: Fixed.h:261
CFixed operator/(float n) const
CFixed MulDiv(CFixed m, CFixed d) const
Compute this*m/d.
Definition: Fixed.h:351
constexpr int ToInt_RoundToNegInfinity() const
Definition: Fixed.h:204
constexpr bool operator>=(CFixed n) const
Numeric comparison.
Definition: Fixed.h:233
constexpr bool operator!=(CFixed n) const
Inequality.
Definition: Fixed.h:224
CFixed operator+(CFixed n) const
Add a CFixed. Might overflow.
Definition: Fixed.h:241
CFixed operator/(CFixed n) const
Divide by a CFixed. Must not have n.IsZero(). Might overflow.
Definition: Fixed.h:281
constexpr bool IsZero() const
Returns true if the number is precisely 0.
Definition: Fixed.h:218
T GetInternalValue() const
Definition: Fixed.h:144
static constexpr CFixed FromDouble(double n)
Definition: Fixed.h:168
constexpr CFixed operator%(CFixed n) const
Mod by a fixed. Must not have n == 0. Result has the same sign as n.
Definition: Fixed.h:314
constexpr int ToInt_RoundToZero() const
Definition: Fixed.h:191
T value
Definition: Fixed.h:131
CFixed operator*(float n) const
static constexpr CFixed FromInt(int n)
Definition: Fixed.h:149
constexpr bool operator>(CFixed n) const
Numeric comparison.
Definition: Fixed.h:236
static CFixed Epsilon()
Definition: Fixed.h:141
static CFixed FromString(const CStr8 &s)
Definition: Fixed.cpp:28
CFixed operator*(int n) const
Multiply by an integer. Might overflow.
Definition: Fixed.h:291
static CFixed Pi()
Definition: Fixed.cpp:184
CFixed operator>>(int n) const
Definition: Fixed.h:267
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:250
Definition: ShaderDefines.cpp:43
#define T(string_literal)
Definition: secure_crt.cpp:81
static fixed max()
Definition: Fixed.h:394
static fixed min()
Definition: Fixed.h:393
CFixed< T, max_t, total_bits, int_bits, fract_bits_, fract_pow2 > fixed
Definition: Fixed.h:390