summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-03-03 11:07:09 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-03-04 06:58:12 +0100
commitd0065f07cdcb62f49586e2067fd164897fddba6f (patch)
tree8d8c9e3408d6959ac4725546a324c370c95f676b /src
parent947c86aae15a59401b11f2d134dd540e38e3ceb6 (diff)
src/timer: a?
Diffstat (limited to 'src')
-rw-r--r--src/timer.cpp30
-rw-r--r--src/timer.hpp53
2 files changed, 68 insertions, 15 deletions
diff --git a/src/timer.cpp b/src/timer.cpp
index bd4baad8..b068c55d 100644
--- a/src/timer.cpp
+++ b/src/timer.cpp
@@ -46,20 +46,30 @@ uint64_t Time::init() noexcept { return get_time(); }
bool Time::operator==(const Time&) const noexcept = default;
std::strong_ordering Time::operator<=>(const Time&) const noexcept = default;
-double Time::to_seconds(const Ns& ts) noexcept
+float Time::to_seconds(const Ns& ts) noexcept
{
- auto x1 = double{ts};
- auto x2 = x1 * 1e-9;
- fm_assert(x2 < double{1 << 24});
- return x2;
+ if (ts.stamp == 0) [[unlikely]]
+ return 0;
+ else
+ {
+ auto x = double(ts.stamp) * 1e-9;
+ fm_assert(x < double{1 << 24});
+ //fm_assert(x >= 1e-10f);
+ return (float)x;
+ }
}
-double Time::to_milliseconds(const Ns& ts) noexcept
+float Time::to_milliseconds(const Ns& ts) noexcept
{
- auto x1 = double{ts};
- auto x2 = x1 * 1e-6;
- fm_assert(x2 < double{1 << 24});
- return x2;
+ if (ts.stamp == 0) [[unlikely]]
+ return 0;
+ else
+ {
+ auto x = double(ts.stamp) * 1e-6;
+ fm_assert(x < double{1 << 24});
+ fm_assert(x >= 1e-10);
+ return (float)x;
+ }
}
const char* format_datetime_to_string(char (&buf)[fm_DATETIME_BUF_SIZE])
diff --git a/src/timer.hpp b/src/timer.hpp
index 1350899b..a1e8c1ac 100644
--- a/src/timer.hpp
+++ b/src/timer.hpp
@@ -8,10 +8,51 @@ struct Ns
{
explicit constexpr Ns(): stamp{0} {}
explicit constexpr Ns(uint64_t x) : stamp{x} {}
+
+ 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 * 1e-9);
+ }
+
+ template<typename T>
+ requires std::is_same_v<T, double>
+ explicit constexpr Ns(T x) : stamp{}
+ {
+ constexpr double max{uint64_t{1} << 54};
+ fm_assert(x >= 0);
+ fm_assert(x <= max);
+ stamp = uint64_t(x * 1e-9);
+ }
+
explicit constexpr operator uint64_t() const { return stamp; }
- explicit constexpr operator double() const { return stamp; }
+ //explicit constexpr operator double() const { return stamp; }
explicit constexpr operator float() const = delete;
+ template<typename T>
+ requires (std::is_same_v<T, float>)
+ static Ns from_nonzero(T seconds)
+ {
+ constexpr float max{uint64_t{1} << 24};
+ fm_assert(seconds >= 0);
+ fm_assert (seconds <= max);
+ return Ns((uint64_t)seconds);
+ }
+
+ template<typename T>
+ requires (std::is_same_v<T, double>)
+ static Ns from_nonzero(T seconds)
+ {
+ constexpr double max{uint64_t{1} << 54};
+ fm_assert(seconds >= 0);
+ fm_assert(seconds <= max);
+ return Ns((uint64_t)seconds);
+ }
+
static constexpr Ns from_millis(uint64_t a)
{
constexpr auto b = uint64_t(1e6);
@@ -64,15 +105,17 @@ struct Ns
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 <= 1 << 24 && x >= 0);
+ fm_assert(x >= 0);
+ fm_assert(x <= max);
return Ns{uint64_t(x)};
}
template<typename T>
requires std::is_same_v<float, T>
- friend constexpr Ns operator*(T lhs, const Ns& rhs) { return rhs * lhs; }
+ friend constexpr inline Ns operator*(T lhs, const Ns& rhs) { return rhs * lhs; }
friend constexpr uint64_t operator/(const Ns& lhs, const Ns& rhs)
{
@@ -134,8 +177,8 @@ struct Time final
friend Ns operator-(const Time& lhs, const Time& rhs) noexcept;
[[nodiscard]] Ns update(const Time& ts = now()) & noexcept;
- static double to_seconds(const Ns& ts) noexcept;
- static double to_milliseconds(const Ns& ts) noexcept;
+ static float to_seconds(const Ns& ts) noexcept;
+ static float to_milliseconds(const Ns& ts) noexcept;
uint64_t stamp = init();