1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
5 Copyright (C) 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
7 dynamically-partitionable "assign" class, directly equivalent
12 * http://libre-riscv.org/3d_gpu/architecture/dynamic_simd/assign
13 * http://bugs.libre-riscv.org/show_bug.cgi?id=709
17 from nmigen
import Signal
, Module
, Elaboratable
, Cat
, Const
, signed
18 from nmigen
.back
.pysim
import Simulator
, Settle
19 from nmutil
.extend
import ext
21 from ieee754
.part_mul_add
.partpoints
import PartitionPoints
22 from ieee754
.part
.partsig
import SimdSignal
25 def get_runlengths(pbit
, size
):
28 # identify where the 1s are, which indicates "start of a new partition"
29 # we want a list of the lengths of all partitions
31 if pbit
& (1<<i
): # it's a 1: ends old partition, starts new
32 res
.append(count
) # add partition
33 count
= 1 # start again
36 # end reached, add whatever is left. could have done this by creating
37 # "fake" extra bit on the partitions, but hey
40 print ("get_runlengths", bin(pbit
), size
, res
)
45 class PartitionedAssign(Elaboratable
):
46 def __init__(self
, shape
, assign
, ctx
):
47 """Create a ``PartitionedAssign`` operator
49 # work out the length (total of all SimdSignals)
54 self
.output
= SimdSignal(mask
, self
.shape
, reset_less
=True)
55 self
.partition_points
= self
.output
.partpoints
56 self
.mwidth
= len(self
.partition_points
)+1
58 def get_chunk(self
, y
, numparts
):
60 if not isinstance(x
, SimdSignal
):
61 # assume Scalar. totally different rules
62 end
= numparts
* (len(x
) // self
.mwidth
)
64 # SimdSignal: start at partition point
65 keys
= [0] + list(x
.partpoints
.keys()) + [len(x
)]
66 # get current index and increment it (for next Assign chunk)
69 print ("getting", upto
, numparts
, keys
, len(x
))
70 # get the partition point as far as we are up to
72 end
= keys
[upto
+numparts
]
73 print ("start end", start
, end
, len(x
))
76 def elaborate(self
, platform
):
80 keys
= list(self
.partition_points
.keys())
81 print ("keys", keys
, "values", self
.partition_points
.values())
82 print ("ptype", self
.ptype
)
83 outpartsize
= len(self
.output
) // self
.mwidth
84 width
, signed
= self
.output
.shape()
85 print ("width, signed", width
, signed
)
87 with m
.Switch(self
.ptype
.get_switch()):
88 # for each partition possibility, create a Assign sequence
89 for pbit
in self
.ptype
.get_cases():
90 # set up some indices pointing to where things have got
91 # then when called below in the inner nested loop they give
92 # the relevant sequential chunk
95 # get a list of the length of each partition run
96 runlengths
= get_runlengths(pbit
, len(keys
))
97 print ("pbit", bin(pbit
), "runs", runlengths
)
98 for i
in runlengths
: # for each partition
99 thing
= self
.get_chunk(y
, i
) # sequential chunks
100 # now check the length: truncate, extend or leave-alone
101 outlen
= i
* outpartsize
103 thing
= ext(thing
, (tlen
, signed
), outlen
)
106 # direct access to the underlying Signal
107 comb
+= self
.output
.sig
.eq(Cat(*output
))
112 if isinstance(self
.assign
, SimdSignal
):
113 return [self
.assign
.lower(), self
.output
.lower()]
114 return [self
.assign
, self
.output
.lower()]
117 if __name__
== "__main__":
118 from ieee754
.part
.test
.test_partsig
import create_simulator
121 a
= SimdSignal(mask
, 32)
122 m
.submodules
.ass
= ass
= PartitionedAssign(signed(48), a
, a
.ptype
)
123 omask
= (1<<len(ass
.output
))-1
126 sim
= create_simulator(m
, traces
, "partass")
130 yield a
.sig
.eq(0xa12345c7)
132 out
= yield ass
.output
.sig
133 print("out 000", bin(out
&omask
), hex(out
&omask
))
136 out
= yield ass
.output
.sig
137 print("out 010", bin(out
&omask
), hex(out
&omask
))
140 out
= yield ass
.output
.sig
141 print("out 110", bin(out
&omask
), hex(out
&omask
))
144 out
= yield ass
.output
.sig
145 print("out 111", bin(out
&omask
), hex(out
&omask
))
147 sim
.add_process(process
)
148 with sim
.write_vcd("partition_ass.vcd", "partition_ass.gtkw",
157 def __init__(self
, mask
):
161 def get_switch(self
):
162 return Cat(self
.get_mask())
164 return range(1<<len(self
.get_mask()))
166 def blanklanes(self
):
168 ptype
= PartType(mask
)
169 m
.submodules
.ass
= ass
= PartitionedAssign(signed(48), a
, ptype
)
170 omask
= (1<<len(ass
.output
))-1
173 sim
= create_simulator(m
, traces
, "partass")
177 yield a
.eq(0xa12345c7)
179 out
= yield ass
.output
.sig
180 print("out 000", bin(out
&omask
), hex(out
&omask
))
183 out
= yield ass
.output
.sig
184 print("out 010", bin(out
&omask
), hex(out
&omask
))
187 out
= yield ass
.output
.sig
188 print("out 110", bin(out
&omask
), hex(out
&omask
))
191 out
= yield ass
.output
.sig
192 print("out 111", bin(out
&omask
), hex(out
&omask
))
194 sim
.add_process(process
)
195 with sim
.write_vcd("partition_ass.vcd", "partition_ass.gtkw",