Skip to content

Commit

Permalink
Remove all support for directly generating usize, isize values
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jul 26, 2024
1 parent 3ad1d6e commit 63e823c
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 63 deletions.
2 changes: 0 additions & 2 deletions rand_distr/src/weighted_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,11 @@ macro_rules! impl_weight_for_int {

impl_weight_for_float!(f64);
impl_weight_for_float!(f32);
impl_weight_for_int!(usize);
impl_weight_for_int!(u128);
impl_weight_for_int!(u64);
impl_weight_for_int!(u32);
impl_weight_for_int!(u16);
impl_weight_for_int!(u8);
impl_weight_for_int!(isize);
impl_weight_for_int!(i128);
impl_weight_for_int!(i64);
impl_weight_for_int!(i32);
Expand Down
36 changes: 3 additions & 33 deletions src/distr/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use core::arch::x86_64::__m512i;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::{__m128i, __m256i};
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU8,
};
#[cfg(feature = "simd_support")]
use core::simd::*;
Expand Down Expand Up @@ -63,20 +63,6 @@ impl Distribution<u128> for Standard {
}
}

impl Distribution<usize> for Standard {
#[inline]
#[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
rng.next_u32() as usize
}

#[inline]
#[cfg(target_pointer_width = "64")]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
rng.next_u64() as usize
}
}

macro_rules! impl_int_from_uint {
($ty:ty, $uty:ty) => {
impl Distribution<$ty> for Standard {
Expand All @@ -93,7 +79,6 @@ impl_int_from_uint! { i16, u16 }
impl_int_from_uint! { i32, u32 }
impl_int_from_uint! { i64, u64 }
impl_int_from_uint! { i128, u128 }
impl_int_from_uint! { isize, usize }

macro_rules! impl_nzint {
($ty:ty, $new:path) => {
Expand All @@ -114,14 +99,12 @@ impl_nzint!(NonZeroU16, NonZeroU16::new);
impl_nzint!(NonZeroU32, NonZeroU32::new);
impl_nzint!(NonZeroU64, NonZeroU64::new);
impl_nzint!(NonZeroU128, NonZeroU128::new);
impl_nzint!(NonZeroUsize, NonZeroUsize::new);

impl_nzint!(NonZeroI8, NonZeroI8::new);
impl_nzint!(NonZeroI16, NonZeroI16::new);
impl_nzint!(NonZeroI32, NonZeroI32::new);
impl_nzint!(NonZeroI64, NonZeroI64::new);
impl_nzint!(NonZeroI128, NonZeroI128::new);
impl_nzint!(NonZeroIsize, NonZeroIsize::new);

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
macro_rules! x86_intrinsic_impl {
Expand Down Expand Up @@ -163,7 +146,7 @@ macro_rules! simd_impl {
}

#[cfg(feature = "simd_support")]
simd_impl!(u8, i8, u16, i16, u32, i32, u64, i64, usize, isize);
simd_impl!(u8, i8, u16, i16, u32, i32, u64, i64);

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
x86_intrinsic_impl!(
Expand Down Expand Up @@ -191,14 +174,12 @@ mod tests {
fn test_integers() {
let mut rng = crate::test::rng(806);

rng.sample::<isize, _>(Standard);
rng.sample::<i8, _>(Standard);
rng.sample::<i16, _>(Standard);
rng.sample::<i32, _>(Standard);
rng.sample::<i64, _>(Standard);
rng.sample::<i128, _>(Standard);

rng.sample::<usize, _>(Standard);
rng.sample::<u8, _>(Standard);
rng.sample::<u16, _>(Standard);
rng.sample::<u32, _>(Standard);
Expand Down Expand Up @@ -239,17 +220,6 @@ mod tests {
111087889832015897993126088499035356354,
],
);
#[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
test_samples(0usize, &[2220326409, 2575017975, 2018088303]);
#[cfg(target_pointer_width = "64")]
test_samples(
0usize,
&[
11059617991457472009,
16096616328739788143,
1487364411147516184,
],
);

test_samples(0i8, &[9, -9, 111]);
// Skip further i* types: they are simple reinterpretation of u* samples
Expand Down
4 changes: 1 addition & 3 deletions src/distr/uniform_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,10 @@ uniform_int_impl! { i16, u16, u32 }
uniform_int_impl! { i32, u32, u32 }
uniform_int_impl! { i64, u64, u64 }
uniform_int_impl! { i128, u128, u128 }
uniform_int_impl! { isize, usize, usize }
uniform_int_impl! { u8, u8, u32 }
uniform_int_impl! { u16, u16, u32 }
uniform_int_impl! { u32, u32, u32 }
uniform_int_impl! { u64, u64, u64 }
uniform_int_impl! { usize, usize, usize }
uniform_int_impl! { u128, u128, u128 }

#[cfg(feature = "simd_support")]
Expand Down Expand Up @@ -476,7 +474,7 @@ mod tests {
);)*
}};
}
t!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, i128, u128);
t!(i8, i16, i32, i64, u8, u16, u32, u64, i128, u128);

#[cfg(feature = "simd_support")]
{
Expand Down
20 changes: 0 additions & 20 deletions src/distr/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,6 @@ macro_rules! wmul_impl_large {
}
wmul_impl_large! { u128, 64 }

macro_rules! wmul_impl_usize {
($ty:ty) => {
impl WideningMultiply for usize {
type Output = (usize, usize);

#[inline(always)]
fn wmul(self, x: usize) -> Self::Output {
let (high, low) = (self as $ty).wmul(x as $ty);
(high as usize, low as usize)
}
}
};
}
#[cfg(target_pointer_width = "16")]
wmul_impl_usize! { u16 }
#[cfg(target_pointer_width = "32")]
wmul_impl_usize! { u32 }
#[cfg(target_pointer_width = "64")]
wmul_impl_usize! { u64 }

#[cfg(feature = "simd_support")]
mod simd_wmul {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/distr/weighted_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ mod test {
#[test]
fn overflow() {
assert_eq!(
WeightedIndex::new([2, usize::MAX]),
WeightedIndex::new([2, u32::MAX]),
Err(WeightError::Overflow)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ mod test {
#[test]
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
fn test_random() {
let _n: usize = random();
let _n: u64 = random();
let _f: f32 = random();
let _o: Option<Option<i8>> = random();
#[allow(clippy::type_complexity)]
let _many: (
(),
(usize, isize, Option<(u32, (bool,))>),
Option<(u32, (bool,))>,
(u8, i8, u16, i16, u32, i32, u64, i64),
(f32, (f64, (f64,))),
) = random();
Expand Down
4 changes: 2 additions & 2 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ macro_rules! impl_fill {
}
}

impl_fill!(u16, u32, u64, usize, u128,);
impl_fill!(i8, i16, i32, i64, isize, i128,);
impl_fill!(u16, u32, u64, u128,);
impl_fill!(i8, i16, i32, i64, i128,);

impl<T, const N: usize> Fill for [T; N]
where
Expand Down

0 comments on commit 63e823c

Please sign in to comment.