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

6.9 KiB
Raw Permalink Blame History

Reduction interface

Values and ownership

  1. reduction.h defines RP_MAX_ITEMS=4096, RP_MAX_WORKERS=64, RP_OK=0, RP_INVALID=1, and RP_OVERFLOW=2. Arithmetic uses mathematical signed integers. An int64_t output must be representable from -9223372036854775808 through 9223372036854775807. A transient sum outside this range does not itself cause failure if the completed sum is representable.
  2. Each rp_span contains size_t begin,end; each rp_chunk contains size_t count and int64_t sum,minimum,maximum. An empty chunk has all four fields zero. A nonempty chunk records its item count, sum, smallest value and largest value.
  3. For the arithmetic APIs in clauses 5–10, every argument marked const is unchanged over its complete object or supplied byte span, including unused array elements. Output objects may start with arbitrary values. A failed library call leaves its complete output object unchanged. Successful calls replace the documented fields; no output is a view into input storage. Calls have no persistent state and separate objects remain independent when interleaved or reused. Input and output storage do not overlap.
  4. In clauses 5–10, a null required pointer returns RP_INVALID without dereferencing it. Counts and capacities refer to actual caller-provided arrays, and callers provide storage for the declared count or capacity. There is no artificial limit on repeated calls or console requests.

Library calls

  1. int rp_token_i64(const char *text, size_t length, int64_t *out) reads exactly length bytes, without requiring a trailing NUL. Its accepted grammar is an optional ASCII + or - followed by one or more ASCII digits. Leading zeros and signed zero are allowed. Whitespace, embedded NUL, other bytes, missing digits, or an unrepresentable value return RP_INVALID; success writes the value and returns RP_OK. Both pointers are required.
  2. int rp_span_for(size_t count, size_t workers, size_t index, struct rp_span *out) requires count<=4096, 1<=workers<=64, index<workers, and a nonnull output. Invalid arguments return RP_INVALID. On success workers partition indexes [0,count) in order. With q=count/workers and r=count%workers, the first r workers receive q+1 items and the remaining workers receive q items. The result contains the begin index and the end index after the worker's final item. Empty spans are allowed. Return RP_OK.
  3. int rp_plan(size_t count, size_t workers, struct rp_span *spans, size_t capacity) returns all spans from clause 6. The output pointer is required and capacity>=workers; other size rules are those of clause 6. It returns RP_INVALID on invalid arguments, otherwise RP_OK. On success it writes the first workers elements and leaves every later element through capacity unchanged. On failure it writes no elements.
  4. int rp_partial(const int64_t *values, size_t count, struct rp_span span, struct rp_chunk *out) requires count<=4096 and 0<=span.begin<=span.end<=count. The output is required; values may be null only when count is zero. It summarizes values in [span.begin,span.end) according to clause 2. Other values are ignored. Invalid arguments return RP_INVALID. A completed sum outside int64 returns RP_OVERFLOW; otherwise it writes the chunk and returns RP_OK.
  5. int rp_merge(const struct rp_chunk *chunks, size_t workers, struct rp_chunk *out) requires both pointers and 1<=workers<=64. Every chunk has count at most 4096; the combined count is at most 4096. Empty chunks have four zero fields. A nonempty chunk has minimum<=maximum and count*minimum<=sum<=count*maximum, with those comparisons performed in mathematical integers. Violation of any of these rules returns RP_INVALID before sum overflow is considered. The merged count and sum are the sums of those fields, and extrema come from nonempty chunks. A combined sum outside int64 returns RP_OVERFLOW; success writes the merged chunk and returns RP_OK.
  6. int rp_run(const struct rp_request *request, struct rp_result *out) requires both pointers. A request contains size_t count,workers, int64_t lower,upper, and int64_t values[4096]; it requires count<=4096, 1<=workers<=64, and lower<upper. Each of the first count values satisfies lower<=value<upper. Unused value elements do not affect computation. Validate the whole request before computing any worker result. Any invalid request returns RP_INVALID, including an invalid value after an earlier group that would overflow. Then compute clause 6 spans, clause 8 chunks and clause 9 total. A worker sum or merged sum outside int64 returns RP_OVERFLOW. On success return RP_OK, write result.workers, total, and chunks in worker order, and set all inactive chunk fields through index 63 to zero.

Console

  1. int rp_console(FILE *input, FILE *output) reads requests until EOF without closing either stream. A null stream returns 2. Every LF-terminated input line is one request; a nonempty final line without LF is also a request. Empty stdin yields zero stdout bytes. There is no fixed line or token length limit apart from available memory. NUL is an input byte, not an end marker. Space, tab and CR are the only separators within a line.
  2. Each line contains decimal integers count workers lower upper value0 .... Integer tokens follow clause 5. Exactly count values must follow the four header tokens. Empty lines, missing or extra tokens, malformed tokens, negative count/workers, and values or header fields outside their declared ranges produce RP_INVALID. The reduction then follows clause 10, including zero count and workers exceeding count.
  3. Emit one JSON object per request in the same order. A rejected request has integer status 1 or 2. A successful object has integer status:0, integer workers, object total, and array chunks of exactly workers objects. Each chunk object has integer count,sum,minimum,maximum. These fields equal the library result. Additional object members and JSON whitespace are allowed. Consecutive objects are separated by JSON whitespace; NaN and Infinity are not JSON values. The output stream consists only of these JSON values and whitespace and is encoded in UTF-8. JSON booleans are not integers.
  4. Input errors and arithmetic overflow are per-request results; continue processing later lines and return 0 on normal EOF. Return 2 for a stream read/write failure or an allocation failure; already emitted results may remain. Stderr is diagnostic text with no required content or encoding. For the executable, normal request processing returns 0; it uses stdin/stdout with the same contract.

Delivery

  1. make builds the complete program from the declared files with the provided C11/POSIX Linux toolchain and no downloads. make smoke runs smoke.txt. The sample's three completed totals are respectively 16, 0, and 6. Those examples do not replace any part of this contract. All behavior in this document is required. Only the files listed in README.md are collected.