ReedSolomonForneyMagnitude constructor
Creates a fixed-capacity Forney magnitude evaluator.
Implementation
ReedSolomonForneyMagnitude(
Logic locator,
Logic syndromes,
Logic errorMask, {
required ReedSolomonCode code,
required int correctionCapacity,
super.name = 'reed_solomon_forney_magnitude',
}) : super(
definitionName: 'ReedSolomonForneyMagnitude_N${code.length}_T'
'$correctionCapacity',
) {
final width = code.field.degree;
final locatorWidth = width * (correctionCapacity + 1);
final syndromeWidth = width * code.paritySymbols;
if (locator.width != locatorWidth) {
throw PortWidthMismatchException(locator, locatorWidth);
}
if (syndromes.width != syndromeWidth) {
throw PortWidthMismatchException(syndromes, syndromeWidth);
}
if (errorMask.width != code.length) {
throw PortWidthMismatchException(errorMask, code.length);
}
final localLocator = addInput('locator', locator, width: locatorWidth);
final localSyndromes =
addInput('syndromes', syndromes, width: syndromeWidth);
final localMask = addInput('errorMask', errorMask, width: code.length);
GfLogic syndrome(int index) => GfLogic.of(
localSyndromes.slice((index + 1) * width - 1, index * width),
field: code.field,
);
GfLogic locatorCoefficient(int index) => GfLogic.of(
localLocator.slice((index + 1) * width - 1, index * width),
field: code.field,
);
final omega = <GfLogic>[
for (var index = 0; index < code.paritySymbols; index++)
GfLogic.constant(code.field.zero),
];
for (var left = 0; left <= correctionCapacity; left++) {
for (var right = 0;
right < code.paritySymbols && left + right < code.paritySymbols;
right++) {
omega[left + right] =
omega[left + right] + locatorCoefficient(left) * syndrome(right);
}
}
Logic packed = Const(0, width: code.length * width);
for (var position = 0; position < code.length; position++) {
final inverseRoot = GfLogic.constant(
code.field.x.pow(
BigInt.from((code.fullLength - position) % code.fullLength),
),
);
var evaluator = omega.last;
for (var coefficient = omega.length - 2;
coefficient >= 0;
coefficient--) {
evaluator = evaluator * inverseRoot + omega[coefficient];
}
var derivative = GfLogic.constant(code.field.zero);
for (var coefficient = correctionCapacity;
coefficient > 0;
coefficient--) {
derivative = derivative * inverseRoot;
if (coefficient.isOdd) {
derivative += locatorCoefficient(coefficient);
}
}
final inverseDerivative = GfInverse.typed(derivative);
final location = code.field.x.pow(BigInt.from(position));
final scale = GfLogic.constant(
location.pow(BigInt.from((1 - code.firstRoot) % code.fullLength)),
);
final magnitude = evaluator * scale * inverseDerivative.gfResult;
final selected = GfLogic.of(
mux(
localMask[position],
magnitude,
GfLogic.constant(code.field.zero),
),
field: code.field,
);
packed = packed | selected.zeroExtend(packed.width) << (position * width);
}
addOutput('magnitudes', width: packed.width) <= packed;
addOutput('valid') <= localMask.or();
}