forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#107 from rust-lang/feat/simd-round
Add SIMD rounding intrinsics
- Loading branch information
Showing
4 changed files
with
136 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
macro_rules! float_rounding_test { | ||
{ $vector:ident, $scalar:tt, $int_scalar:tt } => { | ||
mod $scalar { | ||
type Vector<const LANES: usize> = core_simd::$vector<LANES>; | ||
type Scalar = $scalar; | ||
type IntScalar = $int_scalar; | ||
|
||
#[cfg(feature = "std")] | ||
test_helpers::test_lanes! { | ||
fn ceil<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::ceil, | ||
&Scalar::ceil, | ||
&|_| true, | ||
) | ||
} | ||
|
||
fn floor<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::floor, | ||
&Scalar::floor, | ||
&|_| true, | ||
) | ||
} | ||
|
||
fn round<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::round, | ||
&Scalar::round, | ||
&|_| true, | ||
) | ||
} | ||
|
||
fn trunc<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::trunc, | ||
&Scalar::trunc, | ||
&|_| true, | ||
) | ||
} | ||
|
||
fn fract<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::fract, | ||
&Scalar::fract, | ||
&|_| true, | ||
) | ||
} | ||
} | ||
|
||
test_helpers::test_lanes! { | ||
fn from_int<const LANES: usize>() { | ||
test_helpers::test_unary_elementwise( | ||
&Vector::<LANES>::round_from_int, | ||
&|x| x as Scalar, | ||
&|_| true, | ||
) | ||
} | ||
|
||
fn to_int_unchecked<const LANES: usize>() { | ||
// The maximum integer that can be represented by the equivalently sized float has | ||
// all of the mantissa digits set to 1, pushed up to the MSB. | ||
const ALL_MANTISSA_BITS: IntScalar = ((1 << <Scalar>::MANTISSA_DIGITS) - 1); | ||
const MAX_REPRESENTABLE_VALUE: Scalar = | ||
(ALL_MANTISSA_BITS << (core::mem::size_of::<Scalar>() * 8 - <Scalar>::MANTISSA_DIGITS as usize - 1)) as Scalar; | ||
|
||
let mut runner = proptest::test_runner::TestRunner::default(); | ||
runner.run( | ||
&test_helpers::array::UniformArrayStrategy::new(-MAX_REPRESENTABLE_VALUE..MAX_REPRESENTABLE_VALUE), | ||
|x| { | ||
let result_1 = unsafe { Vector::from_array(x).to_int_unchecked().to_array() }; | ||
let result_2 = { | ||
let mut result = [0; LANES]; | ||
for (i, o) in x.iter().zip(result.iter_mut()) { | ||
*o = unsafe { i.to_int_unchecked() }; | ||
} | ||
result | ||
}; | ||
test_helpers::prop_assert_biteq!(result_1, result_2); | ||
Ok(()) | ||
}, | ||
).unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
float_rounding_test! { SimdF32, f32, i32 } | ||
float_rounding_test! { SimdF64, f64, i64 } |