Skip to content

Commit

Permalink
doc update, checkOverflow as property
Browse files Browse the repository at this point in the history
  • Loading branch information
desmonddak committed Feb 7, 2025
1 parent 3962f99 commit 45f18ae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions doc/components/fixed_point.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ The [FixedPoint](https://intel.github.io/rohd-hcl/rohd_hcl/FixedPoint-class.html

## FixedToFloat

This component converts a fixed-point signal to a floating point signal specified by exponent and mantissa width. The output is rounded to the nearest even (RNE) when applicable and set to infinity if the input exceed the representable range.
The [FixedToFloat](https://intel.github.io/rohd-hcl/rohd_hcl/FixedToFloat-class.html) component converts a fixed-point signal to a floating point signal specified by exponent and mantissa width. The output is rounded to the nearest even (RNE) when applicable and set to infinity if the input exceed the representable range.

## FloatToFixed

This component converts a floating-point signal to a signed fixed-point signal. Infinities and NaN's are not supported. The integer and fraction widths are auto-calculated to achieve lossless conversion.

If the `m` and `n` integer and fraction widths are supplied, then lossy conversion is performed to fit the floating-point value into the fixed-point value. For testing, [FixedPointValue] has a `canStore` method to predetermine if a given double can fit. For execution, [FloatToFixed] can perform overflow detection by setting a `checkOverflow` option.
If the `m` and `n` integer and fraction widths are supplied, then lossy conversion is performed to fit the floating-point value into the fixed-point value. For testing, [FixedPointValue](https://intel.github.io/rohd-hcl/rohd_hcl/FixedPointValue-class.html) has a `canStore` method to predetermine if a given double can fit. For execution, [FloatToFixed](https://intel.github.io/rohd-hcl/rohd_hcl/FloatToFixed-class.html) can perform overflow detection by setting a `checkOverflow` option, which is a property of the class and set in the constructor (default is false as it must add significant logic to do the check).

## Float8ToFixed

Expand Down
5 changes: 4 additions & 1 deletion lib/src/arithmetic/float_to_fixed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FloatToFixed extends Module {
/// Width of output fractional part.
late final int n;

/// Add overflow checking logic
final bool checkOverflow;

/// Return true if the conversion overflowed.
Logic? get overflow => tryOutput('overflow');

Expand All @@ -43,7 +46,7 @@ class FloatToFixed extends Module {
/// case that loss can occur and an optional output [overflow] will be
/// produced that returns true when overflow occurs.
FloatToFixed(FloatingPoint float,
{super.name = 'FloatToFixed', int? m, int? n, bool checkOverflow = false})
{super.name = 'FloatToFixed', int? m, int? n, this.checkOverflow = false})
: super(
definitionName: 'FloatE${float.exponent.width}'
'M${float.mantissa.width}ToFixed') {
Expand Down
9 changes: 4 additions & 5 deletions lib/src/arithmetic/values/fixed_point_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,12 @@ class FixedPointValue implements Comparable<FixedPointValue> {
/// Return a string representation of FloatingPointValue.
/// return sign, exponent, mantissa as binary strings.
@override
String toString() =>
"(${signed ? '${value[-1].toString(includeWidth: false)} ' : ''}"
String toString() => "(${signed ? '${value[-1].bitString} ' : ''}"
"${(m > 0) ? '${value.slice(m + n - 1, n).bitString} ' : ''}"
'${value.slice(n - 1, 0).toString(includeWidth: false)})';
'${value.slice(n - 1, 0).bitString})';

/// Return true if double [val] be stored in FixedPointValue with [m] and [n]
/// lengths.
/// Return true if double [val] to be stored in [FixedPointValue]
/// with [m] and [n] lengths without overflowing.
static bool canStore(double val,
{required bool signed, required int m, required int n}) {
final w = signed ? 1 + m + n : m + n;
Expand Down

0 comments on commit 45f18ae

Please sign in to comment.