cxxrtl: always inline internal cells and slice/concat operations.
[yosys.git] / backends / cxxrtl / cxxrtl.h
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2019-2020 whitequark <whitequark@whitequark.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 */
18
19 // This file is included by the designs generated with `write_cxxrtl`. It is not used in Yosys itself.
20 //
21 // The CXXRTL support library implements compile time specialized arbitrary width arithmetics, as well as provides
22 // composite lvalues made out of bit slices and concatenations of lvalues. This allows the `write_cxxrtl` pass
23 // to perform a straightforward translation of RTLIL structures to readable C++, relying on the C++ compiler
24 // to unwrap the abstraction and generate efficient code.
25
26 #ifndef CXXRTL_H
27 #define CXXRTL_H
28
29 #include <cstddef>
30 #include <cstdint>
31 #include <cassert>
32 #include <limits>
33 #include <type_traits>
34 #include <tuple>
35 #include <vector>
36 #include <map>
37 #include <algorithm>
38 #include <memory>
39 #include <sstream>
40
41 #include <backends/cxxrtl/cxxrtl_capi.h>
42
43 // CXXRTL essentially uses the C++ compiler as a hygienic macro engine that feeds an instruction selector.
44 // It generates a lot of specialized template functions with relatively large bodies that, when inlined
45 // into the caller and (for those with loops) unrolled, often expose many new optimization opportunities.
46 // Because of this, most of the CXXRTL runtime must be always inlined for best performance.
47 #ifndef __has_attribute
48 # define __has_attribute(x) 0
49 #endif
50 #if __has_attribute(always_inline)
51 #define CXXRTL_ALWAYS_INLINE inline __attribute__((__always_inline__))
52 #else
53 #define CXXRTL_ALWAYS_INLINE inline
54 #endif
55
56 namespace cxxrtl {
57
58 // All arbitrary-width values in CXXRTL are backed by arrays of unsigned integers called chunks. The chunk size
59 // is the same regardless of the value width to simplify manipulating values via FFI interfaces, e.g. driving
60 // and introspecting the simulation in Python.
61 //
62 // It is practical to use chunk sizes between 32 bits and platform register size because when arithmetics on
63 // narrower integer types is legalized by the C++ compiler, it inserts code to clear the high bits of the register.
64 // However, (a) most of our operations do not change those bits in the first place because of invariants that are
65 // invisible to the compiler, (b) we often operate on non-power-of-2 values and have to clear the high bits anyway.
66 // Therefore, using relatively wide chunks and clearing the high bits explicitly and only when we know they may be
67 // clobbered results in simpler generated code.
68 typedef uint32_t chunk_t;
69
70 template<typename T>
71 struct chunk_traits {
72 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
73 "chunk type must be an unsigned integral type");
74 using type = T;
75 static constexpr size_t bits = std::numeric_limits<T>::digits;
76 static constexpr T mask = std::numeric_limits<T>::max();
77 };
78
79 template<class T>
80 struct expr_base;
81
82 template<size_t Bits>
83 struct value : public expr_base<value<Bits>> {
84 static constexpr size_t bits = Bits;
85
86 using chunk = chunk_traits<chunk_t>;
87 static constexpr chunk::type msb_mask = (Bits % chunk::bits == 0) ? chunk::mask
88 : chunk::mask >> (chunk::bits - (Bits % chunk::bits));
89
90 static constexpr size_t chunks = (Bits + chunk::bits - 1) / chunk::bits;
91 chunk::type data[chunks] = {};
92
93 value() = default;
94 template<typename... Init>
95 explicit constexpr value(Init ...init) : data{init...} {}
96
97 value(const value<Bits> &) = default;
98 value(value<Bits> &&) = default;
99 value<Bits> &operator=(const value<Bits> &) = default;
100
101 // A (no-op) helper that forces the cast to value<>.
102 CXXRTL_ALWAYS_INLINE
103 const value<Bits> &val() const {
104 return *this;
105 }
106
107 std::string str() const {
108 std::stringstream ss;
109 ss << *this;
110 return ss.str();
111 }
112
113 // Operations with compile-time parameters.
114 //
115 // These operations are used to implement slicing, concatenation, and blitting.
116 // The trunc, zext and sext operations add or remove most significant bits (i.e. on the left);
117 // the rtrunc and rzext operations add or remove least significant bits (i.e. on the right).
118 template<size_t NewBits>
119 CXXRTL_ALWAYS_INLINE
120 value<NewBits> trunc() const {
121 static_assert(NewBits <= Bits, "trunc() may not increase width");
122 value<NewBits> result;
123 for (size_t n = 0; n < result.chunks; n++)
124 result.data[n] = data[n];
125 result.data[result.chunks - 1] &= result.msb_mask;
126 return result;
127 }
128
129 template<size_t NewBits>
130 CXXRTL_ALWAYS_INLINE
131 value<NewBits> zext() const {
132 static_assert(NewBits >= Bits, "zext() may not decrease width");
133 value<NewBits> result;
134 for (size_t n = 0; n < chunks; n++)
135 result.data[n] = data[n];
136 return result;
137 }
138
139 template<size_t NewBits>
140 CXXRTL_ALWAYS_INLINE
141 value<NewBits> sext() const {
142 static_assert(NewBits >= Bits, "sext() may not decrease width");
143 value<NewBits> result;
144 for (size_t n = 0; n < chunks; n++)
145 result.data[n] = data[n];
146 if (is_neg()) {
147 result.data[chunks - 1] |= ~msb_mask;
148 for (size_t n = chunks; n < result.chunks; n++)
149 result.data[n] = chunk::mask;
150 result.data[result.chunks - 1] &= result.msb_mask;
151 }
152 return result;
153 }
154
155 template<size_t NewBits>
156 CXXRTL_ALWAYS_INLINE
157 value<NewBits> rtrunc() const {
158 static_assert(NewBits <= Bits, "rtrunc() may not increase width");
159 value<NewBits> result;
160 constexpr size_t shift_chunks = (Bits - NewBits) / chunk::bits;
161 constexpr size_t shift_bits = (Bits - NewBits) % chunk::bits;
162 chunk::type carry = 0;
163 if (shift_chunks + result.chunks < chunks) {
164 carry = (shift_bits == 0) ? 0
165 : data[shift_chunks + result.chunks] << (chunk::bits - shift_bits);
166 }
167 for (size_t n = result.chunks; n > 0; n--) {
168 result.data[n - 1] = carry | (data[shift_chunks + n - 1] >> shift_bits);
169 carry = (shift_bits == 0) ? 0
170 : data[shift_chunks + n - 1] << (chunk::bits - shift_bits);
171 }
172 return result;
173 }
174
175 template<size_t NewBits>
176 CXXRTL_ALWAYS_INLINE
177 value<NewBits> rzext() const {
178 static_assert(NewBits >= Bits, "rzext() may not decrease width");
179 value<NewBits> result;
180 constexpr size_t shift_chunks = (NewBits - Bits) / chunk::bits;
181 constexpr size_t shift_bits = (NewBits - Bits) % chunk::bits;
182 chunk::type carry = 0;
183 for (size_t n = 0; n < chunks; n++) {
184 result.data[shift_chunks + n] = (data[n] << shift_bits) | carry;
185 carry = (shift_bits == 0) ? 0
186 : data[n] >> (chunk::bits - shift_bits);
187 }
188 if (carry != 0)
189 result.data[result.chunks - 1] = carry;
190 return result;
191 }
192
193 // Bit blit operation, i.e. a partial read-modify-write.
194 template<size_t Stop, size_t Start>
195 CXXRTL_ALWAYS_INLINE
196 value<Bits> blit(const value<Stop - Start + 1> &source) const {
197 static_assert(Stop >= Start, "blit() may not reverse bit order");
198 constexpr chunk::type start_mask = ~(chunk::mask << (Start % chunk::bits));
199 constexpr chunk::type stop_mask = (Stop % chunk::bits + 1 == chunk::bits) ? 0
200 : (chunk::mask << (Stop % chunk::bits + 1));
201 value<Bits> masked = *this;
202 if (Start / chunk::bits == Stop / chunk::bits) {
203 masked.data[Start / chunk::bits] &= stop_mask | start_mask;
204 } else {
205 masked.data[Start / chunk::bits] &= start_mask;
206 for (size_t n = Start / chunk::bits + 1; n < Stop / chunk::bits; n++)
207 masked.data[n] = 0;
208 masked.data[Stop / chunk::bits] &= stop_mask;
209 }
210 value<Bits> shifted = source
211 .template rzext<Stop + 1>()
212 .template zext<Bits>();
213 return masked.bit_or(shifted);
214 }
215
216 // Helpers for selecting extending or truncating operation depending on whether the result is wider or narrower
217 // than the operand. In C++17 these can be replaced with `if constexpr`.
218 template<size_t NewBits, typename = void>
219 struct zext_cast {
220 CXXRTL_ALWAYS_INLINE
221 value<NewBits> operator()(const value<Bits> &val) {
222 return val.template zext<NewBits>();
223 }
224 };
225
226 template<size_t NewBits>
227 struct zext_cast<NewBits, typename std::enable_if<(NewBits < Bits)>::type> {
228 CXXRTL_ALWAYS_INLINE
229 value<NewBits> operator()(const value<Bits> &val) {
230 return val.template trunc<NewBits>();
231 }
232 };
233
234 template<size_t NewBits, typename = void>
235 struct sext_cast {
236 CXXRTL_ALWAYS_INLINE
237 value<NewBits> operator()(const value<Bits> &val) {
238 return val.template sext<NewBits>();
239 }
240 };
241
242 template<size_t NewBits>
243 struct sext_cast<NewBits, typename std::enable_if<(NewBits < Bits)>::type> {
244 CXXRTL_ALWAYS_INLINE
245 value<NewBits> operator()(const value<Bits> &val) {
246 return val.template trunc<NewBits>();
247 }
248 };
249
250 template<size_t NewBits>
251 CXXRTL_ALWAYS_INLINE
252 value<NewBits> zcast() const {
253 return zext_cast<NewBits>()(*this);
254 }
255
256 template<size_t NewBits>
257 CXXRTL_ALWAYS_INLINE
258 value<NewBits> scast() const {
259 return sext_cast<NewBits>()(*this);
260 }
261
262 // Operations with run-time parameters (offsets, amounts, etc).
263 //
264 // These operations are used for computations.
265 bool bit(size_t offset) const {
266 return data[offset / chunk::bits] & (1 << (offset % chunk::bits));
267 }
268
269 void set_bit(size_t offset, bool value = true) {
270 size_t offset_chunks = offset / chunk::bits;
271 size_t offset_bits = offset % chunk::bits;
272 data[offset_chunks] &= ~(1 << offset_bits);
273 data[offset_chunks] |= value ? 1 << offset_bits : 0;
274 }
275
276 bool is_zero() const {
277 for (size_t n = 0; n < chunks; n++)
278 if (data[n] != 0)
279 return false;
280 return true;
281 }
282
283 explicit operator bool() const {
284 return !is_zero();
285 }
286
287 bool is_neg() const {
288 return data[chunks - 1] & (1 << ((Bits - 1) % chunk::bits));
289 }
290
291 bool operator ==(const value<Bits> &other) const {
292 for (size_t n = 0; n < chunks; n++)
293 if (data[n] != other.data[n])
294 return false;
295 return true;
296 }
297
298 bool operator !=(const value<Bits> &other) const {
299 return !(*this == other);
300 }
301
302 value<Bits> bit_not() const {
303 value<Bits> result;
304 for (size_t n = 0; n < chunks; n++)
305 result.data[n] = ~data[n];
306 result.data[chunks - 1] &= msb_mask;
307 return result;
308 }
309
310 value<Bits> bit_and(const value<Bits> &other) const {
311 value<Bits> result;
312 for (size_t n = 0; n < chunks; n++)
313 result.data[n] = data[n] & other.data[n];
314 return result;
315 }
316
317 value<Bits> bit_or(const value<Bits> &other) const {
318 value<Bits> result;
319 for (size_t n = 0; n < chunks; n++)
320 result.data[n] = data[n] | other.data[n];
321 return result;
322 }
323
324 value<Bits> bit_xor(const value<Bits> &other) const {
325 value<Bits> result;
326 for (size_t n = 0; n < chunks; n++)
327 result.data[n] = data[n] ^ other.data[n];
328 return result;
329 }
330
331 value<Bits> update(const value<Bits> &val, const value<Bits> &mask) const {
332 return bit_and(mask.bit_not()).bit_or(val.bit_and(mask));
333 }
334
335 template<size_t AmountBits>
336 value<Bits> shl(const value<AmountBits> &amount) const {
337 // Ensure our early return is correct by prohibiting values larger than 4 Gbit.
338 static_assert(Bits <= chunk::mask, "shl() of unreasonably large values is not supported");
339 // Detect shifts definitely large than Bits early.
340 for (size_t n = 1; n < amount.chunks; n++)
341 if (amount.data[n] != 0)
342 return {};
343 // Past this point we can use the least significant chunk as the shift size.
344 size_t shift_chunks = amount.data[0] / chunk::bits;
345 size_t shift_bits = amount.data[0] % chunk::bits;
346 if (shift_chunks >= chunks)
347 return {};
348 value<Bits> result;
349 chunk::type carry = 0;
350 for (size_t n = 0; n < chunks - shift_chunks; n++) {
351 result.data[shift_chunks + n] = (data[n] << shift_bits) | carry;
352 carry = (shift_bits == 0) ? 0
353 : data[n] >> (chunk::bits - shift_bits);
354 }
355 return result;
356 }
357
358 template<size_t AmountBits, bool Signed = false>
359 value<Bits> shr(const value<AmountBits> &amount) const {
360 // Ensure our early return is correct by prohibiting values larger than 4 Gbit.
361 static_assert(Bits <= chunk::mask, "shr() of unreasonably large values is not supported");
362 // Detect shifts definitely large than Bits early.
363 for (size_t n = 1; n < amount.chunks; n++)
364 if (amount.data[n] != 0)
365 return {};
366 // Past this point we can use the least significant chunk as the shift size.
367 size_t shift_chunks = amount.data[0] / chunk::bits;
368 size_t shift_bits = amount.data[0] % chunk::bits;
369 if (shift_chunks >= chunks)
370 return {};
371 value<Bits> result;
372 chunk::type carry = 0;
373 for (size_t n = 0; n < chunks - shift_chunks; n++) {
374 result.data[chunks - shift_chunks - 1 - n] = carry | (data[chunks - 1 - n] >> shift_bits);
375 carry = (shift_bits == 0) ? 0
376 : data[chunks - 1 - n] << (chunk::bits - shift_bits);
377 }
378 if (Signed && is_neg()) {
379 for (size_t n = chunks - shift_chunks; n < chunks; n++)
380 result.data[n] = chunk::mask;
381 if (shift_bits != 0)
382 result.data[chunks - shift_chunks] |= chunk::mask << (chunk::bits - shift_bits);
383 }
384 return result;
385 }
386
387 template<size_t AmountBits>
388 value<Bits> sshr(const value<AmountBits> &amount) const {
389 return shr<AmountBits, /*Signed=*/true>(amount);
390 }
391
392 size_t ctpop() const {
393 size_t count = 0;
394 for (size_t n = 0; n < chunks; n++) {
395 // This loop implements the population count idiom as recognized by LLVM and GCC.
396 for (chunk::type x = data[n]; x != 0; count++)
397 x = x & (x - 1);
398 }
399 return count;
400 }
401
402 size_t ctlz() const {
403 size_t count = 0;
404 for (size_t n = 0; n < chunks; n++) {
405 chunk::type x = data[chunks - 1 - n];
406 if (x == 0) {
407 count += (n == 0 ? Bits % chunk::bits : chunk::bits);
408 } else {
409 // This loop implements the find first set idiom as recognized by LLVM.
410 for (; x != 0; count++)
411 x >>= 1;
412 }
413 }
414 return count;
415 }
416
417 template<bool Invert, bool CarryIn>
418 std::pair<value<Bits>, bool /*CarryOut*/> alu(const value<Bits> &other) const {
419 value<Bits> result;
420 bool carry = CarryIn;
421 for (size_t n = 0; n < result.chunks; n++) {
422 result.data[n] = data[n] + (Invert ? ~other.data[n] : other.data[n]) + carry;
423 carry = (result.data[n] < data[n]) ||
424 (result.data[n] == data[n] && carry);
425 }
426 result.data[result.chunks - 1] &= result.msb_mask;
427 return {result, carry};
428 }
429
430 value<Bits> add(const value<Bits> &other) const {
431 return alu</*Invert=*/false, /*CarryIn=*/false>(other).first;
432 }
433
434 value<Bits> sub(const value<Bits> &other) const {
435 return alu</*Invert=*/true, /*CarryIn=*/true>(other).first;
436 }
437
438 value<Bits> neg() const {
439 return value<Bits> { 0u }.sub(*this);
440 }
441
442 bool ucmp(const value<Bits> &other) const {
443 bool carry;
444 std::tie(std::ignore, carry) = alu</*Invert=*/true, /*CarryIn=*/true>(other);
445 return !carry; // a.ucmp(b) ≡ a u< b
446 }
447
448 bool scmp(const value<Bits> &other) const {
449 value<Bits> result;
450 bool carry;
451 std::tie(result, carry) = alu</*Invert=*/true, /*CarryIn=*/true>(other);
452 bool overflow = (is_neg() == !other.is_neg()) && (is_neg() != result.is_neg());
453 return result.is_neg() ^ overflow; // a.scmp(b) ≡ a s< b
454 }
455 };
456
457 // Expression template for a slice, usable as lvalue or rvalue, and composable with other expression templates here.
458 template<class T, size_t Stop, size_t Start>
459 struct slice_expr : public expr_base<slice_expr<T, Stop, Start>> {
460 static_assert(Stop >= Start, "slice_expr() may not reverse bit order");
461 static_assert(Start < T::bits && Stop < T::bits, "slice_expr() must be within bounds");
462 static constexpr size_t bits = Stop - Start + 1;
463
464 T &expr;
465
466 slice_expr(T &expr) : expr(expr) {}
467 slice_expr(const slice_expr<T, Stop, Start> &) = delete;
468
469 CXXRTL_ALWAYS_INLINE
470 operator value<bits>() const {
471 return static_cast<const value<T::bits> &>(expr)
472 .template rtrunc<T::bits - Start>()
473 .template trunc<bits>();
474 }
475
476 CXXRTL_ALWAYS_INLINE
477 slice_expr<T, Stop, Start> &operator=(const value<bits> &rhs) {
478 // Generic partial assignment implemented using a read-modify-write operation on the sliced expression.
479 expr = static_cast<const value<T::bits> &>(expr)
480 .template blit<Stop, Start>(rhs);
481 return *this;
482 }
483
484 // A helper that forces the cast to value<>, which allows deduction to work.
485 CXXRTL_ALWAYS_INLINE
486 value<bits> val() const {
487 return static_cast<const value<bits> &>(*this);
488 }
489 };
490
491 // Expression template for a concatenation, usable as lvalue or rvalue, and composable with other expression templates here.
492 template<class T, class U>
493 struct concat_expr : public expr_base<concat_expr<T, U>> {
494 static constexpr size_t bits = T::bits + U::bits;
495
496 T &ms_expr;
497 U &ls_expr;
498
499 concat_expr(T &ms_expr, U &ls_expr) : ms_expr(ms_expr), ls_expr(ls_expr) {}
500 concat_expr(const concat_expr<T, U> &) = delete;
501
502 CXXRTL_ALWAYS_INLINE
503 operator value<bits>() const {
504 value<bits> ms_shifted = static_cast<const value<T::bits> &>(ms_expr)
505 .template rzext<bits>();
506 value<bits> ls_extended = static_cast<const value<U::bits> &>(ls_expr)
507 .template zext<bits>();
508 return ms_shifted.bit_or(ls_extended);
509 }
510
511 CXXRTL_ALWAYS_INLINE
512 concat_expr<T, U> &operator=(const value<bits> &rhs) {
513 ms_expr = rhs.template rtrunc<T::bits>();
514 ls_expr = rhs.template trunc<U::bits>();
515 return *this;
516 }
517
518 // A helper that forces the cast to value<>, which allows deduction to work.
519 CXXRTL_ALWAYS_INLINE
520 value<bits> val() const {
521 return static_cast<const value<bits> &>(*this);
522 }
523 };
524
525 // Base class for expression templates, providing helper methods for operations that are valid on both rvalues and lvalues.
526 //
527 // Note that expression objects (slices and concatenations) constructed in this way should NEVER be captured because
528 // they refer to temporaries that will, in general, only live until the end of the statement. For example, both of
529 // these snippets perform use-after-free:
530 //
531 // const auto &a = val.slice<7,0>().slice<1>();
532 // value<1> b = a;
533 //
534 // auto &&c = val.slice<7,0>().slice<1>();
535 // c = value<1>{1u};
536 //
537 // An easy way to write code using slices and concatenations safely is to follow two simple rules:
538 // * Never explicitly name any type except `value<W>` or `const value<W> &`.
539 // * Never use a `const auto &` or `auto &&` in any such expression.
540 // Then, any code that compiles will be well-defined.
541 template<class T>
542 struct expr_base {
543 template<size_t Stop, size_t Start = Stop>
544 CXXRTL_ALWAYS_INLINE
545 slice_expr<const T, Stop, Start> slice() const {
546 return {*static_cast<const T *>(this)};
547 }
548
549 template<size_t Stop, size_t Start = Stop>
550 CXXRTL_ALWAYS_INLINE
551 slice_expr<T, Stop, Start> slice() {
552 return {*static_cast<T *>(this)};
553 }
554
555 template<class U>
556 CXXRTL_ALWAYS_INLINE
557 concat_expr<const T, typename std::remove_reference<const U>::type> concat(const U &other) const {
558 return {*static_cast<const T *>(this), other};
559 }
560
561 template<class U>
562 CXXRTL_ALWAYS_INLINE
563 concat_expr<T, typename std::remove_reference<U>::type> concat(U &&other) {
564 return {*static_cast<T *>(this), other};
565 }
566 };
567
568 template<size_t Bits>
569 std::ostream &operator<<(std::ostream &os, const value<Bits> &val) {
570 auto old_flags = os.flags(std::ios::right);
571 auto old_width = os.width(0);
572 auto old_fill = os.fill('0');
573 os << val.bits << '\'' << std::hex;
574 for (size_t n = val.chunks - 1; n != (size_t)-1; n--) {
575 if (n == val.chunks - 1 && Bits % value<Bits>::chunk::bits != 0)
576 os.width((Bits % value<Bits>::chunk::bits + 3) / 4);
577 else
578 os.width((value<Bits>::chunk::bits + 3) / 4);
579 os << val.data[n];
580 }
581 os.fill(old_fill);
582 os.width(old_width);
583 os.flags(old_flags);
584 return os;
585 }
586
587 template<size_t Bits>
588 struct wire {
589 static constexpr size_t bits = Bits;
590
591 value<Bits> curr;
592 value<Bits> next;
593
594 wire() = default;
595 constexpr wire(const value<Bits> &init) : curr(init), next(init) {}
596 template<typename... Init>
597 explicit constexpr wire(Init ...init) : curr{init...}, next{init...} {}
598
599 wire(const wire<Bits> &) = delete;
600 wire(wire<Bits> &&) = default;
601 wire<Bits> &operator=(const wire<Bits> &) = delete;
602
603 bool commit() {
604 if (curr != next) {
605 curr = next;
606 return true;
607 }
608 return false;
609 }
610 };
611
612 template<size_t Bits>
613 std::ostream &operator<<(std::ostream &os, const wire<Bits> &val) {
614 os << val.curr;
615 return os;
616 }
617
618 template<size_t Width>
619 struct memory {
620 std::vector<value<Width>> data;
621
622 size_t depth() const {
623 return data.size();
624 }
625
626 memory() = delete;
627 explicit memory(size_t depth) : data(depth) {}
628
629 memory(const memory<Width> &) = delete;
630 memory<Width> &operator=(const memory<Width> &) = delete;
631
632 // The only way to get the compiler to put the initializer in .rodata and do not copy it on stack is to stuff it
633 // into a plain array. You'd think an std::initializer_list would work here, but it doesn't, because you can't
634 // construct an initializer_list in a constexpr (or something) and so if you try to do that the whole thing is
635 // first copied on the stack (probably overflowing it) and then again into `data`.
636 template<size_t Size>
637 struct init {
638 size_t offset;
639 value<Width> data[Size];
640 };
641
642 template<size_t... InitSize>
643 explicit memory(size_t depth, const init<InitSize> &...init) : data(depth) {
644 data.resize(depth);
645 // This utterly reprehensible construct is the most reasonable way to apply a function to every element
646 // of a parameter pack, if the elements all have different types and so cannot be cast to an initializer list.
647 auto _ = {std::move(std::begin(init.data), std::end(init.data), data.begin() + init.offset)...};
648 (void)_;
649 }
650
651 // An operator for direct memory reads. May be used at any time during the simulation.
652 const value<Width> &operator [](size_t index) const {
653 assert(index < data.size());
654 return data[index];
655 }
656
657 // An operator for direct memory writes. May only be used before the simulation is started. If used
658 // after the simulation is started, the design may malfunction.
659 value<Width> &operator [](size_t index) {
660 assert(index < data.size());
661 return data[index];
662 }
663
664 // A simple way to make a writable memory would be to use an array of wires instead of an array of values.
665 // However, there are two significant downsides to this approach: first, it has large overhead (2× space
666 // overhead, and O(depth) time overhead during commit); second, it does not simplify handling write port
667 // priorities. Although in principle write ports could be ordered or conditionally enabled in generated
668 // code based on their priorities and selected addresses, the feedback arc set problem is computationally
669 // expensive, and the heuristic based algorithms are not easily modified to guarantee (rather than prefer)
670 // a particular write port evaluation order.
671 //
672 // The approach used here instead is to queue writes into a buffer during the eval phase, then perform
673 // the writes during the commit phase in the priority order. This approach has low overhead, with both space
674 // and time proportional to the amount of write ports. Because virtually every memory in a practical design
675 // has at most two write ports, linear search is used on every write, being the fastest and simplest approach.
676 struct write {
677 size_t index;
678 value<Width> val;
679 value<Width> mask;
680 int priority;
681 };
682 std::vector<write> write_queue;
683
684 void update(size_t index, const value<Width> &val, const value<Width> &mask, int priority = 0) {
685 assert(index < data.size());
686 // Queue up the write while keeping the queue sorted by priority.
687 write_queue.insert(
688 std::upper_bound(write_queue.begin(), write_queue.end(), priority,
689 [](const int a, const write& b) { return a < b.priority; }),
690 write { index, val, mask, priority });
691 }
692
693 bool commit() {
694 bool changed = false;
695 for (const write &entry : write_queue) {
696 value<Width> elem = data[entry.index];
697 elem = elem.update(entry.val, entry.mask);
698 changed |= (data[entry.index] != elem);
699 data[entry.index] = elem;
700 }
701 write_queue.clear();
702 return changed;
703 }
704 };
705
706 struct metadata {
707 const enum {
708 MISSING = 0,
709 UINT = 1,
710 SINT = 2,
711 STRING = 3,
712 DOUBLE = 4,
713 } value_type;
714
715 // In debug mode, using the wrong .as_*() function will assert.
716 // In release mode, using the wrong .as_*() function will safely return a default value.
717 const unsigned uint_value = 0;
718 const signed sint_value = 0;
719 const std::string string_value = "";
720 const double double_value = 0.0;
721
722 metadata() : value_type(MISSING) {}
723 metadata(unsigned value) : value_type(UINT), uint_value(value) {}
724 metadata(signed value) : value_type(SINT), sint_value(value) {}
725 metadata(const std::string &value) : value_type(STRING), string_value(value) {}
726 metadata(const char *value) : value_type(STRING), string_value(value) {}
727 metadata(double value) : value_type(DOUBLE), double_value(value) {}
728
729 metadata(const metadata &) = default;
730 metadata &operator=(const metadata &) = delete;
731
732 unsigned as_uint() const {
733 assert(value_type == UINT);
734 return uint_value;
735 }
736
737 signed as_sint() const {
738 assert(value_type == SINT);
739 return sint_value;
740 }
741
742 const std::string &as_string() const {
743 assert(value_type == STRING);
744 return string_value;
745 }
746
747 double as_double() const {
748 assert(value_type == DOUBLE);
749 return double_value;
750 }
751 };
752
753 typedef std::map<std::string, metadata> metadata_map;
754
755 // Helper class to disambiguate values/wires and their aliases.
756 struct debug_alias {};
757
758 // This structure is intended for consumption via foreign function interfaces, like Python's ctypes.
759 // Because of this it uses a C-style layout that is easy to parse rather than more idiomatic C++.
760 //
761 // To avoid violating strict aliasing rules, this structure has to be a subclass of the one used
762 // in the C API, or it would not be possible to cast between the pointers to these.
763 struct debug_item : ::cxxrtl_object {
764 enum : uint32_t {
765 VALUE = CXXRTL_VALUE,
766 WIRE = CXXRTL_WIRE,
767 MEMORY = CXXRTL_MEMORY,
768 ALIAS = CXXRTL_ALIAS,
769 };
770
771 debug_item(const ::cxxrtl_object &object) : cxxrtl_object(object) {}
772
773 template<size_t Bits>
774 debug_item(value<Bits> &item) {
775 static_assert(sizeof(item) == value<Bits>::chunks * sizeof(chunk_t),
776 "value<Bits> is not compatible with C layout");
777 type = VALUE;
778 width = Bits;
779 depth = 1;
780 curr = item.data;
781 next = item.data;
782 }
783
784 template<size_t Bits>
785 debug_item(const value<Bits> &item) {
786 static_assert(sizeof(item) == value<Bits>::chunks * sizeof(chunk_t),
787 "value<Bits> is not compatible with C layout");
788 type = VALUE;
789 width = Bits;
790 depth = 1;
791 curr = const_cast<chunk_t*>(item.data);
792 next = nullptr;
793 }
794
795 template<size_t Bits>
796 debug_item(wire<Bits> &item) {
797 static_assert(sizeof(item.curr) == value<Bits>::chunks * sizeof(chunk_t) &&
798 sizeof(item.next) == value<Bits>::chunks * sizeof(chunk_t),
799 "wire<Bits> is not compatible with C layout");
800 type = WIRE;
801 width = Bits;
802 depth = 1;
803 curr = item.curr.data;
804 next = item.next.data;
805 }
806
807 template<size_t Width>
808 debug_item(memory<Width> &item) {
809 static_assert(sizeof(item.data[0]) == value<Width>::chunks * sizeof(chunk_t),
810 "memory<Width> is not compatible with C layout");
811 type = MEMORY;
812 width = Width;
813 depth = item.data.size();
814 curr = item.data.empty() ? nullptr : item.data[0].data;
815 next = nullptr;
816 }
817
818 template<size_t Bits>
819 debug_item(debug_alias, const value<Bits> &item) {
820 static_assert(sizeof(item) == value<Bits>::chunks * sizeof(chunk_t),
821 "value<Bits> is not compatible with C layout");
822 type = ALIAS;
823 width = Bits;
824 depth = 1;
825 curr = const_cast<chunk_t*>(item.data);
826 next = nullptr;
827 }
828
829 template<size_t Bits>
830 debug_item(debug_alias, const wire<Bits> &item) {
831 static_assert(sizeof(item.curr) == value<Bits>::chunks * sizeof(chunk_t) &&
832 sizeof(item.next) == value<Bits>::chunks * sizeof(chunk_t),
833 "wire<Bits> is not compatible with C layout");
834 type = ALIAS;
835 width = Bits;
836 depth = 1;
837 curr = const_cast<chunk_t*>(item.curr.data);
838 next = nullptr;
839 }
840 };
841 static_assert(std::is_standard_layout<debug_item>::value, "debug_item is not compatible with C layout");
842
843 typedef std::map<std::string, debug_item> debug_items;
844
845 struct module {
846 module() {}
847 virtual ~module() {}
848
849 module(const module &) = delete;
850 module &operator=(const module &) = delete;
851
852 virtual bool eval() = 0;
853 virtual bool commit() = 0;
854
855 size_t step() {
856 size_t deltas = 0;
857 bool converged = false;
858 do {
859 converged = eval();
860 deltas++;
861 } while (commit() && !converged);
862 return deltas;
863 }
864
865 virtual void debug_info(debug_items &items, std::string path = "") {
866 (void)items, (void)path;
867 }
868 };
869
870 } // namespace cxxrtl
871
872 // Internal structure used to communicate with the implementation of the C interface.
873 typedef struct _cxxrtl_toplevel {
874 std::unique_ptr<cxxrtl::module> module;
875 } *cxxrtl_toplevel;
876
877 // Definitions of internal Yosys cells. Other than the functions in this namespace, CXXRTL is fully generic
878 // and indepenent of Yosys implementation details.
879 //
880 // The `write_cxxrtl` pass translates internal cells (cells with names that start with `$`) to calls of these
881 // functions. All of Yosys arithmetic and logical cells perform sign or zero extension on their operands,
882 // whereas basic operations on arbitrary width values require operands to be of the same width. These functions
883 // bridge the gap by performing the necessary casts. They are named similar to `cell_A[B]`, where A and B are `u`
884 // if the corresponding operand is unsigned, and `s` if it is signed.
885 namespace cxxrtl_yosys {
886
887 using namespace cxxrtl;
888
889 // std::max isn't constexpr until C++14 for no particular reason (it's an oversight), so we define our own.
890 template<class T>
891 CXXRTL_ALWAYS_INLINE
892 constexpr T max(const T &a, const T &b) {
893 return a > b ? a : b;
894 }
895
896 // Logic operations
897 template<size_t BitsY, size_t BitsA>
898 CXXRTL_ALWAYS_INLINE
899 value<BitsY> logic_not(const value<BitsA> &a) {
900 return value<BitsY> { a ? 0u : 1u };
901 }
902
903 template<size_t BitsY, size_t BitsA, size_t BitsB>
904 CXXRTL_ALWAYS_INLINE
905 value<BitsY> logic_and(const value<BitsA> &a, const value<BitsB> &b) {
906 return value<BitsY> { (bool(a) & bool(b)) ? 1u : 0u };
907 }
908
909 template<size_t BitsY, size_t BitsA, size_t BitsB>
910 CXXRTL_ALWAYS_INLINE
911 value<BitsY> logic_or(const value<BitsA> &a, const value<BitsB> &b) {
912 return value<BitsY> { (bool(a) | bool(b)) ? 1u : 0u };
913 }
914
915 // Reduction operations
916 template<size_t BitsY, size_t BitsA>
917 CXXRTL_ALWAYS_INLINE
918 value<BitsY> reduce_and(const value<BitsA> &a) {
919 return value<BitsY> { a.bit_not().is_zero() ? 1u : 0u };
920 }
921
922 template<size_t BitsY, size_t BitsA>
923 CXXRTL_ALWAYS_INLINE
924 value<BitsY> reduce_or(const value<BitsA> &a) {
925 return value<BitsY> { a ? 1u : 0u };
926 }
927
928 template<size_t BitsY, size_t BitsA>
929 CXXRTL_ALWAYS_INLINE
930 value<BitsY> reduce_xor(const value<BitsA> &a) {
931 return value<BitsY> { (a.ctpop() % 2) ? 1u : 0u };
932 }
933
934 template<size_t BitsY, size_t BitsA>
935 CXXRTL_ALWAYS_INLINE
936 value<BitsY> reduce_xnor(const value<BitsA> &a) {
937 return value<BitsY> { (a.ctpop() % 2) ? 0u : 1u };
938 }
939
940 template<size_t BitsY, size_t BitsA>
941 CXXRTL_ALWAYS_INLINE
942 value<BitsY> reduce_bool(const value<BitsA> &a) {
943 return value<BitsY> { a ? 1u : 0u };
944 }
945
946 // Bitwise operations
947 template<size_t BitsY, size_t BitsA>
948 CXXRTL_ALWAYS_INLINE
949 value<BitsY> not_u(const value<BitsA> &a) {
950 return a.template zcast<BitsY>().bit_not();
951 }
952
953 template<size_t BitsY, size_t BitsA>
954 CXXRTL_ALWAYS_INLINE
955 value<BitsY> not_s(const value<BitsA> &a) {
956 return a.template scast<BitsY>().bit_not();
957 }
958
959 template<size_t BitsY, size_t BitsA, size_t BitsB>
960 CXXRTL_ALWAYS_INLINE
961 value<BitsY> and_uu(const value<BitsA> &a, const value<BitsB> &b) {
962 return a.template zcast<BitsY>().bit_and(b.template zcast<BitsY>());
963 }
964
965 template<size_t BitsY, size_t BitsA, size_t BitsB>
966 CXXRTL_ALWAYS_INLINE
967 value<BitsY> and_ss(const value<BitsA> &a, const value<BitsB> &b) {
968 return a.template scast<BitsY>().bit_and(b.template scast<BitsY>());
969 }
970
971 template<size_t BitsY, size_t BitsA, size_t BitsB>
972 CXXRTL_ALWAYS_INLINE
973 value<BitsY> or_uu(const value<BitsA> &a, const value<BitsB> &b) {
974 return a.template zcast<BitsY>().bit_or(b.template zcast<BitsY>());
975 }
976
977 template<size_t BitsY, size_t BitsA, size_t BitsB>
978 CXXRTL_ALWAYS_INLINE
979 value<BitsY> or_ss(const value<BitsA> &a, const value<BitsB> &b) {
980 return a.template scast<BitsY>().bit_or(b.template scast<BitsY>());
981 }
982
983 template<size_t BitsY, size_t BitsA, size_t BitsB>
984 CXXRTL_ALWAYS_INLINE
985 value<BitsY> xor_uu(const value<BitsA> &a, const value<BitsB> &b) {
986 return a.template zcast<BitsY>().bit_xor(b.template zcast<BitsY>());
987 }
988
989 template<size_t BitsY, size_t BitsA, size_t BitsB>
990 CXXRTL_ALWAYS_INLINE
991 value<BitsY> xor_ss(const value<BitsA> &a, const value<BitsB> &b) {
992 return a.template scast<BitsY>().bit_xor(b.template scast<BitsY>());
993 }
994
995 template<size_t BitsY, size_t BitsA, size_t BitsB>
996 CXXRTL_ALWAYS_INLINE
997 value<BitsY> xnor_uu(const value<BitsA> &a, const value<BitsB> &b) {
998 return a.template zcast<BitsY>().bit_xor(b.template zcast<BitsY>()).bit_not();
999 }
1000
1001 template<size_t BitsY, size_t BitsA, size_t BitsB>
1002 CXXRTL_ALWAYS_INLINE
1003 value<BitsY> xnor_ss(const value<BitsA> &a, const value<BitsB> &b) {
1004 return a.template scast<BitsY>().bit_xor(b.template scast<BitsY>()).bit_not();
1005 }
1006
1007 template<size_t BitsY, size_t BitsA, size_t BitsB>
1008 CXXRTL_ALWAYS_INLINE
1009 value<BitsY> shl_uu(const value<BitsA> &a, const value<BitsB> &b) {
1010 return a.template zcast<BitsY>().template shl(b);
1011 }
1012
1013 template<size_t BitsY, size_t BitsA, size_t BitsB>
1014 CXXRTL_ALWAYS_INLINE
1015 value<BitsY> shl_su(const value<BitsA> &a, const value<BitsB> &b) {
1016 return a.template scast<BitsY>().template shl(b);
1017 }
1018
1019 template<size_t BitsY, size_t BitsA, size_t BitsB>
1020 CXXRTL_ALWAYS_INLINE
1021 value<BitsY> sshl_uu(const value<BitsA> &a, const value<BitsB> &b) {
1022 return a.template zcast<BitsY>().template shl(b);
1023 }
1024
1025 template<size_t BitsY, size_t BitsA, size_t BitsB>
1026 CXXRTL_ALWAYS_INLINE
1027 value<BitsY> sshl_su(const value<BitsA> &a, const value<BitsB> &b) {
1028 return a.template scast<BitsY>().template shl(b);
1029 }
1030
1031 template<size_t BitsY, size_t BitsA, size_t BitsB>
1032 CXXRTL_ALWAYS_INLINE
1033 value<BitsY> shr_uu(const value<BitsA> &a, const value<BitsB> &b) {
1034 return a.template shr(b).template zcast<BitsY>();
1035 }
1036
1037 template<size_t BitsY, size_t BitsA, size_t BitsB>
1038 CXXRTL_ALWAYS_INLINE
1039 value<BitsY> shr_su(const value<BitsA> &a, const value<BitsB> &b) {
1040 return a.template shr(b).template scast<BitsY>();
1041 }
1042
1043 template<size_t BitsY, size_t BitsA, size_t BitsB>
1044 CXXRTL_ALWAYS_INLINE
1045 value<BitsY> sshr_uu(const value<BitsA> &a, const value<BitsB> &b) {
1046 return a.template shr(b).template zcast<BitsY>();
1047 }
1048
1049 template<size_t BitsY, size_t BitsA, size_t BitsB>
1050 CXXRTL_ALWAYS_INLINE
1051 value<BitsY> sshr_su(const value<BitsA> &a, const value<BitsB> &b) {
1052 return a.template sshr(b).template scast<BitsY>();
1053 }
1054
1055 template<size_t BitsY, size_t BitsA, size_t BitsB>
1056 CXXRTL_ALWAYS_INLINE
1057 value<BitsY> shift_uu(const value<BitsA> &a, const value<BitsB> &b) {
1058 return shr_uu<BitsY>(a, b);
1059 }
1060
1061 template<size_t BitsY, size_t BitsA, size_t BitsB>
1062 CXXRTL_ALWAYS_INLINE
1063 value<BitsY> shift_su(const value<BitsA> &a, const value<BitsB> &b) {
1064 return shr_su<BitsY>(a, b);
1065 }
1066
1067 template<size_t BitsY, size_t BitsA, size_t BitsB>
1068 CXXRTL_ALWAYS_INLINE
1069 value<BitsY> shift_us(const value<BitsA> &a, const value<BitsB> &b) {
1070 return b.is_neg() ? shl_uu<BitsY>(a, b.template sext<BitsB + 1>().neg()) : shr_uu<BitsY>(a, b);
1071 }
1072
1073 template<size_t BitsY, size_t BitsA, size_t BitsB>
1074 CXXRTL_ALWAYS_INLINE
1075 value<BitsY> shift_ss(const value<BitsA> &a, const value<BitsB> &b) {
1076 return b.is_neg() ? shl_su<BitsY>(a, b.template sext<BitsB + 1>().neg()) : shr_su<BitsY>(a, b);
1077 }
1078
1079 template<size_t BitsY, size_t BitsA, size_t BitsB>
1080 CXXRTL_ALWAYS_INLINE
1081 value<BitsY> shiftx_uu(const value<BitsA> &a, const value<BitsB> &b) {
1082 return shift_uu<BitsY>(a, b);
1083 }
1084
1085 template<size_t BitsY, size_t BitsA, size_t BitsB>
1086 CXXRTL_ALWAYS_INLINE
1087 value<BitsY> shiftx_su(const value<BitsA> &a, const value<BitsB> &b) {
1088 return shift_su<BitsY>(a, b);
1089 }
1090
1091 template<size_t BitsY, size_t BitsA, size_t BitsB>
1092 CXXRTL_ALWAYS_INLINE
1093 value<BitsY> shiftx_us(const value<BitsA> &a, const value<BitsB> &b) {
1094 return shift_us<BitsY>(a, b);
1095 }
1096
1097 template<size_t BitsY, size_t BitsA, size_t BitsB>
1098 CXXRTL_ALWAYS_INLINE
1099 value<BitsY> shiftx_ss(const value<BitsA> &a, const value<BitsB> &b) {
1100 return shift_ss<BitsY>(a, b);
1101 }
1102
1103 // Comparison operations
1104 template<size_t BitsY, size_t BitsA, size_t BitsB>
1105 CXXRTL_ALWAYS_INLINE
1106 value<BitsY> eq_uu(const value<BitsA> &a, const value<BitsB> &b) {
1107 constexpr size_t BitsExt = max(BitsA, BitsB);
1108 return value<BitsY>{ a.template zext<BitsExt>() == b.template zext<BitsExt>() ? 1u : 0u };
1109 }
1110
1111 template<size_t BitsY, size_t BitsA, size_t BitsB>
1112 CXXRTL_ALWAYS_INLINE
1113 value<BitsY> eq_ss(const value<BitsA> &a, const value<BitsB> &b) {
1114 constexpr size_t BitsExt = max(BitsA, BitsB);
1115 return value<BitsY>{ a.template sext<BitsExt>() == b.template sext<BitsExt>() ? 1u : 0u };
1116 }
1117
1118 template<size_t BitsY, size_t BitsA, size_t BitsB>
1119 CXXRTL_ALWAYS_INLINE
1120 value<BitsY> ne_uu(const value<BitsA> &a, const value<BitsB> &b) {
1121 constexpr size_t BitsExt = max(BitsA, BitsB);
1122 return value<BitsY>{ a.template zext<BitsExt>() != b.template zext<BitsExt>() ? 1u : 0u };
1123 }
1124
1125 template<size_t BitsY, size_t BitsA, size_t BitsB>
1126 CXXRTL_ALWAYS_INLINE
1127 value<BitsY> ne_ss(const value<BitsA> &a, const value<BitsB> &b) {
1128 constexpr size_t BitsExt = max(BitsA, BitsB);
1129 return value<BitsY>{ a.template sext<BitsExt>() != b.template sext<BitsExt>() ? 1u : 0u };
1130 }
1131
1132 template<size_t BitsY, size_t BitsA, size_t BitsB>
1133 CXXRTL_ALWAYS_INLINE
1134 value<BitsY> eqx_uu(const value<BitsA> &a, const value<BitsB> &b) {
1135 return eq_uu<BitsY>(a, b);
1136 }
1137
1138 template<size_t BitsY, size_t BitsA, size_t BitsB>
1139 CXXRTL_ALWAYS_INLINE
1140 value<BitsY> eqx_ss(const value<BitsA> &a, const value<BitsB> &b) {
1141 return eq_ss<BitsY>(a, b);
1142 }
1143
1144 template<size_t BitsY, size_t BitsA, size_t BitsB>
1145 CXXRTL_ALWAYS_INLINE
1146 value<BitsY> nex_uu(const value<BitsA> &a, const value<BitsB> &b) {
1147 return ne_uu<BitsY>(a, b);
1148 }
1149
1150 template<size_t BitsY, size_t BitsA, size_t BitsB>
1151 CXXRTL_ALWAYS_INLINE
1152 value<BitsY> nex_ss(const value<BitsA> &a, const value<BitsB> &b) {
1153 return ne_ss<BitsY>(a, b);
1154 }
1155
1156 template<size_t BitsY, size_t BitsA, size_t BitsB>
1157 CXXRTL_ALWAYS_INLINE
1158 value<BitsY> gt_uu(const value<BitsA> &a, const value<BitsB> &b) {
1159 constexpr size_t BitsExt = max(BitsA, BitsB);
1160 return value<BitsY> { b.template zext<BitsExt>().ucmp(a.template zext<BitsExt>()) ? 1u : 0u };
1161 }
1162
1163 template<size_t BitsY, size_t BitsA, size_t BitsB>
1164 CXXRTL_ALWAYS_INLINE
1165 value<BitsY> gt_ss(const value<BitsA> &a, const value<BitsB> &b) {
1166 constexpr size_t BitsExt = max(BitsA, BitsB);
1167 return value<BitsY> { b.template sext<BitsExt>().scmp(a.template sext<BitsExt>()) ? 1u : 0u };
1168 }
1169
1170 template<size_t BitsY, size_t BitsA, size_t BitsB>
1171 CXXRTL_ALWAYS_INLINE
1172 value<BitsY> ge_uu(const value<BitsA> &a, const value<BitsB> &b) {
1173 constexpr size_t BitsExt = max(BitsA, BitsB);
1174 return value<BitsY> { !a.template zext<BitsExt>().ucmp(b.template zext<BitsExt>()) ? 1u : 0u };
1175 }
1176
1177 template<size_t BitsY, size_t BitsA, size_t BitsB>
1178 CXXRTL_ALWAYS_INLINE
1179 value<BitsY> ge_ss(const value<BitsA> &a, const value<BitsB> &b) {
1180 constexpr size_t BitsExt = max(BitsA, BitsB);
1181 return value<BitsY> { !a.template sext<BitsExt>().scmp(b.template sext<BitsExt>()) ? 1u : 0u };
1182 }
1183
1184 template<size_t BitsY, size_t BitsA, size_t BitsB>
1185 CXXRTL_ALWAYS_INLINE
1186 value<BitsY> lt_uu(const value<BitsA> &a, const value<BitsB> &b) {
1187 constexpr size_t BitsExt = max(BitsA, BitsB);
1188 return value<BitsY> { a.template zext<BitsExt>().ucmp(b.template zext<BitsExt>()) ? 1u : 0u };
1189 }
1190
1191 template<size_t BitsY, size_t BitsA, size_t BitsB>
1192 CXXRTL_ALWAYS_INLINE
1193 value<BitsY> lt_ss(const value<BitsA> &a, const value<BitsB> &b) {
1194 constexpr size_t BitsExt = max(BitsA, BitsB);
1195 return value<BitsY> { a.template sext<BitsExt>().scmp(b.template sext<BitsExt>()) ? 1u : 0u };
1196 }
1197
1198 template<size_t BitsY, size_t BitsA, size_t BitsB>
1199 CXXRTL_ALWAYS_INLINE
1200 value<BitsY> le_uu(const value<BitsA> &a, const value<BitsB> &b) {
1201 constexpr size_t BitsExt = max(BitsA, BitsB);
1202 return value<BitsY> { !b.template zext<BitsExt>().ucmp(a.template zext<BitsExt>()) ? 1u : 0u };
1203 }
1204
1205 template<size_t BitsY, size_t BitsA, size_t BitsB>
1206 CXXRTL_ALWAYS_INLINE
1207 value<BitsY> le_ss(const value<BitsA> &a, const value<BitsB> &b) {
1208 constexpr size_t BitsExt = max(BitsA, BitsB);
1209 return value<BitsY> { !b.template sext<BitsExt>().scmp(a.template sext<BitsExt>()) ? 1u : 0u };
1210 }
1211
1212 // Arithmetic operations
1213 template<size_t BitsY, size_t BitsA>
1214 CXXRTL_ALWAYS_INLINE
1215 value<BitsY> pos_u(const value<BitsA> &a) {
1216 return a.template zcast<BitsY>();
1217 }
1218
1219 template<size_t BitsY, size_t BitsA>
1220 CXXRTL_ALWAYS_INLINE
1221 value<BitsY> pos_s(const value<BitsA> &a) {
1222 return a.template scast<BitsY>();
1223 }
1224
1225 template<size_t BitsY, size_t BitsA>
1226 CXXRTL_ALWAYS_INLINE
1227 value<BitsY> neg_u(const value<BitsA> &a) {
1228 return a.template zcast<BitsY>().neg();
1229 }
1230
1231 template<size_t BitsY, size_t BitsA>
1232 CXXRTL_ALWAYS_INLINE
1233 value<BitsY> neg_s(const value<BitsA> &a) {
1234 return a.template scast<BitsY>().neg();
1235 }
1236
1237 template<size_t BitsY, size_t BitsA, size_t BitsB>
1238 CXXRTL_ALWAYS_INLINE
1239 value<BitsY> add_uu(const value<BitsA> &a, const value<BitsB> &b) {
1240 return a.template zcast<BitsY>().add(b.template zcast<BitsY>());
1241 }
1242
1243 template<size_t BitsY, size_t BitsA, size_t BitsB>
1244 CXXRTL_ALWAYS_INLINE
1245 value<BitsY> add_ss(const value<BitsA> &a, const value<BitsB> &b) {
1246 return a.template scast<BitsY>().add(b.template scast<BitsY>());
1247 }
1248
1249 template<size_t BitsY, size_t BitsA, size_t BitsB>
1250 CXXRTL_ALWAYS_INLINE
1251 value<BitsY> sub_uu(const value<BitsA> &a, const value<BitsB> &b) {
1252 return a.template zcast<BitsY>().sub(b.template zcast<BitsY>());
1253 }
1254
1255 template<size_t BitsY, size_t BitsA, size_t BitsB>
1256 CXXRTL_ALWAYS_INLINE
1257 value<BitsY> sub_ss(const value<BitsA> &a, const value<BitsB> &b) {
1258 return a.template scast<BitsY>().sub(b.template scast<BitsY>());
1259 }
1260
1261 template<size_t BitsY, size_t BitsA, size_t BitsB>
1262 CXXRTL_ALWAYS_INLINE
1263 value<BitsY> mul_uu(const value<BitsA> &a, const value<BitsB> &b) {
1264 value<BitsY> product;
1265 value<BitsY> multiplicand = a.template zcast<BitsY>();
1266 const value<BitsB> &multiplier = b;
1267 uint32_t multiplicand_shift = 0;
1268 for (size_t step = 0; step < BitsB; step++) {
1269 if (multiplier.bit(step)) {
1270 multiplicand = multiplicand.shl(value<32> { multiplicand_shift });
1271 product = product.add(multiplicand);
1272 multiplicand_shift = 0;
1273 }
1274 multiplicand_shift++;
1275 }
1276 return product;
1277 }
1278
1279 template<size_t BitsY, size_t BitsA, size_t BitsB>
1280 CXXRTL_ALWAYS_INLINE
1281 value<BitsY> mul_ss(const value<BitsA> &a, const value<BitsB> &b) {
1282 value<BitsB + 1> ub = b.template sext<BitsB + 1>();
1283 if (ub.is_neg()) ub = ub.neg();
1284 value<BitsY> y = mul_uu<BitsY>(a.template scast<BitsY>(), ub);
1285 return b.is_neg() ? y.neg() : y;
1286 }
1287
1288 template<size_t BitsY, size_t BitsA, size_t BitsB>
1289 CXXRTL_ALWAYS_INLINE
1290 std::pair<value<BitsY>, value<BitsY>> divmod_uu(const value<BitsA> &a, const value<BitsB> &b) {
1291 constexpr size_t Bits = max(BitsY, max(BitsA, BitsB));
1292 value<Bits> quotient;
1293 value<Bits> dividend = a.template zext<Bits>();
1294 value<Bits> divisor = b.template zext<Bits>();
1295 if (dividend.ucmp(divisor))
1296 return {/*quotient=*/value<BitsY> { 0u }, /*remainder=*/dividend.template trunc<BitsY>()};
1297 uint32_t divisor_shift = dividend.ctlz() - divisor.ctlz();
1298 divisor = divisor.shl(value<32> { divisor_shift });
1299 for (size_t step = 0; step <= divisor_shift; step++) {
1300 quotient = quotient.shl(value<1> { 1u });
1301 if (!dividend.ucmp(divisor)) {
1302 dividend = dividend.sub(divisor);
1303 quotient.set_bit(0, true);
1304 }
1305 divisor = divisor.shr(value<1> { 1u });
1306 }
1307 return {quotient.template trunc<BitsY>(), /*remainder=*/dividend.template trunc<BitsY>()};
1308 }
1309
1310 template<size_t BitsY, size_t BitsA, size_t BitsB>
1311 CXXRTL_ALWAYS_INLINE
1312 std::pair<value<BitsY>, value<BitsY>> divmod_ss(const value<BitsA> &a, const value<BitsB> &b) {
1313 value<BitsA + 1> ua = a.template sext<BitsA + 1>();
1314 value<BitsB + 1> ub = b.template sext<BitsB + 1>();
1315 if (ua.is_neg()) ua = ua.neg();
1316 if (ub.is_neg()) ub = ub.neg();
1317 value<BitsY> y, r;
1318 std::tie(y, r) = divmod_uu<BitsY>(ua, ub);
1319 if (a.is_neg() != b.is_neg()) y = y.neg();
1320 if (a.is_neg()) r = r.neg();
1321 return {y, r};
1322 }
1323
1324 template<size_t BitsY, size_t BitsA, size_t BitsB>
1325 CXXRTL_ALWAYS_INLINE
1326 value<BitsY> div_uu(const value<BitsA> &a, const value<BitsB> &b) {
1327 return divmod_uu<BitsY>(a, b).first;
1328 }
1329
1330 template<size_t BitsY, size_t BitsA, size_t BitsB>
1331 CXXRTL_ALWAYS_INLINE
1332 value<BitsY> div_ss(const value<BitsA> &a, const value<BitsB> &b) {
1333 return divmod_ss<BitsY>(a, b).first;
1334 }
1335
1336 template<size_t BitsY, size_t BitsA, size_t BitsB>
1337 CXXRTL_ALWAYS_INLINE
1338 value<BitsY> mod_uu(const value<BitsA> &a, const value<BitsB> &b) {
1339 return divmod_uu<BitsY>(a, b).second;
1340 }
1341
1342 template<size_t BitsY, size_t BitsA, size_t BitsB>
1343 CXXRTL_ALWAYS_INLINE
1344 value<BitsY> mod_ss(const value<BitsA> &a, const value<BitsB> &b) {
1345 return divmod_ss<BitsY>(a, b).second;
1346 }
1347
1348 // Memory helper
1349 struct memory_index {
1350 bool valid;
1351 size_t index;
1352
1353 template<size_t BitsAddr>
1354 memory_index(const value<BitsAddr> &addr, size_t offset, size_t depth) {
1355 static_assert(value<BitsAddr>::chunks <= 1, "memory address is too wide");
1356 size_t offset_index = addr.data[0];
1357
1358 valid = (offset_index >= offset && offset_index < offset + depth);
1359 index = offset_index - offset;
1360 }
1361 };
1362
1363 } // namespace cxxrtl_yosys
1364
1365 #endif