Skip to content

Commit 209836f

Browse files
committed
Implement Uniform for SIMD float types
1 parent d80efb2 commit 209836f

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/distributions/uniform.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ use Rng;
117117
use distributions::Distribution;
118118
use distributions::float::IntoFloat;
119119

120+
#[cfg(feature="simd_support")]
121+
use core::simd::*;
122+
120123
/// Sample values uniformly between two bounds.
121124
///
122125
/// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
@@ -580,7 +583,7 @@ pub struct UniformFloat<X> {
580583
}
581584

582585
macro_rules! uniform_float_impl {
583-
($ty:ty, $bits_to_discard:expr, $next_u:ident) => {
586+
($ty:ty, $uty:ident, $bits_to_discard:expr) => {
584587
impl SampleUniform for $ty {
585588
type Sampler = UniformFloat<$ty>;
586589
}
@@ -621,8 +624,8 @@ macro_rules! uniform_float_impl {
621624

622625
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
623626
// Generate a value in the range [1, 2)
624-
let value1_2 = (rng.$next_u() >> $bits_to_discard)
625-
.into_float_with_exponent(0);
627+
let value: $uty = rng.gen::<$uty>() >> $bits_to_discard;
628+
let value1_2 = value.into_float_with_exponent(0);
626629
// We don't use `f64::mul_add`, because it is not available with
627630
// `no_std`. Furthermore, it is slower for some targets (but
628631
// faster for others). However, the order of multiplication and
@@ -643,8 +646,8 @@ macro_rules! uniform_float_impl {
643646
let scale = high - low;
644647
let offset = low - scale;
645648
// Generate a value in the range [1, 2)
646-
let value1_2 = (rng.$next_u() >> $bits_to_discard)
647-
.into_float_with_exponent(0);
649+
let value: $uty = rng.gen::<$uty>() >> $bits_to_discard;
650+
let value1_2 = value.into_float_with_exponent(0);
648651
// Doing multiply before addition allows some architectures to
649652
// use a single instruction.
650653
value1_2 * scale + offset
@@ -653,8 +656,24 @@ macro_rules! uniform_float_impl {
653656
}
654657
}
655658

656-
uniform_float_impl! { f32, 32 - 23, next_u32 }
657-
uniform_float_impl! { f64, 64 - 52, next_u64 }
659+
uniform_float_impl! { f32, u32, 32 - 23 }
660+
uniform_float_impl! { f64, u64, 64 - 52 }
661+
662+
#[cfg(feature="simd_support")]
663+
uniform_float_impl! { f32x2, u32x2, 32 - 23 }
664+
#[cfg(feature="simd_support")]
665+
uniform_float_impl! { f32x4, u32x4, 32 - 23 }
666+
#[cfg(feature="simd_support")]
667+
uniform_float_impl! { f32x8, u32x8, 32 - 23 }
668+
#[cfg(feature="simd_support")]
669+
uniform_float_impl! { f32x16, u32x16, 32 - 23 }
670+
671+
#[cfg(feature="simd_support")]
672+
uniform_float_impl! { f64x2, u64x2, 64 - 52 }
673+
#[cfg(feature="simd_support")]
674+
uniform_float_impl! { f64x4, u64x4, 64 - 52 }
675+
#[cfg(feature="simd_support")]
676+
uniform_float_impl! { f64x8, u64x8, 64 - 52 }
658677

659678

660679

0 commit comments

Comments
 (0)