• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[macro_export]
2 macro_rules! bench_func {
3     ($name: ident, $desc: expr, op => $func: ident, from => $from: expr) => {
4         pub(crate) fn $name(c: &mut Criterion) {
5             const SIZE: usize = 1 << 13;
6             let mut rng = support::PCG32::default();
7             let inputs =
8                 criterion::black_box((0..SIZE).map(|_| $from(&mut rng)).collect::<Vec<_>>());
9             // pre-fill output vector with some random value
10             let mut outputs = vec![$func($from(&mut rng)); SIZE];
11             let mut i = 0;
12             c.bench_function($desc, |b| {
13                 b.iter(|| {
14                     i = (i + 1) & (SIZE - 1);
15                     unsafe {
16                         *outputs.get_unchecked_mut(i) = $func(*inputs.get_unchecked(i));
17                     }
18                 })
19             });
20             criterion::black_box(outputs);
21         }
22     };
23 }
24 
25 #[macro_export]
26 macro_rules! bench_unop {
27     ($name: ident, $desc: expr, op => $unop: ident, from => $from: expr) => {
28         pub(crate) fn $name(c: &mut Criterion) {
29             const SIZE: usize = 1 << 13;
30             let mut rng = support::PCG32::default();
31             let inputs =
32                 criterion::black_box((0..SIZE).map(|_| $from(&mut rng)).collect::<Vec<_>>());
33             // pre-fill output vector with some random value
34             let mut outputs = vec![$from(&mut rng).$unop(); SIZE];
35             let mut i = 0;
36             c.bench_function($desc, |b| {
37                 b.iter(|| {
38                     i = (i + 1) & (SIZE - 1);
39                     unsafe {
40                         *outputs.get_unchecked_mut(i) = inputs.get_unchecked(i).$unop();
41                     }
42                 })
43             });
44             criterion::black_box(outputs);
45         }
46     };
47 }
48 
49 #[macro_export]
50 macro_rules! bench_binop {
51     ($name: ident, $desc: expr, op => $binop: ident, from1 => $from1:expr, from2 => $from2:expr) => {
52         pub(crate) fn $name(c: &mut Criterion) {
53             const SIZE: usize = 1 << 13;
54             let mut rng = support::PCG32::default();
55             let inputs1 =
56                 criterion::black_box((0..SIZE).map(|_| $from1(&mut rng)).collect::<Vec<_>>());
57             let inputs2 =
58                 criterion::black_box((0..SIZE).map(|_| $from2(&mut rng)).collect::<Vec<_>>());
59             // pre-fill output vector with some random value
60             let mut outputs = vec![$from1(&mut rng).$binop($from2(&mut rng)); SIZE];
61             let mut i = 0;
62             c.bench_function($desc, |b| {
63                 b.iter(|| {
64                     i = (i + 1) & (SIZE - 1);
65                     unsafe {
66                         *outputs.get_unchecked_mut(i) = inputs1.get_unchecked(i).$binop(*inputs2.get_unchecked(i));
67                     }
68                 })
69             });
70             criterion::black_box(outputs);
71         }
72     };
73     ($name: ident, $desc: expr, op => $binop: ident, from => $from: expr) => {
74         bench_binop!($name, $desc, op => $binop, from1 => $from, from2 => $from);
75     };
76 }
77 
78 #[macro_export]
79 macro_rules! bench_trinop {
80     ($name: ident, $desc: expr, op => $trinop: ident, from1 => $from1:expr, from2 => $from2:expr, from3 => $from3:expr) => {
81         pub(crate) fn $name(c: &mut Criterion) {
82             const SIZE: usize = 1 << 13;
83             let mut rng = support::PCG32::default();
84             let inputs1 =
85                 criterion::black_box((0..SIZE).map(|_| $from1(&mut rng)).collect::<Vec<_>>());
86             let inputs2 =
87                 criterion::black_box((0..SIZE).map(|_| $from2(&mut rng)).collect::<Vec<_>>());
88             let inputs3 =
89                 criterion::black_box((0..SIZE).map(|_| $from3(&mut rng)).collect::<Vec<_>>());
90             // pre-fill output vector with some random value
91             let mut outputs =
92                 vec![$from1(&mut rng).$trinop($from2(&mut rng), $from3(&mut rng)); SIZE];
93             let mut i = 0;
94             c.bench_function($desc, |b| {
95                 b.iter(|| {
96                     i = (i + 1) & (SIZE - 1);
97                     unsafe {
98                         *outputs.get_unchecked_mut(i) = inputs1
99                             .get_unchecked(i)
100                             .$trinop(*inputs2.get_unchecked(i), *inputs3.get_unchecked(i));
101                     }
102                 })
103             });
104             criterion::black_box(outputs);
105         }
106     };
107 }
108 
109 #[macro_export]
110 macro_rules! bench_select {
111     ($name:ident, $desc:expr, ty => $ty: ident, op => $op: ident, from => $from:expr) => {
112         pub(crate) fn $name(c: &mut Criterion) {
113             const SIZE: usize = 1 << 13;
114             let mut rng = support::PCG32::default();
115             let inputs1 =
116                 criterion::black_box((0..SIZE).map(|_| $from(&mut rng)).collect::<Vec<_>>());
117             let inputs2 =
118                 criterion::black_box((0..SIZE).map(|_| $from(&mut rng)).collect::<Vec<_>>());
119             let masks = vec![$from(&mut rng).$op($from(&mut rng)); SIZE];
120             // pre-fill output vector with some random value
121             let mut outputs = vec![$from(&mut rng); SIZE];
122             let mut i = 0;
123             c.bench_function($desc, |b| {
124                 b.iter(|| {
125                     i = (i + 1) & (SIZE - 1);
126                     unsafe {
127                         *outputs.get_unchecked_mut(i) = $ty::select(
128                             *masks.get_unchecked(i),
129                             *inputs1.get_unchecked(i),
130                             *inputs2.get_unchecked(i),
131                         );
132                     }
133                 })
134             });
135             criterion::black_box(outputs);
136         }
137     };
138 }
139 #[macro_export]
140 macro_rules! bench_from_ypr {
141     ($name: ident, $desc: expr, ty => $ty:ty) => {
142         pub(crate) fn $name(c: &mut Criterion) {
143             const SIZE: usize = 1 << 13;
144             let mut rng = support::PCG32::default();
145             let inputs = criterion::black_box(
146                 (0..SIZE)
147                     .map(|_| {
148                         (
149                             random_radians(&mut rng),
150                             random_radians(&mut rng),
151                             random_radians(&mut rng),
152                         )
153                     })
154                     .collect::<Vec<_>>(),
155             );
156             let mut outputs = vec![<$ty>::default(); SIZE];
157             let mut i = 0;
158             c.bench_function($desc, |b| {
159                 b.iter(|| {
160                     i = (i + 1) & (SIZE - 1);
161                     unsafe {
162                         let data = inputs.get_unchecked(i);
163                         *outputs.get_unchecked_mut(i) =
164                             <$ty>::from_euler(glam::EulerRot::YXZ, data.0, data.1, data.2)
165                     }
166                 })
167             });
168         }
169     };
170 }
171 
172 #[macro_export]
173 macro_rules! euler {
174     ($name: ident, $desc: expr, ty => $t: ty, storage => $storage: ty, zero => $zero: expr, rand => $rand: ident) => {
175         pub(crate) fn $name(c: &mut Criterion) {
176             const UPDATE_RATE: f32 = 1.0 / 60.0;
177             const NUM_OBJECTS: usize = 10000;
178 
179             struct TestData {
180                 acc: Vec<$storage>,
181                 vel: Vec<$storage>,
182                 pos: Vec<$storage>,
183             }
184 
185             let mut rng = support::PCG32::default();
186             let mut data = TestData {
187                 acc: vec![$rand(&mut rng); NUM_OBJECTS],
188                 vel: vec![$zero; NUM_OBJECTS],
189                 pos: vec![$zero; NUM_OBJECTS],
190             };
191             let dt = <$t>::splat(UPDATE_RATE);
192 
193             c.bench_function($desc, |b| {
194                 b.iter(|| {
195                     for ((position, acceleration), velocity) in
196                         data.pos.iter_mut().zip(&data.acc).zip(&mut data.vel)
197                     {
198                         let local_acc: $t = (*acceleration).into();
199                         let mut local_pos: $t = (*position).into();
200                         let mut local_vel: $t = (*velocity).into();
201                         local_vel += local_acc * dt;
202                         local_pos += local_vel * dt;
203                         *velocity = local_vel.into();
204                         *position = local_pos.into();
205                     }
206                 })
207             });
208         }
209     };
210 }
211