Newer
Older
Angus Lothian
committed
return a + b, a - b
Angus Lothian
committed
class MAD(AbstractOperation):
Angus Lothian
committed
Gives the result of multiplying the first input by the second input and
then adding the third input.
.. math:: y = x_0 \times x_1 + x_2
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
Parameters
==========
src0, src1, src2 : SignalSourceProvider, optional
The three signals to determine the multiply-add operation of.
name : Name, optional
Operation name.
latency : int, optional
Operation latency (delay from input to output in time units).
latency_offsets : dict[str, int], optional
Used if inputs have different arrival times or if the inputs should arrive
after the operator has stared. For example, ``{"in0": 0, "in1": 0, "in2": 2}``
which corresponds to *src2*, i.e., the term to be added, arriving two time
units later than *src0* and *src1*. If not provided and *latency* is provided,
set to zero. Hence, the previous example can be written as ``{"in1": 1}``
only.
execution_time : int, optional
Operation execution time (time units before operator can be reused).
See Also
--------
Multiplication
Addition
Angus Lothian
committed
"""
__slots__ = (
"_src0",
"_src1",
"_src2",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_src0: Optional[SignalSourceProvider]
_src1: Optional[SignalSourceProvider]
_src2: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
is_swappable = True
def __init__(
self,
src0: Optional[SignalSourceProvider] = None,
src1: Optional[SignalSourceProvider] = None,
src2: Optional[SignalSourceProvider] = None,
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
Angus Lothian
committed
"""Construct a MAD operation."""
super().__init__(
input_count=3,
output_count=1,
input_sources=[src0, src1, src2],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
Angus Lothian
committed
@classmethod
def type_name(cls) -> TypeName:
Angus Lothian
committed
def evaluate(self, a, b, c):
return a * b + c
@property
def is_linear(self) -> bool:
return (
self.input(0).connected_source.operation.is_constant
or self.input(1).connected_source.operation.is_constant
)
def swap_io(self) -> None:
self._input_ports = [
self._input_ports[1],
self._input_ports[0],
self._input_ports[2],
]
for i, p in enumerate(self._input_ports):
p._index = i
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
class MADS(AbstractOperation):
__slots__ = (
"_is_add",
"_override_zero_on_src0",
"_src0",
"_src1",
"_src2",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_is_add: Optional[bool]
_override_zero_on_src0: Optional[bool]
_src0: Optional[SignalSourceProvider]
_src1: Optional[SignalSourceProvider]
_src2: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
is_swappable = True
def __init__(
self,
is_add: Optional[bool] = True,
override_zero_on_src0: Optional[bool] = False,
src0: Optional[SignalSourceProvider] = None,
src1: Optional[SignalSourceProvider] = None,
src2: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
):
"""Construct a MADS operation."""
super().__init__(
input_count=3,
output_count=1,
name=Name(name),
input_sources=[src0, src1, src2],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
)
self.set_param("is_add", is_add)
self.set_param("override_zero_on_src0", override_zero_on_src0)
@classmethod
def type_name(cls) -> TypeName:
return TypeName("mads")
def evaluate(self, a, b, c):
if self.is_add:
if self.override_zero_on_src0:
return b * c
else:
return a + b * c
else:
if self.override_zero_on_src0:
return -b * c
else:
return a - b * c
@property
def is_add(self) -> bool:
"""Get if operation is an addition."""
return self.param("is_add")
@is_add.setter
def is_add(self, is_add: bool) -> None:
"""Set if operation is an addition."""
self.set_param("is_add", is_add)
@property
def override_zero_on_src0(self) -> bool:
"""Get if operation is overriding a zero on port src0."""
return self.param("override_zero_on_src0")
@override_zero_on_src0.setter
def override_zero_on_src0(self, override_zero_on_src0: bool) -> None:
"""Set if operation is overriding a zero on port src0."""
self.set_param("override_zero_on_src0", override_zero_on_src0)
@property
def is_linear(self) -> bool:
return (
self.input(1).connected_source.operation.is_constant
or self.input(2).connected_source.operation.is_constant
)
def swap_io(self) -> None:
self._input_ports = [
self._input_ports[0],
self._input_ports[2],
self._input_ports[1],
]
for i, p in enumerate(self._input_ports):
p._index = i
class SymmetricTwoportAdaptor(AbstractOperation):
Wave digital filter symmetric twoport-adaptor operation.
.. math::
\begin{eqnarray}
y_0 & = & x_1 + \text{value}\times\left(x_1 - x_0\right)\\
y_1 & = & x_0 + \text{value}\times\left(x_1 - x_0\right)
\end{eqnarray}
__slots__ = (
"_src0",
"_src1",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_src0: Optional[SignalSourceProvider]
_src1: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
is_swappable = True
src0: Optional[SignalSourceProvider] = None,
src1: Optional[SignalSourceProvider] = None,
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
"""Construct a SymmetricTwoportAdaptor operation."""
super().__init__(
input_count=2,
output_count=2,
input_sources=[src0, src1],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
@classmethod
def type_name(cls) -> TypeName:
def evaluate(self, a, b):
return b + tmp, a + tmp
@property
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
"""Set the constant value of this operation."""
if -1 <= value <= 1:
self.set_param("value", value)
else:
raise ValueError('value must be between -1 and 1 (inclusive)')
def swap_io(self) -> None:
# Swap inputs and outputs and change sign of coefficient
self._input_ports.reverse()
for i, p in enumerate(self._input_ports):
p._index = i
self._output_ports.reverse()
for i, p in enumerate(self._output_ports):
p._index = i
self.set_param("value", -self.value)
class Reciprocal(AbstractOperation):
r"""
Reciprocal operation.
Gives the reciprocal of its input.
.. math:: y = \frac{1}{x}
Parameters
----------
src0 : :class:`~b_asic.port.SignalSourceProvider`, optional
The signal to compute the reciprocal of.
name : Name, optional
Operation name.
latency : int, optional
Operation latency (delay from input to output in time units).
latency_offsets : dict[str, int], optional
Used if input arrives later than when the operator starts, e.g.,
``{"in0": 0`` which corresponds to *src0* arriving one time unit after the
operator starts. If not provided and *latency* is provided, set to zero.
execution_time : int, optional
Operation execution time (time units before operator can be reused).
See also
========
Division
__slots__ = ("_src0", "_name", "_latency", "_latency_offsets", "_execution_time")
_src0: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
def __init__(
self,
src0: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
):
"""Construct a Reciprocal operation."""
super().__init__(
input_count=1,
output_count=1,
name=Name(name),
input_sources=[src0],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
)
@classmethod
def type_name(cls) -> TypeName:
return TypeName("rec")
def evaluate(self, a):
return 1 / a
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
class RightShift(AbstractOperation):
r"""
Arithmetic right-shift operation.
Shifts the input to the right assuming a fixed-point representation, so
a multiplication by a power of two.
.. math:: y = x \gg \text{value} = 2^{-\text{value}}x \text{ where value} \geq 0
Parameters
----------
value : int
Number of bits to shift right.
src0 : :class:`~b_asic.port.SignalSourceProvider`, optional
The signal to shift right.
name : Name, optional
Operation name.
latency : int, optional
Operation latency (delay from input to output in time units).
latency_offsets : dict[str, int], optional
Used if input arrives later than when the operator starts, e.g.,
``{"in0": 0`` which corresponds to *src0* arriving one time unit after the
operator starts. If not provided and *latency* is provided, set to zero.
execution_time : int, optional
Operation execution time (time units before operator can be reused).
See Also
--------
LeftShift
Shift
"""
__slots__ = (
"_value",
"_src0",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_value: Num
_src0: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
is_linear = True
def __init__(
self,
value: int = 0,
src0: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
):
"""Construct a RightShift operation with the given value."""
super().__init__(
input_count=1,
output_count=1,
name=Name(name),
input_sources=[src0],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
)
self.value = value
@classmethod
def type_name(cls) -> TypeName:
return TypeName("rshift")
def evaluate(self, a):
return a * 2 ** (-self.param("value"))
@property
def value(self) -> int:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: int) -> None:
"""Set the constant value of this operation."""
if not isinstance(value, int):
raise TypeError("value must be an int")
if value < 0:
raise ValueError("value must be non-negative")
self.set_param("value", value)
class LeftShift(AbstractOperation):
r"""
Arithmetic left-shift operation.
Shifts the input to the left assuming a fixed-point representation, so
a multiplication by a power of two.
.. math:: y = x \ll \text{value} = 2^{\text{value}}x \text{ where value} \geq 0
Parameters
----------
value : int
Number of bits to shift left.
src0 : :class:`~b_asic.port.SignalSourceProvider`, optional
The signal to shift left.
name : Name, optional
Operation name.
latency : int, optional
Operation latency (delay from input to output in time units).
latency_offsets : dict[str, int], optional
Used if input arrives later than when the operator starts, e.g.,
``{"in0": 0`` which corresponds to *src0* arriving one time unit after the
operator starts. If not provided and *latency* is provided, set to zero.
execution_time : int, optional
Operation execution time (time units before operator can be reused).
See Also
--------
RightShift
Shift
"""
__slots__ = (
"_value",
"_src0",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_value: Num
_src0: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
is_linear = True
def __init__(
self,
value: int = 0,
src0: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
):
"""Construct a RightShift operation with the given value."""
super().__init__(
input_count=1,
output_count=1,
name=Name(name),
input_sources=[src0],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
)
self.value = value
@classmethod
def type_name(cls) -> TypeName:
return TypeName("lshift")
def evaluate(self, a):
return a * 2 ** (self.param("value"))
@property
def value(self) -> int:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: int) -> None:
"""Set the constant value of this operation."""
if not isinstance(value, int):
raise TypeError("value must be an int")
if value < 0:
raise ValueError("value must be non-negative")
self.set_param("value", value)
class Shift(AbstractOperation):
r"""
Arithmetic shift operation.
Shifts the input to the left or right assuming a fixed-point representation, so
a multiplication by a power of two. By definition a positive value is a shift to
the left.
.. math:: y = x \ll \text{value} = 2^{\text{value}}x
Parameters
----------
value : int
Number of bits to shift. Positive *value* shifts to the left.
src0 : :class:`~b_asic.port.SignalSourceProvider`, optional
The signal to shift.
name : Name, optional
Operation name.
latency : int, optional
Operation latency (delay from input to output in time units).
latency_offsets : dict[str, int], optional
Used if input arrives later than when the operator starts, e.g.,
``{"in0": 0`` which corresponds to *src0* arriving one time unit after the
operator starts. If not provided and *latency* is provided, set to zero.
execution_time : int, optional
Operation execution time (time units before operator can be reused).
See Also
--------
LeftShift
RightShift
"""
__slots__ = (
"_value",
"_src0",
"_name",
"_latency",
"_latency_offsets",
"_execution_time",
)
_value: Num
_src0: Optional[SignalSourceProvider]
_name: Name
_latency: Optional[int]
_latency_offsets: Optional[Dict[str, int]]
_execution_time: Optional[int]
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
is_linear = True
def __init__(
self,
value: int = 0,
src0: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
latency_offsets: Optional[Dict[str, int]] = None,
execution_time: Optional[int] = None,
):
"""Construct a Shift operation with the given value."""
super().__init__(
input_count=1,
output_count=1,
name=Name(name),
input_sources=[src0],
latency=latency,
latency_offsets=latency_offsets,
execution_time=execution_time,
)
self.value = value
@classmethod
def type_name(cls) -> TypeName:
return TypeName("shift")
def evaluate(self, a):
return a * 2 ** (self.param("value"))
@property
def value(self) -> int:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: int) -> None:
"""Set the constant value of this operation."""
if not isinstance(value, int):
raise TypeError("value must be an int")
self.set_param("value", value)
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
class DontCare(AbstractOperation):
r"""
Dont-care operation
Used for ignoring the input to another operation and thus avoiding dangling input nodes.
Parameters
----------
name : Name, optional
Operation name.
"""
__slots__ = "_name"
_name: Name
is_linear = True
def __init__(self, name: Name = ""):
"""Construct a DontCare operation."""
super().__init__(
input_count=0,
output_count=1,
name=name,
latency_offsets={"out0": 0},
)
@classmethod
def type_name(cls) -> TypeName:
return TypeName("dontcare")
def evaluate(self):
return 0
@property
def latency(self) -> int:
return self.latency_offsets["out0"]
def __repr__(self) -> str:
return "DontCare()"
def __str__(self) -> str:
return "dontcare"
class Sink(AbstractOperation):
r"""
Sink operation.
Used for ignoring the output from another operation to avoid dangling output nodes.
Parameters
==========
name : Name, optional
Operation name.
"""
__slots__ = "_name"
_name: Name
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
is_linear = True
def __init__(self, name: Name = ""):
"""Construct a Sink operation."""
super().__init__(
input_count=1,
output_count=0,
name=name,
latency_offsets={"in0": 0},
)
@classmethod
def type_name(cls) -> TypeName:
return TypeName("sink")
def evaluate(self):
raise NotImplementedError
@property
def latency(self) -> int:
return self.latency_offsets["in0"]
def __repr__(self) -> str:
return "Sink()"
def __str__(self) -> str:
return "sink"