Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random test now available for the adder. Fails with a stack overflow … #6

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/sklansky.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,34 @@ class Adder extends Module {
a = addInput('a', a, width: a.width);
b = addInput('b', b, width: b.width);
final u = ppGen(
List<Logic>.generate(a.width, (i) => [a[i] & b[i], a[i] ^ b[i]].swizzle() ),
// generate, propagate or generate
List<Logic>.generate(a.width, (i) => [a[i] & b[i], a[i] | b[i]].swizzle() ),
(lhs, rhs) => [rhs[1] | rhs[0] & lhs[1], rhs[0] & lhs[0]].swizzle()
);
addOutput('out', width: a.width) <= List<Logic>.generate(a.width, (i) => (i==0) ? a[i] ^ b[i] : a[i]^b[i]^u.val[i-1][1]).rswizzle();
}
}

class Incr extends Module {
Logic get out => output('out');
Incr(Logic inp, ParallelPrefix Function(List<Logic>, Logic Function(Logic, Logic )) ppGen) {
inp = addInput('inp', inp, width: inp.width);
final u = ppGen(
List<Logic>.generate(inp.width, (i) => inp[i] ),
(lhs, rhs) => rhs & lhs
);
addOutput('out', width: inp.width) <= (List<Logic>.generate(inp.width, (i) => ((i==0) ? ~inp[i] : inp[i]^u.val[i-1])).rswizzle());
}
}

class Decr extends Module {
Logic get out => output('out');
Decr(Logic inp, ParallelPrefix Function(List<Logic>, Logic Function(Logic, Logic )) ppGen) {
inp = addInput('inp', inp, width: inp.width);
final u = ppGen(
List<Logic>.generate(inp.width, (i) => ~inp[i] ),
(lhs, rhs) => rhs & lhs
);
addOutput('out', width: inp.width) <= (List<Logic>.generate(inp.width, (i) => ((i==0) ? ~inp[i] : inp[i]^u.val[i-1])).rswizzle());
}
}
240 changes: 169 additions & 71 deletions test/sklansky_test.dart
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import 'package:sklansky/sklansky.dart';
import 'package:rohd/rohd.dart';
import 'package:rohd/src/utilities/simcompare.dart';
import 'dart:math';
import 'package:test/test.dart';


void testOrScan(int n, fn) {

test('or_scan_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();


int computeOrScan(j) {
var result = 0;
var found = false;
for (var i=0; i<n; ++i) {
if (found || ((1<<i) & j) != 0) {
result |= 1<<i;
found = true;
}
test('or_scan_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();

int computeOrScan(j) {
var result = 0;
var found = false;
for (var i = 0; i < n; ++i) {
if (found || ((1 << i) & j) != 0) {
result |= 1 << i;
found = true;
}
return result;
}
return result;
}

// put/expect testing
// put/expect testing

for (var j=0; j < (1<<n); ++j) {
final golden = computeOrScan(j);
inp.put(j);
final result = mod.out.value.toInt();
//print("$j ${result} ${golden}");
expect(result, equals(golden));
}
for (var j = 0; j < (1 << n); ++j) {
final golden = computeOrScan(j);
inp.put(j);
final result = mod.out.value.toInt();
//print("$j ${result} ${golden}");
expect(result, equals(golden));
}

/*
/*
WaveDumper(mod);

// SimCompare testing
Expand All @@ -51,71 +49,147 @@ void testOrScan(int n, fn) {
);
expect(simResult, equals(true));
*/

});
});
}


void testPriorityEncoder(int n, fn) {
test('priority_encoder_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();

int computePriorityEncoding(j) {
for (var i=0; i<n; ++i) {
if (((1<<i) & j) != 0) {
return 1<<i;
}
test('priority_encoder_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();

int computePriorityEncoding(j) {
for (var i = 0; i < n; ++i) {
if (((1 << i) & j) != 0) {
return 1 << i;
}
return 0;
}
return 0;
}

// put/expect testing

for (var j = 0; j < (1 << n); ++j) {
final golden = computePriorityEncoding(j);
inp.put(j);
final result = mod.out.value.toInt();
// print("priority_encoder: $j ${result} ${golden}");
expect(result, equals(golden));
}
});
}

void testAdder(int n, fn) {
test('adder_$n', () async {
var a = Logic(name: 'a', width: n);
var b = Logic(name: 'b', width: n);

final mod = fn(a, b);
await mod.build();

int computeAdder(aa, bb) {
return (aa + bb) & ((1 << n) - 1);
}

// put/expect testing
// put/expect testing

for (var j=0; j < (1<<n); ++j) {
final golden = computePriorityEncoding(j);
inp.put(j);
for (var aa = 0; aa < (1 << n); ++aa) {
for (var bb = 0; bb < (1 << n); ++bb) {
final golden = computeAdder(aa, bb);
a.put(aa);
b.put(bb);
final result = mod.out.value.toInt();
// print("priority_encoder: $j ${result} ${golden}");
//print("adder: $aa $bb $result $golden");
expect(result, equals(golden));
}
}
});
}

});
BigInt genRandomBigInt(int nBits) {
BigInt result = BigInt.from(0);
while (nBits > 0) {
var shaveOff = min(16, nBits);
result =
(result << shaveOff) + BigInt.from(Random().nextInt(1 << shaveOff));
nBits -= shaveOff;
}
return result;
}

void testAdder(int n, fn) {
test('adder_$n', () async {
var a = Logic(name: 'a', width: n);
var b = Logic(name: 'b', width: n);
void testAdderRandom(int n, int nSamples, fn) {
test('adder_$n', () async {
var a = Logic(name: 'a', width: n);
var b = Logic(name: 'b', width: n);

final mod = fn(a, b);
await mod.build();
final mod = fn(a, b);
await mod.build();

int computeAdder(aa, bb) {
return (aa + bb) & ((1<<n)-1);
}
BigInt computeAdder(aa, bb) {
return (aa + bb) & ((BigInt.from(1) << n) - BigInt.from(1));
}
// put/expect testing

for (var i = 0; i < nSamples; ++i) {
var aa = genRandomBigInt(n);
var bb = genRandomBigInt(n);
final golden = computeAdder(aa, bb);
a.put(aa);
b.put(bb);
final result = mod.out.value.toBigInt();
//print("adder: ${aa.toRadixString(16)} ${bb.toRadixString(16)} ${result.toRadixString(16)} ${golden.toRadixString(16)}");
expect(result, equals(golden));
}
});
}

// put/expect testing
void testIncr(int n, fn) {
test('incr_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();

for (var aa=0; aa < (1<<n); ++aa) {
for (var bb=0; bb < (1<<n); ++bb) {
int computeIncr(aa) {
return (aa + 1) & ((1 << n) - 1);
}

final golden = computeAdder(aa, bb);
a.put(aa);
b.put(bb);
final result = mod.out.value.toInt();
//print("adder: $aa $bb $result $golden");
expect(result, equals(golden));
}
}
});
// put/expect testing

for (var aa = 0; aa < (1 << n); ++aa) {
final golden = computeIncr(aa);
inp.put(aa);
final result = mod.out.value.toInt();
//print("incr: $aa $result $golden");
expect(result, equals(golden));
}
});
}

void testDecr(int n, fn) {
test('decr_$n', () async {
var inp = Logic(name: 'inp', width: n);
final mod = fn(inp);
await mod.build();

int computeDecr(aa) {
return (aa - 1) % (1 << n);
}

// put/expect testing

for (var aa = 0; aa < (1 << n); ++aa) {
final golden = computeDecr(aa);
inp.put(aa);
final result = mod.out.value.toInt();
//print("decr: $aa $result $golden");
expect(result, equals(golden));
}
});
}

void main() {
tearDown(Simulator.reset);


group('largest_pow2_less_than', () {
test('largest_pow2_less_than', () async {
expect(largestPow2LessThan(5), equals(4));
Expand All @@ -125,26 +199,50 @@ void main() {
});

group('or_scan', () {
for (var n in [7,8,9]) {
for (var n in [7, 8, 9]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testOrScan(n, (inp) => OrScan(inp, ppGen));
}
}
});

group('priority_encoder', () {
for (var n in [7,8,9]) {
for (var n in [7, 8, 9]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testPriorityEncoder(n, (inp) => PriorityEncoder(inp, ppGen));
}
}
});

group('adder', () {
for (var n in [3,4,5]) {
for (var n in [3, 4, 5]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testAdder(n, (a, b) => Adder(a, b, ppGen));
}
}
});

group('adderRandom', () {
for (var n in [650]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testAdderRandom(n, 10, (a, b) => Adder(a, b, ppGen));
}
}
});

group('incr', () {
for (var n in [7, 8, 9]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testIncr(n, (inp) => Incr(inp, ppGen));
}
}
});

group('decr', () {
for (var n in [7, 8, 9]) {
for (var ppGen in [Ripple.new, Sklansky.new]) {
testDecr(n, (inp) => Decr(inp, ppGen));
}
}
});
}