Skip to content

Commit

Permalink
cleanup comments, fix FP8 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
desmonddak committed Oct 21, 2024
1 parent 963f235 commit 0a16197
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:meta/meta.dart';
import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';

/// A representation of a single precision floating point value
/// A representation of a single-precision floating-point value.
class FloatingPoint32Value extends FloatingPointValue {
/// The exponent width
static const int exponentWidth = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'dart:typed_data';
import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';

/// A representation of a double precision floating point value
/// A representation of a double-precision floating-point value.
class FloatingPoint64Value extends FloatingPointValue {
/// The exponent width
static const int _exponentWidth = 11;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class FloatingPoint8E4M3Value extends FloatingPointValue {

/// Numeric conversion of a [FloatingPoint8E4M3Value] from a host double
factory FloatingPoint8E4M3Value.fromDouble(double inDouble) {
if ((inDouble > maxValue) | (inDouble < minValue)) {
if ((inDouble.abs() > maxValue) |
((inDouble != 0) & (inDouble.abs() < minValue))) {
throw RohdHclException('Number exceeds E4M3 range');
}
final fpv = FloatingPointValue.fromDouble(inDouble,
Expand Down Expand Up @@ -155,7 +156,8 @@ class FloatingPoint8E5M2Value extends FloatingPointValue {

/// Numeric conversion of a [FloatingPoint8E5M2Value] from a host double
factory FloatingPoint8E5M2Value.fromDouble(double inDouble) {
if ((inDouble > maxValue) | (inDouble < minValue)) {
if ((inDouble.abs() > maxValue) |
((inDouble != 0) & (inDouble.abs() < minValue))) {
throw RohdHclException('Number exceeds E5M2 range');
}
final fpv = FloatingPointValue.fromDouble(inDouble,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:meta/meta.dart';
import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';

/// A representation of a single precision floating point value
/// A representation of a BF16 floating-point value.
class FloatingPointBF16Value extends FloatingPointValue {
/// The exponent width
static const int exponentWidth = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:meta/meta.dart';
import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';

/// A representation of a single precision floating point value
/// A representation of an FP16 floating-point value.
class FloatingPointFP16Value extends FloatingPointValue {
/// The exponent width
static const int exponentWidth = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:meta/meta.dart';
import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';

/// A representation of a single precision floating point value
/// A representation of a TF32 floating-point value.
class FloatingPointTF32Value extends FloatingPointValue {
/// The exponent width
static const int exponentWidth = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class FloatingPointValue implements Comparable<FloatingPointValue> {
final LogicValue value;

/// The sign of the value: 1 means a negative value
final LogicValue sign;
late final LogicValue sign;

/// The exponent of the floating point: this is biased about a midpoint for
/// positive and negative exponents
Expand Down Expand Up @@ -243,7 +243,7 @@ class FloatingPointValue implements Comparable<FloatingPointValue> {
);
}

// TODO(desmonddak): make a toString (with radix)
// TODO(desmonddak): toRadixString() would be useful, not limited to binary

/// [FloatingPointValue] constructor from a set of [BigInt]s of the binary
/// representation and the size of the exponent and mantissa
Expand All @@ -264,13 +264,14 @@ class FloatingPointValue implements Comparable<FloatingPointValue> {
mantissa:
LogicValue.ofBigInt(BigInt.from(mantissa), mantissaWidth));

/// Construct a [FloatingPointValue] from a Logic word
factory FloatingPointValue.fromLogic(
int exponentWidth, int mantissaWidth, LogicValue val) =>
FloatingPointValue(
sign: val[-1],
exponent: val.slice(exponentWidth + mantissaWidth - 1, mantissaWidth),
mantissa: val.slice(mantissaWidth - 1, 0));
/// Construct a [FloatingPointValue] from a [LogicValue]
FloatingPointValue.fromLogicValue(
int exponentWidth, int mantissaWidth, LogicValue val)
: this(
sign: val[-1],
exponent:
val.slice(exponentWidth + mantissaWidth - 1, mantissaWidth),
mantissa: val.slice(mantissaWidth - 1, 0));

/// Return the [FloatingPointValue] representing the constant specified
factory FloatingPointValue.getFloatingPointConstant(
Expand Down
6 changes: 2 additions & 4 deletions test/arithmetic/floating_point/floating_point_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ void main() {
exponentWidth: 4, mantissaWidth: 3);
expect(val, fp.toDouble());
expect(str, fp.toString());
final fp8 = FloatingPointValue.fromDouble(val,
exponentWidth: 4, mantissaWidth: 3);
final fp8 = FloatingPoint8E4M3Value.fromDouble(val);
expect(val, fp8.toDouble());
expect(str, fp8.toString());
}
Expand All @@ -185,8 +184,7 @@ void main() {
exponentWidth: 5, mantissaWidth: 2);
expect(val, fp.toDouble());
expect(str, fp.toString());
final fp8 = FloatingPointValue.fromDouble(val,
exponentWidth: 5, mantissaWidth: 2);
final fp8 = FloatingPoint8E5M2Value.fromDouble(val);
expect(val, fp8.toDouble());
expect(str, fp8.toString());
}
Expand Down

0 comments on commit 0a16197

Please sign in to comment.