gateutil 0.1.0

The library with basic utilities for GateSim.
Documentation
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
use gatesim::*;
use gateutil::*;

use std::str::FromStr;

#[test]
fn test_min_and_max_depth_list() {
    assert_eq!(
        (
            vec![
                (0, 0),
                (0, 0),
                (0, 0),
                (0, 0),
                (1, 1),
                (1, 1),
                (1, 1),
                (1, 1),
                (2, 2),
                (2, 2),
                (3, 3),
                (3, 4)
            ],
            3,
            4
        ),
        min_and_max_depth_list(
            &Circuit::new(
                4,
                [
                    Gate::new_xor(1, 2),
                    Gate::new_xor(0, 3),
                    Gate::new_nor(1, 2),
                    Gate::new_nor(0, 3),
                    Gate::new_and(4, 5),
                    Gate::new_and(6, 7),
                    Gate::new_nimpl(8, 9),
                    Gate::new_nimpl(9, 10),
                ],
                [(11, true)],
            )
            .unwrap()
        )
    );
    assert_eq!(
        (
            vec![
                (0, 0),
                (0, 0),
                (0, 0),
                (0, 0),
                (0, 0),
                (0, 0),
                (1, 1),
                (1, 1),
                (1, 2),
                (1, 1),
                (2, 2),
                (2, 3),
                (1, 2),
                (2, 3),
                (1, 4),
            ],
            1,
            4
        ),
        min_and_max_depth_list(
            &Circuit::new(
                6,
                [
                    Gate::new_and(0, 1),
                    Gate::new_nimpl(2, 3),
                    Gate::new_xor(7, 4),
                    Gate::new_nor(1, 2),
                    Gate::new_nimpl(9, 6),
                    Gate::new_nimpl(10, 7),
                    Gate::new_nimpl(6, 5),
                    Gate::new_nimpl(8, 12),
                    Gate::new_nimpl(11, 5),
                ],
                [(10, false), (8, true), (13, false), (14, false)]
            )
            .unwrap()
        )
    );
}

#[test]
fn test_simple_pipeliner() {
    assert_eq!(
        (
            Circuit::new(
                4,
                [
                    Gate::new_xor(1, 2),
                    Gate::new_xor(0, 3),
                    Gate::new_nor(1, 2),
                    Gate::new_nor(0, 3),
                    Gate::new_and(4, 5),
                    Gate::new_and(6, 7),
                    Gate::new_nimpl(8, 9),
                    Gate::new_nimpl(9, 10),
                ],
                [(11, true)],
            )
            .unwrap(),
            1
        ),
        simple_pipeliner(
            Circuit::new(
                4,
                [
                    Gate::new_xor(1, 2),
                    Gate::new_xor(0, 3),
                    Gate::new_nor(1, 2),
                    Gate::new_nor(0, 3),
                    Gate::new_and(4, 5),
                    Gate::new_and(6, 7),
                    Gate::new_nimpl(8, 9),
                    Gate::new_nimpl(9, 10),
                ],
                [(11, true)],
            )
            .unwrap(),
            5
        )
    );
    assert_eq!(
        (
            Circuit::new(
                6,
                [
                    Gate::new_xor(3, 4),
                    Gate::new_xor(2, 5),
                    Gate::new_nor(3, 4),
                    Gate::new_nor(2, 5),
                    Gate::new_and(6, 7),
                    Gate::new_and(8, 9),
                    Gate::new_nimpl(0, 1),
                    Gate::new_nimpl(1, 12),
                ],
                [(10, false), (11, false), (13, true)],
            )
            .unwrap(),
            2
        ),
        simple_pipeliner(
            Circuit::new(
                4,
                [
                    Gate::new_xor(1, 2),
                    Gate::new_xor(0, 3),
                    Gate::new_nor(1, 2),
                    Gate::new_nor(0, 3),
                    Gate::new_and(4, 5),
                    Gate::new_and(6, 7),
                    Gate::new_nimpl(8, 9),
                    Gate::new_nimpl(9, 10),
                ],
                [(11, true)],
            )
            .unwrap(),
            2
        )
    );
    assert_eq!(
        (
            Circuit::from_str(
                r##"    {
        0:30
        1:31
        2:32
        3
        4
        5
        6
        7
        8:35
        9:37
        10:38
        11:39
        12:40
        13:41
        14:42
        15:43
        16:44
        17:45
        18:46
        19:47
        20:48
        21:49
        22:50
        23:51
        24:52
        25:53
        26:54
        27:55
        28:56
        29:57
        30:58
        31:59
        32:60
        33:61
        34:62
        35
        36
        37
        38
        39
        40:65
        41:67
        42:68
        43:69
        44:70
        45:71
        46:72
        47:73
        48:74
        49:75
        50:76
        51:77
        52:78
        53:79
        54:80
        55:81
        56:82
        57:83
        58:84
        59:85
        60:86
        61:87
        62:88
        63:89
        64:90
        65
        66
        67
        68
        69
        70:93
        71:95
        72:96
        73:97
        74:98
        75:99
        76:100
        77:101
        78:102
        79:103
        80:104
        81:105
        82:106
        83:107
        84:108
        85:109
        86:110
        87:111
        88:112
        89:113
        90:114
        91:115
        92:116
        93
        94
        95
        96
        97
        98:119
        99:121
        100:122
        101:123
        102:124
        103:125
        104:126
        105:127
        106:128
        107:129
        108:130
        109:131
        110:132
        111:133
        112:134
        113:135
        114:136
        115:137
        116:138
        117:139
        118:140
        119
        120
        121
        122
        123
        124:143
        125:145
        126:146
        127:147
        128:148
        129:149
        130:150
        131:151
        132:152
        133:153
        134:154
        135:155
        136:156
        137:157
        138:158
        139:159
        140:160
        141:161
        142:162
        143
        144
        145
        146
        147
        148:165
        149:167
        150:168
        151:169
        152:170n
        153:171n
        154:172n
        155:173n
        156:174n
        157:175n
        158:176n
        159:177n
        160:178n
        161:179n
        162:180n
        163:181n
        164:182n
        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
        xor(168,184):0
        xor(169,185)
        and(168,184)
        xor(170,186)
        and(169,185)
        xor(171,187):3
        and(170,186):5
        xor(172,188):6
        and(171,187):7
        xor(173,189):8
        and(172,188):9
        xor(174,190):10
        and(173,189):11
        xor(175,191):12
        and(174,190):13
        xor(176,192):14
        and(175,191):15
        xor(177,193):16
        and(176,192):17
        xor(178,194):18
        and(177,193):19
        xor(179,195):20
        and(178,194):21
        xor(180,196):22
        and(179,195):23
        xor(181,197):24
        and(180,196):25
        xor(182,198):26
        and(181,197):27
        xor(183,199):28
        and(182,198):29
        xor(201,202):1
        and(201,202)
        nor(232,204)
        xor(203,233):2
        nimpl(203,233):4
        nor(4,5)
        xor(3,236):33
        nimpl(3,236)
        nor(238,7)
        xor(6,239):34
        nimpl(6,239):36
        nor(36,37)
        xor(35,242):63
        nimpl(35,242)
        nor(244,39)
        xor(38,245):64
        nimpl(38,245):66
        nor(66,67)
        xor(65,248):91
        nimpl(65,248)
        nor(250,69)
        xor(68,251):92
        nimpl(68,251):94
        nor(94,95)
        xor(93,254):117
        nimpl(93,254)
        nor(256,97)
        xor(96,257):118
        nimpl(96,257):120
        nor(120,121)
        xor(119,260):141
        nimpl(119,260)
        nor(262,123)
        xor(122,263):142
        nimpl(122,263):144
        nor(144,145)
        xor(143,266):163
        nimpl(143,266)
        nor(268,147)
        xor(146,269):164
        nimpl(146,269):166
        nor(166,167)
        xor(165,272):183n
    }(200)
"##
            )
            .unwrap(),
            8
        ),
        simple_pipeliner(
            Circuit::<usize>::from_str(
                r##"    {
        0
        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
        xor(0,16):0
        xor(1,17)
        and(0,16)
        xor(33,34):1
        xor(2,18)
        and(33,34)
        and(1,17)
        nor(37,38)
        xor(36,39):2n
        xor(3,19)
        nimpl(36,39)
        and(2,18)
        nor(42,43)
        xor(41,44):3n
        xor(4,20)
        nimpl(41,44)
        and(3,19)
        nor(47,48)
        xor(46,49):4n
        xor(5,21)
        nimpl(46,49)
        and(4,20)
        nor(52,53)
        xor(51,54):5n
        xor(6,22)
        nimpl(51,54)
        and(5,21)
        nor(57,58)
        xor(56,59):6n
        xor(7,23)
        nimpl(56,59)
        and(6,22)
        nor(62,63)
        xor(61,64):7n
        xor(8,24)
        nimpl(61,64)
        and(7,23)
        nor(67,68)
        xor(66,69):8n
        xor(9,25)
        nimpl(66,69)
        and(8,24)
        nor(72,73)
        xor(71,74):9n
        xor(10,26)
        nimpl(71,74)
        and(9,25)
        nor(77,78)
        xor(76,79):10n
        xor(11,27)
        nimpl(76,79)
        and(10,26)
        nor(82,83)
        xor(81,84):11n
        xor(12,28)
        nimpl(81,84)
        and(11,27)
        nor(87,88)
        xor(86,89):12n
        xor(13,29)
        nimpl(86,89)
        and(12,28)
        nor(92,93)
        xor(91,94):13n
        xor(14,30)
        nimpl(91,94)
        and(13,29)
        nor(97,98)
        xor(96,99):14n
        xor(15,31)
        nimpl(96,99)
        and(14,30)
        nor(102,103)
        xor(101,104):15n
    }(32)
"##
            )
            .unwrap(),
            4
        )
    );
    // next testcase
    assert_eq!(
        (
            Circuit::from_str(
                r##"{
        0
        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
        and(10,11)
        nor(12,13)
        xor(14,15)
        nimpl(16,17)
        nor(18,19)
        nimpl(21,20)
        xor(22,23)
        nor(24,25)
        nimpl(26,27)
        xor(28,29)
        and(30,31)
        nor(32,33)
        nor(34,35)
        nimpl(37,36)
        xor(38,39)
        and(40,41)
        nor(42,43):0
        nimpl(44,45):1
        xor(46,47):2
        and(48,49):3
        xor(50,51):4
        nor(52,53):5
        nimpl(55,54):6
        xor(56,57):7
        nor(0,1)
        nor(2,3)
        xor(4,5)
        and(6,7)
        nimpl(67,66):8
        nimpl(68,69):9
        and(8,9):10
    }(42)
"##
            )
            .unwrap(),
            3
        ),
        simple_pipeliner(
            Circuit::<usize>::from_str(
                r##"    {
                0 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
                and(0,1)
                nor(2,3)
                xor(4,5)
                nimpl(6,7)
                nor(8,9)
                nimpl(11,10)
                xor(12,13)
                nor(14,15)
                nimpl(16,17)
                xor(18,19)
                and(20,21)
                nor(22,23)
                nor(24,25)
                nimpl(27,26)
                xor(28,29)
                and(30,31)
                nor(32,33)
                nimpl(34,35)
                xor(36,37)
                and(38,39)
                xor(40,41)
                nor(42,43)
                nimpl(45,44)
                xor(46,47)
                nor(48,49)
                nor(50,51)
                xor(52,53)
                and(54,55)
                nimpl(57,56)
                nimpl(58,59)
                and(60,61):0
}(32)
"##
            )
            .unwrap(),
            2,
        )
    );
    // fullmul 4x4
    assert_eq!(
        (
            Circuit::from_str(
                r##"    {
        0:16
        1:17
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16:28
        17:29
        18:30
        19:31
        20
        21
        22
        23:33
        24
        25:35
        26:36
        27:37
        28:38
        29:39
        30:40
        31:41
        32:42
        33
        34
        35
        36
        37:46
        38:47
        39:48
        40:49
        41:50n
        42:51
        43:52
        44:53
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        and(47,51):0
        and(47,52)
        and(48,51)
        and(47,53)
        and(48,52)
        and(49,51)
        and(47,54)
        and(48,53)
        and(49,52)
        and(50,51)
        and(48,54)
        and(49,53)
        and(50,52)
        and(49,54)
        and(50,53)
        and(50,54):13
        xor(56,57):1
        xor(59,60)
        and(56,57):3
        xor(61,62)
        xor(63,64)
        and(59,60):5
        xor(66,67)
        and(63,64):8
        and(61,62):10
        xor(68,69)
        and(66,67)
        and(68,69):15
        xor(58,72):2
        xor(74,75):4
        and(58,72):6
        xor(65,77):7
        and(74,75):9
        xor(80,81):11
        and(65,77):12
        and(80,81):14
        xor(2,3):18
        xor(4,5)
        and(2,3)
        xor(7,8)
        nor(9,10)
        and(4,5):22
        and(7,8)
        nor(14,15)
        nor(93,6)
        xor(94,95):20
        nor(97,12)
        nimpl(94,95):24
        xor(13,98):25
        nimpl(13,98):27
        xor(92,99):19
        nimpl(92,99):21
        xor(11,101):23
        nimpl(11,101):26
        nor(21,22)
        xor(20,109):32
        nor(20,109)
        nor(111,24):34
        xor(33,34):43
        nor(33,34)
        nor(114,36)
        xor(35,115):44
        nor(35,115):45
        nor(45,46):54n
    }(55)
"##
            )
            .unwrap(),
            5
        ),
        simple_pipeliner(
            Circuit::<usize>::from_str(
                r##"
    {
        0
        1
        2
        3
        4
        5
        6
        7
        and(0,4):0
        and(0,5)
        and(1,4)
        xor(9,10):1
        and(0,6)
        and(1,5)
        and(2,4)
        xor(13,14)
        xor(12,15)
        and(9,10)
        xor(16,17):2
        and(0,7)
        and(1,6)
        xor(19,20)
        and(2,5)
        and(3,4)
        xor(22,23)
        xor(21,24)
        and(13,14)
        xor(25,26)
        and(16,17)
        and(12,15)
        nor(28,29)
        xor(27,30):3n
        and(1,7)
        and(2,6)
        and(3,5)
        xor(33,34)
        xor(32,35)
        and(22,23)
        xor(36,37)
        and(21,24)
        and(19,20)
        nor(39,40)
        xor(38,41)
        nimpl(27,30)
        and(25,26)
        nor(43,44)
        xor(42,45):4
        and(2,7)
        and(3,6)
        xor(47,48)
        and(33,34)
        xor(49,50)
        and(36,37)
        and(32,35)
        nor(52,53)
        xor(51,54)
        nor(42,45)
        nimpl(38,41)
        nor(56,57)
        xor(55,58):5
        and(3,7)
        and(49,50)
        and(47,48)
        nor(61,62)
        xor(60,63)
        nor(55,58)
        nimpl(51,54)
        nor(65,66)
        xor(64,67):6
        nor(64,67)
        nimpl(60,63)
        nor(69,70):7n
    }(8)
"##
            )
            .unwrap(),
            3
        )
    );
}