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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#pragma once
#include "compat/assert.hpp"
#include <compare>
namespace floormat {
// todo! move to .inl
struct Ns
{
explicit constexpr Ns(): stamp{0} {}
template<typename T>
requires (std::is_integral_v<T> && std::is_unsigned_v<T>)
explicit constexpr Ns(T x) : stamp{x} {}
template<typename T>
requires (std::is_integral_v<T> && !std::is_unsigned_v<T>)
explicit constexpr Ns(T x) : stamp{uint64_t(x)} { fm_assert(x >= T{0}); }
#if 0
template<typename T>
requires std::is_same_v<T, float>
explicit constexpr Ns(T x) : stamp{}
{
constexpr float max{uint64_t{1} << 24};
fm_assert(x >= 0);
fm_assert(x <= max);
stamp = uint64_t(x);
}
template<typename T>
requires std::is_same_v<T, double>
explicit constexpr Ns(T x) : stamp{}
{
constexpr double max{uint64_t{1} << 53};
fm_assert(x >= 0);
fm_assert(x <= max);
stamp = uint64_t(x);
}
#endif
explicit constexpr operator uint64_t() const { return stamp; }
explicit constexpr operator double() const = delete;
explicit constexpr operator float() const = delete;
template<typename T>
requires (std::is_same_v<T, double>)
friend Ns operator*(const Ns& lhs, T b)
{
constexpr double max{uint64_t{1} << 53};
auto a = lhs.stamp;
fm_assert(b >= 0);
fm_assert(b <= max);
auto x = double(a) * b;
fm_assert(x <= max);
fm_assert(x >= 0);
return Ns{(uint64_t)x};
}
friend constexpr 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};
}
friend constexpr Ns operator-(const Ns& lhs, const Ns& rhs)
{
auto a = lhs.stamp, b = rhs.stamp;
fm_assert(a >= b);
return Ns{a - b};
}
friend Ns operator*(const Ns&, const Ns&) = delete;
template<typename T>
requires (std::is_integral_v<T> && std::is_unsigned_v<T>)
friend constexpr Ns operator*(const Ns& lhs, T rhs)
{
auto a = lhs.stamp, b = uint64_t{rhs};
auto x = a * b;
fm_assert(b == 0 || x / b == a);
return Ns{x};
}
template<typename T>
requires (std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) < sizeof(uint64_t))
friend constexpr Ns operator*(const Ns& lhs, T rhs)
{
fm_assert(rhs >= T{0});
auto b = uint64_t(rhs);
auto x = lhs.stamp * b;
fm_assert(b == 0 || x / b == lhs.stamp);
return Ns{x};
}
template<typename T>
friend constexpr Ns operator*(T lhs, const Ns& rhs) { return rhs * lhs; }
template<typename T>
requires std::is_same_v<float, T>
friend constexpr Ns operator*(const Ns& lhs, T rhs)
{
constexpr float max{uint64_t{1} << 24};
auto a = lhs.stamp;
auto x = float(a) * float{rhs};
fm_assert(x >= 0);
fm_assert(x <= max);
return Ns{uint64_t(x)};
}
#if 0
template<typename T> requires (!std::is_same_v<Ns, T>)
friend constexpr Ns operator*(const T& lhs, const Ns& rhs) { return rhs * lhs; }
#endif
friend constexpr uint64_t operator/(const Ns& lhs, const Ns& rhs)
{
auto a = lhs.stamp, b = rhs.stamp;
fm_assert(b != 0);
return a / b;
}
friend constexpr Ns operator/(const Ns& lhs, uint64_t b)
{
auto a = lhs.stamp;
fm_assert(b != 0);
return Ns{a / b};
}
#if 0
template<typename T>
requires std::is_floating_point_v<T>
friend constexpr double operator/(const Ns& lhs, double b)
{
fm_assert(b != 0.);
auto x = double{lhs.stamp / b};
constexpr auto max = double{uint64_t{1} << 53};
fm_assert(x <= max);
return x;
}
#endif
friend constexpr uint64_t operator%(const Ns& lhs, const Ns& rhs)
{
auto a = lhs.stamp, b = rhs.stamp;
fm_assert(b != 0);
return a % b;
}
friend constexpr Ns operator%(const Ns& lhs, uint64_t b)
{
auto a = lhs.stamp;
fm_assert(b != 0);
return Ns{a % b};
}
friend constexpr Ns& operator+=(Ns& lhs, const Ns& rhs)
{
constexpr auto max = (uint64_t)-1;
auto b = rhs.stamp;
fm_assert(max - lhs.stamp >= b);
lhs.stamp += b;
return lhs;
}
friend constexpr bool operator==(const Ns& lhs, const Ns& rhs) = default;
friend constexpr std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs)
{
auto a = lhs.stamp, b = rhs.stamp;
return a <=> b;
}
friend Debug& operator<<(Debug& dbg, const Ns& box);
uint64_t stamp;
};
constexpr inline Ns Minute{60000000000}, Second{1000000000}, Millisecond{1000000}, Microsecond{1000};
constexpr inline const Ns& Minutes{Minute}, Seconds{Second}, Milliseconds{Millisecond}, Microseconds{Microsecond};
struct Time final
{
static Time now() noexcept;
bool operator==(const Time&) const noexcept;
std::strong_ordering operator<=>(const Time&) const noexcept;
friend Ns operator-(const Time& lhs, const Time& rhs) noexcept;
[[nodiscard]] Ns update(const Time& ts = now()) & noexcept;
static float to_seconds(const Ns& ts) noexcept;
static float to_milliseconds(const Ns& ts) noexcept;
uint64_t stamp = init();
private:
static uint64_t init() noexcept;
};
constexpr inline size_t fm_DATETIME_BUF_SIZE = 32;
const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]);
} // namespace floormat
|