|
| 1 | +// Copyright (C) 2025 Intel Corporation |
| 2 | +// SPDX-License-Indentifier: BSD-3-Clause |
| 3 | +// |
| 4 | +// fixed_point_sqrt.dart |
| 5 | +// An abstract base class defining the API for floating-point square root. |
| 6 | +// |
| 7 | +// 2025 March 3 |
| 8 | +// Authors: James Farwell <james.c.farwell@intel.com>, Stephen |
| 9 | +// Weeks <stephen.weeks@intel.com> |
| 10 | + |
| 11 | +/// An abstract API for fixed point square root. |
| 12 | +library; |
| 13 | + |
| 14 | +import 'package:meta/meta.dart'; |
| 15 | +import 'package:rohd/rohd.dart'; |
| 16 | +import 'package:rohd_hcl/rohd_hcl.dart'; |
| 17 | + |
| 18 | +/// Abstract base class |
| 19 | +abstract class FixedPointSqrtBase extends Module { |
| 20 | + /// Width of the input and output fields. |
| 21 | + final int numWidth; |
| 22 | + |
| 23 | + /// The value [a], named this way to allow for a local variable 'a'. |
| 24 | + @protected |
| 25 | + late final FixedPoint a; |
| 26 | + |
| 27 | + /// getter for the computed output. |
| 28 | + late final FixedPoint sqrtF = a.clone(name: 'sqrtF')..gets(output('sqrtF')); |
| 29 | + |
| 30 | + /// Square root a fixed point number [a], returning result in [sqrtF]. |
| 31 | + FixedPointSqrtBase(FixedPoint a, |
| 32 | + {super.name = 'fixed_point_square_root', String? definitionName}) |
| 33 | + : numWidth = a.width, |
| 34 | + super( |
| 35 | + definitionName: |
| 36 | + definitionName ?? 'FixedPointSquareRoot${a.width}') { |
| 37 | + this.a = a.clone(name: 'a')..gets(addInput('a', a, width: a.width)); |
| 38 | + |
| 39 | + addOutput('sqrtF', width: numWidth); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Implementation |
| 44 | +/// Algorithm explained here; |
| 45 | +/// https://projectf.io/posts/square-root-in-verilog/ |
| 46 | +class FixedPointSqrt extends FixedPointSqrtBase { |
| 47 | + /// Constructor |
| 48 | + FixedPointSqrt(super.a) { |
| 49 | + Logic solution = |
| 50 | + FixedPoint(signed: a.signed, name: 'solution', m: a.m + 1, n: a.n + 1); |
| 51 | + Logic remainder = |
| 52 | + FixedPoint(signed: a.signed, name: 'remainder', m: a.m + 1, n: a.n + 1); |
| 53 | + Logic subtractionValue = |
| 54 | + FixedPoint(signed: a.signed, name: 'subValue', m: a.m + 1, n: a.n + 1); |
| 55 | + Logic aLoc = |
| 56 | + FixedPoint(signed: a.signed, name: 'aLoc', m: a.m + 1, n: a.n + 1); |
| 57 | + |
| 58 | + solution = Const(0, width: aLoc.width); |
| 59 | + remainder = Const(0, width: aLoc.width); |
| 60 | + subtractionValue = Const(0, width: aLoc.width); |
| 61 | + aLoc = [Const(0), a, Const(0)].swizzle(); |
| 62 | + |
| 63 | + final outputSqrt = a.clone(name: 'sqrtF'); |
| 64 | + output('sqrtF') <= outputSqrt; |
| 65 | + |
| 66 | + // loop once through input value |
| 67 | + for (var i = 0; i < ((numWidth + 2) >> 1); i++) { |
| 68 | + // append bits from a, two at a time |
| 69 | + remainder = [ |
| 70 | + remainder.slice(numWidth + 2 - 3, 0), |
| 71 | + aLoc.slice(aLoc.width - 1 - (i * 2), aLoc.width - 2 - (i * 2)) |
| 72 | + ].swizzle(); |
| 73 | + subtractionValue = |
| 74 | + [solution.slice(numWidth + 2 - 3, 0), Const(1, width: 2)].swizzle(); |
| 75 | + solution = [ |
| 76 | + solution.slice(numWidth + 2 - 2, 0), |
| 77 | + subtractionValue.lte(remainder) |
| 78 | + ].swizzle(); |
| 79 | + remainder = mux(subtractionValue.lte(remainder), |
| 80 | + remainder - subtractionValue, remainder); |
| 81 | + } |
| 82 | + |
| 83 | + // loop again to finish remainder |
| 84 | + for (var i = 0; i < ((numWidth + 2) >> 1) - 1; i++) { |
| 85 | + // don't try to append bits from a, they are done |
| 86 | + remainder = |
| 87 | + [remainder.slice(numWidth + 2 - 3, 0), Const(0, width: 2)].swizzle(); |
| 88 | + subtractionValue = |
| 89 | + [solution.slice(numWidth + 2 - 3, 0), Const(1, width: 2)].swizzle(); |
| 90 | + solution = [ |
| 91 | + solution.slice(numWidth + 2 - 2, 0), |
| 92 | + subtractionValue.lte(remainder) |
| 93 | + ].swizzle(); |
| 94 | + remainder = mux(subtractionValue.lte(remainder), |
| 95 | + remainder - subtractionValue, remainder); |
| 96 | + } |
| 97 | + solution = solution + 1; |
| 98 | + outputSqrt <= solution.slice(aLoc.width - 1, aLoc.width - a.width); |
| 99 | + } |
| 100 | +} |
0 commit comments