Skip to content

Commit

Permalink
cleanup of API making more methods private, fixing SignMag adder API
Browse files Browse the repository at this point in the history
  • Loading branch information
desmonddak committed Aug 13, 2024
1 parent d61c8b0 commit a26a709
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 32 deletions.
6 changes: 1 addition & 5 deletions lib/src/arithmetic/adder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ abstract class Adder extends Module {
/// The addition results [sum] including carry bit
Logic get sum => output('sum');

/// Implementation needs to provide a method for calculating the full sum
@protected
Logic calculateSum();

/// Implementation needs to provide a method for calculating the sum
/// without carry
@protected
Expand All @@ -57,7 +53,7 @@ abstract class Adder extends Module {

out <= calculateOut();
carryOut <= calculateCarry();
sum <= calculateSum();
sum <= [carryOut, out].swizzle();
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/src/arithmetic/multiplier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class MultiplyAccumulate extends Module {
/// Take input [a] and input [b], compute their
/// product, add input [c] to produce the [accumulate] result.
MultiplyAccumulate(Logic a, Logic b, Logic c,
{this.signed = false, super.name}) {
{required this.signed, super.name}) {
this.a = addInput('a', a, width: a.width);
this.b = addInput('b', b, width: b.width);
this.c = addInput('c', c, width: c.width);
Expand Down Expand Up @@ -103,7 +103,7 @@ class CompressionTreeMultiplyAccumulate extends MultiplyAccumulate {
/// a given radix and final adder functor
CompressionTreeMultiplyAccumulate(super.a, super.b, super.c, int radix,
ParallelPrefix Function(List<Logic>, Logic Function(Logic, Logic)) ppTree,
{super.signed = false})
{required super.signed})
: super(
name: 'Compression Tree Multiply Accumulate: '
'R${radix}_${ppTree.call([Logic()], (a, b) => Logic()).name}') {
Expand Down Expand Up @@ -154,7 +154,8 @@ class MutiplyOnly extends MultiplyAccumulate {
/// Construct a MultiplyAccumulate that only multiplies to enable
/// using the same tester with zero addend.
MutiplyOnly(super.a, super.b, super.c,
Multiplier Function(Logic a, Logic b) multiplyGenerator)
Multiplier Function(Logic a, Logic b) multiplyGenerator,
{super.signed = false}) // Will be overrwridden by multiplyGenerator
: super(name: 'Multiply Only: ${multiplyGenerator.call(a, b).name}') {
final accumulate = addOutput('accumulate', width: a.width + b.width + 1);

Expand Down
4 changes: 0 additions & 4 deletions lib/src/arithmetic/parallel_prefix_operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ class ParallelPrefixAdder extends Adder {
@override
@protected
Logic calculateCarry() => _carry;

@override
@protected
Logic calculateSum() => [_carry, _out].swizzle();
}

/// Incrementer based on ParallelPrefix tree
Expand Down
23 changes: 13 additions & 10 deletions lib/src/arithmetic/partial_product_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ class PartialProductGenerator {
case SignExtension.none:
;
case SignExtension.brute:
bruteForceSignExtend();
_bruteForceSignExtend();
case SignExtension.stop:
signExtendWithStopBitsRect();
_signExtendWithStopBitsRect();
case SignExtension.compact:
signExtendCompact();
_signExtendCompact();
case SignExtension.compactRect:
signExtendCompactRect();
_signExtendCompactRect();
}
}

Expand All @@ -114,7 +114,7 @@ class PartialProductGenerator {
}

/// Fully sign extend the PP array: useful for reference only
void bruteForceSignExtend() {
void _bruteForceSignExtend() {
if (_signExtended) {
throw RohdHclException('Partial Product array already sign-extended');
}
Expand Down Expand Up @@ -142,7 +142,7 @@ class PartialProductGenerator {
/// enough that carry bit lands outside another row).
/// This technique can then be combined with a first-row extension technique
/// for folding in the final carry.
void signExtendWithStopBitsRect() {
void _signExtendWithStopBitsRect() {
if (_signExtended) {
throw RohdHclException('Partial Product array already sign-extended');
}
Expand Down Expand Up @@ -219,7 +219,7 @@ class PartialProductGenerator {
}

/// Sign extend the PP array using stop bits without adding a row.
void signExtendCompact() {
void _signExtendCompact() {
// An implementation of
// Mohanty, B.K., Choubey, A. Efficient Design for Radix-8 Booth Multiplier
// and Its Application in Lifting 2-D DWT. Circuits Syst Signal Process 36,
Expand Down Expand Up @@ -323,7 +323,7 @@ class PartialProductGenerator {
/// This routine works with different widths of multiplicand/multiplier,
/// an extension of Mohanty, B.K., Choubey designed by
/// Desmond A. Kirkpatrick
void signExtendCompactRect() {
void _signExtendCompactRect() {
if (_signExtended) {
throw RohdHclException('Partial Product array already sign-extended');
}
Expand Down Expand Up @@ -462,7 +462,11 @@ class PartialProductGenerator {
}
return maxW;
}
}

/// Debug routines for printing out partial product matrix during
/// simulation with live logic values
extension EvaluateLivePartialProduct on PartialProductGenerator {
/// Accumulate the partial products and return as BigInt
BigInt evaluate() {
final maxW = maxWidth();
Expand All @@ -479,8 +483,7 @@ class PartialProductGenerator {
}

/// Print out the partial product matrix
@override
String toString() {
String representation() {
final str = StringBuffer();

final maxW = maxWidth();
Expand Down
4 changes: 0 additions & 4 deletions lib/src/arithmetic/ripple_carry_adder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ class RippleCarryAdder extends Adder {
@override
@protected
Logic calculateCarry() => _carry;

@override
@protected
Logic calculateSum() => [_carry, _out].swizzle();
}
16 changes: 10 additions & 6 deletions lib/src/arithmetic/sign_magnitude_adder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ class SignMagnitudeAdder extends Adder {
/// The sign of the result
Logic get sign => output('sign');

/// If largest magnitude negative is first argument, no compare and
/// swap is needed.
bool largestNegativeFirst;

late final Logic _out;
late final Logic _carry = Logic();

/// [SignMagnitudeAdder] constructor with an unsigned adder functor
SignMagnitudeAdder(Logic as, super.a, Logic bs, super.b,
Adder Function(Logic, Logic) adderGen)
Adder Function(Logic, Logic) adderGen,
{this.largestNegativeFirst = false})
: _out = Logic(width: a.width),
super(
name: 'Ones Complement Adder: '
Expand All @@ -42,14 +47,17 @@ class SignMagnitudeAdder extends Adder {
bSign = addInput('bSign', bs);
final sign = addOutput('sign');

final computeSign = mux(largestNegativeFirst ? Const(1) : Const(0), aSign,
mux(a.lt(b) & aSign, bSign, aSign));

final aOnesComplement = mux(aSign, ~a, a);
final bOnesComplement = mux(bSign, ~b, b);

final adder = adderGen(aOnesComplement, bOnesComplement);
final endAround = adder.carryOut & (aSign | bSign);
final localOut = mux(endAround, adder.sum + 1, adder.sum);

sign <= aSign;
sign <= computeSign;
_out <= mux(sign, ~localOut, localOut).slice(_out.width - 1, 0);
_carry <= localOut.slice(localOut.width - 1, localOut.width - 1);
}
Expand All @@ -61,8 +69,4 @@ class SignMagnitudeAdder extends Adder {
@override
@protected
Logic calculateCarry() => _carry;

@override
@protected
Logic calculateSum() => [_carry, _out].swizzle();
}

0 comments on commit a26a709

Please sign in to comment.