Debian Codegraph @

.cargo/vendor/half/benches/convert.rs

raw
1 use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
2 use half::prelude::*;
3 use std::{f32, f64, iter};
4
5 const SIMD_LARGE_BENCH_SLICE_LEN: usize = 1024;
6
7 fn bench_f32_to_f16(c: &mut Criterion) {
8 let mut group = c.benchmark_group("Convert f16 From f32");
9 for val in &[
10 0.,
11 -0.,
12 1.,
13 f32::MIN,
14 f32::MAX,
15 f32::MIN_POSITIVE,
16 f32::NEG_INFINITY,
17 f32::INFINITY,
18 f32::NAN,
19 f32::consts::E,
20 f32::consts::PI,
21 ] {
22 group.bench_with_input(BenchmarkId::new("f16::from_f32", val), val, |b, i| {
23 b.iter(|| f16::from_f32(*i))
24 });
25 }
26 }
27
28 fn bench_f64_to_f16(c: &mut Criterion) {
29 let mut group = c.benchmark_group("Convert f16 From f64");
30 for val in &[
31 0.,
32 -0.,
33 1.,
34 f64::MIN,
35 f64::MAX,
36 f64::MIN_POSITIVE,
37 f64::NEG_INFINITY,
38 f64::INFINITY,
39 f64::NAN,
40 f64::consts::E,
41 f64::consts::PI,
42 ] {
43 group.bench_with_input(BenchmarkId::new("f16::from_f64", val), val, |b, i| {
44 b.iter(|| f16::from_f64(*i))
45 });
46 }
47 }
48
49 fn bench_f16_to_f32(c: &mut Criterion) {
50 let mut group = c.benchmark_group("Convert f16 to f32");
51 for val in &[
52 f16::ZERO,
53 f16::NEG_ZERO,
54 f16::ONE,
55 f16::MIN,
56 f16::MAX,
57 f16::MIN_POSITIVE,
58 f16::NEG_INFINITY,
59 f16::INFINITY,
60 f16::NAN,
61 f16::E,
62 f16::PI,
63 ] {
64 group.bench_with_input(BenchmarkId::new("f16::to_f32", val), val, |b, i| {
65 b.iter(|| i.to_f32())
66 });
67 }
68 }
69
70 fn bench_f16_to_f64(c: &mut Criterion) {
71 let mut group = c.benchmark_group("Convert f16 to f64");
72 for val in &[
73 f16::ZERO,
74 f16::NEG_ZERO,
75 f16::ONE,
76 f16::MIN,
77 f16::MAX,
78 f16::MIN_POSITIVE,
79 f16::NEG_INFINITY,
80 f16::INFINITY,
81 f16::NAN,
82 f16::E,
83 f16::PI,
84 ] {
85 group.bench_with_input(BenchmarkId::new("f16::to_f64", val), val, |b, i| {
86 b.iter(|| i.to_f64())
87 });
88 }
89 }
90
91 criterion_group!(
92 f16_sisd,
93 bench_f32_to_f16,
94 bench_f64_to_f16,
95 bench_f16_to_f32,
96 bench_f16_to_f64
97 );
98
99 fn bench_slice_f32_to_f16(c: &mut Criterion) {
100 let mut constant_buffer = [f16::ZERO; 11];
101 let constants = [
102 0.,
103 -0.,
104 1.,
105 f32::MIN,
106 f32::MAX,
107 f32::MIN_POSITIVE,
108 f32::NEG_INFINITY,
109 f32::INFINITY,
110 f32::NAN,
111 f32::consts::E,
112 f32::consts::PI,
113 ];
114 c.bench_function(
115 "HalfFloatSliceExt::convert_from_f32_slice/constants",
116 |b: &mut Bencher<'_>| {
117 b.iter(|| black_box(&mut constant_buffer).convert_from_f32_slice(black_box(&constants)))
118 },
119 );
120
121 let large: Vec<_> = iter::repeat(0)
122 .enumerate()
123 .map(|(i, _)| i as f32)
124 .take(SIMD_LARGE_BENCH_SLICE_LEN)
125 .collect();
126 let mut large_buffer = [f16::ZERO; SIMD_LARGE_BENCH_SLICE_LEN];
127 c.bench_function(
128 "HalfFloatSliceExt::convert_from_f32_slice/large",
129 |b: &mut Bencher<'_>| {
130 b.iter(|| black_box(&mut large_buffer).convert_from_f32_slice(black_box(&large)))
131 },
132 );
133 }
134
135 fn bench_slice_f64_to_f16(c: &mut Criterion) {
136 let mut constant_buffer = [f16::ZERO; 11];
137 let constants = [
138 0.,
139 -0.,
140 1.,
141 f64::MIN,
142 f64::MAX,
143 f64::MIN_POSITIVE,
144 f64::NEG_INFINITY,
145 f64::INFINITY,
146 f64::NAN,
147 f64::consts::E,
148 f64::consts::PI,
149 ];
150 c.bench_function(
151 "HalfFloatSliceExt::convert_from_f64_slice/constants",
152 |b: &mut Bencher<'_>| {
153 b.iter(|| black_box(&mut constant_buffer).convert_from_f64_slice(black_box(&constants)))
154 },
155 );
156
157 let large: Vec<_> = iter::repeat(0)
158 .enumerate()
159 .map(|(i, _)| i as f64)
160 .take(SIMD_LARGE_BENCH_SLICE_LEN)
161 .collect();
162 let mut large_buffer = [f16::ZERO; SIMD_LARGE_BENCH_SLICE_LEN];
163 c.bench_function(
164 "HalfFloatSliceExt::convert_from_f64_slice/large",
165 |b: &mut Bencher<'_>| {
166 b.iter(|| black_box(&mut large_buffer).convert_from_f64_slice(black_box(&large)))
167 },
168 );
169 }
170
171 fn bench_slice_f16_to_f32(c: &mut Criterion) {
172 let mut constant_buffer = [0f32; 11];
173 let constants = [
174 f16::ZERO,
175 f16::NEG_ZERO,
176 f16::ONE,
177 f16::MIN,
178 f16::MAX,
179 f16::MIN_POSITIVE,
180 f16::NEG_INFINITY,
181 f16::INFINITY,
182 f16::NAN,
183 f16::E,
184 f16::PI,
185 ];
186 c.bench_function(
187 "HalfFloatSliceExt::convert_to_f32_slice/constants",
188 |b: &mut Bencher<'_>| {
189 b.iter(|| black_box(&constants).convert_to_f32_slice(black_box(&mut constant_buffer)))
190 },
191 );
192
193 let large: Vec<_> = iter::repeat(0)
194 .enumerate()
195 .map(|(i, _)| f16::from_f32(i as f32))
196 .take(SIMD_LARGE_BENCH_SLICE_LEN)
197 .collect();
198 let mut large_buffer = [0f32; SIMD_LARGE_BENCH_SLICE_LEN];
199 c.bench_function(
200 "HalfFloatSliceExt::convert_to_f32_slice/large",
201 |b: &mut Bencher<'_>| {
202 b.iter(|| black_box(&large).convert_to_f32_slice(black_box(&mut large_buffer)))
203 },
204 );
205 }
206
207 fn bench_slice_f16_to_f64(c: &mut Criterion) {
208 let mut constant_buffer = [0f64; 11];
209 let constants = [
210 f16::ZERO,
211 f16::NEG_ZERO,
212 f16::ONE,
213 f16::MIN,
214 f16::MAX,
215 f16::MIN_POSITIVE,
216 f16::NEG_INFINITY,
217 f16::INFINITY,
218 f16::NAN,
219 f16::E,
220 f16::PI,
221 ];
222 c.bench_function(
223 "HalfFloatSliceExt::convert_to_f64_slice/constants",
224 |b: &mut Bencher<'_>| {
225 b.iter(|| black_box(&constants).convert_to_f64_slice(black_box(&mut constant_buffer)))
226 },
227 );
228
229 let large: Vec<_> = iter::repeat(0)
230 .enumerate()
231 .map(|(i, _)| f16::from_f64(i as f64))
232 .take(SIMD_LARGE_BENCH_SLICE_LEN)
233 .collect();
234 let mut large_buffer = [0f64; SIMD_LARGE_BENCH_SLICE_LEN];
235 c.bench_function(
236 "HalfFloatSliceExt::convert_to_f64_slice/large",
237 |b: &mut Bencher<'_>| {
238 b.iter(|| black_box(&large).convert_to_f64_slice(black_box(&mut large_buffer)))
239 },
240 );
241 }
242
243 criterion_group!(
244 f16_simd,
245 bench_slice_f32_to_f16,
246 bench_slice_f64_to_f16,
247 bench_slice_f16_to_f32,
248 bench_slice_f16_to_f64
249 );
250
251 fn bench_f32_to_bf16(c: &mut Criterion) {
252 let mut group = c.benchmark_group("Convert bf16 From f32");
253 for val in &[
254 0.,
255 -0.,
256 1.,
257 f32::MIN,
258 f32::MAX,
259 f32::MIN_POSITIVE,
260 f32::NEG_INFINITY,
261 f32::INFINITY,
262 f32::NAN,
263 f32::consts::E,
264 f32::consts::PI,
265 ] {
266 group.bench_with_input(BenchmarkId::new("bf16::from_f32", val), val, |b, i| {
267 b.iter(|| bf16::from_f32(*i))
268 });
269 }
270 }
271
272 fn bench_f64_to_bf16(c: &mut Criterion) {
273 let mut group = c.benchmark_group("Convert bf16 From f64");
274 for val in &[
275 0.,
276 -0.,
277 1.,
278 f64::MIN,
279 f64::MAX,
280 f64::MIN_POSITIVE,
281 f64::NEG_INFINITY,
282 f64::INFINITY,
283 f64::NAN,
284 f64::consts::E,
285 f64::consts::PI,
286 ] {
287 group.bench_with_input(BenchmarkId::new("bf16::from_f64", val), val, |b, i| {
288 b.iter(|| bf16::from_f64(*i))
289 });
290 }
291 }
292
293 fn bench_bf16_to_f32(c: &mut Criterion) {
294 let mut group = c.benchmark_group("Convert bf16 to f32");
295 for val in &[
296 bf16::ZERO,
297 bf16::NEG_ZERO,
298 bf16::ONE,
299 bf16::MIN,
300 bf16::MAX,
301 bf16::MIN_POSITIVE,
302 bf16::NEG_INFINITY,
303 bf16::INFINITY,
304 bf16::NAN,
305 bf16::E,
306 bf16::PI,
307 ] {
308 group.bench_with_input(BenchmarkId::new("bf16::to_f32", val), val, |b, i| {
309 b.iter(|| i.to_f32())
310 });
311 }
312 }
313
314 fn bench_bf16_to_f64(c: &mut Criterion) {
315 let mut group = c.benchmark_group("Convert bf16 to f64");
316 for val in &[
317 bf16::ZERO,
318 bf16::NEG_ZERO,
319 bf16::ONE,
320 bf16::MIN,
321 bf16::MAX,
322 bf16::MIN_POSITIVE,
323 bf16::NEG_INFINITY,
324 bf16::INFINITY,
325 bf16::NAN,
326 bf16::E,
327 bf16::PI,
328 ] {
329 group.bench_with_input(BenchmarkId::new("bf16::to_f64", val), val, |b, i| {
330 b.iter(|| i.to_f64())
331 });
332 }
333 }
334
335 criterion_group!(
336 bf16_sisd,
337 bench_f32_to_bf16,
338 bench_f64_to_bf16,
339 bench_bf16_to_f32,
340 bench_bf16_to_f64
341 );
342
343 criterion_main!(f16_sisd, bf16_sisd, f16_simd);