-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
1540 lines (1346 loc) · 45.8 KB
/
build.rs
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
use convert_case::{self, Case, Casing};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;
use rand::Rng;
use std::fs;
use std::ops::Range;
use std::path::{Path, PathBuf};
/// Runs the build script.
fn main() -> std::io::Result<()> {
println!("cargo:rerun-if-changed=build.rs");
write_all_files("./src/")
}
/// Writes all files to a directory.
pub fn write_all_files(dir: &str) -> std::io::Result<()> {
let pth = Path::new(dir);
fs::create_dir_all(pth)?;
write_one_fle(emit_main_fle(), &pth.join("main.rs"))?;
Ok(())
}
/// Writes a token stream to a file.
pub fn write_one_fle(fle_stm: TokenStream, fle_pth: &PathBuf) -> std::io::Result<()> {
let fle = syn::parse_file(fle_stm.to_string().as_str()).unwrap();
let fmt = prettyplease::unparse(&fle);
fs::write(fle_pth, fmt)
}
pub fn emit_imports() -> TokenStream {
let mut stm = TokenStream::new();
stm.extend(quote! {
#![allow(clippy::into_iter_on_ref)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::slow_vector_initialization)]
#![allow(dead_code)]
#![allow(unused_imports)]
use anyhow::{bail, Result};
use ben::*;
use ben::Sta::*;
use itr::*;
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::sync::Arc;
use std::sync::mpsc::channel;
use std::thread::{self, JoinHandle};
use threadpool::ThreadPool;
use Lbl::*;
});
stm
}
pub fn emit_main_fle() -> TokenStream {
let tok_fns = [
emit_imports,
emit_main_fn,
emit_new_stdy,
emit_lbl_enum,
emit_lbl_impl_display,
emit_lbl_impl_enumstructval,
emit_lbl_impl_label,
];
let ret = tok_fns.iter().fold(TokenStream::new(), |mut stm, tok_fn| {
stm.extend(tok_fn());
stm
});
ret
}
/// Returns label strings for all enum cases.
pub fn lbl_strs_all() -> Vec<&'static str> {
let mut ret = lbl_strs_plain();
ret.extend(lbl_strs_struct_u32());
ret
}
/// Returns label strings mapping to a plain enum cases.
pub fn lbl_strs_plain() -> Vec<&'static str> {
vec![
"add", "alc", "arr", "chk", "cnt", "cst", "idx", "into_itr", "itr", "join", "lop", "mat",
"mcr", "mpsc", "none", "one", "ptr", "rnd", "rd", "rsz", "seq", "slc", "u8", "unchk", "usize",
"val", "vct",
]
}
/// Returns label strings which map to struct u32 cases of an enum.
pub fn lbl_strs_struct_u32() -> Vec<&'static str> {
vec!["len", "acm", "unr", "thd"]
}
pub const LBL_NAM: &str = "Lbl";
pub fn emit_lbl_enum() -> TokenStream {
let mut stm = TokenStream::new();
let idn_lbl = Ident::new(LBL_NAM, Span::call_site());
// enum: start
stm.extend(quote! {
/// Benchmark labels.
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum #idn_lbl
});
// enum: inner
let mut stm_inr = TokenStream::new();
for lbl_str in lbl_strs_plain() {
let idn = Ident::new(lbl_str.to_case(Case::Pascal).as_str(), Span::call_site());
stm_inr.extend(quote! {
#idn,
});
}
for lbl_str in lbl_strs_struct_u32() {
let idn = Ident::new(lbl_str.to_case(Case::Pascal).as_str(), Span::call_site());
stm_inr.extend(quote! {
#idn(u32),
});
}
// enum: end
stm.extend(quote! {
{
#stm_inr
}
});
stm
}
pub fn emit_lbl_impl_display() -> TokenStream {
let mut stm_0 = TokenStream::new();
let mut stm_1 = TokenStream::new();
let mut stm_2 = TokenStream::new();
let mut stm_3 = TokenStream::new();
let idn_lbl = Ident::new(LBL_NAM, Span::call_site());
stm_0.extend(quote! { impl fmt::Display for #idn_lbl });
stm_1.extend(quote! { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result });
stm_2.extend(quote! { match *self });
for lbl_str in lbl_strs_plain() {
let idn = Ident::new(lbl_str.to_case(Case::Pascal).as_str(), Span::call_site());
let lit = Literal::string(lbl_str);
stm_3.extend(quote! {
#idn => write!(f, #lit),
});
}
for lbl_str in lbl_strs_struct_u32() {
let idn = Ident::new(lbl_str.to_case(Case::Pascal).as_str(), Span::call_site());
let mut tmp = String::from(lbl_str);
tmp.push_str("({})");
let lit = Literal::string(tmp.as_str());
let lit_alt = Literal::string(lbl_str);
stm_3.extend(quote! {
#idn(x) => {
if f.alternate() {
write!(f, #lit_alt)
} else {
write!(f, #lit, x)
}
},
});
}
stm_2.extend(quote! {
{
#stm_3
}
});
stm_1.extend(quote! {
{
#stm_2
}
});
stm_0.extend(quote! {
{
#stm_1
}
});
stm_0
}
pub fn emit_lbl_impl_enumstructval() -> TokenStream {
let mut stm_0 = TokenStream::new();
let mut stm_1 = TokenStream::new();
let mut stm_2 = TokenStream::new();
let mut stm_3 = TokenStream::new();
let idn_lbl = Ident::new(LBL_NAM, Span::call_site());
stm_0.extend(quote! { impl EnumStructVal for #idn_lbl });
stm_1.extend(quote! { fn val(&self) -> Result<u32> });
stm_2.extend(quote! { match *self });
for lbl_str in lbl_strs_struct_u32() {
let idn = Ident::new(lbl_str.to_case(Case::Pascal).as_str(), Span::call_site());
stm_3.extend(quote! {
#idn(x) => Ok(x),
});
}
stm_3.extend(quote! { _ => bail!("label '{}' isn't a struct enum", self), });
stm_2.extend(quote! {
{
#stm_3
}
});
stm_1.extend(quote! {
{
#stm_2
}
});
stm_0.extend(quote! {
{
#stm_1
}
});
stm_0
}
pub fn emit_lbl_impl_label() -> TokenStream {
let mut stm = TokenStream::new();
let idn_lbl = Ident::new(LBL_NAM, Span::call_site());
stm.extend(quote! {
impl Label for #idn_lbl { }
});
stm
}
pub fn emit_main_fn() -> TokenStream {
let mut stm = TokenStream::new();
stm.extend(quote! {
/// Runs a benchmark function analysis.
pub fn main() -> Result<()> {
let mut stdy = new_stdy()?;
let itr: u16 = 64;
let mut qry = QryBld::new();
// Allocation: array vs vector macro
let alc_arr_id = qry.sel(&[Alc, Arr]);
let alc_vct_mcr_id = qry.sel(&[Alc, Vct, Mcr]);
qry.cmp(alc_arr_id, alc_vct_mcr_id);
// Allocation: array vs vector capacity and resize
let alc_vct_rsz_id = qry.sel(&[Alc, Vct, Rsz]);
qry.cmp(alc_arr_id, alc_vct_rsz_id);
// Allocation: vector macro vs vector capacity and resize
qry.cmp(alc_vct_mcr_id, alc_vct_rsz_id);
// Lookup: Sequential: array vs match
let rd_seq_arr_id = qry.sel(&[Rd, Seq, Arr]);
let rd_seq_mat_id = qry.sel(&[Rd, Seq, Mat]);
qry.cmp(rd_seq_arr_id, rd_seq_mat_id);
// Lookup: Random: array vs match
let rd_rnd_arr_id = qry.sel(&[Rd, Rnd, Arr]);
let rd_rnd_mat_id = qry.sel(&[Rd, Rnd, Mat]);
qry.cmp(rd_rnd_arr_id, rd_rnd_mat_id);
// Iteration: range index bounds checked vs range index unchecked
let lop_idx_chk_id = qry.sel(&[Lop, Idx, Chk]);
let lop_idx_unchk_id = qry.sel(&[Lop, Idx, Unchk]);
qry.cmp(lop_idx_chk_id, lop_idx_unchk_id);
// Iteration: range index (bounds checked) vs iterator
let lop_vct_itr_id = qry.sel(&[Lop, Vct, Itr]);
qry.cmp(lop_idx_chk_id, lop_vct_itr_id);
// Iteration: Vector: iterator vs into iterator
let lop_vct_intoitr_id = qry.sel(&[Lop, Vct, IntoItr]);
qry.cmp(lop_vct_itr_id, lop_vct_intoitr_id);
// Iteration: Slice: iterator vs into iterator
let lop_slc_itr_id = qry.sel(&[Lop, Slc, Itr]);
let lop_slc_intoitr_id = qry.sel(&[Lop, Slc, IntoItr]);
qry.cmp(lop_slc_itr_id, lop_slc_intoitr_id);
// Cast: u8 vs usize
let cst_u8_id = qry.sel(&[Cst, U8]);
let cst_usize_id = qry.sel(&[Cst, Usize]);
qry.cmp(cst_u8_id, cst_usize_id);
// Accumulate: read pointer vs read de-referenced value
let acm_rd_ptr_id = qry.sel(&[Acm(1), Rd, Ptr]);
let acm_rd_val_id = qry.sel(&[Acm(1), Rd, Val]);
qry.cmp(acm_rd_ptr_id, acm_rd_val_id);
// Accumulate: total count vs multiple add one
let acm_add_cnt_id = qry.sel(&[Acm(1), Add, Cnt]);
let acm_add_one_id = qry.sel(&[Acm(1), Add, One]);
qry.cmp(acm_add_cnt_id, acm_add_one_id);
// Accumulate: acm 1, unr 1, thd 1 vs acm 2, unr 2, thd 1
let acm1_unr1_thd1 = qry.sel(&[Acm(1), Unr(1), Thd(1)]);
let acm2_unr2_thd1 = qry.sel(&[Acm(2), Unr(2), Thd(1)]);
qry.cmp(acm1_unr1_thd1, acm2_unr2_thd1);
// Accumulate: acm 1, unr 1, thd 1 vs acm 1, unr 8, thd 1
let acm1_unr8_thd1 = qry.sel(&[Acm(1), Unr(8), Thd(1)]);
qry.cmp(acm1_unr1_thd1, acm1_unr8_thd1);
// Accumulate: unr 1, var 1 vs unr 8, var 8
let acm8_unr8_thd1 = qry.sel(&[Acm(8), Unr(8), Thd(1)]);
qry.cmp(acm1_unr1_thd1, acm8_unr8_thd1);
// Accumulate: unr 8, var 8 vs unr 16, var 16
let acm16_unr16_thd1 = qry.sel(&[Acm(16), Unr(16), Thd(1)]);
qry.cmp(acm8_unr8_thd1, acm16_unr16_thd1);
// Accumulate: unr 1, var 1 vs unr 16, var 16
qry.cmp(acm1_unr1_thd1, acm16_unr16_thd1);
// Accumulate: acm 1, unr 1, thd 2, join vs acm 1, unr 1, thd 2, mpsc
let acm1_unr1_thd2_join = qry.sel(&[Acm(1), Unr(1), Thd(2), Join]);
let acm1_unr1_thd2_mpsc = qry.sel(&[Acm(1), Unr(1), Thd(2), Mpsc]);
qry.cmp(acm1_unr1_thd2_join, acm1_unr1_thd2_mpsc);
// Accumulate: acm 1, unr 1, thd 2, mpsc vs acm 1, unr 1, thd 4, mpsc
let acm1_unr1_thd4_mpsc = qry.sel(&[Acm(1), Unr(1), Thd(4), Mpsc]);
qry.cmp(acm1_unr1_thd2_mpsc, acm1_unr1_thd4_mpsc);
// Accumulate: acm 1, unr 1, thd 4, mpsc vs acm 1, unr 1, thd 8, mpsc
let acm1_unr1_thd8_mpsc = qry.sel(&[Acm(1), Unr(1), Thd(8), Mpsc]);
qry.cmp(acm1_unr1_thd4_mpsc, acm1_unr1_thd8_mpsc);
// Accumulate: acm 1, unr 1, thd 8, mpsc vs acm 1, unr 1, thd 16, mpsc
let acm1_unr1_thd16_mpsc = qry.sel(&[Acm(1), Unr(1), Thd(16), Mpsc]);
qry.cmp(acm1_unr1_thd8_mpsc, acm1_unr1_thd16_mpsc);
qry.cmp(acm1_unr1_thd1, acm1_unr1_thd2_mpsc);
qry.cmp(acm1_unr1_thd1, acm1_unr1_thd4_mpsc);
qry.cmp(acm1_unr1_thd1, acm1_unr1_thd8_mpsc);
qry.cmp(acm1_unr1_thd1, acm1_unr1_thd16_mpsc);
stdy.run(qry, itr)?;
Ok(())
}
});
stm
}
pub fn emit_new_stdy() -> TokenStream {
let mut stm = TokenStream::new();
let idn_lbl = Ident::new(LBL_NAM, Span::call_site());
// fn: start
stm.extend(quote! {
/// Returns a study with registered benchmark functions.
pub fn new_stdy() -> Result<Stdy<#idn_lbl>>
});
// fn: inner
let mut stm_inr = TokenStream::new();
let tok_bens = [
emit_alc_arr,
emit_alc_vct_mcr,
emit_alc_vct_rsz,
emit_rd_seq_arr,
emit_rd_seq_mat,
emit_rd_rnd_arr,
emit_rd_rnd_mat,
emit_lop_idx_chk,
emit_lop_idx_unchk,
emit_lop_vec_itr,
emit_lop_vec_into_itr,
emit_lop_slc_itr,
emit_lop_slc_into_itr,
emit_cst_u8,
emit_cst_usize,
emit_acm_rd_ptr,
emit_acm_rd_val,
emit_acm_add_cnt,
emit_acm_add_one,
emit_acm1_unr1_thd1,
emit_acm2_unr2_thd1,
emit_acm1_unr8_thd1,
emit_acm8_unr8_thd1,
emit_acm16_unr16_thd1,
emit_acm1_unr1_thd2_join,
emit_acm1_unr1_thd2_mpsc,
emit_acm1_unr1_thd4_mpsc,
emit_acm1_unr1_thd8_mpsc,
emit_acm1_unr1_thd16_mpsc,
];
tok_bens
.iter()
.for_each(|tok_ben| stm_inr.extend(tok_ben()));
// fn: end
stm.extend(quote! {
{
let mut stdy = Stdy::new();
#stm_inr
Ok(stdy)
}
});
stm
}
pub static ALC_RNG: Range<u32> = 4..18;
pub fn emit_alc_arr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in ALC_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins(Len(#lit_len), || [0u32; #lit_len]);
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Alc, Arr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_alc_vct_mcr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in ALC_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins(Len(#lit_len), || vec![0u32; #lit_len]);
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Alc, Vct, Mcr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_alc_vct_rsz() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in ALC_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins(Len(#lit_len), || {
let mut ret = Vec::<u32>::with_capacity(#lit_len);
ret.resize(#lit_len, 0);
ret
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Alc, Vct, Rsz], |x| {
#stm_inr
});
});
stm
}
pub static RD_RNG: Range<u32> = 4..12;
pub fn emit_rd_seq_arr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in RD_RNG.clone().map(|x| 2u32.pow(x)) {
// Create an array with random elements.
let mut stm_arr = TokenStream::new();
let mut rng = rand::thread_rng();
for _ in 0..len {
let lit_ret_n = Literal::u32_unsuffixed(rng.gen_range(0..u32::MAX));
stm_arr.extend(quote! { #lit_ret_n, });
}
// Read each element from an array in sequence.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins(Len(#lit_len), || {
let arr = [#stm_arr];
let mut ret = [0u32; 1];
for idx in 0..#lit_len {
ret[0] = arr[idx];
}
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Rd, Seq, Arr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_rd_seq_mat() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
let mut rng = rand::thread_rng();
for len in RD_RNG.clone().map(|x| 2u32.pow(x)) {
// Create match arms which return a random u32.
let mut stm_arm = TokenStream::new();
for idx in 0..len {
let lit_idx = Literal::u32_unsuffixed(idx);
let lit_ret_n = Literal::u32_unsuffixed(rng.gen_range(0..u32::MAX));
stm_arm.extend(quote! {
#lit_idx => #lit_ret_n,
});
}
// Read each element from a match in sequence.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins(Len(#lit_len), || {
let mut ret = [0u32; 1];
for idx in 0..#lit_len {
ret[0] = match idx {
#stm_arm
_ => panic!("uh oh, no no: beyond the match limit"),
}
}
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Rd, Seq, Mat], |x| {
#stm_inr
});
});
stm
}
pub fn emit_rd_rnd_arr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
let mut rng = rand::thread_rng();
for len in RD_RNG.clone().map(|x| 2u32.pow(x)) {
// Create an array with random elements.
let mut stm_arr = TokenStream::new();
for _ in 0..len {
let lit_ret_n = Literal::u32_unsuffixed(rng.gen_range(0..u32::MAX));
stm_arr.extend(quote! { #lit_ret_n, });
}
// Read each element from an array in sequence.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let arr = [#stm_arr];
let mut idxs: Vec<usize> = (0..#lit_len).collect();
let mut rng = thread_rng();
idxs.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for idx in idxs {
ret[0] = arr[idx];
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Rd, Rnd, Arr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_rd_rnd_mat() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
let mut rng = rand::thread_rng();
for len in RD_RNG.clone().map(|x| 2u32.pow(x)) {
// Create match arms which return a random u32.
let mut stm_arm = TokenStream::new();
for idx in 0..len {
let lit_idx = Literal::u32_unsuffixed(idx);
let lit_ret_n = Literal::u32_unsuffixed(rng.gen_range(0..u32::MAX));
stm_arm.extend(quote! {
#lit_idx => #lit_ret_n,
});
}
// Read each element from an array in sequence.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut idxs: Vec<usize> = (0..#lit_len).collect();
let mut rng = thread_rng();
idxs.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for idx in idxs {
ret[0] = match idx {
#stm_arm
_ => panic!("uh oh, no no: beyond the match limit"),
}
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Rd, Rnd, Mat], |x| {
#stm_inr
});
});
stm
}
pub static LOP_RNG: Range<u32> = 4..18;
pub fn emit_lop_idx_chk() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Read each element from an array in sequence.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for idx in 0..#lit_len {
ret[0] = vals[idx];
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, Idx, Chk], |x| {
#stm_inr
});
});
stm
}
pub fn emit_lop_idx_unchk() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Iterate a for loop with range syntax 0..len.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
unsafe {
for idx in 0..#lit_len {
ret[0] = *vals.get_unchecked(idx);
}
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, Idx, Unchk], |x| {
#stm_inr
});
});
stm
}
pub fn emit_lop_vec_itr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Iterate a for loop with range syntax 0..len.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for val in vals.iter() {
ret[0] = *val;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, Itr, Vct], |x| {
#stm_inr
});
});
stm
}
pub fn emit_lop_vec_into_itr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Iterate a for loop with range syntax 0..len.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for val in vals.into_iter() {
ret[0] = val;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, IntoItr, Vct], |x| {
#stm_inr
});
});
stm
}
pub fn emit_lop_slc_itr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Iterate a for loop with range syntax 0..len.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for val in vals.as_slice().iter() {
ret[0] = *val;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, Slc, Itr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_lop_slc_into_itr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in LOP_RNG.clone().map(|x| 2u32.pow(x)) {
// Iterate a for loop with range syntax 0..len.
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0u32; 1];
tme.borrow_mut().start();
for val in vals.as_slice().into_iter() {
ret[0] = *val;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Lop, Slc, IntoItr], |x| {
#stm_inr
});
});
stm
}
pub static CST_RNG: Range<u32> = 4..18;
pub fn emit_cst_u8() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in CST_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0usize; 1];
tme.borrow_mut().start();
for val in vals.iter() {
ret[0] += ((*val > 0xFF) as u8 + (*val > 0xFFFF) as u8 + (*val > 0xFFFFFF) as u8) as usize;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Cst, U8], |x| {
#stm_inr
});
});
stm
}
pub fn emit_cst_usize() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in CST_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0usize; 1];
tme.borrow_mut().start();
for val in vals.iter() {
ret[0] += (*val > 0xFF) as usize + (*val > 0xFFFF) as usize + (*val > 0xFFFFFF) as usize;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Cst, Usize], |x| {
#stm_inr
});
});
stm
}
pub static ACM_RNG: Range<u32> = 4..18;
pub fn emit_acm_rd_ptr() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in ACM_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0usize; 1];
tme.borrow_mut().start();
for val in vals.iter() {
ret[0] += (*val > 0xFF) as usize + (*val > 0xFFFF) as usize + (*val > 0xFFFFFF) as usize;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Acm(1), Rd, Ptr], |x| {
#stm_inr
});
});
stm
}
pub fn emit_acm_rd_val() -> TokenStream {
let mut stm = TokenStream::new();
// sec: inner
let mut stm_inr = TokenStream::new();
for len in ACM_RNG.clone().map(|x| 2u32.pow(x)) {
let lit_len = Literal::u32_unsuffixed(len);
stm_inr.extend(quote! {
x.ins_prm(Len(#lit_len), |tme| {
let mut vals: Vec<u32> = (0u32..#lit_len).collect();
let mut rng = thread_rng();
vals.shuffle(&mut rng);
let mut ret = [0usize; 1];
tme.borrow_mut().start();
for val in vals.iter() {
let val = *val;
ret[0] += (val > 0xFF) as usize + (val > 0xFFFF) as usize + (val > 0xFFFFFF) as usize;
}
tme.borrow_mut().stop();
ret[0]
});
});
}
// sec: end
stm.extend(quote! {
stdy.reg_bld(&[Acm(1), Rd, Val], |x| {
#stm_inr
});
});
stm
}