M11.1: time-based rate conversion (ADR-0099, segment 1)
Foundation / checks (deploy) (push) Successful in 3m27s
Foundation / checks (ecs) (push) Successful in 1m51s
Foundation / checks (client) (push) Successful in 12m31s
Foundation / checks (network) (push) Successful in 13m30s
Foundation / checks (tsan) (push) Canceled after 8m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 27m37s
Foundation / checks (postgres) (push) Canceled after 15m9s
Foundation / checks (deploy) (push) Successful in 3m27s
Foundation / checks (ecs) (push) Successful in 1m51s
Foundation / checks (client) (push) Successful in 12m31s
Foundation / checks (network) (push) Successful in 13m30s
Foundation / checks (tsan) (push) Canceled after 8m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 27m37s
Foundation / checks (postgres) (push) Canceled after 15m9s
Rates are stored per second in their integer base units and converted at the tick boundary with no carried state: costs round up, capacities down (maintainer decision), error below one unit per tick. per_second_from_per_tick is the migration's exact inverse (x20 at 50 ms) and refuses inexact values. New rate-contracts test. No biology uses it yet; genome v2, development v2 and process fields follow. Refresh the dev session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include "evo/accounting/rates.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace evo::accounting {
|
||||
namespace {
|
||||
constexpr auto u64_max = std::numeric_limits<std::uint64_t>::max();
|
||||
bool valid_tick(std::uint64_t tick_microseconds) noexcept {
|
||||
return tick_microseconds != 0 && tick_microseconds <= max_tick_microseconds;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::uint64_t, RateError>
|
||||
per_tick(std::uint64_t per_second, std::uint64_t tick_microseconds, Rounding rounding) noexcept {
|
||||
if (!valid_tick(tick_microseconds))
|
||||
return std::unexpected(RateError::invalid_tick);
|
||||
// Whole seconds and the fraction apart, so no intermediate exceeds 64 bits: the
|
||||
// fraction's product is below 10^6 * 10^6.
|
||||
const auto whole = per_second / microseconds_per_second;
|
||||
const auto part = per_second % microseconds_per_second;
|
||||
if (whole > u64_max / tick_microseconds)
|
||||
return std::unexpected(RateError::overflow);
|
||||
const auto scaled = part * tick_microseconds;
|
||||
auto amount = whole * tick_microseconds;
|
||||
const auto fraction = scaled / microseconds_per_second;
|
||||
if (amount > u64_max - fraction)
|
||||
return std::unexpected(RateError::overflow);
|
||||
amount += fraction;
|
||||
if (rounding == Rounding::up && scaled % microseconds_per_second != 0) {
|
||||
if (amount == u64_max)
|
||||
return std::unexpected(RateError::overflow);
|
||||
++amount;
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
bool exact_per_tick(std::uint64_t per_second, std::uint64_t tick_microseconds) noexcept {
|
||||
return valid_tick(tick_microseconds) &&
|
||||
(per_second % microseconds_per_second) * tick_microseconds % microseconds_per_second ==
|
||||
0;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, RateError>
|
||||
per_second_from_per_tick(std::uint64_t per_tick_amount, std::uint64_t tick_microseconds) noexcept {
|
||||
if (!valid_tick(tick_microseconds))
|
||||
return std::unexpected(RateError::invalid_tick);
|
||||
if (per_tick_amount > u64_max / microseconds_per_second)
|
||||
return std::unexpected(RateError::overflow);
|
||||
const auto scaled = per_tick_amount * microseconds_per_second;
|
||||
if (scaled % tick_microseconds != 0)
|
||||
return std::unexpected(RateError::inexact);
|
||||
return scaled / tick_microseconds;
|
||||
}
|
||||
} // namespace evo::accounting
|
||||
Reference in New Issue
Block a user