use EQCombiner in PartitionedEq experiment
[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_cmp.equal_ortree import PartitionedEq
21 from ieee754.part_mul_add.partpoints import make_partition
22 from operator import or_, xor, and_, not_
23
24 from nmigen import (Signal,
25 )
26 def applyop(op1, op2, op):
27 if isinstance(op1, PartitionedSignal):
28 op1 = op1.sig
29 if isinstance(op2, PartitionedSignal):
30 op2 = op2.sig
31 return op(op1, op2)
32
33
34 class PartitionedSignal:
35 def __init__(self, mask, *args, **kwargs):
36 self.sig = Signal(*args, **kwargs)
37 width = self.sig.shape()[0] # get signal width
38 self.partpoints = make_partition(mask, width) # create partition points
39 self.modnames = {}
40 for name in ['add', 'eq']:
41 self.modnames[name] = 0
42
43 def set_module(self, m):
44 self.m = m
45
46 def get_modname(self, category):
47 self.modnames[category] += 1
48 return "%s%d" % (category, self.modnames[category])
49
50 def eq(self, val):
51 return self.sig.eq(val)
52
53 def __and__(self, other):
54 return applyop(self, other, and_)
55
56 def __rand__(self, other):
57 return applyop(other, self, and_)
58
59 def __or__(self, other):
60 return applyop(self, other, or_)
61
62 def __ror__(self, other):
63 return applyop(other, self, or_)
64
65 def __xor__(self, other):
66 return applyop(self, other, xor)
67
68 def __rxor__(self, other):
69 return applyop(other, self, xor)
70
71 def __add__(self, other):
72 shape = self.sig.shape()
73 pa = PartitionedAdder(shape[0], self.partpoints)
74 setattr(self.m.submodules, self.get_modname('add'), pa)
75 comb = self.m.d.comb
76 comb += pa.a.eq(self.sig)
77 if isinstance(other, PartitionedSignal):
78 comb += pa.b.eq(other.sig)
79 else:
80 comb += pa.b.eq(other)
81 return pa.output
82
83 def __eq__(self, other):
84 print ("eq", self, other)
85 shape = self.sig.shape()
86 pa = PartitionedEq(shape[0], self.partpoints)
87 setattr(self.m.submodules, self.get_modname('eq'), pa)
88 comb = self.m.d.comb
89 comb += pa.a.eq(self.sig)
90 if isinstance(other, PartitionedSignal):
91 comb += pa.b.eq(other.sig)
92 else:
93 comb += pa.b.eq(other)
94 return pa.output