convert to partition mask rather than partition points
[ieee754fpu.git] / src / ieee754 / part / partsig.py
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
3
4 """
5 Copyright (C) 2020 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
6
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*.
10
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.
15
16 http://bugs.libre-riscv.org/show_bug.cgi?id=132
17 """
18
19 from ieee754.part_mul_add.adder import PartitionedAdder
20 from ieee754.part_mul_add.partpoints import make_partition
21 from nmigen import (Signal,
22 )
23
24 class PartitionedSignal:
25 def __init__(self, mask, *args, **kwargs):
26 self.sig = Signal(*args, **kwargs)
27 width = self.sig.shape()[0] # get signal width
28 self.partpoints = make_partition(mask, width) # create partition points
29 self.modnames = {}
30 for name in ['add']:
31 self.modnames[name] = 0
32
33 def set_module(self, m):
34 self.m = m
35
36 def get_modname(self, category):
37 self.modnames[category] += 1
38 return "%s%d" % (category, self.modnames[category])
39
40 def eq(self, val):
41 return self.sig.eq(val)
42
43 def __xor__(self, other):
44 if isinstance(other, PartitionedSignal):
45 return self.sig ^ other.sig
46 return self.sig ^ other
47
48 def __add__(self, other):
49 shape = self.sig.shape()
50 pa = PartitionedAdder(shape[0], self.partpoints)
51 setattr(self.m.submodules, self.get_modname('add'), pa)
52 comb = self.m.d.comb
53 comb += pa.a.eq(self.sig)
54 if isinstance(other, PartitionedSignal):
55 comb += pa.b.eq(other.sig)
56 else:
57 comb += pa.b.eq(other)
58 return pa.output