diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-05 21:04:20 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-03-05 21:56:56 +0100 |
commit | 71d8fa55ab671ae152c420738b2d9d49f8f1106d (patch) | |
tree | a4ad0eb37840245622b99ee49c1d555ccb238b02 /src/timer.hpp | |
parent | a56a8cc899dc90701f1690051da4e4318e95566c (diff) |
clean up timer & nanosecond includes
Diffstat (limited to 'src/timer.hpp')
-rw-r--r-- | src/timer.hpp | 183 |
1 files changed, 2 insertions, 181 deletions
diff --git a/src/timer.hpp b/src/timer.hpp index 36874e9e..67556c86 100644 --- a/src/timer.hpp +++ b/src/timer.hpp @@ -1,188 +1,9 @@ #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 Ns; struct Time final { @@ -201,7 +22,7 @@ private: static uint64_t init() noexcept; }; -constexpr inline size_t fm_DATETIME_BUF_SIZE = 32; +#define fm_DATETIME_BUF_SIZE 32 const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]); } // namespace floormat |