b_asic.comparison_operations

Comparisons and logical types

Signals have one of three logical types: SignalType.ALGORITHM, SignalType.BOOLEAN, or SignalType.INTEGER. These identify independent data paths; the algorithm’s numeric representation is still selected separately using DataType. Untyped graphs continue to default to algorithm data.

LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual, Equal, and NotEqual return boolean values. Their signals use boolean storage by default, or integer storage when required by a connected operation; false is zero and true is one. Comparison accepts the predicate as its first argument, either a ComparisonType enum member or one of "<", "<=", ">", ">=", "==", and "!=". Equality supports complex values; ordered comparisons require real values. These operations do not overload Python object equality or ordering.

Select(condition, in0, in1) accepts boolean or integer conditions and returns in0 when its condition is nonzero, otherwise in1. Both data branches execute under the static schedule. The data inputs must have the same logical type, which is also the output type.

For example, this graph selects positive input samples and counts them:

from b_asic import (
    Addition, Constant, Delay, GreaterThan,
    Input, Output, Select, SFG, SignalType,
)

source = Input(signal_type=SignalType.ALGORITHM)
positive = GreaterThan(source, Constant(0))
state = Delay(initial_value=0)
count = Addition(state, positive)
state <<= count
selected = Select(positive, source, Constant(0))
graph = SFG([source], [Output(selected), Output(count)])

assert graph.output(0).signal_type is SignalType.ALGORITHM
assert graph.output(1).signal_type is SignalType.INTEGER
graph.validate_signal_types()

For a complete example with a schedule, shared processing elements, separate typed memories, and interconnect multiplexers, see Threshold gating with comparisons and shared selectors.

Interfaces, constants, delays, and selectors propagate constraints in both directions, including through feedback and nested graphs. Addition, subtraction, negation, and constant multiplication accept algorithm or integer data. Comparison results can feed integer arithmetic directly, including when the same result also controls a selector. A boolean source may feed an integer input without changing its stored type; the interconnect zero-extends it. Python integer constants remain algorithm data unless constrained by their connections or an explicit signal_type argument. Boolean constants and reset values must be zero or one; integer constants and reset values must be integral.

Read port.signal_type or signal.signal_type for the resolved type, and operation.port_types for the input/output signatures. On homogeneous operations, operation.signal_type is an optional explicit constraint; it is None when inferred. SFG construction and validate_signal_types() reject incompatible connections. Type queries recompute constraints after graph edits.

Resource assignment separates memories and processing elements by logical type/signature, even for nonoverlapping accesses. Consequently, architecture interconnect multiplexers connect compatible data paths. Integer width in the VHDL implementation is independent of the logical signal type. Native simulation retains its existing complex-double storage; counts should remain below 2**52. Simulation with a global quantizing DataType has not been extended to mixed types.

VHDL generation supports integer Addition, Subtraction, and AddSub using VhdlPrinter(data_type, integer_bits=32, integer_signed=True). The integer width includes the sign bit when signed and is shared by every integer PE, memory, and interface, independently of the algorithm representation. Boolean ports, pipeline registers, and storage use std_logic and are zero-extended at integer input multiplexers. Integer arithmetic uses modulo 2**integer_bits arithmetic independently of the algorithm’s overflow and quantization policies. Set integer_signed=False for unsigned integers. Register splits and control pipelines follow the existing arithmetic structure.

Comparisons select their VHDL printer by input format and always return std_logic. The generic Comparison and all six named comparisons support real fixed-point and integer operands. A shared PE generates only its scheduled predicates. Ordered predicates share one widened subtraction and, when needed, a zero check; equality-only PEs share one equality check and optional inversion. Fixed-point and integer comparisons can share that PE by sign/zero-extending their raw operands at its multiplexers. Both operands of each comparison retain the same binary point. Select selects its printer by the data inputs and handles its boolean condition separately.

Comparison and conditional replacement operations for signal flow graphs.

class b_asic.comparison_operations.Comparison(comparison_type: ComparisonType | str, in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: AbstractOperation

Compare two values of the same logical type and produce a boolean.

comparison_type accepts a ComparisonType or its operator string ("<", "<=", ">", ">=", "==", "!="). Ordered comparisons require real algorithm data or integers. Equality also accepts booleans and complex algorithm data. NaNs follow the underlying numeric comparison rules.

The optional input sources, name, latency, latency_offsets and execution_time follow the conventions of the other binary operations. Numeric representation settings never quantize the boolean output. The output uses boolean storage by default, or integer storage when required by its consumers (false is zero and true is one).

property comparison_type: ComparisonType

The comparison predicate.

evaluate(a, b, data_type=None) → bool

Evaluate the operation and generate a list of output values.

Parameters:
*inputs

List of input values.

data_typeDataType, optional

Data type to use for quantization during evaluation.

property is_swappable: bool

Return True if the inputs (and outputs) to the operation can be swapped.

Swapping require that the operation retains the same function, but it is allowed to modify values to do so.

classmethod type_name() → TypeName

Get the type name of this graph component.

class b_asic.comparison_operations.ComparisonType(*values)

Bases: StrEnum

The six comparison predicates accepted by Comparison.

EQUAL = '=='
GREATER_THAN = '>'
GREATER_THAN_OR_EQUAL = '>='
LESS_THAN = '<'
LESS_THAN_OR_EQUAL = '<='
NOT_EQUAL = '!='
class b_asic.comparison_operations.Equal(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the inputs are equal, including complex values.

class b_asic.comparison_operations.GreaterThan(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the first input is greater than the second.

class b_asic.comparison_operations.GreaterThanOrEqual(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the first input is greater than or equal to the second.

class b_asic.comparison_operations.LessThan(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the first input is less than the second.

class b_asic.comparison_operations.LessThanOrEqual(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the first input is less than or equal to the second.

class b_asic.comparison_operations.NotEqual(in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: _FixedComparison

Return whether the inputs differ, including complex values.

class b_asic.comparison_operations.ReplaceZero(value: int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat = 1, in0: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None, real_valued: bool | None = None)

Bases: _RealValuedOperation

Replace an exactly zero input with a constant.

\[\begin{split}y = \begin{cases} \text{value}, & x_0 = 0, \\ x_0, & x_0 \ne 0. \end{cases}\end{split}\]

Both signed zeros compare equal to zero. No tolerance is used. Complex inputs are zero only when both their real and imaginary parts are zero. Complex-to-real conversion requires a finite replacement unless real_valued=True.

Parameters:
valueNumber, default: 1

Replacement constant, following the constant-first convention of ConstantMultiplication.

in0SignalSourceProvider, optional

Input signal.

nameName, optional

Operation name.

latencyint, optional

Operation latency.

latency_offsetsdict[str, int], optional

Input and output latency offsets.

execution_timeint, optional

Time before the operation can be reused.

real_valuedbool or None, default: None

Use only the real input part. The replacement must then be real.

evaluate(a, data_type=None) → int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat

Evaluate the operation and generate a list of output values.

Parameters:
*inputs

List of input values.

data_typeDataType, optional

Data type to use for quantization during evaluation.

classmethod type_name() → TypeName

Get the type name of this graph component.

property value: int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat

Get the replacement constant.

class b_asic.comparison_operations.Select(condition: SignalSourceProvider | None = None, in0: SignalSourceProvider | None = None, in1: SignalSourceProvider | None = None, name: str = '', latency: int | None = None, latency_offsets: dict[str, int] | None = None, execution_time: int | None = None)

Bases: AbstractOperation

Select in0 when condition is true, otherwise in1.

The condition uses boolean or integer storage; zero is false and nonzero is true. The two data inputs and output share one inferred logical type. Both data branches are evaluated and statically scheduled.

evaluate(condition, a, b, data_type=None) → int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat

Evaluate the operation and generate a list of output values.

Parameters:
*inputs

List of input values.

data_typeDataType, optional

Data type to use for quantization during evaluation.

classmethod type_name() → TypeName

Get the type name of this graph component.