/home/runner/work/LabNeura/LabNeura/src/labneura/eda/mapping/generic/bit_graph.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef LABNEURA_EDA_BIT_GRAPH_H |
2 | | #define LABNEURA_EDA_BIT_GRAPH_H |
3 | | |
4 | | #include <algorithm> |
5 | | #include <cstddef> |
6 | | #include <cstdint> |
7 | | #include <map> |
8 | | #include <optional> |
9 | | #include <stdexcept> |
10 | | #include <string> |
11 | | #include <tuple> |
12 | | #include <utility> |
13 | | #include <vector> |
14 | | |
15 | | namespace labneura::eda::synth { |
16 | | |
17 | | using Bit = std::int32_t; |
18 | | using Word = std::vector<Bit>; // least-significant bit first |
19 | | |
20 | | constexpr Bit kZero = 0; |
21 | | constexpr Bit kOne = 1; |
22 | | constexpr Bit kLatch = 2; // value of a signal not assigned on every path |
23 | | |
24 | | enum class Op { Const, Latch, Input, Wire, Not, And, Or, Xor, Mux, Flop, LatchCell, Tri }; |
25 | | |
26 | | struct Node { |
27 | | Op op = Op::Const; |
28 | | Bit a = -1; // Not/And/Or/Xor: operands; Mux: a0; Wire: alias target; Flop: D |
29 | | Bit b = -1; // Mux: a1; Flop: clock |
30 | | Bit c = -1; // Mux: select; Flop: async reset (active high), -1 if none |
31 | | bool negedge = false; |
32 | | std::string name; |
33 | | bool reset_value = false; // Flop: value loaded while the async reset is active |
34 | | Bit d = -1; // Flop with both async controls: async set (active high); c is then the reset |
35 | | }; |
36 | | // LatchCell: a = D, b = enable (transparent while high). |
37 | | // Tri: a = data, b = enable; drives high-impedance while the enable is low. |
38 | | |
39 | | // Gate-level DAG with structural hashing and constant folding. |
40 | | class BitGraph { |
41 | | public: |
42 | 290 | BitGraph() { |
43 | 290 | nodes_.push_back({Op::Const, -1, -1, -1, false, "0"}); |
44 | 290 | nodes_.push_back({Op::Const, -1, -1, -1, false, "1"}); |
45 | 290 | nodes_.push_back({Op::Latch, -1, -1, -1, false, "latch"}); |
46 | 290 | tainted_.assign(3, 0); |
47 | 290 | tainted_[kLatch] = 1; |
48 | 290 | } |
49 | | |
50 | 300k | const Node& node(Bit id) const { return nodes_.at(static_cast<std::size_t>(id)); } |
51 | 740k | Node& node(Bit id) { return nodes_.at(static_cast<std::size_t>(id)); } |
52 | 276k | std::size_t size() const { return nodes_.size(); } |
53 | | |
54 | 17.1k | Bit input(const std::string& name) { |
55 | 17.1k | nodes_.push_back({Op::Input, -1, -1, -1, false, name}); |
56 | 17.1k | return last(); |
57 | 17.1k | } |
58 | 49.7k | Bit wire() { |
59 | 49.7k | nodes_.push_back({Op::Wire, -1, -1, -1, false, {}}); |
60 | 49.7k | return last(); |
61 | 49.7k | } |
62 | 6.56k | Bit flop(const std::string& name, bool negedge) { |
63 | 6.56k | nodes_.push_back({Op::Flop, -1, -1, -1, negedge, name}); |
64 | 6.56k | return last(); |
65 | 6.56k | } |
66 | | |
67 | 4 | Bit latch_cell(const std::string& name) { |
68 | 4 | nodes_.push_back({Op::LatchCell, -1, -1, -1, false, name, false}); |
69 | 4 | return last(); |
70 | 4 | } |
71 | | |
72 | 2 | Bit tri(Bit data, Bit enable) { |
73 | 2 | nodes_.push_back({Op::Tri, data, enable, -1, false, {}}); |
74 | 2 | return last(); |
75 | 2 | } |
76 | | |
77 | 68.5k | Bit make_not(Bit a) { |
78 | 68.5k | if (a == kZero) return kOne; |
79 | 58.8k | if (a == kOne) return kZero; |
80 | 50.7k | if (node(a).op == Op::Not) return node(a).a; |
81 | 38.5k | return hashed(Op::Not, a, -1, -1); |
82 | 50.7k | } |
83 | 170k | Bit make_and(Bit a, Bit b) { |
84 | 170k | if (a == kZero || b == kZero) return kZero; |
85 | 104k | if (a == kOne) return b; |
86 | 88.3k | if (b == kOne) return a; |
87 | 85.5k | if (a == b) return a; |
88 | 85.4k | if (is_complement(a, b)) return kZero; |
89 | 85.4k | if (a > b) std::swap(a, b); |
90 | 85.4k | return hashed(Op::And, a, b, -1); |
91 | 85.4k | } |
92 | 76.3k | Bit make_or(Bit a, Bit b) { |
93 | 76.3k | if (a == kOne || b == kOne) return kOne; |
94 | 68.3k | if (a == kZero) return b; |
95 | 31.3k | if (b == kZero) return a; |
96 | 30.7k | if (a == b) return a; |
97 | 30.7k | if (is_complement(a, b)) return kOne; |
98 | 30.6k | if (a > b) std::swap(a, b); |
99 | 30.6k | return hashed(Op::Or, a, b, -1); |
100 | 30.7k | } |
101 | 111k | Bit make_xor(Bit a, Bit b) { |
102 | 111k | if (a == kZero) return b; |
103 | 53.1k | if (b == kZero) return a; |
104 | 31.6k | if (a == kOne) return make_not(b); |
105 | 23.8k | if (b == kOne) return make_not(a); |
106 | 14.0k | if (a == b) return kZero; |
107 | 14.0k | if (is_complement(a, b)) return kOne; |
108 | 14.0k | if (a > b) std::swap(a, b); |
109 | 14.0k | return hashed(Op::Xor, a, b, -1); |
110 | 14.0k | } |
111 | | // select ? when_true : when_false |
112 | 188k | Bit make_mux(Bit select, Bit when_true, Bit when_false) { |
113 | 188k | if (select == kOne) return when_true; |
114 | 186k | if (select == kZero) return when_false; |
115 | 172k | if (when_true == when_false) return when_true; |
116 | | // A value that may be "unassigned" must keep its mux: folding a constant arm |
117 | | // away would lose the condition under which the signal is assigned. |
118 | 84.7k | if (is_tainted(when_true) || is_tainted(when_false)) { |
119 | 20.9k | return hashed(Op::Mux, when_false, when_true, select); |
120 | 20.9k | } |
121 | 63.8k | if (when_false == kZero && when_true == kOne) return select; |
122 | 63.0k | if (when_false == kOne && when_true == kZero) return make_not(select); |
123 | 62.7k | if (when_false == kZero) return make_and(select, when_true); |
124 | 59.4k | if (when_true == kZero) return make_and(make_not(select), when_false); |
125 | 47.4k | if (when_true == kOne) return make_or(select, when_false); |
126 | 45.3k | if (when_false == kOne) return make_or(make_not(select), when_true); |
127 | 45.2k | return hashed(Op::Mux, when_false, when_true, select); |
128 | 45.3k | } |
129 | | |
130 | | private: |
131 | | std::vector<Node> nodes_; |
132 | | std::vector<char> tainted_; // node depends on the unassigned-value marker |
133 | | std::map<std::tuple<int, Bit, Bit, Bit>, Bit> table_; |
134 | | |
135 | 577k | bool is_tainted(Bit id) const { |
136 | 577k | return id >= 0 && static_cast<std::size_t>(id) < tainted_.size() && tainted_[id]; |
137 | 577k | } |
138 | 362k | Bit last() const { return static_cast<Bit>(nodes_.size() - 1); } |
139 | 130k | bool is_complement(Bit a, Bit b) const { |
140 | 130k | return (node(a).op == Op::Not && node(a).a == b) || |
141 | 130k | (node(b).op == Op::Not && node(b).a == a); |
142 | 130k | } |
143 | 234k | Bit hashed(Op op, Bit a, Bit b, Bit c) { |
144 | 234k | const auto key = std::make_tuple(static_cast<int>(op), a, b, c); |
145 | 234k | const auto found = table_.find(key); |
146 | 234k | if (found != table_.end()) return found->second; |
147 | 144k | Node created; |
148 | 144k | created.op = op; |
149 | 144k | created.a = a; |
150 | 144k | created.b = b; |
151 | 144k | created.c = c; |
152 | 144k | const bool tainted = is_tainted(a) || is_tainted(b) || is_tainted(c); |
153 | 144k | nodes_.push_back(std::move(created)); |
154 | 144k | tainted_.resize(nodes_.size(), 0); |
155 | 144k | tainted_.back() = tainted; |
156 | 144k | table_.emplace(key, last()); |
157 | 144k | return last(); |
158 | 234k | } |
159 | | }; |
160 | | |
161 | 1.06k | inline Word constant_word(std::uint64_t value, std::size_t width) { |
162 | 1.06k | Word word(width); |
163 | 16.4k | for (std::size_t i = 0; i < width; ++i) { |
164 | 15.4k | word[i] = i < 64 && ((value >> i) & 1U) ? kOne : kZero; |
165 | 15.4k | } |
166 | 1.06k | return word; |
167 | 1.06k | } |
168 | | |
169 | 6.64k | inline std::optional<std::uint64_t> as_constant(const Word& word) { |
170 | 6.64k | std::uint64_t value = 0; |
171 | 218k | for (std::size_t i = 0; i < word.size(); ++i) { |
172 | 211k | if (word[i] == kOne) { |
173 | 13.6k | if (i >= 64) return std::nullopt; |
174 | 13.6k | value |= std::uint64_t{1} << i; |
175 | 198k | } else if (word[i] != kZero) { |
176 | 25 | return std::nullopt; |
177 | 25 | } |
178 | 211k | } |
179 | 6.61k | return value; |
180 | 6.64k | } |
181 | | |
182 | 46.6k | inline Word resize(const Word& word, std::size_t width, bool sign_extend) { |
183 | 46.6k | Word out(word.begin(), |
184 | 46.6k | word.begin() + static_cast<std::ptrdiff_t>(std::min(width, word.size()))); |
185 | 46.6k | const Bit fill = sign_extend && !word.empty() ? word.back() : kZero; |
186 | 46.6k | out.resize(width, fill); |
187 | 46.6k | return out; |
188 | 46.6k | } |
189 | | |
190 | | // Word-level operators built on top of a BitGraph. |
191 | | class WordOps { |
192 | | public: |
193 | 145 | explicit WordOps(BitGraph& graph) : g(graph) {} |
194 | | |
195 | 650 | Word bit_not(const Word& a) { |
196 | 650 | Word out(a.size()); |
197 | 9.59k | for (std::size_t i = 0; i < a.size(); ++i) out[i] = g.make_not(a[i]); |
198 | 650 | return out; |
199 | 650 | } |
200 | 1.18k | Word bitwise(char op, const Word& a, const Word& b) { |
201 | 1.18k | Word out(a.size()); |
202 | 4.27k | for (std::size_t i = 0; i < a.size(); ++i) { |
203 | 3.09k | out[i] = op == '&' ? g.make_and(a[i], b[i]) |
204 | 3.09k | : op == '|' ? g.make_or(a[i], b[i]) |
205 | 1.38k | : g.make_xor(a[i], b[i]); |
206 | 3.09k | } |
207 | 1.18k | return out; |
208 | 1.18k | } |
209 | 2.36k | Bit reduce_or(const Word& a) { |
210 | 2.36k | Bit result = kZero; |
211 | 2.61k | for (const Bit bit : a) result = g.make_or(result, bit); |
212 | 2.36k | return result; |
213 | 2.36k | } |
214 | 3 | Bit reduce_and(const Word& a) { |
215 | 3 | Bit result = kOne; |
216 | 6 | for (const Bit bit : a) result = g.make_and(result, bit); |
217 | 3 | return result; |
218 | 3 | } |
219 | 75 | Bit reduce_xor(const Word& a) { |
220 | 75 | Bit result = kZero; |
221 | 1.94k | for (const Bit bit : a) result = g.make_xor(result, bit); |
222 | 75 | return result; |
223 | 75 | } |
224 | 360 | Word mux(Bit select, const Word& when_true, const Word& when_false) { |
225 | 360 | Word out(when_true.size()); |
226 | 6.95k | for (std::size_t i = 0; i < out.size(); ++i) { |
227 | 6.59k | out[i] = g.make_mux(select, when_true[i], when_false[i]); |
228 | 6.59k | } |
229 | 360 | return out; |
230 | 360 | } |
231 | 1.11k | Word add(const Word& a, const Word& b, Bit carry_in = kZero) { |
232 | 1.11k | Word out(a.size()); |
233 | 1.11k | Bit carry = carry_in; |
234 | 33.9k | for (std::size_t i = 0; i < a.size(); ++i) { |
235 | 32.8k | const Bit half = g.make_xor(a[i], b[i]); |
236 | 32.8k | out[i] = g.make_xor(half, carry); |
237 | 32.8k | carry = g.make_or(g.make_and(a[i], b[i]), g.make_and(half, carry)); |
238 | 32.8k | } |
239 | 1.11k | return out; |
240 | 1.11k | } |
241 | 285 | Word sub(const Word& a, const Word& b) { return add(a, bit_not(b), kOne); } |
242 | | // Unsigned restoring division; division by zero yields all-ones / dividend. |
243 | 2 | std::pair<Word, Word> divmod_unsigned(const Word& a, const Word& b) { |
244 | 2 | const std::size_t n = a.size(); |
245 | 2 | Word divisor = resize(b, n + 1, false); |
246 | 2 | Word remainder(n + 1, kZero); |
247 | 2 | Word quotient(n, kZero); |
248 | 18 | for (std::size_t step = n; step-- > 0;) { |
249 | 16 | Word shifted(n + 1, kZero); |
250 | 16 | shifted[0] = a[step]; |
251 | 144 | for (std::size_t i = 1; i <= n; ++i) shifted[i] = remainder[i - 1]; |
252 | 16 | const Bit fits = g.make_not(less_than(shifted, divisor, false)); |
253 | 16 | remainder = mux(fits, sub(shifted, divisor), shifted); |
254 | 16 | quotient[step] = fits; |
255 | 16 | } |
256 | 2 | return {quotient, resize(remainder, n, false)}; |
257 | 2 | } |
258 | | // Signed division truncating toward zero; remainder takes the dividend's sign. |
259 | 2 | std::pair<Word, Word> divmod(const Word& a, const Word& b, bool is_signed) { |
260 | 2 | if (!is_signed || a.empty()) return divmod_unsigned(a, b); |
261 | 0 | const Bit sign_a = a.back(); |
262 | 0 | const Bit sign_b = b.empty() ? kZero : b.back(); |
263 | 0 | const Word abs_a = mux(sign_a, negate(a), a); |
264 | 0 | const Word abs_b = mux(sign_b, negate(b), b); |
265 | 0 | auto [q, r] = divmod_unsigned(abs_a, abs_b); |
266 | 0 | q = mux(g.make_xor(sign_a, sign_b), negate(q), q); |
267 | 0 | r = mux(sign_a, negate(r), r); |
268 | 0 | return {q, r}; |
269 | 2 | } |
270 | 12 | Word negate(const Word& a) { return sub(constant_word(0, a.size()), a); } |
271 | 14 | Word multiply(const Word& a, const Word& b) { |
272 | 14 | Word result = constant_word(0, a.size()); |
273 | 423 | for (std::size_t i = 0; i < b.size() && i < a.size(); ++i) { |
274 | 409 | Word partial(a.size(), kZero); |
275 | 7.12k | for (std::size_t j = 0; i + j < a.size(); ++j) { |
276 | 6.71k | partial[i + j] = g.make_and(a[j], b[i]); |
277 | 6.71k | } |
278 | 409 | result = add(result, partial); |
279 | 409 | } |
280 | 14 | return result; |
281 | 14 | } |
282 | 3.50k | Bit equal(const Word& a, const Word& b) { |
283 | 3.50k | Bit result = kOne; |
284 | 25.4k | for (std::size_t i = 0; i < a.size(); ++i) { |
285 | 21.9k | result = g.make_and(result, g.make_not(g.make_xor(a[i], b[i]))); |
286 | 21.9k | } |
287 | 3.50k | return result; |
288 | 3.50k | } |
289 | | // a < b |
290 | 474 | Bit less_than(const Word& a, const Word& b, bool is_signed) { |
291 | 474 | Bit less = kZero; |
292 | 14.6k | for (std::size_t i = 0; i < a.size(); ++i) { |
293 | 14.2k | const bool top = i + 1 == a.size() && is_signed; |
294 | 14.2k | const Bit ai = top ? b[i] : a[i]; |
295 | 14.2k | const Bit bi = top ? a[i] : b[i]; |
296 | 14.2k | const Bit differ = g.make_xor(ai, bi); |
297 | 14.2k | less = g.make_mux(differ, bi, less); |
298 | 14.2k | } |
299 | 474 | return less; |
300 | 474 | } |
301 | | enum class Shift { Left, LogicalRight, ArithmeticRight }; |
302 | 7 | Word shift(const Word& a, const Word& amount, Shift kind) { |
303 | 7 | const Bit fill = kind == Shift::ArithmeticRight && !a.empty() ? a.back() : kZero; |
304 | 7 | Word current = a; |
305 | 7 | Bit overflow = kZero; |
306 | 42 | for (std::size_t level = 0; level < amount.size(); ++level) { |
307 | 35 | if (level >= 62 || (std::uint64_t{1} << level) >= a.size()) { |
308 | 0 | overflow = g.make_or(overflow, amount[level]); |
309 | 0 | continue; |
310 | 0 | } |
311 | 35 | const std::size_t step = std::size_t{1} << level; |
312 | 35 | Word shifted(a.size()); |
313 | 1.79k | for (std::size_t i = 0; i < a.size(); ++i) { |
314 | 1.76k | if (kind == Shift::Left) { |
315 | 480 | shifted[i] = i >= step ? current[i - step] : kZero; |
316 | 1.28k | } else { |
317 | 1.28k | shifted[i] = i + step < a.size() ? current[i + step] : fill; |
318 | 1.28k | } |
319 | 1.76k | } |
320 | 35 | current = mux(amount[level], shifted, current); |
321 | 35 | } |
322 | 7 | return mux(overflow, Word(a.size(), kind == Shift::Left ? kZero : fill), |
323 | 7 | current); |
324 | 7 | } |
325 | | // options[k] selected when index == k; out-of-range yields zeros. |
326 | | Word select(const std::vector<Word>& options, const Word& index, |
327 | 0 | std::size_t width) { |
328 | 0 | std::vector<Word> level = options; |
329 | 0 | Bit overflow = kZero; |
330 | 0 | std::size_t used = 0; |
331 | 0 | while ((std::size_t{1} << used) < std::max<std::size_t>(level.size(), 1)) ++used; |
332 | 0 | for (std::size_t bit = 0; bit < index.size(); ++bit) { |
333 | 0 | if (bit >= used) { |
334 | 0 | overflow = g.make_or(overflow, index[bit]); |
335 | 0 | continue; |
336 | 0 | } |
337 | 0 | std::vector<Word> next; |
338 | 0 | for (std::size_t i = 0; i < level.size(); i += 2) { |
339 | 0 | const Word& low = level[i]; |
340 | 0 | const Word high = i + 1 < level.size() |
341 | 0 | ? level[i + 1] |
342 | 0 | : constant_word(0, width); |
343 | 0 | next.push_back(mux(index[bit], high, low)); |
344 | 0 | } |
345 | 0 | level = std::move(next); |
346 | 0 | } |
347 | 0 | Word result = level.empty() ? constant_word(0, width) : level.front(); |
348 | 0 | if (used > index.size()) { |
349 | 0 | // Not every option is addressable; remaining ones are unreachable. |
350 | 0 | } |
351 | 0 | return mux(overflow, constant_word(0, width), result); |
352 | 0 | } |
353 | | |
354 | | private: |
355 | | BitGraph& g; |
356 | | }; |
357 | | |
358 | | } // namespace labneura::eda::synth |
359 | | |
360 | | #endif |