amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

265 lines
6.1 KiB
C++

#include "interlock.hpp"
#include <limits>
namespace maskwrite {
Interlock::Interlock()
: words_{}, sampled_{}, pending_{}, touched_{}, queue_{}, functions_{},
owner_(-1), phase_(IDLE), count_(0), generation_(0)
{
}
bool Interlock::actor_ok(Number actor)
{
return actor >= 0 && actor < 8;
}
bool Interlock::reg_ok(Number reg)
{
return reg >= 0 && reg < static_cast<Number>(RegisterCount);
}
bool Interlock::kind_ok(Number kind)
{
return kind >= SET && kind <= REPLACE;
}
bool Interlock::word_ok(Number value)
{
return value >= 0 && value <= 4294967295LL;
}
Status Interlock::require_owner(Number actor) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (owner_ != actor) {
return OWNER;
}
return OK;
}
void Interlock::clear_transaction()
{
owner_ = -1;
phase_ = IDLE;
count_ = 0;
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
sampled_[reg] = 0;
pending_[reg] = 0;
touched_[reg] = false;
}
for (auto& reg : functions_) {
for (auto& bit : reg) {
bit[0] = false;
bit[1] = true;
}
}
for (Operation& operation : queue_) {
operation.reg = 0;
operation.kind = 0;
operation.mask = 0;
operation.value = 0;
}
}
Status Interlock::reset()
{
for (std::uint32_t& word : words_) {
word = 0;
}
generation_ = 0;
clear_transaction();
return OK;
}
Status Interlock::load(Number reg, Number value)
{
if (!reg_ok(reg)) {
return RANGE;
}
if (!word_ok(value)) {
return RANGE;
}
if (owner_ != -1) {
return BUSY;
}
words_[static_cast<std::size_t>(reg)] = static_cast<std::uint32_t>(value);
return OK;
}
Status Interlock::begin(Number actor)
{
if (!actor_ok(actor)) {
return RANGE;
}
if (owner_ != -1) {
return BUSY;
}
clear_transaction();
owner_ = actor;
phase_ = BUILD;
return OK;
}
Status Interlock::stage(Number actor, Number reg, Number kind,
Number mask, Number value)
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
if (!kind_ok(kind)) {
return RANGE;
}
if (!word_ok(mask)) {
return RANGE;
}
if (!word_ok(value)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != BUILD) {
return PHASE;
}
if (count_ == QueueLimit) {
return FULL;
}
Operation next;
next.reg = reg;
next.kind = kind;
next.mask = static_cast<std::uint32_t>(mask);
next.value = static_cast<std::uint32_t>(value);
queue_[count_] = next;
compose(next);
++count_;
return OK;
}
void Interlock::compose(const Operation& operation)
{
auto& reg = functions_[static_cast<std::size_t>(operation.reg)];
for (unsigned bit = 0; bit < 32; ++bit) {
std::uint32_t flag = std::uint32_t(1) << bit;
if ((operation.mask & flag) == 0) {
continue;
}
bool next = operation.kind == SET ||
(operation.kind == REPLACE && (operation.value & flag) != 0);
reg[bit][0] = next;
reg[bit][1] = next;
}
}
Status Interlock::capture(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != BUILD) {
return PHASE;
}
if (count_ == 0) {
return EMPTY;
}
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
sampled_[reg] = words_[reg];
pending_[reg] = 0;
touched_[reg] = true;
for (unsigned bit = 0; bit < 32; ++bit) {
unsigned input = (words_[reg] >> bit) & 1U;
if (functions_[reg][bit][input]) {
pending_[reg] |= std::uint32_t(1) << bit;
}
}
}
phase_ = CAPTURED;
return OK;
}
Status Interlock::commit(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
if (touched_[reg]) {
words_[reg] = pending_[reg];
}
}
++generation_;
clear_transaction();
return OK;
}
Status Interlock::abort(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
clear_transaction();
return OK;
}
Status Interlock::word(Number reg, std::uint32_t& result) const
{
if (!reg_ok(reg)) {
return RANGE;
}
result = words_[static_cast<std::size_t>(reg)];
return OK;
}
Status Interlock::prepared(Number actor, Number reg, std::uint32_t& result) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
result = pending_[static_cast<std::size_t>(reg)];
return OK;
}
Status Interlock::changed(Number actor, Number reg, std::uint32_t& result) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
std::size_t index = static_cast<std::size_t>(reg);
result = sampled_[index] ^ pending_[index];
return OK;
}
Status Interlock::operation(Number index, Operation& result) const
{
if (index < 0 || index >= static_cast<Number>(QueueLimit)) {
return RANGE;
}
if (static_cast<std::size_t>(index) >= count_) {
return EMPTY;
}
result = queue_[static_cast<std::size_t>(index)];
return OK;
}
State Interlock::state() const
{
State result;
result.owner = owner_;
result.phase = phase_;
result.count = static_cast<Number>(count_);
result.generation = generation_;
return result;
}
}