Skip to content

Commit 1f89154

Browse files
im-0sebcrozet
andauthored
Update rand to 0.9 (#76)
* 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. * fix: avoid unnecessary unsafe code --------- Co-authored-by: Sébastien Crozet <sebcrozet@dimforge.com>
1 parent f19139d commit 1f89154

4 files changed

Lines changed: 70 additions & 21 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Unreleased
2+
- `rand` dependency updated to 0.9.
23
- Add single-lanes portable-simd trait implementations.
34

45
## Release v0.9.1 (05 Sept. 2025)
@@ -97,4 +98,4 @@ vectorization, if it can.
9798
- Add the `libm_force` cargo feature that forces the use of `libm`, even when we don't target `no-std`.
9899
- Add `copysign` to copy the sign from one number to another.
99100
- Add `simd_horizontal_min`, `simd_horizontal_max` to compute the min/max among the lanes of a single SIMD number.
100-
- Wrap all SIMD bools from `packed_simd` into our own `Simd<_>` newtype.
101+
- 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
@@ -31,7 +31,7 @@ num-complex = { version = "0.4", default-features = false }
3131
wide = { version = "0.8.1", default-features = false, optional = true }
3232
fixed = { version = "1", optional = true }
3333
cordic = { version = "0.1", optional = true }
34-
rand = { version = "0.8", optional = true }
34+
rand = { version = "0.9", optional = true }
3535
serde = { version = "1", default-features = false, optional = true }
3636
rkyv = { version = "0.7", optional = true }
3737
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: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! ignore_snd (
99

1010
macro_rules! impl_rand_auto_simd (
1111
($($t: ty, $($i: ident),*;)*) => ($(
12-
impl rand::distributions::Distribution<AutoSimd<$t>> for rand::distributions::Standard {
12+
impl rand::distr::Distribution<AutoSimd<$t>> for rand::distr::StandardUniform {
1313
#[inline(always)]
1414
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AutoSimd<$t> {
1515
AutoSimd::<$t>::new($(
@@ -49,9 +49,6 @@ impl_rand_auto_simd!(
4949
[i8; 16], _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
5050
[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;
5151
// [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;
5552
[u128; 1], _0;
5653
[u128; 2], _0, _1;
5754
[u128; 4], _0, _1, _2, _3;
@@ -73,9 +70,6 @@ impl_rand_auto_simd!(
7370
[u8; 16], _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
7471
[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;
7572
// [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;
7973
[bool; 1], _0;
8074
[bool; 2], _0, _1;
8175
[bool; 4], _0, _1, _2, _3;
@@ -85,10 +79,32 @@ impl_rand_auto_simd!(
8579
// [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;
8680
);
8781

82+
macro_rules! impl_rand_auto_simd_xsize (
83+
($($t: ty, $sampler: ident, $($i: ident),*;)*) => ($(
84+
impl rand::distr::Distribution<AutoSimd<$t>> for rand::distr::StandardUniform {
85+
#[inline(always)]
86+
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AutoSimd<$t> {
87+
AutoSimd::<$t>::new($(
88+
ignore_snd!([$sampler(rng)], [$i])
89+
),*)
90+
}
91+
}
92+
)*)
93+
);
94+
95+
impl_rand_auto_simd_xsize!(
96+
[isize; 2], sample_isize, _0, _1;
97+
[isize; 4], sample_isize, _0, _1, _2, _3;
98+
[isize; 8], sample_isize, _0, _1, _2, _3, _4, _5, _6, _7;
99+
[usize; 2], sample_usize, _0, _1;
100+
[usize; 4], sample_usize, _0, _1, _2, _3;
101+
[usize; 8], sample_usize, _0, _1, _2, _3, _4, _5, _6, _7;
102+
);
103+
88104
#[cfg(feature = "wide")]
89105
macro_rules! impl_rand_wide_simd (
90106
($($t: ident, $wrapped: ty, $arr: ty;)*) => ($(
91-
impl rand::distributions::Distribution<$t> for rand::distributions::Standard {
107+
impl rand::distr::Distribution<$t> for rand::distr::StandardUniform {
92108
#[inline(always)]
93109
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $t {
94110
$t(<$wrapped as From<$arr>>::from(self.sample(rng)))
@@ -107,7 +123,7 @@ impl_rand_wide_simd!(
107123
#[cfg(feature = "portable_simd")]
108124
macro_rules! impl_rand_portable_simd (
109125
($($wrapped: ty, $($i: ident),*;)*) => ($(
110-
impl rand::distributions::Distribution<$wrapped> for rand::distributions::Standard {
126+
impl rand::distr::Distribution<$wrapped> for rand::distr::StandardUniform {
111127
#[inline(always)]
112128
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $wrapped {
113129
<$wrapped>::new($(
@@ -145,9 +161,6 @@ impl_rand_portable_simd!(
145161
i8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
146162
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;
147163
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;
151164
u16x2, _0, _1;
152165
u16x4, _0, _1, _2, _3;
153166
u16x8, _0, _1, _2, _3, _4, _5, _6, _7;
@@ -166,9 +179,6 @@ impl_rand_portable_simd!(
166179
u8x16, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
167180
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;
168181
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;
172182
mask16x2, _0, _1;
173183
mask16x4, _0, _1, _2, _3;
174184
mask16x8, _0, _1, _2, _3, _4, _5, _6, _7;
@@ -191,3 +201,41 @@ impl_rand_portable_simd!(
191201
masksizex4, _0, _1, _2, _3;
192202
masksizex8, _0, _1, _2, _3, _4, _5, _6, _7;
193203
);
204+
205+
#[cfg(feature = "portable_simd")]
206+
macro_rules! impl_rand_portable_simd_xsize (
207+
($($wrapped: ty, $sampler: ident, $($i: ident),*;)*) => ($(
208+
impl rand::distr::Distribution<$wrapped> for rand::distr::StandardUniform {
209+
#[inline(always)]
210+
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $wrapped {
211+
<$wrapped>::new($(
212+
ignore_snd!([$sampler(rng)], [$i])
213+
),*)
214+
}
215+
}
216+
)*)
217+
);
218+
219+
#[cfg(feature = "portable_simd")]
220+
impl_rand_portable_simd_xsize!(
221+
isizex2, sample_isize, _0, _1;
222+
isizex4, sample_isize, _0, _1, _2, _3;
223+
isizex8, sample_isize, _0, _1, _2, _3, _4, _5, _6, _7;
224+
usizex2, sample_usize, _0, _1;
225+
usizex4, sample_usize, _0, _1, _2, _3;
226+
usizex8, sample_usize, _0, _1, _2, _3, _4, _5, _6, _7;
227+
);
228+
229+
#[inline(always)]
230+
fn sample_isize<R: rand::Rng + ?Sized>(rng: &mut R) -> isize {
231+
let mut bytes = [0u8; _];
232+
rng.fill_bytes(&mut bytes);
233+
isize::from_le_bytes(bytes)
234+
}
235+
236+
#[inline(always)]
237+
fn sample_usize<R: rand::Rng + ?Sized>(rng: &mut R) -> usize {
238+
let mut bytes = [0u8; _];
239+
rng.fill_bytes(&mut bytes);
240+
usize::from_le_bytes(bytes)
241+
}

0 commit comments

Comments
 (0)