|
| 1 | +// Copyright 2021 Developers of the Rand project. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +use criterion::{criterion_group, criterion_main}; |
| 10 | +use criterion::{BenchmarkGroup, BenchmarkId, Criterion}; |
| 11 | +use rand::distributions::uniform::{SampleUniform, UniformSampler}; |
| 12 | +use rand::prelude::*; |
| 13 | +use packed_simd::*; |
| 14 | + |
| 15 | +type BenchRng = SmallRng; |
| 16 | + |
| 17 | +macro_rules! bench_simd_group { |
| 18 | + ($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => { |
| 19 | + for input in $inputs { |
| 20 | + $g.bench_with_input( |
| 21 | + BenchmarkId::new($name, input.0), |
| 22 | + &input.1, |
| 23 | + |b, (low, high)| { |
| 24 | + let mut rng = BenchRng::from_entropy(); |
| 25 | + b.iter(|| <$T as SampleUniform>::Sampler::$f(low, high, &mut rng)) |
| 26 | + }, |
| 27 | + ); |
| 28 | + } |
| 29 | + $g.bench_function(BenchmarkId::new($name, "varying"), |b| { |
| 30 | + let (low, mut high) = ($inputs[0].1 .0, $inputs[1].1 .1); |
| 31 | + let mut rng = BenchRng::from_entropy(); |
| 32 | + b.iter(|| { |
| 33 | + high = high.wrapping_add(1); |
| 34 | + <$T as SampleUniform>::Sampler::$f(low, high, &mut rng) |
| 35 | + }) |
| 36 | + }); |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +macro_rules! bench_simd { |
| 41 | + ($c:expr, $T:ty, $high:expr) => {{ |
| 42 | + let mut g = $c.benchmark_group(concat!("uniform_int_", stringify!($T))); |
| 43 | + let inputs = &[("high_reject", $high), ("low_reject", (-1, 2))]; |
| 44 | + |
| 45 | + bench_simd_group!("Old", $T, sample_single_inclusive, g, inputs); |
| 46 | +// bench_simd_group!("ONeill", $T, sample_single_inclusive_oneill, g, inputs); |
| 47 | + bench_simd_group!("Canon", $T, sample_single_inclusive_canon, g, inputs); |
| 48 | +// bench_simd_group!("Canon-Lemire", $T, sample_inclusive_canon_lemire, g, inputs); |
| 49 | +// bench_simd_group!("Bitmask", $T, sample_single_inclusive_bitmask, g, inputs); |
| 50 | + }}; |
| 51 | +} |
| 52 | + |
| 53 | +fn uniform_int(c: &mut Criterion) { |
| 54 | + // for i8/i16, we use 32-bit integers internally so rejection is most common near full-size |
| 55 | + // the exact values were determined with an exhaustive search |
| 56 | + bench_simd!(c, i8x16, (i8::MIN, 116)); |
| 57 | +// bench_simd!(c, i16, (i16::MIN, 32407)); |
| 58 | +// bench_simd!(c, i32, (i32::MIN, 1)); |
| 59 | +// bench_simd!(c, i64, (i64::MIN, 1)); |
| 60 | +// bench_simd!(c, i128, (i128::MIN, 1)); |
| 61 | +} |
| 62 | + |
| 63 | +criterion_group! { |
| 64 | + name = benches; |
| 65 | + config = Criterion::default(); |
| 66 | + targets = uniform_int |
| 67 | + // targets = uniform_int_i8x16, uniform_int_i16, uniform_int_i32, uniform_int_i64, uniform_int_i128 |
| 68 | +} |
| 69 | +criterion_main!(benches); |
0 commit comments