All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
292 lines
7.6 KiB
C++
292 lines
7.6 KiB
C++
#include "timer_bank.hpp"
|
|
#include <array>
|
|
#include <limits>
|
|
#include <vector>
|
|
|
|
namespace timers {
|
|
namespace {
|
|
constexpr int INVALID = -22;
|
|
using Word = std::uint64_t;
|
|
using Bytes = std::vector<std::uint8_t>;
|
|
|
|
struct Request {
|
|
unsigned opcode = 0;
|
|
unsigned order = 0;
|
|
std::array<Word, 5> words{};
|
|
int first = 0;
|
|
int second = 0;
|
|
unsigned flags = 0;
|
|
};
|
|
|
|
std::size_t arity(unsigned opcode)
|
|
{
|
|
switch (opcode) {
|
|
case 1: return 2;
|
|
case 2: return 1;
|
|
case 3: return 1;
|
|
case 4: return 5;
|
|
case 5: return 2;
|
|
case 6: return 1;
|
|
case 7: return 3;
|
|
case 8: return 2;
|
|
case 9: return 2;
|
|
case 10: return 2;
|
|
case 11: return 1;
|
|
case 12: return 0;
|
|
case 13: return 1;
|
|
case 14: return 0;
|
|
default: return 99;
|
|
}
|
|
}
|
|
|
|
Word decode_word(const Bytes &bytes, std::size_t offset, unsigned order)
|
|
{
|
|
Word value = 0;
|
|
if (order == 0) {
|
|
for (unsigned index = 0; index < 8; ++index) {
|
|
value |= static_cast<Word>(bytes[offset + index]) << (index * 8);
|
|
}
|
|
} else {
|
|
for (unsigned index = 0; index < 8; ++index) {
|
|
value = (value << 8) | bytes[offset + index];
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void append_word(Bytes &bytes, Word value, unsigned order)
|
|
{
|
|
if (order == 0) {
|
|
for (unsigned index = 0; index < 8; ++index) {
|
|
bytes.push_back(static_cast<std::uint8_t>(value >> (index * 8)));
|
|
}
|
|
} else {
|
|
for (unsigned index = 0; index < 8; ++index) {
|
|
bytes.push_back(static_cast<std::uint8_t>(value >> ((7 - index) * 8)));
|
|
}
|
|
}
|
|
}
|
|
|
|
bool as_int(Word value, int &out)
|
|
{
|
|
const Word positive_max = static_cast<Word>(std::numeric_limits<int>::max());
|
|
const Word negative_min = static_cast<Word>(
|
|
static_cast<std::int64_t>(std::numeric_limits<int>::min()));
|
|
if (value <= positive_max) {
|
|
out = static_cast<int>(value);
|
|
return true;
|
|
}
|
|
if (value < negative_min) {
|
|
return false;
|
|
}
|
|
const Word magnitude = ~value + 1;
|
|
out = static_cast<int>(-static_cast<std::int64_t>(magnitude));
|
|
return true;
|
|
}
|
|
|
|
bool as_unsigned(Word value, unsigned &out)
|
|
{
|
|
if (value > std::numeric_limits<unsigned>::max()) {
|
|
return false;
|
|
}
|
|
out = static_cast<unsigned>(value);
|
|
return true;
|
|
}
|
|
|
|
bool decode_request(const Bytes &bytes, Request &request)
|
|
{
|
|
if (bytes.size() < 8) {
|
|
return false;
|
|
}
|
|
if (bytes[0] != 0x54 || bytes[1] != 0x42 || bytes[2] != 1 ||
|
|
bytes[4] > 1 || bytes[5] != 0) {
|
|
return false;
|
|
}
|
|
const unsigned opcode = bytes[3];
|
|
const std::size_t count = arity(opcode);
|
|
if (count > request.words.size()) {
|
|
return false;
|
|
}
|
|
const std::size_t payload_length = static_cast<std::size_t>(bytes[6]) |
|
|
(static_cast<std::size_t>(bytes[7]) << 8);
|
|
if (payload_length != count * 8 || bytes.size() != 8 + payload_length) {
|
|
return false;
|
|
}
|
|
Request parsed;
|
|
parsed.opcode = opcode;
|
|
parsed.order = bytes[4];
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
parsed.words[index] = decode_word(bytes, 8 + index * 8, parsed.order);
|
|
}
|
|
switch (opcode) {
|
|
case 1:
|
|
if (!as_int(parsed.words[0], parsed.first) ||
|
|
!as_unsigned(parsed.words[1], parsed.flags)) {
|
|
return false;
|
|
}
|
|
break;
|
|
case 2:
|
|
case 3:
|
|
case 7:
|
|
case 8:
|
|
case 10:
|
|
case 11:
|
|
case 13:
|
|
if (!as_int(parsed.words[0], parsed.first)) {
|
|
return false;
|
|
}
|
|
break;
|
|
case 4:
|
|
if (!as_int(parsed.words[0], parsed.first) ||
|
|
!as_unsigned(parsed.words[1], parsed.flags)) {
|
|
return false;
|
|
}
|
|
break;
|
|
case 5:
|
|
if (!as_int(parsed.words[0], parsed.first)) {
|
|
return false;
|
|
}
|
|
break;
|
|
case 9:
|
|
if (!as_int(parsed.words[0], parsed.first) ||
|
|
!as_int(parsed.words[1], parsed.second)) {
|
|
return false;
|
|
}
|
|
break;
|
|
case 6:
|
|
case 12:
|
|
case 14:
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
request = parsed;
|
|
return true;
|
|
}
|
|
|
|
void append_setting(std::vector<Word> &words, const Setting &setting)
|
|
{
|
|
words.push_back(setting.remaining);
|
|
words.push_back(setting.interval);
|
|
words.push_back(setting.armed);
|
|
}
|
|
|
|
void append_inspection(std::vector<Word> &words, const Inspection &inspection)
|
|
{
|
|
words.push_back(inspection.clock);
|
|
words.push_back(inspection.nonblock);
|
|
words.push_back(inspection.remaining);
|
|
words.push_back(inspection.interval);
|
|
words.push_back(inspection.armed);
|
|
words.push_back(inspection.pending);
|
|
words.push_back(inspection.canceled);
|
|
words.push_back(inspection.references);
|
|
}
|
|
|
|
Inspection repeated_inspection(Word nonce)
|
|
{
|
|
return Inspection{nonce, nonce, nonce, nonce, nonce, nonce, nonce, nonce};
|
|
}
|
|
|
|
std::vector<Word> dispatch(Bank &bank, const Request &request)
|
|
{
|
|
std::vector<Word> words(1, 0);
|
|
const std::array<Word, 5> &args = request.words;
|
|
int result = 0;
|
|
switch (request.opcode) {
|
|
case 1:
|
|
result = bank.create(request.first, request.flags);
|
|
break;
|
|
case 2:
|
|
result = bank.duplicate(request.first);
|
|
break;
|
|
case 3:
|
|
result = bank.close(request.first);
|
|
break;
|
|
case 4: {
|
|
Setting old{args[4], args[4], args[4]};
|
|
result = bank.set(request.first, request.flags, args[2], args[3], old);
|
|
append_setting(words, old);
|
|
break;
|
|
}
|
|
case 5:
|
|
result = bank.advance(request.first, args[1]);
|
|
break;
|
|
case 6:
|
|
result = bank.jump(args[0]);
|
|
break;
|
|
case 7: {
|
|
Word value = args[2];
|
|
result = bank.read(request.first, args[1], value);
|
|
words.push_back(value);
|
|
break;
|
|
}
|
|
case 8: {
|
|
Inspection out = repeated_inspection(args[1]);
|
|
result = bank.inspect(request.first, out);
|
|
append_inspection(words, out);
|
|
break;
|
|
}
|
|
case 9:
|
|
result = bank.set_nonblock(request.first, request.second);
|
|
break;
|
|
case 10: {
|
|
Setting out{args[1], args[1], args[1]};
|
|
result = bank.get(request.first, out);
|
|
append_setting(words, out);
|
|
break;
|
|
}
|
|
case 11:
|
|
result = bank.poll(request.first);
|
|
break;
|
|
case 12:
|
|
words.push_back(static_cast<Word>(bank.open_count()));
|
|
words.push_back(static_cast<Word>(bank.object_count()));
|
|
break;
|
|
case 13:
|
|
words.push_back(bank.clock_value(request.first));
|
|
break;
|
|
case 14:
|
|
bank.reset();
|
|
break;
|
|
default:
|
|
result = INVALID;
|
|
break;
|
|
}
|
|
words[0] = static_cast<Word>(static_cast<std::int64_t>(result));
|
|
return words;
|
|
}
|
|
|
|
Bytes encode_response(const Request &request, const std::vector<Word> &words)
|
|
{
|
|
const std::size_t length = words.size() * 8;
|
|
Bytes bytes;
|
|
bytes.reserve(8 + length);
|
|
bytes.push_back(0x54);
|
|
bytes.push_back(0x42);
|
|
bytes.push_back(1);
|
|
bytes.push_back(static_cast<std::uint8_t>(request.opcode | 0x80));
|
|
bytes.push_back(static_cast<std::uint8_t>(request.order));
|
|
bytes.push_back(0);
|
|
bytes.push_back(static_cast<std::uint8_t>(length));
|
|
bytes.push_back(static_cast<std::uint8_t>(length >> 8));
|
|
for (Word word : words) {
|
|
append_word(bytes, word, request.order);
|
|
}
|
|
return bytes;
|
|
}
|
|
}
|
|
|
|
int transact(Bank &bank, const std::vector<std::uint8_t> &request,
|
|
std::vector<std::uint8_t> &response)
|
|
{
|
|
Request decoded;
|
|
if (!decode_request(request, decoded)) {
|
|
return INVALID;
|
|
}
|
|
const std::vector<Word> words = dispatch(bank, decoded);
|
|
response = encode_response(decoded, words);
|
|
return 0;
|
|
}
|
|
}
|