-
Notifications
You must be signed in to change notification settings - Fork 9
/
cuttlesim.hpp
1505 lines (1244 loc) · 45.3 KB
/
cuttlesim.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! Preamble shared by all Kôika programs compiled to C++ !*/
#ifndef _PREAMBLE_HPP
#define _PREAMBLE_HPP
#include <algorithm> // For std::max
#include <array>
#include <cstddef> // For size_t
#include <cstdint> // For uintN_t
#include <cstring> // For memcpy
#include <limits> // For std::numeric_limits used in prims::mask
#include <ostream> // For std::ostream used in operator<<
#include <string> // For prims::display
#include <utility> // for std::forward
#include <type_traits> // For std::conditional_t
#ifndef SIM_MINIMAL
#include <chrono> // For VCD headers
#include <ctime> // for gmtime
#include <cctype> // For isgraph
#include <cstdlib> // For getenv
#include <initializer_list>
#include <iomanip> // For std::setfill
#include <iostream>
#include <sstream> // For std::ostringstream
#include <fstream> // For VCD files
#include <random> // For executing rules in random order
#endif // #ifndef SIM_MINIMAL
#ifdef SIM_DEBUG
#include <iostream>
static inline void _sim_assert_fn(const char* repr,
bool expr,
const char* file,
const int line,
const char* err_msg) {
if (!expr) {
std::cerr << file << ":" << line << ": "
<< err_msg << std::endl
<< "Failed assertion: " << repr;
abort();
}
}
#define _sim_assert(expr, msg) _sim_assert_fn(#expr, expr, __FILE__, __LINE__, msg)
#else
#define _sim_assert(expr, msg) ;
#endif // #ifdef SIM_DEBUG
#define _unused __attribute__((unused))
#if defined(__clang__)
#define _unoptimized __attribute__((optnone))
#elif defined(__GNUG__)
#define _unoptimized __attribute__((optimize("O0")))
#else
#define _unoptimized
#endif
#ifndef SIM_MINIMAL
#define _virtual virtual
#else
#define _virtual
#endif
#if defined(SIM_MINIMAL) && defined(SIM_KEEP_DISPLAY)
#define _display_unoptimized _unoptimized
#else
#define _display_unoptimized
#endif
#ifdef SIM_FLATTEN
#define _flatten __attribute__((flatten))
#else
#define _flatten
#endif
#if defined(SIM_NOINLINE)
#define _inline __attribute__((noinline))
#elif defined(SIM_ALWAYS_INLINE)
#define _inline __attribute__((always_inline))
#else
#define _inline
#endif
#define _noreturn __attribute__((noreturn))
#define _unlikely(b) __builtin_expect((b), 0)
#define MULTIPRECISION_THRESHOLD 64
#ifdef NEEDS_BOOST_MULTIPRECISION
#include <boost/multiprecision/cpp_int.hpp>
#if BOOST_VERSION < 106800
// https://github.com/boostorg/multiprecision/commit/bbe819f8034a3c854deffc6191410b91ac27b3d6
// Before 1.68, static_cast<uint16_t>(uint128_t{1 << 16}) gives 65535 instead of 0
#pragma message("Bignum truncation is broken in Boost < 1.68; if you run into issues, try upgrading.")
#endif
template<std::size_t size>
using wbits_t = std::conditional_t<size <= 128, boost::multiprecision::uint128_t,
std::conditional_t<size <= 256, boost::multiprecision::uint256_t,
std::conditional_t<size <= 512, boost::multiprecision::uint512_t,
std::conditional_t<size <= 1024, boost::multiprecision::uint1024_t,
void>>>>;
template<std::size_t size>
using wsbits_t = std::conditional_t<size <= 128, boost::multiprecision::int128_t,
std::conditional_t<size <= 256, boost::multiprecision::int256_t,
std::conditional_t<size <= 512, boost::multiprecision::int512_t,
std::conditional_t<size <= 1024, boost::multiprecision::int1024_t,
void>>>>;
#else
template<std::size_t size>
using wbits_t = void;
template<std::size_t size>
using wsbits_t = void;
#endif // #ifdef NEEDS_BOOST_MULTIPRECISION
namespace cuttlesim {
static _unused const char* version = "CuttleSim v0.0.1";
}
template<std::size_t size>
using bits_t = std::conditional_t<size <= 8, std::uint8_t,
std::conditional_t<size <= 16, std::uint16_t,
std::conditional_t<size <= 32, std::uint32_t,
std::conditional_t<size <= 64, std::uint64_t,
wbits_t<size>>>>>;
template<std::size_t size>
using sbits_t = std::conditional_t<size <= 8, std::int8_t,
std::conditional_t<size <= 16, std::int16_t,
std::conditional_t<size <= 32, std::int32_t,
std::conditional_t<size <= 64, std::int64_t,
wsbits_t<size>>>>>;
/// # Implementation of Kôika primitives
namespace prims {
using bitwidth = std::size_t;
using size_t = std::size_t;
/// ## Utility functions
template<typename T>
T _noreturn unreachable() {
__builtin_unreachable();
}
static _unused void assume(bool condition) {
if (!condition) { unreachable<void>(); }
}
/// ## Array type (array<T, len>)
template<typename T, size_t len>
struct array : public std::array<T, len> { // Inherit to be able to overload ‘==’
// https://stackoverflow.com/questions/24280521/
// TODO: remove this constructor once we move to C++17
template <typename... Args>
// NOLINTNEXTLINE(google-explicit-constructor)
array(Args&&... args) : std::array<T, len>({std::forward<Args>(args)...}) {}
};
/// ## Bitvector type (bits<n>)
template <bitwidth sz> struct bits;
// https://stackoverflow.com/questions/4660123/
template <bitwidth sz> std::ostream& operator<<(std::ostream& /*os*/, const bits<sz>& /*bs*/);
template<bitwidth sz>
struct bits {
bits_t<sz> v;
#ifndef __OPTIMIZE__
// This makes debugging easier
static constexpr bitwidth size = sz;
#endif
/// ### Representation invariant
static constexpr bitwidth padding_width() noexcept {
// making this a function avoids polluting GDB's output
return std::numeric_limits<bits_t<sz>>::digits - sz;
}
// Not constexpr because Boost's >> isn't constexpr
static bits_t<sz> bitmask() noexcept {
auto pw = bits<sz>::padding_width(); // https://stackoverflow.com/questions/8452952/
return std::numeric_limits<bits_t<sz>>::max() >> pw;
}
void invariant() const noexcept {
// Knowing this invariant can sometimes help the compiler; it does in
// particular in ‘operator bool()’ below.
assume(v <= bitmask());
}
/// ### Casts
sbits_t<sz> to_sbits() const {
sbits_t<sz> sx; // FIXME does this work with multiprecision?
std::memcpy(&sx, &this->v, sizeof sx);
return sx;
}
static bits<sz> of_sbits(sbits_t<sz> sx) {
bits_t<sz> x; // FIXME does this work with multiprecision?
std::memcpy(&x, &sx, sizeof x);
return bits<sz>::mk(x);
}
sbits_t<sz> to_shifted_sbits() const {
// This constructs an int of the same bitsize as x, with the same
// bitpattern, except that it uses the high bits of the storage type instead
// of the low ones (e.g. 4'b1101 is represented as 8'b11010000).
return (*this << bits<sz>::padding_width()).to_sbits();
}
static bits<sz> of_shifted_sbits(sbits_t<sz> sx) {
return of_sbits(sx) >> bits<sz>::padding_width();
}
/// ### Constants
// Not constexpr because of ::bitmask
static bits<sz> ones() {
return bits<sz>::mk(bits<sz>::bitmask());
}
/// ### Member functions
explicit operator bool() const {
invariant(); // Knowing this invariant helps GCC generate better code
return bool(v); // Writing bool(v & bitmask()) works just as well
}
// Add an implicit cast to bool for size sz == 1
// https://github.com/mit-plv/koika/issues/18
template <int sz_ = sz>
operator std::enable_if_t<sz_ == 1, bool> () const {
invariant();
return bool(v);
}
explicit operator bits_t<sz>() const {
return v;
}
template<bitwidth idx_sz>
bits<1> operator[](bits<idx_sz> idx) const;
bits<sz>& operator&=(bits<sz> arg);
bits<sz>& operator|=(bits<sz> arg);
bits<sz>& operator^=(bits<sz> arg);
bits<sz>& operator+=(bits<sz> arg);
bits<sz>& operator-=(bits<sz> arg);
bits<sz>& operator<<=(size_t shift);
bits<sz>& operator>>=(size_t shift);
template<bitwidth shift_sz> bits<sz>& operator<<=(bits<shift_sz> shift);
template<bitwidth shift_sz> bits<sz>& operator>>=(bits<shift_sz> shift);
// https://stackoverflow.com/questions/4660123/
friend std::ostream& operator<<<sz>(std::ostream& os, const bits<sz>& bs);
/// ### Constructors
template<typename T>
static constexpr bits<sz> mk(T arg) {
return bits{static_cast<bits_t<sz>>(arg)};
}
static bits<sz> of_str(const std::string& str) {
bits<sz> out{};
std::size_t len = str.length();
for (std::size_t pos = std::max(len, sz) - sz; pos < len; pos++) {
if (str[pos] == '1')
out |= bits<sz>{1} << len - pos - 1;
}
return out;
}
};
/// ## Special case for 0-bit bitvectors (bits<0>)
template<>
struct bits<0> {
bits_t<0> v = 0;
/// ### Representation invariant
static constexpr bitwidth padding_width() noexcept { return std::numeric_limits<bits_t<0>>::digits; }
static bits_t<0> bitmask() noexcept { return 0; }
void invariant() const noexcept { assume(v == 0); }
/// ### Casts
sbits_t<0> to_sbits() const { return 0; };
static bits<0> of_sbits(sbits_t<0>) { return {}; }
sbits_t<0> to_shifted_sbits() const { return 0; }
static bits<0> of_shifted_sbits(sbits_t<0>) { return {}; }
/// ### Constants
static bits<0> ones() { return {}; }
/// ### Member functions
explicit operator bool() const { return false; }
explicit operator bits_t<0>() const { return 0; }
template<bitwidth idx_sz> bits<1>
operator[](bits<idx_sz>) const { return bits<1>{0}; }
bits<0>& operator&=(bits<0>) { return *this; }
bits<0>& operator|=(bits<0>) { return *this; }
bits<0>& operator^=(bits<0>) { return *this; }
bits<0>& operator+=(bits<0>) { return *this; }
bits<0>& operator-=(bits<0>) { return *this; }
bits<0>& operator<<=(size_t) { return *this; }
bits<0>& operator>>=(size_t) { return *this; }
template<bitwidth shift_sz> bits<0>& operator<<=(bits<shift_sz>) { return *this; }
template<bitwidth shift_sz> bits<0>& operator>>=(bits<shift_sz>) { return *this; }
friend std::ostream& operator<<<0>(std::ostream& os, const bits<0>& bs);
/// ### Constructors
template<typename T> static constexpr bits<0> mk(T /*arg*/) { return {}; }
};
/// ## Unit type
using unit = bits<0>;
static const _unused unit tt{};
/// ## Bitvector literals
namespace literal_parsing {
template<unsigned int base, char c>
constexpr bool valid_digit() {
switch (base) {
case 2:
return c == '0' || c == '1';
case 10:
return '0' <= c && c <= '9';
case 16:
return (('0' <= c && c <= '9') ||
('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'));
default:
return false;
}
}
template <unsigned int base, char c>
constexpr unsigned int parse_digit() noexcept {
static_assert(base == 2 || base == 10 || base == 16, "Invalid base");
static_assert(valid_digit<base, c>(), "Invalid digit");
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'f') {
return c - 'a' + 10;
} else if ('A' <= c && c <= 'F') {
return c - 'A' + 10;
} else {
unreachable<unsigned int>();
}
}
template <unsigned int base, std::uint64_t max, std::uint64_t num>
constexpr std::uint64_t parse_u64() noexcept {
static_assert(max >= num, "Overflow in literal parsing");
return num;
}
template <unsigned int base, std::uint64_t max, std::uint64_t num, char c, char... cs>
constexpr std::uint64_t parse_u64() noexcept {
const std::uint64_t digit = parse_digit<base, c>();
static_assert((max - digit) / base >= num, "Overflow in literal parsing");
return parse_u64<base, max, base * num + digit, cs...>();
}
enum class parser { u64, u128, u256, u512, u1024, unsupported };
template <parser p, unsigned int base, bitwidth sz, char... cs>
struct parse_number {
static_assert(p != parser::unsupported, "Unsupported bitsize.");
#ifndef NEEDS_BOOST_MULTIPRECISION
static_assert(p == parser::u64, "Needs boost::multiprecision to parse numbers with > 64 bits.");
#endif
static_assert(p == parser::u64 || base == 16, "boost::multiprecision only supports base-16 literals.");
};
template <unsigned int base, bitwidth sz, char... cs>
struct parse_number<parser::u64, base, sz, cs...> {
// Not using bits<sz>::bitmask because it isn't constexpr
static constexpr std::uint64_t max = std::numeric_limits<bits_t<sz>>::max() >> bits<sz>::padding_width();
static constexpr bits_t<sz> v = parse_u64<base, max, 0, cs...>();
};
#ifdef NEEDS_BOOST_MULTIPRECISION
using namespace boost::multiprecision::literals;
template <bitwidth sz, char... cs>
struct parse_number<parser::u128, 16, sz, cs...> {
static constexpr bits_t<sz> v = operator "" _cppui128<'0', 'x', cs...>();
};
template <bitwidth sz, char... cs>
struct parse_number<parser::u256, 16, sz, cs...> {
static constexpr bits_t<sz> v = operator "" _cppui256<'0', 'x', cs...>();
};
template <bitwidth sz, char... cs>
struct parse_number<parser::u512, 16, sz, cs...> {
static constexpr bits_t<sz> v = operator "" _cppui512<'0', 'x', cs...>();
};
template <bitwidth sz, char... cs>
struct parse_number<parser::u1024, 16, sz, cs...> {
static constexpr bits_t<sz> v = operator "" _cppui1024<'0', 'x', cs...>();
};
#endif
constexpr parser get_parser(bitwidth sz) noexcept {
if (sz <= 64) {
return parser::u64;
} else if (sz <= 128) {
return parser::u128;
} else if (sz <= 256) {
return parser::u256;
} else if (sz <= 512) {
return parser::u512;
} else if (sz <= 1024) {
return parser::u1024;
} else {
return parser::unsupported;
}
}
template <bool imm, unsigned int base, bitwidth sz, char... cs>
struct parse_literal;
template <unsigned int base, bitwidth sz, char... cs>
struct parse_literal<true, base, sz, '\'', cs...> {
static_assert(sz <= 64, "Immediates can't have size > 64.");
static constexpr bits_t<sz> v = parse_number<get_parser(sz), base, sz, cs...>::v;
};
template <unsigned int base, bitwidth sz, char... cs>
struct parse_literal<false, base, sz, '\'', cs...> {
static constexpr bits<sz> v = bits<sz>{parse_number<get_parser(sz), base, sz, cs...>::v};
};
template <bool imm, unsigned int base, bitwidth sz, char c, char... cs>
struct parse_literal<imm, base, sz, c, cs...> {
static constexpr bitwidth sz_digit = parse_digit<10, c>();
static constexpr auto v = parse_literal<imm, base, 10 * sz + sz_digit, cs...>::v;
};
template <bool imm, char... cs>
using parse_bin = parse_literal<imm, 2, 0, cs...>;
template <bool imm, char... cs>
using parse_dec = parse_literal<imm, 10, 0, cs...>;
template <bool imm, char... cs> struct parse_hex;
template <bool imm, char c0, char c1, char... cs>
struct parse_hex<imm, c0, c1, cs...> {
static_assert(c0 == '0' && c1 == 'x', "Hex literal must start with 0x");
static constexpr auto v = parse_literal<imm, 16, 0, cs...>::v;
};
} // namespace literal_parsing
namespace literals {
// ‘auto v = …; return v’: see
// * https://stackoverflow.com/questions/8452952/c-linker-error-with-class-static-constexpr
// * https://stackoverflow.com/questions/45970113/
// * https://stackoverflow.com/a/18940384/695591
// … or compile with -std=c++17
// Binary representation: 4'0010_b
template <char... cs> constexpr auto operator "" _b() {
constexpr auto v = literal_parsing::parse_bin<false, cs...>::v; return v;
}
// Decimal representation: 11'1234_d
template <char... cs> constexpr auto operator "" _d() {
constexpr auto v = literal_parsing::parse_dec<false, cs...>::v; return v;
}
// Hex representation: 0x16'abcd_x
template <char... cs> constexpr auto operator "" _x() {
constexpr auto v = literal_parsing::parse_hex<false, cs...>::v; return v;
}
// Immediate binary representation: 4'0010_bv
template <char... cs> constexpr auto operator "" _bv() {
constexpr auto v = literal_parsing::parse_bin<true, cs...>::v; return v;
}
// Immediate decimal representation: 11'1234_dv
template <char... cs> constexpr auto operator "" _dv() {
constexpr auto v = literal_parsing::parse_dec<true, cs...>::v; return v;
}
// Immediate hex representation: 0x16'abcd_xv
template <char... cs> constexpr auto operator "" _xv() {
constexpr auto v = literal_parsing::parse_hex<true, cs...>::v; return v;
}
} // namespace literals
/// ## Bit- and array-manipulation functions
template<bitwidth sz>
static bits<sz> mask(bits<sz> arg) {
return arg & bits<sz>::ones();
}
template<bitwidth ret_sz, bitwidth sz>
static bits<ret_sz> widen(const bits<sz> arg) {
static_assert(ret_sz >= sz, "Call to widen has ret_sz < sz");
return bits<ret_sz>::mk(arg.v);
}
template<bitwidth ret_sz, bitwidth sz>
static bits<ret_sz> truncate(const bits<sz> arg) {
if (sz > MULTIPRECISION_THRESHOLD && sz > ret_sz) {
// Truncation is broken in Boost::multiprecision < 1.68.0, so mask before truncating
// https://github.com/boostorg/multiprecision/commit/bbe819f8034a3c854deffc6191410b91ac27b3d6
return bits<ret_sz>::mk(arg.v & bits<ret_sz>::bitmask());
} else {
return mask(bits<ret_sz>::mk(arg.v));
}
}
template<bitwidth sz>
bits<1> msb(const bits<sz> arg) {
return sz == 0 ? 0 : truncate<1>(arg >> (sz - 1));
}
template<bitwidth sz>
template<bitwidth idx_sz>
bits<1> bits<sz>::operator[](const bits<idx_sz> idx) const {
return truncate<1>((*this) >> idx);
}
template<bitwidth idx, bitwidth sz1, bitwidth width>
bits<sz1> slice_subst(const bits<sz1> data, const bits<width> repl) {
const bits<sz1> mask = ~(widen<sz1>(bits<width>::ones()) << idx);
return (data & mask) | (widen<sz1>(repl) << idx);
}
template<bitwidth width, bitwidth sz1, bitwidth sz2>
bits<width> islice(const bits<sz1> data, const bits<sz2> idx) {
return truncate<width>(data >> idx);
}
template<bitwidth sz>
bits<sz> operator&(const bits<sz> data1, const bits<sz> data2) {
return bits<sz>::mk(data1.v & data2.v);
}
template<bitwidth sz>
bits<sz> operator|(const bits<sz> data1, const bits<sz> data2) {
return bits<sz>::mk(data1.v | data2.v);
}
template<bitwidth sz>
bits<sz> operator^(const bits<sz> data1, const bits<sz> data2) {
return bits<sz>::mk(data1.v ^ data2.v);
}
template<bitwidth sz1, bitwidth sz2>
bits<sz1> asr(const bits<sz1> data, const bits<sz2> shift) {
// Implementation-defined, assumes that the compiler does an arithmetic shift
return bits<sz1>::of_shifted_sbits(data.to_shifted_sbits() >> shift.v);
}
template<bitwidth sz1>
bits<sz1> operator>>(const bits<sz1> data, const size_t shift) {
return bits<sz1>::mk(data.v >> shift);
}
template<bitwidth sz1>
bits<sz1> operator<<(const bits<sz1> data, const size_t shift) {
return mask(bits<sz1>::mk(data.v << shift));
}
template<bitwidth sz1, bitwidth sz2>
bits<sz1> operator>>(const bits<sz1> data, const bits<sz2> shift) {
return bits<sz1>::mk(data.v >> shift.v);
}
template<bitwidth sz1, bitwidth sz2>
bits<sz1> operator<<(const bits<sz1> data, const bits<sz2> shift) {
return mask(bits<sz1>::mk(data.v << shift.v));
}
static _unused bits<1> operator!(const bits<1> x) {
return bits<1>::mk(!x.v);
}
template<bitwidth sz>
bits<1> operator==(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v == y.v);
}
template<typename T, size_t len>
bits<1> operator==(const array<T, len> x, const array<T, len> y) {
return bits<1>::mk(static_cast<const std::array<T, len>&>(x) ==
static_cast<const std::array<T, len>&>(y));
}
template<bitwidth sz>
bits<1> operator!=(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v != y.v);;
}
template<bitwidth sz>
bits<sz> operator+(const bits<sz> x, const bits<sz> y) {
return mask(bits<sz>::mk(x.v + y.v));
}
template<bitwidth sz>
bits<sz> operator-(const bits<sz> x, const bits<sz> y) {
return mask(bits<sz>::mk(x.v + ~y.v + 1));
}
template<bitwidth sz_x, bitwidth sz_y>
bits<sz_x + sz_y> operator*(const bits<sz_x> x, const bits<sz_y> y) {
return mask(bits<sz_x + sz_y>::mk(widen<sz_x + sz_y>(x).v *
widen<sz_x + sz_y>(y).v));
}
template<bitwidth sz>
bits<1> operator<(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v < y.v);
}
template<bitwidth sz>
bits<1> operator>(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v > y.v);
}
template<bitwidth sz>
bits<1> operator<=(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v <= y.v);
}
template<bitwidth sz>
bits<1> operator>=(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.v >= y.v);
}
template<bitwidth sz>
bits<1> slt(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.to_shifted_sbits() < y.to_shifted_sbits());
}
template<bitwidth sz>
bits<1> sgt(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.to_shifted_sbits() > y.to_shifted_sbits());
}
template<bitwidth sz>
bits<1> sle(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.to_shifted_sbits() <= y.to_shifted_sbits());
}
template<bitwidth sz>
bits<1> sge(const bits<sz> x, const bits<sz> y) {
return bits<1>::mk(x.to_shifted_sbits() >= y.to_shifted_sbits());
}
template<bitwidth sz1, bitwidth sz2>
bits<sz1 + sz2> concat(const bits<sz1> x, const bits<sz2> y) {
return widen<sz1 + sz2>(x) << sz2 | widen<sz1 + sz2>(y);
}
template<bitwidth sz>
bits<sz> operator~(const bits<sz> data) {
return mask(bits<sz>::mk(~data.v));
}
template<bitwidth width, bitwidth sz>
bits<std::max(sz, width)> sext(const bits<sz> x) {
constexpr bitwidth maxsz = std::max(sz, width);
constexpr bitwidth nbits = width >= sz ? width - sz : bitwidth{0};
// Implementation-defined, assumes that the compiler does an arithmetic shift
return bits<maxsz>::of_shifted_sbits((widen<maxsz>(x) << nbits).to_shifted_sbits() >> nbits);
}
template<bitwidth width, bitwidth sz>
bits<std::max(sz, width)> zextl(const bits<sz> x) {
return widen<std::max(sz, width)>(x);
}
template<bitwidth width, bitwidth sz>
bits<std::max(sz, width)> zextr(const bits<sz> x) {
constexpr bitwidth maxsz = std::max(sz, width);
return widen<maxsz>(x) << (maxsz - sz);
}
template<bitwidth times, bitwidth sz> struct repeat_t {
static bits<sz * times> v(bits<sz> bs) {
return concat(repeat_t<times - 1, sz>::v(bs), bs);
};
};
template<bitwidth sz> struct repeat_t<0, sz> {
static bits<0> v(bits<sz> /*unused*/) { return tt; };
};
template<bitwidth times> struct repeat_t<times, 1> {
static bits<times> v(bits<1> bs) { return sext<times>(bs); };
};
template<bitwidth sz> struct repeat_t<1, sz> {
static constexpr auto v(bits<sz> bs) { return bs; }
};
template<bitwidth times, bitwidth sz>
bits<sz * times> repeat(const bits<sz> bs) {
return repeat_t<times, sz>::v(bs);
}
template<bitwidth idx, bitwidth width, bitwidth sz1>
bits<width> slice(const bits<sz1> data) {
return truncate<width>(data >> idx);
}
template<size_t pos, typename T, size_t len>
array<T, len> replace(const array<T, len> arr, T val) {
array<T, len> copy = arr;
copy[pos] = val;
return copy;
}
template<bitwidth sz>
bits<sz>& bits<sz>::operator&=(const bits<sz> arg) { return (*this = *this & arg); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator|=(const bits<sz> arg) { return (*this = *this | arg); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator^=(const bits<sz> arg) { return (*this = *this ^ arg); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator+=(const bits<sz> arg) { return (*this = *this + arg); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator-=(const bits<sz> arg) { return (*this = *this - arg); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator<<=(const size_t shift) { return (*this = *this << shift); }
template<bitwidth sz>
bits<sz>& bits<sz>::operator>>=(const size_t shift) { return (*this = *this >> shift); }
template<bitwidth sz> template<bitwidth shift_sz>
bits<sz>& bits<sz>::operator<<=(const bits<shift_sz> shift) { return (*this = *this << shift); }
template<bitwidth sz> template<bitwidth shift_sz>
bits<sz>& bits<sz>::operator>>=(const bits<shift_sz> shift) { return (*this = *this >> shift); }
/// ## Display functions
enum fmtstyle { full, hex, dec, bin };
struct fmtopts {
bool strings;
bool newline;
fmtstyle style;
};
static const _unused fmtopts default_fmtopts{ true, true, fmtstyle::full };
template<typename T>
static _unused _display_unoptimized unit display(const _unused T& msg,
const _unused fmtopts opts = default_fmtopts) {
#ifndef SIM_MINIMAL
fmt(std::cout, msg, opts);
std::cout << std::endl;
#endif
return tt;
}
#ifndef SIM_MINIMAL
namespace internal {
template<typename T, size_t len>
std::string string_of_bytestring(const array<T, len>& val) {
std::string s{};
for (size_t pos = 0; pos < len; pos ++) {
s.push_back(static_cast<char>(val[pos].v));
}
return s;
}
} // namespace internal
#endif
template<size_t len>
static _unused _display_unoptimized unit putstring(const _unused array<bits<8>, len>& msg) {
#ifndef SIM_MINIMAL
std::cout << internal::string_of_bytestring(msg);
#endif
return tt;
}
/// ## Other primitives
template<typename T>
unit ignore(const T /*unused*/) {
return tt;
}
/// ## Type info
template<typename T> struct type_info;
template<bitwidth sz> struct type_info<bits<sz>> {
static constexpr bitwidth size = sz;
};
template<typename T, size_t len> struct type_info<array<T, len>> {
static constexpr bitwidth size{len * type_info<T>::size};
};
/// ## Packing and unpacking
// Forward-declared; our compiler defines one instance per struct and enum.
// Unpack needs to be structs to get return-type polymorphism through explicit
// template instantiation. Pack is a struct to make overloading it easier.
template<typename T, bitwidth sz> struct _unpack;
template<typename T> static bits<type_info<T>::size> pack(const T val);
template<typename T, bitwidth sz>
static T unpack(const bits<sz>& bs) {
return _unpack<T, sz>::unpack(bs);
}
/// ### Bits packing and unpacking (no-op, but needed by array packing/unpacking)
template<bitwidth sz>
static bits<sz> pack(const bits<sz> val) {
return val;
}
template<bitwidth sz, bitwidth packed_sz>
struct _unpack<bits<sz>, packed_sz> {
static_assert(sz == packed_sz, "Inconsistent size parameters in call to unpack");
static bits<sz> unpack(const bits<packed_sz> bs) {
return bs;
}
};
/// ### Array packing and unpacking
template<typename T, size_t len>
static bits<type_info<array<T, len>>::size> pack(const array<T, len>& val) {
constexpr bitwidth elem_sz = type_info<T>::size;
constexpr bitwidth packed_sz = type_info<array<T, len>>::size;
bits<packed_sz> packed{};
for (size_t pos = 0; pos < len; pos++) {
packed <<= elem_sz;
packed |= prims::widen<packed_sz>(prims::pack(val[pos]));
}
return packed;
}
template<typename T, size_t len, bitwidth packed_sz>
struct _unpack<array<T, len>, packed_sz> {
// We need a struct for return-type polymorphism
static constexpr bitwidth elem_sz = type_info<T>::size;
static constexpr bitwidth expected_sz = len * elem_sz;
static_assert(expected_sz == packed_sz,
"Inconsistent size parameters in call to unpack");
static array<T, len> unpack(bits<packed_sz> bs) { // not const&
array<T, len> unpacked{};
for (size_t pos = 0; pos < len; pos++) { // FIXME check the order of elements
unpacked[len - 1 - pos] = prims::unpack<T>(truncate<elem_sz>(bs));
bs >>= elem_sz;
}
return unpacked;
}
};
/// ## Printing
/// ### Bitvector-printing functions
#ifndef SIM_MINIMAL
// This convenience function creates a string from an object
template<typename T>
std::string repr(const T& val, const fmtopts opts = default_fmtopts) {
std::ostringstream stream;
fmt(stream, val, opts);
return stream.str();
}
// This default overload passes a default set of format options
template<typename T>
std::ostream& fmt(std::ostream& os, const T& val) {
return fmt(os, val, default_fmtopts);
}
enum class prefixes { sized, plain, minimal };
namespace internal {
template<bitwidth sz>
static std::ostream& bits_fmt(std::ostream& os, const bits<sz>& val,
const fmtstyle style, const prefixes prefix) {
if (prefix == prefixes::sized) {
os << sz << "'";
}
switch (style) {
case fmtstyle::bin:
os << (prefix == prefixes::plain ? "0b" : "b");
if (val == bits<sz>{0}) {
os << "0";
} else {
for (bitwidth pos = sz; pos > 0; pos--) {
unsigned int bit = prims::truncate<1>(val >> (pos - 1u)).v;
os << bit;
}
}
break;
case fmtstyle::hex:
os << (prefix == prefixes::plain ? "0x" : "x");
os << std::hex << +val.v << std::dec;
break;
case fmtstyle::dec:
os << std::dec << +val.v;
break;
case fmtstyle::full:
if (sz <= 64) {
bits_fmt(os, val, fmtstyle::bin, prefixes::minimal);
os << " ("; bits_fmt(os, val, fmtstyle::hex, prefixes::plain);
os << ", "; bits_fmt(os, val, fmtstyle::dec, prefixes::plain);
os << ")";
} else {
bits_fmt(os, val, fmtstyle::hex, prefixes::minimal);
}
break;
}
return os;
}
} // namespace internal
template<bitwidth sz>
static std::ostream& fmt(std::ostream& os, const bits<sz>& val, const fmtopts opts) {
return internal::bits_fmt(os, val, opts.style, prefixes::sized);
}
template<bitwidth sz>
std::ostream& operator<<(std::ostream& os, const bits<sz>& bs) {
return fmt(os, bs);
}
/// ### Array printing functions
namespace internal {
template<typename T>
static std::ostream& array_fmt(std::ostream& os, const std::size_t len, const T* val, fmtopts opts) {
if (opts.style == fmtstyle::full) {
opts.style = fmtstyle::hex;
}
os << "[";
if (len != 0) {
fmt(os, val[0], opts);
for (size_t pos = 1; pos < len; pos++) {
os << "; ";
fmt(os, val[pos], opts);
}
}
os << "]";
return os;
}
} // namespace internal
template<typename T, size_t len>
static std::ostream& fmt(std::ostream& os, const array<T, len>& val, const fmtopts opts) {
return internal::array_fmt(os, len, val.data(), opts);
}
template<size_t len>
static std::ostream& fmt(std::ostream& os, const array<bits<8>, len>& val, const fmtopts opts) {
if (opts.strings) {
os << "\"" << std::hex << std::setfill('0');
for (size_t pos = 0; pos < len; pos ++) {
unsigned char chr = static_cast<unsigned char>(val[pos].v);
if (chr == '\\' || chr == '"') {
os << "\\" << chr;
} else if (std::isgraph(chr)) {
os << chr;
} else {
os << "\\x" << std::setw(2) << static_cast<unsigned>(chr);
}
}
os << "\"";
} else {
internal::array_fmt(os, len, val.data(), opts);
}
return os;
}
template<typename T, size_t len>
std::ostream& operator<<(std::ostream& os, const array<T, len>& val) {
return fmt(os, val);
}
#endif // #ifndef SIM_MINIMAL