summaryrefslogtreecommitdiffhomepage
path: root/src/timer-ns.cpp
blob: a6d1a073787b2989ba0a8ca860d84eae57736eb6 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "timer.hpp"
#include "compat/assert.hpp"
#include "compat/debug.hpp"
#include <cr/Debug.h>
#include <mg/Functions.h>

namespace floormat {

namespace {

#if 0
constexpr auto MAX = (uint64_t)-1, HALF = MAX/2;

static_assert(MAX - (MAX-0) <= 0);
static_assert(MAX - (MAX-1) <= 1);
static_assert(MAX - (MAX-2) <= 2);

static_assert(HALF + HALF + 1 == MAX);;
static_assert(MAX - HALF <= HALF+1);

//static_assert(MAX - (MAX-1) <= 0); // must fail
//static_assert(MAX - (MAX-2) <= 1); // must fail
//static_assert(MAX - HALF <= HALF); // must fail

#endif

} // namespace

Ns operator+(const Ns& lhs, const Ns& rhs)
{
    constexpr auto max = (uint64_t)-1;
    auto a = lhs.stamp, b = rhs.stamp;
    fm_assert(max - a >= b);
    return Ns{a + b};
}

Ns operator-(const Ns& lhs, const Ns& rhs)
{
    auto a = lhs.stamp, b = rhs.stamp;
    fm_assert(a >= b);
    return Ns{a - b};
}

Ns operator*(const Ns& lhs, uint64_t b)
{
    auto a = lhs.stamp;
    auto x = a * b;
    //fm_assert(!(a != 0 && x / a != b));
    fm_assert(a == 0 || x / a == b);
    return Ns{x};
}

Ns operator*(uint64_t a, const Ns& rhs)
{
    auto b = rhs.stamp;
    return Ns{a} * b;
}

uint64_t operator/(const Ns& lhs, const Ns& rhs)
{
    auto a = lhs.stamp, b = rhs.stamp;
    fm_assert(b != 0);
    return a / b;
}

Ns operator/(const Ns& lhs, uint64_t b)
{
    auto a = lhs.stamp;
    fm_assert(b != 0);
    return Ns{a / b};
}

uint64_t operator%(const Ns& lhs, const Ns& rhs)
{
    auto a = lhs.stamp, b = rhs.stamp;
    fm_assert(b != 0);
    return a % b;
}

Ns operator%(const Ns& lhs, uint64_t b)
{
    auto a = lhs.stamp;
    fm_assert(b != 0);
    return Ns{a % b};
}

bool operator==(const Ns& lhs, const Ns& rhs)
{
    auto a = lhs.stamp, b = rhs.stamp;
    return a == b;
}

std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs)
{
    auto a = lhs.stamp, b = rhs.stamp;
    return a <=> b;
}

Ns::operator uint64_t() const { return stamp; }
Ns::operator float() const { return float(stamp); }
uint64_t Ns::operator*() const { return stamp; }

Debug& operator<<(Debug& dbg, const Ns& box)
{
    const auto value = (float)((double)box.stamp * 1e-6);
    const auto absval = Math::abs(value);
    int precision;
    if (absval < 2)
        precision = 4;
    else if (absval < 5)
        precision = 2;
    else if (absval < 100)
        precision = 1;
    else
        precision = 0;
    auto flags = dbg.flags();
    dbg << "";
    dbg.setFlags(flags | Debug::Flag::NoSpace);
    dbg << "{";
    dbg << fraction(value, precision);
    //dbg << " ms";
    dbg << "}";
    dbg.setFlags(flags);
    return dbg;
}

} // namespace floormat