Go to the documentation of this file.
14 #if __cplusplus >= 202002L
15 #define __fp_consteval consteval
17 #define __fp_consteval constexpr
47 template <
intmax_t IntSize,
intmax_t FracSize,
bool Signed = true>
50 template <
intmax_t IntSize,
intmax_t FracSize,
bool Signed>
class FixedPoint {
51 static __fp_consteval
auto bytesForBits(
intmax_t v) {
return ((v + 7) / 8); }
52 static constexpr
auto storage_size = bytesForBits(IntSize + FracSize) * 8;
58 template <
intmax_t OI,
intmax_t OF,
bool S>
60 FixedPoint<(OI > IntSize ? OI : IntSize), (OF > FracSize ? OF : FracSize),
62 : OI < IntSize ? Signed
70 unsigned _BitInt(storage_size)>;
84 template <
typename T, std::enable_if_t<std::is_
integral_v<T>,
bool> = true>
103 template <
typename T,
104 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
105 [[clang::always_inline]] __fp_consteval
explicit FixedPoint(T
f) {
111 template <
intmax_t OI,
intmax_t OF,
bool S>
113 if constexpr (FracSize > OF)
139 template <
intmax_t OI,
intmax_t OF,
bool S = true>
144 [[clang::always_inline]] constexpr
bool is_signed()
const {
return Signed; }
146 return IntSize + FracSize;
244 val = (temp.get() * other.get()) >> FracSize;
285 template <
intmax_t OI,
intmax_t OF,
bool S>
286 [[clang::always_inline]] constexpr
auto
293 template <
intmax_t OI,
intmax_t OF,
bool S>
294 [[clang::always_inline]] constexpr
FixedPoint &
300 template <
intmax_t OI,
intmax_t OF,
bool S>
301 [[clang::always_inline]] constexpr
auto
308 template <
intmax_t OI,
intmax_t OF,
bool S>
309 [[clang::always_inline]] constexpr
FixedPoint &
314 template <
intmax_t OI,
intmax_t OF,
bool S>
315 [[clang::always_inline]] constexpr
auto
322 template <
intmax_t OI,
intmax_t OF,
bool S>
323 [[clang::always_inline]] constexpr
FixedPoint &
328 template <
intmax_t OI,
intmax_t OF,
bool S>
329 [[clang::always_inline]] constexpr
auto
336 template <
intmax_t OI,
intmax_t OF,
bool S>
337 [[clang::always_inline]] constexpr
FixedPoint &
342 template <
intmax_t OI,
intmax_t OF,
bool S>
343 [[clang::always_inline]] constexpr
auto
350 template <
intmax_t OI,
intmax_t OF,
bool S>
351 [[clang::always_inline]] constexpr
FixedPoint &
356 template <
intmax_t OI,
intmax_t OF,
bool S>
357 [[clang::always_inline]] constexpr
auto
364 template <
intmax_t OI,
intmax_t OF,
bool S>
365 [[clang::always_inline]] constexpr
FixedPoint &
371 template <
intmax_t OI,
intmax_t OF,
bool S>
372 [[clang::always_inline]] constexpr
auto
378 template <
intmax_t OI,
intmax_t OF,
bool S>
379 [[clang::always_inline]] constexpr
FixedPoint &
386 [[clang::always_inline]] constexpr
bool
390 [[clang::always_inline]] constexpr
bool
394 [[clang::always_inline]] constexpr
bool
398 [[clang::always_inline]] constexpr
bool
402 [[clang::always_inline]] constexpr
bool
406 [[clang::always_inline]] constexpr
bool
410 template <
intmax_t OI,
intmax_t OF,
bool S>
411 [[clang::always_inline]] constexpr
auto
417 template <
intmax_t OI,
intmax_t OF,
bool S>
418 [[clang::always_inline]] constexpr
auto
424 template <
intmax_t OI,
intmax_t OF,
bool S>
425 [[clang::always_inline]] constexpr
auto
431 template <
intmax_t OI,
intmax_t OF,
bool S>
432 [[clang::always_inline]] constexpr
auto
438 template <
intmax_t OI,
intmax_t OF,
bool S>
439 [[clang::always_inline]] constexpr
auto
445 template <
intmax_t OI,
intmax_t OF,
bool S>
446 [[clang::always_inline]] constexpr
auto
488 [[clang::always_inline]] __fp_consteval
fs8_8
489 operator""_s8_8(
long double fixed) {
492 [[clang::always_inline]] __fp_consteval
fs12_4
493 operator""_s12_4(
long double fixed) {
496 [[clang::always_inline]] __fp_consteval
fs16_8
497 operator""_s16_8(
long double fixed) {
500 [[clang::always_inline]] __fp_consteval
fs8_16
501 operator""_s8_16(
long double fixed) {
504 [[clang::always_inline]] __fp_consteval
fs12_12
505 operator""_s12_12(
long double fixed) {
508 [[clang::always_inline]] __fp_consteval
fs16_16
509 operator""_s16_16(
long double fixed) {
512 [[clang::always_inline]] __fp_consteval
fs24_8
513 operator""_s24_8(
long double fixed) {
517 [[clang::always_inline]] __fp_consteval
fu8_8
518 operator""_u8_8(
long double fixed) {
521 [[clang::always_inline]] __fp_consteval
fu12_4
522 operator""_u12_4(
long double fixed) {
525 [[clang::always_inline]] __fp_consteval
fu16_8
526 operator""_u16_8(
long double fixed) {
529 [[clang::always_inline]] __fp_consteval
fu8_16
530 operator""_u8_16(
long double fixed) {
533 [[clang::always_inline]] __fp_consteval
fu12_12
534 operator""_u12_12(
long double fixed) {
537 [[clang::always_inline]] __fp_consteval
fu16_16
538 operator""_u16_16(
long double fixed) {
541 [[clang::always_inline]] __fp_consteval
fu24_8
542 operator""_u24_8(
long double fixed) {
546 [[clang::always_inline]] __fp_consteval
fs8_8
547 operator""_8_8(
long double fixed) {
550 [[clang::always_inline]] __fp_consteval
fs12_4
551 operator""_12_4(
long double fixed) {
554 [[clang::always_inline]] __fp_consteval
fs16_8
555 operator""_16_8(
long double fixed) {
558 [[clang::always_inline]] __fp_consteval
fs8_16
559 operator""_8_16(
long double fixed) {
562 [[clang::always_inline]] __fp_consteval
fs12_12
563 operator""_12_12(
long double fixed) {
566 [[clang::always_inline]] __fp_consteval
fs16_16
567 operator""_16_16(
long double fixed) {
570 [[clang::always_inline]] __fp_consteval
fs24_8
571 operator""_24_8(
long double fixed) {
576 #endif // _FIXED_POINT_H
constexpr FixedPoint & operator/=(FixedPoint o)
Definition: fixed_point.h:228
constexpr FixedPoint operator^(FixedPoint o) const
Definition: fixed_point.h:201
constexpr FixedPoint operator/(FixedPoint o) const
Definition: fixed_point.h:232
constexpr FixedPoint operator+(FixedPoint o) const
Definition: fixed_point.h:212
constexpr auto operator&(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:344
constexpr FixedPoint & operator&=(FixedPoint o)
Definition: fixed_point.h:179
constexpr FracType as_f() const
Returns just the fractional portion.
Definition: fixed_point.h:124
constexpr FixedPoint operator&(FixedPoint o) const
Definition: fixed_point.h:183
constexpr intmax_t frac_bitcount() const
Definition: fixed_point.h:151
constexpr FixedPoint(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:112
constexpr FixedPoint(T i)
Definition: fixed_point.h:85
constexpr auto operator^(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:330
constexpr FixedPoint & operator&=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:352
constexpr void set_f(FracType value)
Update just the fractional portion.
Definition: fixed_point.h:130
FixedPoint< 16, 16, true > fs16_16
Definition: fixed_point.h:469
FixedPoint< 16, 8, false > fu16_8
Definition: fixed_point.h:474
IntType i
Definition: fixed_point.h:78
constexpr FixedPoint operator*(FixedPoint o) const
Definition: fixed_point.h:247
constexpr auto operator>(FixedPoint< OI, OF, S > o) const noexcept
Definition: fixed_point.h:433
constexpr bool operator>=(const FixedPoint &o) const noexcept
Definition: fixed_point.h:403
FixedPoint< 8, 16, false > fu8_16
Definition: fixed_point.h:475
constexpr bool operator>(const FixedPoint &o) const noexcept
Definition: fixed_point.h:399
constexpr auto operator|(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:358
constexpr FixedPoint & operator<<=(StorageType v)
Definition: fixed_point.h:264
constexpr FixedPoint & operator-=(FixedPoint o)
Definition: fixed_point.h:218
constexpr void set_i(IntType value)
Update just the integral portion.
Definition: fixed_point.h:128
constexpr auto operator+(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:287
constexpr FixedPoint & operator+=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:295
constexpr auto operator>=(FixedPoint< OI, OF, S > o) const noexcept
Definition: fixed_point.h:440
FixedPoint< 12, 12, true > fs12_12
Definition: fixed_point.h:468
constexpr FixedPoint< OI, OF, S > as()
Definition: fixed_point.h:140
constexpr FixedPoint operator-() const
Definition: fixed_point.h:158
constexpr auto operator-(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:302
FracType f
Definition: fixed_point.h:77
constexpr bool operator<=(const FixedPoint &o) const noexcept
Definition: fixed_point.h:407
constexpr FixedPoint operator-(FixedPoint o) const
Definition: fixed_point.h:222
constexpr FixedPoint & operator>>=(StorageType v)
Definition: fixed_point.h:253
constexpr bool operator==(const FixedPoint &o) const noexcept
Definition: fixed_point.h:387
constexpr FixedPoint(IntType i, FracType f)
Constructor for setting both the integral and fractional part.
Definition: fixed_point.h:89
FixedPoint & operator++()
Definition: fixed_point.h:275
::intmax_t intmax_t
Definition: cstdint:19
constexpr FixedPoint & operator*=(FixedPoint o)
Definition: fixed_point.h:238
constexpr intmax_t bitcount() const
Definition: fixed_point.h:145
constexpr FixedPoint & operator-=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:310
FixedPoint< 16, 8, true > fs16_8
Definition: fixed_point.h:466
constexpr auto operator==(FixedPoint< OI, OF, S > o) const noexcept
Definition: fixed_point.h:412
FixedPoint< 12, 4, true > fs12_4
Definition: fixed_point.h:465
constexpr FixedPoint operator<<(StorageType v) const
Definition: fixed_point.h:269
FixedPoint< 24, 8, false > fu24_8
Definition: fixed_point.h:478
constexpr FixedPoint & operator^=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:338
constexpr void set(StorageType value)
Update the entire value.
Definition: fixed_point.h:132
constexpr FixedPoint & operator%=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:324
std::conditional_t< Signed, _BitInt(IntSize), unsigned _BitInt(IntSize)> IntType
Definition: fixed_point.h:67
constexpr FixedPoint & operator*=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:380
FixedPoint< 8, 8, true > fs8_8
Definition: fixed_point.h:464
constexpr bool is_signed() const
Definition: fixed_point.h:144
constexpr FixedPoint & operator+=(FixedPoint o)
Definition: fixed_point.h:208
FixedPoint< 24, 8, true > fs24_8
Definition: fixed_point.h:470
FixedPoint< 8, 16, true > fs8_16
Definition: fixed_point.h:467
Definition: fixed_point.h:462
FixedPoint< 12, 12, false > fu12_12
Definition: fixed_point.h:476
constexpr IntType as_i() const
Returns just the integral portion.
Definition: fixed_point.h:122
unsigned _BitInt(FracSize) FracType
Definition: fixed_point.h:68
constexpr FixedPoint & operator|=(FixedPoint< OI, OF, S > o)
Definition: fixed_point.h:366
typename conditional< B, T, F >::type conditional_t
Definition: type_traits:26
constexpr auto operator%(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:316
FixedPoint< 16, 16, false > fu16_16
Definition: fixed_point.h:477
FixedPoint< 12, 4, false > fu12_4
Definition: fixed_point.h:473
constexpr bool operator!=(const FixedPoint &o) const noexcept
Definition: fixed_point.h:395
FixedPoint< 8, 8, false > fu8_8
Definition: fixed_point.h:472
constexpr FixedPoint operator~() const
Definition: fixed_point.h:163
constexpr FixedPoint & operator=(FixedPoint o)
Definition: fixed_point.h:94
Definition: fixed_point.h:48
constexpr FixedPoint & operator^=(FixedPoint o)
Definition: fixed_point.h:197
constexpr FixedPoint operator|(FixedPoint o) const
Definition: fixed_point.h:192
constexpr FixedPoint(const FixedPoint &o)
Definition: fixed_point.h:92
constexpr FixedPoint & operator|=(FixedPoint o)
Definition: fixed_point.h:188
constexpr auto operator!=(FixedPoint< OI, OF, S > o) const noexcept
Definition: fixed_point.h:426
constexpr auto operator*(FixedPoint< OI, OF, S > o) const
Definition: fixed_point.h:373
FixedPoint operator++(int)
Definition: fixed_point.h:279
constexpr StorageType get() const
Returns the entire value.
Definition: fixed_point.h:126
StorageType val
Definition: fixed_point.h:75
constexpr FixedPoint operator>>(StorageType v) const
Definition: fixed_point.h:258
constexpr FixedPoint operator%(FixedPoint o) const
Definition: fixed_point.h:174
constexpr intmax_t int_bitcount() const
Definition: fixed_point.h:148
std::conditional_t< Signed, _BitInt(storage_size), unsigned _BitInt(storage_size)> StorageType
Definition: fixed_point.h:70
constexpr bool operator<(const FixedPoint &o) const noexcept
Definition: fixed_point.h:391
constexpr FixedPoint & operator%=(FixedPoint o)
Definition: fixed_point.h:170