Skip to content

Commit b6d7d4c

Browse files
committed
Merge CompareAll and CastFromInt
1 parent 6d2b7e5 commit b6d7d4c

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

src/distributions/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use core::mem;
1414
use Rng;
1515
use distributions::{Distribution, Standard};
16-
use distributions::utils::CastFromInt;
16+
use distributions::utils::FloatSIMDUtils;
1717
#[cfg(feature="simd_support")]
1818
use core::simd::*;
1919

src/distributions/uniform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ use std::time::Duration;
116116
use Rng;
117117
use distributions::Distribution;
118118
use distributions::float::IntoFloat;
119-
use distributions::utils::{WideningMultiply, CompareAll, FloatAsSIMD, BoolAsSIMD};
119+
use distributions::utils::{WideningMultiply, FloatSIMDUtils, FloatAsSIMD, BoolAsSIMD};
120120

121121
#[cfg(not(feature = "std"))]
122122
#[allow(unused_imports)] // rustc doesn't detect that this is actually used

src/distributions/utils.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -95,46 +95,18 @@ wmul_impl_usize! { u32 }
9595
wmul_impl_usize! { u64 }
9696

9797

98-
pub trait CastFromInt<T> {
99-
fn cast_from_int(i: T) -> Self;
100-
}
101-
102-
impl CastFromInt<u32> for f32 {
103-
fn cast_from_int(i: u32) -> Self { i as f32 }
104-
}
105-
106-
impl CastFromInt<u64> for f64 {
107-
fn cast_from_int(i: u64) -> Self { i as f64 }
108-
}
109-
110-
#[cfg(feature="simd_support")]
111-
macro_rules! simd_float_from_int {
112-
($ty:ident, $uty:ident) => {
113-
impl CastFromInt<$uty> for $ty {
114-
fn cast_from_int(i: $uty) -> Self { $ty::from(i) }
115-
}
116-
}
117-
}
118-
119-
#[cfg(feature="simd_support")] simd_float_from_int! { f32x2, u32x2 }
120-
#[cfg(feature="simd_support")] simd_float_from_int! { f32x4, u32x4 }
121-
#[cfg(feature="simd_support")] simd_float_from_int! { f32x8, u32x8 }
122-
#[cfg(feature="simd_support")] simd_float_from_int! { f32x16, u32x16 }
123-
#[cfg(feature="simd_support")] simd_float_from_int! { f64x2, u64x2 }
124-
#[cfg(feature="simd_support")] simd_float_from_int! { f64x4, u64x4 }
125-
#[cfg(feature="simd_support")] simd_float_from_int! { f64x8, u64x8 }
126-
127-
128-
/// `PartialOrd` for vectors compares lexicographically. We want to compare all
129-
/// the individual SIMD lanes instead, and get the combined result over all
130-
/// lanes. This is possible using something like `a.lt(b).all()`, but we
131-
/// implement it as a trait so we can write the same code for `f32` and `f64`.
132-
/// Only the comparison functions we need are implemented.
133-
pub trait CompareAll {
134-
type Mask;
98+
/// Helper trait when dealing with scalar and SIMD floating point types.
99+
pub(crate) trait FloatSIMDUtils {
100+
// `PartialOrd` for vectors compares lexicographically. We want to compare all
101+
// the individual SIMD lanes instead, and get the combined result over all
102+
// lanes. This is possible using something like `a.lt(b).all()`, but we
103+
// implement it as a trait so we can write the same code for `f32` and `f64`.
104+
// Only the comparison functions we need are implemented.
135105
fn all_lt(self, other: Self) -> bool;
136106
fn all_le(self, other: Self) -> bool;
137107
fn all_finite(self) -> bool;
108+
109+
type Mask;
138110
fn finite_mask(self) -> Self::Mask;
139111
fn gt_mask(self, other: Self) -> Self::Mask;
140112
fn ge_mask(self, other: Self) -> Self::Mask;
@@ -143,9 +115,14 @@ pub trait CompareAll {
143115
// representable by the floating-point type. At least one of the lanes
144116
// must be set.
145117
fn decrease_masked(self, mask: Self::Mask) -> Self;
118+
119+
// Convert from int value. Conversion is done while retaining the numerical
120+
// value, not by retaining the binary representation.
121+
type UInt;
122+
fn cast_from_int(i: Self::UInt) -> Self;
146123
}
147124

148-
// Implement functions available in std builds but missing from core primitives
125+
/// Implement functions available in std builds but missing from core primitives
149126
#[cfg(not(std))]
150127
pub(crate) trait Float : Sized {
151128
type Bits;
@@ -157,7 +134,7 @@ pub(crate) trait Float : Sized {
157134
fn from_bits(v: Self::Bits) -> Self;
158135
}
159136

160-
// Implement functions on f32/f64 to give them APIs similar to SIMD types
137+
/// Implement functions on f32/f64 to give them APIs similar to SIMD types
161138
pub(crate) trait FloatAsSIMD : Sized {
162139
#[inline(always)]
163140
fn lanes() -> usize { 1 }
@@ -217,7 +194,7 @@ macro_rules! scalar_float_impl {
217194
}
218195
}
219196

220-
impl CompareAll for $ty {
197+
impl FloatSIMDUtils for $ty {
221198
type Mask = bool;
222199
#[inline(always)]
223200
fn all_lt(self, other: Self) -> bool { self < other }
@@ -236,6 +213,8 @@ macro_rules! scalar_float_impl {
236213
debug_assert!(mask, "At least one lane must be set");
237214
<$ty>::from_bits(self.to_bits() - 1)
238215
}
216+
type UInt = $uty;
217+
fn cast_from_int(i: Self::UInt) -> Self { i as $ty }
239218
}
240219

241220
impl FloatAsSIMD for $ty {}
@@ -249,7 +228,7 @@ scalar_float_impl!(f64, u64);
249228
#[cfg(feature="simd_support")]
250229
macro_rules! simd_impl {
251230
($ty:ident, $f_scalar:ident, $mty:ident, $uty:ident) => {
252-
impl CompareAll for $ty {
231+
impl FloatSIMDUtils for $ty {
253232
type Mask = $mty;
254233
#[inline(always)]
255234
fn all_lt(self, other: Self) -> bool { self.lt(other).all() }
@@ -279,6 +258,8 @@ macro_rules! simd_impl {
279258
debug_assert!(mask.any(), "At least one lane must be set");
280259
<$ty>::from_bits(<$uty>::from_bits(self) + <$uty>::from_bits(mask))
281260
}
261+
type UInt = $uty;
262+
fn cast_from_int(i: Self::UInt) -> Self { $ty::from(i) }
282263
}
283264
}
284265
}

0 commit comments

Comments
 (0)