BchBerlekampMassey constructor
Creates a bounded Berlekamp--Massey locator.
Consumes the first 2 * correctionCapacity syndromes; additional complete
syndromes from a stronger code may be supplied.
Implementation
BchBerlekampMassey(
Logic syndromes, {
required BchCode code,
required int correctionCapacity,
super.name = 'bch_berlekamp_massey',
}) : super(
definitionName:
'BchBerlekampMassey_D${code.field.degree}_T$correctionCapacity',
) {
if (correctionCapacity < 1) {
throw ArgumentError.value(correctionCapacity, 'correctionCapacity');
}
final width = code.field.degree;
final syndromeCount = correctionCapacity * 2;
if (code.designedDistance - 1 < syndromeCount) {
throw ArgumentError(
'BCH designed distance must provide twice the correction capacity '
'in consecutive syndromes.',
);
}
if (syndromes.width < syndromeCount * width ||
syndromes.width % width != 0) {
throw PortWidthMismatchException(syndromes, syndromeCount * width);
}
final localSyndromes =
addInput('syndromes', syndromes, width: syndromes.width);
if (correctionCapacity == 2) {
final bounded = BchTwoErrorLocator(
localSyndromes.getRange(0, syndromeCount * width),
code: code,
);
addOutput('locator', width: width * 3) <= bounded.locator;
addOutput('locatorDegree', width: 3) <= bounded.locatorDegree;
addOutput('valid') <= bounded.valid;
return;
}
GfLogic syndrome(int index) => GfLogic.of(
localSyndromes.slice((index + 1) * width - 1, index * width),
field: code.field,
);
var coefficients = <GfLogic>[
GfLogic.constant(code.field.one),
for (var index = 0; index < correctionCapacity; index++)
GfLogic.constant(code.field.zero),
];
var previous = List<GfLogic>.from(coefficients);
Logic degree = Const(0, width: (correctionCapacity + 1).bitLength);
Logic shift = Const(1, width: (correctionCapacity + 1).bitLength);
var scale = GfLogic.constant(code.field.one);
for (var index = 0; index < syndromeCount; index++) {
var discrepancy = syndrome(index);
for (var coefficient = 1;
coefficient <= correctionCapacity && coefficient <= index;
coefficient++) {
final term = coefficients[coefficient] * syndrome(index - coefficient);
discrepancy = discrepancy +
GfLogic.of(
mux(
degree.gte(Const(coefficient, width: degree.width)),
term,
GfLogic.constant(code.field.zero),
),
field: code.field,
);
}
final inverse = GfInverse.typed(scale);
final factor = discrepancy * inverse.gfResult;
final nonzero = discrepancy.neq(GfLogic.constant(code.field.zero));
final update =
nonzero & (degree + degree).lte(Const(index, width: degree.width));
final nextCoefficients = <GfLogic>[];
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++) {
Logic shifted = GfLogic.constant(code.field.zero);
for (var amount = 1; amount <= syndromeCount + 1; amount++) {
final source = coefficient >= amount
? previous[coefficient - amount]
: GfLogic.constant(code.field.zero);
shifted = mux(
shift.eq(Const(amount, width: shift.width)),
source,
shifted,
);
}
final updated = coefficients[coefficient] +
GfLogic.of(factor * GfLogic.of(shifted, field: code.field),
field: code.field);
nextCoefficients.add(
GfLogic.of(
mux(nonzero, updated, coefficients[coefficient]),
field: code.field,
),
);
}
final nextDegree = mux(
update,
Const(index + 1, width: degree.width) - degree,
degree,
);
final nextShift = mux(
nonzero,
mux(
update,
Const(1, width: shift.width),
shift + Const(1, width: shift.width),
),
shift + Const(1, width: shift.width),
);
final nextPrevious = [
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++)
GfLogic.of(
mux(update, coefficients[coefficient], previous[coefficient]),
field: code.field,
),
];
coefficients = nextCoefficients;
previous = nextPrevious;
degree = nextDegree;
shift = nextShift;
scale = GfLogic.of(
mux(update, discrepancy, scale),
field: code.field,
);
}
Logic packed = Const(0, width: width * (correctionCapacity + 1));
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++) {
packed = packed |
coefficients[coefficient].zeroExtend(packed.width) <<
(coefficient * width);
}
addOutput('locator', width: packed.width) <= packed;
addOutput('locatorDegree', width: degree.width) <= degree;
addOutput('valid') <=
degree.neq(0) &
degree.lte(Const(correctionCapacity, width: degree.width));
}