Skip to content

Commit ee25c2e

Browse files
committed
feat: update rand to 0.9
Implementations of `Distribution<T> for StandardUniform` for all SIMD types containing `isize` and `usize` now generate random values using `fill_bytes()`. Uniform distribution for `isize` and `usize` was removed from `rand` crate. See rust-random/rand#1487 for details. There is a new `UniformUsize` distribution, but it is limited to a maximum value under u32::MAX for portability across 32/64bits and for use as array indexes and lengths. Probably it is not suitable if a real uniform distribution is required.
1 parent e7fe5c2 commit ee25c2e

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Unreleased
2+
- `rand` dependency updated to 0.9.
3+
14
## Release v0.9.1 (05 Sept. 2025)
25
- `into_arr`, `from_arr`, `map`, `zip_map` are now public on `WideF32x4`, `WideF32x8` and `WideF64x4`.
36
- `from_arr` and `into_arr` are now public on `WideBoolF32x4`, `WideBoolF32x8`, `WideBoolF64x4`.
@@ -94,4 +97,4 @@ vectorization, if it can.
9497
- Add the `libm_force` cargo feature that forces the use of `libm`, even when we don't target `no-std`.
9598
- Add `copysign` to copy the sign from one number to another.
9699
- Add `simd_horizontal_min`, `simd_horizontal_max` to compute the min/max among the lanes of a single SIMD number.
97-
- Wrap all SIMD bools from `packed_simd` into our own `Simd<_>` newtype.
100+
- Wrap all SIMD bools from `packed_simd` into our own `Simd<_>` newtype.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ wide = { version = "0.7", default-features = false, optional = true }
3232
fixed = { version = "1", optional = true }
3333
cordic = { version = "0.1", optional = true }
3434
paste = "1.0"
35-
rand = { version = "0.8", optional = true }
35+
rand = { version = "0.9", optional = true }
3636
serde = { version = "1", default-features = false, optional = true }
3737
rkyv = { version = "0.7", optional = true }
3838
libm_force = { package = "libm", version = "0.2", optional = true }

src/scalar/fixed_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ macro_rules! impl_fixed_type (
7676
}
7777

7878
#[cfg(feature = "rand")]
79-
impl<Fract: $LeEqDim> rand::distributions::Distribution<$FixedI<Fract>> for rand::distributions::Standard {
79+
impl<Fract: $LeEqDim> rand::distr::Distribution<$FixedI<Fract>> for rand::distr::StandardUniform {
8080
#[inline]
8181
fn sample<'a, G: rand::Rng + ?Sized>(&self, rng: &mut G) -> $FixedI<Fract> {
82-
let bits = rng.gen();
82+
let bits = rng.random();
8383
$FixedI(fixed::$FixedI::from_bits(bits))
8484
}
8585
}
8686

8787
#[cfg(feature = "rand")]
88-
impl<Fract: $LeEqDim> rand::distributions::Distribution<$FixedI<Fract>> for rand::distributions::OpenClosed01 {
88+
impl<Fract: $LeEqDim> rand::distr::Distribution<$FixedI<Fract>> for rand::distr::OpenClosed01 {
8989
#[inline]
9090
fn sample<'a, G: rand::Rng + ?Sized>(&self, rng: &mut G) -> $FixedI<Fract> {
91-
let val: f64 = rng.gen();
91+
let val: f64 = rng.random();
9292
$FixedI(fixed::$FixedI::from_num(val))
9393
}
9494
}

src/simd/rand_impl.rs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use core::mem::{size_of, MaybeUninit};
2+
use core::slice;
3+
14
use crate::simd::*;
25

36
// Given two token streams in the format `ignore_snd!([first_token_tree], [second])` will simply
@@ -9,7 +12,7 @@ macro_rules! ignore_snd (
912

1013
macro_rules! impl_rand_auto_simd (
1114
($($t: ty, $($i: ident),*;)*) => ($(
12-
impl rand::distributions::Distribution<AutoSimd<$t>> for rand::distributions::Standard {
15+
impl rand::distr::Distribution<AutoSimd<$t>> for rand::distr::StandardUniform {
1316
#[inline(always)]
1417
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AutoSimd<$t> {
1518
AutoSimd::<$t>::new($(
@@ -49,9 +52,6 @@ impl_rand_auto_simd!(
4952
[i8; 16], _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
5053
[i8; 32], _0, _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;
5154
// [i8; 64], _0, _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;
52-
[isize; 2], _0, _1;
53-
[isize; 4], _0, _1, _2, _3;
54-
[isize; 8], _0, _1, _2, _3, _4, _5, _6, _7;
5555
[u128; 1], _0;
5656
[u128; 2], _0, _1;
5757
[u128; 4], _0, _1, _2, _3;
@@ -73,9 +73,6 @@ impl_rand_auto_simd!(
7373
[u8; 16], _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
7474
[u8; 32], _0, _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;
7575
// [u8; 64], _0, _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;
76-
[usize; 2], _0, _1;
77-
[usize; 4], _0, _1, _2, _3;
78-
[usize; 8], _0, _1, _2, _3, _4, _5, _6, _7;
7976
[bool; 1], _0;
8077
[bool; 2], _0, _1;
8178
[bool; 4], _0, _1, _2, _3;
@@ -85,10 +82,32 @@ impl_rand_auto_simd!(
8582
// [bool; 64], _0, _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;
8683
);
8784

85+
macro_rules! impl_rand_auto_simd_xsize (
86+
($($t: ty, $sampler: ident, $($i: ident),*;)*) => ($(
87+
impl rand::distr::Distribution<AutoSimd<$t>> for rand::distr::StandardUniform {
88+
#[inline(always)]
89+
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AutoSimd<$t> {
90+
AutoSimd::<$t>::new($(
91+
ignore_snd!([$sampler(rng)], [$i])
92+
),*)
93+
}
94+
}
95+
)*)
96+
);
97+
98+
impl_rand_auto_simd_xsize!(
99+
[isize; 2], sample_isize, _0, _1;
100+
[isize; 4], sample_isize, _0, _1, _2, _3;
101+
[isize; 8], sample_isize, _0, _1, _2, _3, _4, _5, _6, _7;
102+
[usize; 2], sample_usize, _0, _1;
103+
[usize; 4], sample_usize, _0, _1, _2, _3;
104+
[usize; 8], sample_usize, _0, _1, _2, _3, _4, _5, _6, _7;
105+
);
106+
88107
#[cfg(feature = "wide")]
89108
macro_rules! impl_rand_wide_simd (
90109
($($t: ident, $wrapped: ty, $arr: ty;)*) => ($(
91-
impl rand::distributions::Distribution<$t> for rand::distributions::Standard {
110+
impl rand::distr::Distribution<$t> for rand::distr::StandardUniform {
92111
#[inline(always)]
93112
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $t {
94113
$t(<$wrapped as From<$arr>>::from(self.sample(rng)))
@@ -107,7 +126,7 @@ impl_rand_wide_simd!(
107126
#[cfg(feature = "portable_simd")]
108127
macro_rules! impl_rand_portable_simd (
109128
($($wrapped: ty, $($i: ident),*;)*) => ($(
110-
impl rand::distributions::Distribution<$wrapped> for rand::distributions::Standard {
129+
impl rand::distr::Distribution<$wrapped> for rand::distr::StandardUniform {
111130
#[inline(always)]
112131
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $wrapped {
113132
<$wrapped>::new($(
@@ -145,9 +164,6 @@ impl_rand_portable_simd!(
145164
i8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
146165
i8x32, _0, _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;
147166
i8x64, _0, _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;
148-
isizex2, _0, _1;
149-
isizex4, _0, _1, _2, _3;
150-
isizex8, _0, _1, _2, _3, _4, _5, _6, _7;
151167
u16x2, _0, _1;
152168
u16x4, _0, _1, _2, _3;
153169
u16x8, _0, _1, _2, _3, _4, _5, _6, _7;
@@ -166,9 +182,6 @@ impl_rand_portable_simd!(
166182
u8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
167183
u8x32, _0, _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;
168184
u8x64, _0, _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;
169-
usizex2, _0, _1;
170-
usizex4, _0, _1, _2, _3;
171-
usizex8, _0, _1, _2, _3, _4, _5, _6, _7;
172185
mask16x2, _0, _1;
173186
mask16x4, _0, _1, _2, _3;
174187
mask16x8, _0, _1, _2, _3, _4, _5, _6, _7;
@@ -191,3 +204,51 @@ impl_rand_portable_simd!(
191204
masksizex4, _0, _1, _2, _3;
192205
masksizex8, _0, _1, _2, _3, _4, _5, _6, _7;
193206
);
207+
208+
#[cfg(feature = "portable_simd")]
209+
macro_rules! impl_rand_portable_simd_xsize (
210+
($($wrapped: ty, $sampler: ident, $($i: ident),*;)*) => ($(
211+
impl rand::distr::Distribution<$wrapped> for rand::distr::StandardUniform {
212+
#[inline(always)]
213+
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $wrapped {
214+
<$wrapped>::new($(
215+
ignore_snd!([$sampler(rng)], [$i])
216+
),*)
217+
}
218+
}
219+
)*)
220+
);
221+
222+
#[cfg(feature = "portable_simd")]
223+
impl_rand_portable_simd_xsize!(
224+
isizex2, sample_isize, _0, _1;
225+
isizex4, sample_isize, _0, _1, _2, _3;
226+
isizex8, sample_isize, _0, _1, _2, _3, _4, _5, _6, _7;
227+
usizex2, sample_usize, _0, _1;
228+
usizex4, sample_usize, _0, _1, _2, _3;
229+
usizex8, sample_usize, _0, _1, _2, _3, _4, _5, _6, _7;
230+
);
231+
232+
#[inline(always)]
233+
fn sample_isize<R: rand::Rng + ?Sized>(rng: &mut R) -> isize {
234+
let mut result: MaybeUninit<isize> = MaybeUninit::uninit();
235+
unsafe {
236+
rng.fill_bytes(slice::from_raw_parts_mut(
237+
result.as_mut_ptr().cast(),
238+
size_of::<isize>(),
239+
));
240+
result.assume_init()
241+
}
242+
}
243+
244+
#[inline(always)]
245+
fn sample_usize<R: rand::Rng + ?Sized>(rng: &mut R) -> usize {
246+
let mut result: MaybeUninit<usize> = MaybeUninit::uninit();
247+
unsafe {
248+
rng.fill_bytes(slice::from_raw_parts_mut(
249+
result.as_mut_ptr().cast(),
250+
size_of::<usize>(),
251+
));
252+
result.assume_init()
253+
}
254+
}

0 commit comments

Comments
 (0)