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>
191 lines
5.6 KiB
C
191 lines
5.6 KiB
C
#include "mode_formula.h"
|
|
#include <string.h>
|
|
|
|
static int valid_channel(int64_t channel)
|
|
{
|
|
return channel>=0 && channel<MF_CHANNELS;
|
|
}
|
|
static int valid_scalar(int64_t value)
|
|
{
|
|
return value>=-1000 && value<=1000;
|
|
}
|
|
static int valid_profile(const mf_profile *profile)
|
|
{
|
|
if (!valid_scalar(profile->gate)) return 0;
|
|
if (!valid_scalar(profile->base)) return 0;
|
|
if (!valid_scalar(profile->gain)) return 0;
|
|
if (profile->mode!=0 && profile->mode!=1) return 0;
|
|
if (!valid_scalar(profile->shift)) return 0;
|
|
if (!valid_scalar(profile->scale)) return 0;
|
|
if (!valid_scalar(profile->pivot)) return 0;
|
|
return 1;
|
|
}
|
|
static void empty_accumulator(mf_channel *channel)
|
|
{
|
|
channel->count=0;
|
|
channel->sum=0;
|
|
channel->minimum=0;
|
|
channel->maximum=0;
|
|
channel->last_ticket=0;
|
|
channel->used=0;
|
|
channel->head=0;
|
|
}
|
|
static int64_t evaluate_raw(const mf_profile *p,int64_t input)
|
|
{
|
|
return p->base+p->gain*input ? 1 : 0;
|
|
}
|
|
static int64_t evaluate_value(const mf_profile *p,int64_t raw)
|
|
{
|
|
if (p->mode==0) return raw;
|
|
return p->shift+p->scale*raw-p->pivot;
|
|
}
|
|
static mf_event make_event(const mf_bank *bank,const mf_channel *channel,int64_t input)
|
|
{
|
|
mf_event event;
|
|
event.ticket=bank->next_ticket;
|
|
event.generation=channel->generation;
|
|
event.input=input;
|
|
event.raw=evaluate_raw(&channel->profile,input);
|
|
event.value=evaluate_value(&channel->profile,event.raw);
|
|
return event;
|
|
}
|
|
static void add_statistics(mf_channel *channel,const mf_event *event)
|
|
{
|
|
if (channel->count==0) {
|
|
channel->minimum=event->value;
|
|
channel->maximum=event->value;
|
|
} else {
|
|
if (event->value<channel->minimum) channel->minimum=event->value;
|
|
if (event->value>channel->maximum) channel->maximum=event->value;
|
|
}
|
|
channel->count+=1;
|
|
channel->sum+=event->value;
|
|
channel->last_ticket=event->ticket;
|
|
}
|
|
static void remember(mf_channel *channel,const mf_event *event)
|
|
{
|
|
channel->history[channel->head]=*event;
|
|
channel->head=(channel->head+1)%MF_HISTORY;
|
|
if (channel->used<MF_HISTORY) channel->used+=1;
|
|
}
|
|
static void commit(mf_bank *bank,mf_channel *channel,const mf_event *event)
|
|
{
|
|
add_statistics(channel,event);
|
|
remember(channel,event);
|
|
bank->next_ticket+=1;
|
|
}
|
|
void mf_reset(mf_bank *bank)
|
|
{
|
|
memset(bank,0,sizeof(*bank));
|
|
bank->next_ticket=1;
|
|
}
|
|
int mf_configure(mf_bank *bank,int64_t index,const mf_profile *profile)
|
|
{
|
|
mf_channel *channel;
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
if (!valid_profile(profile)) return MF_RANGE;
|
|
channel=&bank->channels[index];
|
|
channel->profile=*profile;
|
|
channel->configured=1;
|
|
channel->generation+=1;
|
|
empty_accumulator(channel);
|
|
return MF_OK;
|
|
}
|
|
int mf_apply(mf_bank *bank,int64_t index,int64_t input,mf_event *out)
|
|
{
|
|
mf_channel *channel;
|
|
mf_event event;
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
if (!valid_scalar(input)) return MF_RANGE;
|
|
channel=&bank->channels[index];
|
|
if (!channel->configured) return MF_UNCONFIGURED;
|
|
event=make_event(bank,channel,input);
|
|
commit(bank,channel,&event);
|
|
*out=event;
|
|
return MF_OK;
|
|
}
|
|
static void initialize_batch(mf_batch_result *result)
|
|
{
|
|
result->code=MF_OK;
|
|
result->count=0;
|
|
result->first_ticket=0;
|
|
result->sum=0;
|
|
result->minimum=0;
|
|
result->maximum=0;
|
|
}
|
|
static void fold_batch(mf_batch_result *result,const mf_event *event)
|
|
{
|
|
if (result->count==0) {
|
|
result->first_ticket=event->ticket;
|
|
result->minimum=event->value;
|
|
result->maximum=event->value;
|
|
} else {
|
|
if (event->value<result->minimum) result->minimum=event->value;
|
|
if (event->value>result->maximum) result->maximum=event->value;
|
|
}
|
|
result->sum+=event->value;
|
|
result->count+=1;
|
|
}
|
|
int mf_batch(mf_bank *bank,int64_t index,const int64_t *inputs,size_t length,mf_batch_result *result)
|
|
{
|
|
mf_channel *channel;
|
|
size_t i;
|
|
initialize_batch(result);
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
if (length>MF_BATCH) return MF_RANGE;
|
|
for (i=0;i<length;i++) {
|
|
if (!valid_scalar(inputs[i])) return MF_RANGE;
|
|
}
|
|
channel=&bank->channels[index];
|
|
if (!channel->configured) return MF_UNCONFIGURED;
|
|
for (i=0;i<length;i++) {
|
|
mf_event event=make_event(bank,channel,inputs[i]);
|
|
commit(bank,channel,&event);
|
|
fold_batch(result,&event);
|
|
}
|
|
return MF_OK;
|
|
}
|
|
int mf_stats(const mf_bank *bank,int64_t index,mf_channel *out)
|
|
{
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
*out=bank->channels[index];
|
|
return MF_OK;
|
|
}
|
|
int mf_history(const mf_bank *bank,int64_t index,int64_t offset,mf_event *out)
|
|
{
|
|
const mf_channel *channel;
|
|
size_t position;
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
if (offset<0) return MF_RANGE;
|
|
channel=&bank->channels[index];
|
|
if (!channel->configured) return MF_UNCONFIGURED;
|
|
if ((uint64_t)offset>=channel->used) return MF_MISSING;
|
|
position=(channel->head+MF_HISTORY-1-(size_t)offset)%MF_HISTORY;
|
|
*out=channel->history[position];
|
|
return MF_OK;
|
|
}
|
|
int mf_clear(mf_bank *bank,int64_t index)
|
|
{
|
|
mf_channel *channel;
|
|
if (!valid_channel(index)) return MF_RANGE;
|
|
channel=&bank->channels[index];
|
|
if (!channel->configured) return MF_UNCONFIGURED;
|
|
empty_accumulator(channel);
|
|
return MF_OK;
|
|
}
|
|
|
|
int mf_get_profile(const mf_bank *bank,int64_t index,mf_profile *out,int64_t *generation)
|
|
{
|
|
const mf_channel *channel;
|
|
if (index<0 || index>=MF_CHANNELS) {
|
|
return MF_RANGE;
|
|
}
|
|
channel=&bank->channels[index];
|
|
if (!channel->configured) {
|
|
return MF_UNCONFIGURED;
|
|
}
|
|
*out=channel->profile;
|
|
*generation=channel->generation;
|
|
return MF_OK;
|
|
}
|