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>
135 lines
3.2 KiB
C++
135 lines
3.2 KiB
C++
#include "quant.hpp"
|
|
|
|
namespace aq {
|
|
namespace detail {
|
|
std::int64_t gcd(std::int64_t left, std::int64_t right)
|
|
{
|
|
if (left < 0) {
|
|
left = -left;
|
|
}
|
|
if (right < 0) {
|
|
right = -right;
|
|
}
|
|
while (right != 0) {
|
|
std::int64_t remainder = left % right;
|
|
left = right;
|
|
right = remainder;
|
|
}
|
|
return left;
|
|
}
|
|
|
|
Status normalize(const Profile& input, Profile& output)
|
|
{
|
|
if (input.num < 1 || input.num > 1000000) {
|
|
return Status::Scale;
|
|
}
|
|
if (input.den < 1 || input.den > 1000000) {
|
|
return Status::Scale;
|
|
}
|
|
if (input.zero < -128 || input.zero > 127) {
|
|
return Status::Zero;
|
|
}
|
|
if (input.low < -128 || input.low > 127) {
|
|
return Status::Range;
|
|
}
|
|
if (input.high < -128 || input.high > 127) {
|
|
return Status::Range;
|
|
}
|
|
if (input.low > input.high) {
|
|
return Status::Range;
|
|
}
|
|
if (input.zero < input.low || input.zero > input.high) {
|
|
return Status::Range;
|
|
}
|
|
Profile normalized = input;
|
|
std::int64_t divisor = gcd(input.num, input.den);
|
|
normalized.num /= divisor;
|
|
normalized.den /= divisor;
|
|
output = normalized;
|
|
return Status::Ok;
|
|
}
|
|
|
|
bool same(const Profile& left, const Profile& right)
|
|
{
|
|
if (left.num != right.num) {
|
|
return false;
|
|
}
|
|
if (left.den != right.den) {
|
|
return false;
|
|
}
|
|
if (left.zero != right.zero) {
|
|
return false;
|
|
}
|
|
if (left.low != right.low) {
|
|
return false;
|
|
}
|
|
return left.high == right.high;
|
|
}
|
|
|
|
bool valid(Rational value)
|
|
{
|
|
if (value.num < -1000000000 || value.num > 1000000000) {
|
|
return false;
|
|
}
|
|
if (value.den < 1 || value.den > 1000000) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool valid_code(int value)
|
|
{
|
|
return value >= -128 && value <= 127;
|
|
}
|
|
|
|
Rational reduce(std::int64_t numerator, std::int64_t denominator)
|
|
{
|
|
if (numerator == 0) {
|
|
return Rational{0, 1};
|
|
}
|
|
std::int64_t divisor = gcd(numerator, denominator);
|
|
Rational result;
|
|
result.num = numerator / divisor;
|
|
result.den = denominator / divisor;
|
|
return result;
|
|
}
|
|
|
|
int encode(const Profile& profile, Rational input, int& clipping)
|
|
{
|
|
const std::int64_t numerator = input.num * profile.den;
|
|
const std::int64_t denominator = input.den * profile.num;
|
|
const bool negative = numerator < 0;
|
|
const std::int64_t magnitude = negative ? -numerator : numerator;
|
|
int first = 0;
|
|
int last = 256;
|
|
while (first < last) {
|
|
int middle = first + (last - first + 1) / 2;
|
|
std::int64_t threshold = (2 * middle - 1) * denominator;
|
|
if (2 * magnitude >= threshold) {
|
|
first = middle;
|
|
} else {
|
|
last = middle - 1;
|
|
}
|
|
}
|
|
const int signed_value = negative ? -first : first;
|
|
const std::int64_t shifted = signed_value + profile.zero;
|
|
clipping = 0;
|
|
if (shifted < profile.low) {
|
|
clipping = -1;
|
|
return profile.low;
|
|
}
|
|
if (shifted > profile.high) {
|
|
clipping = 1;
|
|
return profile.high;
|
|
}
|
|
return static_cast<int>(shifted);
|
|
}
|
|
|
|
Rational decode(const Profile& profile, int input)
|
|
{
|
|
const std::int64_t distance = static_cast<std::int64_t>(input) - profile.zero;
|
|
return reduce(distance * profile.num, profile.den);
|
|
}
|
|
}
|
|
}
|