restructure partsig, use common function for PartitionedEqGtGe
[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_cmp.eq_gt_ge import PartitionedEqGtGe
22 from ieee754.part_mul_add.partpoints import make_partition
23 from operator import or_, xor, and_, not_
24
25 from nmigen import (Signal,
26 )
27 def applyop(op1, op2, op):
28 if isinstance(op1, PartitionedSignal):
29 op1 = op1.sig
30 if isinstance(op2, PartitionedSignal):
31 op2 = op2.sig
32 return op(op1, op2)
33
34
35 class PartitionedSignal:
36 def __init__(self, mask, *args, **kwargs):
37 self.sig = Signal(*args, **kwargs)
38 width = self.sig.shape()[0] # get signal width
39 self.partpoints = make_partition(mask, width) # create partition points
40 self.modnames = {}
41 for name in ['add', 'eq', 'gt', 'ge']:
42 self.modnames[name] = 0
43
44 def set_module(self, m):
45 self.m = m
46
47 def get_modname(self, category):
48 self.modnames[category] += 1
49 return "%s%d" % (category, self.modnames[category])
50
51 def eq(self, val):
52 return self.sig.eq(val)
53
54 def __and__(self, other):
55 return applyop(self, other, and_)
56
57 def __rand__(self, other):
58 return applyop(other, self, and_)
59
60 def __or__(self, other):
61 return applyop(self, other, or_)
62
63 def __ror__(self, other):
64 return applyop(other, self, or_)
65
66 def __xor__(self, other):
67 return applyop(self, other, xor)
68
69 def __rxor__(self, other):
70 return applyop(other, self, xor)
71
72 def __add__(self, other):
73 shape = self.sig.shape()
74 pa = PartitionedAdder(shape[0], self.partpoints)
75 setattr(self.m.submodules, self.get_modname('add'), pa)
76 comb = self.m.d.comb
77 comb += pa.a.eq(self.sig)
78 if isinstance(other, PartitionedSignal):
79 comb += pa.b.eq(other.sig)
80 else:
81 comb += pa.b.eq(other)
82 return pa.output
83
84 def _compare(self, width, op1, op2, opname, optype):
85 #print (opname, op1, op2)
86 pa = PartitionedEqGtGe(width, self.partpoints)
87 setattr(self.m.submodules, self.get_modname(opname), pa)
88 comb = self.m.d.comb
89 comb += pa.opcode.eq(optype) # set opcode
90 if isinstance(op1, PartitionedSignal):
91 comb += pa.a.eq(op1.sig)
92 else:
93 comb += pa.a.eq(op1)
94 if isinstance(op2, PartitionedSignal):
95 comb += pa.b.eq(op2.sig)
96 else:
97 comb += pa.b.eq(op2)
98 return pa.output
99
100 def __eq__(self, other):
101 width = self.sig.shape()[0]
102 return self._compare(width, self, other, "eq", PartitionedEqGtGe.EQ)
103
104 def __ne__(self, other):
105 return ~self.__eq__(other)
106
107 def __gt__(self, other):
108 width = self.sig.shape()[0]
109 return self._compare(width, self, other, "gt", PartitionedEqGtGe.GT)
110
111 def __ge__(self, other):
112 width = self.sig.shape()[0]
113 return self._compare(width, self, other, "ge", PartitionedEqGtGe.GE)