23 template<
typename T = Int64,
unsigned int P = 20>
struct FixedPoint {
24 static_assert(P <=
sizeof(T) * 8);
32 static const constexpr T one = T(1) << P;
43 constexpr explicit FixedPoint(T raw) noexcept : raw(raw) {}
56 : raw(
static_cast<T
>(f * one)) {}
60 [[nodiscard]]
explicit constexpr operator float()
const {
return static_cast<float>(raw) / one; }
65 const double d) noexcept
66 : raw(
static_cast<T
>(d *
double(one))) {}
70 [[nodiscard]]
explicit constexpr operator double()
const {
return double(raw) / double(one); }
75 long double d) noexcept
76 : raw(
static_cast<T
>(d * one)) {}
80 [[nodiscard]]
explicit constexpr operator long double()
const {
return static_cast<long double>(raw) / one; }
87 template<
typename TT,
unsigned int TP>
90 : raw(other.template convert<T, P>()) {}
92 template<
typename TT,
unsigned int TP> [[nodiscard]] TT
convert()
const {
93 if constexpr(TP > P) {
94 return static_cast<TT
>(raw) << (TP - P);
96 return static_cast<TT
>(raw) >> (P - TP);
138 template<
typename T,
unsigned int P>
149 template<
typename T,
unsigned int P>
160 template<
typename T,
unsigned int P>
162 using UT =
typename std::make_unsigned<T>::type;
164 constexpr size_t bits =
sizeof(
T) * 8;
165 constexpr size_t halfBits = bits / 2;
166 constexpr UT halfMask = (
UT(1) << (4 *
sizeof(
T))) -
UT(1);
168 UT a =
UT(x.raw) >> halfBits;
169 UT b =
UT(x.raw) & halfMask;
170 UT c =
UT(y.raw) >> halfBits;
171 UT d =
UT(y.raw) & halfMask;
178 UT mid34 = (
bd >> halfBits) + (
bc & halfMask) + (
ad & halfMask);
183 if constexpr(std::is_signed<T>::value) {
193 raw |=
UT(std::numeric_limits<T>::lowest());
207 template<
typename T,
unsigned int P>
209 if constexpr(
sizeof(
T) * 4 <=
P) {
244 template<
typename T,
unsigned int P>
278 template<
typename T,
unsigned int P>
305 template<
typename T,
unsigned int P>
307 return a.raw == b.raw;
316 template<
typename T,
unsigned int P>
318 return a.raw != b.raw;
327 template<
typename T,
unsigned int P>
329 return a.raw < b.raw;
348 template<
typename T,
unsigned int P>
350 return a.raw <= b.raw;
369 template<
typename T,
unsigned int P>
371 return a.raw > b.raw;
390 template<
typename T,
unsigned int P>
392 return a.raw >= b.raw;
409 if constexpr(std::is_signed<T>::value) {
410 return x.
raw <
T(0) ? -x : x;
418 if constexpr(std::is_signed<T>::value) {
434 if constexpr(std::is_signed<T>::value) {
460 T b =
T(1) << ((
sizeof(
T) * 8) - 2);
509 static_assert(std::is_signed_v<T>);
523 t0 = -Type(0.013480470);
524 t0 =
t0 *
t4 + Type(0.057477314);
525 t0 =
t0 *
t4 - Type(0.121239071);
526 t0 =
t0 *
t4 + Type(0.195635925);
527 t0 =
t0 *
t4 - Type(0.332994597);
528 t0 =
t0 *
t4 + Type(0.999995630);
532 t3 = (x < 0) ? Type(3.141592654) -
t3 :
t3;
545 template <
typename B,
unsigned int F>
553 template <
typename B,
unsigned int F>
563 x = x /
Fixed(Math::PI / 2.0);
566 if (x <
Fixed(0.0)) {
571 if (x >
Fixed(2.0)) {
577 if (x >
Fixed(1.0)) {
592 if (x >
Fixed(0.0)) {
593 return sin(x - (
Fixed(Math::PI * 2.0) -
Fixed(Math::PI / 2.0)));
595 return sin(
Fixed(Math::PI / 2.0) + x);
604 return std::numeric_limits<FixedPoint<T, P>>::infinity();
627 typedef CeresEngine::Math::FixedPoint<typename std::make_signed<T>::type,
P> type;
631 typedef CeresEngine::Math::FixedPoint<typename std::make_unsigned<T>::type,
P> type;
635 using Type = CeresEngine::Math::FixedPoint<T, P>;
636 constexpr size_t operator()(
const Type& obj)
const {
return CeresEngine::hash(obj.raw); }
641 using Tp = CeresEngine::Math::FixedPoint<T, P>;
644 static inline constexpr bool is_specialized =
true;
647 static inline constexpr bool is_integer =
false;
650 static inline constexpr bool is_signed = numeric_limits<T>::is_signed;
653 static inline constexpr bool is_exact =
true;
657 static inline constexpr bool has_infinity =
false;
661 static inline constexpr bool has_quiet_NaN =
false;
665 static inline constexpr bool has_signaling_NaN =
false;
668 static inline constexpr float_denorm_style has_denorm = denorm_absent;
672 static inline constexpr bool has_denorm_loss =
false;
675 static inline constexpr float_round_style round_style = round_toward_zero;
678 static inline constexpr bool is_iec559 =
true;
681 static inline constexpr bool is_bounded =
true;
684 static inline constexpr bool is_modulo =
false;
687 static inline constexpr int digits =
static_cast<int>(
P);
690 static inline constexpr int digits10 =
static_cast<int>(digits * 0.3010299956639812);
694 static inline constexpr int max_digits10 = 0;
697 static inline constexpr int radix = 2;
701 static inline constexpr int min_exponent = 0;
705 static inline constexpr int min_exponent10 = 0;
709 static inline constexpr int max_exponent = 0;
713 static inline constexpr int max_exponent10 = 0;
716 static inline constexpr bool traps = std::numeric_limits<T>::traps;
719 static inline constexpr bool tinyness_before =
false;
722 [[nodiscard]]
static constexpr Tp min() noexcept {
return Tp(
T(0)); }
725 [[nodiscard]]
static constexpr Tp lowest() noexcept {
return Tp(std::numeric_limits<T>::lowest()); }
728 [[nodiscard]]
static constexpr Tp max() noexcept {
return Tp(std::numeric_limits<T>::max()); }
732 [[nodiscard]]
static constexpr Tp
epsilon() noexcept {
return Tp(
T(1)); }
735 [[nodiscard]]
static constexpr Tp round_error() noexcept {
return Tp(0); }
738 [[nodiscard]]
static constexpr Tp infinity() noexcept {
return max(); }
741 [[nodiscard]]
static constexpr Tp quiet_NaN() noexcept {
return Tp(0); }
744 [[nodiscard]]
static constexpr Tp signaling_NaN() noexcept {
return Tp(0); }
748 [[nodiscard]]
static constexpr Tp denorm_min() noexcept {
return Tp(0); }
Definition Application.hpp:19
ButtonSet & operator-=(ButtonSet &set, const Button button)
Removes a button from a set.
Definition Input.hpp:257
constexpr bool operator<(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:162
bool operator!=(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:416
ButtonSet & operator+=(ButtonSet &set, const Button button)
Adds a button to a set.
Definition Input.hpp:207
constexpr bool operator>=(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:157
ButtonSet operator-(const ButtonSet &set, const Button button)
Removes a button from a set.
Definition Input.hpp:270
bool operator==(const ShortAllocator< T, N, A1 > &x, const ShortAllocator< U, M, A2 > &y) noexcept
Definition Allocator.hpp:411
std::int64_t Int64
Definition DataTypes.hpp:24
constexpr bool operator>(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:152
void copy(const A &a, B &b, T &&t=T())
Copies values from one container to another.
Definition Iterator.hpp:564
BasicString< T, CharTraits, RawAllocator > operator+(const BasicString< T, CharTraits, RawAllocator > &lhs, const T *rhs)
Definition String.hpp:119
@ Fixed
Renders the shadow map from a fixed point.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
constexpr bool operator<=(const Optional< A > &lhs, const Optional< B > &rhs)
Definition Optional.hpp:167
const T epsilon
Return the epsilon constant for floating point types.
Definition Math.hpp:860
constexpr FixedPoint< T, P > frac(const FixedPoint< T, P > &x)
Returns the fractional portion of a fixed-point value.
Definition FixedPoint.hpp:417
constexpr FixedPoint< T, P > & operator++(FixedPoint< T, P > &a) noexcept
Increment the fixed-point value by adding 1.0 to it.
Definition FixedPoint.hpp:234
constexpr FixedPoint< T, P > floor(const FixedPoint< T, P > &x)
Returns largest integer not greater than a fixed-point value.
Definition FixedPoint.hpp:442
constexpr FixedPoint< T, P > ceil(const FixedPoint< T, P > &x)
Returns smallest integer not less than a fixed-point value.
Definition FixedPoint.hpp:447
constexpr FixedPoint< T, P > cos(const FixedPoint< T, P > &x)
Returns cosine of a fixed-point value.
Definition FixedPoint.hpp:588
constexpr FixedPoint< T, P > atan(const FixedPoint< T, P > &x)
Returns arctangent of a fixed-point value.
Definition FixedPoint.hpp:540
constexpr FixedPoint< T, P > atan2(const FixedPoint< T, P > &y, const FixedPoint< T, P > &x)
Returns arctangent of a fixed-point value.
Definition FixedPoint.hpp:508
constexpr FixedPoint< B, F > fmod(FixedPoint< B, F > x, FixedPoint< B, F > y) noexcept
Definition FixedPoint.hpp:546
constexpr FixedPoint< T, P > abs(const FixedPoint< T, P > &x)
Returns the absolute value of a fixed-point value.
Definition FixedPoint.hpp:408
constexpr FixedPoint< T, P > round(const FixedPoint< T, P > &x)
Returns the rounded value a fixed-point value.
Definition FixedPoint.hpp:453
constexpr FixedPoint< T, P > sqrt(const FixedPoint< T, P > &x)
Returns square root of a fixed-point value.
Definition FixedPoint.hpp:458
FixedPoint< B, F > sin(FixedPoint< B, F > x) noexcept
Definition FixedPoint.hpp:554
constexpr FixedPoint< T, P > & operator--(FixedPoint< T, P > &a) noexcept
Decrement the fixed-point value by subtracting 1.0 from it.
Definition FixedPoint.hpp:268
constexpr FixedPoint< T, P > sign(const FixedPoint< T, P > &x)
Returns sign of scalar a fixed-point value.
Definition FixedPoint.hpp:429
constexpr TExtent2< T > & operator*=(TExtent2< T > &lhs, T rhs) noexcept
Returns the multiplication of left hand side extent 'lhs' and the right hand side extent 'rhs'.
Definition Extent.hpp:151
constexpr FixedPoint< T, P > asin(FixedPoint< T, P > x)
Returns arcsine of a fixed-point value.
Definition FixedPoint.hpp:491
constexpr FixedPoint< T, P > acos(FixedPoint< T, P > x)
Returns arccosine of a fixed-point value.
Definition FixedPoint.hpp:475
constexpr FixedPoint< T, P > tan(const FixedPoint< T, P > &x)
Returns tangent of a fixed-point value. Returns the tangent of a in radians.
Definition FixedPoint.hpp:600
Definition FixedPoint.hpp:23
constexpr FixedPoint(float f) noexcept
Creates a new fixed-point number from an existing float value.
Definition FixedPoint.hpp:54
TT convert() const
Definition FixedPoint.hpp:92
constexpr FixedPoint(long double d) noexcept
Creates a new fixed-point number from an existing double value.
Definition FixedPoint.hpp:74
constexpr FixedPoint()=default
Creates a new fixed-point number and initializes it with 0.0.
constexpr FixedPoint(const FixedPoint< TT, TP > &other)
Creates a new fixed-point number by converting representation from another fixed-point.
Definition FixedPoint.hpp:88
constexpr FixedPoint(const FixedPoint &)=default
Creates a new fixed-point number by copying another.
constexpr FixedPoint(const double d) noexcept
Creates a new fixed-point number from an existing double value.
Definition FixedPoint.hpp:64
T raw
The underlying integer value.
Definition FixedPoint.hpp:35
constexpr FixedPoint(T raw) noexcept
Creates a new fixed-point number and initializes it with the given raw integer value.
Definition FixedPoint.hpp:43
constexpr FixedPoint & operator=(const FixedPoint &)=default
Assigns the fixed-point number by copying another.