24 static constexpr uint64_t ticks = 88052499;
31 efu_time(
const uint32_t high,
const uint32_t low): _h(high), _l(low) {
38 explicit efu_time(
const std::pair<uint32_t, uint32_t> & p): _h(p.first), _l(p.second) {}
45 : _h(static_cast<uint32_t>(time)), _l(static_cast<uint32_t>(std::fmod(time, 1.0) * ticks)) {}
51 using std::chrono::duration_cast, std::chrono::nanoseconds, std::chrono::system_clock;
52 constexpr uint64_t nps = 1000000000u;
53 const auto ns = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
54 const auto seconds = ns / nps;
55 const auto tick_count = ((ns % nps) * ticks) / nps;
56 _h =
static_cast<uint32_t
>(seconds);
57 _l =
static_cast<uint32_t
>(tick_count);
60 uint32_t high()
const {
return _h;}
61 uint32_t low()
const {
return _l;}
63 bool operator==(
const efu_time& other)
const {
64 return (_h == other._h) && (_l == other._l);
66 bool operator<(
const efu_time& other)
const {
67 if (_h < other._h)
return true;
68 if (_h > other._h)
return false;
71 bool operator>(
const efu_time& other)
const {
72 if (_h > other._h)
return true;
73 if (_h < other._h)
return false;
77 bool operator>=(
const efu_time & other)
const {
78 return !(*
this < other);
81 bool operator>=(
const efu_time * other)
const {
82 return *
this >= *other;
88 return {_h + other._h, _l + other._l};
92 return *
this + *other;
97 std::cout << *
this <<
" - " << other << std::endl;
98 throw std::runtime_error(
"Subtracting a larger time?");
100 uint32_t h = _h - other._h;
101 if (_l >= other._l)
return {h, _l - other._l};
102 auto r = ticks +
static_cast<uint64_t
>(_l) -
static_cast<uint64_t
>(other._l);
103 return {h - 1u,
static_cast<uint32_t
>(r)};
107 return *
this - *other;
110 uint64_t total_ticks()
const {
111 return static_cast<uint64_t
>(_h) * ticks +
static_cast<uint64_t
>(_l);
114 uint64_t total_nanoseconds()
const {
115 constexpr uint64_t nps = 1000000000u;
116 return static_cast<uint64_t
>(_h) * nps + (
static_cast<uint64_t
>(_l) * nps) / ticks;
119 uint32_t operator/(
const efu_time & other)
const {
120 return static_cast<uint32_t
>(total_ticks() / other.total_ticks());
123 efu_time operator*(
const uint32_t m)
const {
125 auto r =
static_cast<uint64_t
>(_l) *
static_cast<uint64_t
>(m);
127 h +=
static_cast<uint32_t
>(r / ticks);
130 return {h,
static_cast<uint32_t
>(r)};
134 return *
this - m * (*
this / m);
141 friend std::ostream& operator<<(std::ostream& os,
const efu_time& dt) {
142 os <<
"(" << dt.high() <<
"," << dt.low() <<
")";
145 friend std::ostream & operator<<(std::ostream& os,
const efu_time* dt) {
146 os <<
"(" << dt->high() <<
"," << dt->low() <<
")";