Skip to content

Commit 0216f02

Browse files
sweeksBigBluelucasphillips
authored andcommitted
More code review changes
Adding docs that pipelining is for future plumbing work Fixing sqrt output to be more sensical Removing magic number from random number test Fixing error signal for floating point sqrt logic Alligning names for floating_point_sqrt.dart Catching and properly reporting error on DeNorm Updating docs to mention we do not support DeNorms fixing fixed sqrt test
1 parent 6e190bf commit 0216f02

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

doc/components/fixed_point.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ This component converts an 8-bit floating-point (FP8) representation ([FloatingP
2828

2929
## FixedPointSqrt
3030

31-
This component computes the square root of a 3.x fixed-point value, returning a result in the same format. The square root value is rounded to the ordered number of bits. The integral part must be 3 bits, and the fractional part may be any odd value <= 51. Even numbers of bits are currently not supported, integral bits in numbers other than 3 are currently not supported.
31+
This component computes the square root of a 3.x fixed-point value, returning a result in the same format. The square root value is rounded to the ordered number of bits. The integral part must be 3 bits, and the fractional part may be any odd value <= 51. Even numbers of bits are currently not supported, integral bits in numbers other than 3 are currently not supported.

doc/components/floating_point.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ A second [FloatingPointAdderRound](https://intel.github.io/rohd-hcl/rohd_hcl/Flo
8686
## FloatingPointSqrt
8787

8888
A very basic [FloatingPointSqrtSimple] component is available which does not perform any
89-
rounding. It also only operates on variable mantissas of an odd value (1,3,5,etc) but these odd mantissas can be of variable length up to 51. It takes one
89+
rounding and does not support DeNorm numbers. It also only operates on variable mantissas of an odd value (1,3,5,etc) but these odd mantissas can be of variable length up to 51. It takes one
9090
[FloatingPoint](https://intel.github.io/rohd-hcl/rohd_hcl/FloatingPoint-class.html) [LogicStructure](https://intel.github.io/rohd/rohd/LogicStructure-class.html) and
9191
performs a square root on it, returning the [FloatingPoint](https://intel.github.io/rohd-hcl/rohd_hcl/FloatingPoint-class.html) value on the output.
9292

lib/src/arithmetic/fixed_sqrt.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ abstract class FixedPointSqrtBase extends Module {
2323
late final FixedPoint a;
2424

2525
/// getter for the computed output.
26-
late final FixedPoint sqrtF = a.clone(name: 'sqrtF')..gets(output('sqrtF'));
26+
late final FixedPoint sqrt = a.clone(name: 'sqrt')..gets(output('sqrt'));
2727

28-
/// Square root a fixed point number [a], returning result in [sqrtF].
28+
/// Square root a fixed point number [a], returning result in [sqrt].
2929
FixedPointSqrtBase(FixedPoint a,
3030
{super.name = 'fixed_point_square_root', String? definitionName})
3131
: width = a.width,
@@ -34,7 +34,7 @@ abstract class FixedPointSqrtBase extends Module {
3434
definitionName ?? 'FixedPointSquareRoot${a.width}') {
3535
this.a = a.clone(name: 'a')..gets(addInput('a', a, width: a.width));
3636

37-
addOutput('sqrtF', width: width);
37+
addOutput('sqrt', width: width);
3838
}
3939
}
4040

@@ -62,8 +62,8 @@ class FixedPointSqrt extends FixedPointSqrtBase {
6262
subtractionValue = Const(0, width: aLoc.width);
6363
aLoc = [Const(0), a, Const(0)].swizzle();
6464

65-
final outputSqrt = a.clone(name: 'sqrtF');
66-
output('sqrtF') <= outputSqrt;
65+
final outputSqrt = a.clone(name: 'sqrt');
66+
output('sqrt') <= outputSqrt;
6767

6868
// loop once through input value
6969
for (var i = 0; i < ((width + 2) >> 1); i++) {

lib/src/arithmetic/floating_point/floating_point_sqrt.dart

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
// 2025 March 3
88
// Authors: James Farwell <james.c.farwell@intel.com>,
9-
//Stephen Weeks <stephen.weeks@intel.com>,
10-
//Curtis Anderson <curtis.anders@intel.com>
9+
// Stephen Weeks <stephen.weeks@intel.com>,
10+
// Curtis Anderson <curtis.anders@intel.com>
1111

1212
import 'package:meta/meta.dart';
1313
import 'package:rohd/rohd.dart';
@@ -23,16 +23,19 @@ abstract class FloatingPointSqrt<FpType extends FloatingPoint> extends Module {
2323

2424
/// The [clk] : if a non-null clock signal is passed in, a pipestage is added
2525
/// to the square root to help optimize frequency.
26+
/// Plummed for future work for pipelining, currently unsupported.
2627
@protected
2728
late final Logic? clk;
2829

2930
/// Optional [reset], used only if a [clk] is not null to reset the pipeline
3031
/// flops.
32+
/// Plummed for future work for pipelining, currently unsupported.
3133
@protected
3234
late final Logic? reset;
3335

3436
/// Optional [enable], used only if a [clk] is not null to enable the pipeline
3537
/// flops.
38+
/// Plummed for future work for pipelining, currently unsupported.
3639
@protected
3740
late final Logic? enable;
3841

@@ -41,16 +44,16 @@ abstract class FloatingPointSqrt<FpType extends FloatingPoint> extends Module {
4144
late final FpType a;
4245

4346
/// getter for the computed [FloatingPoint] output.
44-
late final FloatingPoint sqrtR = (a.clone(name: 'sqrtR') as FpType)
45-
..gets(output('sqrtR'));
47+
late final FloatingPoint sqrt = (a.clone(name: 'sqrt') as FpType)
48+
..gets(output('sqrt'));
4649

4750
/// getter for the [error] output.
4851
late final Logic error = Logic(name: 'error')..gets(output('error'));
4952

5053
/// The internal error signal to pass through
5154
late final Logic errorSig;
5255

53-
/// Square root a floating point number [a], returning result in [sqrtR].
56+
/// Square root a floating point number [a], returning result in [sqrt].
5457
/// - [clk], [reset], [enable] are optional inputs to control a pipestage
5558
/// (only inserted if [clk] is provided)
5659
FloatingPointSqrt(FpType a,
@@ -71,10 +74,8 @@ abstract class FloatingPointSqrt<FpType extends FloatingPoint> extends Module {
7174
this.a = (a.clone(name: 'a') as FpType)
7275
..gets(addInput('a', a, width: a.width));
7376

74-
addOutput('sqrtR', width: exponentWidth + mantissaWidth + 1);
75-
errorSig = Logic(name: 'error');
77+
addOutput('sqrt', width: exponentWidth + mantissaWidth + 1);
7678
addOutput('error');
77-
output('error') <= errorSig;
7879
}
7980

8081
/// Pipelining helper that uses the context for signals clk/enable/reset

lib/src/arithmetic/floating_point/floating_point_sqrt_simple.dart

+20-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'package:rohd_hcl/rohd_hcl.dart';
1616
class FloatingPointSqrtSimple<FpType extends FloatingPoint>
1717
extends FloatingPointSqrt<FpType> {
1818
/// Square root one floating point number [a], returning results
19-
/// [sqrtR] and [error]
19+
/// [sqrt] and [error]
2020
FloatingPointSqrtSimple(super.a,
2121
{super.clk,
2222
super.reset,
@@ -28,14 +28,17 @@ class FloatingPointSqrtSimple<FpType extends FloatingPoint>
2828
final outputSqrt = FloatingPoint(
2929
exponentWidth: exponentWidth,
3030
mantissaWidth: mantissaWidth,
31-
name: 'sqrtR');
32-
output('sqrtR') <= outputSqrt;
31+
name: 'sqrt');
32+
output('sqrt') <= outputSqrt;
33+
late final error = output('error');
3334

3435
// check to see if we do sqrt at all or just return a
3536
final isInf = a.isAnInfinity.named('isInf');
3637
final isNaN = a.isNaN.named('isNan');
3738
final isZero = a.isAZero.named('isZero');
38-
final enableSqrt = ~((isInf | isNaN | isZero) | a.sign).named('enableSqrt');
39+
final isDeNormal = ~a.isNormal.named('isDenorm');
40+
final enableSqrt =
41+
~((isInf | isNaN | isZero | isDeNormal) | a.sign).named('enableSqrt');
3942

4043
// debias the exponent
4144
final deBiasAmt = (1 << a.exponent.width - 1) - 1;
@@ -44,8 +47,9 @@ class FloatingPointSqrtSimple<FpType extends FloatingPoint>
4447
final deBiasExp = a.exponent - deBiasAmt;
4548

4649
// shift exponent
47-
final shiftedExp =
48-
[deBiasExp[-1], deBiasExp.slice(a.exponent.width - 1, 1)].swizzle();
50+
final shiftedExp = [deBiasExp[-1], deBiasExp.slice(a.exponent.width - 1, 1)]
51+
.swizzle()
52+
.named('deBiasExp');
4953

5054
// check if exponent was odd
5155
final isExpOdd = deBiasExp[0];
@@ -61,7 +65,7 @@ class FloatingPointSqrtSimple<FpType extends FloatingPoint>
6165

6266
// mux to choose if we do square root or not
6367
final fixedSqrt = aFixedAdj.clone()
64-
..gets(mux(enableSqrt, FixedPointSqrt(aFixedAdj).sqrtF, aFixedAdj)
68+
..gets(mux(enableSqrt, FixedPointSqrt(aFixedAdj).sqrt, aFixedAdj)
6569
.named('sqrtMux'));
6670

6771
// convert back to floating point representation
@@ -70,14 +74,14 @@ class FloatingPointSqrtSimple<FpType extends FloatingPoint>
7074

7175
// final calculation results
7276
Combinational([
73-
errorSig < Const(0),
77+
error < Const(0),
7478
If.block([
7579
Iff(isInf & ~a.sign, [
7680
outputSqrt < outputSqrt.inf(),
7781
]),
7882
ElseIf(isInf & a.sign, [
7983
outputSqrt < outputSqrt.inf(negative: true),
80-
errorSig < Const(1),
84+
error < Const(1),
8185
]),
8286
ElseIf(isNaN, [
8387
outputSqrt < outputSqrt.nan,
@@ -89,7 +93,13 @@ class FloatingPointSqrtSimple<FpType extends FloatingPoint>
8993
]),
9094
ElseIf(a.sign, [
9195
outputSqrt < outputSqrt.nan,
92-
errorSig < Const(1),
96+
error < Const(1),
97+
]),
98+
ElseIf(isDeNormal, [
99+
outputSqrt.sign < a.sign,
100+
outputSqrt.exponent < a.exponent,
101+
outputSqrt.mantissa < a.mantissa,
102+
error < Const(1),
93103
]),
94104
Else([
95105
outputSqrt.sign < a.sign,

test/arithmetic/fixed_sqrt_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void main() {
4545
fixed.put(FixedPointValue.ofDouble(test,
4646
signed: fixed.signed, m: fixed.m, n: fixed.n));
4747

48-
final fpvResult = dut.sqrtF.fixedPointValue;
48+
final fpvResult = dut.sqrt.fixedPointValue;
4949

5050
final fpvExpected = FixedPointValue.ofDouble(sqrt(test),
5151
signed: fixed.signed, m: fixed.m, n: fixed.n);

test/arithmetic/floating_point/floating_point_sqrt_test.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void main() {
5252
final Logic expError = Const(0);
5353

5454
fp.put(fv);
55-
final fpOut = sqrtT.sqrtR;
55+
final fpOut = sqrtT.sqrt;
5656
final eOut = sqrtT.error;
5757
expect(fpOut.floatingPointValue, equals(expSqrt),
5858
reason: '\t${fp.floatingPointValue} '
@@ -132,7 +132,7 @@ void main() {
132132

133133
fp.put(fv);
134134

135-
final compResult = sqrtDUT.sqrtR;
135+
final compResult = sqrtDUT.sqrt;
136136
final compError = sqrtDUT.error;
137137

138138
final expResult = FloatingPointValue.populator(
@@ -158,13 +158,14 @@ void main() {
158158
test('FP: random number sqrt', () {
159159
const exponentWidth = 5;
160160
const mantissaWidth = 9;
161+
final systemTestIter = pow(2, exponentWidth) * pow(2, mantissaWidth);
161162

162163
final fp = FloatingPoint(
163164
exponentWidth: exponentWidth, mantissaWidth: mantissaWidth);
164165

165166
final sqrtDUT = FloatingPointSqrtSimple(fp);
166167
final rand = Random(513);
167-
for (var i = 0; i < 4096; i++) {
168+
for (var i = 0; i < systemTestIter; i++) {
168169
final fv = FloatingPointValue.populator(
169170
exponentWidth: exponentWidth, mantissaWidth: mantissaWidth)
170171
.random(rand, normal: true);
@@ -176,7 +177,7 @@ void main() {
176177
fp.sign.value.toBool()) {
177178
continue;
178179
}
179-
final compResult = sqrtDUT.sqrtR;
180+
final compResult = sqrtDUT.sqrt;
180181
final compError = sqrtDUT.error;
181182

182183
final expResult = FloatingPointValue.populator(

0 commit comments

Comments
 (0)