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
, PartitionPoints
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
= len(self
.sig
) # get signal width
43 # create partition points
44 if isinstance(mask
, PartitionPoints
):
45 self
.partpoints
= mask
47 self
.partpoints
= make_partition(mask
, width
)
49 for name
in ['add', 'eq', 'gt', 'ge', 'ls']:
50 self
.modnames
[name
] = 0
52 def set_module(self
, m
):
55 def get_modname(self
, category
):
56 self
.modnames
[category
] += 1
57 return "%s_%d" % (category
, self
.modnames
[category
])
60 return self
.sig
.eq(getsig(val
))
62 # unary ops that do not require partitioning
67 # unary ops that require partitioning
70 z
= Const(0, len(self
.partpoints
)+1)
71 result
, _
= self
.add_op(self
, ~
0, carry
=z
) # TODO, subop
74 # binary ops that don't require partitioning
76 def __and__(self
, other
):
77 return applyop(self
, other
, and_
)
79 def __rand__(self
, other
):
80 return applyop(other
, self
, and_
)
82 def __or__(self
, other
):
83 return applyop(self
, other
, or_
)
85 def __ror__(self
, other
):
86 return applyop(other
, self
, or_
)
88 def __xor__(self
, other
):
89 return applyop(self
, other
, xor
)
91 def __rxor__(self
, other
):
92 return applyop(other
, self
, xor
)
94 # binary ops that need partitioning
96 # TODO: detect if the 2nd operand is a Const, a Signal or a
97 # PartitionedSignal. if it's a Const or a Signal, a global shift
98 # can occur. if it's a PartitionedSignal, that's much more interesting.
99 def ls_op(self
, op1
, op2
, carry
, shr_flag
=0):
101 if isinstance(op2
, Const
) or isinstance(op2
, Signal
):
103 pa
= PartitionedScalarShift(len(op1
), self
.partpoints
)
107 pa
= PartitionedDynamicShift(len(op1
), self
.partpoints
)
108 setattr(self
.m
.submodules
, self
.get_modname('ls'), pa
)
111 comb
+= pa
.data
.eq(op1
)
112 comb
+= pa
.shifter
.eq(op2
)
113 comb
+= pa
.shift_right
.eq(shr_flag
)
117 comb
+= pa
.shift_right
.eq(shr_flag
)
118 # XXX TODO: carry-in, carry-out
119 #comb += pa.carry_in.eq(carry)
120 return (pa
.output
, 0)
122 def __lshift__(self
, other
):
123 z
= Const(0, len(self
.partpoints
)+1)
124 result
, _
= self
.ls_op(self
, other
, carry
=z
) # TODO, carry
127 def __rlshift__(self
, other
):
128 raise NotImplementedError
129 return Operator("<<", [other
, self
])
131 def __rshift__(self
, other
):
132 z
= Const(0, len(self
.partpoints
)+1)
133 result
, _
= self
.ls_op(self
, other
, carry
=z
, shr_flag
=1) # TODO, carry
136 def __rrshift__(self
, other
):
137 raise NotImplementedError
138 return Operator(">>", [other
, self
])
140 def add_op(self
, op1
, op2
, carry
):
143 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
144 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
148 comb
+= pa
.carry_in
.eq(carry
)
149 return (pa
.output
, pa
.carry_out
)
151 def sub_op(self
, op1
, op2
, carry
=~
0):
154 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
155 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
158 comb
+= pa
.b
.eq(~op2
)
159 comb
+= pa
.carry_in
.eq(carry
)
160 return (pa
.output
, pa
.carry_out
)
162 def __add__(self
, other
):
163 result
, _
= self
.add_op(self
, other
, carry
=0)
166 def __radd__(self
, other
):
167 result
, _
= self
.add_op(other
, self
)
170 def __sub__(self
, other
):
171 result
, _
= self
.sub_op(self
, other
)
174 def __rsub__(self
, other
):
175 result
, _
= self
.sub_op(other
, self
)
178 def __mul__(self
, other
):
179 return Operator("*", [self
, other
])
181 def __rmul__(self
, other
):
182 return Operator("*", [other
, self
])
184 def __check_divisor(self
):
185 width
, signed
= self
.shape()
187 # Python's division semantics and Verilog's division semantics
188 # differ for negative divisors (Python uses div/mod, Verilog
189 # uses quo/rem); for now, avoid the issue
190 # completely by prohibiting such division operations.
191 raise NotImplementedError(
192 "Division by a signed value is not supported")
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 # binary comparison ops that need partitioning
218 def _compare(self
, width
, op1
, op2
, opname
, optype
):
219 # print (opname, op1, op2)
220 pa
= PartitionedEqGtGe(width
, self
.partpoints
)
221 setattr(self
.m
.submodules
, self
.get_modname(opname
), pa
)
223 comb
+= pa
.opcode
.eq(optype
) # set opcode
224 if isinstance(op1
, PartitionedSignal
):
225 comb
+= pa
.a
.eq(op1
.sig
)
228 if isinstance(op2
, PartitionedSignal
):
229 comb
+= pa
.b
.eq(op2
.sig
)
234 def __eq__(self
, other
):
235 width
= len(self
.sig
)
236 return self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
238 def __ne__(self
, other
):
239 width
= len(self
.sig
)
240 eq
= self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
241 ne
= Signal(eq
.width
)
242 self
.m
.d
.comb
+= ne
.eq(~eq
)
245 def __gt__(self
, other
):
246 width
= len(self
.sig
)
247 return self
._compare
(width
, self
, other
, "gt", PartitionedEqGtGe
.GT
)
249 def __lt__(self
, other
):
250 width
= len(self
.sig
)
251 # swap operands, use gt to do lt
252 return self
._compare
(width
, other
, self
, "gt", PartitionedEqGtGe
.GT
)
254 def __ge__(self
, other
):
255 width
= len(self
.sig
)
256 return self
._compare
(width
, self
, other
, "ge", PartitionedEqGtGe
.GE
)
258 def __le__(self
, other
):
259 width
= len(self
.sig
)
260 # swap operands, use ge to do le
261 return self
._compare
(width
, other
, self
, "ge", PartitionedEqGtGe
.GE
)
266 """Conversion to boolean.
271 ``1`` if any bits are set, ``0`` otherwise.
273 return self
.any() # have to see how this goes
274 #return Operator("b", [self])
277 """Check if any bits are ``1``.
282 ``1`` if any bits are set, ``0`` otherwise.
284 return self
!= Const(0) # leverage the __ne__ operator here
285 return Operator("r|", [self
])
288 """Check if all bits are ``1``.
293 ``1`` if all bits are set, ``0`` otherwise.
295 return self
== Const(-1) # leverage the __eq__ operator here
298 """Compute pairwise exclusive-or of every bit.
303 ``1`` if an odd number of bits are set, ``0`` if an
304 even number of bits are set.
306 # XXXX TODO: return partition-mask-sized set of bits
307 raise NotImplementedError
308 return Operator("r^", [self
])
310 def implies(premise
, conclusion
):
316 ``0`` if ``premise`` is true and ``conclusion`` is not,
319 # amazingly, this should actually work.
320 return ~premise | conclusion