Skip to content

Commit

Permalink
Merge branch 'fpfixes' of https://github.com/desmonddak/rohd-hcl into…
Browse files Browse the repository at this point in the history
… desmonddak-fpfixes
  • Loading branch information
desmonddak committed Nov 12, 2024
2 parents de30796 + a7b91c1 commit 633ecbb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/arithmetic/multiplier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class CompressionTreeMultiplier extends Multiplier {
ppTree = KoggeStone.new,
super.signed = false,
super.name = 'compression_tree_multiplier'}) {
if (selectSigned != null) {
selectSigned = addInput('selectSigned', selectSigned);
}

final product = addOutput('product', width: a.width + b.width);
final pp = PartialProductGeneratorCompactRectSignExtension(
a, b, RadixEncoder(radix),
Expand Down Expand Up @@ -126,6 +130,9 @@ class CompressionTreeMultiplyAccumulate extends MultiplyAccumulate {
ParallelPrefix Function(List<Logic>, Logic Function(Logic, Logic))
ppTree = KoggeStone.new,
super.name = 'compression_tree_mac'}) {
if (selectSigned != null) {
selectSigned = addInput('selectSigned', selectSigned);
}
final accumulate = addOutput('accumulate', width: a.width + b.width + 1);
final pp = PartialProductGeneratorCompactRectSignExtension(
a, b, RadixEncoder(radix),
Expand Down
76 changes: 76 additions & 0 deletions test/arithmetic/multiplier_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ import 'package:rohd_hcl/src/arithmetic/evaluate_compressor.dart';
import 'package:rohd_hcl/src/arithmetic/partial_product_sign_extend.dart';
import 'package:test/test.dart';

/// Simple multiplier to demonstrate instantiation of CompressionTreeMultiplier
class SimpleMultiplier extends Module {
/// The output of the simple multiplier
late final Logic product;

/// Construct a simple multiplier with runtime sign operation
SimpleMultiplier(Logic a, Logic b, Logic multASigned)
: super(name: 'my_test_module') {
a = addInput('a', a, width: a.width);
b = addInput('b', b, width: b.width);
multASigned = addInput('multASigned', multASigned);
product = addOutput('product', width: a.width + b.width);

final mult = CompressionTreeMultiplier(a, b, 4, selectSigned: multASigned);
product <= mult.product;
}
}

// Inner test of a multipy accumulate unit
void checkMultiplyAccumulate(
MultiplyAccumulate mod, BigInt bA, BigInt bB, BigInt bC) {
Expand Down Expand Up @@ -175,6 +193,64 @@ void main() {
}
});

test('single multiplier', () async {
const width = 8;
final a = Logic(name: 'a', width: width);
final b = Logic(name: 'b', width: width);
const av = 12;
const bv = 13;
for (final signed in [true, false]) {
for (final useSignedLogic in [true, false]) {
final bA = signed
? BigInt.from(av).toSigned(width)
: BigInt.from(av).toUnsigned(width);
final bB = signed
? BigInt.from(bv).toSigned(width)
: BigInt.from(bv).toUnsigned(width);

final Logic? signedSelect;

if (useSignedLogic) {
signedSelect = Logic()..put(signed ? 1 : 0);
} else {
signedSelect = null;
}

// Set these so that printing inside module build will have Logic values
a.put(bA);
b.put(bB);

final mod = CompressionTreeMultiplier(a, b, 4,
signed: !useSignedLogic && signed, selectSigned: signedSelect);
await mod.build();
mod.generateSynth();
final golden = bA * bB;
final result = mod.signed
? mod.product.value.toBigInt().toSigned(mod.product.width)
: mod.product.value.toBigInt().toUnsigned(mod.product.width);
expect(result, equals(golden));
}
}
});

test('trivial instnatiated multiplier', () async {
const dataWidth = 8;
const av = 12;
const bv = 13;

final multA = Logic(name: 'multA', width: dataWidth);
final multB = Logic(name: 'multB', width: dataWidth);
final signedOperands = Logic(name: 'signedOperands');

multA.put(av);
multB.put(bv);

final mult = SimpleMultiplier(multA, multB, signedOperands);

await mult.build();
mult.generateSynth();
});

test('single mac', () async {
const width = 8;
final a = Logic(name: 'a', width: width);
Expand Down

0 comments on commit 633ecbb

Please sign in to comment.