@@ -117,6 +117,9 @@ use Rng;
117
117
use distributions:: Distribution ;
118
118
use distributions:: float:: IntoFloat ;
119
119
120
+ #[ cfg( feature="simd_support" ) ]
121
+ use core:: simd:: * ;
122
+
120
123
/// Sample values uniformly between two bounds.
121
124
///
122
125
/// [`Uniform::new`] and [`Uniform::new_inclusive`] construct a uniform
@@ -580,7 +583,7 @@ pub struct UniformFloat<X> {
580
583
}
581
584
582
585
macro_rules! uniform_float_impl {
583
- ( $ty: ty, $bits_to_discard : expr , $next_u : ident ) => {
586
+ ( $ty: ty, $uty : ident , $bits_to_discard : expr ) => {
584
587
impl SampleUniform for $ty {
585
588
type Sampler = UniformFloat <$ty>;
586
589
}
@@ -621,8 +624,8 @@ macro_rules! uniform_float_impl {
621
624
622
625
fn sample<R : Rng + ?Sized >( & self , rng: & mut R ) -> Self :: X {
623
626
// 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 ) ;
626
629
// We don't use `f64::mul_add`, because it is not available with
627
630
// `no_std`. Furthermore, it is slower for some targets (but
628
631
// faster for others). However, the order of multiplication and
@@ -643,8 +646,8 @@ macro_rules! uniform_float_impl {
643
646
let scale = high - low;
644
647
let offset = low - scale;
645
648
// 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 ) ;
648
651
// Doing multiply before addition allows some architectures to
649
652
// use a single instruction.
650
653
value1_2 * scale + offset
@@ -653,8 +656,24 @@ macro_rules! uniform_float_impl {
653
656
}
654
657
}
655
658
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 }
658
677
659
678
660
679
0 commit comments