BchTwoErrorLocator constructor
Creates a two-error locator from four packed syndromes.
Implementation
BchTwoErrorLocator(
Logic syndromes, {
required BchCode code,
super.name = 'bch_two_error_locator',
}) : super(
definitionName: 'BchTwoErrorLocator_D${code.field.degree}',
) {
if (code.designedDistance < 5) {
throw ArgumentError('Two-error BCH location requires distance >= 5.');
}
final width = code.field.degree;
if (syndromes.width != width * 4) {
throw PortWidthMismatchException(syndromes, width * 4);
}
final localSyndromes =
addInput('syndromes', syndromes, width: syndromes.width);
GfLogic syndrome(int index) => GfLogic.of(
localSyndromes.slice((index + 1) * width - 1, index * width),
field: code.field,
);
final s1 = syndrome(0);
final s2 = syndrome(1);
final s3 = syndrome(2);
final s4 = syndrome(3);
final denominator = (s1 * s3) + (s2 * s2);
final numeratorOne = (s1 * s4) + (s2 * s3);
final numeratorTwo = (s2 * s4) + (s3 * s3);
final inverse = GfInverse.typed(denominator);
final sigmaOne = numeratorOne * inverse.gfResult;
final sigmaTwo = numeratorTwo * inverse.gfResult;
final nonzero = denominator.neq(GfLogic.constant(code.field.zero));
final packedLocator = Const(1, width: width * 3) |
(sigmaOne.zeroExtend(width * 3) << width) |
(sigmaTwo.zeroExtend(width * 3) << (2 * width));
addOutput('locator', width: width * 3) <=
mux(nonzero, packedLocator, Const(0, width: width * 3));
addOutput('locatorDegree', width: 3) <=
mux(
nonzero,
Logic.from(Const(2, width: 3)),
Logic.from(Const(0, width: 3)),
);
addOutput('valid') <= nonzero;
}