Skip to content

Commit

Permalink
full set of sign variants (signed/unsigned/selected) for operands of …
Browse files Browse the repository at this point in the history
…integer multiplication
  • Loading branch information
ganewto committed Dec 3, 2024
1 parent 3530022 commit ee51e5a
Show file tree
Hide file tree
Showing 15 changed files with 1,170 additions and 801 deletions.
4 changes: 2 additions & 2 deletions lib/src/arithmetic/carry_save_mutiplier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CarrySaveMultiplier extends Multiplier {
CarrySaveMultiplier(super.a, super.b,
{required Logic clk,
required Logic reset,
required super.signed,
super.name = 'carry_save_multiplier'}) {
super.name = 'carry_save_multiplier'})
: super(signedMultiplicand: false, signedMultiplier: false) {
if (a.width != b.width) {
throw RohdHclException('inputs of a and b should have same width.');
}
Expand Down
6 changes: 2 additions & 4 deletions lib/src/arithmetic/evaluate_partial_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ extension EvaluateLivePartialProduct on PartialProductGenerator {
}
}
final sum = LogicValue.ofBigInt(accum, maxW).toBigInt();
return signed
return isSignedMultiplicand() | isSignedMultiplier()
? sum.toSigned(maxW)
: (selectSigned != null && !selectSigned!.value.isZero)
? sum.toSigned(maxW)
: sum;
: sum;
}

/// Print out the partial product matrix
Expand Down
27 changes: 18 additions & 9 deletions lib/src/arithmetic/multiplicand_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:rohd_hcl/rohd_hcl.dart';

/// A class accessing the multiples of the multiplicand at a position
class MultiplicandSelector {
/// radix of the selector
/// The radix of the selector
int radix;

/// The bit shift of the selector (typically overlaps 1)
Expand All @@ -21,17 +21,25 @@ class MultiplicandSelector {
/// New width of partial products generated from the multiplicand
int get width => multiplicand.width + shift - 1;

/// Access the multiplicand
/// The base multiplicand from which to generate multiples to select.
Logic multiplicand = Logic();

/// Place to store multiples of the multiplicand
/// Place to store [multiples] of the [multiplicand] (e.g. *1, *2, *-1, *-2..)
late LogicArray multiples;

/// Generate required multiples of multiplicand
/// Build a [MultiplicandSelector] generationg required [multiples] of
/// [multiplicand] to [select] using a [RadixEncoder] argument.
///
/// [multiplicand] is base multiplicand multiplied by Booth encodings of
/// the [RadixEncoder] during [select].
///
/// [signedMultiplicand] generates a fixed signed selector versus using
/// [selectSignedMultiplicand] which is a runtime sign selection [Logic]
/// in which case [signedMultiplicand] must be false.
MultiplicandSelector(this.radix, this.multiplicand,
{Logic? selectSigned, bool signed = false})
{Logic? selectSignedMultiplicand, bool signedMultiplicand = false})
: shift = log2Ceil(radix) {
if (signed && (selectSigned != null)) {
if (signedMultiplicand && (selectSignedMultiplicand != null)) {
throw RohdHclException('sign reconfiguration requires signed=false');
}
if (radix > 16) {
Expand All @@ -41,15 +49,16 @@ class MultiplicandSelector {
final numMultiples = radix ~/ 2;
multiples = LogicArray([numMultiples], width);
final Logic extendedMultiplicand;
if (selectSigned == null) {
extendedMultiplicand = signed
if (selectSignedMultiplicand == null) {
extendedMultiplicand = signedMultiplicand
? multiplicand.signExtend(width)
: multiplicand.zeroExtend(width);
} else {
final len = multiplicand.width;
final sign = multiplicand[len - 1];
final extension = [
for (var i = len; i < width; i++) mux(selectSigned, sign, Const(0))
for (var i = len; i < width; i++)
mux(selectSignedMultiplicand, sign, Const(0))
];
extendedMultiplicand = (multiplicand.elements + extension).rswizzle();
}
Expand Down
Loading

0 comments on commit ee51e5a

Please sign in to comment.