FixedPointSqrt constructor
Constructor
Implementation
FixedPointSqrt(super.a) {
if (a.signed) {
throw RohdHclException('Signed values not supported');
}
Logic solution = FixedPoint(
signed: a.signed,
name: 'solution',
integerWidth: a.integerWidth + 1,
fractionWidth: a.fractionWidth + 1);
Logic remainder = FixedPoint(
signed: a.signed,
name: 'remainder',
integerWidth: a.integerWidth + 1,
fractionWidth: a.fractionWidth + 1);
Logic subtractionValue = FixedPoint(
signed: a.signed,
name: 'subValue',
integerWidth: a.integerWidth + 1,
fractionWidth: a.fractionWidth + 1);
Logic aLoc = FixedPoint(
signed: a.signed,
name: 'aLoc',
integerWidth: a.integerWidth + 1,
fractionWidth: a.fractionWidth + 1);
solution = Const(0, width: aLoc.width).named('solution');
remainder = Const(0, width: aLoc.width).named('remainder');
subtractionValue = Const(0, width: aLoc.width).named('subtraction');
aLoc = [Const(0), a, Const(0)].swizzle().named('a_loc');
final outputSqrt = a.clone(name: 'sqrt');
output('sqrt') <= outputSqrt;
// loop once through input value
for (var i = 0; i < ((width + 2) >> 1); i++) {
// append bits from a, two at a time
remainder = [
remainder.slice(width + 2 - 3, 0),
aLoc.slice(aLoc.width - 1 - (i * 2), aLoc.width - 2 - (i * 2))
].swizzle().named('remainder_iter$i');
subtractionValue = [solution.slice(width + 2 - 3, 0), Const(1, width: 2)]
.swizzle()
.named('subtraction_value_iter$i');
solution = [
solution.slice(width + 2 - 2, 0),
subtractionValue.lte(remainder)
].swizzle().named('solution_iter$i');
remainder = mux(subtractionValue.lte(remainder),
remainder - subtractionValue, remainder)
.named('remainder_mux_iter$i');
}
// loop again to finish remainder
for (var i = 0; i < ((width + 2) >> 1) - 1; i++) {
// don't try to append bits from a, they are done
remainder = [remainder.slice(width + 2 - 3, 0), Const(0, width: 2)]
.swizzle()
.named('remainder_final_iter$i');
subtractionValue = [solution.slice(width + 2 - 3, 0), Const(1, width: 2)]
.swizzle()
.named('subtraction_value_final_iter$i');
solution = [
solution.slice(width + 2 - 2, 0),
subtractionValue.lte(remainder)
].swizzle().named('solution_final_iter$i');
remainder = mux(subtractionValue.lte(remainder),
remainder - subtractionValue, remainder)
.named('remainder_finalmux_iter$i');
}
solution = (solution + 1).named('solutionPlusOne');
outputSqrt <=
solution.slice(aLoc.width - 1, aLoc.width - a.width).named('solutionF');
}