amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

357 lines
10 KiB
C++

#include "sg.hpp"
#include <algorithm>
#include <set>
namespace sg {
bool Engine::bounded(I value, I upper) const {
if (value < 0) {
return false;
}
if (value > upper) {
return false;
}
return true;
}
bool Engine::valid_id(I id) const {
return bounded(id, 31);
}
bool Engine::valid_cursor(I id) const {
return bounded(id, 15);
}
bool Engine::valid_alignment(I alignment) const {
if (alignment < 1 || alignment > 64) {
return false;
}
return (alignment & (alignment - 1)) == 0;
}
bool Engine::interval(I offset, I count, I length) const {
if (!bounded(offset, length)) {
return false;
}
if (!bounded(count, length)) {
return false;
}
return count <= length - offset;
}
Result Engine::failure(Code code) const {
Result out;
out.code = code;
return out;
}
I Engine::total() const {
I sum = 0;
for (I id : order_) {
auto found = segments_.find(id);
if (found != segments_.end()) {
sum += static_cast<I>(found->second.bytes.size());
}
}
return sum;
}
bool Engine::ordered(I id) const {
for (I item : order_) {
if (item == id) {
return true;
}
}
return false;
}
Result Engine::put(I id, I length, I alignment, I value) {
if (!valid_id(id) || !bounded(length, 256)) {
return failure(RANGE);
}
if (!valid_alignment(alignment) || !bounded(value, 255)) {
return failure(RANGE);
}
if (segments_.find(id) != segments_.end()) {
return failure(EXISTS);
}
if (!cursors_.empty()) {
return failure(BUSY);
}
Segment segment;
segment.alignment = alignment;
segment.bytes.assign(static_cast<std::size_t>(length), static_cast<unsigned char>(value));
segments_.emplace(id, std::move(segment));
Result out;
out.data = {id, length, alignment};
return out;
}
Result Engine::remove(I id) {
if (!valid_id(id)) {
return failure(RANGE);
}
auto found = segments_.find(id);
if (found == segments_.end()) {
return failure(MISSING);
}
if (!cursors_.empty()) {
return failure(BUSY);
}
if (ordered(id)) {
return failure(IN_USE);
}
I length = static_cast<I>(found->second.bytes.size());
segments_.erase(found);
Result out;
out.data = {id, length};
return out;
}
Result Engine::order(const std::vector<I>& ids) {
if (ids.size() > 32) {
return failure(RANGE);
}
for (I id : ids) {
if (!valid_id(id)) {
return failure(RANGE);
}
}
std::set<I> seen;
for (I id : ids) {
if (!seen.insert(id).second) {
return failure(DUPLICATE);
}
}
for (I id : ids) {
if (segments_.find(id) == segments_.end()) {
return failure(MISSING);
}
}
if (!cursors_.empty()) {
return failure(BUSY);
}
order_ = ids;
Result out;
out.data.push_back(total());
out.data.push_back(static_cast<I>(order_.size()));
for (I id : order_) {
out.data.push_back(id);
}
return out;
}
Result Engine::write(I id, I offset, I count, I value) {
if (!valid_id(id) || !bounded(offset, 256) || !bounded(count, 256)) {
return failure(RANGE);
}
if (!bounded(value, 255)) {
return failure(RANGE);
}
auto found = segments_.find(id);
if (found == segments_.end()) {
return failure(MISSING);
}
I size = static_cast<I>(found->second.bytes.size());
if (!interval(offset, count, size)) {
return failure(RANGE);
}
if (!tickets_.empty()) {
return failure(BUSY);
}
for (I index = 0; index < count; ++index) {
found->second.bytes[static_cast<std::size_t>(offset + index)] = static_cast<unsigned char>(value);
}
Result out;
out.data = {count};
return out;
}
Result Engine::read(I id, I offset, I count) const {
if (!valid_id(id) || !bounded(offset, 256) || !bounded(count, 256)) {
return failure(RANGE);
}
auto found = segments_.find(id);
if (found == segments_.end()) {
return failure(MISSING);
}
I size = static_cast<I>(found->second.bytes.size());
if (!interval(offset, count, size)) {
return failure(RANGE);
}
Result out;
for (I index = 0; index < count; ++index) {
unsigned char byte = found->second.bytes[static_cast<std::size_t>(offset + index)];
out.data.push_back(static_cast<I>(byte));
}
return out;
}
Result Engine::segment(I id) const {
if (!valid_id(id)) {
return failure(RANGE);
}
auto found = segments_.find(id);
if (found == segments_.end()) {
return failure(MISSING);
}
I length = static_cast<I>(found->second.bytes.size());
I sum = 0;
for (unsigned char byte : found->second.bytes) {
sum += static_cast<I>(byte);
}
Result out;
out.data = {id, length, found->second.alignment, ordered(id) ? 1 : 0, sum};
return out;
}
std::vector<I> Engine::point(I offset) const {
std::vector<std::pair<I,I>> cells;
for (I id : order_) {
for (I local = 0; local < static_cast<I>(segments_.at(id).bytes.size()); ++local) {
cells.emplace_back(id, local);
}
}
if (offset == static_cast<I>(cells.size())) {
return {-1,0,0};
}
auto cell = cells.at(static_cast<std::size_t>(offset));
I remaining = static_cast<I>(segments_.at(cell.first).bytes.size()) - cell.second;
return {cell.first, cell.second, remaining};
}
std::vector<I> Engine::pieces(I offset, I count, I chunk) const {
std::vector<std::pair<I,I>> cells;
for (I id : order_) {
for (I local = 0; local < static_cast<I>(segments_.at(id).bytes.size()); ++local) {
cells.emplace_back(id, local);
}
}
std::vector<I> answer;
I cursor = offset;
while (cursor < offset + count) {
auto first = cells.at(static_cast<std::size_t>(cursor));
I take = 1;
while (take < chunk && cursor + take < offset + count) {
auto next = cells.at(static_cast<std::size_t>(cursor + take));
if (next.first != first.first || next.second != first.second + take) {
break;
}
++take;
}
answer.insert(answer.end(), {first.first, first.second, take});
cursor += take;
}
return answer;
}
Result Engine::locate(I offset) const {
if (!bounded(offset, total())) {
return failure(RANGE);
}
Result out;
out.data = point(offset);
return out;
}
Result Engine::slice(I offset, I count, I chunk) const {
if (!bounded(offset, 8192) || !bounded(count, 8192)) {
return failure(RANGE);
}
if (chunk < 1 || chunk > 256) {
return failure(RANGE);
}
if (!interval(offset, count, total())) {
return failure(RANGE);
}
Result out;
out.data = pieces(offset, count, chunk);
return out;
}
Result Engine::gather(I offset, I count) const {
if (!bounded(offset, 8192) || !bounded(count, 8192)) {
return failure(RANGE);
}
if (!interval(offset, count, total())) {
return failure(RANGE);
}
std::vector<I> ranges = pieces(offset, count, 256);
Result out;
for (std::size_t index = 0; index < ranges.size(); index += 3) {
I id = ranges[index];
I local = ranges[index + 1];
I length = ranges[index + 2];
const Segment& segment = segments_.at(id);
for (I byte = 0; byte < length; ++byte) {
std::size_t at = static_cast<std::size_t>(local + byte);
out.data.push_back(static_cast<I>(segment.bytes[at]));
}
}
return out;
}
Result Engine::scatter(I offset, const std::vector<I>& bytes) {
if (!bounded(offset, 8192) || bytes.size() > 8192) {
return failure(RANGE);
}
for (I value : bytes) {
if (!bounded(value, 255)) {
return failure(RANGE);
}
}
I count = static_cast<I>(bytes.size());
if (!interval(offset, count, total())) {
return failure(RANGE);
}
if (!tickets_.empty()) {
return failure(BUSY);
}
std::vector<I> ranges = pieces(offset, count, 256);
std::size_t consumed = 0;
for (std::size_t index = 0; index < ranges.size(); index += 3) {
I id = ranges[index];
I local = ranges[index + 1];
I length = ranges[index + 2];
Segment& segment = segments_.at(id);
for (I byte = 0; byte < length; ++byte) {
std::size_t at = static_cast<std::size_t>(local + byte);
segment.bytes[at] = static_cast<unsigned char>(bytes[consumed]);
++consumed;
}
}
Result out;
out.data = {count};
return out;
}
Result Engine::copy(I source, I destination, I count) {
if (!bounded(source, 8192)) {
return failure(RANGE);
}
if (!bounded(destination, 8192)) {
return failure(RANGE);
}
if (!bounded(count, 8192)) {
return failure(RANGE);
}
I length = total();
if (!interval(source, count, length)) {
return failure(RANGE);
}
if (!interval(destination, count, length)) {
return failure(RANGE);
}
if (!tickets_.empty()) {
return failure(BUSY);
}
std::vector<I> from = pieces(source, count, 256);
std::vector<unsigned char> saved;
saved.reserve(static_cast<std::size_t>(count));
for (std::size_t part = 0; part < from.size(); part += 3) {
I id = from[part];
I first = from[part + 1];
I length = from[part + 2];
const Segment& segment = segments_.at(id);
for (I index = 0; index < length; ++index) {
std::size_t at = static_cast<std::size_t>(first + index);
saved.push_back(segment.bytes[at]);
}
}
std::vector<I> to = pieces(destination, count, 256);
std::size_t consumed = 0;
for (std::size_t part = 0; part < to.size(); part += 3) {
I id = to[part];
I first = to[part + 1];
I length = to[part + 2];
Segment& segment = segments_.at(id);
for (I index = 0; index < length; ++index) {
std::size_t at = static_cast<std::size_t>(first + index);
segment.bytes[at] = saved[consumed];
++consumed;
}
}
Result out;
out.data = {count};
return out;
}
}