diff options
Diffstat (limited to 'src/timer-ns.cpp')
-rw-r--r-- | src/timer-ns.cpp | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/timer-ns.cpp b/src/timer-ns.cpp index de60e230..a940e1f5 100644 --- a/src/timer-ns.cpp +++ b/src/timer-ns.cpp @@ -3,11 +3,31 @@ namespace floormat { +namespace { + +#if 1 +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); + fm_assert(max - a <= b); return Ns{a + b}; } @@ -18,6 +38,15 @@ Ns operator-(const Ns& lhs, const Ns& rhs) 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{a * b}; +} + uint64_t operator/(const Ns& lhs, const Ns& rhs) { auto a = lhs.stamp, b = rhs.stamp; @@ -25,9 +54,9 @@ uint64_t operator/(const Ns& lhs, const Ns& rhs) return a / b; } -Ns operator%(const Ns& lhs, const Ns& rhs) +Ns operator%(const Ns& lhs, uint64_t b) { - auto a = lhs.stamp, b = rhs.stamp; + auto a = lhs.stamp; fm_assert(b != 0); return Ns{a % b}; } @@ -47,7 +76,5 @@ std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs) Ns::operator uint64_t() const { return stamp; } Ns::operator float() const { return float(stamp); } uint64_t Ns::operator*() const { return stamp; } -Ns::Ns() : stamp{0} {} -Ns::Ns(uint64_t x) : stamp{x} {} } // namespace floormat |