-
Notifications
You must be signed in to change notification settings - Fork 33
FixedPoint is now a LogicStructure with a Populator #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9f4c66e
refactor fixed-point to use LogicStructure and populator methodology
desmonddak 00e03eb
remove unused code
desmonddak 84f6aa5
update doc
desmonddak cf4e657
watch out for commented-out code
desmonddak 8407586
renaming fixed point fields
desmonddak cea2ede
better names for FixedPointValue fields
desmonddak 9bee223
naming fpv as fxv for clarity
desmonddak 0eb4eff
cleanup structure naming and old m,n variable names in fixed point
desmonddak d26290b
more naming changes for fixed
desmonddak f62d725
renaming fixed point fields in configurator
desmonddak a5a5093
resolve to flutter 3.27.0 for best confapp display
desmonddak 40c6f77
resolve to flutter 3.27.0 for best confapp display
desmonddak 75382a2
fix opacity complaints
desmonddak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,61 +7,92 @@ | |
| // 2024 October 24 | ||
| // Author: Soner Yaldiz <[email protected]> | ||
|
|
||
| import 'dart:math'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:rohd/rohd.dart'; | ||
| import 'package:rohd_hcl/rohd_hcl.dart'; | ||
|
|
||
| /// A representation of (un)signed fixed-point logic following | ||
| /// Q notation (Qm.n format) as introduced by | ||
| /// (Texas Instruments)[https://www.ti.com/lit/ug/spru565b/spru565b.pdf]. | ||
| class FixedPoint extends Logic { | ||
| /// [signed] indicates whether the representation is signed. | ||
| class FixedPoint extends LogicStructure { | ||
| /// The integer part of the fixed-point number. | ||
| Logic integer; | ||
|
|
||
| /// The fractional part of the fixed-point number. | ||
| Logic fraction; | ||
|
|
||
| /// Return true if signed | ||
| final bool signed; | ||
|
|
||
| /// [m] is the number of bits reserved for the integer part. | ||
| final int m; | ||
| /// [integerWidth] is the number of bits reserved for the integer part. | ||
| int get integerWidth => integer.width - (signed ? 1 : 0); | ||
|
|
||
| /// [n] is the number of bits reserved for the fractional part. | ||
| final int n; | ||
| /// [fractionWidth] is the number of bits reserved for the fractional part. | ||
| int get fractionWidth => fraction.width; | ||
|
|
||
| static int _fixedPointWidth(bool s, int a, int b) => s ? 1 + a + b : a + b; | ||
| /// Utility to keep track of the Logic structure name by attaching it | ||
| /// to the Logic signal name in the output Verilog. | ||
| static String _nameJoin(String? structName, String signalName) { | ||
| if (structName == null) { | ||
| return signalName; | ||
| } | ||
| return '${structName}_$signalName'; | ||
| } | ||
|
|
||
| /// Constructs a [FixedPoint] signal. | ||
| FixedPoint( | ||
| {required this.signed, | ||
| required this.m, | ||
| required this.n, | ||
| super.name, | ||
| super.naming}) | ||
| : super(width: _fixedPointWidth(signed, m, n)) { | ||
| if ((m < 0) | (n < 0)) { | ||
| throw RohdHclException('m and n must be non-negative'); | ||
| } | ||
| if (max(m, n) == 0) { | ||
| throw RohdHclException('either m or n must be greater than zero'); | ||
| } | ||
| } | ||
| {required int mWidth, | ||
| required int nWidth, | ||
| bool signed = true, | ||
| String? name}) | ||
| : this._( | ||
| Logic( | ||
| width: mWidth + (signed ? 1 : 0), | ||
| name: _nameJoin(name, 'integer'), | ||
desmonddak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| naming: Naming.mergeable), | ||
| Logic( | ||
| width: nWidth, | ||
| name: _nameJoin(name, 'fraction'), | ||
| naming: Naming.mergeable), | ||
| signed, | ||
| name: name); | ||
|
|
||
| /// [FixedPoint] internal constructor. | ||
| FixedPoint._(this.integer, this.fraction, this.signed, {super.name}) | ||
| : super([fraction, integer]); | ||
|
|
||
| /// Retrieve the [FixedPointValue] of this [FixedPoint] logical signal. | ||
| FixedPointValue get fixedPointValue => | ||
| FixedPointValue(value: value, signed: signed, m: m, n: n); | ||
| FixedPointValue get fixedPointValue => valuePopulator().ofFixedPoint(this); | ||
|
|
||
| /// A [FixedPointValuePopulator] for values associated with this | ||
| /// [FloatingPoint] type. | ||
| @mustBeOverridden | ||
| FixedPointValuePopulator valuePopulator() => FixedPointValue.populator( | ||
| mWidth: integerWidth, nWidth: fractionWidth, signed: signed); | ||
|
|
||
| /// Clone for I/O ports. | ||
| @override | ||
| FixedPoint clone({String? name}) => FixedPoint(signed: signed, m: m, n: n); | ||
| FixedPoint clone({String? name}) => | ||
| FixedPoint(signed: signed, mWidth: integerWidth, nWidth: fractionWidth); | ||
|
|
||
| /// Cast logic to fixed point | ||
| FixedPoint.of(Logic signal, | ||
| {required this.signed, required this.m, required this.n}) | ||
| : super(width: _fixedPointWidth(signed, m, n)) { | ||
| this <= signal; | ||
| } | ||
| {required int mWidth, required int nWidth, bool signed = true}) | ||
| : this._(signal.slice(nWidth + (signed ? mWidth : mWidth - 1), nWidth), | ||
| signal.slice(nWidth - 1, 0), signed, | ||
| name: signal.name); | ||
|
|
||
| @override | ||
| void put(dynamic val, {bool fill = false}) { | ||
| if (val is FixedPointValue) { | ||
| if ((signed != val.signed) | (m != val.m) | (n != val.n)) { | ||
| throw RohdHclException('Value is not compatible with signal.'); | ||
| if ((signed != val.signed) | | ||
| (integerWidth != val.integerWidth) | | ||
| (fractionWidth != val.fractionWidth)) { | ||
| throw RohdHclException('Value is not compatible with signal. ' | ||
| 'Expected: signed=$signed, mWidth=$integerWidth, ' | ||
| 'nWidth=$fractionWidth. ' | ||
| 'Got: signed=${val.signed}, mWidth=${val.integerWidth}, ' | ||
| 'nWidth=${val.fractionWidth}.'); | ||
| } | ||
| super.put(val.value); | ||
| } else { | ||
|
|
@@ -74,7 +105,9 @@ class FixedPoint extends Logic { | |
| if (other is! FixedPoint) { | ||
| throw RohdHclException('Input must be fixed point signal.'); | ||
| } | ||
| if ((signed != other.signed) | (m != other.m) | (n != other.n)) { | ||
| if ((signed != other.signed) | | ||
| (integerWidth != other.integerWidth) | | ||
| (fractionWidth != other.fractionWidth)) { | ||
| throw RohdHclException('Inputs are not comparable.'); | ||
| } | ||
| } | ||
|
|
@@ -127,7 +160,8 @@ class FixedPoint extends Logic { | |
| Logic _multiply(dynamic other) { | ||
| _verifyCompatible(other); | ||
| final product = Multiply(this, other).out; | ||
| return FixedPoint.of(product, signed: false, m: 2 * m, n: 2 * n); | ||
| return FixedPoint.of(product, | ||
| signed: false, mWidth: 2 * integerWidth, nWidth: 2 * fractionWidth); | ||
| } | ||
|
|
||
| /// Greater-than. | ||
|
|
@@ -138,7 +172,7 @@ class FixedPoint extends Logic { | |
| @override | ||
| Logic operator >=(dynamic other) => gte(other); | ||
|
|
||
| /// multiply | ||
| /// multiply: TODO(desmonddak): this needs tests | ||
| @override | ||
| Logic operator *(dynamic other) => _multiply(other); | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.