#include "clock.hpp" #include namespace shared_clock { namespace { Tick distance(Tick a, Tick b) { return a >= b ? a - b : b - a; } bool sample_domain(const Sample& sample) { return sample.id != 0 && sample.local_send <= LIMIT && sample.local_receive <= LIMIT && sample.hint <= LIMIT && sample.remote_receive.fraction <= 1023 && sample.remote_send.fraction <= 1023; } } Status unfold(Stamp stamp, Tick hint, Tick& out) { if (stamp.fraction > 1023 || hint > LIMIT) { return Status::Range; } const Tick residue = static_cast(stamp.seconds) * 1024 + stamp.fraction; Tick era = 0; if (hint > residue) { const Tick delta = hint - residue; era = delta / PERIOD; if (delta % PERIOD > PERIOD / 2) { ++era; } } const Tick last_era = (LIMIT - residue) / PERIOD; if (era > last_era) { era = last_era; } out = residue + era * PERIOD; return Status::Ok; } Status ticks_to_microseconds(Tick ticks, std::uint64_t& out) { if (ticks > LIMIT) { return Status::Range; } const std::uint64_t seconds = ticks / 1024; const std::uint64_t fraction = ticks % 1024; out = seconds * 1000000 + (fraction * 1000000 + 512) / 1024; return Status::Ok; } Status microseconds_to_ticks(std::uint64_t micros, Tick& out) { const Tick seconds = micros / 1000000; const Tick fraction = micros % 1000000; if (seconds > LIMIT / 1024) { return Status::Range; } const Tick rounded = seconds * 1024 + (fraction * 1024 + 500000) / 1000000; if (rounded > LIMIT) { return Status::Range; } out = rounded; return Status::Ok; } Status evaluate(const Sample& sample, Policy policy, Estimate& out) { if (!sample_domain(sample) || policy.max_delay > MAX_SPAN) { return Status::Range; } if (sample.local_receive < sample.local_send) { return Status::Inconsistent; } Tick remote_receive = 0; Tick remote_send = 0; Status status = unfold(sample.remote_receive, sample.hint, remote_receive); if (status != Status::Ok) { return status; } status = unfold(sample.remote_send, sample.hint, remote_send); if (status != Status::Ok) { return status; } if (remote_send < remote_receive) { return Status::Inconsistent; } const Tick local_span = sample.local_receive - sample.local_send; const Tick remote_span = remote_send - remote_receive; if (local_span > MAX_SPAN || remote_span > MAX_REMOTE_SPAN || local_span < remote_span) { return Status::Inconsistent; } const Tick delay = local_span - remote_span; if (delay > policy.max_delay) { return Status::Rejected; } const auto first = static_cast(remote_receive) - static_cast(sample.local_send); const auto second = static_cast(remote_send) - static_cast(sample.local_receive); const auto offset = (first + second) / 2; const auto remote = static_cast(sample.local_receive) + offset; if (remote < 0 || static_cast(remote) > LIMIT) { return Status::Range; } Estimate result; result.local = sample.local_receive; result.remote = static_cast(remote); result.offset = offset; result.delay = delay; result.id = sample.id; out = result; return Status::Ok; } Status validate_snapshot(const Snapshot& snapshot) { if (!snapshot.sequence || !snapshot.id || snapshot.local > LIMIT || snapshot.remote > LIMIT || snapshot.delay > MAX_SPAN || snapshot.offset < -static_cast(LIMIT) || snapshot.offset > static_cast(LIMIT)) { return Status::Range; } const auto computed = static_cast(snapshot.local) + snapshot.offset; if (computed < 0 || static_cast(computed) != snapshot.remote) { return Status::Range; } return Status::Ok; } }