amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

114 lines
3.0 KiB
C

#include "reduction.h"
#include <string.h>
struct work_record {
struct rp_span span;
struct rp_chunk chunk;
int status;
};
static int valid_request(const struct rp_request *request)
{
size_t index;
if (request == NULL) {
return 0;
}
if (request->count > RP_MAX_ITEMS) {
return 0;
}
if (request->workers == 0 || request->workers > RP_MAX_WORKERS) {
return 0;
}
if (request->lower >= request->upper) {
return 0;
}
for (index = 0; index < request->count; ++index) {
int64_t value = request->values[index];
if (value < request->lower || value > request->upper) {
return 0;
}
}
return 1;
}
static int prepare_records(const struct rp_request *request,
struct work_record *records)
{
struct rp_span spans[RP_MAX_WORKERS];
size_t index;
int status;
status = rp_plan(request->count, request->workers, spans, RP_MAX_WORKERS);
if (status != RP_OK) {
return status;
}
for (index = 0; index < request->workers; ++index) {
records[index].span = spans[index];
records[index].chunk.count = 0;
records[index].chunk.sum = 0;
records[index].chunk.minimum = 0;
records[index].chunk.maximum = 0;
records[index].status = RP_OK;
}
return RP_OK;
}
static void evaluate_records(const struct rp_request *request,
struct work_record *records)
{
size_t index;
for (index = 0; index < request->workers; ++index) {
records[index].status = rp_partial(request->values, request->count,
records[index].span,
&records[index].chunk);
}
}
static int collect_records(const struct work_record *records, size_t workers,
struct rp_result *result)
{
size_t index;
int status = RP_OK;
for (index = 0; index < workers; ++index) {
if (records[index].status != RP_OK) {
status = records[index].status;
}
}
if (status != RP_OK) {
return status;
}
for (index = 0; index < workers; ++index) {
result->chunks[index] = records[index].chunk;
}
status = rp_merge(result->chunks, workers, &result->total);
if (status != RP_OK) {
return status;
}
result->workers = workers;
return RP_OK;
}
int rp_run(const struct rp_request *request, struct rp_result *out)
{
struct work_record records[RP_MAX_WORKERS];
struct rp_result pending;
int status;
if (out == NULL) {
return RP_INVALID;
}
if (!valid_request(request)) {
return RP_INVALID;
}
memset(&pending, 0, sizeof pending);
status = prepare_records(request, records);
if (status != RP_OK) {
return status;
}
evaluate_records(request, records);
status = collect_records(records, request->workers, &pending);
if (status != RP_OK) {
return status;
}
*out = pending;
return RP_OK;
}