One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
6.9 KiB
C
250 lines
6.9 KiB
C
#include "width.h"
|
|
#include <string.h>
|
|
static int lane_valid(int lane)
|
|
{
|
|
return lane >= 0 && lane < PW_LANES;
|
|
}
|
|
static int zero_tail(const PWRequest *request)
|
|
{
|
|
return request->b == 0 && request->c == 0;
|
|
}
|
|
static int zero_arguments(const PWRequest *request)
|
|
{
|
|
return request->a == 0 && zero_tail(request);
|
|
}
|
|
uint32_t pw_effective(const PWContext *context, int lane)
|
|
{
|
|
if (!lane_valid(lane)) {
|
|
return 0;
|
|
}
|
|
const PWLane *state = &context->lanes[lane];
|
|
if (state->active) {
|
|
return state->pending;
|
|
}
|
|
return state->live;
|
|
}
|
|
static void assign_word(PWLane *lane, uint32_t word)
|
|
{
|
|
if (lane->active) {
|
|
if (lane->pending != word) {
|
|
lane->dirty = 1;
|
|
}
|
|
lane->pending = word;
|
|
} else {
|
|
if (lane->live != word) {
|
|
lane->generation++;
|
|
}
|
|
lane->live = word;
|
|
}
|
|
}
|
|
void pw_init(PWContext *context, int32_t seed)
|
|
{
|
|
memset(context, 0, sizeof(*context));
|
|
for (int i = 0; i < PW_LANES; ++i) {
|
|
uint32_t salt = PW_LANE_SALT * (uint32_t)(i + 1);
|
|
uint32_t word = (uint32_t)seed ^ salt;
|
|
context->initial[i] = word;
|
|
context->lanes[i].live = word;
|
|
context->lanes[i].pending = word;
|
|
}
|
|
pw_port_init(&context->port);
|
|
pw_audit_init(&context->audit);
|
|
}
|
|
static int apply_bit(PWContext *context, const PWRequest *request)
|
|
{
|
|
int32_t position = request->a;
|
|
if (position < 0 || position >= 32) {
|
|
return PW_INVALID;
|
|
}
|
|
if (request->b != 0 && request->b != 1) {
|
|
return PW_INVALID;
|
|
}
|
|
if (request->c != 0) {
|
|
return PW_INVALID;
|
|
}
|
|
uint32_t bit = pw_port_shift(&context->port, UINT32_C(1), position);
|
|
uint32_t value = pw_effective(context, request->lane);
|
|
if (request->b == 1) {
|
|
value |= bit;
|
|
} else {
|
|
value &= ~bit;
|
|
}
|
|
assign_word(&context->lanes[request->lane], value);
|
|
return PW_OK;
|
|
}
|
|
static int apply_range(PWContext *context, const PWRequest *request)
|
|
{
|
|
int32_t low = request->a;
|
|
int32_t high = request->b;
|
|
if (low < 0 || high < low || high >= 32) {
|
|
return PW_INVALID;
|
|
}
|
|
if (request->c < 0 || request->c > 2) {
|
|
return PW_INVALID;
|
|
}
|
|
uint32_t upper = pw_port_shift(&context->port, UINT32_C(1), (int64_t)high + 1);
|
|
uint32_t lower = pw_port_shift(&context->port, UINT32_C(1), low);
|
|
uint32_t mask = (upper - UINT32_C(1)) ^ (lower - UINT32_C(1));
|
|
uint32_t value = pw_effective(context, request->lane);
|
|
if (request->c == 0) {
|
|
value &= ~mask;
|
|
} else if (request->c == 1) {
|
|
value |= mask;
|
|
} else {
|
|
value ^= mask;
|
|
}
|
|
assign_word(&context->lanes[request->lane], value);
|
|
return PW_OK;
|
|
}
|
|
static int apply_write(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!zero_tail(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
assign_word(&context->lanes[request->lane], (uint32_t)request->a);
|
|
return PW_OK;
|
|
}
|
|
static int apply_begin(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!zero_arguments(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
PWLane *lane = &context->lanes[request->lane];
|
|
if (lane->active) {
|
|
return PW_STATE;
|
|
}
|
|
lane->pending = lane->live;
|
|
lane->active = 1;
|
|
lane->dirty = 0;
|
|
return PW_OK;
|
|
}
|
|
static int apply_commit(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!zero_arguments(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
PWLane *lane = &context->lanes[request->lane];
|
|
if (!lane->active) {
|
|
return PW_STATE;
|
|
}
|
|
lane->live = lane->pending;
|
|
if (lane->dirty) {
|
|
lane->generation++;
|
|
}
|
|
lane->active = 0;
|
|
lane->dirty = 0;
|
|
return PW_OK;
|
|
}
|
|
static int apply_abort(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!zero_arguments(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
PWLane *lane = &context->lanes[request->lane];
|
|
if (!lane->active) {
|
|
return PW_STATE;
|
|
}
|
|
lane->pending = lane->live;
|
|
lane->active = 0;
|
|
lane->dirty = 0;
|
|
return PW_OK;
|
|
}
|
|
static int apply_copy(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!lane_valid(request->a) || !zero_tail(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
uint32_t value = pw_effective(context, request->a);
|
|
assign_word(&context->lanes[request->lane], value);
|
|
return PW_OK;
|
|
}
|
|
static int apply_swap(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!lane_valid(request->a) || !zero_tail(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
PWLane *target = &context->lanes[request->lane];
|
|
PWLane *peer = &context->lanes[request->a];
|
|
if (target->active || peer->active) {
|
|
return PW_STATE;
|
|
}
|
|
if (target != peer && target->live != peer->live) {
|
|
uint32_t temporary = target->live;
|
|
target->live = peer->live;
|
|
peer->live = temporary;
|
|
target->generation++;
|
|
peer->generation++;
|
|
}
|
|
return PW_OK;
|
|
}
|
|
static int apply_restore(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!zero_arguments(request)) {
|
|
return PW_INVALID;
|
|
}
|
|
assign_word(&context->lanes[request->lane], context->initial[request->lane]);
|
|
return PW_OK;
|
|
}
|
|
static int dispatch(PWContext *context, const PWRequest *request)
|
|
{
|
|
if (!lane_valid(request->lane)) {
|
|
return PW_INVALID;
|
|
}
|
|
switch (request->op) {
|
|
case PW_BIT:
|
|
return apply_bit(context, request);
|
|
case PW_RANGE:
|
|
return apply_range(context, request);
|
|
case PW_WRITE:
|
|
return apply_write(context, request);
|
|
case PW_BEGIN:
|
|
return apply_begin(context, request);
|
|
case PW_COMMIT:
|
|
return apply_commit(context, request);
|
|
case PW_ABORT:
|
|
return apply_abort(context, request);
|
|
case PW_COPY:
|
|
return apply_copy(context, request);
|
|
case PW_SWAP:
|
|
return apply_swap(context, request);
|
|
case PW_RESTORE:
|
|
return apply_restore(context, request);
|
|
default:
|
|
return PW_INVALID;
|
|
}
|
|
}
|
|
int pw_apply(PWContext *context, const PWRequest *request)
|
|
{
|
|
uint32_t before = pw_effective(context, request->lane);
|
|
int result = dispatch(context, request);
|
|
context->sequence++;
|
|
if (result == PW_OK) {
|
|
context->ok++;
|
|
context->lanes[request->lane].accepted++;
|
|
} else {
|
|
context->bad++;
|
|
if (lane_valid(request->lane)) {
|
|
context->lanes[request->lane].rejected++;
|
|
}
|
|
}
|
|
PWEvent event;
|
|
event.sequence = context->sequence;
|
|
event.request = *request;
|
|
event.result = result;
|
|
event.before = before;
|
|
event.after = pw_effective(context, request->lane);
|
|
pw_audit_push(&context->audit, &event);
|
|
return result;
|
|
}
|
|
void pw_snapshot(const PWContext *context, PWSnapshot *snapshot)
|
|
{
|
|
for (int i = 0; i < PW_LANES; ++i) {
|
|
snapshot->lanes[i] = context->lanes[i];
|
|
}
|
|
snapshot->sequence = context->sequence;
|
|
snapshot->ok = context->ok;
|
|
snapshot->bad = context->bad;
|
|
snapshot->audit = pw_audit_digest(&context->audit);
|
|
snapshot->invalid_requests = context->port.invalid_requests;
|
|
}
|