summaryrefslogtreecommitdiff
path: root/include/fixed_point.h
blob: 2a9ec8d0f702f78a5a172fed88ab1d72092d628b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef FIXED_POINT_H
#define FIXED_POINT_H

#include <cstdint>
#include <type_traits>
#include <compare>

struct fp_t {
    static constexpr std::int32_t Q = 16;
    static constexpr std::int32_t K = 1 << (Q - 1);
    std::int32_t raw;
    
    static constexpr fp_t from_raw(std::int32_t r) { return fp_t{r}; }
    static constexpr fp_t from_int(std::int32_t i) { return fp_t{i << 16}; }

    fp_t operator+(const fp_t& other) const;
    fp_t operator-(const fp_t& other) const;
    fp_t operator*(const fp_t& other) const;
    fp_t operator/(const fp_t& other) const;
    auto operator<=>(const fp_t&) const = default;
};

static_assert(std::is_trivially_copyable_v<fp_t>, "fp_t must remain trivially copyable for rollback determinism");
static_assert(sizeof(fp_t) == 4, "fp_t must stay 4 bytes — check for unexpected padding");

#endif // !FIXED_POINT_H