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>
223 lines
5.2 KiB
C++
223 lines
5.2 KiB
C++
#include "quant.hpp"
|
|
|
|
namespace aq {
|
|
Bank::Bank()
|
|
: profiles_{}, ready_{}, revision_(0), counters_{}
|
|
{
|
|
for (std::size_t index = 0; index < max_channels; ++index) {
|
|
ready_[index] = false;
|
|
profiles_[index] = Profile{1, 1, 0, -128, 127};
|
|
}
|
|
}
|
|
|
|
Status Bank::configure(std::size_t channel, const Profile& profile)
|
|
{
|
|
if (channel >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
Profile normalized{};
|
|
Status status = detail::normalize(profile, normalized);
|
|
if (status != Status::Ok) {
|
|
return status;
|
|
}
|
|
if (ready_[channel] && detail::same(profiles_[channel], normalized)) {
|
|
return Status::Ok;
|
|
}
|
|
profiles_[channel] = normalized;
|
|
ready_[channel] = true;
|
|
++revision_;
|
|
return Status::Ok;
|
|
}
|
|
|
|
Status Bank::replace(std::size_t count, const Profile* profiles)
|
|
{
|
|
if (count < 1 || count > max_channels) {
|
|
return Status::Shape;
|
|
}
|
|
if (profiles == nullptr) {
|
|
return Status::Buffer;
|
|
}
|
|
std::array<Profile, max_channels> normalized{};
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
Status status = detail::normalize(profiles[index], normalized[index]);
|
|
if (status != Status::Ok) {
|
|
return status;
|
|
}
|
|
}
|
|
bool changed = false;
|
|
for (std::size_t index = 0; index < max_channels; ++index) {
|
|
bool desired = index < count;
|
|
if (ready_[index] != desired) {
|
|
changed = true;
|
|
}
|
|
if (desired && ready_[index]) {
|
|
if (!detail::same(profiles_[index], normalized[index])) {
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
if (!changed) {
|
|
return Status::Ok;
|
|
}
|
|
for (std::size_t index = 0; index < max_channels; ++index) {
|
|
ready_[index] = index < count;
|
|
if (ready_[index]) {
|
|
profiles_[index] = normalized[index];
|
|
} else {
|
|
profiles_[index] = Profile{1, 1, 0, -128, 127};
|
|
}
|
|
}
|
|
++revision_;
|
|
return Status::Ok;
|
|
}
|
|
|
|
Status Bank::erase(std::size_t channel)
|
|
{
|
|
if (channel >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (!ready_[channel]) {
|
|
return Status::Ok;
|
|
}
|
|
ready_[channel] = false;
|
|
profiles_[channel] = Profile{1, 1, 0, -128, 127};
|
|
++revision_;
|
|
return Status::Ok;
|
|
}
|
|
|
|
void Bank::reset()
|
|
{
|
|
bool changed = false;
|
|
for (std::size_t index = 0; index < max_channels; ++index) {
|
|
if (ready_[index]) {
|
|
changed = true;
|
|
}
|
|
}
|
|
if (!changed) {
|
|
return;
|
|
}
|
|
for (std::size_t index = 0; index < max_channels; ++index) {
|
|
ready_[index] = false;
|
|
profiles_[index] = Profile{1, 1, 0, -128, 127};
|
|
}
|
|
++revision_;
|
|
}
|
|
|
|
Status Bank::get(std::size_t channel, Profile& output) const
|
|
{
|
|
if (channel >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (!ready_[channel]) {
|
|
return Status::NotReady;
|
|
}
|
|
output = profiles_[channel];
|
|
return Status::Ok;
|
|
}
|
|
|
|
bool Bank::ready(std::size_t channel) const
|
|
{
|
|
if (channel >= max_channels) {
|
|
return false;
|
|
}
|
|
return ready_[channel];
|
|
}
|
|
|
|
std::size_t Bank::count() const
|
|
{
|
|
std::size_t result = 0;
|
|
for (bool state : ready_) {
|
|
if (state) {
|
|
++result;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::uint64_t Bank::revision() const
|
|
{
|
|
return revision_;
|
|
}
|
|
|
|
Counters Bank::counters() const
|
|
{
|
|
return counters_;
|
|
}
|
|
|
|
void Bank::clear_counters()
|
|
{
|
|
counters_ = Counters{};
|
|
}
|
|
|
|
void Bank::record_clip(int direction, Counters& counters) const
|
|
{
|
|
if (direction < 0) {
|
|
++counters.clipped_low;
|
|
}
|
|
if (direction > 0) {
|
|
++counters.clipped_high;
|
|
}
|
|
}
|
|
|
|
Status Bank::quantize(std::size_t channel, Rational input, int& output)
|
|
{
|
|
if (channel >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (!ready_[channel]) {
|
|
return Status::NotReady;
|
|
}
|
|
if (!detail::valid(input)) {
|
|
return Status::Value;
|
|
}
|
|
int clipping = 0;
|
|
int encoded = detail::encode(profiles_[channel], input, clipping);
|
|
output = encoded;
|
|
++counters_.quantized;
|
|
record_clip(clipping, counters_);
|
|
return Status::Ok;
|
|
}
|
|
|
|
Status Bank::dequantize(std::size_t channel, int input, Rational& output)
|
|
{
|
|
if (channel >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (!ready_[channel]) {
|
|
return Status::NotReady;
|
|
}
|
|
if (!detail::valid_code(input)) {
|
|
return Status::Value;
|
|
}
|
|
output = detail::decode(profiles_[channel], input);
|
|
++counters_.dequantized;
|
|
return Status::Ok;
|
|
}
|
|
|
|
Status Bank::convert(std::size_t source, std::size_t destination, int input, int& output)
|
|
{
|
|
if (source >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (destination >= max_channels) {
|
|
return Status::Index;
|
|
}
|
|
if (!ready_[source]) {
|
|
return Status::NotReady;
|
|
}
|
|
if (!ready_[destination]) {
|
|
return Status::NotReady;
|
|
}
|
|
if (!detail::valid_code(input)) {
|
|
return Status::Value;
|
|
}
|
|
Rational decoded = detail::decode(profiles_[source], input);
|
|
int clipping = 0;
|
|
int encoded = detail::encode(profiles_[destination], decoded, clipping);
|
|
output = encoded;
|
|
++counters_.converted;
|
|
record_clip(clipping, counters_);
|
|
return Status::Ok;
|
|
}
|
|
}
|