1 # SPDX-License-Identifier: LGPL-3-or-later
2 # Copyright 2021 Jacob Lifshay
3 # Copyright (C) 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 # Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
6 # of Horizon 2020 EU Programme 957073.
8 """Bitwise logic operators implemented using a look-up table, like LUTs in
9 FPGAs. Inspired by x86's `vpternlog[dq]` instructions.
11 https://bugs.libre-soc.org/show_bug.cgi?id=745
12 https://www.felixcloutier.com/x86/vpternlogd:vpternlogq
15 from nmigen
.hdl
.ast
import Array
, Cat
, Repl
, Signal
16 from nmigen
.hdl
.dsl
import Module
17 from nmigen
.hdl
.ir
import Elaboratable
18 from dataclasses
import dataclass
21 class BitwiseMux(Elaboratable
):
22 """Mux, but treating input/output Signals as bit vectors, rather than
23 integers. This means each bit in the output is independently multiplexed
24 based on the corresponding bit in each of the inputs.
27 def __init__(self
, width
):
28 self
.sel
= Signal(width
)
29 self
.t
= Signal(width
)
30 self
.f
= Signal(width
)
31 self
.output
= Signal(width
)
33 def elaborate(self
, platform
):
35 m
.d
.comb
+= self
.output
.eq((~self
.sel
& self
.f
) |
(self
.sel
& self
.t
))
39 class BitwiseLut(Elaboratable
):
40 """Bitwise logic operators implemented using a look-up table, like LUTs in
41 FPGAs. Inspired by x86's `vpternlog[dq]` instructions.
43 Each output bit `i` is set to `lut[Cat(inp[i] for inp in self.inputs)]`
46 def __init__(self
, input_count
, width
):
49 the number of inputs. ternlog-style instructions have 3 inputs.
51 the number of bits in each input/output.
53 self
.input_count
= input_count
57 return Signal(width
, name
=f
"input{i}")
58 self
.inputs
= tuple(inp(i
) for i
in range(input_count
)) # inputs
59 self
.lut
= Signal(2 ** input_count
) # lookup input
60 self
.output
= Signal(width
) # output
62 def elaborate(self
, platform
):
65 lut_array
= Array(self
.lut
) # create dynamic-indexable LUT array
68 for bit
in range(self
.width
):
69 # take the bit'th bit of every input, create a LUT index from it
70 index
= Signal(self
.input_count
, name
="index%d" % bit
)
71 comb
+= index
.eq(Cat(inp
[bit
] for inp
in self
.inputs
))
72 # store output bit in a list - Cat() it after (simplifies graphviz)
73 outbit
= Signal(name
="out%d" % bit
)
74 comb
+= outbit
.eq(lut_array
[index
])
77 # finally Cat() all the output bits together
78 comb
+= self
.output
.eq(Cat(*out
))
82 return list(self
.inputs
) + [self
.lut
, self
.output
]
87 """Mux in tree for `TreeBitwiseLut`."""
89 container
: "TreeBitwiseLut"
90 parent
: "_TreeMuxNode | None"
91 child0
: "_TreeMuxNode | None"
92 child1
: "_TreeMuxNode | None"
96 def child_index(self
):
97 """index of this node, when looked up in this node's parent's children.
99 if self
.parent
is None:
101 return int(self
.parent
.child1
is self
)
103 def add_child(self
, child_index
):
105 out
=Signal(self
.container
.width
),
106 container
=self
.container
, parent
=self
,
107 child0
=None, child1
=None, depth
=1 + self
.depth
)
109 assert self
.child1
is None
112 assert self
.child0
is None
114 node
.out
.name
= "node_out_" + node
.key_str
121 while node
.parent
is not None:
122 retval
.append(node
.child_index
)
129 k
= ['x'] * self
.container
.input_count
130 for i
, v
in enumerate(self
.key
):
131 k
[i
] = '1' if v
else '0'
132 return '0b' + ''.join(reversed(k
))
135 class TreeBitwiseLut(Elaboratable
):
136 """Tree-based version of BitwiseLut. Has identical API, so see `BitwiseLut`
137 for API documentation. This version may produce more efficient hardware.
140 def __init__(self
, input_count
, width
):
141 self
.input_count
= input_count
145 return Signal(width
, name
=f
"input{i}")
146 self
.inputs
= tuple(inp(i
) for i
in range(input_count
))
147 self
.output
= Signal(width
)
148 self
.lut
= Signal(2 ** input_count
)
149 self
._tree
_root
= _TreeMuxNode(
150 out
=self
.output
, container
=self
, parent
=None,
151 child0
=None, child1
=None, depth
=0)
152 self
._build
_tree
(self
._tree
_root
)
154 def _build_tree(self
, node
):
155 if node
.depth
< self
.input_count
:
156 self
._build
_tree
(node
.add_child(0))
157 self
._build
_tree
(node
.add_child(1))
159 def _elaborate_tree(self
, m
, node
):
160 if node
.depth
< self
.input_count
:
161 mux
= BitwiseMux(self
.width
)
162 setattr(m
.submodules
, "mux_" + node
.key_str
, mux
)
164 mux
.f
.eq(node
.child0
.out
),
165 mux
.t
.eq(node
.child1
.out
),
166 mux
.sel
.eq(self
.inputs
[node
.depth
]),
167 node
.out
.eq(mux
.output
),
169 self
._elaborate
_tree
(m
, node
.child0
)
170 self
._elaborate
_tree
(m
, node
.child1
)
172 index
= int(node
.key_str
, base
=2)
173 m
.d
.comb
+= node
.out
.eq(Repl(self
.lut
[index
], self
.width
))
175 def elaborate(self
, platform
):
177 self
._elaborate
_tree
(m
, self
._tree
_root
)
181 return [*self
.inputs
, self
.lut
, self
.output
]
184 # useful to see what is going on:
185 # python3 src/nmutil/test/test_lut.py
186 # yosys <<<"read_ilang sim_test_out/__main__.TestBitwiseLut.test_tree/0.il; proc;;; show top"