1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
5 Copyright (C) 2020 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
7 dynamic-partitionable class similar to Signal, which, when the partition
8 is fully open will be identical to Signal. when partitions are closed,
9 the class turns into a SIMD variant of Signal. *this is dynamic*.
11 the basic fundamental idea is: write code once, and if you want a SIMD
12 version of it, use PartitionedSignal in place of Signal. job done.
13 this however requires the code to *not* be designed to use nmigen.If,
14 nmigen.Case, or other constructs: only Mux and other logic.
16 * http://bugs.libre-riscv.org/show_bug.cgi?id=132
19 from ieee754
.part_mul_add
.adder
import PartitionedAdder
20 from ieee754
.part_cmp
.eq_gt_ge
import PartitionedEqGtGe
21 from ieee754
.part_shift
.part_shift_dynamic
import PartitionedDynamicShift
22 from ieee754
.part_shift
.part_shift_scalar
import PartitionedScalarShift
23 from ieee754
.part_mul_add
.partpoints
import make_partition
24 from operator
import or_
, xor
, and_
, not_
26 from nmigen
import (Signal
, Const
)
30 if isinstance(op1
, PartitionedSignal
):
35 def applyop(op1
, op2
, op
):
36 return op(getsig(op1
), getsig(op2
))
39 class PartitionedSignal
:
40 def __init__(self
, mask
, *args
, **kwargs
):
41 self
.sig
= Signal(*args
, **kwargs
)
42 width
= self
.sig
.shape()[0] # get signal width
43 # create partition points
44 self
.partpoints
= make_partition(mask
, width
)
46 for name
in ['add', 'eq', 'gt', 'ge', 'ls']:
47 self
.modnames
[name
] = 0
49 def set_module(self
, m
):
52 def get_modname(self
, category
):
53 self
.modnames
[category
] += 1
54 return "mod_%s_%d" % (category
, self
.modnames
[category
])
57 return self
.sig
.eq(getsig(val
))
59 # unary ops that do not require partitioning
64 # unary ops that require partitioning
67 result
, _
= self
.add_op(self
, ~
0, carry
=0) # TODO, subop
70 # binary ops that don't require partitioning
72 def __and__(self
, other
):
73 return applyop(self
, other
, and_
)
75 def __rand__(self
, other
):
76 return applyop(other
, self
, and_
)
78 def __or__(self
, other
):
79 return applyop(self
, other
, or_
)
81 def __ror__(self
, other
):
82 return applyop(other
, self
, or_
)
84 def __xor__(self
, other
):
85 return applyop(self
, other
, xor
)
87 def __rxor__(self
, other
):
88 return applyop(other
, self
, xor
)
90 # binary ops that need partitioning
92 # TODO: detect if the 2nd operand is a Const, a Signal or a
93 # PartitionedSignal. if it's a Const or a Signal, a global shift
94 # can occur. if it's a PartitionedSignal, that's much more interesting.
95 def ls_op(self
, op1
, op2
, carry
):
97 if isinstance(op2
, Const
) or isinstance(op2
, Signal
):
100 pa
= PartitionedScalarShift(shape
[0], self
.partpoints
)
105 pa
= PartitionedDynamicShift(shape
[0], self
.partpoints
)
106 setattr(self
.m
.submodules
, self
.get_modname('ls'), pa
)
109 comb
+= pa
.data
.eq(op1
)
110 comb
+= pa
.shifter
.eq(op2
)
114 # XXX TODO: carry-in, carry-out
115 #comb += pa.carry_in.eq(carry)
116 return (pa
.output
, 0)
118 def __lshift__(self
, other
):
119 result
, _
= self
.ls_op(self
, other
, carry
=0)
122 def __rlshift__(self
, other
):
123 raise NotImplementedError
124 return Operator("<<", [other
, self
])
126 def __rshift__(self
, other
):
127 raise NotImplementedError
128 return Operator(">>", [self
, other
])
130 def __rrshift__(self
, other
):
131 raise NotImplementedError
132 return Operator(">>", [other
, self
])
134 def add_op(self
, op1
, op2
, carry
):
138 pa
= PartitionedAdder(shape
[0], self
.partpoints
)
139 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
143 comb
+= pa
.carry_in
.eq(carry
)
144 return (pa
.output
, pa
.carry_out
)
146 def sub_op(self
, op1
, op2
, carry
=~
0):
150 pa
= PartitionedAdder(shape
[0], self
.partpoints
)
151 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
154 comb
+= pa
.b
.eq(~op2
)
155 comb
+= pa
.carry_in
.eq(carry
)
156 return (pa
.output
, pa
.carry_out
)
158 def __add__(self
, other
):
159 result
, _
= self
.add_op(self
, other
, carry
=0)
162 def __radd__(self
, other
):
163 result
, _
= self
.add_op(other
, self
)
166 def __sub__(self
, other
):
167 result
, _
= self
.sub_op(self
, other
)
170 def __rsub__(self
, other
):
171 result
, _
= self
.sub_op(other
, self
)
174 def __mul__(self
, other
):
175 return Operator("*", [self
, other
])
177 def __rmul__(self
, other
):
178 return Operator("*", [other
, self
])
180 def __check_divisor(self
):
181 width
, signed
= self
.shape()
183 # Python's division semantics and Verilog's division semantics
184 # differ for negative divisors (Python uses div/mod, Verilog
185 # uses quo/rem); for now, avoid the issue
186 # completely by prohibiting such division operations.
187 raise NotImplementedError(
188 "Division by a signed value is not supported")
190 def __mod__(self
, other
):
191 raise NotImplementedError
192 other
= Value
.cast(other
)
193 other
.__check
_divisor
()
194 return Operator("%", [self
, other
])
196 def __rmod__(self
, other
):
197 raise NotImplementedError
198 self
.__check
_divisor
()
199 return Operator("%", [other
, self
])
201 def __floordiv__(self
, other
):
202 raise NotImplementedError
203 other
= Value
.cast(other
)
204 other
.__check
_divisor
()
205 return Operator("//", [self
, other
])
207 def __rfloordiv__(self
, other
):
208 raise NotImplementedError
209 self
.__check
_divisor
()
210 return Operator("//", [other
, self
])
212 # binary comparison ops that need partitioning
214 def _compare(self
, width
, op1
, op2
, opname
, optype
):
215 # print (opname, op1, op2)
216 pa
= PartitionedEqGtGe(width
, self
.partpoints
)
217 setattr(self
.m
.submodules
, self
.get_modname(opname
), pa
)
219 comb
+= pa
.opcode
.eq(optype
) # set opcode
220 if isinstance(op1
, PartitionedSignal
):
221 comb
+= pa
.a
.eq(op1
.sig
)
224 if isinstance(op2
, PartitionedSignal
):
225 comb
+= pa
.b
.eq(op2
.sig
)
230 def __eq__(self
, other
):
231 width
= self
.sig
.shape()[0]
232 return self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
234 def __ne__(self
, other
):
235 width
= self
.sig
.shape()[0]
236 eq
= self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
237 ne
= Signal(eq
.width
)
238 self
.m
.d
.comb
+= ne
.eq(~eq
)
241 def __gt__(self
, other
):
242 width
= self
.sig
.shape()[0]
243 return self
._compare
(width
, self
, other
, "gt", PartitionedEqGtGe
.GT
)
245 def __lt__(self
, other
):
246 width
= self
.sig
.shape()[0]
247 return self
._compare
(width
, other
, self
, "gt", PartitionedEqGtGe
.GT
)
249 def __ge__(self
, other
):
250 width
= self
.sig
.shape()[0]
251 return self
._compare
(width
, self
, other
, "ge", PartitionedEqGtGe
.GE
)
253 def __le__(self
, other
):
254 width
= self
.sig
.shape()[0]
255 return self
._compare
(width
, other
, self
, "ge", PartitionedEqGtGe
.GE
)
260 """Conversion to boolean.
265 ``1`` if any bits are set, ``0`` otherwise.
267 raise NotImplementedError
268 return Operator("b", [self
])
271 """Check if any bits are ``1``.
276 ``1`` if any bits are set, ``0`` otherwise.
278 raise NotImplementedError
279 return Operator("r|", [self
])
282 """Check if all bits are ``1``.
287 ``1`` if all bits are set, ``0`` otherwise.
289 raise NotImplementedError
290 return Operator("r&", [self
])
293 """Compute pairwise exclusive-or of every bit.
298 ``1`` if an odd number of bits are set, ``0`` if an
299 even number of bits are set.
301 # XXXX TODO: return partition-mask-sized set of bits
302 raise NotImplementedError
303 return Operator("r^", [self
])
305 def implies(premise
, conclusion
):
311 ``0`` if ``premise`` is true and ``conclusion`` is not,
314 # amazingly, this should actually work.
315 return ~premise | conclusion