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>
122 lines
3.6 KiB
C
122 lines
3.6 KiB
C
#include "reduction.h"
|
|
#include <limits.h>
|
|
|
|
int rp_token_i64(const char *text, size_t length, int64_t *out)
|
|
{
|
|
size_t cursor = 0;
|
|
int negative = 0;
|
|
uint64_t magnitude = 0;
|
|
uint64_t limit = INT64_MAX;
|
|
if (text == NULL || out == NULL || length == 0) {
|
|
return RP_INVALID;
|
|
}
|
|
if (text[0] == '+' || text[0] == '-') {
|
|
negative = text[0] == '-';
|
|
cursor = 1;
|
|
}
|
|
if (cursor == length) {
|
|
return RP_INVALID;
|
|
}
|
|
if (negative) {
|
|
limit += 1;
|
|
}
|
|
while (cursor < length) {
|
|
unsigned char byte = (unsigned char)text[cursor];
|
|
unsigned digit;
|
|
if (byte < '0' || byte > '9') {
|
|
return RP_INVALID;
|
|
}
|
|
digit = byte - '0';
|
|
if (magnitude > limit / 10 ||
|
|
(magnitude == limit / 10 && digit > limit % 10)) {
|
|
return RP_INVALID;
|
|
}
|
|
magnitude = magnitude * 10 + digit;
|
|
cursor += 1;
|
|
}
|
|
if (negative && magnitude == (uint64_t)INT64_MAX + 1) {
|
|
*out = INT64_MIN;
|
|
} else if (negative) {
|
|
*out = -(int64_t)magnitude;
|
|
} else {
|
|
*out = (int64_t)magnitude;
|
|
}
|
|
return RP_OK;
|
|
}
|
|
|
|
static int chunk_shape(const struct rp_chunk *chunk)
|
|
{
|
|
__int128 smallest;
|
|
__int128 largest;
|
|
if (chunk->count > RP_MAX_ITEMS) {
|
|
return 0;
|
|
}
|
|
if (chunk->count == 0) {
|
|
return chunk->sum == 0 && chunk->minimum == 0 && chunk->maximum == 0;
|
|
}
|
|
if (chunk->minimum > chunk->maximum) {
|
|
return 0;
|
|
}
|
|
smallest = (__int128)chunk->minimum * chunk->count;
|
|
largest = (__int128)chunk->maximum * chunk->count;
|
|
return smallest <= chunk->sum && chunk->sum <= largest;
|
|
}
|
|
|
|
int rp_merge(const struct rp_chunk *chunks, size_t workers,
|
|
struct rp_chunk *out)
|
|
{
|
|
struct rp_chunk merged = {0, 0, 0, 0};
|
|
__int128 sum = 0;
|
|
size_t index;
|
|
if (chunks == NULL || out == NULL || workers == 0 || workers > RP_MAX_WORKERS) {
|
|
return RP_INVALID;
|
|
}
|
|
for (index = 0; index < workers; ++index) {
|
|
if (!chunk_shape(&chunks[index]) ||
|
|
chunks[index].count > RP_MAX_ITEMS - merged.count) {
|
|
return RP_INVALID;
|
|
}
|
|
merged.count += chunks[index].count;
|
|
}
|
|
for (index = 0; index < workers; ++index) {
|
|
const struct rp_chunk *chunk = &chunks[index];
|
|
sum += chunk->sum;
|
|
if (chunk->count != 0) {
|
|
if (merged.minimum == 0 && merged.maximum == 0) {
|
|
size_t previous;
|
|
int seen = 0;
|
|
for (previous = 0; previous < index; ++previous) {
|
|
seen |= chunks[previous].count != 0;
|
|
}
|
|
if (!seen) {
|
|
merged.minimum = chunk->minimum;
|
|
merged.maximum = chunk->maximum;
|
|
}
|
|
}
|
|
if (chunk->minimum < merged.minimum) {
|
|
merged.minimum = chunk->minimum;
|
|
}
|
|
if (chunk->maximum > merged.maximum) {
|
|
merged.maximum = chunk->maximum;
|
|
}
|
|
}
|
|
}
|
|
if (sum < INT64_MIN || sum > INT64_MAX) {
|
|
return RP_OVERFLOW;
|
|
}
|
|
merged.sum = (int64_t)sum;
|
|
*out = merged;
|
|
return RP_OK;
|
|
}
|
|
|
|
int rp_span_for(size_t count, size_t workers, size_t index, struct rp_span *out)
|
|
{
|
|
if (!out || count > RP_MAX_ITEMS || !workers || workers > RP_MAX_WORKERS || index >= workers)
|
|
return RP_INVALID;
|
|
size_t quotient = count / workers, remainder = count % workers;
|
|
size_t a = index < remainder ? index : remainder;
|
|
size_t b = index + 1 < remainder ? index + 1 : remainder;
|
|
*out = (struct rp_span){index * quotient + a, (index + 1) * quotient + b};
|
|
return RP_OK;
|
|
}
|