Skip to content

Commit 5ec6390

Browse files
committed
benches/uniform: add SIMD benches
1 parent 039e740 commit 5ec6390

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

benches/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ version = "0.1.0"
44
edition = "2021"
55
publish = false
66

7+
[features]
8+
# Option (requires nightly Rust): experimental SIMD support
9+
simd_support = ["rand/simd_support"]
10+
11+
712
[dependencies]
813

914
[dev-dependencies]

benches/benches/uniform.rs

+47-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
//! Implement benchmarks for uniform distributions over integer types
1010
11+
#![cfg_attr(feature = "simd_support", feature(portable_simd))]
12+
1113
use core::time::Duration;
1214
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
1315
use rand::distr::uniform::{SampleRange, Uniform};
1416
use rand::prelude::*;
1517
use rand_chacha::ChaCha8Rng;
1618
use rand_pcg::{Pcg32, Pcg64};
19+
#[cfg(feature = "simd_support")]
20+
use std::simd::{num::SimdUint, Simd};
1721

1822
const WARM_UP_TIME: Duration = Duration::from_millis(1000);
1923
const MEASUREMENT_TIME: Duration = Duration::from_secs(3);
@@ -29,22 +33,46 @@ macro_rules! sample {
2933
((x >> bits) * (x & mask)) as $T
3034
}};
3135

36+
(@range $T:ty, $U:ty, $len:tt, $rng:ident) => {{
37+
let bits = (<$T>::BITS / 2);
38+
let mask = Simd::splat((1 as $U).wrapping_neg() >> bits);
39+
let bits = Simd::splat(bits as $U);
40+
let x = $rng.random::<Simd<$U, $len>>();
41+
((x >> bits) * (x & mask)).cast()
42+
}};
43+
44+
(@MIN $T:ty, 1) => {
45+
<$T>::MIN
46+
};
47+
48+
(@MIN $T:ty, $len:tt) => {
49+
Simd::<$T, $len>::splat(<$T>::MIN)
50+
};
51+
52+
(@wrapping_add $lhs:expr, $rhs:expr, 1) => {
53+
$lhs.wrapping_add($rhs)
54+
};
55+
56+
(@wrapping_add $lhs:expr, $rhs:expr, $len:tt) => {
57+
($lhs + $rhs)
58+
};
59+
3260
($R:ty, $T:ty, $U:ty, $len:tt, $g:expr) => {
3361
$g.bench_function(BenchmarkId::new(stringify!($R), "single"), |b| {
3462
let mut rng = <$R>::from_rng(&mut rand::rng());
3563
let range = sample!(@range $T, $U, $len, rng);
36-
let low = <$T>::MIN;
37-
let high = low.wrapping_add(range);
64+
let low = sample!(@MIN $T, $len);
65+
let high = sample!(@wrapping_add low, range, $len);
3866

3967
b.iter(|| (low..=high).sample_single(&mut rng));
4068
});
4169

4270
$g.bench_function(BenchmarkId::new(stringify!($R), "distr"), |b| {
4371
let mut rng = <$R>::from_rng(&mut rand::rng());
4472
let range = sample!(@range $T, $U, $len, rng);
45-
let low = <$T>::MIN;
46-
let high = low.wrapping_add(range);
47-
let dist = Uniform::<$T>::new_inclusive(<$T>::MIN, high).unwrap();
73+
let low = sample!(@MIN $T, $len);
74+
let high = sample!(@wrapping_add low, range, $len);
75+
let dist = Uniform::new_inclusive(low, high).unwrap();
4876

4977
b.iter(|| dist.sample(&mut rng));
5078
});
@@ -74,6 +102,20 @@ fn sample(c: &mut Criterion) {
74102
sample!(c, i32, u32, 1);
75103
sample!(c, i64, u64, 1);
76104
sample!(c, i128, u128, 1);
105+
#[cfg(feature = "simd_support")]
106+
sample!(c, u8, u8, 8);
107+
#[cfg(feature = "simd_support")]
108+
sample!(c, u8, u8, 16);
109+
#[cfg(feature = "simd_support")]
110+
sample!(c, u8, u8, 32);
111+
#[cfg(feature = "simd_support")]
112+
sample!(c, u8, u8, 64);
113+
#[cfg(feature = "simd_support")]
114+
sample!(c, i16, u16, 8);
115+
#[cfg(feature = "simd_support")]
116+
sample!(c, i16, u16, 16);
117+
#[cfg(feature = "simd_support")]
118+
sample!(c, i16, u16, 32);
77119
}
78120

79121
criterion_group! {

0 commit comments

Comments
 (0)