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

rohd-hcl: find #37

Merged
merged 22 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions lib/rohd_hcl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export 'src/arbiter.dart';
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
export 'src/exceptions.dart';
export 'src/fifo.dart';
export 'src/find.dart';
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
export 'src/interfaces/interfaces.dart';
export 'src/memory.dart';
export 'src/one_hot.dart';
Expand Down
43 changes: 43 additions & 0 deletions lib/src/count.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// count.dart
// Implementation of Count Functionality.
//
// 2023 July 11
// Author: Rahul Gautham Putcha <[email protected]>
//

import 'dart:math';

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/src/utils.dart';

/// [Count] `1` or `0`
class Count extends Module {
/// [_output] is output of Count (use index for accessing from outside Module)
late Logic _output;

/// [index] is an getter for output of Count
Logic get index => _output;

/// [Count] `1` or `0`
///
/// Takes in [bus] of type [Logic]. by default performs [countOne] (`1`)
/// if [countOne] is `false` will count `0`
Count(Logic bus, {bool countOne = true}) {
Logic count = Const(0, width: max(1, log2Ceil(bus.width)));
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < bus.width; i++) {
count += bus[i].zeroExtend(count.width);
}
_output =
addOutput('count${countOne ? "One" : "Zero"}', width: count.width);

_output <=
(countOne
// count one
? count
// Count zero by removing one's from bus width
: Const(bus.width, width: count.width) - count);
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
79 changes: 79 additions & 0 deletions lib/src/find.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// find.dart
// Implementation of Find Functionality.
//
// 2023 July 11
// Author: Rahul Gautham Putcha <[email protected]>
//

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';
import 'package:rohd_hcl/src/count.dart';

/// Find functionality
///
/// Takes in a [Logic] to find location of `1`s or `0`s
class Find extends Module {
/// [_output] is output of Find (use index for accessing from outside Module)
late Logic _output;

/// [index] is an getter for output of Find
Logic get index => _output;

/// Find `1`s or `0`s
///
/// Takes in filter search parameter [one], default [Find] `1`.
/// If [one] is `true` [Find] `1` else [Find] `0`.
///
/// By default [Find] will look for first search parameter `1` or `0`.
/// If [n] is given, [Find] an [n]th search from first
/// occurance
Find(Logic bus, {bool one = true, Logic? n}) {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
if (n != null) {
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
if (one && i == 0) {
oneHotList.add(~bus[i] & n.eq(0));
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
} else {
final zeroCount = Count(bus.getRange(0, i + 1), countOne: one);
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved

var paddedNValue = n;
var paddedCountValue = zeroCount.index;
if (n.width < zeroCount.index.width) {
paddedNValue = n.zeroExtend(zeroCount.index.width);
} else {
paddedCountValue = zeroCount.index.zeroExtend(n.width);
}

// If `bus[i]` is a `0` and the number of `0`'s from index 0 to `i`
// is `n`
oneHotList.add(
(one ? bus[i] : ~bus[i]) & paddedCountValue.eq(paddedNValue));
}
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
_output = addOutput('findNthOne', width: bin.width);
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
_output <= bin;
} else {
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
final busCheck = one ? bus[i] : ~bus[i];
if (i == 0) {
oneHotList.add(busCheck);
} else {
final rangeCheck =
one ? ~bus.getRange(0, i).or() : bus.getRange(0, i).and();
oneHotList.add(busCheck & rangeCheck);
}
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
_output = addOutput('findFirstOne', width: bin.width);
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
_output <= bin;
return;
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
27 changes: 27 additions & 0 deletions test/count_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// find_test.dart
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
// Tests for Find
//
// 2023 June 8
// Author: Max Korbel <[email protected]>
//

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/src/count.dart';
import 'package:test/test.dart';

void main() {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
test('count all 1s', () {
final bus = Const(bin('01101'), width: 5);
final mod = Count(bus);
expect(mod.index.value.toInt(), 3);
});

test('count all 0s', () {
final bus = Const(bin('001101'), width: 6);
final mod = Count(bus, countOne: false);
expect(mod.index.value.toInt(), 3);
});
}
40 changes: 40 additions & 0 deletions test/find_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// find_test.dart
// Tests for Find
//
// 2023 June 8
// Author: Max Korbel <[email protected]>
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
//

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';
import 'package:rohd_hcl/src/utils.dart';
import 'package:test/test.dart';

void main() {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
test('find first one', () {
final bus = Const(bin('0111000100'), width: 10);
final mod = Find(bus);
expect(mod.index.value.toInt(), 2);
});

test('find nth one', () {
final bus = Const(bin('10110'), width: 5);
final mod = Find(bus, n: Const(3, width: log2Ceil(5)));
expect(mod.index.value.toInt(), 4);
});

test('find first zero', () {
final bus = Const(bin('0111011111'), width: 10);
final mod = Find(bus, one: false);
expect(mod.index.value.toInt(), 5);
});

test('find nth zero', () {
final bus = Const(bin('0111001010'), width: 10);
final mod = Find(bus, one: false, n: Const(3, width: log2Ceil(10)));
expect(mod.index.value.toInt(), 4);
});
}