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 1 commit
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
10 changes: 9 additions & 1 deletion lib/src/signals/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,15 @@ class Logic {
].swizzle();
}

/// Returns a new [Logic] with width [newWidth] where new bits added are sign
///This function calculate the value of a signal or value
Logic abs() {
if (width == 0) {
return this;
}
return mux(this[-1], ~this + 1, this);
}

/// Returns a new [Logic] wi}th 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
4 changes: 4 additions & 0 deletions lib/src/signals/logic_structure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,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 Expand Up @@ -515,4 +518,5 @@ class LogicStructure implements Logic {
void _updateWire(_Wire newWire) {
throw UnsupportedError('Delegated to elements');
}

}
12 changes: 12 additions & 0 deletions lib/src/values/logic_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,18 @@ abstract class LogicValue implements Comparable<LogicValue> {
return op(this, other);
}

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