Skip to content

Absolute value #442

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

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion lib/src/modules/gates.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Intel Corporation
// Copyright (C) 2021-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// gates.dart
Expand Down
13 changes: 11 additions & 2 deletions lib/src/signals/logic.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Intel Corporation
// Copyright (C) 2021-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// logic.dart
Expand Down Expand Up @@ -638,7 +638,16 @@ class Logic {
].swizzle();
}

/// Returns a new [Logic] with width [newWidth] where new bits added are sign
/// Calculates the absolute value of a signal, assuming that the
/// number is a two's complement.
Logic abs() {
if (width == 0) {
return this;
}
return mux(this[-1], ~this + 1, this);
}

/// Returns a new [Logic] width width [newWidth] where new bits added are sign
/// bits as the most significant bits. The sign is determined using two's
/// complement, so it takes the most significant bit of the original signal
/// and extends with that.
Expand Down
5 changes: 4 additions & 1 deletion lib/src/signals/logic_structure.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Intel Corporation
// Copyright (C) 2023-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// logic_structure.dart
Expand Down Expand Up @@ -483,6 +483,9 @@ class LogicStructure implements Logic {
@override
Logic get reversed => packed.reversed;

@override
Logic abs() => packed.abs();

@override
Logic signExtend(int newWidth) => packed.signExtend(newWidth);

Expand Down
16 changes: 15 additions & 1 deletion lib/src/values/logic_value.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Intel Corporation
// Copyright (C) 2021-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// logic_values.dart
Expand Down Expand Up @@ -862,6 +862,20 @@ abstract class LogicValue implements Comparable<LogicValue> {
return op(this, other);
}

/// Calculates the absolute value, assuming that the
/// number is a two's complement.
LogicValue abs() {
if (width == 0) {
return this;
}
if (!this[-1].isValid) {
return LogicValue.filled(width, LogicValue.x);
}
return this[-1] == LogicValue.one
? ~this + LogicValue.ofInt(1, width)
: this;
}

/// Unary AND operation.
///
/// Returns `1` iff all bits are `1`.
Expand Down
2 changes: 1 addition & 1 deletion test/extend_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2022-2023 Intel Corporation
// Copyright (C) 2022-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// extend_test.dart
Expand Down
26 changes: 25 additions & 1 deletion test/gate_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Intel Corporation
// Copyright (C) 2021-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// gate_test.dart
Expand Down Expand Up @@ -55,6 +55,16 @@ class UnaryGateTestModule extends Module {
}
}

class Absolute extends Module {
Absolute(Logic a) : super(name: 'absolute') {
a = addInput('a', a, width: a.width);

final y = addOutput('y', width: a.width);

y <= a.abs();
}
}

class ShiftTestModule extends Module {
dynamic constant; // int or BigInt

Expand Down Expand Up @@ -410,6 +420,20 @@ void main() {
expect(simResult, equals(true));
});

test('absolute', () async {
final mod = Absolute(Logic(width: 4));
await mod.build();
final vectors = [
Vector({'a': bin('1111')}, {'y': bin('0001')}),
Vector({'a': bin('0110')}, {'y': bin('0110')}),
Vector({'a': bin('0010')}, {'y': bin('0010')}),
Vector({'a': bin('1011')}, {'y': bin('0101')}),
];
await SimCompare.checkFunctionalVector(mod, vectors);
final simResult = SimCompare.iverilogVector(mod, vectors);
expect(simResult, equals(true));
});

test('Mux bus', () async {
final mod = MuxWrapper(Logic(), Logic(width: 8), Logic(width: 8));
await mod.build();
Expand Down
20 changes: 19 additions & 1 deletion test/logic_value_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Intel Corporation
// Copyright (C) 2021-2024 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// logic_value_test.dart
Expand Down Expand Up @@ -1163,6 +1163,24 @@ void main() {
equals(LogicValue.filled(4, LogicValue.x)));
});

test('absolute', () {
expect(
// test of positive value
LogicValue.ofInt(2, 32).abs(),
equals(LogicValue.ofInt(2, 32)));
expect(
// test of negative value
LogicValue.ofInt(-7, 32).abs(),
equals(LogicValue.ofInt(7, 32)));
expect(
// test of zero width
LogicValue.filled(0, LogicValue.zero).abs(),
equals(LogicValue.filled(0, LogicValue.zero)));
expect(
//test string
LogicValue.ofString('000010').abs(),
equals(LogicValue.ofString('000010')));
});
test('addsub', () {
expect(
// + normal
Expand Down