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_bits
.xor
import PartitionedXOR
22 from ieee754
.part_bits
.bool import PartitionedBool
23 from ieee754
.part_bits
.all
import PartitionedAll
24 from ieee754
.part_shift
.part_shift_dynamic
import PartitionedDynamicShift
25 from ieee754
.part_shift
.part_shift_scalar
import PartitionedScalarShift
26 from ieee754
.part_mul_add
.partpoints
import make_partition2
, PartitionPoints
27 from ieee754
.part_mux
.part_mux
import PMux
28 from ieee754
.part_ass
.passign
import PAssign
29 from ieee754
.part_cat
.pcat
import PCat
30 from operator
import or_
, xor
, and_
, not_
32 from nmigen
import (Signal
, Const
)
33 from nmigen
.hdl
.ast
import UserValue
, Shape
37 if isinstance(op1
, PartitionedSignal
):
42 def applyop(op1
, op2
, op
):
43 if isinstance(op1
, PartitionedSignal
):
44 result
= PartitionedSignal
.like(op1
)
46 result
= PartitionedSignal
.like(op2
)
47 result
.m
.d
.comb
+= result
.sig
.eq(op(getsig(op1
), getsig(op2
)))
52 # for sub-modules to be created on-demand. Mux is done slightly
53 # differently (has its own global)
54 for name
in ['add', 'eq', 'gt', 'ge', 'ls', 'xor', 'bool', 'all']:
58 class PartitionedSignal(UserValue
):
59 # XXX ################################################### XXX
60 # XXX Keep these functions in the same order as ast.Value XXX
61 # XXX ################################################### XXX
62 def __init__(self
, mask
, *args
, src_loc_at
=0, **kwargs
):
63 super().__init
__(src_loc_at
=src_loc_at
)
64 self
.sig
= Signal(*args
, **kwargs
)
65 width
= len(self
.sig
) # get signal width
66 # create partition points
67 if isinstance(mask
, PartitionPoints
):
68 self
.partpoints
= mask
70 self
.partpoints
= make_partition2(mask
, width
)
73 def set_module(self
, m
):
76 def get_modname(self
, category
):
77 modnames
[category
] += 1
78 return "%s_%d" % (category
, modnames
[category
])
81 def like(other
, *args
, **kwargs
):
82 """Builds a new PartitionedSignal with the same PartitionPoints and
83 Signal properties as the other"""
84 result
= PartitionedSignal(PartitionPoints(other
.partpoints
))
85 result
.sig
= Signal
.like(other
.sig
, *args
, **kwargs
)
91 # now using __Assign__
93 # return self.sig.eq(getsig(val))
95 # nmigen-redirected constructs (Mux, Cat, Switch, Assign)
97 def __Mux__(self
, val1
, val2
):
98 # print ("partsig mux", self, val1, val2)
99 assert len(val1
) == len(val2
), \
100 "PartitionedSignal width sources must be the same " \
101 "val1 == %d, val2 == %d" % (len(val1
), len(val2
))
102 return PMux(self
.m
, self
.partpoints
, self
, val1
, val2
)
104 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=458
105 #def __Switch__(self, cases, *, src_loc=None, src_loc_at=0,
108 def __Cat__(self
, *args
, src_loc_at
=0):
109 args
= [self
] + list(args
)
111 assert isinstance(sig
, PartitionedSignal
), \
112 "All PartitionedSignal.__Cat__ arguments must be " \
113 "a PartitionedSignal. %s is not." % repr(sig
)
114 return PCat(self
.m
, args
, self
.partpoints
)
116 def __Assign__(self
, val
, *, src_loc_at
=0):
117 # print ("partsig ass", self, val)
118 return PAssign(self
.m
, self
, val
, self
.partpoints
)
120 # no override needed, Value.__bool__ sufficient
121 # def __bool__(self):
123 # unary ops that do not require partitioning
125 def __invert__(self
):
126 result
= PartitionedSignal
.like(self
)
127 self
.m
.d
.comb
+= result
.sig
.eq(~self
.sig
)
130 # unary ops that require partitioning
133 z
= Const(0, len(self
.sig
))
134 result
, _
= self
.sub_op(z
, self
)
137 # binary ops that need partitioning
139 def add_op(self
, op1
, op2
, carry
):
142 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
143 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
147 comb
+= pa
.carry_in
.eq(carry
)
148 result
= PartitionedSignal
.like(self
)
149 comb
+= result
.sig
.eq(pa
.output
)
150 return result
, pa
.carry_out
152 def sub_op(self
, op1
, op2
, carry
=~
0):
155 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
156 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
159 comb
+= pa
.b
.eq(~op2
)
160 comb
+= pa
.carry_in
.eq(carry
)
161 result
= PartitionedSignal
.like(self
)
162 comb
+= result
.sig
.eq(pa
.output
)
163 return result
, pa
.carry_out
165 def __add__(self
, other
):
166 result
, _
= self
.add_op(self
, other
, carry
=0)
169 def __radd__(self
, other
):
170 # https://bugs.libre-soc.org/show_bug.cgi?id=718
171 result
, _
= self
.add_op(other
, self
)
174 def __sub__(self
, other
):
175 result
, _
= self
.sub_op(self
, other
)
178 def __rsub__(self
, other
):
179 # https://bugs.libre-soc.org/show_bug.cgi?id=718
180 result
, _
= self
.sub_op(other
, self
)
183 def __mul__(self
, other
):
184 raise NotImplementedError # too complicated at the moment
185 return Operator("*", [self
, other
])
187 def __rmul__(self
, other
):
188 raise NotImplementedError # too complicated at the moment
189 return Operator("*", [other
, self
])
191 # not needed: same as Value.__check_divisor
192 #def __check_divisor(self):
194 def __mod__(self
, other
):
195 raise NotImplementedError
196 other
= Value
.cast(other
)
197 other
.__check
_divisor
()
198 return Operator("%", [self
, other
])
200 def __rmod__(self
, other
):
201 raise NotImplementedError
202 self
.__check
_divisor
()
203 return Operator("%", [other
, self
])
205 def __floordiv__(self
, other
):
206 raise NotImplementedError
207 other
= Value
.cast(other
)
208 other
.__check
_divisor
()
209 return Operator("//", [self
, other
])
211 def __rfloordiv__(self
, other
):
212 raise NotImplementedError
213 self
.__check
_divisor
()
214 return Operator("//", [other
, self
])
216 # not needed: same as Value.__check_shamt
217 #def __check_shamt(self):
219 # TODO: detect if the 2nd operand is a Const, a Signal or a
220 # PartitionedSignal. if it's a Const or a Signal, a global shift
221 # can occur. if it's a PartitionedSignal, that's much more interesting.
222 def ls_op(self
, op1
, op2
, carry
, shr_flag
=0):
224 if isinstance(op2
, Const
) or isinstance(op2
, Signal
):
226 pa
= PartitionedScalarShift(len(op1
), self
.partpoints
)
230 pa
= PartitionedDynamicShift(len(op1
), self
.partpoints
)
232 # TODO: case where the *shifter* is a PartitionedSignal but
233 # the thing *being* Shifted is a scalar (Signal, expression)
234 # https://bugs.libre-soc.org/show_bug.cgi?id=718
235 setattr(self
.m
.submodules
, self
.get_modname('ls'), pa
)
238 comb
+= pa
.data
.eq(op1
)
239 comb
+= pa
.shifter
.eq(op2
)
240 comb
+= pa
.shift_right
.eq(shr_flag
)
244 comb
+= pa
.shift_right
.eq(shr_flag
)
245 # XXX TODO: carry-in, carry-out (for arithmetic shift)
246 #comb += pa.carry_in.eq(carry)
247 return (pa
.output
, 0)
249 def __lshift__(self
, other
):
250 z
= Const(0, len(self
.partpoints
)+1)
251 result
, _
= self
.ls_op(self
, other
, carry
=z
) # TODO, carry
254 def __rlshift__(self
, other
):
255 # https://bugs.libre-soc.org/show_bug.cgi?id=718
256 raise NotImplementedError
257 return Operator("<<", [other
, self
])
259 def __rshift__(self
, other
):
260 z
= Const(0, len(self
.partpoints
)+1)
261 result
, _
= self
.ls_op(self
, other
, carry
=z
, shr_flag
=1) # TODO, carry
264 def __rrshift__(self
, other
):
265 # https://bugs.libre-soc.org/show_bug.cgi?id=718
266 raise NotImplementedError
267 return Operator(">>", [other
, self
])
269 # binary ops that don't require partitioning
271 def __and__(self
, other
):
272 return applyop(self
, other
, and_
)
274 def __rand__(self
, other
):
275 return applyop(other
, self
, and_
)
277 def __or__(self
, other
):
278 return applyop(self
, other
, or_
)
280 def __ror__(self
, other
):
281 return applyop(other
, self
, or_
)
283 def __xor__(self
, other
):
284 return applyop(self
, other
, xor
)
286 def __rxor__(self
, other
):
287 return applyop(other
, self
, xor
)
289 # binary comparison ops that need partitioning
291 def _compare(self
, width
, op1
, op2
, opname
, optype
):
292 # print (opname, op1, op2)
293 pa
= PartitionedEqGtGe(width
, self
.partpoints
)
294 setattr(self
.m
.submodules
, self
.get_modname(opname
), pa
)
296 comb
+= pa
.opcode
.eq(optype
) # set opcode
297 if isinstance(op1
, PartitionedSignal
):
298 comb
+= pa
.a
.eq(op1
.sig
)
301 if isinstance(op2
, PartitionedSignal
):
302 comb
+= pa
.b
.eq(op2
.sig
)
307 def __eq__(self
, other
):
308 width
= len(self
.sig
)
309 return self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
311 def __ne__(self
, other
):
312 width
= len(self
.sig
)
313 eq
= self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
314 ne
= Signal(eq
.width
)
315 self
.m
.d
.comb
+= ne
.eq(~eq
)
318 def __lt__(self
, other
):
319 width
= len(self
.sig
)
320 # swap operands, use gt to do lt
321 return self
._compare
(width
, other
, self
, "gt", PartitionedEqGtGe
.GT
)
323 def __le__(self
, other
):
324 width
= len(self
.sig
)
325 # swap operands, use ge to do le
326 return self
._compare
(width
, other
, self
, "ge", PartitionedEqGtGe
.GE
)
328 def __gt__(self
, other
):
329 width
= len(self
.sig
)
330 return self
._compare
(width
, self
, other
, "gt", PartitionedEqGtGe
.GT
)
332 def __ge__(self
, other
):
333 width
= len(self
.sig
)
334 return self
._compare
(width
, self
, other
, "ge", PartitionedEqGtGe
.GE
)
336 # no override needed: Value.__abs__ is general enough it does the job
342 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=716
343 # def __getitem__(self, key):
345 def __new_sign(self
, signed
):
346 shape
= Shape(len(self
), signed
=signed
)
347 result
= PartitionedSignal
.like(self
, shape
=shape
)
348 self
.m
.d
.comb
+= result
.sig
.eq(self
.sig
)
351 # http://bugs.libre-riscv.org/show_bug.cgi?id=719
352 def as_unsigned(self
):
353 return self
.__new
_sign
(False)
355 return self
.__new
_sign
(True)
360 """Conversion to boolean.
365 ``1`` if any bits are set, ``0`` otherwise.
367 width
= len(self
.sig
)
368 pa
= PartitionedBool(width
, self
.partpoints
)
369 setattr(self
.m
.submodules
, self
.get_modname("bool"), pa
)
370 self
.m
.d
.comb
+= pa
.a
.eq(self
.sig
)
374 """Check if any bits are ``1``.
379 ``1`` if any bits are set, ``0`` otherwise.
381 return self
!= Const(0) # leverage the __ne__ operator here
382 return Operator("r|", [self
])
385 """Check if all bits are ``1``.
390 ``1`` if all bits are set, ``0`` otherwise.
392 # something wrong with PartitionedAll, but self == Const(-1)"
393 # XXX https://bugs.libre-soc.org/show_bug.cgi?id=176#c17
394 #width = len(self.sig)
395 #pa = PartitionedAll(width, self.partpoints)
396 #setattr(self.m.submodules, self.get_modname("all"), pa)
397 #self.m.d.comb += pa.a.eq(self.sig)
399 return self
== Const(-1) # leverage the __eq__ operator here
402 """Compute pairwise exclusive-or of every bit.
407 ``1`` if an odd number of bits are set, ``0`` if an
408 even number of bits are set.
410 width
= len(self
.sig
)
411 pa
= PartitionedXOR(width
, self
.partpoints
)
412 setattr(self
.m
.submodules
, self
.get_modname("xor"), pa
)
413 self
.m
.d
.comb
+= pa
.a
.eq(self
.sig
)
416 # not needed: Value.implies does the job
417 # def implies(premise, conclusion):
419 # TODO. contains a Value.cast which means an override is needed (on both)
420 # def bit_select(self, offset, width):
421 # def word_select(self, offset, width):
423 # not needed: Value.matches, amazingly, should do the job
424 # def matches(self, *patterns):
426 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=713
428 return self
.sig
.shape()